#15945: fix(memory-flush): only write memoryFlushCompactionCount when compaction succeeds
stale
size: XS
Cluster:
Memory Management Enhancements
## Problem
`runMemoryFlushIfNeeded` writes `memoryFlushCompactionCount` to the session store **even when compaction does not complete**. This sets `memoryFlushCompactionCount === compactionCount`, causing `shouldRunMemoryFlush` to return `false` on all future turns — permanently blocking auto-compaction for that session.
Sessions get stuck above the token threshold (e.g., 120k+ tokens with a 60k softThreshold) with no way to recover without manual intervention.
## Fix
Only include `memoryFlushCompactionCount` in the session store update when `memoryCompactionCompleted` is true. When compaction fails or doesn't fire, the field is omitted from the update, preserving whatever value was previously stored.
## Changes
- `agent-runner-memory.ts`: Changed `memoryFlushCompactionCount` to `undefined` by default; conditionally spread into the update object only when compaction completed
- Updated existing test assertion to expect `undefined` when compaction doesn't complete (was incorrectly asserting the buggy behavior)
## Testing
All 8 memory-flush tests pass. All 11 `memory-flush.test.ts` unit tests pass.
Closes #15930
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Fixed critical bug where `memoryFlushCompactionCount` was written to session store even when compaction didn't complete, permanently blocking future auto-compaction for affected sessions.
- Changed `memoryFlushCompactionCount` initialization from computed value to `undefined`
- Only writes `memoryFlushCompactionCount` when `memoryCompactionCompleted` is true using conditional spread
- Updated test to expect `undefined` when compaction doesn't complete (was incorrectly asserting buggy behavior)
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The fix is a targeted correction of critical logic error with clear before/after behavior. The change correctly implements conditional field updates using standard TypeScript patterns, existing tests validate both success and failure paths, and the fix directly addresses the root cause described in the issue.
- No files require special attention
<sub>Last reviewed commit: 2912c50</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#12760: fix(memory-flush): fire on every compaction cycle instead of skippi...
by lailoo · 2026-02-09
87.8%
#20713: fix(compaction): trigger memory flush after missed compaction cycles
by zerone0x · 2026-02-19
86.3%
#12254: fix: add minimum token guard to prevent double compaction after flush
by Yida-Dev · 2026-02-09
86.0%
#15196: fix: clear stale token totals after compaction
by bufordtjustice2918 · 2026-02-13
85.4%
#4999: fix(memory-flush): use contextTokens instead of totalTokens for thr...
by Farfadium · 2026-01-30
85.4%
#15173: fix(session): reset totalTokens after compaction when estimate unav...
by EnzoGaillardSystems · 2026-02-13
82.3%
#9012: fix(memory): resilient flush for large sessions [AI-assisted]
by cheenu1092-oss · 2026-02-04
82.3%
#10644: feat: add runDuringHeartbeats option for memory flush (AI Assisted)
by tripphillips · 2026-02-06
82.3%
#14021: feat(compaction): optional memory flush before manual /compact
by phenomenoner · 2026-02-11
80.4%
#18663: feat: progressive compaction escalation and mechanical flush fallback
by Adamya05 · 2026-02-16
80.1%