Skip to content

fix(session): reserve output headroom when limit.input is set in isOverflow#1265

Open
0xSkybreaker wants to merge 1 commit into
XiaomiMiMo:mainfrom
0xSkybreaker:fix/isoverflow-input-limit-output-headroom
Open

fix(session): reserve output headroom when limit.input is set in isOverflow#1265
0xSkybreaker wants to merge 1 commit into
XiaomiMiMo:mainfrom
0xSkybreaker:fix/isoverflow-input-limit-output-headroom

Conversation

@0xSkybreaker

Copy link
Copy Markdown

The usable() function in overflow.ts subtracted outputReserve only when using the full context window, but not when limit.input was set. This meant compaction would only trigger after consuming the full input budget, leaving zero room for the next API call's output tokens — potentially causing model calls to fail.

Root Cause

In packages/opencode/src/session/overflow.ts, the usable() function had a ternary:

return input.model.limit.input
  ? Math.max(0, input.model.limit.input - reserved)           // ← missing outputReserve!
  : Math.max(0, context - outputReserve - reserved)

The outputReserve (capped at 20K) was only subtracted in the else branch. Models with limit.input set (e.g., Claude with prompt caching) got no output headroom, meaning compaction didn't trigger until the full input budget was exhausted — leaving no room for the model to generate a response.

Fix

return input.model.limit.input
  ? Math.max(0, input.model.limit.input - outputReserve - reserved)  // ✓ fixed
  : Math.max(0, context - outputReserve - reserved)

Both branches now consistently reserve output headroom before triggering compaction.

Tests

  • All 12 existing overflow tests continue to pass
  • Updated 3 previously-labeled "BUG" tests to serve as regression tests verifying the fix
  • The fix makes the limit.input path symmetric with the context-only path — models with identical real capacity now agree on when to compact

Related: #10634, #8089, #11086, #12621

…erflow

The usable() function subtracted outputReserve only when using the full
context window, but not when limit.input was set. This meant compaction
would only trigger after consuming the full input budget, leaving zero
room for the next API call output tokens — potentially causing model
calls to fail.

Fix: consistently subtract outputReserve in both branches of the ternary,
so models with limit.input get the same output headroom reservation as
models using the full context window.

Also updated the previously-skipped "BUG" tests in overflow.test.ts to
reflect the corrected behavior and serve as regression tests.

Related: #10634, #8089, #11086, #12621
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant