Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(sdk/go): omit unset write and session options (#6725)

bot-of-qin-ctxa586e73146e1
diff --git a/sdk/go/client_test.go b/sdk/go/client_test.go
index d9124442a..b8e5ea8c2 100644
--- a/sdk/go/client_test.go
+++ b/sdk/go/client_test.go
@@ -948,10 +948,16 @@ func TestFilesystemTagProjectionOptionsAreSent(t *testing.T) {
}
func TestBatchWriteAndDownloadBytes(t *testing.T) {
+ var wait *bool
client, closeServer := testClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method + " " + r.URL.Path {
case "POST /api/v1/content/batch-write":
body := readJSONBody(t, r)
+ if wait == nil {
+ requireBodyKeysAbsent(t, body, "wait")
+ } else if body["wait"] != *wait {
+ t.Errorf("wait = %#v, want %v", body["wait"], *wait)
+ }
if body["root_uri"] != "viking://resources/project" || body["future_flag"] != float64(0) || body["processing_mode"] != "vectors_only" || body["defer_processing"] != true {
t.Fatalf("body = %#v", body)
}
@@ -985,7 +991,7 @@ func TestBatchWriteAndDownloadBytes(t *testing.T) {
}))
defer closeServer()
- if _, err := client.BatchWrite(context.Background(), "resources/project", []BatchWriteOperation{
+ operations := []BatchWriteOperation{
{
URI: "resources/project/a.txt",
Content: String("hello"),
@@ -994,8 +1000,16 @@ func TestBatchWriteAndDownloadBytes(t *testing.T) {
TagMode: "append",
ACL: &ACLSpec{ACLMode: "restricted", Entries: []ACLEntry{{Principal: "group:engineering", Level: "write"}}},
},
- }, &BatchWriteOptions{ProcessingMode: "vectors_only", DeferProcessing: true, Extra: map[string]any{"future_flag": 0}}); err != nil {
- t.Fatal(err)
+ }
+ for _, wait = range []*bool{nil, Bool(false), Bool(true)} {
+ opts := &BatchWriteOptions{Wait: wait, ProcessingMode: "vectors_only", DeferProcessing: true, Extra: map[string]any{"future_flag": 0}}
+ if _, err := client.BatchWrite(context.Background(), "resources/project", operations, opts); err != nil {
+ t.Fatal(err)
+ }
+ opts.Extra = map[string]any{"wait": true}
+ if _, err := client.BatchWrite(context.Background(), "resources/project", operations, opts); err == nil || !strings.Contains(err.Error(), `extra cannot override "wait"`) {
+ t.Fatalf("expected wait conflict, got %v", err)
+ }
}
data, err := client.DownloadBytes(context.Background(), "resources/project/a.txt")
if err != nil {
@@ -1007,20 +1021,34 @@ func TestBatchWriteAndDownloadBytes(t *testing.T) {
}
func TestAddResourceExtra(t *testing.T) {
+ var body map[string]any
client, closeServer := testClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- body := readJSONBody(t, r)
- if body["future_flag"] != false {
- t.Fatalf("body = %#v", body)
- }
+ body = readJSONBody(t, r)
writeOK(t, w, map[string]any{"root_uri": "viking://resources/a"})
}))
defer closeServer()
- if _, err := client.AddResource(context.Background(), "https://example.com/a.md", &AddResourceOptions{
- CreateParent: Bool(false),
- Extra: map[string]any{"future_flag": false},
- }); err != nil {
- t.Fatal(err)
+ for _, opts := range []*AddResourceOptions{nil, {}, {CreateParent: Bool(false)}, {CreateParent: Bool(true)}} {
+ if opts != nil {
+ opts.Extra = map[string]any{"future_flag": false}
+ }
+ if _, err := client.AddResource(context.Background(), "https://example.com/a.md", opts); err != nil {
+ t.Fatal(err)
+ }
+ if opts == nil || opts.CreateParent == nil {
+ requireBodyKeysAbsent(t, body, "create_parent")
+ } else if body["create_parent"] != *opts.CreateParent {
+ t.Fatalf("create_parent = %#v, want %v", body["create_parent"], *opts.CreateParent)
+ }
+ if opts != nil {
+ if body["future_flag"] != false {
+ t.Fatalf("body = %#v", body)
+ }
+ opts.Extra = map[string]any{"create_parent": true}
+ if _, err := client.AddResource(context.Background(), "https://example.com/a.md", opts); err == nil || !strings.Contains(err.Error(), `extra cannot override "create_parent"`) {
+ t.Fatalf("expected create_parent conflict, got %v", err)
+ }
+ }
}
}
@@ -1104,19 +1132,48 @@ func TestSessionSendsLatestMessageAndRetentionFields(t *testing.T) {
}
func TestCreateSessionSendsExtra(t *testing.T) {
+ var body map[string]any
client, closeServer := testClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- body := readJSONBody(t, r)
- if body["future_flag"] != false {
- t.Fatalf("body = %#v", body)
- }
+ body = readJSONBody(t, r)
writeOK(t, w, map[string]any{"session_id": "session-1"})
}))
defer closeServer()
- if _, err := client.CreateSession(context.Background(), &CreateSessionOptions{
- Extra: map[string]any{"future_flag": false},
- }); err != nil {
- t.Fatal(err)
+ for _, tc := range []struct {
+ opts *CreateSessionOptions
+ want map[string]any
+ }{
+ {nil, map[string]any{}},
+ {&CreateSessionOptions{}, map[string]any{}},
+ {&CreateSessionOptions{SessionID: "s1"}, map[string]any{"session_id": "s1"}},
+ {&CreateSessionOptions{Extra: map[string]any{"future_flag": false}}, map[string]any{"future_flag": false}},
+ {
+ &CreateSessionOptions{MemoryPolicy: map[string]any{}, AutoCommitPolicy: map[string]any{}, MemoryExtractionConfig: map[string]any{}},
+ map[string]any{"memory_policy": map[string]any{}, "auto_commit_policy": map[string]any{}, "memory_extraction_config": map[string]any{}},
+ },
+ {
+ &CreateSessionOptions{AutoCommitPolicy: map[string]any{"message_count_threshold": 25}},
+ map[string]any{"auto_commit_policy": map[string]any{"message_count_threshold": float64(25)}},
+ },
+ {
+ &CreateSessionOptions{DisableAutoCommit: true, AutoCommitPolicy: map[string]any{"message_count_threshold": 25}},
+ map[string]any{"auto_commit_policy": nil},
+ },
+ } {
+ if _, err := client.CreateSession(context.Background(), tc.opts); err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(body, tc.want) {
+ t.Fatalf("body = %#v, want %#v", body, tc.want)
+ }
+ if tc.opts != nil {
+ for _, field := range []string{"memory_policy", "auto_commit_policy", "memory_extraction_config"} {
+ tc.opts.Extra = map[string]any{field: map[string]any{}}
+ if _, err := client.CreateSession(context.Background(), tc.opts); err == nil || !strings.Contains(err.Error(), fmt.Sprintf("extra cannot override %q", field)) {
+ t.Fatalf("expected %s conflict, got %v", field, err)
+ }
+ }
+ }
}
}
diff --git a/sdk/go/filesystem.go b/sdk/go/filesystem.go
index 851c27880..7d7c2a749 100644
--- a/sdk/go/filesystem.go
+++ b/sdk/go/filesystem.go
@@ -319,11 +319,13 @@ func (c *Client) BatchWrite(
"operations": normalized,
}
if opts != nil {
- setAny(payload, "wait", opts.Wait)
+ if opts.Wait != nil {
+ payload["wait"] = *opts.Wait
+ }
setAny(payload, "defer_processing", opts.DeferProcessing)
setAny(payload, "telemetry", opts.Telemetry)
setString(payload, "processing_mode", opts.ProcessingMode)
- if err := mergeExtra(payload, opts.Extra); err != nil {
+ if err := mergeExtraProtected(payload, opts.Extra, "wait"); err != nil {
return nil, err
}
}
diff --git a/sdk/go/resources.go b/sdk/go/resources.go
index 2a83b8324..9032acf57 100644
--- a/sdk/go/resources.go
+++ b/sdk/go/resources.go
@@ -27,7 +27,9 @@ func (c *Client) AddResource(ctx context.Context, path string, opts *AddResource
}
setString(payload, "to", opts.To)
setString(payload, "parent", opts.Parent)
- setAny(payload, "create_parent", opts.CreateParent)
+ if opts.CreateParent != nil {
+ payload["create_parent"] = *opts.CreateParent
+ }
setString(payload, "add_type", opts.AddType)
setString(payload, "ignore_dirs", opts.IgnoreDirs)
setString(payload, "include", opts.Include)
@@ -59,7 +61,7 @@ func (c *Client) AddResource(ctx context.Context, path string, opts *AddResource
if err := c.addLocalUpload(ctx, payload, path, true); err != nil {
return nil, err
}
- if err := mergeExtra(payload, opts.Extra); err != nil {
+ if err := mergeExtraProtected(payload, opts.Extra, "create_parent"); err != nil {
return nil, err
}
var result map[string]any
diff --git a/sdk/go/sessions.go b/sdk/go/sessions.go
index fcada7dc0..b7180ab1b 100644
--- a/sdk/go/sessions.go
+++ b/sdk/go/sessions.go
@@ -18,15 +18,19 @@ func (c *Client) CreateSession(ctx context.Context, opts *CreateSessionOptions)
}
payload := map[string]any{}
setString(payload, "session_id", opts.SessionID)
- setAny(payload, "memory_policy", opts.MemoryPolicy)
+ if opts.MemoryPolicy != nil {
+ payload["memory_policy"] = opts.MemoryPolicy
+ }
if opts.DisableAutoCommit {
payload["auto_commit_policy"] = nil
- } else {
- setAny(payload, "auto_commit_policy", opts.AutoCommitPolicy)
+ } else if opts.AutoCommitPolicy != nil {
+ payload["auto_commit_policy"] = opts.AutoCommitPolicy
+ }
+ if opts.MemoryExtractionConfig != nil {
+ payload["memory_extraction_config"] = opts.MemoryExtractionConfig
}
- setAny(payload, "memory_extraction_config", opts.MemoryExtractionConfig)
setAny(payload, "telemetry", opts.Telemetry)
- if err := mergeExtra(payload, opts.Extra); err != nil {
+ if err := mergeExtraProtected(payload, opts.Extra, "memory_policy", "auto_commit_policy", "memory_extraction_config"); err != nil {
return nil, err
}
var result map[string]any