fix(core): stop duplicating the boundary message in compaction head#33330
Open
Robin1987China wants to merge 1 commit into
Open
fix(core): stop duplicating the boundary message in compaction head#33330Robin1987China wants to merge 1 commit into
Robin1987China wants to merge 1 commit into
Conversation
select() partitions the conversation into head (summarized) and recent (kept) at a token budget. When the boundary lands mid-message, that message is split into splitPrefix (head) and splitSuffix (recent). The split branch set split = index + 1 and reused it for both head's slice end and recent's slice start, so head = slice(0, index + 1) included the full boundary message AND splitPrefix (a truncated copy) — duplicating it. head is the exact text fed to the summarizer, so on every overflow-triggered compaction where the boundary lands mid-message the boundary message was sent twice (full + truncated), wasting tokens and risking pushing the summary prompt past the context limit so compaction silently fails. Track two boundaries: headEnd (end of head's full-message slice) and recentStart (start of recent's slice). They differ only in the split case (headEnd = index, recentStart = index + 1); all other paths keep them equal, preserving existing behavior. Export select and add a regression test for the duplication plus a fully-fitting sanity case. Closes anomalyco#33329
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #33329
Type of change
What does this PR do?
selectinpackages/core/src/session/compaction.tspartitions the conversation intohead(summarized during auto-compaction) andrecent(kept verbatim) at a token budget. When the boundary lands mid-message, that message is split intosplitPrefix(head) andsplitSuffix(recent).The split branch set
split = index + 1and reused that single value for both the end of head's full-message slice and the start of recent's slice:head = [...conversation.slice(0, split), splitPrefix]→slice(0, index + 1)includes the full boundary message, thensplitPrefix(a truncated copy of the same message) is appended → the boundary message is duplicated in head.recent = [splitSuffix, ...conversation.slice(split)]→ correct.headis the exact text fed to the summarizer, so on every overflow-triggered compaction where the boundary lands mid-message (common for a large tool result), the boundary message was sent twice (full + truncated). This wastes input tokens and can push the summary prompt past the context limit, making compaction silently fail to recover from overflow.The fix tracks two boundaries —
headEnd(end of head's full-message slice) andrecentStart(start of recent's slice). They differ only in the mid-message split case (headEnd = index,recentStart = index + 1); all other paths keep them equal, preserving existing behavior (including theremaining === 0case where the boundary message goes entirely to head).How did you verify your code works?
selectand added a regression test constructing entries whose budget boundary lands mid-message, asserting the boundary message appears inheadonly as the truncated prefix (not duplicated in full) andrecentholds the suffix + following messages.bun test test/session-compaction.test.ts— 3 pass.bun typecheck(packages/core) — clean.Checklist