This commit is contained in:
Muchen 2026-03-15 23:43:32 +01:00 committed by GitHub
commit 99d7e73333
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 2 deletions

View File

@ -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-sizebased 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<SessionEntry, "compactionCount" | "memoryFlushCompactionCount">,
): 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;
}

View File

@ -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);
});
});