Skip to content

Commit 9ef699f

Browse files
committed
fix(gateway): bound maintenance run expiry checks
1 parent 912a276 commit 9ef699f

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎src/gateway/server-maintenance.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,52 @@ describe("startGatewayMaintenanceTimers", () => {
394394
stopMaintenanceTimers(timers);
395395
});
396396

397+
it("evicts pending accepted agent dedupe entries with invalid run expiry", async () => {
398+
vi.useFakeTimers();
399+
vi.setSystemTime(new Date("2026-03-22T00:00:00Z"));
400+
const { startGatewayMaintenanceTimers } = await import("./server-maintenance.js");
401+
const deps = createMaintenanceTimerDeps();
402+
const now = Date.now();
403+
deps.dedupe.set("agent:invalid-expiry-pending-agent", {
404+
ts: now - DEDUPE_TTL_MS - 1,
405+
ok: true,
406+
payload: {
407+
runId: "invalid-expiry-pending-agent",
408+
sessionKey: "agent:main:main",
409+
status: "accepted",
410+
expiresAtMs: Number.POSITIVE_INFINITY,
411+
},
412+
});
413+
414+
const timers = startGatewayMaintenanceTimers(deps);
415+
416+
await vi.advanceTimersByTimeAsync(60_000);
417+
418+
expect(deps.dedupe.has("agent:invalid-expiry-pending-agent")).toBe(false);
419+
420+
stopMaintenanceTimers(timers);
421+
});
422+
423+
it("aborts active runs with invalid expiry timestamps", async () => {
424+
vi.useFakeTimers();
425+
vi.setSystemTime(new Date("2026-03-22T00:00:00Z"));
426+
const { startGatewayMaintenanceTimers } = await import("./server-maintenance.js");
427+
const deps = createMaintenanceTimerDeps();
428+
const runId = "run-invalid-expiry";
429+
const activeRun = createActiveRun("main");
430+
activeRun.expiresAtMs = Number.POSITIVE_INFINITY;
431+
deps.chatAbortControllers.set(runId, activeRun);
432+
433+
const timers = startGatewayMaintenanceTimers(deps);
434+
435+
await vi.advanceTimersByTimeAsync(60_000);
436+
437+
expect(activeRun.controller.signal.aborted).toBe(true);
438+
expect(deps.chatAbortControllers.has(runId)).toBe(false);
439+
440+
stopMaintenanceTimers(timers);
441+
});
442+
397443
it("keeps active exec approval dedupe aliases past the normal ttl", async () => {
398444
vi.useFakeTimers();
399445
vi.setSystemTime(new Date("2026-03-22T00:00:00Z"));

‎src/gateway/server-maintenance.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { HealthSummary } from "../commands/health.js";
22
import { sweepStaleRunContexts } from "../infra/agent-events.js";
33
import { cleanOldMedia } from "../media/store.js";
4+
import { isFutureDateTimestampMs } from "../shared/number-coercion.js";
45
import { abortChatRunById, type ChatAbortControllerEntry } from "./chat-abort.js";
56
import { pruneStaleControlPlaneBuckets } from "./control-plane-rate-limit.js";
67
import type { ChatRunState } from "./server-chat-state.js";
@@ -124,7 +125,7 @@ export function startGatewayMaintenanceTimers(params: {
124125
return false;
125126
}
126127
const expiresAtMs = (payload as { expiresAtMs?: unknown }).expiresAtMs;
127-
return typeof expiresAtMs === "number" && Number.isFinite(expiresAtMs) && expiresAtMs > now;
128+
return isFutureDateTimestampMs(expiresAtMs, { nowMs: now });
128129
};
129130
const isActiveRunDedupeKey = (key: string, dedupeEntry: DedupeEntry) => {
130131
if (!key.startsWith("agent:") && !key.startsWith("chat:")) {
@@ -183,7 +184,7 @@ export function startGatewayMaintenanceTimers(params: {
183184
};
184185

185186
for (const [runId, entry] of params.chatAbortControllers) {
186-
if (now <= entry.expiresAtMs) {
187+
if (isFutureDateTimestampMs(entry.expiresAtMs, { nowMs: now })) {
187188
continue;
188189
}
189190
abortChatRunById(

0 commit comments

Comments
 (0)