Skip to content

fix(session): trigger auto-compaction at the effective input ceiling (#45168) - #45933

Closed
reisi007 wants to merge 1 commit into
anomalyco:devfrom
reisi007:overflow-input-ceiling
Closed

reisi007 wants to merge 1 commit into
anomalyco:devfrom
reisi007:overflow-input-ceiling

Conversation

@reisi007

@reisi007 reisi007 commented Aug 28, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #45168

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Auto-compaction was gated by usable() = context - maxOutputTokens. maxOutputTokens is capped at OUTPUT_TOKEN_MAX (32000), so for opencode-go/hy3 (models.dev: context 256000, output 64000, no limit.input) the threshold came out to 224000. The provider actually pins input at 262144 - 65536 = 196608, so the measured token count plateaus around 198k and isOverflow() never returned true — the session kept re-sending the full ~196k context as raw, uncached input.

usable() now derives the input ceiling from the model's effective input ceiling and reserves a 20k buffer for the next response:

inputCeiling    = limit.input ?? (context - output)
effectiveCeiling = min(inputCeiling, context - output)   // always leaves room for a full response
usable          = effectiveCeiling - min(reserved, output)

For hy3 that is 192000 - 20000 = 172000, which the pinned count (198108) exceeds, so compaction fires before the provider caps input. Because this is based on the real input ceiling rather than context - maxOutputTokens, it works for any model whose real input cap sits below context - reserved. Both the V1 (processor/prompt) and V2 (SessionCompaction) call paths use this same function, so both are covered.

How did you verify your code works?

Screenshots / recordings

N/A — this is a session/compaction logic change, not a UI change.

Checklist

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

…nomalyco#45168)

Auto-compaction was gated by usable() = context - maxOutputTokens. Because
maxOutputTokens is capped at OUTPUT_TOKEN_MAX (32000), the resulting ceiling
(224000 for opencode-go/hy3) was unreachable: the provider pins real input at
262144 - 65536 = 196608, so the measured token count plateaus there and
isOverflow() never returned true. The session then silently re-sent the full
~196k context as raw (uncached) input on every request.

usable() now derives the input ceiling from limit.input ?? (context - output),
minus a headroom buffer for the next response. This makes the overflow check
fire before the provider caps input, for any model whose real input ceiling
sits below context - reserved.

Covers both the V1 (processor/prompt) and V2 (SessionCompaction) call paths,
which share this function. Adds unit tests reproducing anomalyco#45168 with tencent/hy3
plus anthropic, openai, google, and deepseek models from models.dev.

Fixes anomalyco#45168
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Aug 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

compaction: auto-compaction never triggers on hy3 — token count plateaus below the isOverflow threshold, context pinned at 196608 raw input tokens

1 participant