Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(core): stop duplicating the cut entry into both head and recent during compaction - #14055

Open
hobostay wants to merge 1 commit into
Kilo-Org:mainfrom
hobostay:fix/compaction-head-split-duplication
Open

fix(core): stop duplicating the cut entry into both head and recent during compaction#14055
hobostay wants to merge 1 commit into
Kilo-Org:mainfrom
hobostay:fix/compaction-head-split-duplication

Conversation

@hobostay

Copy link
Copy Markdown
Contributor

What

Fixes an off-by-one in SessionCompaction.select() (packages/core/src/session/compaction.ts) that duplicated the partially-split conversation entry into both the head (summarized away) and recent (kept) outputs of auto-compaction.

Why

When the keep budget overflows mid-entry, the entry at index is split into a prefix (intended for head) and a suffix (intended for recent). But head was built with conversation.slice(0, split) where split = index + 1, so the full entry was included in head in addition to its prefix — and its suffix therefore appeared in both outputs. The summary prompt sent to the model contained duplicated content with an inflated token count, which also makes the Token.estimate(summaryPrompt) > context - summaryOutput guard more likely to abort auto-compaction entirely, leaving the session stuck on context-overflow errors.

The fix keeps separate cut points: head takes the entries before the split index plus the prefix; recent takes the suffix plus the entries after it.

Testing

Added a regression test in packages/core/test/session-compaction.test.ts that constructs four fixed-size entries with a budget that splits the third one. It fails on the previous logic (head contains the cut entry's tail) and passes with the fix. bun test (packages/core, 1325 pass) and bun run typecheck are green; the single failure in the full suite (ProjectCopy > requires force to remove a dirty git worktree) reproduces on unmodified main on this machine and is unrelated.

@kilo-code-bot

kilo-code-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

The off-by-one fix in SessionCompaction.select() is correct: the split entry's prefix now goes only to head and its suffix only to recent, eliminating the duplication while losing no content. Traced the split, all-fit, and zero-remaining branches against Token.estimate — partitioning is exact in each case. The regression test fails on the old logic (head contains END1) and passes on the new logic. Changeset targets the fixed release package @kilocode/cli and the private @opencode-ai/core package cannot carry one.

Files Reviewed (3 files)
  • .changeset/compaction-split-duplication.md
  • packages/core/src/session/compaction.ts
  • packages/core/test/session-compaction.test.ts

Reviewed by deepseek-v4.1-flash · Input: 0 · Output: 0 · Cached: 0

Review guidance: REVIEW.md from base branch main

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.

1 participant