Commit Graph
2 Commits
Author SHA1 Message Date
yetone a6a99717e0 gateway: thinking with no signature never goes to Claude
citricdu (Discord, 0.1.3356): Claude Code auto-compacted a session on a
Smart group that had moved to Cursor's Grok 4.7, then the group went
back to Kiro's Claude Opus 5.5. The compacted history kept a thinking
block Grok made, which magpie hands a Messages client signed "", and
Kiro answered 13 times in a row with a bare invalid_request_error. The
same request without the block was answered.

withoutForeignThinking dropped only DeepSeek's UUID-signed blocks and
kept an empty signature on purpose ("a block with no signature Claude
takes"), and Kiro's refusal names nothing boundRefusal reads, so
refuseBoundThinking never mended it. Now every thinking block whose
signature is empty, blank or missing is left out for an upstream
serving Claude (api.anthropic.com, or a claude- model: relays, Bedrock
and Vertex ids, a plugin's fetch such as Kiro's), on every route
forwardOnce takes (relayed, translated, plugin, count_tokens), and
refuseBoundThinking runs it too. Text, tool_use and tool_result stay;
Claude's own signed thinking and redacted_thinking are untouched. A
model that learned thinkingBackNeeded (DeepSeek under another name)
keeps its unsigned blocks. Chat and Responses translated into Messages
already dropped unsigned thinking in anthropicBody.

Tests: TestUnsignedThinkingNotSentToClaude builds the reporter's shape
(the compaction summary, then two tool turns opening with Grok's
thinking signed "") against a fake Kiro that gives the same bare 400,
relayed and through the fake plugin's fetch (FAKE_KIRO model added).
TestGrokThinkingThenClaude runs the switch itself: a group answered by
a Chat Grok, then moved to Claude mid tool loop.
TestWithoutForeignThinking adds an unsigned block, Bedrock and Vertex
ids, a missing and a blank signature.

Run (mote, at this head rebased on 136178803):
- gofmt -l: clean; go vet ./internal/gateway: clean
- go test -run 'TestUnsignedThinkingNotSentToClaude|TestGrokThinkingThenClaude|TestWithoutForeignThinking|TestDeepSeekThinkingThenClaude|Bound|TestPluginBoundThinkingLeftOut|ThinkingBack' ./internal/gateway: ok
Broken by hand: the old predicate (DeepSeek signatures only, the
"signature" precheck only) put back: TestWithoutForeignThinking,
TestUnsignedThinkingNotSentToClaude/relayed and
TestGrokThinkingThenClaude fail (Kiro relay 400 "Improperly formed
request."). The plugin subtest passes on the old code too.
Not run before push: the full suite and the Linux/Windows vet; they
are queued behind other sessions' suites, and their result follows.
Not tried: a real Kiro or Anthropic account; Kiro's exact error text
is the reporter's description, not captured bytes.

Reported-by: citricdu (Discord)
2026-11-09 17:24:09 +08:00
yetone f3335dd1b4 gateway: DeepSeek's thinking is left out of a request to Claude (GgbondHi6930 on X)
GgbondHi6930 on X: a Smart group of DeepSeek and a Claude API key. A
conversation ran on DeepSeek at max effort; with DeepSeek unticked its
next turn went to Claude and failed "signature in thinking". A new
conversation worked.

Cause: DeepSeek's Anthropic endpoint signs each thinking block with its
reply's id, a UUID (checked against api.deepseek.com/anthropic). The
agent keeps those blocks and sends them back, so the first Claude turn
carries them, and Claude refuses the request, 400 "messages.1.content.0:
Invalid `signature` in `thinking` block" (real reply through
OpenRouter's Anthropic route, req_011CgfP4zyRcUhPiTg5sLpoe). Until now
magpie mended this only after the refusal, in forward's bound-thinking
retry. That needs the refusal to reach forward as a 400/422 with
Claude's wording, and it spends a request. With fakes that answer as
Claude does, main recovers through it, on Messages (whole and streamed)
and on Codex's Responses. So the reporter's failure is where that retry
can't read the refusal. A relay's status or wording is the likely case;
it is not reproduced exactly.

Change: forwardOnce, which every route to an Anthropic upstream passes
(relayed, translated Chat/Responses, group fallback and Smart routing,
a plugin's fetch, count_tokens), now calls withoutForeignThinking. For
an upstream serving Claude (api.anthropic.com, or a claude- model name)
and not DeepSeek, it leaves out each thinking block whose signature is a
UUID. Those blocks never reach Claude, and refuseBoundThinking reads a
refusal's indices against that same body. These go byte for byte:
- Claude's own signatures (base64; 3,000 of Claude Code's own were
  checked, all base64 and 800+ characters);
- unsigned blocks (Claude took one in a real request);
- redacted_thinking;
- other vendors' signatures, which the retry still mends.
DeepSeek's endpoint and every other upstream get the body unchanged.

Tests (thinking_switch_test.go):
- TestDeepSeekThinkingThenClaude: the reporter's case, whole and
  streamed. 3 DeepSeek tool turns, then DeepSeek unticked and 3 Claude
  turns. Claude refuses nothing and gets its own blocks back as signed.
  With DeepSeek ticked again and Claude unticked, DeepSeek is sent every
  block, its own and Claude's.
- TestDeepSeekThinkingThenClaudeResponses: the same through Codex's
  /v1/responses.
- TestWithoutForeignThinking: Claude, DeepSeek and Kimi upstreams.

Run on mote (mote-run):
- gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./... and
  GOOS=windows go vet ./...: clean, at cfa9a6987 (base 17d2ef10f).
- go test -timeout 45m ./internal/gateway/... ./internal/provider/...
  at cfa9a6987: provider ok. Gateway ok except
  TestCodexPoolSaysTheClientsVersion, which is #4807 (passes alone 3/3;
  commented there). It was fixed on main by 17f2387e1, which this commit
  is rebased onto.
- After the rebase onto 17f2387e1, which touched none of these files'
  code: -run 'Thinking|Bound|TestCodexPoolSaysTheClientsVersion' ok.

Broken by hand: with withoutForeignThinking returning the body as it
came, all three tests go red ("Claude refused 1 of its 4 requests",
"Claude refused 1 of its 3 requests", "message 1 = [thinking,text]").

Not tried:
- a real Claude account through magpie; no Anthropic key was usable
  here, and the relay accounts found (aihubmix, yylx) are out of credit;
- the reporter's own relay;
- Chat Completions clients, which carry no signatures;
- Kimi, GLM and MiniMax signatures, which the refusal path still mends.
2026-11-03 13:16:35 +08:00