fix(agents): bound media duplicate guard age

This commit is contained in:
Peter Steinberger
2026-05-29 01:12:40 -04:00
parent 309fdd95da
commit 8eb5ff08c8
2 changed files with 30 additions and 1 deletions

View File

@@ -378,6 +378,34 @@ describe("image generation task status", () => {
).toBeUndefined();
});
it("does not keep stale recent starts forever for non-finite maxAgeMs", () => {
const now = Date.now();
recordRecentMediaGenerationTaskStartForSession({
sessionKey: "agent:main",
taskKind: IMAGE_GENERATION_TASK_KIND,
sourcePrefix: "image_generate",
taskId: "task-stale",
runId: "run-stale",
taskLabel: "stale prompt",
requestKey: "image-request:stale",
providerId: "xai",
progressSummary: "Generating stale image",
nowMs: now - 1,
});
expect(
findRecentStartedMediaGenerationTaskForSession({
sessionKey: "agent:main",
taskKind: IMAGE_GENERATION_TASK_KIND,
sourcePrefix: "image_generate",
taskLabel: "stale prompt",
requestKey: "image-request:stale",
maxAgeMs: Number.POSITIVE_INFINITY,
nowMs: now,
}),
).toBeUndefined();
});
it("does not block a distinct prompt from a cached active recent start", () => {
recordRecentMediaGenerationTaskStartForSession({
sessionKey: "agent:main",

View File

@@ -1,3 +1,4 @@
import { resolveNonNegativeIntegerOption } from "../shared/number-coercion.js";
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalString,
@@ -220,7 +221,7 @@ export function findRecentStartedMediaGenerationTaskForSession(params: {
return undefined;
}
const nowMs = params.nowMs ?? Date.now();
const maxAgeMs = Math.max(0, Math.floor(params.maxAgeMs));
const maxAgeMs = resolveNonNegativeIntegerOption(params.maxAgeMs, 0);
const taskLabel = normalizeOptionalString(params.taskLabel);
pruneRecentMediaGenerationTaskStarts({ maxAgeMs, nowMs, preserveKey: key });
const entries = recentMediaGenerationTaskStarts.get(key);