fix(session): prevent context overflow by adding safety margin to compaction check#6562
Open
o1x3 wants to merge 2 commits intoanomalyco:devfrom
Open
fix(session): prevent context overflow by adding safety margin to compaction check#6562o1x3 wants to merge 2 commits intoanomalyco:devfrom
o1x3 wants to merge 2 commits intoanomalyco:devfrom
Conversation
…paction check - Add 50K overhead buffer to account for system prompts and tool definitions - Add mid-turn overflow detection in finish-step to catch runaway tool loops - Add 30K truncation for MCP tool outputs and webfetch results - Handle compact signal to trigger compaction mid-turn Fixes anomalyco#6068
This was referenced Jan 20, 2026
00637c0 to
71e0ba2
Compare
f1ae801 to
08fa7f7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #6068 - Auto compaction not triggering reliably with Anthropic models causing "prompt is too long" errors.
Root cause: The overflow check happens before system prompts (~25K tokens) and tool definitions (~10K tokens) are added to the request, so 40-60K tokens of overhead aren't accounted for.
Changes:
isOverflow()calculationfinish-stephandlerTest plan