Skip to content

fix(opencode): bound output reservation in compaction budget - #51712

Closed
naoufalelbani wants to merge 2 commits into
anomalyco:devfrom
naoufalelbani:compaction-budget
Closed

naoufalelbani wants to merge 2 commits into
anomalyco:devfrom
naoufalelbani:compaction-budget

Conversation

@naoufalelbani

Copy link
Copy Markdown

Issue for this PR

Closes #51711

Type of change

  • Bug fix

What does this PR do?

usable() in src/session/overflow.ts subtracted a model's full capped output limit from
the context window instead of a bounded reservation, so a 1M model with a 512k output
capability got a 524,288 budget and compacted at 600k with ~500k of window still free.

The reservation is now bounded by the compaction buffer, limits are validated once in
normalizeLimits, and both call sites pass flags.outputTokenMax. Malformed metadata
degrades to "unknown" rather than to a small default, so a 1M model is never mistaken for
a 16k one. preserveRecentBudget is unchanged.

One semantic change worth a maintainer's call: shouldCompact returns false when the
budget is <= 0, so models with unknown limits skip predictive compaction and rely on
compactAfterOverflow. The old 0 >= 0 compacted on the first step of every turn. Happy
to flip it or gate it behind config.

How did you verify your code works?

bun test test/session/compaction-budget.test.ts test/session/compaction.test.ts
bun test test/session/processor-effect.test.ts test/session/prompt.test.ts
bun run typecheck

Covers 1M/128k/32k scaling, the 16.6k non-compaction case, malformed metadata (0,
negative, NaN, Infinity, null, string), output > context, input > context,
reasoning accounting, and retention asserted against the real preserveRecentBudget.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

@thdxr

thdxr commented Oct 10, 2026

Copy link
Copy Markdown
Member

This branch has been upgraded to V2. We're no longer taking PRs for V1. If you think this is still relevant, please port it over and open it for V2.

— from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊

@thdxr thdxr closed this Oct 10, 2026
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.

Compaction budget too small for large-context models

2 participants