[PR #6562] fix(session): prevent context overflow by adding safety margin to compaction check #11974

Open
opened 2026-02-16 18:16:54 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/6562

State: open
Merged: No


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:

  • Add 50K overhead buffer to isOverflow() calculation
  • Add mid-turn overflow detection in finish-step handler
  • Add 30K truncation for MCP tool outputs and webfetch results

Test plan

  • Typecheck passes
  • Session tests pass
  • Tool tests pass
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/6562 **State:** open **Merged:** No --- ## 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**: - Add 50K overhead buffer to `isOverflow()` calculation - Add mid-turn overflow detection in `finish-step` handler - Add 30K truncation for MCP tool outputs and webfetch results ## Test plan - [x] Typecheck passes - [x] Session tests pass - [x] Tool tests pass
yindo added the pull-request label 2026-02-16 18:16:54 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11974