Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions internal/httpapi/agent_tool_native_terminal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -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)
}
}
20 changes: 17 additions & 3 deletions internal/httpapi/agent_tool_terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"sort"
"strings"
"unicode/utf8"

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -153,6 +166,7 @@ func assistantWriteToolNameList() []string {
for name := range assistantWriteOperations {
names = append(names, name)
}
sort.Strings(names)
return names
}

Expand Down
Loading