Files
magpie/internal/gateway/bridge_object_root_test.go
T
yetone ef73a0b1ca claude bridge: a tool whose schema has a oneOf at its root reaches the model (Lee931018 on X)
Through the Claude subscription bridge, Codex desktop's browser tool
mcp__cua_repl__js never reached a Claude model; mcp__cua_repl__js_reset
did. The tools.json the run's Claude Code is handed carried each
inputSchema as the client sent it, and cua_repl__js's is a oneOf of
objects at the root, which Anthropic's API refuses, so Claude Code left
the tool out.

bridgeTools, the one place the bridge's tools are built (the first
tools.json and the tools a ToolSearch loads mid-turn), now folds each
schema with objectSchema / provider.ObjectRoot, as every Messages request
magpie builds already is (anthropic.go) and as the gateway does for
relays and xAI (object_root.go): the variants' properties merged, a field
every variant requires stays required, allOf's required all kept. A
schema with an object root goes byte for byte as it came. The checks of
a call against the original schema (answer tool) are unchanged.

Paths checked: Messages built from a translated request (objectSchema in
anthropic.go), a Messages body passed through (objectRootsBody, folded
for every upstream), Chat/Responses to relays (foldsRoots + retry on
rootUnionRefusal), Grok (grokBody). The bridge was the only one left.

Run (mote, at 48bea11dc): gofmt; go vet ./internal/gateway on darwin,
linux (nogui, CGO off) and windows; go test -run
'TestClaudeBridge|ObjectRoot|OneOf|BridgeTools|ToolNames'
./internal/gateway: ok.
Broken by hand: without the objectSchema line in bridgeTools,
TestClaudeBridgeToolsHaveObjectRoots fails (oneOf/anyOf/allOf kept at
the root, type missing).
Not tried: a real Codex desktop with the cua browser plugin and a real
Claude Code run; the whole internal/gateway package is queued behind
mote's suite lock and runs after the tag.
2026-11-10 11:13:58 +08:00

64 lines
2.5 KiB
Go

package gateway
import (
"encoding/json"
"slices"
"testing"
)
// Lee931018 on X: through the Claude bridge, Codex desktop's browser tool
// mcp__cua_repl__js, whose inputSchema is a oneOf of objects at its root,
// never reached the model, since Claude Code leaves out a tool whose root
// Anthropic's API refuses. The tools.json the run's Claude Code is handed
// has every schema with an object root, its variants' fields merged; one
// that already has one goes as it came.
func TestClaudeBridgeToolsHaveObjectRoots(t *testing.T) {
js := json.RawMessage(`{"oneOf":[` +
`{"type":"object","properties":{"code":{"type":"string"},"timeout_ms":{"type":"number"}},"required":["code"],"additionalProperties":false},` +
`{"type":"object","properties":{"code":{"type":"string"},"title":{"type":"string"}},"required":["code","title"],"additionalProperties":false}` +
`],"$schema":"http://json-schema.org/draft-07/schema#"}`)
for _, root := range []string{"oneOf", "anyOf", "allOf"} {
schema := json.RawMessage(string(js[:2]) + root + string(js[7:]))
plain := json.RawMessage(`{"type":"object","properties":{},"additionalProperties":false}`)
req := &Request{Tools: []Tool{{Name: "mcp__cua_repl__js", Schema: schema}, {Name: "mcp__cua_repl__js_reset", Schema: plain}}}
offer := bridgeTools(req)
if len(offer) != 2 {
t.Fatalf("%s: offered %d tools, want 2", root, len(offer))
}
var m map[string]any
if err := json.Unmarshal(offer[0].InputSchema, &m); err != nil {
t.Fatal(err)
}
for _, k := range []string{"oneOf", "anyOf", "allOf"} {
if _, ok := m[k]; ok {
t.Errorf("%s: the js tool's schema keeps %s at its root: %s", root, k, offer[0].InputSchema)
}
}
if m["type"] != "object" {
t.Errorf("%s: the js tool's root type is %v, want object: %s", root, m["type"], offer[0].InputSchema)
}
props, _ := m["properties"].(map[string]any)
for _, k := range []string{"code", "timeout_ms", "title"} {
if _, ok := props[k]; !ok {
t.Errorf("%s: the js tool lost %s: %s", root, k, offer[0].InputSchema)
}
}
var req1 []string
rs, _ := m["required"].([]any)
for _, r := range rs {
req1 = append(req1, r.(string))
}
slices.Sort(req1)
want := []string{"code"}
if root == "allOf" {
want = []string{"code", "title"}
}
if !slices.Equal(req1, want) {
t.Errorf("%s: required %v, want %v", root, req1, want)
}
if string(offer[1].InputSchema) != string(plain) {
t.Errorf("%s: a schema with an object root changed: %s", root, offer[1].InputSchema)
}
}
}