// WHICH INTERRUPTED JOB A NEW ONE RESUMES, carried through the action that // makes it (jobs/resumeInterrupted.ts, 2026-10-26). // // The resume pass re-queues a job the way Retry does — runJobSpec(spec), which // calls the kind's server action, which calls runManagedFunction — so nothing // in that path can be handed "this one resumes job X". An async context can: // the pass runs the re-queue inside `runWithResumeTag`, and the first job of // the tagged kind made inside it (makeJob in streamCommand.ts) claims the tag // and records `resumedFrom` / `resumeCount` on its record — before its first // meta write, so a second crash still finds the count on disk. Claimed once: // a sub-job the resumed job itself starts (a hand-off, a refresh) is not a // resume. // // SERVER-ONLY (node:async_hooks). One storage per process, on globalThis, the // house pattern (lib/jobContext.ts). import { AsyncLocalStorage } from "node:async_hooks"; export type ResumeTag = { kind: string; resumedFrom: string; resumeCount: number; // The first line of the resumed job's log. note: string; }; type Held = ResumeTag & { claimed: boolean }; const KEY = Symbol.for("archilyzer.resumeTag"); function storage(): AsyncLocalStorage { const g = globalThis as unknown as Record | undefined>; return (g[KEY] ??= new AsyncLocalStorage()); } export function runWithResumeTag(tag: ResumeTag, fn: () => T): T { return storage().run({ ...tag, claimed: false }, fn); } // The tag for a job of `kind` being made now, once; else null. export function claimResumeTag(kind: string): ResumeTag | null { const held = storage().getStore(); if (!held || held.claimed || held.kind !== kind) return null; held.claimed = true; const { claimed: _claimed, ...tag } = held; return tag; }