opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix(session): check for context overflow mid-turn in finish-step

Open aryasaatvik opened this issue 3 weeks ago • 1 comments

Summary

  • fixes #4845
  • Check for context overflow during streaming in finish-step event 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 trigger SessionCompaction.create immediately

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
CleanShot 2025-12-31 at 05 52 57@2x CleanShot 2025-12-31 at 05 51 40@2x

Changes

  • processor.ts: Check isOverflow in finish-step, set needsCompaction flag, break loop early, return "compact"
  • prompt.ts: Handle "compact" return, trigger SessionCompaction.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.

aryasaatvik avatar Dec 30 '25 22:12 aryasaatvik