Skip to content

fix(provider): strip reasoning blocks from non-latest assistant messages to fix compaction#26892

Open
jralph wants to merge 1 commit into
anomalyco:devfrom
jralph:fix/strip-reasoning-blocks-compaction
Open

fix(provider): strip reasoning blocks from non-latest assistant messages to fix compaction#26892
jralph wants to merge 1 commit into
anomalyco:devfrom
jralph:fix/strip-reasoning-blocks-compaction

Conversation

@jralph
Copy link
Copy Markdown

@jralph jralph commented May 11, 2026

Issue for this PR

Closes #14332

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

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 modified error that occurs when historical signatures become invalid during compaction or multi-turn replay.

Also fixes hasSignedReasoning in message-v2.ts to detect Bedrock signatures (was only checking the anthropic key), and skips sanitizeSurrogates/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:
Screenshot 2026-05-11 at 15 30 45

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…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
@jralph jralph force-pushed the fix/strip-reasoning-blocks-compaction branch from 2610bd3 to 4bd306b Compare May 11, 2026 14:45
@github-actions
Copy link
Copy Markdown
Contributor

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

Based on the search results, I found a potentially related PR:

Related PR

PR #14393: "fix: preserve thinking block signatures and fix compaction headroom asymmetry"
#14393

Why it may be related:

  • Directly addresses thinking block signatures and compaction issues
  • Deals with the same root problem area (thinking blocks during message compaction)
  • Both PRs fix signature validation and compaction failures related to Anthropic/Bedrock extended thinking

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 #14332 issue or if it's a complementary fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Amazon Bedrock Opus 4.6 compaction failure

1 participant