fix: clamp reserveTokensFloor to prevent negative memory flush threshold
When reserveTokensFloor equals contextWindowTokens, the flush threshold becomes zero and memory flush never triggers. Clamp reserveTokens so the threshold always remains positive. Fixes #50611
This commit is contained in:
parent
df536c3248
commit
5788066510
@ -197,9 +197,12 @@ export function shouldRunMemoryFlush(params: {
|
||||
return false;
|
||||
}
|
||||
const contextWindow = Math.max(1, Math.floor(params.contextWindowTokens));
|
||||
const reserveTokens = Math.max(0, Math.floor(params.reserveTokensFloor));
|
||||
const softThreshold = Math.max(0, Math.floor(params.softThresholdTokens));
|
||||
const threshold = Math.max(0, contextWindow - reserveTokens - softThreshold);
|
||||
const reserveTokens = Math.min(
|
||||
Math.max(0, Math.floor(params.reserveTokensFloor)),
|
||||
Math.max(0, contextWindow - softThreshold - 1),
|
||||
);
|
||||
const threshold = contextWindow - reserveTokens - softThreshold;
|
||||
if (threshold <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -357,6 +357,17 @@ describe("shouldRunMemoryFlush", () => {
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("triggers when reserveTokensFloor equals contextWindowTokens", () => {
|
||||
expect(
|
||||
shouldRunMemoryFlush({
|
||||
entry: { totalTokens: 199_000, compactionCount: 0 },
|
||||
contextWindowTokens: 200_000,
|
||||
reserveTokensFloor: 200_000,
|
||||
softThresholdTokens: 4_000,
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("hasAlreadyFlushedForCurrentCompaction", () => {
|
||||
@ -483,4 +494,4 @@ describe("incrementCompactionCount", () => {
|
||||
// totalTokens unchanged
|
||||
expect(stored[sessionKey].totalTokens).toBe(180_000);
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user