Skip to content

Commit a808632

Browse files
Guardiannwclaude
authored andcommitted
Add task --resume-thread <id> for explicit thread resume
--resume/--resume-last picks the newest tracked task thread, which is ambiguous for orchestrators driving several concurrent Codex jobs in one repository: the thread that gets resumed depends on job-store state and session scoping rather than the caller's intent. The app-server layer already supports targeting a thread (runAppServerTurn resumeThreadId); this exposes it on the task subcommand so callers that recorded a threadId from a previous run (task --json) can continue that exact conversation. - task --resume-thread <id> resumes the given thread (foreground and --background; the worker path serializes resumeThreadId through the stored request) - conflicts with --resume/--resume-last/--fresh are rejected - a prompt remains optional, matching --resume-last semantics Co-Authored-By: Claude Fable 5 <[email protected]> (cherry picked from commit 84d82a4)
1 parent 6d20be4 commit a808632

2 files changed

Lines changed: 71 additions & 12 deletions

File tree

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function printUsage() {
8989
" node scripts/codex-companion.mjs setup [--enable-review-gate|--disable-review-gate] [--json]",
9090
" node scripts/codex-companion.mjs review [--wait|--background] [--base <ref>] [--scope <auto|working-tree|branch>]",
9191
" node scripts/codex-companion.mjs adversarial-review [--wait|--background] [--base <ref>] [--scope <auto|working-tree|branch>] [focus text]",
92-
" node scripts/codex-companion.mjs task [--background] [--write] [--resume-last|--resume|--fresh] [--model <model|spark>] [--effort <none|minimal|low|medium|high|xhigh|ultra|max>] [prompt]",
92+
" node scripts/codex-companion.mjs task [--background] [--write] [--resume-last|--resume|--resume-thread <id>|--fresh] [--model <model|spark>] [--effort <none|minimal|low|medium|high|xhigh|ultra|max>] [prompt]",
9393
" node scripts/codex-companion.mjs transfer [--source <claude-jsonl>] [--json]",
9494
" node scripts/codex-companion.mjs status [job-id] [--all] [--json]",
9595
" node scripts/codex-companion.mjs result [job-id] [--json]",
@@ -545,11 +545,11 @@ async function executeTaskRun(request) {
545545

546546
const taskMetadata = buildTaskRunMetadata({
547547
prompt: request.prompt,
548-
resumeLast: request.resumeLast
548+
resumeLast: Boolean(request.resumeLast || request.resumeThreadId)
549549
});
550550

551-
let resumeThreadId = null;
552-
if (request.resumeLast) {
551+
let resumeThreadId = request.resumeThreadId ?? null;
552+
if (!resumeThreadId && request.resumeLast) {
553553
const latestThread = await resolveLatestTrackedTaskThread(workspaceRoot, {
554554
excludeJobId: request.jobId
555555
});
@@ -703,14 +703,15 @@ function buildTaskJob(workspaceRoot, taskMetadata, write) {
703703
});
704704
}
705705

706-
function buildTaskRequest({ cwd, model, effort, prompt, write, resumeLast, jobId }) {
706+
function buildTaskRequest({ cwd, model, effort, prompt, write, resumeLast, resumeThreadId, jobId }) {
707707
return {
708708
cwd,
709709
model,
710710
effort,
711711
prompt,
712712
write,
713713
resumeLast,
714+
resumeThreadId: resumeThreadId ?? null,
714715
jobId
715716
};
716717
}
@@ -755,9 +756,9 @@ function readTaskPrompt(cwd, options, positionals) {
755756
return positionalPrompt || readStdinIfPiped();
756757
}
757758

758-
function requireTaskRequest(prompt, resumeLast) {
759-
if (!prompt && !resumeLast) {
760-
throw new Error("Provide a prompt, a prompt file, piped stdin, or use --resume-last.");
759+
function requireTaskRequest(prompt, resume) {
760+
if (!prompt && !resume) {
761+
throw new Error("Provide a prompt, a prompt file, piped stdin, or use --resume-last/--resume-thread.");
761762
}
762763
}
763764

@@ -1155,7 +1156,7 @@ async function handleReview(argv) {
11551156

11561157
async function handleTask(argv) {
11571158
const { options, positionals } = parseCommandInput(argv, {
1158-
valueOptions: ["model", "effort", "cwd", "prompt-file"],
1159+
valueOptions: ["model", "effort", "cwd", "prompt-file", "resume-thread"],
11591160
booleanOptions: ["json", "write", "resume-last", "resume", "fresh", "background", "wait"],
11601161
aliasMap: {
11611162
m: "model"
@@ -1169,22 +1170,26 @@ async function handleTask(argv) {
11691170
const prompt = readTaskPrompt(cwd, options, positionals);
11701171

11711172
const resumeLast = Boolean(options["resume-last"] || options.resume);
1173+
const resumeThreadId = options["resume-thread"] || null;
11721174
const fresh = Boolean(options.fresh);
11731175
if (resumeLast && fresh) {
11741176
throw new Error("Choose either --resume/--resume-last or --fresh.");
11751177
}
11761178
if (options.background && options.wait) {
11771179
throw new Error("Choose either --background or --wait, not both.");
11781180
}
1181+
if (resumeThreadId && (resumeLast || fresh)) {
1182+
throw new Error("Choose either --resume-thread or --resume/--resume-last/--fresh.");
1183+
}
11791184
const write = Boolean(options.write);
11801185
const taskMetadata = buildTaskRunMetadata({
11811186
prompt,
1182-
resumeLast
1187+
resumeLast: resumeLast || Boolean(resumeThreadId)
11831188
});
11841189

11851190
if (options.background) {
11861191
ensureCodexAvailable(cwd);
1187-
requireTaskRequest(prompt, resumeLast);
1192+
requireTaskRequest(prompt, resumeLast || Boolean(resumeThreadId));
11881193

11891194
const job = buildTaskJob(workspaceRoot, taskMetadata, write);
11901195
const request = buildTaskRequest({
@@ -1194,6 +1199,7 @@ async function handleTask(argv) {
11941199
prompt,
11951200
write,
11961201
resumeLast,
1202+
resumeThreadId,
11971203
jobId: job.id
11981204
});
11991205
const { payload } = enqueueBackgroundTask(cwd, job, request);
@@ -1210,7 +1216,7 @@ async function handleTask(argv) {
12101216
}
12111217

12121218
ensureCodexAvailable(cwd);
1213-
requireTaskRequest(prompt, resumeLast);
1219+
requireTaskRequest(prompt, resumeLast || Boolean(resumeThreadId));
12141220

12151221
const job = buildTaskJob(workspaceRoot, taskMetadata, write);
12161222
const request = buildTaskRequest({
@@ -1220,6 +1226,7 @@ async function handleTask(argv) {
12201226
prompt,
12211227
write,
12221228
resumeLast,
1229+
resumeThreadId,
12231230
jobId: job.id
12241231
});
12251232
await runForegroundTaskWorker(

‎tests/runtime.test.mjs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,58 @@ test("task --resume-last resumes the latest persisted task thread", () => {
561561
assert.equal(result.stdout, "Resumed the prior run.\nFollow-up prompt accepted.\n[[codex-task status=complete]]\n");
562562
});
563563

564+
test("task --resume-thread resumes the specified thread", () => {
565+
const repo = makeTempDir();
566+
const binDir = makeTempDir();
567+
installFakeCodex(binDir);
568+
initGitRepo(repo);
569+
fs.writeFileSync(path.join(repo, "README.md"), "hello\n");
570+
run("git", ["add", "README.md"], { cwd: repo });
571+
run("git", ["commit", "-m", "init"], { cwd: repo });
572+
573+
const firstRun = run("node", [SCRIPT, "task", "--json", "initial task"], {
574+
cwd: repo,
575+
env: buildEnv(binDir)
576+
});
577+
assert.equal(firstRun.status, 0, firstRun.stderr);
578+
const firstPayload = JSON.parse(firstRun.stdout);
579+
assert.ok(firstPayload.threadId, "first run should report a threadId");
580+
581+
const result = run(
582+
"node",
583+
[SCRIPT, "task", "--resume-thread", firstPayload.threadId, "--json", "follow up"],
584+
{
585+
cwd: repo,
586+
env: buildEnv(binDir)
587+
}
588+
);
589+
590+
assert.equal(result.status, 0, result.stderr);
591+
const payload = JSON.parse(result.stdout);
592+
assert.equal(payload.threadId, firstPayload.threadId);
593+
assert.equal(payload.rawOutput, "Resumed the prior run.\nFollow-up prompt accepted.");
594+
});
595+
596+
test("task --resume-thread rejects --resume-last and --fresh", () => {
597+
const repo = makeTempDir();
598+
const binDir = makeTempDir();
599+
installFakeCodex(binDir);
600+
initGitRepo(repo);
601+
602+
for (const conflicting of ["--resume-last", "--fresh"]) {
603+
const result = run(
604+
"node",
605+
[SCRIPT, "task", "--resume-thread", "thr_explicit", conflicting, "follow up"],
606+
{
607+
cwd: repo,
608+
env: buildEnv(binDir)
609+
}
610+
);
611+
assert.equal(result.status, 1);
612+
assert.match(result.stderr, /Choose either --resume-thread/);
613+
}
614+
});
615+
564616
test("task-resume-candidate returns the latest rescue thread from the current session", () => {
565617
const workspace = makeTempDir();
566618
const stateDir = resolveStateDir(workspace);

0 commit comments

Comments
 (0)