Skip to content

Commit 7d63a7a

Browse files
zknprclaude
andcommitted
fix(codex): task worker atomically claims its job before running (PR #346 review P1)
The launcher-side terminal recheck (926d0f0) kills a cancelled job's fresh worker, but the kill is asynchronous: the detached worker could read its record and push a turn to the shared app-server before dying, landing writes after the user saw cancellation succeed. The worker now claims its job through the same locked updateJobStores transaction before entering the runner — an active, unowned record transitions to running with the worker pid; terminal records are repaired across both stores and skipped; missing or foreign-pid records are skipped without any write. Skips exit 0 with a logged reason and never overwrite the user-visible cancelled status. Covered for both launcher/worker orderings and cancel-between-read-and-claim. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 3a5b37e commit 7d63a7a

4 files changed

Lines changed: 386 additions & 37 deletions

File tree

‎plugins/codex/scripts/codex-companion.mjs‎

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
generateJobId,
3131
getConfig,
3232
listJobs,
33+
resolveJobLogFile,
3334
setConfig,
3435
updateJobStores,
3536
updateState,
@@ -66,7 +67,7 @@ import {
6667
renderTaskResult
6768
} from "./lib/render.mjs";
6869
import { buildTaskDispatchedStatusToken } from "./lib/task-status-token.mjs";
69-
import { commitSpawnedTaskWorker } from "./lib/task-launch-state.mjs";
70+
import { commitSpawnedTaskWorker, runClaimedTaskWorker } from "./lib/task-launch-state.mjs";
7071

7172
const ROOT_DIR = path.resolve(fileURLToPath(new URL("..", import.meta.url)));
7273
const REVIEW_SCHEMA = path.join(ROOT_DIR, "schemas", "review-output.schema.json");
@@ -1228,38 +1229,47 @@ async function handleTaskWorker(argv) {
12281229

12291230
const cwd = resolveCommandCwd(options);
12301231
const workspaceRoot = resolveCommandWorkspace(options);
1231-
const storedJob = readStoredJob(workspaceRoot, options["job-id"]);
1232-
if (!storedJob) {
1233-
throw new Error(`No stored job found for ${options["job-id"]}.`);
1234-
}
1235-
1236-
const request = storedJob.request;
1237-
if (!request || typeof request !== "object") {
1238-
throw new Error(`Stored job ${options["job-id"]} is missing its task request payload.`);
1239-
}
1232+
const jobId = options["job-id"];
1233+
await runClaimedTaskWorker(
1234+
workspaceRoot,
1235+
jobId,
1236+
process.pid,
1237+
async (storedJob) => {
1238+
const request = storedJob.request;
1239+
if (!request || typeof request !== "object") {
1240+
throw new Error(`Stored job ${jobId} is missing its task request payload.`);
1241+
}
12401242

1241-
const { logFile, progress } = createTrackedProgress(
1242-
{
1243-
...storedJob,
1244-
workspaceRoot
1243+
const { logFile, progress } = createTrackedProgress(
1244+
{
1245+
...storedJob,
1246+
workspaceRoot
1247+
},
1248+
{
1249+
logFile: storedJob.logFile ?? null
1250+
}
1251+
);
1252+
await runTrackedJob(
1253+
{
1254+
...storedJob,
1255+
workspaceRoot,
1256+
logFile
1257+
},
1258+
() =>
1259+
executeTaskRun({
1260+
...request,
1261+
onProgress: progress
1262+
}),
1263+
{ logFile }
1264+
);
12451265
},
12461266
{
1247-
logFile: storedJob.logFile ?? null
1267+
onSkip(outcome) {
1268+
const logFile = outcome.job?.logFile ?? resolveJobLogFile(workspaceRoot, jobId);
1269+
appendLogLine(logFile, `Skipped task worker execution because job is ${outcome.status}.`);
1270+
}
12481271
}
12491272
);
1250-
await runTrackedJob(
1251-
{
1252-
...storedJob,
1253-
workspaceRoot,
1254-
logFile
1255-
},
1256-
() =>
1257-
executeTaskRun({
1258-
...request,
1259-
onProgress: progress
1260-
}),
1261-
{ logFile }
1262-
);
12631273
}
12641274

12651275
function reconcileExitedStatusWorker(snapshot) {

‎plugins/codex/scripts/lib/task-launch-state.mjs‎

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,101 @@ function resolveLaunchStatus(stateJob, storedJob = null, fallback = "queued") {
2121
return stateJob?.status ?? storedJob?.status ?? fallback;
2222
}
2323

24+
function buildTerminalJob(stateJob, storedJob, status) {
25+
const terminalSource = stateJob.status === status ? stateJob : storedJob;
26+
return {
27+
...stateJob,
28+
...(storedJob ?? {}),
29+
...(terminalSource ?? {}),
30+
status,
31+
pid: null
32+
};
33+
}
34+
35+
function isClaimableByWorker(job, workerPid) {
36+
return job.status === "queued" || (job.status === "running" && Number(job.pid) === workerPid);
37+
}
38+
39+
function claimTaskWorker(workspaceRoot, jobId, workerPid, options = {}) {
40+
const transaction = updateJobStores(
41+
workspaceRoot,
42+
jobId,
43+
({ stateJob, storedJob }) => {
44+
if (!stateJob || !storedJob) {
45+
return {
46+
value: {
47+
claimed: false,
48+
status: "missing",
49+
job: storedJob ?? stateJob
50+
}
51+
};
52+
}
53+
54+
if (isTerminalJobRecord(stateJob, storedJob)) {
55+
const status = resolveLaunchStatus(stateJob, storedJob, "cancelled");
56+
const terminalJob = buildTerminalJob(stateJob, storedJob, status);
57+
return {
58+
stateJob: terminalJob,
59+
storedJob: terminalJob,
60+
value: {
61+
claimed: false,
62+
status,
63+
job: terminalJob
64+
}
65+
};
66+
}
67+
68+
if (!isClaimableByWorker(stateJob, workerPid) || !isClaimableByWorker(storedJob, workerPid)) {
69+
const status = stateJob.status === "running" || storedJob.status === "running" ? "running" : "queued";
70+
return {
71+
value: {
72+
claimed: false,
73+
status,
74+
job: {
75+
...storedJob,
76+
...stateJob,
77+
status
78+
}
79+
}
80+
};
81+
}
82+
83+
const claimedAt = new Date().toISOString();
84+
const claimedJob = {
85+
...storedJob,
86+
...stateJob,
87+
status: "running",
88+
phase: "starting",
89+
pid: workerPid,
90+
startedAt: stateJob.startedAt ?? storedJob.startedAt ?? claimedAt,
91+
updatedAt: claimedAt
92+
};
93+
return {
94+
stateJob: claimedJob,
95+
storedJob: claimedJob,
96+
value: {
97+
claimed: true,
98+
status: "running",
99+
job: claimedJob
100+
}
101+
};
102+
},
103+
{ beforeCommit: options.beforeCommit }
104+
);
105+
return transaction.value;
106+
}
107+
108+
export async function runClaimedTaskWorker(workspaceRoot, jobId, workerPid, runner, options = {}) {
109+
const outcome = claimTaskWorker(workspaceRoot, jobId, workerPid, options);
110+
if (!outcome.claimed) {
111+
options.onSkip?.(outcome);
112+
return outcome;
113+
}
114+
115+
const runnerResult = await runner(outcome.job);
116+
return { ...outcome, runnerResult };
117+
}
118+
24119
export function commitSpawnedTaskWorker(workspaceRoot, jobId, childPid, options = {}) {
25120
const transaction = updateJobStores(
26121
workspaceRoot,
@@ -38,17 +133,10 @@ export function commitSpawnedTaskWorker(workspaceRoot, jobId, childPid, options
38133

39134
if (isTerminalJobRecord(stateJob, storedJob)) {
40135
const launchStatus = resolveLaunchStatus(stateJob, storedJob, "cancelled");
41-
const terminalSource = stateJob.status === launchStatus ? stateJob : storedJob;
42136
// A process can exit between the two atomic file replacements. Repair
43137
// the non-terminal side from whichever store already won, while the
44138
// lock prevents a concurrent launcher or cancellation from intervening.
45-
const terminalJob = {
46-
...stateJob,
47-
...(storedJob ?? {}),
48-
...(terminalSource ?? {}),
49-
status: launchStatus,
50-
pid: null
51-
};
139+
const terminalJob = buildTerminalJob(stateJob, storedJob, launchStatus);
52140
return {
53141
stateJob: terminalJob,
54142
storedJob: terminalJob,

‎tests/runtime.test.mjs‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,69 @@ test("task --background enqueues a detached worker and exposes per-job status",
12431243
assert.match(resultPayload.storedJob.rendered, /Handled the requested task/);
12441244
});
12451245

1246+
test("task worker logs a cancelled no-op without entering the app-server runner", () => {
1247+
const repo = makeTempDir();
1248+
const binDir = makeTempDir();
1249+
const fakeStatePath = path.join(binDir, "fake-codex-state.json");
1250+
const jobId = "task-worker-cancelled-before-claim";
1251+
installFakeCodex(binDir);
1252+
initGitRepo(repo);
1253+
fs.writeFileSync(path.join(repo, "README.md"), "hello\n", "utf8");
1254+
run("git", ["add", "README.md"], { cwd: repo });
1255+
run("git", ["commit", "-m", "init"], { cwd: repo });
1256+
1257+
const stateDir = resolveStateDir(repo);
1258+
const jobsDir = path.join(stateDir, "jobs");
1259+
const logFile = path.join(jobsDir, `${jobId}.log`);
1260+
const cancelledJob = {
1261+
id: jobId,
1262+
kind: "task",
1263+
jobClass: "task",
1264+
title: "Codex Task",
1265+
status: "cancelled",
1266+
phase: "cancelled",
1267+
pid: null,
1268+
logFile,
1269+
request: {
1270+
cwd: repo,
1271+
model: null,
1272+
effort: null,
1273+
prompt: "This runner must never start.",
1274+
write: true,
1275+
resumeLast: false,
1276+
jobId
1277+
},
1278+
createdAt: "2026-07-16T12:00:00.000Z",
1279+
updatedAt: "2026-07-16T12:00:01.000Z",
1280+
completedAt: "2026-07-16T12:00:01.000Z",
1281+
cancelledAt: "2026-07-16T12:00:01.000Z",
1282+
errorMessage: "Cancelled by user."
1283+
};
1284+
fs.mkdirSync(jobsDir, { recursive: true });
1285+
fs.writeFileSync(logFile, "[2026-07-16T12:00:00.000Z] Queued for background execution.\n", "utf8");
1286+
fs.writeFileSync(path.join(jobsDir, `${jobId}.json`), `${JSON.stringify(cancelledJob, null, 2)}\n`, "utf8");
1287+
fs.writeFileSync(
1288+
path.join(stateDir, "state.json"),
1289+
`${JSON.stringify({ version: 1, config: { stopReviewGate: false }, jobs: [cancelledJob] }, null, 2)}\n`,
1290+
"utf8"
1291+
);
1292+
1293+
const result = run("node", [SCRIPT, "task-worker", "--cwd", repo, "--job-id", jobId], {
1294+
cwd: repo,
1295+
env: buildEnv(binDir)
1296+
});
1297+
1298+
assert.equal(result.status, 0, result.stderr);
1299+
assert.equal(fs.existsSync(fakeStatePath), false, "task worker entered the app-server runner");
1300+
assert.match(fs.readFileSync(logFile, "utf8"), /Skipped task worker execution because job is cancelled\./);
1301+
const stateJob = JSON.parse(fs.readFileSync(path.join(stateDir, "state.json"), "utf8")).jobs[0];
1302+
const storedJob = JSON.parse(fs.readFileSync(path.join(jobsDir, `${jobId}.json`), "utf8"));
1303+
assert.equal(stateJob.status, "cancelled");
1304+
assert.equal(stateJob.errorMessage, "Cancelled by user.");
1305+
assert.equal(storedJob.status, "cancelled");
1306+
assert.equal(storedJob.errorMessage, "Cancelled by user.");
1307+
});
1308+
12461309
test("task --background skips spawning when the queued job was cancelled before worker launch", () => {
12471310
const repo = makeTempDir();
12481311
const binDir = makeTempDir();

0 commit comments

Comments
 (0)