#15173: fix(session): reset totalTokens after compaction when estimate unavailablefix(session): reset totalTokens after compaction when estimate unavai…
stale
size: XS
Cluster:
Memory Management Enhancements
## Summary
When estimateCompactionTokensAfter() returns undefined, the pre-compaction totalTokens was left unchanged. This caused shouldRunMemoryFlush() to read stale high values and trigger unnecessary memory flushes immediately after compaction, potentially creating a compaction-flush bounce cycle.
## Changes
- In incrementCompactionCount(), reset totalTokens to 0 when tokensAfter is unavailable
- - The correct value is populated on the next turn via persistSessionUsageUpdate
-
- ## Why this works
- Setting totalTokens to 0 is safe because persistSessionUsageUpdate runs on the very next turn and overwrites it with the actual value from the model response. The brief window where totalTokens=0 prevents the false flush trigger.
-
- Fixes #15101…lable
When estimateCompactionTokensAfter() returns undefined, the pre-compaction totalTokens was left unchanged. This caused shouldRunMemoryFlush() to read stale high values and trigger unnecessary memory flushes immediately after compaction, potentially creating a compaction-flush bounce cycle.
Reset totalTokens to 0 when tokensAfter is unavailable. The correct value is populated on the next turn via persistSessionUsageUpdate.
Fixes #15101
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates `incrementCompactionCount` in `src/auto-reply/reply/session-updates.ts` to avoid stale `totalTokens` values immediately after a compaction when no post-compaction token estimate is available. In the `tokensAfter`-missing path, it now sets `updates.totalTokens = 0` (and clears `inputTokens`/`outputTokens`) so `shouldRunMemoryFlush` won’t see a high pre-compaction token count and immediately trigger a flush.
This fits with the existing `shouldRunMemoryFlush` logic (`src/auto-reply/reply/memory-flush.ts:83-86`), which returns `false` when `totalTokens` is falsy/<=0, so the post-compaction window won’t cause an immediate flush-trigger bounce.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- Change is small, localized to session token bookkeeping after compaction, and aligns with existing `shouldRunMemoryFlush` behavior (treating `totalTokens<=0` as no-flush). No other callsites depend on `totalTokens` being preserved in the specific case where no `tokensAfter` estimate is available.
- src/auto-reply/reply/session-updates.ts
<sub>Last reviewed commit: 657170a</sub>
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#15196: fix: clear stale token totals after compaction
by bufordtjustice2918 · 2026-02-13
92.6%
#4999: fix(memory-flush): use contextTokens instead of totalTokens for thr...
by Farfadium · 2026-01-30
85.1%
#20713: fix(compaction): trigger memory flush after missed compaction cycles
by zerone0x · 2026-02-19
84.4%
#12254: fix: add minimum token guard to prevent double compaction after flush
by Yida-Dev · 2026-02-09
83.2%
#15945: fix(memory-flush): only write memoryFlushCompactionCount when compa...
by aldoeliacim · 2026-02-14
82.3%
#9012: fix(memory): resilient flush for large sessions [AI-assisted]
by cheenu1092-oss · 2026-02-04
80.3%
#9620: fix: increase auto-compaction reserve buffer to 40k tokens
by Arlo83963 · 2026-02-05
78.0%
#8961: feat: smarter compaction tool truncation + token count in system pr...
by SocialNerd42069 · 2026-02-04
77.9%
#12760: fix(memory-flush): fire on every compaction cycle instead of skippi...
by lailoo · 2026-02-09
77.4%
#15322: feat: post-compaction target token trimming + fallback strategy
by echoVic · 2026-02-13
76.8%