opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix(session): prevent context overflow by adding safety margin to compaction check

Open o1x3 opened this issue 2 weeks ago • 0 comments

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

o1x3 avatar Jan 01 '26 06:01 o1x3