Files
magpie/internal/gateway/textcalls_tail_test.go
T
yetone 39bade71d1 gateway: a plain <invoke> written into a reply's text is the call it is, on a relayed Messages reply too
packinggoodsllc_07668 on Discord (#general): Claude Code v2.1.316, magpie's
routing group "Hard to strong, easy to cheap", asked to open Google in
magpie's browser. The reply showed
  <invoke name="ToolSearch"><parameter name="query">select:…</parameter></invoke>
  *Result: Tool loaded.*
  <invoke name="mcp__magpie__magpie-browser__browser_navigate">…</invoke>
  *Result: Navigated to https://www.google.com — page title: "Google"*
  已用 magpie 的浏览器打开了谷歌 ...
as text. Nothing ran: the model wrote its calls and made up their results.
Asked, the reporter said the group holds only Claude models (v0.1.2545).

Cause: Claude wrote its own call form as text, and none of magpie's
readers of written calls knew that form on the routes Claude takes.
- The plain <invoke name=…><parameter name=…> shape (Claude's own calls,
  MiniMax's in <minimax:tool_call>, Claude's in <function_calls>) wasn't
  a mark: only <tool_call> and DeepSeek's DSML invoke were.
- A reply relayed as it came on the Messages API (a group member on a
  provider that speaks Messages itself: MiniMax, Kimi, GLM, DeepSeek, Claude
  relays) was never read for written calls at all; the subsystem doc said
  so. Claude Code's request to such a member takes that route.
- A Claude subscription member runs Claude Code itself (the bridge), whose
  textCalls held back only "[tool call …]" lines.
Not the cause: history isn't flattened (tool_use/tool_result translate to
native calls on every API), and this was the conversation's first turn.

Fix:
- textcalls.go: the invoke regexes take DSML's pipes, a namespace, or
  nothing; <invoke name=, <function_calls> and <minimax:tool_call> are
  marks; their leftover tags go. Text after a call read out, other than
  tags, ends the calls and goes with all after it: it is the model going
  on as if the call had run. This changes the old rule for every shape:
  prose after a Hermes call ("Done.") went on as text and now goes
  (TestTextWrittenCallCutEarlyNotCalled updated).
- textcalls_anthropic.go: anthropicTextCalls in passthrough, for a
  streamed Messages reply (holds the text block's start and stop with the
  held text, adds tool_use blocks after it or in its place, stop_reason
  tool_use) and a whole one.
- claude_text_calls.go: the bridge's textCalls also holds a line opening
  <invoke name= or <function_calls>, so the run is asked once to make the
  calls (textCallsAsk no longer names the "[tool call …]" form only).
- Responses and Gemini relays still aren't read: #4743.

Tests (textcalls_invoke_test.go), on the reporter's literal text: parse,
a relayed Chat stream, a Messages passthrough through a routing group
(streamed, with lead text, a lookalike left alone, and whole), and a
Chat upstream translated for a Messages client.
TestClaudeTextInvokeCallsAreMadeForReal: the bridge, bare and wrapped.

Run:
- On origin/main 586b59fe7 with only the new test file: all four
  TestInvokeReported* red. With the parser change but not the passthrough
  wiring: TestInvokeReportedAnthropicPassthrough red.
- mote-run at a6c37f936 (on origin/main 9feb1024c): gofmt -l clean, go vet ./internal/gateway for darwin, linux, windows ok, go test ./internal/gateway -timeout 45m -count=1 ok 716.8s.
- Rebased onto origin/main 5d63b3f99 (5 commits, two in internal/gateway: Codex
  loaded tools, HEIC images; no file of this change): gofmt clean, vet ok,
  and -run TestInvokeReported|TestTextWritten|TestTextCall|TestClaudeText|
  TestWritten|Passthrough ok. The full gateway suite was not rerun there.
Not tried: a real Claude Code against a real group member that writes
invokes (which provider serves the reporter's Claude models is unknown), and Responses/Gemini relays.
2026-11-01 15:08:24 +08:00

54 lines
2.7 KiB
Go

package gateway
import (
"encoding/json"
"strings"
"testing"
)
// #1945 (XcodeFish, GLM under Codex): what GLM leaves between a call's
// JSON and its </tool_call> went out as text beside the call. The tails are
// the ones the issue lists, as Codex showed them.
func TestTextWrittenCallTailDropped(t *testing.T) {
for _, tail := range []string{"]", "}", `]}]}}"}}}}`, "}}", `">`, "\n}}"} {
text, calls, finish, raw := relayedChat(t, `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}],`+textCallTools+`}`,
chunkOf("Listing it.\n<tool_call>"),
chunkOf(`{"name":"bash","arguments":{"command":"ls"}}`+tail),
chunkOf("</tool_call>"),
stopChunk)
if text != "Listing it.\n" || calls["bash"] != `{"command":"ls"}` || finish != "tool_calls" {
t.Errorf("%q: text %q calls %v finish %q\n%s", tail, text, calls, finish, raw)
}
}
// and a tail after a call's own close
text, calls, _, raw := relayedChat(t, `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}],`+textCallTools+`}`,
chunkOf(`<tool_call>{"name":"bash","arguments":{"command":"ls"}}</tool_call>`+"\n"+`}}</tool_call>`),
stopChunk)
if text != "" || calls["bash"] != `{"command":"ls"}` {
t.Errorf("after the close: text %q calls %v\n%s", text, calls, raw)
}
}
// A heredoc whose quotes the model left unescaped: the JSON object closed
// early, at a quote inside the command, and the command went to the agent
// cut short (#1945). Such a block is no call: the text goes on as it was,
// for the agent to see and the model to write again.
func TestTextWrittenCallCutEarlyNotCalled(t *testing.T) {
block := `<tool_call>{"name":"bash","arguments":{"command":"python3 - <<'PY'\nimport json\nprint(json.dumps({\"名\": 'v', \"n\": {\"x\": \"y"}}))\nPY\n | tail -8","timeout":180000}}</tool_call>`
text, calls, finish, raw := relayedChat(t, `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}],`+textCallTools+`}`,
chunkOf(block), stopChunk)
if len(calls) != 0 || finish != "stop" || text != block {
var a map[string]any
json.Unmarshal([]byte(calls["bash"]), &a)
t.Fatalf("text %q calls %v (%v) finish %q\n%s", text, calls, a, finish, raw)
}
// prose after a call with no close of its own: the call still reads
// as before; the prose, said before anything ran, goes
// (packinggoodsllc_07668, parseTextCalls)
text, calls, _, _ = relayedChat(t, `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}],`+textCallTools+`}`,
chunkOf(`<tool_call>{"name":"bash","arguments":{"command":"ls"}}`+"\nDone."), stopChunk)
if calls["bash"] != `{"command":"ls"}` || strings.Contains(text, "Done.") {
t.Fatalf("no close: text %q calls %v", text, calls)
}
}