Repository navigation
fix(ai): send thinking back exactly as the endpoint streamed it - #68182
Conversation
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
7b67e53 to
ae6e1a2
Compare
There was a problem hiding this comment.
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.
166865a to
d1a2bce
Compare
There was a problem hiding this comment.
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.
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-) orcompatibility.requireSignature: falsewas 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 dropThe decoder also keeps an empty
signature_delta, which it used to ignore, so a gateway that leaves the signature out ofcontent_block_startand sends an empty delta later is handled the same way. The firstsignature_deltafor 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 streamssignature: ""incontent_block_startand then replaces it with asignature_deltabefore the block closes, so a completed Claude block always ends with a real signature. Only a block cut off mid-stream still has"". Core'stoLLMMessagesalready 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 explicitrequireSignature: true, which the Vercel AI Gateway sets foranthropic/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 answers400 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:packages/ai/src/provider-error.ts,schema/errors.ts): a client-error response whose message says thesignaturein athinkingblock is invalid becomesInvalidRequestwith classificationinvalid-reasoning, next tocontext-overflowandpayload-too-large.error.detailsprojects it to its ownkind: "invalid-reasoning"withstatusandcode, likecontext-overflow, and the step readsdetails.kind.packages/core/src/session/runner/step.ts,llm.ts): a newRequireSignaturestep outcome, built like the overflow recovery. It doesn't use the backoff retry policy, since the request itself changes.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.resolveModelsetsrequireSignature: truefor 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 setsrequireSignature(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.6on a generic gateway streams thinking with an empty signature, both incontent_block_startand as an emptysignature_delta. The block is recorded as{ signature: "" }and sent back assignature: "". Reasoning with no metadata is still dropped, andrequireSignature: truestill drops it. The test fails ondev.packages/core/test/session-runner.test.ts"requires thinking signatures on a route that rejected an empty one": the second request sendsthinkingwithsignature: ""and gets the Anthropic 400, classified throughclassifyProviderFailure. 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 testinpackages/ai(2383 pass); inpackages/core, the session-runner, session-step and promise-tool-cancel files (358 pass) andtest/session.bun typecheckinpackages/core,bun run check, prettier on the changed files.Public API: the only addition is the
invalid-reasoningvalue ofAIErrorKindandAIErrorDetails: 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'sInvalid `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)