#15196: fix: clear stale token totals after compaction
stale
size: XS
Cluster:
Memory Management Enhancements
Fixes #15101. Clears stale session token totals when compaction does not provide a valid post-compaction estimate, preventing false memory flush triggers.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates `incrementCompactionCount` to clear cached token totals when compaction doesn’t provide a valid `tokensAfter` estimate, aiming to avoid false memory-flush triggers from stale counts.
The key behavioral change is that `tokensAfter` being omitted now also triggers clearing `totalTokens`/`inputTokens`/`outputTokens`. In the current codebase there are call sites (notably the memory-flush compaction path) that call `incrementCompactionCount` without a post-compaction estimate, and downstream logic (`shouldRunMemoryFlush`) depends on `entry.totalTokens` being present. As written, this can clear totals in those flows and prevent future memory flushes from triggering until totals are repopulated elsewhere.
<h3>Confidence Score: 2/5</h3>
- This PR introduces a behavior change that can break memory-flush triggering when compaction counts are incremented without a post-compaction token estimate.
- Although the intent is to prevent stale totals from causing false triggers, the implementation treats an omitted `tokensAfter` the same as an invalid estimate and clears cached totals. Verified call sites omit `tokensAfter`, and the memory flush decision path requires `totalTokens`, so this can cause real functional regressions and also appears to break an existing test expectation.
- src/auto-reply/reply/session-updates.ts
<sub>Last reviewed commit: ca944c5</sub>
<!-- greptile_other_comments_section -->
<sub>(4/5) You can add custom instructions or style guidelines for the agent [here](https://app.greptile.com/review/github)!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#15173: fix(session): reset totalTokens after compaction when estimate unav...
by EnzoGaillardSystems · 2026-02-13
92.6%
#15945: fix(memory-flush): only write memoryFlushCompactionCount when compa...
by aldoeliacim · 2026-02-14
85.4%
#4999: fix(memory-flush): use contextTokens instead of totalTokens for thr...
by Farfadium · 2026-01-30
85.4%
#12254: fix: add minimum token guard to prevent double compaction after flush
by Yida-Dev · 2026-02-09
83.7%
#20713: fix(compaction): trigger memory flush after missed compaction cycles
by zerone0x · 2026-02-19
83.6%
#9012: fix(memory): resilient flush for large sessions [AI-assisted]
by cheenu1092-oss · 2026-02-04
82.6%
#8961: feat: smarter compaction tool truncation + token count in system pr...
by SocialNerd42069 · 2026-02-04
81.2%
#12760: fix(memory-flush): fire on every compaction cycle instead of skippi...
by lailoo · 2026-02-09
80.0%
#9620: fix: increase auto-compaction reserve buffer to 40k tokens
by Arlo83963 · 2026-02-05
80.0%
#15322: feat: post-compaction target token trimming + fallback strategy
by echoVic · 2026-02-13
79.2%