From 6d3bc24db76518c9e4607f941d79710a58c91823 Mon Sep 17 00:00:00 2001 From: yowucai Date: Thu, 3 Dec 2026 11:37:53 +0800 Subject: [PATCH] fix(assistant): deduplicate and sort submit_assistant_reply tool enum --- .../agent_tool_native_terminal_test.go | 87 +++++++++++++++++++ internal/httpapi/agent_tool_terminal.go | 20 ++++- 2 files changed, 104 insertions(+), 3 deletions(-) diff --git a/internal/httpapi/agent_tool_native_terminal_test.go b/internal/httpapi/agent_tool_native_terminal_test.go index 347fe1ddf..dd4870998 100644 --- a/internal/httpapi/agent_tool_native_terminal_test.go +++ b/internal/httpapi/agent_tool_native_terminal_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "os" "path/filepath" + "slices" "strconv" "strings" "testing" @@ -159,3 +160,89 @@ func TestAssistantDeferredWorkCannotDependOnSessionCron(t *testing.T) { t.Fatal("missing durable scheduling contract") } } + +func TestSubmitAssistantReplySchemaEnumUniqueAndSorted(t *testing.T) { + var assertUniqueEnums func(path string, node any) + assertUniqueEnums = func(path string, node any) { + switch v := node.(type) { + case map[string]any: + if rawEnum, ok := v["enum"]; ok { + items, ok := rawEnum.([]any) + if !ok { + t.Fatalf("%s.enum is not an array: %T", path, rawEnum) + } + seen := make(map[string]struct{}, len(items)) + for _, item := range items { + key, _ := json.Marshal(item) + s := string(key) + if _, dup := seen[s]; dup { + t.Fatalf("%s.enum contains duplicate value %s", path, s) + } + seen[s] = struct{}{} + } + } + for k, child := range v { + assertUniqueEnums(path+"."+k, child) + } + case []any: + for i, child := range v { + assertUniqueEnums(path+"["+strconv.Itoa(i)+"]", child) + } + } + } + + execModes := []assistantExecMode{ + {}, + {AutoWrite: true, ConfirmDangerous: true}, + {AutoWrite: true, ReviewWrites: true}, + {AutoWrite: true}, + } + for _, mode := range []AgentToolMode{AgentToolReport, AgentToolClarify, AgentToolAssistant} { + for _, exec := range execModes { + for _, desc := range terminalToolDescriptors(mode, exec) { + var parsed any + if err := json.Unmarshal(desc.InputSchema, &parsed); err != nil { + t.Fatalf("mode=%s tool=%s invalid schema json: %v", mode, desc.Name, err) + } + assertUniqueEnums(desc.Name, parsed) + } + } + } + + raw, err := json.Marshal(submitAssistantReplySchema(assistantExecMode{})) + if err != nil { + t.Fatal(err) + } + var parsed struct { + Properties struct { + Drafts struct { + Items struct { + Properties struct { + Tool struct { + Enum []string `json:"enum"` + } `json:"tool"` + } `json:"properties"` + } `json:"items"` + } `json:"drafts"` + } `json:"properties"` + } + if err := json.Unmarshal(raw, &parsed); err != nil { + t.Fatal(err) + } + enum := parsed.Properties.Drafts.Items.Properties.Tool.Enum + if len(enum) == 0 { + t.Fatal("drafts[].tool.enum must not be empty") + } + if !slices.IsSorted(enum) { + t.Fatalf("drafts[].tool.enum is not sorted: %v", enum) + } + count := 0 + for _, name := range enum { + if name == "assistant_tasks_save" { + count++ + } + } + if count != 1 { + t.Fatalf("assistant_tasks_save appeared %d times in drafts[].tool.enum, want 1", count) + } +} diff --git a/internal/httpapi/agent_tool_terminal.go b/internal/httpapi/agent_tool_terminal.go index c82e8803b..421440d5c 100644 --- a/internal/httpapi/agent_tool_terminal.go +++ b/internal/httpapi/agent_tool_terminal.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "sort" "strings" "unicode/utf8" @@ -111,11 +112,23 @@ func submitAssistantReplySchema(exec assistantExecMode) map[string]any { names := assistantWriteToolNameList() toolSchema := map[string]any{"type": "string", "description": "必填:本草稿要执行的写入工具名称,如 issues_save、issues_dispatch;不要省略,不带 mcp__ 前缀"} if len(names) > 0 { - enum := make([]any, 0, len(names)+1) + seen := make(map[string]struct{}, len(names)+1) + ordered := make([]string, 0, len(names)+1) for _, name := range names { - enum = append(enum, name) + if _, ok := seen[name]; ok { + continue + } + seen[name] = struct{}{} + ordered = append(ordered, name) + } + if _, ok := seen["assistant_tasks_save"]; !ok { + ordered = append(ordered, "assistant_tasks_save") + } + sort.Strings(ordered) + enum := make([]any, len(ordered)) + for i, name := range ordered { + enum[i] = name } - enum = append(enum, "assistant_tasks_save") toolSchema["enum"] = enum } draft := map[string]any{ @@ -153,6 +166,7 @@ func assistantWriteToolNameList() []string { for name := range assistantWriteOperations { names = append(names, name) } + sort.Strings(names) return names }