fix: address review feedback - init ToolNameMap eagerly, log collisions, add collision test

This commit is contained in:
sususu98
2026-03-22 13:24:03 +08:00
parent 2398ebad55
commit 755ca75879
3 changed files with 18 additions and 4 deletions
+14
View File
@@ -93,6 +93,20 @@ func TestSanitizedToolNameMap(t *testing.T) {
t.Error("expected nil for nil input")
}
})
t.Run("collision keeps first mapping", func(t *testing.T) {
raw := []byte(`{"tools":[
{"name":"read/file","input_schema":{}},
{"name":"read@file","input_schema":{}}
]}`)
m := SanitizedToolNameMap(raw)
if m == nil {
t.Fatal("expected non-nil map")
}
if m["read_file"] != "read/file" {
t.Errorf("expected first mapping read/file, got %q", m["read_file"])
}
})
}
func TestRestoreSanitizedToolName(t *testing.T) {
+3
View File
@@ -8,6 +8,7 @@ import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
@@ -298,6 +299,8 @@ func SanitizedToolNameMap(rawJSON []byte) map[string]string {
}
if _, exists := out[sanitized]; !exists {
out[sanitized] = name
} else {
log.Warnf("sanitized tool name collision: %q and %q both map to %q, keeping first", out[sanitized], name, sanitized)
}
return true
})