Skip to content

fix(core): keep the compaction tail within its keep allowance - #59305

Closed
xxwtiancai wants to merge 1 commit into
anomalyco:devfrom
xxwtiancai:compaction-keep-budget
Closed

xxwtiancai wants to merge 1 commit into
anomalyco:devfrom
xxwtiancai:compaction-keep-budget

Conversation

@xxwtiancai

Copy link
Copy Markdown

Issue for this PR

Closes #54910

Type of change

  • Bug fix

What does this PR do?

Manual compaction could keep far more than keep.tokens verbatim beside the summary. recentStart walked the kept tail back to the nearest earlier user message, so a long autonomous run after the last user message was kept whole, and an oversized newest entry was always kept anyway. Running the repro script from the bot's comment on dev (e0b6039e1), the first request after /compact was 65,952 tokens with the ~60k turn verbatim.

The walk back to a user message now only happens while the tail stays inside the allowance, and a newest entry that alone exceeds it is summarized instead of kept. When neither applies, the tail starts at the newest entries that fit, mid-run if needed — each assistant message already carries its own tool calls and results, so a mid-run start does not split them.

One deliberate behavior change: an oversized newest exchange is no longer always kept verbatim (it is covered by the summary instead). The summary compaction flattens the whole conversation… test was updated for that.

How did you verify your code works?

  • Two new regression tests in packages/core/test/session-compaction.test.ts: an oversized newest turn leaves no keepID/recent text, and a tail over the allowance starts at the newest fitting reply instead of walking back past it.
  • Re-ran the opencode-drive reproduction from the issue against a checkout with this change: the first request after /compact dropped from ~66k tokens (turn verbatim) to ~5.9k (summary only).
  • Compaction suites pass (40 tests across 5 files); typecheck adds no new errors beyond the pre-existing ToolDefinition import error on dev (test/session-model-request-hooks.test.ts).

Screenshots / recordings

Not a UI change.

Checklist

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

Manual compaction could keep far more than keep.tokens verbatim beside
the summary: recentStart walked the tail back to the nearest earlier
user message, keeping a long autonomous run whole, and an oversized
newest entry was always kept. The walk back now only happens while the
tail stays inside the allowance, and an oversized newest entry is
summarized instead of kept.

Closes anomalyco#54910
@opencode-agent opencode-agent Bot closed this Oct 26, 2026
@opencode-agent

Copy link
Copy Markdown
Contributor

Thanks for tracking this down and for re-running the reproduction from #54910. I think this duplicates an existing open pull request:

Your version can start the tail partway through an assistant run instead of keeping only whole user exchanges, so it can keep some recent text where #53233 would keep none. If you think that matters, please raise it on #53233. A review there, your 66k → 5.9k numbers or your regression tests would all help.

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: manual compaction keeps full conversation in next request

1 participant