test/generic-chat-surface.test.ts holds its process ~300 s after the reaction test passes -> slowest test file
Summary
test/generic-chat-surface.test.ts takes about 5 minutes to finish even though its tests are done in about 30 seconds. The last test, "a person's reaction to the agent's message starts one turn, while the agent's own and reactions it has no stake in start none", passes in under a second, but its file process then sits idle for 300 s before it exits. On a sharded run that one file sets the wall time of its whole shard.
Root cause
The test holds a live turn, delivers a reaction, then in finally releases the held turn and closes the app:
} finally {
mockHeldTurns.release("live");
await genericChat.close();
}
Since reaction turns queue behind a live run, releasing "live" lets the queued reaction turn start right as the app closes. Its surface turn is still in pollRun, and waitRun in src/surfaces/run-wait.ts keeps retrying runs.get against the closed store once a second:
try {
run = await deps.runs.get(runId);
} catch (err) {
if (Date.now() - lastProgressAt >= RUN_STALL_BUDGET_MS) throw err;
run = undefined;
}
RUN_STALL_BUDGET_MS is 300_000, so the poll timer keeps the test process alive for 5 minutes. I confirmed the leftover handle is the run-watch.ts 1 s timer under pollRun -> waitRun.
Repro
At 35c814ab9, with a local Postgres:
NODE_OPTIONS='--test-name-pattern=^a.person.s.reaction.to.the.agent.s.message.starts.one.turn..while.the.agent.s.own.and.reactions.it.has.no.stake.in.start.none$' DATABASE_URL=... node scripts/run-pg-tests.mjs --jobs 1 test/generic-chat-surface.test.ts- Observed: the test passes in 0.66 s, the file reports
# ok test/generic-chat-surface.test.ts (308.5s). - Any other single test in the file finishes in about 2 s.
Suggested fix
Wait for the thread's runs to settle before closing:
} finally {
mockHeldTurns.release("live");
await settle(async () =>
(await built.runs.list({ threadRef: `${CONTAINER}:1002` })).every((run) => isTerminal(run.status)),
);
await genericChat.close();
}
(with import { isTerminal } from "../src/runs/run-store.ts";)
Verified locally at 35c814ab9: the file goes from 308.5 s to 2.7 s and the test still passes.
Separately, it might be worth having waitRun stop early once its store is closed instead of spending the whole stall budget, since any test that closes mid-turn will hit the same wait. I didn't change that.