Skip to content

fix(ai): send thinking back exactly as the endpoint streamed it - #68182

Merged
rekram1-node merged 5 commits into
devfrom
replay-own-thinking
Dec 4, 2026
Merged

rekram1-node merged 5 commits into
devfrom
replay-own-thinking

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Fixes #68060, following up on #68071 (comment).

GLM, DeepSeek and other models behind custom Anthropic-compatible gateways stream thinking blocks with signature: "". On every later turn we dropped those blocks unless the model or base URL was in the hardcoded list (Kimi, MiMo, qwen, minimax-) or compatibility.requireSignature: false was set, so the model lost its earlier reasoning.

Now we send reasoning back the way the endpoint gave it to us. The decoder already records a streamed signature: "" in the reasoning's provider metadata, so the lowering can tell "this route sent an empty signature" apart from "no signature at all":

 reasoning part lowered for Anthropic Messages:
   signed                                   -> thinking with that signature
   compatibility.requireSignature set       -> obey it
+  this route's metadata says signature ""  -> thinking with signature ""
   no signature metadata (other model)      -> hardcoded list decides, else drop

The decoder also keeps an empty signature_delta, which it used to ignore, so a gateway that leaves the signature out of content_block_start and sends an empty delta later is handled the same way. The first signature_delta for a block replaces the start signature and later ones append to it, so an empty delta after a real signature leaves the real one in place. Anthropic sends one delta per block, so Claude is unaffected.

Why this doesn't send "" to Anthropic. Claude streams signature: "" in content_block_start and then replaces it with a signature_delta before the block closes, so a completed Claude block always ends with a real signature. Only a block cut off mid-stream still has "". Core's toLLMMessages already drops reasoning from a cut-off stream unless the model moved on to output, and Anthropic only moves on after the thinking block closes. Reasoning from another model reaches the lowering without provider metadata, so the old strict path still applies to it. An explicit requireSignature: true, which the Vercel AI Gateway sets for anthropic/ models, still drops these blocks.

Safety net: a route that rejects ""

A gateway in front of Claude that strips signatures would now get "" back, and Anthropic answers 400 messages.N.content.M: Invalid `signature` in `thinking` block. Without handling, every later turn on that session would fail. So the session falls back to the old behavior for that route, once, and the server remembers it until it restarts:

step fails before any output
  and the error is classified invalid-reasoning
  and the request sent thinking whose metadata says signature ""
  and config didn't set requireSignature
  and this step hasn't done this yet
-> remember the route in memory, log a warning, retry the step
-> the retry resolves the model with requireSignature: true, so unsigned thinking is dropped
  • Classification (packages/ai/src/provider-error.ts, schema/errors.ts): a client-error response whose message says the signature in a thinking block is invalid becomes InvalidRequest with classification invalid-reasoning, next to context-overflow and payload-too-large. error.details projects it to its own kind: "invalid-reasoning" with status and code, like context-overflow, and the step reads details.kind.
  • Retry (packages/core/src/session/runner/step.ts, llm.ts): a new RequireSignature step outcome, built like the overflow recovery. It doesn't use the backoff retry policy, since the request itself changes.
  • Memory (packages/core/src/session/thinking-signature.ts): a process-wide service, one instance per server, holds a set of rejected routes keyed <providerID>/<modelID>/<baseURL>. The key ignores Session and Location, so other Sessions, projects and moved Sessions on the same route share it. SessionContext.resolveModel sets requireSignature: true for a route in the set, so later requests on it go out strict from the first try. There is no expiry and no size limit: a route is added at most once, only after a rejection, and the set starts empty whenever the server restarts, which happens on every update. If the model's compatibility already sets requireSignature (config or built-in provider defaults), that value wins. No events, tables or disk state.

The hardcoded list stays. For same-model replay it's now mostly redundant: Moonshot and Alibaba recordings stream signature: "" already. It still decides what happens to unsigned reasoning after a model switch.

Verification

  • packages/ai/test/provider/anthropic-messages.test.ts: glm-4.6 on a generic gateway streams thinking with an empty signature, both in content_block_start and as an empty signature_delta. The block is recorded as { signature: "" } and sent back as signature: "". Reasoning with no metadata is still dropped, and requireSignature: true still drops it. The test fails on dev.
  • packages/core/test/session-runner.test.ts "requires thinking signatures on a route that rejected an empty one": the second request sends thinking with signature: "" and gets the Anthropic 400, classified through classifyProviderFailure. The retry compiles without the thinking block, and the third prompt goes out strict on its first try through the in-memory set. It fails without the safety-net change.
  • bun test in packages/ai (2383 pass); in packages/core, the session-runner, session-step and promise-tool-cancel files (358 pass) and test/session. bun typecheck in packages/core, bun run check, prettier on the changed files.

