Merge 7f9fbf8faf53bdb9b908a781a949dc925a3bb2ec into 6b4c24c2e55b5b4013277bd799525086f6a0c40f

This commit is contained in:
Kaspre 2026-03-21 04:45:13 +00:00 committed by GitHub
commit 8df8c8306f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 5 deletions

View File

@ -521,20 +521,21 @@ export async function runMemoryFlushIfNeeded(params: {
return result;
},
});
let memoryFlushCompactionCount =
const memoryFlushCompactionCount =
activeSessionEntry?.compactionCount ??
(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 {

View File

@ -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({