← Back to PRs

#15945: fix(memory-flush): only write memoryFlushCompactionCount when compaction succeeds

by aldoeliacim open 2026-02-14 03:02 View on GitHub →
stale size: XS
## 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