diff --git a/src/auto-reply/reply/memory-flush.ts b/src/auto-reply/reply/memory-flush.ts index 95f6dbaa053..56ca33f2faf 100644 --- a/src/auto-reply/reply/memory-flush.ts +++ b/src/auto-reply/reply/memory-flush.ts @@ -218,11 +218,21 @@ export function shouldRunMemoryFlush(params: { * Returns true when a memory flush has already been performed for the current * compaction cycle. This prevents repeated flush runs within the same cycle — * important for both the token-based and transcript-size–based trigger paths. + * + * Note: When compactionCount is 0 (never compacted), we always return false + * to allow the first memory flush. This fixes the edge case where both + * compactionCount and memoryFlushCompactionCount are 0, which previously + * incorrectly returned true (0 === 0), blocking the first flush. */ export function hasAlreadyFlushedForCurrentCompaction( entry: Pick, ): boolean { const compactionCount = entry.compactionCount ?? 0; const lastFlushAt = entry.memoryFlushCompactionCount; + // When compactionCount is 0, the session has never been compacted. + // Allow flush in this case to enable first-time memory flush. + if (compactionCount === 0) { + return false; + } return typeof lastFlushAt === "number" && lastFlushAt === compactionCount; } diff --git a/src/auto-reply/reply/reply-state.test.ts b/src/auto-reply/reply/reply-state.test.ts index f83d313e2d3..246cb12f508 100644 --- a/src/auto-reply/reply/reply-state.test.ts +++ b/src/auto-reply/reply/reply-state.test.ts @@ -386,12 +386,23 @@ describe("hasAlreadyFlushedForCurrentCompaction", () => { ).toBe(false); }); - it("treats missing compactionCount as 0", () => { + it("returns false when compactionCount is 0 (never compacted)", () => { + // When compactionCount is 0, the session has never been compacted, + // so we should allow the first memory flush. + expect( + hasAlreadyFlushedForCurrentCompaction({ + compactionCount: 0, + memoryFlushCompactionCount: 0, + }), + ).toBe(false); + }); + + it("treats missing compactionCount as 0 and allows flush", () => { expect( hasAlreadyFlushedForCurrentCompaction({ memoryFlushCompactionCount: 0, }), - ).toBe(true); + ).toBe(false); }); });