diff --git a/src/auto-reply/reply/agent-runner-memory.ts b/src/auto-reply/reply/agent-runner-memory.ts index ebef0f807ed..48479386dab 100644 --- a/src/auto-reply/reply/agent-runner-memory.ts +++ b/src/auto-reply/reply/agent-runner-memory.ts @@ -521,7 +521,7 @@ export async function runMemoryFlushIfNeeded(params: { return result; }, }); - let memoryFlushCompactionCount = + const memoryFlushCompactionCount = activeSessionEntry?.compactionCount ?? (params.sessionKey ? activeSessionStore?.[params.sessionKey]?.compactionCount : 0) ?? 0; diff --git a/src/auto-reply/reply/reply-state.test.ts b/src/auto-reply/reply/reply-state.test.ts index f83d313e2d3..ac23cb710e9 100644 --- a/src/auto-reply/reply/reply-state.test.ts +++ b/src/auto-reply/reply/reply-state.test.ts @@ -347,6 +347,37 @@ describe("shouldRunMemoryFlush", () => { ).toBe(true); }); + it("triggers on every compaction cycle when flush records pre-increment count (#12590)", () => { + const params = { + contextWindowTokens: 100_000, + reserveTokensFloor: 5_000, + softThresholdTokens: 2_000, + }; + + // Cycle 1: compactionCount=1, no prior flush → triggers + expect( + shouldRunMemoryFlush({ entry: { totalTokens: 95_000, compactionCount: 1 }, ...params }), + ).toBe(true); + + // After flush records compactionCount=1, compaction increments to 2. + // Cycle 2: compactionCount=2, memoryFlushCompactionCount=1 → triggers + expect( + shouldRunMemoryFlush({ + entry: { totalTokens: 95_000, compactionCount: 2, memoryFlushCompactionCount: 1 }, + ...params, + }), + ).toBe(true); + + // After flush records compactionCount=2, compaction increments to 3. + // Cycle 3: compactionCount=3, memoryFlushCompactionCount=2 → triggers + expect( + shouldRunMemoryFlush({ + entry: { totalTokens: 95_000, compactionCount: 3, memoryFlushCompactionCount: 2 }, + ...params, + }), + ).toBe(true); + }); + it("ignores stale cached totals", () => { expect( shouldRunMemoryFlush({