From 6c099edd39de412daf6aaef6b899fb6691365cee Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 10 Oct 2026 21:39:40 +0800 Subject: [PATCH] =?UTF-8?q?gateway:=20a=20conversation=20with=20another=20?= =?UTF-8?q?vendor's=20call=20ids=20("Bash:0#=E2=80=A6")=20goes=20on=20to?= =?UTF-8?q?=20a=20Messages=20upstream=20with=20tool=5Fuse=20ids=20Anthropi?= =?UTF-8?q?c=20takes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sibling of the previous commit (echo_ts's colon-joined item ids). When a Codex or Chat conversation moves to a Messages upstream, buildAnthropic put each call's id in tool_use.id and tool_result.tool_use_id as it came. Anthropic takes those only of [A-Za-z0-9_-] and refuses the whole request over one with another character, which another vendor's calls can have (Devin's "Bash:0#a65b…", #1304; a relay's or a plugin's). Both now go through anthropicOutID, the encoding Anthropic clients already get (mp_ + base64url, stateless, the same on every request), so a call and its result keep matching; a safe id (toolu_…, call_…) goes byte for byte, and an id a Claude Code client sent back as mp_… is decoded on the way in and encoded the same way out. Test: TestAnthropicUpstreamTakesAnotherVendorsCallIDs (Codex input with Devin's literal call id and a toolu_ one, to a Messages upstream). On the old code it fails: tool_use "Bash:0#a65b6a5e02194b87bbc796c4428c946d". Run: go test -tags nogui ./... (sandbox HOME, pinned caches; every package ok but internal/gateway, which hit the 10m default at load average ~145, then passed alone with -timeout 40m in 683s), gofmt -l, GOOS=linux / GOOS=windows go vet -tags nogui ./internal/gateway/. Not run against Anthropic's real API. --- internal/gateway/anthropic.go | 7 ++- .../gateway/anthropic_upstream_ids_test.go | 61 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 internal/gateway/anthropic_upstream_ids_test.go diff --git a/internal/gateway/anthropic.go b/internal/gateway/anthropic.go index 2fcc23fcf..7c6a77991 100644 --- a/internal/gateway/anthropic.go +++ b/internal/gateway/anthropic.go @@ -674,7 +674,10 @@ func buildAnthropic(r *Request, model string) []byte { case Image: rest = append(rest, imageBlock(p)) case ToolCall: - rest = append(rest, aBlock{Type: "tool_use", ID: p.ID, Name: p.Name, Input: argsOf(p)}) + // Anthropic takes a tool_use id only of [A-Za-z0-9_-]: + // another vendor's call (Devin's "Bash:0#…") goes as + // anthropicOutID makes it, its result named the same + rest = append(rest, aBlock{Type: "tool_use", ID: anthropicOutID(p.ID), Name: p.Name, Input: argsOf(p)}) case ToolResult: c, _ := json.Marshal(p.Text) if len(p.Images) > 0 { @@ -688,7 +691,7 @@ func buildAnthropic(r *Request, model string) []byte { } c, _ = json.Marshal(blocks) } - results = append(results, aBlock{Type: "tool_result", ToolUseID: p.CallID, Content: c, IsError: p.IsError}) + results = append(results, aBlock{Type: "tool_result", ToolUseID: anthropicOutID(p.CallID), Content: c, IsError: p.IsError}) case Thinking: if p.sealedBy(sealAnthropic) { rest = append(rest, aBlock{Type: "redacted_thinking", sealed: p.Sealed}) diff --git a/internal/gateway/anthropic_upstream_ids_test.go b/internal/gateway/anthropic_upstream_ids_test.go new file mode 100644 index 000000000..fa82c5e83 --- /dev/null +++ b/internal/gateway/anthropic_upstream_ids_test.go @@ -0,0 +1,61 @@ +package gateway + +import ( + "encoding/json" + "regexp" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// A Codex conversation that had another vendor's tool call, its call_id +// with a character Anthropic doesn't take (Devin's "Bash:0#…", #1304), went +// on to a Messages upstream with that id as the tool_use's, which Anthropic +// turns away ("tool_use.id: String should match pattern +// '^[a-zA-Z0-9_-]+$'"). It goes as one Anthropic takes, its result named +// the same, and a safe id goes as it was. +func TestAnthropicUpstreamTakesAnotherVendorsCallIDs(t *testing.T) { + f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg_1","type":"message","role":"assistant","model":"m1","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}`} + setup(t, provider.Anthropic, f) + codexPostTo(New(), `{"model":"fake/m1","input":[ + {"type":"message","role":"user","content":[{"type":"input_text","text":"list files"}]}, + {"type":"function_call","call_id":"`+devinRawID+`","name":"shell","arguments":"{\"cmd\":\"ls\"}"}, + {"type":"function_call_output","call_id":"`+devinRawID+`","output":"main.go"}, + {"type":"function_call","call_id":"toolu_01AbC","name":"shell","arguments":"{\"cmd\":\"pwd\"}"}, + {"type":"function_call_output","call_id":"toolu_01AbC","output":"/src"}]}`) + var q struct { + Messages []struct { + Content []struct { + Type string `json:"type"` + ID string `json:"id"` + ToolUseID string `json:"tool_use_id"` + } `json:"content"` + } `json:"messages"` + } + if err := json.Unmarshal(f.got, &q); err != nil { + t.Fatalf("%v: %s", err, f.got) + } + ok := regexp.MustCompile(`^[a-zA-Z0-9_-]+$`) + var uses, results []string + for _, m := range q.Messages { + for _, c := range m.Content { + switch c.Type { + case "tool_use": + uses = append(uses, c.ID) + case "tool_result": + results = append(results, c.ToolUseID) + } + } + } + if len(uses) != 2 || len(results) != 2 { + t.Fatalf("tool_use %v, tool_result %v: %s", uses, results, f.got) + } + for i := range uses { + if !ok.MatchString(uses[i]) || uses[i] != results[i] { + t.Errorf("tool_use %q, its result %q", uses[i], results[i]) + } + } + if uses[1] != "toolu_01AbC" { + t.Errorf("safe id sent as %q", uses[1]) + } +}