[PR #6480] fix(session): check for context overflow mid-turn in finish-step #11928

Closed
opened 2026-02-16 18:16:52 -05:00 by yindo · 0 comments
Owner

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

State: closed
Merged: Yes


Summary

  • fixes Permission prompt doesn't support mouse interaction (#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.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/6480 **State:** closed **Merged:** Yes --- ## 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 | |--------|-------| | <img width="2294" height="1508" alt="CleanShot 2025-12-31 at 05 52 57@2x" src="https://github.com/user-attachments/assets/970e0b4f-9443-49da-be46-8ab5d2b5e2db" /> | <img width="2990" height="1996" alt="CleanShot 2025-12-31 at 05 51 40@2x" src="https://github.com/user-attachments/assets/ee1c6b4f-50ec-4d40-bbfd-31137cb6cc0e" /> | ## 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.
yindo added the pull-request label 2026-02-16 18:16:52 -05:00
yindo closed this issue 2026-02-16 18:16:52 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11928