fix(compaction): keep last valid cut point when recent-token budget overshoots#40
Merged
Merged
Conversation
…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>
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.
What
findCutPoint(compaction) defaultedcutIndex = 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 andcutIndexstayed atcutPoints[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.tspackages/agent/src/harness/compaction/compaction.ts(Ported as a senpi-native fix from gajae-code
534b4f0a; senpi keeps itsestimateTokenschar/4 estimator — gajae's nativeestimateEntryTokenswas 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— REDexpected +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— REDexpected +0 to be 1, GREEN after fix.Regression (all green, no skips):
test/compaction.test.ts: 30/30test/harness/compaction.test.ts: 21/21test/compaction/policy suite: 18 files, 125/125Gates: full
npm run checkgreen via pre-commit (biome,check:ts-imports,check:shrinkwrap,tsgo --noEmit, browser/web-ui smoke). Repo-rootnpm testruns 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-agentandpackages/agentimplementations with tests.findCutPointnow falls back to the last valid cut point when no cut point exists at or after the budget-crossing entry.Written for commit f154622. Summary will update on new commits.