fix: drop thinking blocks for Anthropic to prevent session corruption#27142
fix: drop thinking blocks for Anthropic to prevent session corruption#27142slatem wants to merge 1 commit intoopenclaw:mainfrom
Conversation
Greptile SummaryFixes critical session corruption bug in Anthropic models by extending
Confidence Score: 5/5
Last reviewed commit: d3035de |
The Anthropic API rejects `thinking` and `redacted_thinking` blocks in historical assistant messages when those blocks have been modified from the original response (e.g. after session serialization round-trips). Changes: - Enable dropThinkingBlocks for Anthropic (was only Copilot Claude) - Also strip `redacted_thinking` blocks (was only `thinking`) - Add test coverage for redacted_thinking block handling
9b76595 to
804fa2f
Compare
xiaoyaner0201
left a comment
There was a problem hiding this comment.
🔍 Duplicate PR Group: Anthropic Thinking Block Compaction
This PR is one of 7 open PRs addressing the same issue: Anthropic thinking/redacted_thinking blocks from older assistant messages cause API errors during context compaction.
Related PRs
| PR | Author | Date | Approach | Preserves latest | redacted_thinking | Compaction path | Tests | Clean diff |
|---|---|---|---|---|---|---|---|---|
| #24261 | @Vaibhavee89 | 02-23 | Guard functions + logging | N/A (no actual strip) | Detect only | ❌ | Medium | ✅ |
| #25381 | @Nipurn123 | 02-24 | Modify dropThinkingBlocks skip latest |
✅ | ❌ | ❌ | Good | ❌ (bundles sendDice) |
| #27142 | @slatem | 02-26 | Extend dropThinkingBlocks to Anthropic |
❌ (strips all) | ✅ | ❌ | Limited | ✅ |
| #39919 | @taw0002 | 03-08 | New strip function in sanitizeSessionHistory | ✅ | ✅ | ❌ | Good | ✅ |
| #39940 | @liangruochong44-ui | 03-08 | Policy flag for Anthropic | ❌ (strips all) | ✅ | ❌ | Medium | ❌ (bundles cron/SQLite/UI) |
| #43783 | @coletoncodes | 03-12 | New stripThinkingFromNonLatestAssistant + compact path |
✅ | ✅ | ✅ | Best | ✅ |
| #44650 | @rikisann | 03-13 | Runtime monkey-patch of node_modules | ✅ | ✅ | ❌ | None |
Analysis
After reading all 7 diffs:
#43783 is the strongest candidate. It:
- Creates a standalone
stripThinkingFromNonLatestAssistant()without changingdropThinkingBlockssemantics (preserves Copilot behavior) - Integrates via existing
policy.preserveSignaturesflag (proper transcript policy mechanism) - Only PR that also fixes the compaction path (
compact.ts) — others only fixsanitizeSessionHistory - Handles both
thinkingandredacted_thinkingblock types - Clean diff with comprehensive tests (toolResult interleaving, empty blocks, reference equality)
#39919 is a close second (same core function, clean diff, good tests) but misses the compaction path.
Notable discovery from #44650: sanitizeSurrogates may corrupt thinking block signatures — worth investigating separately even though the monkey-patch approach is not viable.
Not recommended: #27142 and #39940 strip thinking from ALL messages including latest (breaks multi-turn quality). #25381 and #39940 bundle unrelated changes.
Similarities identified via embedding-based clustering (cosine > 0.82). Maintainers: #43783 appears ready for review.
|
This pull request has been automatically marked as stale due to inactivity. |
|
Closing due to inactivity. |
Problem
When extended thinking is enabled (
thinking=low/high) on Anthropic models, session compaction or gateway restart can modifythinking/redacted_thinkingblocks in the stored transcript. Anthropic's API requires these blocks to be byte-for-byte identical to the original response — any modification causes a permanent400 invalid_request_error:The session is bricked until
/reset, losing all conversation context.Root Cause
resolveTranscriptPolicyinsrc/agents/transcript-policy.tsonly enablesdropThinkingBlocksfor GitHub Copilot Claude endpoints. For Anthropic's own API (anthropic-messages) and Bedrock (bedrock-converse-stream), thinking blocks are preserved in the transcript but can get corrupted during compaction or session reconstruction.Fix
Enable
dropThinkingBlocksfor all Anthropic-compatible providers (isAnthropic), not just GitHub Copilot Claude. Anthropic's API accepts omitted thinking blocks — it only rejects modified ones. This is the minimal, safe fix:Trade-off
The model loses thinking context from prior assistant turns. This is minor — thinking blocks are internal reasoning and don't significantly affect multi-turn coherence. The alternative (sessions permanently bricking) is far worse.
Tests
Added 5 new test cases covering:
dropThinkingBlocks: truedropThinkingBlocks: truedropThinkingBlocks: true(existing behavior preserved)dropThinkingBlocks: falsedropThinkingBlocks: falseAll 12 tests pass.
Fixes #19524
Fixes #25194