Skip to content

Commit 52e4f07

Browse files
committed
Avoid no-op status upserts and preserve dead-pid metadata assertions
1 parent f0b9379 commit 52e4f07

2 files changed

Lines changed: 178 additions & 1 deletion

File tree

‎plugins/codex/scripts/lib/job-control.mjs‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,37 @@ function normalizeTrackedPid(pid) {
1818
return Number.isFinite(numeric) && numeric > 0 ? numeric : null;
1919
}
2020

21+
function normalizeNullable(value) {
22+
return value ?? null;
23+
}
24+
25+
function buildIndexSyncPatch(indexJob, sourceJob) {
26+
return {
27+
id: indexJob.id,
28+
status: sourceJob.status ?? indexJob.status ?? null,
29+
phase: sourceJob.phase ?? null,
30+
pid: Number.isFinite(sourceJob.pid) ? sourceJob.pid : null,
31+
completedAt: sourceJob.completedAt ?? null,
32+
errorMessage: sourceJob.errorMessage ?? null,
33+
threadId: sourceJob.threadId ?? null,
34+
turnId: sourceJob.turnId ?? null,
35+
summary: sourceJob.summary ?? indexJob.summary ?? null
36+
};
37+
}
38+
39+
function indexNeedsSync(indexJob, patch) {
40+
return (
41+
normalizeNullable(indexJob.status) !== normalizeNullable(patch.status) ||
42+
normalizeNullable(indexJob.phase) !== normalizeNullable(patch.phase) ||
43+
normalizeNullable(Number.isFinite(indexJob.pid) ? indexJob.pid : null) !== normalizeNullable(patch.pid) ||
44+
normalizeNullable(indexJob.completedAt) !== normalizeNullable(patch.completedAt) ||
45+
normalizeNullable(indexJob.errorMessage) !== normalizeNullable(patch.errorMessage) ||
46+
normalizeNullable(indexJob.threadId) !== normalizeNullable(patch.threadId) ||
47+
normalizeNullable(indexJob.turnId) !== normalizeNullable(patch.turnId) ||
48+
normalizeNullable(indexJob.summary) !== normalizeNullable(patch.summary)
49+
);
50+
}
51+
2152
/**
2253
* Marks a job as failed when its tracked process has died unexpectedly.
2354
* Re-reads the latest persisted state from disk before writing to guard
@@ -362,9 +393,24 @@ export function buildSingleJobSnapshot(cwd, reference, options = {}) {
362393
throw new Error(`No job found for "${reference}". Run /codex:status to inspect known jobs.`);
363394
}
364395

396+
let storedJob = null;
397+
try {
398+
storedJob = readStoredJob(workspaceRoot, selected.id);
399+
} catch {
400+
storedJob = null;
401+
}
402+
403+
const latest = storedJob ? { ...selected, ...storedJob } : selected;
404+
if (storedJob) {
405+
const indexPatch = buildIndexSyncPatch(selected, latest);
406+
if (indexNeedsSync(selected, indexPatch)) {
407+
upsertJob(workspaceRoot, indexPatch);
408+
}
409+
}
410+
365411
return {
366412
workspaceRoot,
367-
job: enrichJob(selected, { maxProgressLines: options.maxProgressLines })
413+
job: enrichJob(latest, { maxProgressLines: options.maxProgressLines })
368414
};
369415
}
370416

‎tests/runtime.test.mjs‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,135 @@ test("status --wait times out cleanly when a job is still active", () => {
12161216
assert.equal(payload.waitTimedOut, true);
12171217
});
12181218

1219+
test("status <job-id> does not rewrite updatedAt when persisted snapshot matches index", () => {
1220+
const workspace = makeTempDir();
1221+
const stateDir = resolveStateDir(workspace);
1222+
const jobsDir = path.join(stateDir, "jobs");
1223+
fs.mkdirSync(jobsDir, { recursive: true });
1224+
1225+
const olderJobFile = path.join(jobsDir, "task-older.json");
1226+
fs.writeFileSync(
1227+
olderJobFile,
1228+
JSON.stringify(
1229+
{
1230+
id: "task-older",
1231+
status: "completed",
1232+
title: "Codex Task",
1233+
phase: "done",
1234+
pid: null,
1235+
summary: "Older completed task",
1236+
completedAt: "2026-03-18T15:31:00.000Z",
1237+
threadId: "thr_older",
1238+
turnId: "turn_older"
1239+
},
1240+
null,
1241+
2
1242+
) + "\n",
1243+
"utf8"
1244+
);
1245+
1246+
fs.writeFileSync(
1247+
path.join(stateDir, "state.json"),
1248+
JSON.stringify(
1249+
{
1250+
version: 1,
1251+
config: { stopReviewGate: false },
1252+
jobs: [
1253+
{
1254+
id: "task-latest",
1255+
status: "completed",
1256+
title: "Codex Task",
1257+
jobClass: "task",
1258+
phase: "done",
1259+
pid: null,
1260+
summary: "Newest completed task",
1261+
completedAt: "2026-03-18T15:40:00.000Z",
1262+
updatedAt: "2026-03-18T15:40:00.000Z"
1263+
},
1264+
{
1265+
id: "task-older",
1266+
status: "completed",
1267+
title: "Codex Task",
1268+
jobClass: "task",
1269+
phase: "done",
1270+
pid: null,
1271+
summary: "Older completed task",
1272+
completedAt: "2026-03-18T15:31:00.000Z",
1273+
threadId: "thr_older",
1274+
turnId: "turn_older",
1275+
updatedAt: "2026-03-18T15:31:00.000Z"
1276+
}
1277+
]
1278+
},
1279+
null,
1280+
2
1281+
) + "\n",
1282+
"utf8"
1283+
);
1284+
1285+
const before = JSON.parse(fs.readFileSync(path.join(stateDir, "state.json"), "utf8"));
1286+
const beforeOlderUpdatedAt = before.jobs.find((job) => job.id === "task-older")?.updatedAt;
1287+
1288+
const result = run("node", [SCRIPT, "status", "task-older", "--json"], {
1289+
cwd: workspace
1290+
});
1291+
1292+
assert.equal(result.status, 0, result.stderr);
1293+
const payload = JSON.parse(result.stdout);
1294+
assert.equal(payload.job.id, "task-older");
1295+
assert.equal(payload.job.status, "completed");
1296+
1297+
const after = JSON.parse(fs.readFileSync(path.join(stateDir, "state.json"), "utf8"));
1298+
const afterOlder = after.jobs.find((job) => job.id === "task-older");
1299+
assert.equal(after.jobs[0]?.id, "task-latest", "read-only status must not reorder job recency");
1300+
assert.equal(afterOlder?.updatedAt, beforeOlderUpdatedAt, "read-only status must not rewrite updatedAt");
1301+
});
1302+
1303+
test("status --wait tolerates malformed per-job JSON while worker rewrites and falls back to index", () => {
1304+
const workspace = makeTempDir();
1305+
const stateDir = resolveStateDir(workspace);
1306+
const jobsDir = path.join(stateDir, "jobs");
1307+
fs.mkdirSync(jobsDir, { recursive: true });
1308+
1309+
const jobFile = path.join(jobsDir, "task-live.json");
1310+
fs.writeFileSync(jobFile, "{\n \"id\": \"task-live\",\n", "utf8");
1311+
1312+
fs.writeFileSync(
1313+
path.join(stateDir, "state.json"),
1314+
JSON.stringify(
1315+
{
1316+
version: 1,
1317+
config: { stopReviewGate: false },
1318+
jobs: [
1319+
{
1320+
id: "task-live",
1321+
status: "running",
1322+
title: "Codex Task",
1323+
jobClass: "task",
1324+
summary: "Malformed job-file fallback",
1325+
createdAt: "2026-03-18T15:30:00.000Z",
1326+
startedAt: "2026-03-18T15:30:01.000Z",
1327+
updatedAt: "2026-03-18T15:30:02.000Z"
1328+
}
1329+
]
1330+
},
1331+
null,
1332+
2
1333+
) + "\n",
1334+
"utf8"
1335+
);
1336+
1337+
const result = run("node", [SCRIPT, "status", "task-live", "--wait", "--timeout-ms", "25", "--json"], {
1338+
cwd: workspace
1339+
});
1340+
1341+
assert.equal(result.status, 0, result.stderr);
1342+
const payload = JSON.parse(result.stdout);
1343+
assert.equal(payload.job.id, "task-live");
1344+
assert.equal(payload.job.status, "running");
1345+
assert.equal(payload.waitTimedOut, true);
1346+
});
1347+
12191348
test("status --wait marks dead-pid jobs failed instead of timing out", () => {
12201349
const workspace = makeTempDir();
12211350
const stateDir = resolveStateDir(workspace);
@@ -1284,6 +1413,8 @@ test("status --wait marks dead-pid jobs failed instead of timing out", () => {
12841413
const job = state.jobs.find((candidate) => candidate.id === "task-dead");
12851414
assert.equal(job?.status, "failed");
12861415
assert.equal(job?.pid, null);
1416+
assert.equal(job?.jobClass, "task");
1417+
assert.equal(job?.summary, "Investigate flaky test");
12871418

12881419
const stored = JSON.parse(fs.readFileSync(jobFile, "utf8"));
12891420
assert.equal(stored.status, "failed");

0 commit comments

Comments
 (0)