fix(compaction): use symmetric heuristic stats pair and authoritative display - #2407
Open
yangjj-iso wants to merge 4 commits into
Open
fix(compaction): use symmetric heuristic stats pair and authoritative display#2407yangjj-iso wants to merge 4 commits into
yangjj-iso wants to merge 4 commits into
Conversation
…e .js import extensions Assistant-model: CatPaw
…il test The bastani-inc#2052 change made `BoundaryView.displayTokensBefore` a required field and removed the mixed-unit `tokensBefore - region.tokenEstimate` tail derivation, but two pre-existing tests were not updated to match and broke CI. - `expandable-message-hints` (agent-suite) constructs a minimal `BoundaryView` without `displayTokensBefore`, so `updateDisplay()` threw a TypeError on `undefined.toLocaleString()`. Make the field optional and fall back to the heuristic `stats.tokensBefore`, mirroring the fallback `extractDisplayTokensBefore` already applies to legacy entries. In production the field is always supplied, so this only affects callers that build the view directly. - `compaction-small-region-fresh > a load-bearing small region drops an oversized protected tail` (suites) relied on the removed tail derivation, so with `getKeptTailTokenEstimate` now defaulting to 0 the tail was never oversized and `keptTail` came back true. Set the estimate explicitly via `setKeptTailTokenEstimate`, the same path production uses, matching the sibling assertion in `compaction-fresh-window`. Also document the user-facing bastani-inc#2052 fix (symmetric heuristic stats pair plus authoritative "Compacted from N tokens" display) in the changelog, which the original commits omitted.
…mpaction-stats-fork
Collaborator
|
Thanks for the work on this. It looks like #2449 is the newer, focused version of the same #2052 change, and we’re reviewing that PR now. Would you be okay with us closing #2407 in favor of #2449? That would keep the review queue clear and avoid us reviewing or commenting on the older implementation by mistake. If #2407 contains any behavior that you intentionally did not carry into #2449, please let us know so we can account for it before closing. |
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.
Summary
Closes #2052.
The compaction stats mixed three incommensurable token estimators (provider-authoritative, per-message heuristic, and char/4 on serialized text), producing negative
percentReductionvalues when the region was small relative to the preserved tail.Implements option 3 from the issue discussion, as approved by @flora131.
Changes
1. Centralized widening/ratio logic (
computeWholeContextStats)Extracted from the old
withWholeContextStatsinto an exported function incompaction-runner.ts, used by planned, fresh, and extension paths. Both sides of the reported comparison now use the same heuristic: region char/4 + explicit tail estimate.tokensBefore = region.tokenEstimate + tailEstimate(when tail kept)tokensAfter = regionStats.tokensAfter + tailEstimate(when tail kept)2. Removed authoritative-minus-heuristic tail fallback
getKeptTailTokenEstimateincompaction-boundary.tsnow defaults to0instead ofpreparation.tokensBefore - preparation.region.tokenEstimate(authoritative minus heuristic). The old fallback mixed units and could overshoot the real count.Exported
setKeptTailTokenEstimateso test fixtures can set the tail estimate on preparations constructed directly (not throughprepareCompactionBoundary).3. Authoritative display through adapter
VerbatimCompactionDetails.tokensBeforeas an optional field carrying the authoritative count through to the TUI display.CompactionBoundaryMessageComponentusesdisplayTokensBefore(fromentry.tokensBefore/result.tokensBefore) instead ofstats.tokensBeforefor the "Compacted from N tokens" line, projected through theBoundaryViewadapter.details.tokensBeforefall back tostats.tokensBefore.tokensBeforeis preserved onCompactionEntryandVerbatimCompactionResultfor budgeting and threshold decisions.4. No clamping
The percentage is not clamped: a genuine expansion may still be negative.
Regression tests
Added
test/unit/compaction-stats-symmetry.test.ts(11 tests) covering:preserve_recent: 0details.tokensBeforefallbackcomputeWholeContextStatssymmetric ratio propertiesUpdated existing tests in
compaction-fresh-window.test.tsto usesetKeptTailTokenEstimateinstead of the removed mixed-unit fallback.Test results
All 141 affected tests pass. TypeScript type check and Biome lint/format all pass.
Greptile Summary
This change makes compaction reduction statistics compare like-for-like estimates while preserving the provider-authoritative token count in boundary messages. Focused unit and integration coverage confirms that dropping a protected tail includes that tail in the original-context baseline, and that live and resumed views display the authoritative original token count.
Confidence Score: 5/5
No blocking failure remains; the compaction and boundary-rendering paths exercised by the focused checks behave as intended.
No accepted blocking findings remain after exercising fresh compaction with a dropped tail and boundary rendering with distinct heuristic and authoritative counts.
What T-Rex did
Reviews (4): Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." | Re-trigger Greptile