fix(core): avoid 0% context after /compact #9867
Open
+116
−3
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.
Fixes #9416.
Repro
/compactin the TUI.0% context left · ? for shortcutsExpected
After compaction frees space, the footer should show a non-zero context percentage remaining.
Root cause
After remote compaction, the history includes a
compaction_summary(ResponseItem::Compaction) withencrypted_contentthat can be very large. Core recomputes token usage post-compaction viaSession::recompute_token_usage, which callsContextManager::estimate_token_count. The estimator was treating the fullencrypted_contentlength as prompt-sized text, producing an estimated total >= context window; the UI then clamps to0%remaining.Fix
Cap the token estimate for
ResponseItem::Compaction.encrypted_contentto a small, context-window-relative bound (10% of the effective window, capped at 25k tokens). This keeps post-compaction estimates realistic (and avoids transient0%), without special-casing the UI.Tests
Add a regression unit test that constructs a compacted history with a huge
encrypted_contentblob and asserts the estimated total stays below the configured context window.