-
Notifications
You must be signed in to change notification settings - Fork 10k
fix(provider): preserve redacted_thinking blocks and fix signature validation #12131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, I found related PRs that deal with thinking/reasoning blocks, but they appear to be addressing different issues: Related PRs (not exact duplicates):
These are related to thinking/reasoning blocks but address model switching scenarios, whereas PR #12131 focuses on preserving the integrity of redacted_thinking blocks during message processing and multi-turn conversations. No exact duplicate PRs found - PR #12131 appears to be the only one addressing the specific issue of signature validation and block ordering for extended thinking with Claude. |
e962b09 to
e255791
Compare
cryptictech
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went through an AI assisted code review of this PR, as I've faced similar issues with OpenRouter models and was hoping this may help indirectly. Unfortunately, it appears it doesn't, however, it also doesn't impact other model providers. Here is the summary of my AI assisted code review:
Code Review: PR #12131 - Fix "Invalid data in redacted_thinking block" Errors
Summary
This PR fixes signature validation errors when using extended thinking with Claude by removing overly strict handling of redacted_thinking blocks.
Changes Overview
1. packages/opencode/src/provider/transform.ts
Lines 44-85 - normalizeMessages() function:
- Removed: Special handling for preserving
redacted_thinkingblocks (blocks withproviderMetadata?.anthropic) - Removed: Reordering of content blocks to place reasoning first for Anthropic
- Rationale: The previous strict handling was causing signature validation errors when replaying assistant messages
2. packages/opencode/src/session/message-v2.ts
Lines 586-596 - toModelMessages() function:
- Removed: Comment about preserving ALL reasoning parts including redacted_thinking blocks
- Rationale: The comment was documenting behavior that was causing issues
3. packages/opencode/src/session/processor.ts
Lines 88-104 - reasoning-end case:
- Changed: Removed comment about NOT trimmingEnd() thinking text
- Changed: Added
part.text = part.text.trimEnd()to trim trailing whitespace - Rationale: The signature is computed on the exact text including trailing whitespace; modifying it invalidates the signature
4. packages/opencode/src/session/prompt.ts
Lines 968-991 - File handling:
- Changed: Added
.catch(() => undefined)toBun.file(filepath).stat() - Changed: Added optional chaining
stat?.isDirectory()for safer directory check - Changed: Added
.catch(() => [])toLSP.documentSymbol(filePathURI) - Rationale: Prevents failures when file parts reference missing files
5. packages/opencode/test/session/prompt-missing-file.test.ts
New file: Test to ensure prompt does not fail when a file part is missing
6. packages/app/e2e/actions.ts & packages/app/e2e/session/session.spec.ts
UI/UX improvements: More robust session menu interactions and state management
Impact Analysis
Affected Providers
- Anthropic models (including Claude on Bedrock): These changes directly affect Anthropic models by removing strict
redacted_thinkingblock handling
NOT Affected Providers
- OpenRouter: Has separate handling in
variants()andproviderOptions()functions - OpenAI: Has its own interleaved reasoning handling
- Google Gemini: Has its own interleaved reasoning handling
- Mistral: Has its own tool call ID normalization
- GitHub Copilot: Has its own provider-specific handling
Testing
- New test added:
prompt-missing-file.test.tsverifies graceful handling of missing files - Existing E2E tests updated for more robust session menu interactions
Concerns
- Breaking change for Anthropic: Removing the strict
redacted_thinkingblock handling may affect existing sessions that rely on this behavior - Trailing whitespace: Trimming thinking text may affect signature validation if the API expects exact text
Recommendation
Approve with caution. The changes fix a known issue with extended thinking on Claude, but the removal of strict redacted_thinking block handling should be monitored for any unexpected side effects.
e255791 to
75b800c
Compare
The Anthropic API computes a cryptographic signature on the exact thinking text including trailing whitespace. Calling trimEnd() invalidates the signature, causing "Invalid data in redacted_thinking block" errors when thinking blocks are replayed in subsequent API calls. Closes anomalyco#10970
The Anthropic API requires thinking/redacted_thinking blocks to appear before other content blocks in assistant messages. Add reordering logic in normalizeMessages() and preserve all reasoning blocks including redacted_thinking in toModelMessages(). Ref anomalyco#10970
25b0b05 to
993c109
Compare
Summary
Fix "Invalid data in redacted_thinking block" errors when using extended thinking with Claude.
Problem
When extended thinking is enabled, the Anthropic API returns
thinkingandredacted_thinkingblocks with cryptographic signatures. Two issues caused these blocks to be rejected on subsequent API calls:trimEnd()on thinking text — The signature is computed on the exact text including trailing whitespace. Trimming invalidates it.Changes
trimEnd()call on thinking text to preserve signature integritynormalizeMessages()toModelMessages()Testing
Reproduced the error by enabling extended thinking (High) with Claude Opus 4.5, triggering the error on the 2nd message of a session. After the fix, multi-turn conversations with extended thinking work without errors.
Closes #10970