Skip to content

fix(core): keep compaction's retained tail within keep.tokens - #65768

Merged
neriousy merged 1 commit into
devfrom
compaction-keep-bound
Nov 23, 2026
Merged

neriousy merged 1 commit into
devfrom
compaction-keep-bound

Conversation

@neriousy

Copy link
Copy Markdown
Member

Issue for this PR

Closes #43250

Type of change

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

What does this PR do?

compaction.keep.tokens didn't limit the tail kept next to the summary. recentStart found the cut that fit keep, then moved it back to the nearest type === "user" message, however far back that was. In agent-driven sessions (subagents, long tool loops) there are few user messages, so a whole autonomous turn was kept, often many times over keep. synthetic and shell messages were never used as cut points either, even though they go to the model as user messages.

The move back was meant to keep an assistant's tool calls with their results. That doesn't matter here:

  • The kept tail is sent as flattened text inside the checkpoint (SessionRecentText.messageToText), not as provider messages.
  • In v2, each assistant message already holds its own tool calls and results.

So the tail can start at any message:

  • recentStart keeps the newest messages that fit keep and starts the tail at the oldest of them, whatever its type. Each message counts one extra token for the \n\n separator and rounding, so Token.estimate(recent) <= keep holds for the joined text.
  • A newest message that alone exceeds keep is no longer always kept. It's summarized with the rest. keep: 0 now keeps nothing.
  • When everything fits, it falls back to the same behavior as before (latest exchange, or nothing), which is already within keep.

With longer tails, the history before the tail is smaller, and the overflow shrink ladder exposed a related problem. After that history was sent unchanged and rejected, the next, lower target could still be above its estimate, so the identical request went out again. deliver now sends a rejected history only as text after that, the same as it already did for a measured request.

No flag: this is a bug fix.

How did you verify your code works?

  • New tests in packages/core/test/session-compaction.test.ts:
    • A user request followed by 60 tool steps and synthetic notes keeps a tail that starts at a non-user message and is within 15k.
    • A newest message larger than keep keeps no tail.
  • Updated tests:
    • test/config/compaction.test.ts now checks that keep: 0 keeps nothing; before, its assertion matched the default too.
    • session-compaction.test.ts "history before the tail unchanged" sets keep so the 30k tail is still kept.
    • session-runner.test.ts "four smaller compaction inputs" has enough history to shrink and checks that the history isn't resent unchanged.
  • All of these fail on dev and pass here.
  • bun run check passes.
  • Full core suite: 7956 pass. The only 3 failures are shell process-substitution tests, which fail the same way on dev locally.

Screenshots / recordings

N/A

Checklist

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

@neriousy neriousy self-assigned this Nov 23, 2026

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks sound. I ran the new and updated tests in session-compaction, config/compaction and session-runner against the base source: all four fail there, covering the tail kept back to the user message, the oversized newest message, keep: 0 and the history resent unchanged. With this PR, all 298 tests in those files pass. I also checked how the new cut is used downstream: withoutTail finds the synthetic, shell and assistant IDs in the request, and history rebuilds the tail text from keepID. The overflow ladder now resends a rejected history only as text, shorter each time. I couldn't run the typecheck script here because this Bun version has no bun check.

Public API: unchanged. compaction.keep.tokens keeps its name and is now a real upper bound; 0 keeps no tail.

No blocking concerns.

@neriousy
neriousy merged commit ad8858d into dev Nov 23, 2026
11 checks passed
@neriousy
neriousy deleted the compaction-keep-bound branch November 23, 2026 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant