fix: reconcile orphaned jobs whose launcher process has died
Problem
A tracked job's completion is written by the launcher process that owns it
(tracked-jobs.mjs::runTrackedJob): it marks the job running, awaits the
turn, then writes completed/failed. If that launcher dies before the turn
finishes — a cancelled background job, an ended Claude Code session, a crash, or
the machine sleeping — the completion write never runs and the job is frozen at
running/queued with a now-dead pid.
Nothing reconciles this: buildStatusSnapshot / buildSingleJobSnapshot filter
purely on the stored status string, and the only ESRCH handling in the
codebase is in the cancel path. So /codex:status reports the job running
forever and /codex:result refuses with "still running". In practice these
orphans accumulate (I had several stuck for days).
Fix
listJobs() — which every status/result/cancel path flows through — now probes
the recorded launcher pid with process.kill(pid, 0):
ESRCH(process gone) → reconcile the job tofailed, clear the pid, setcompletedAtand an explanatoryerrorMessage. The correction is persisted to both the state index and the per-job file.EPERM/ unknown → treat as alive (fail safe).- Jobs without a pid, or already in a terminal state, are left untouched.
A dead background turn now surfaces as failed (and is retryable) instead of
hanging running indefinitely.
Tests
Adds coverage in tests/state.test.mjs: dead-pid running/queued reconciliation,
persistence across reads, and live-pid / pidless / finished jobs left untouched.
node --test tests/state.test.mjs → 7/7 green.
Note: 3 unrelated tests (#63/#65/#67) fail under the aggregate npm test due to
pre-existing cross-file state leakage; they pass in isolation and fail
identically on main without this change.
I applied this locally on top of db52e28 (clean apply) and it fixes the zombie jobs on my host — listJobs reconciles, and result starts answering for a job whose launcher died instead of refusing forever.
One gap worth closing: the fail-closed liveness default is the best thing about this PR and nothing tests it.
if (error?.code === "ESRCH") {
return false; // no such process
}
return true; // EPERM (process exists, other owner) or anything else -> assume alive
That last return true is what makes this safer than the alternative in #225, which defaults to dead on any unexpected errno. Wrongly marking a live job as failed is much worse than leaving a zombie, since a codex turn routinely sits silent for many minutes.
But flipping it to return false passes all four tests here, and the rest of the suite. A test that catches it:
test("listJobs keeps a running job when the liveness probe fails for a reason other than ESRCH", () => {
const workspace = makeTempDir();
upsertJob(workspace, { id: "eperm", status: "running", pid: DEAD_PID, workspaceRoot: workspace });
const originalKill = process.kill;
process.kill = () => {
throw Object.assign(new Error("operation not permitted"), { code: "EPERM" });
};
let jobs;
try {
jobs = listJobs(workspace);
} finally {
process.kill = originalKill;
}
assert.equal(jobs.find((job) => job.id === "eperm").status, "running");
});
Mutation-checked both ways: it fails with the default flipped and passes with it restored. Feel free to take it as-is.
Environment: Windows 11, node v24.14.0. Suite went 91/79 pass → 96/84 pass with this PR plus that test, no new failures (the 12 pre-existing failures here are Windows-only — Unix-socket and temp-dir assertions that CI never sees).
Duplicate of #425.