-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Recover Codex jobs across workspace scopes #667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
43e61ea
e06cd2c
93beb53
7e107ec
87db4e2
18cdf7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ import { | |
| generateJobId, | ||
| getConfig, | ||
| listJobs, | ||
| resolveJobStartGateFile, | ||
| setConfig, | ||
| upsertJob, | ||
| writeJobFile | ||
|
|
@@ -149,7 +150,8 @@ function parseCommandInput(argv, config = {}) { | |
| } | ||
|
|
||
| function resolveCommandCwd(options = {}) { | ||
| return options.cwd ? path.resolve(process.cwd(), options.cwd) : process.cwd(); | ||
| const requestedCwd = options.cwd ?? process.env.CLAUDE_PROJECT_DIR; | ||
| return requestedCwd ? path.resolve(process.cwd(), requestedCwd) : process.cwd(); | ||
| } | ||
|
|
||
| function resolveCommandWorkspace(options = {}) { | ||
|
|
@@ -668,9 +670,12 @@ async function runForegroundCommand(job, runner, options = {}) { | |
| return execution; | ||
| } | ||
|
|
||
| function spawnDetachedTaskWorker(cwd, jobId) { | ||
| function spawnDetachedTaskWorker(cwd, jobId, startGate) { | ||
| const scriptPath = path.join(ROOT_DIR, "scripts", "codex-companion.mjs"); | ||
| const child = spawn(process.execPath, [scriptPath, "task-worker", "--cwd", cwd, "--job-id", jobId], { | ||
| const child = spawn(process.execPath, [ | ||
| scriptPath, "task-worker", "--cwd", cwd, "--job-id", jobId, | ||
| "--start-gate", startGate | ||
| ], { | ||
| cwd, | ||
| env: process.env, | ||
| detached: true, | ||
|
|
@@ -685,17 +690,36 @@ function enqueueBackgroundTask(cwd, job, request) { | |
| const { logFile } = createTrackedProgress(job); | ||
| appendLogLine(logFile, "Queued for background execution."); | ||
|
|
||
| const child = spawnDetachedTaskWorker(cwd, job.id); | ||
| const queuedRecord = { | ||
| ...job, | ||
| status: "queued", | ||
| phase: "queued", | ||
| pid: child.pid ?? null, | ||
| pid: null, | ||
| logFile, | ||
| request | ||
| }; | ||
| // The job file bootstraps the gated worker, but the shared index must not | ||
| // expose a cancellable job until its detached process has a usable PID. | ||
| writeJobFile(job.workspaceRoot, job.id, queuedRecord); | ||
| upsertJob(job.workspaceRoot, queuedRecord); | ||
|
|
||
| const startGate = resolveJobStartGateFile(job.workspaceRoot, job.id); | ||
| try { | ||
| const child = spawnDetachedTaskWorker(cwd, job.id, startGate); | ||
| const launchRecord = { ...queuedRecord, pid: child.pid ?? null }; | ||
| writeJobFile(job.workspaceRoot, job.id, launchRecord); | ||
| upsertJob(job.workspaceRoot, launchRecord); | ||
| fs.writeFileSync(startGate, "ready\n", "utf8"); | ||
|
Comment on lines
+708
to
+711
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When another process cancels the job after the initial queued record is published but before this launch record is saved, Useful? React with 👍 / 👎. |
||
| } catch (error) { | ||
| const failedRecord = { | ||
| ...queuedRecord, | ||
| status: "failed", | ||
| phase: "failed", | ||
| errorMessage: error instanceof Error ? error.message : String(error) | ||
| }; | ||
| writeJobFile(job.workspaceRoot, job.id, failedRecord); | ||
| upsertJob(job.workspaceRoot, failedRecord); | ||
| throw error; | ||
| } | ||
|
|
||
| return { | ||
| payload: { | ||
|
|
@@ -837,7 +861,7 @@ async function handleTransfer(argv) { | |
|
|
||
| async function handleTaskWorker(argv) { | ||
| const { options } = parseCommandInput(argv, { | ||
| valueOptions: ["cwd", "job-id"] | ||
| valueOptions: ["cwd", "job-id", "start-gate"] | ||
| }); | ||
|
|
||
| if (!options["job-id"]) { | ||
|
|
@@ -846,10 +870,23 @@ async function handleTaskWorker(argv) { | |
|
|
||
| const cwd = resolveCommandCwd(options); | ||
| const workspaceRoot = resolveCommandWorkspace(options); | ||
| if (options["start-gate"]) { | ||
| const deadline = Date.now() + 30000; | ||
| while (!fs.existsSync(options["start-gate"])) { | ||
| if (Date.now() >= deadline) { | ||
| throw new Error(`Timed out waiting for task ${options["job-id"]} to be registered.`); | ||
| } | ||
| await sleep(10); | ||
| } | ||
| fs.unlinkSync(options["start-gate"]); | ||
| } | ||
| const storedJob = readStoredJob(workspaceRoot, options["job-id"]); | ||
| if (!storedJob) { | ||
| throw new Error(`No stored job found for ${options["job-id"]}.`); | ||
| } | ||
| if (storedJob.status === "cancelled") { | ||
| return; | ||
| } | ||
|
|
||
| const request = storedJob.request; | ||
| if (!request || typeof request !== "object") { | ||
|
|
@@ -973,7 +1010,7 @@ async function handleCancel(argv) { | |
| const threadId = existing.threadId ?? job.threadId ?? null; | ||
| const turnId = existing.turnId ?? job.turnId ?? null; | ||
|
|
||
| const interrupt = await interruptAppServerTurn(cwd, { threadId, turnId }); | ||
| const interrupt = await interruptAppServerTurn(workspaceRoot, { threadId, turnId }); | ||
| if (interrupt.attempted) { | ||
| appendLogLine( | ||
| job.logFile, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import fs from "node:fs"; | ||
|
|
||
| import { getSessionRuntimeStatus } from "./codex.mjs"; | ||
| import { getConfig, listJobs, readJobFile, resolveJobFile } from "./state.mjs"; | ||
| import { findJobsAcrossWorkspaces, getConfig, listJobs, readJobFile, resolveJobFile } from "./state.mjs"; | ||
| import { SESSION_ID_ENV } from "./tracked-jobs.mjs"; | ||
| import { resolveWorkspaceRoot } from "./workspace.mjs"; | ||
|
|
||
|
|
@@ -210,6 +210,18 @@ function matchJobReference(jobs, reference, predicate = () => true) { | |
| throw new Error(`No job found for "${reference}". Run /codex:status to list known jobs.`); | ||
| } | ||
|
|
||
| function findCrossWorkspaceJob(reference, predicate = () => true) { | ||
| const matches = findJobsAcrossWorkspaces(reference).filter(predicate); | ||
| if (matches.length === 1) { | ||
| const job = matches[0]; | ||
| return { workspaceRoot: job.workspaceRoot, job }; | ||
| } | ||
| if (matches.length > 1) { | ||
| throw new Error(`Job reference "${reference}" is ambiguous across workspaces. Use the full job id.`); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| export function buildStatusSnapshot(cwd, options = {}) { | ||
| const workspaceRoot = resolveWorkspaceRoot(cwd); | ||
| const config = getConfig(workspaceRoot); | ||
|
|
@@ -242,7 +254,19 @@ export function buildStatusSnapshot(cwd, options = {}) { | |
| export function buildSingleJobSnapshot(cwd, reference, options = {}) { | ||
| const workspaceRoot = resolveWorkspaceRoot(cwd); | ||
| const jobs = sortJobsNewestFirst(listJobs(workspaceRoot)); | ||
| const selected = matchJobReference(jobs, reference); | ||
| let selected; | ||
| try { | ||
| selected = matchJobReference(jobs, reference); | ||
| } catch (error) { | ||
| const crossWorkspace = findCrossWorkspaceJob(reference); | ||
| if (!crossWorkspace) { | ||
| throw error; | ||
| } | ||
| return { | ||
| workspaceRoot: crossWorkspace.workspaceRoot, | ||
| job: enrichJob(crossWorkspace.job, { maxProgressLines: options.maxProgressLines }) | ||
|
Comment on lines
+261
to
+267
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When this fallback finds an active job from another workspace, the rendered status includes Useful? React with 👍 / 👎. |
||
| }; | ||
| } | ||
| if (!selected) { | ||
| throw new Error(`No job found for "${reference}". Run /codex:status to inspect known jobs.`); | ||
| } | ||
|
|
@@ -256,11 +280,23 @@ export function buildSingleJobSnapshot(cwd, reference, options = {}) { | |
| export function resolveResultJob(cwd, reference) { | ||
| const workspaceRoot = resolveWorkspaceRoot(cwd); | ||
| const jobs = sortJobsNewestFirst(reference ? listJobs(workspaceRoot) : filterJobsForCurrentSession(listJobs(workspaceRoot))); | ||
| const selected = matchJobReference( | ||
| jobs, | ||
| reference, | ||
| (job) => job.status === "completed" || job.status === "failed" || job.status === "cancelled" | ||
| ); | ||
| let selected; | ||
| try { | ||
| selected = matchJobReference( | ||
| jobs, | ||
| reference, | ||
| (job) => job.status === "completed" || job.status === "failed" || job.status === "cancelled" | ||
| ); | ||
| } catch (error) { | ||
| const crossWorkspace = findCrossWorkspaceJob( | ||
| reference, | ||
| (job) => job.status === "completed" || job.status === "failed" || job.status === "cancelled" | ||
| ); | ||
| if (crossWorkspace) { | ||
| return crossWorkspace; | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| if (selected) { | ||
| return { workspaceRoot, job: selected }; | ||
|
|
@@ -284,11 +320,19 @@ export function resolveCancelableJob(cwd, reference, options = {}) { | |
| const activeJobs = jobs.filter((job) => job.status === "queued" || job.status === "running"); | ||
|
|
||
| if (reference) { | ||
| const selected = matchJobReference(activeJobs, reference); | ||
| if (!selected) { | ||
| throw new Error(`No active job found for "${reference}".`); | ||
| try { | ||
| const selected = matchJobReference(activeJobs, reference); | ||
| return { workspaceRoot, job: selected }; | ||
| } catch (error) { | ||
| const crossWorkspace = findCrossWorkspaceJob( | ||
| reference, | ||
| (job) => job.status === "queued" || job.status === "running" | ||
| ); | ||
| if (crossWorkspace) { | ||
| return crossWorkspace; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When this fallback resolves a running job from another workspace, Useful? React with 👍 / 👎. |
||
| } | ||
| throw error; | ||
| } | ||
| return { workspaceRoot, job: selected }; | ||
| } | ||
|
|
||
| const sessionScopedActiveJobs = filterJobsForCurrentSession(activeJobs, options); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
cancelruns after enqueue returns but before the worker entersrunTrackedJob, this null PID makesterminateProcessTreea no-op. The worker does not check whether the stored job was cancelled, so it can subsequently overwrite the cancelled record as running and continue executing, including a--writetask. Persist before spawning as intended, but update the queued record withchild.pidbefore returning so cancellation can terminate the worker during this window.Useful? React with 👍 / 👎.