opencode
opencode copied to clipboard
fix(session): prevent context overflow by adding safety margin to compaction check
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-stephandler - Add 30K truncation for MCP tool outputs and webfetch results
Test plan
- [x] Typecheck passes
- [x] Session tests pass
- [x] Tool tests pass