fix(agents): clamp embedded run drain polling

This commit is contained in:
Peter Steinberger
2026-05-30 19:21:17 -04:00
parent fab8d29d21
commit edd8aa2f4e
2 changed files with 23 additions and 1 deletions

View File

@@ -393,6 +393,28 @@ describe("embedded-agent runner run registry", () => {
}
});
it("clamps oversized active-run drain poll intervals", async () => {
vi.useFakeTimers();
try {
const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout");
const handle = createRunHandle();
setActiveEmbeddedRun("session-a", handle);
const waitPromise = waitForActiveEmbeddedRuns(undefined, {
pollMs: Number.MAX_SAFE_INTEGER,
});
await Promise.resolve();
expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
clearActiveEmbeddedRun("session-a", handle);
await vi.advanceTimersByTimeAsync(MAX_TIMER_TIMEOUT_MS);
await expect(waitPromise).resolves.toEqual({ drained: true });
} finally {
await vi.runOnlyPendingTimersAsync();
vi.useRealTimers();
}
});
it("shares active run state across distinct module instances", async () => {
const runsA = await importFreshModule<typeof import("./runs.js")>(
import.meta.url,

View File

@@ -617,7 +617,7 @@ export async function waitForActiveEmbeddedRuns(
opts?: { pollMs?: number },
): Promise<{ drained: boolean }> {
const pollMsRaw = opts?.pollMs ?? 250;
const pollMs = Math.max(10, Math.floor(pollMsRaw));
const pollMs = resolveTimerTimeoutMs(pollMsRaw, 250, 10);
if (timeoutMs !== undefined && timeoutMs <= 0) {
return { drained: getActiveEmbeddedRunCount() === 0 };
}