Skip to content

fix(compaction): use cumulative token usage for auto-compaction overflow check - #46136

Closed
unaiberis wants to merge 1 commit into
anomalyco:devfrom
unaiberis:fix/compaction-trigger-usage-fallback
Closed

unaiberis wants to merge 1 commit into
anomalyco:devfrom
unaiberis:fix/compaction-trigger-usage-fallback

Conversation

@unaiberis

@unaiberis unaiberis commented Aug 29, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #46137

Type of change

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

What does this PR do?

Auto-compaction never triggers for large-context models like opencode-go/hy3.

The overflow check in packages/opencode/src/session/prompt.ts passed only the last finished message's token count (lastFinished.tokens) into compaction.isOverflow({ tokens, model }). isOverflow compares that number against usable(model) = model.limit.context - maxOutputTokens. For hy3 that is 256000 - 64000 = 192000. A single assistant message is only ~20k tokens, so 20000 >= 192000 is always false and the compaction.create({ auto: true }) branch is never reached.

This PR accumulates the cumulative token usage of the whole context and passes that to isOverflow, so auto-compaction fires once the full context crosses the usable threshold. overflow.ts is unchanged.

How did you verify your code works?

  • Verified the root cause against the real opencode2 message DB: hy3 returns usage correctly (1927/1935 assistant messages carry real input/output/reasoning/cache token counts), so the bug is single-message-vs-cumulative, not missing usage.
  • Inspected isOverflow/usable(model) math: for hy3, usable = 192000; cumulative context now reaches it and triggers compaction.
  • Diff is limited to prompt.ts (+28/−4); no behavior change for models where a single message already exceeds the threshold.

Screenshots / recordings

Not a UI change.

Checklist

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

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Aug 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown
Contributor

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

Based on my search, I found two potentially related PRs (excluding the current PR #46136):

  1. PR feat(session): add custom compaction thresholds along with prevention of continuous compaction #10123 - feat(session): add custom compaction thresholds along with prevention of continuous compaction

    • Related because it addresses compaction thresholds and behavior, which is relevant to the overflow check logic
  2. PR feat(opencode): support per-model compaction config #43713 - feat(opencode): support per-model compaction config

    • Related because it adds per-model compaction configuration, which directly intersects with this fix that addresses large-context model overflow detection
  3. PR fix: preserve thinking block signatures and fix compaction headroom asymmetry #14393 - fix: preserve thinking block signatures and fix compaction headroom asymmetry

    • Related because it fixes compaction-related asymmetry issues

These PRs may overlap in scope or provide context for understanding the compaction system. You should review them to ensure this fix doesn't conflict with existing approaches or duplicate efforts.

@github-actions github-actions Bot removed needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Aug 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@unaiberis

Copy link
Copy Markdown
Author

Thanks for the related-PR pointers. I reviewed #10123, #43713, and #14393 against this diff to be precise about overlap:

This PR is a bug fix, not a config feature.
The auto-compaction trigger in prompt.ts (the Layer.effect overflow block) passes only lastFinished.tokens (a single message) to isOverflow. For large-context models like opencode-go/hy3, usable(model) = context - maxOutput = 256000 - 64000 = 192000, but one assistant message is only ~20k tokens, so the check is always false and compaction never fires. This PR accumulates the cumulative token count of the whole context and passes that to isOverflow (which itself is untouched). No new config is added.

Overlap check:

In short: this fixes a base-level trigger bug that exists independently of those efforts; it is not a duplicate. Happy to rebase or fold this into #43713/#10123 if maintainers prefer a single change.

@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.

Auto-compaction never triggers for large-context models (hy3): isOverflow sees only single-message tokens

1 participant