Claude Desktop through magpie, several conversations at once: a
conversation died with "Invalid request … 400 due to tool use concurrency
issues", which Claude Code shows when the upstream answers 400 with
"messages.N: `tool_use` ids were found without `tool_result` blocks
immediately after" or "unexpected `tool_use_id` found in `tool_result`
blocks", and every later turn carried the same history, so it stayed
stuck. pairToolBlocks mended only a translated request (buildAnthropic);
an Anthropic client's own body relayed to a Messages upstream went as it
came.
toolBlocksPaired (tool_blocks_paired.go) mends it in passthrough's
Anthropic case (an API key, a relay, a plugin's fetch) and before
count_tokens, by pairToolBlocks' rule: a call the next message doesn't
answer gets the synthetic interrupted result (is_error), first in the
user message after it or in one of its own; a result answering no call of
the message before becomes the text it said; a second answer is dropped;
a result after other content of its message goes first. It works on the
raw bytes: a body whose exchange pairs comes back byte for byte (nothing
decoded into maps, nothing marshalled), and a mended body keeps every
message it didn't change, and every other member of one it did, as they
came. A Claude subscription's turn goes through the bridge, which writes
the history as text, so it was never refused for this and isn't touched.
Cause: nothing found in magpie that makes such a history. Salted call ids
(tool_use_ids.go) unsalt id and tool_use_id alike, and a stream cut
mid-reply gets an error event, not a made-up block. A reply cut or failed
after a tool_use block was told (an upstream drop or overload under
parallel load, or the user stopping it while parallel calls run) leaves
the client a call with no result; the mend is what un-sticks it.
Tests (vendor's literal refusal texts through validateExchange):
TestToolBlocksPairedLeavesAPairedBodyAsItCame (Claude Code body with
cache_control, thinking, &, key order: same bytes),
…AnswersACutCall, …OrphanResultBecomesText, …CallsWithNoUserTurnAfter,
…SecondAnswerAndOrder, TestMessagesRelayMendsABrokenToolExchange (relay
to a fake that refuses as Anthropic does: 200; a paired request reaches
it with the client's bytes), TestCountTokensMendsABrokenToolExchange.
Broken by hand:
- the two call sites taken out: the relay and count_tokens tests red with
"messages.2: `tool_use` ids were found without `tool_result` blocks
immediately after: toolu_01B".
- toolBlocksPaired decoding and re-marshalling every body: the byte tests
(unit and relay) red.
largeConversation's Anthropic fixture replaced the last call's answer
with "go on", so its last call went unanswered, which Anthropic refuses;
the mend changed it and TestUpstreamBodiesUnchanged/large-anthropic(-group)
went red. Its user's words now follow the answer, as the Chat fixture's
do since 43001be86, and the two hashes were written again. They come out
the same with the call sites taken out, so that paired large body is
relayed byte for byte with and without the mend.
Run:
- mica (macOS), at 5e4bbf0ce (this change before the rebase onto
f9492fd8e and before the fixture change): gofmt -l clean, go vet ./...
ok, GOOS=linux go vet -tags nogui ./... ok. Its GOOS=windows vet and
gateway tests stopped on "no space left on device" (mote and mica both
had under 1 GB free, other sessions' files).
- xiaomi-gpu, native Windows, at the rebased head before the fixture
change: go vet ./... ok.
- WSL magpie-desktop (Linux), rebased on f1a4de736: go vet and
GOOS=windows go vet ./internal/gateway/ ok, go test -count=1
-timeout 45m ./internal/gateway/ ok (1097s); the same at the
f9492fd8e base (1135s).
- At the pushed tree (rebased on 3be322fe4, whose ff28dc6bd changed
gateway.go away from these lines): go vet ./internal/gateway/ ok and
-run "ToolBlocksPaired|MendsABrokenToolExchange|
TestUpstreamBodiesUnchanged|TestMessages|ToolPair|TestAnthropic|Cap|
Near|Round" ok; the whole package not again. The run before the
fixture change was red only on the two large-anthropic cases (above);
its log shows #6339/#6505/#6690's doneHold panic, no test failed for it.
Not tried: a real Claude Desktop or Claude Code conversation against a
real Messages upstream; the miwanzi history itself (not shared). The
full ./... suite was not run, only internal/gateway; the gateway
tests did not run on macOS (no disk on mote or mica).