Skip to content

Commit c571102

Browse files
author
Verso Labs
committed
fix: reconcile liveness across active job consumers
1 parent 1986b7e commit c571102

5 files changed

Lines changed: 48 additions & 6 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
buildSingleJobSnapshot,
3939
buildStatusSnapshot,
4040
readStoredJob,
41+
reconcileJobsLiveness,
4142
resolveCancelableJob,
4243
resolveResultJob,
4344
sortJobsNewestFirst
@@ -336,7 +337,7 @@ async function waitForSingleJobSnapshot(cwd, reference, options = {}) {
336337
async function resolveLatestTrackedTaskThread(cwd, options = {}) {
337338
const workspaceRoot = resolveWorkspaceRoot(cwd);
338339
const sessionId = getCurrentClaudeSessionId();
339-
const jobs = sortJobsNewestFirst(listJobs(workspaceRoot)).filter((job) => job.id !== options.excludeJobId);
340+
const jobs = sortJobsNewestFirst(reconcileJobsLiveness(listJobs(workspaceRoot))).filter((job) => job.id !== options.excludeJobId);
340341
const visibleJobs = filterJobsForCurrentClaudeSession(jobs);
341342
const activeTask = visibleJobs.find((job) => job.jobClass === "task" && (job.status === "queued" || job.status === "running"));
342343
if (activeTask) {
@@ -934,7 +935,7 @@ function handleTaskResumeCandidate(argv) {
934935
const cwd = resolveCommandCwd(options);
935936
const workspaceRoot = resolveCommandWorkspace(options);
936937
const sessionId = getCurrentClaudeSessionId();
937-
const jobs = filterJobsForCurrentClaudeSession(sortJobsNewestFirst(listJobs(workspaceRoot)));
938+
const jobs = filterJobsForCurrentClaudeSession(sortJobsNewestFirst(reconcileJobsLiveness(listJobs(workspaceRoot))));
938939
const candidate = findLatestResumableTaskJob(jobs);
939940

940941
const payload = {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function reconcileJobLiveness(job, options = {}) {
4848
}
4949
}
5050

51-
function reconcileJobsLiveness(jobs, options = {}) {
51+
export function reconcileJobsLiveness(jobs, options = {}) {
5252
return jobs.map((job) => reconcileJobLiveness(job, options));
5353
}
5454

‎plugins/codex/scripts/session-lifecycle-hook.mjs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fs from "node:fs";
44
import process from "node:process";
55

66
import { terminateProcessTree } from "./lib/process.mjs";
7+
import { reconcileJobLiveness } from "./lib/job-control.mjs";
78
import { BROKER_ENDPOINT_ENV } from "./lib/app-server.mjs";
89
import {
910
clearBrokerSession,
@@ -57,7 +58,8 @@ function cleanupSessionJobs(cwd, sessionId) {
5758
}
5859

5960
for (const job of removedJobs) {
60-
const stillRunning = job.status === "queued" || job.status === "running";
61+
const reconciled = reconcileJobLiveness(job);
62+
const stillRunning = reconciled.status === "queued" || reconciled.status === "running";
6163
if (!stillRunning) {
6264
continue;
6365
}

‎plugins/codex/scripts/stop-review-gate-hook.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { fileURLToPath } from "node:url";
99
import { getCodexAvailability } from "./lib/codex.mjs";
1010
import { loadPromptTemplate, interpolateTemplate } from "./lib/prompts.mjs";
1111
import { getConfig, listJobs } from "./lib/state.mjs";
12-
import { sortJobsNewestFirst } from "./lib/job-control.mjs";
12+
import { reconcileJobsLiveness, sortJobsNewestFirst } from "./lib/job-control.mjs";
1313
import { SESSION_ID_ENV } from "./lib/tracked-jobs.mjs";
1414
import { resolveWorkspaceRoot } from "./lib/workspace.mjs";
1515

@@ -145,7 +145,7 @@ function main() {
145145
const workspaceRoot = resolveWorkspaceRoot(cwd);
146146
const config = getConfig(workspaceRoot);
147147

148-
const jobs = sortJobsNewestFirst(filterJobsForCurrentSession(listJobs(workspaceRoot), input));
148+
const jobs = sortJobsNewestFirst(filterJobsForCurrentSession(reconcileJobsLiveness(listJobs(workspaceRoot)), input));
149149
const runningJob = jobs.find((job) => job.status === "queued" || job.status === "running");
150150
const runningTaskNote = runningJob
151151
? `Codex task ${runningJob.id} is still running. Check /codex:status and use /codex:cancel ${runningJob.id} if you want to stop it before ending the session.`

‎tests/runtime.test.mjs‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,26 @@ test("task --resume-last resumes the latest persisted task thread", () => {
503503
assert.equal(result.stdout, "Resumed the prior run.\nFollow-up prompt accepted.\n");
504504
});
505505

506+
test("task --resume-last ignores a stale current-session worker and resumes the prior completed thread", () => {
507+
const repo = makeTempDir();
508+
const binDir = makeTempDir();
509+
installFakeCodex(binDir);
510+
initGitRepo(repo);
511+
fs.writeFileSync(path.join(repo, "README.md"), "hello\n");
512+
run("git", ["add", "README.md"], { cwd: repo });
513+
run("git", ["commit", "-m", "init"], { cwd: repo });
514+
const env = { ...buildEnv(binDir), CODEX_COMPANION_SESSION_ID: "sess-current" };
515+
const first = run("node", [SCRIPT, "task", "initial task"], { cwd: repo, env });
516+
assert.equal(first.status, 0, first.stderr);
517+
const statePath = path.join(resolveStateDir(repo), "state.json");
518+
const state = JSON.parse(fs.readFileSync(statePath, "utf8"));
519+
state.jobs.push({ id: "task-stale", status: "running", title: "Codex Task", jobClass: "task", sessionId: "sess-current", pid: 999999, updatedAt: "2099-01-01T00:00:00.000Z" });
520+
fs.writeFileSync(statePath, `${JSON.stringify(state, null, 2)}\n`, "utf8");
521+
const resumed = run("node", [SCRIPT, "task", "--resume-last", "follow up"], { cwd: repo, env });
522+
assert.equal(resumed.status, 0, resumed.stderr);
523+
assert.equal(resumed.stdout, "Resumed the prior run.\nFollow-up prompt accepted.\n");
524+
});
525+
506526
test("task-resume-candidate returns the latest rescue thread from the current session", () => {
507527
const workspace = makeTempDir();
508528
const stateDir = resolveStateDir(workspace);
@@ -2036,6 +2056,25 @@ test("stop hook logs running tasks to stderr without blocking when the review ga
20362056
assert.match(blocked.stderr, /\/codex:cancel task-live/i);
20372057
});
20382058

2059+
test("stop hook ignores a stale current-session worker when the review gate is disabled", () => {
2060+
const repo = makeTempDir();
2061+
initGitRepo(repo);
2062+
fs.writeFileSync(path.join(repo, "README.md"), "hello\n");
2063+
run("git", ["add", "README.md"], { cwd: repo });
2064+
run("git", ["commit", "-m", "init"], { cwd: repo });
2065+
const stateDir = resolveStateDir(repo);
2066+
fs.mkdirSync(path.join(stateDir, "jobs"), { recursive: true });
2067+
fs.writeFileSync(path.join(stateDir, "state.json"), `${JSON.stringify({ version: 1, config: { stopReviewGate: false }, jobs: [{ id: "task-stale", status: "running", title: "Codex Task", jobClass: "task", sessionId: "sess-current", pid: 999999, updatedAt: "2099-01-01T00:00:00.000Z" }] }, null, 2)}\n`, "utf8");
2068+
const result = run("node", [STOP_HOOK], {
2069+
cwd: repo,
2070+
env: { ...process.env, CODEX_COMPANION_SESSION_ID: "sess-current" },
2071+
input: JSON.stringify({ cwd: repo })
2072+
});
2073+
assert.equal(result.status, 0, result.stderr);
2074+
assert.equal(result.stdout.trim(), "");
2075+
assert.doesNotMatch(result.stderr, /task-stale is still running/i);
2076+
});
2077+
20392078
test("stop hook allows the stop when the review gate is enabled and the stop-time review task is clean", () => {
20402079
const repo = makeTempDir();
20412080
const binDir = makeTempDir();

0 commit comments

Comments
 (0)