Anthropic wire: interrupted-turn replay drops thinking signature, causes hard 400 "Invalid signature in thinking block"

未關閉
#1,062 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
52/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
typescript
領域
backend, cli

研究方向

Start by reading the streaming consumers, finalizeReasoning(), toWireMessages(), and assistantParts() described in the issue, then compare how the gateway path handles thinking metadata with consumeFullStream. Reproduce an interrupted turn and inspect the replayed assistant message. Done means the replay preserves the thinking signature and redacted data, and the interrupted request no longer fails with the reported 400.

由索引模型根據 Issue 內容生成。

描述

Summary

Anthropic-wire sessions fail with a hard 400 whenever the user sends a new message while the model is still streaming. The error is unavoidable in that scenario and the only recovery is continue or restarting the session:

Error: 400 Invalid_request_error Upstream request failed: [invalid_request_error]
messages.1.content.0: Invalid signature in thinking block. The block is bound to
a different conversation. Remove the block, or set
`thinking.block_binding.prefix_mismatch_behavior` ...

Trace ID: 907c498bc0028b25be7ae3dc058394f7

Environment

  • Command Code 1.87.3 (dist/cli.mjs)
  • Linux
  • Custom BYOK provider in ~/.commandcode/providers.json with "api": "anthropic-messages"
  • Model: claude-haiku-5-5

Steps to reproduce

  1. Configure a BYOK provider using the Anthropic Messages wire:

    {
      "provider": {
        "opencode": {
          "baseURL": "https://opencode.ai/inference/go/anthropic",
          "models": {
            "claude-haiku-5-5": { "api": "anthropic-messages" }
          }
        }
      }
    }
    
  2. Start a session on that model and let it produce a turn containing real thinking.

  3. While the turn is still streaming, type and submit a new message.

  4. The request fails with the 400 above, every time.

Root cause (located in dist/cli.mjs, v1.87.3)

Anthropic thinking blocks carry a signature that is bound to one specific assistant prefix. Command Code has two streaming consumers and only one of them round-trips that signature:

  • consumeFullStream (AI SDK path, used by runAiSdkStream for custom providers): finalizeReasoning pushes {type:"thinking", thinking:c, signature:u} where u comes from readReasoningMetadata() — the real signature is preserved and replayed. This path works.
  • consumeStream (Command Code gateway /alpha/generate path): finalizeReasoning hardcodes {type:"thinking", thinking:u, signature:""}. Additionally toWireMessages() serializes the thinking block as only {type:"reasoning", text:n.thinking} — the signature and redactedData fields are dropped entirely, so the gateway never receives them.

When a turn is interrupted mid-stream, withResumedAssistantTurn() rebuilds the partial assistant turn via assistantParts(). That helper only emits a reasoning part when r.signature || r.redactedData is truthy. On the gateway path the signature is always "", so the replayed assistant turn goes out with a thinking block that has no signature (or an empty one). The upstream rejects it as "bound to a different conversation".

Verification performed

I ran a local fake Anthropic endpoint and captured the actual request bodies with cmd -p --resume:

  • Plain --resume continuation on a custom anthropic-messages provider: the 64-character signature is fully replayed (messages[1] thinking block carries it), and the call succeeds. So ordinary multi-turn history is fine.
  • The failure is specific to the interrupted-turn replay / gateway path, where the signature is lost as described above.

This confirms the defect is in the interrupted-replay serialization, not in the BYOK provider, the proxy, or normal history handling.

Relationship to existing issues

This looks like the same class of defect as #877 and #887 (BYOK thinking/reasoning content dropped from replayed history). The fix referenced in #877 (PR #5283) does not appear to be present in the published 1.87.3 build — toWireMessages() still omits signature completely.

Note that @ai-sdk/anthropic 3.0.135 (already a dependency of 1.87.3) supports the escape hatch:

"thinking": { "blockBinding": { "prefixMismatchBehavior": "drop_block" } }

which automatically adds the thinking-binding-controls-2026-08-01 beta and makes the upstream drop a mismatched block instead of erroring. Command Code's thinkingHook() never sets this, so the option is currently unreachable from the client.

Expected behavior

Either:

  1. Preserve and replay signature / redactedData on the consumeStream gateway path (match what consumeFullStream already does), or
  2. Set thinking.blockBinding.prefixMismatchBehavior: "drop_block" for Anthropic-wire models so a stale signature degrades gracefully instead of producing a hard 400.

Option 1 is the correct fix; option 2 would at least stop the session from dying.

Workaround for affected users

  • Do not send messages while a turn is still streaming; wait for it to finish.
  • If you must interrupt, start a fresh process with cmd -p "..." --resume <sessionId>, which takes the signature-preserving path.
主要語言
沒有語言資料
星號
4.2k
分支
377
平均合併
1 天 17 分鐘
30 天內合併 PR
7

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

CommandCodeAI/command-code 的其他 Issue

查看 CommandCodeAI/command-code 的全部 Issue

相似的 Issue

更多 Backend & API Design Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。