Files
loki/scripts/test/settle-background-work.ts
T
CatoandClaude Opus 5 aa287aa2cf fix(hosted): a one-shot runner drains its work before it exits, so the thread hears (#721)
A hosted dispatch ran, Hermes did the task, the run closed `done/success`
with its summary — and the conversation that asked for it never heard a word.
Nothing errored. Nothing logged.

`updateOrchestrationRun` fires `void notifyRunClosed(...)` — its contract is
never to slow a close — and that call is what writes the outcome turn. The web
server outlives such a promise for free. `hosted-runner.ts --once` is a
45-second cron tick: it drained the command and called `process.exit(0)`
while the write was still in flight.

- settle-background-work.ts: a bounded drain window (5s,
  HOSTED_RUNNER_DRAIN_MS), paid only by a tick that actually drained
  something. A window rather than a promise registry, because the void-call is
  this codebase's convention at dozens of sites and threading bookkeeping
  through all of them to repair one process boundary is the larger change.
- loki-loop-e2e.ts now reads the run's state when no turn arrives, so the two
  halves can never be confused again: a CLOSED run with no turn is a lost
  notification (the work happened); an OPEN one is a stuck builder. That
  distinction cost an hour today.


Claude-Session: https://claude.ai/code/session_016YJVAanfj8keVSXyZnTemh

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-15 01:08:42 -07:00

47 lines
1.2 KiB
TypeScript

/**
* The one-shot drain window (lib/orchestration/settle-background-work.ts).
* Run: npx tsx scripts/test/settle-background-work.ts
*/
import assert from "node:assert/strict";
import {
settleBackgroundWork,
BACKGROUND_DRAIN_MS,
} from "@/lib/orchestration/settle-background-work";
let pass = 0;
async function check(name: string, fn: () => Promise<void>) {
await fn();
pass++;
console.log(` ✓ ${name}`);
}
async function main() {
await check("it waits the window it was given", async () => {
const waited: number[] = [];
await settleBackgroundWork(1234, async (d) => {
waited.push(d);
});
assert.deepEqual(waited, [1234]);
});
await check("a non-positive or nonsense window waits not at all", async () => {
const waited: number[] = [];
const spy = async (d: number) => {
waited.push(d);
};
await settleBackgroundWork(0, spy);
await settleBackgroundWork(-1, spy);
await settleBackgroundWork(Number.NaN, spy);
assert.deepEqual(waited, []);
});
await check("the default window is long enough for a database round trip", () => {
assert.ok(BACKGROUND_DRAIN_MS >= 1_000);
return Promise.resolve();
});
console.log(`\nsettle-background-work: ${pass} passed`);
}
main();