Skip to content

fix(opencode): preserve reasoning providerMetadata across model switches#22818

Closed
bainos wants to merge 1 commit intoanomalyco:devfrom
bainos:fix/thinking-block-signature-lost
Closed

fix(opencode): preserve reasoning providerMetadata across model switches#22818
bainos wants to merge 1 commit intoanomalyco:devfrom
bainos:fix/thinking-block-signature-lost

Conversation

@bainos
Copy link
Copy Markdown
Contributor

@bainos bainos commented Apr 16, 2026

Fixes #22813

What

Always preserve providerMetadata on reasoning parts when reconstructing conversation history, regardless of the differentModel flag.

Why

The differentModel guard exists to prevent provider-specific metadata (e.g. OpenAI itemId) from leaking into other providers — correct for text and tool parts. But Anthropic thinking blocks carry a cryptographic signature inside providerMetadata that Anthropic verifies on every subsequent turn. Stripping it causes the API to reject the message with "thinking blocks cannot be modified".

How I verified it

  • Added a regression test: "preserves reasoning providerMetadata even when assistant model differs" in test/session/message-v2.test.ts
  • Full test suite passes (1880/1882; the 2 failures are pre-existing and unrelated)
  • Typecheck clean
  • Manual testing in progress

Anthropic thinking blocks carry a cryptographic signature in providerMetadata
that must survive every turn of a multi-turn conversation. The differentModel
guard was correctly stripping provider-specific metadata from text and tool
parts, but incorrectly doing the same for reasoning parts, causing Anthropic
to reject the request with 'thinking blocks cannot be modified'.

Fixes anomalyco#22813
@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 16, 2026
@github-actions
Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found several related PRs that address similar issues with reasoning/thinking blocks and providerMetadata:

Potentially Related PRs:

  1. fix: replace empty text in reasoning messages to preserve thinking block positions #21860 - "fix: replace empty text in reasoning messages to preserve thinking block positions"

    • Related to preserving thinking block structure
  2. fix(provider): preserve assistant message content when reasoning blocks present #21370 - "fix(provider): preserve assistant message content when reasoning blocks present"

    • Directly addresses preserving content when reasoning blocks are involved
  3. fix: preserve thinking block signatures and fix compaction headroom asymmetry #14393 - "fix: preserve thinking block signatures and fix compaction headroom asymmetry"

    • Addresses thinking block signature preservation (similar concern to your cryptographic signature issue)
  4. fix(provider): skip empty-text filtering for assistant messages in normalizeMessages (#16748) #16750 - "fix(provider): skip empty-text filtering for assistant messages in normalizeMessages"

    • Related to message normalization and thinking blocks

These PRs all address similar concerns around preserving thinking block integrity and metadata. However, PR #22818 (the current PR) appears to be the specific fix for issue #22813, so it's likely a unique solution rather than a duplicate of the older PRs.

@github-actions
Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Apr 16, 2026
@github-actions github-actions bot closed this Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: thinking block signature lost when model differs, breaking multi-turn with extended thinking

1 participant