Skip to content

fix(compaction): keep last valid cut point when recent-token budget overshoots#40

Merged
code-yeongyu merged 1 commit into
mainfrom
fix/compaction-cutpoint-fallback
Jun 15, 2026
Merged

fix(compaction): keep last valid cut point when recent-token budget overshoots#40
code-yeongyu merged 1 commit into
mainfrom
fix/compaction-cutpoint-fallback

Conversation

@code-yeongyu

@code-yeongyu code-yeongyu commented Jun 15, 2026

Copy link
Copy Markdown
Owner

What

findCutPoint (compaction) defaulted cutIndex = cutPoints[0] and only reassigned it when a valid cut point sat at or after the budget-crossing entry. When the recent-token budget was exceeded entirely within trailing tool results (which are never valid cut points), no branch fired and cutIndex stayed at cutPoints[0] — so compaction silently kept the entire history instead of the recent window.

Fix: when no cut point is >= i, fall back to the last valid cut point (cutPoints[cutPoints.length - 1]).

Applied to both definitions:

  • packages/coding-agent/src/core/compaction/compaction.ts
  • packages/agent/src/harness/compaction/compaction.ts

(Ported as a senpi-native fix from gajae-code 534b4f0a; senpi keeps its estimateTokens char/4 estimator — gajae's native estimateEntryTokens was intentionally not adopted.)

Why

Without the fallback, a turn whose recent budget is consumed by large tool outputs after the last user/assistant message would compact to "keep everything", defeating compaction exactly when the context is largest.

Evidence (measured on this repo)

RED → GREEN (new tests, fail before / pass after):

  • packages/coding-agent/test/compaction.test.ts > findCutPoint > falls back to the last valid cut point when the budget is exceeded beyond it — RED expected +0 to be 1, GREEN after fix.
  • packages/agent/test/harness/compaction.test.ts > harness compaction > falls back to the last valid cut point when the budget overshoots it — RED expected +0 to be 1, GREEN after fix.

Regression (all green, no skips):

  • coding-agent test/compaction.test.ts: 30/30
  • agent test/harness/compaction.test.ts: 21/21
  • coding-agent test/compaction/ policy suite: 18 files, 125/125

Gates: full npm run check green via pre-commit (biome, check:ts-imports, check:shrinkwrap, tsgo --noEmit, browser/web-ui smoke). Repo-root npm test runs in CI.

This is a correctness fix (no perf delta), so the improvement is the bug repro above rather than a benchmark number.

Plan: plans/port-gajae-3day-remaining-optimizations.md (WI-1)


Summary by cubic

Fix compaction to keep the last valid cut point when the recent-token budget is overshot within trailing tool results, instead of keeping the entire history. Applied to both packages/coding-agent and packages/agent implementations with tests.

  • Bug Fixes
    • findCutPoint now falls back to the last valid cut point when no cut point exists at or after the budget-crossing entry.
    • Prevents full-history retention when large trailing tool outputs consume the recent budget; added tests covering the overshoot case.

Written for commit f154622. Summary will update on new commits.

Review in cubic

…vershoots

findCutPoint defaulted cutIndex to cutPoints[0] and only reassigned it when a cut point sat at or after the budget-crossing entry. When the recent-token budget was exceeded entirely within trailing tool results (not valid cut points), nothing reassigned cutIndex and compaction silently retained the entire history. Fall back to the last valid cut point in that case. Applied to both findCutPoint definitions (agent harness + coding-agent core).

Plan: plans/port-gajae-3day-remaining-optimizations.md

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@code-yeongyu code-yeongyu merged commit 4004d59 into main Jun 15, 2026
4 checks passed
@code-yeongyu code-yeongyu deleted the fix/compaction-cutpoint-fallback branch June 15, 2026 09:02
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