Public API: the only addition is the invalid-reasoning value of AIErrorKind and AIErrorDetails: the provider rejected reasoning state sent back to it, such as a thinking signature. It's listed in the README's error table, and today it maps one message, Anthropic's Invalid `signature` in `thinking` block. No new exports.


Added by the opencode bot (AI-generated).

Public API: adds the "invalid-reasoning" kind to AIErrorDetails in @opencode/ai (backwards compatible)

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I ran the #68060 reproduction (a mock Anthropic-compatible gateway with glm-4.6, then run and run --continue) from source on both checkouts. On the base, the thinking block streamed with signature: "" was left out of the next request. With this change it is sent back as {type: "thinking", signature: ""}, and signed blocks are sent back as before. The Anthropic Messages tests and typecheck:tsc in packages/ai pass.

The safety argument holds: core only keeps reasoning provider metadata when replaying to the same model, so reasoning after a model switch still goes through the strict path, and an explicit requireSignature still wins.

Public API: unchanged.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No earlier threads were open. The new commit works as described. Against a mock gateway that answers 400 Invalid \signature` in `thinking` blocktosignature: "", the second turn was rejected once and retried without the thinking block, and succeeded with no visible error. The next turn, in a fresh process, went out strict on its first request. The plain #68060 reproduction still sends signature: ""back to a gateway that accepts it.session-runner.test.ts(272/272), the Anthropic Messages and provider-error tests (134/134), andtypecheck:tscinpackages/aiandpackages/core` pass.

Public API: one addition to @opencode/ai, backwards compatible: the exported helper isInvalidThinkingSignature and a new "invalid-thinking-signature" value for the error classification. Config, the HTTP API, the plugin API and events are unchanged.

Nothing new worth flagging.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No earlier threads were open. The switch from the KV entry to a single in-memory set per server works: the place that resolves the model and the place that records a rejection share one instance, and a configured requireSignature still wins. Against a mock gateway that rejects signature: "", every new process gets one rejection, retries without the thinking block and succeeds with no visible error. The #68060 reproduction still sends signature: "" back to a gateway that accepts it. The session-runner, session-step and promise-tool-cancel tests (295/295) and typecheck:tsc in packages/core pass.

Public API: unchanged since the last review. @opencode/ai adds isInvalidThinkingSignature and the "invalid-thinking-signature" error classification (backwards compatible). No disk state is added any more.

Nothing new worth flagging.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No earlier threads were open. The latest commits work: appending later signature_deltas keeps a real signature from being overwritten, and step recovery now keys off details.kind. I ran the #68060 reproduction from source again: signature: "" is still sent back unchanged, and a mock gateway that rejects it gets one rejection, then a retry without the thinking block that succeeds. The Anthropic Messages and provider-error tests (138/138), the session runner and step tests (295/295), and typecheck:tsc in packages/ai and packages/core pass.

Public API: AIErrorDetails / error.details.kind in @opencode/ai gains "invalid-thinking-signature", documented in the README's error table. It is additive and backwards compatible, though TypeScript code that switches exhaustively on kind will need a case for it. The earlier isInvalidThinkingSignature export is gone, so no new exports remain.

Nothing new worth flagging.

@rekram1-node
rekram1-node enabled auto-merge (squash) December 4, 2026 08:11

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No earlier threads were open. The rename to invalid-reasoning is applied consistently across the classifier, the details.kind projection, the step recovery check and the README. I ran the #68060 reproduction from source again: signature: "" is still sent back unchanged, and a mock gateway that rejects it gets one rejection, then a retry without the thinking block that succeeds. The Anthropic Messages and provider-error tests (138/138), the session runner and step tests (295/295), and typecheck:tsc in packages/ai and packages/core pass.

Public API: AIErrorDetails / error.details.kind in @opencode/ai gains "invalid-reasoning", documented in the README's error table. It is additive and backwards compatible, though TypeScript code that switches exhaustively on kind will need a case for it. There are no new exports.

Nothing new worth flagging.

@rekram1-node
rekram1-node merged commit 2ed060d into dev Dec 4, 2026
12 checks passed
@rekram1-node
rekram1-node deleted the replay-own-thinking branch December 4, 2026 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

V2 drops unsigned thinking blocks for Anthropic-compatible providers, with no config to keep them

1 participant