fix(provider): strip reasoning blocks from non-latest assistant messages to fix compaction#26892
fix(provider): strip reasoning blocks from non-latest assistant messages to fix compaction#26892jralph wants to merge 1 commit into
Conversation
…ges to fix compaction The Anthropic/Bedrock API requires thinking blocks in the latest assistant message to be replayed exactly as originally returned. Signatures on historical thinking blocks can become invalid due to text modifications during stream processing, causing compaction and multi-turn conversations to fail with 'thinking blocks cannot be modified'. Strip reasoning blocks from all non-latest assistant messages before sending to the API. The API only validates the latest assistant message, so historical thinking blocks are safely removable. Additional fixes: - Skip normalizeMessages empty-content filtering for assistant messages - Skip sanitizeSurrogates on signed reasoning parts - Detect Bedrock signatures in hasSignedReasoning (was only checking anthropic) Fixes anomalyco#14332
2610bd3 to
4bd306b
Compare
|
The following comment was made by an LLM, it may be inaccurate: Based on the search results, I found a potentially related PR: Related PRPR #14393: "fix: preserve thinking block signatures and fix compaction headroom asymmetry" Why it may be related:
This appears to be a closely related fix to the same compaction problem space. You may want to check if PR #14393 is still open and whether it addresses the same |
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
Issue for this PR
Closes #14332
Type of change
What does this PR do?
Strips reasoning blocks from non-latest assistant messages before sending to the API. Both Anthropic and AWS
Bedrock document that thinking blocks from prior turns can be omitted, the API only validates signatures on the latest assistant message.
This avoids the
thinking blocks cannot be modifiederror that occurs when historical signatures become invalid during compaction or multi-turn replay.Also fixes
hasSignedReasoninginmessage-v2.tsto detect Bedrock signatures (was only checking theanthropickey), and skipssanitizeSurrogates/empty-content filtering on assistant messages to protect the latest message's thinking blocks. It was failing on large sessions consistently and reliably.How did you verify your code works?
226 tests pass (6 new), 0 failures. Manually tested compaction with Opus 4.7 on Bedrock, works where it previously crashed. It wasn't previously failing on small context sessions (for example, saying "hi" and compacting worked), but was failing on larger 180k+ context sessions, and is not anymore.
Screenshots / recordings
Original Issue:

Checklist