opencode
opencode copied to clipboard
fix(session): check for context overflow mid-turn in finish-step
Summary
- fixes #4845
- Check for context overflow during streaming in
finish-stepevent handler using actual token counts from AI SDK - Break stream loop early and return
"compact"when overflow detected - Handle
"compact"return in prompt.ts to triggerSessionCompaction.createimmediately
Problem
Context compaction only triggered after an assistant turn completed. When a model made many tool calls with large outputs, context could overflow mid-turn before compaction had a chance to run.
Solution
Move the overflow check from post-turn to mid-turn by checking in the finish-step event handler (which fires after each step). This uses actual token counts from the AI SDK rather than estimates.
Before / After
| Before | After |
|---|---|
Changes
-
processor.ts: Check
isOverflowinfinish-step, setneedsCompactionflag, break loop early, return"compact" -
prompt.ts: Handle
"compact"return, triggerSessionCompaction.create, continue loop - compaction.test.ts: Unit tests for the compaction data flow
Testing
15 tests covering the compaction data flow:
-
session.compaction.isOverflow(5 tests) - overflow detection logic -
util.token.estimate(3 tests) - token estimation -
session.getUsage(7 tests) - AI SDK usage normalization to our token format
Tests verify the key units without heavy mocking - if getUsage correctly normalizes AI SDK tokens and isOverflow correctly detects overflow, the integration works.