fix(memoryFlush): guard transcript-size forced flush against repeated runs (#32358)

The `forceFlushTranscriptBytes` path (introduced in d729ab21) bypasses the
`memoryFlushCompactionCount` guard that prevents repeated flushes within the
same compaction cycle. Once the session transcript exceeds 2 MB, memory flush
fires on every single message — even when token count is well under the
compaction threshold.

Extract `hasAlreadyFlushedForCurrentCompaction()` from the inline guard in
`shouldRunMemoryFlush` and apply it to both the token-based and the
transcript-size trigger paths.

Fixes #32317

Signed-off-by: HCL <chenglunhu@gmail.com>
This commit is contained in:
hcl
2026-03-03 09:00:18 +08:00
committed by GitHub
parent 924d9e34ef
commit 503d395780
3 changed files with 55 additions and 4 deletions

View File

@@ -161,11 +161,22 @@ export function shouldRunMemoryFlush(params: {
return false;
}
const compactionCount = params.entry.compactionCount ?? 0;
const lastFlushAt = params.entry.memoryFlushCompactionCount;
if (typeof lastFlushAt === "number" && lastFlushAt === compactionCount) {
if (hasAlreadyFlushedForCurrentCompaction(params.entry)) {
return false;
}
return true;
}
/**
* 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.
*/
export function hasAlreadyFlushedForCurrentCompaction(
entry: Pick<SessionEntry, "compactionCount" | "memoryFlushCompactionCount">,
): boolean {
const compactionCount = entry.compactionCount ?? 0;
const lastFlushAt = entry.memoryFlushCompactionCount;
return typeof lastFlushAt === "number" && lastFlushAt === compactionCount;
}