From b8cf08d775335752e553fd9bccf515b173957420 Mon Sep 17 00:00:00 2001 From: Kaspre Date: Sat, 21 Mar 2026 00:39:31 -0400 Subject: [PATCH 1/2] fix(memory): memoryFlush fires every compaction cycle instead of every other After a memoryFlush during compaction, memoryFlushCompactionCount was reassigned to the post-increment compaction count. This locked both counters to the same value, causing the dedup gate to skip the next flush. The result: flush, skip, flush, skip... Remove the reassignment so memoryFlushCompactionCount stays at the pre-increment value. The next compaction increments compactionCount alone, the counters differ, and the flush fires reliably. Fixes #12590. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/auto-reply/reply/agent-runner-memory.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/auto-reply/reply/agent-runner-memory.ts b/src/auto-reply/reply/agent-runner-memory.ts index 267326a7e20..ebef0f807ed 100644 --- a/src/auto-reply/reply/agent-runner-memory.ts +++ b/src/auto-reply/reply/agent-runner-memory.ts @@ -526,15 +526,16 @@ export async function runMemoryFlushIfNeeded(params: { (params.sessionKey ? activeSessionStore?.[params.sessionKey]?.compactionCount : 0) ?? 0; if (memoryCompactionCompleted) { - const nextCount = await incrementCompactionCount({ + await incrementCompactionCount({ sessionEntry: activeSessionEntry, sessionStore: activeSessionStore, sessionKey: params.sessionKey, storePath: params.storePath, }); - if (typeof nextCount === "number") { - memoryFlushCompactionCount = nextCount; - } + // Do NOT reassign memoryFlushCompactionCount to the post-increment value. + // Keeping it at the pre-increment value ensures the next compaction cycle + // sees different counters, allowing memoryFlush to fire every cycle + // instead of every other. See #12590. } if (params.storePath && params.sessionKey) { try { From 7f9fbf8faf53bdb9b908a781a949dc925a3bb2ec Mon Sep 17 00:00:00 2001 From: Kaspre Date: Sat, 21 Mar 2026 00:45:06 -0400 Subject: [PATCH 2/2] =?UTF-8?q?improve:=20adopt=20let=E2=86=92const=20from?= =?UTF-8?q?=20#12760,=20add=20regression=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Credit to @lailoo (PR #12760) for the let→const improvement and the regression test pattern verifying consecutive compaction cycles. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/auto-reply/reply/agent-runner-memory.ts | 2 +- src/auto-reply/reply/reply-state.test.ts | 31 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) 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({