Skip to content

Commit 84d82a4

Browse files
Guardiannwclaude
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]>
1 parent db52e28 commit 84d82a4

2 files changed

Lines changed: 70 additions & 11 deletions

File tree

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function printUsage() {
7979
" node scripts/codex-companion.mjs setup [--enable-review-gate|--disable-review-gate] [--json]",
8080
" node scripts/codex-companion.mjs review [--wait|--background] [--base <ref>] [--scope <auto|working-tree|branch>]",
8181
" node scripts/codex-companion.mjs adversarial-review [--wait|--background] [--base <ref>] [--scope <auto|working-tree|branch>] [focus text]",
82-
" node scripts/codex-companion.mjs task [--background] [--write] [--resume-last|--resume|--fresh] [--model <model|spark>] [--effort <none|minimal|low|medium|high|xhigh>] [prompt]",
82+
" 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>] [prompt]",
8383
" node scripts/codex-companion.mjs transfer [--source <claude-jsonl>] [--json]",
8484
" node scripts/codex-companion.mjs status [job-id] [--all] [--json]",
8585
" node scripts/codex-companion.mjs result [job-id] [--json]",
@@ -464,11 +464,11 @@ async function executeTaskRun(request) {
464464

465465
const taskMetadata = buildTaskRunMetadata({
466466
prompt: request.prompt,
467-
resumeLast: request.resumeLast
467+
resumeLast: Boolean(request.resumeLast || request.resumeThreadId)
468468
});
469469

470-
let resumeThreadId = null;
471-
if (request.resumeLast) {
470+
let resumeThreadId = request.resumeThreadId ?? null;
471+
if (!resumeThreadId && request.resumeLast) {
472472
const latestThread = await resolveLatestTrackedTaskThread(workspaceRoot, {
473473
excludeJobId: request.jobId
474474
});
@@ -601,14 +601,15 @@ function buildTaskJob(workspaceRoot, taskMetadata, write) {
601601
});
602602
}
603603

604-
function buildTaskRequest({ cwd, model, effort, prompt, write, resumeLast, jobId }) {
604+
function buildTaskRequest({ cwd, model, effort, prompt, write, resumeLast, resumeThreadId, jobId }) {
605605
return {
606606
cwd,
607607
model,
608608
effort,
609609
prompt,
610610
write,
611611
resumeLast,
612+
resumeThreadId: resumeThreadId ?? null,
612613
jobId
613614
};
614615
}
@@ -649,9 +650,9 @@ function readTaskPrompt(cwd, options, positionals) {
649650
return positionalPrompt || readStdinIfPiped();
650651
}
651652

652-
function requireTaskRequest(prompt, resumeLast) {
653-
if (!prompt && !resumeLast) {
654-
throw new Error("Provide a prompt, a prompt file, piped stdin, or use --resume-last.");
653+
function requireTaskRequest(prompt, resume) {
654+
if (!prompt && !resume) {
655+
throw new Error("Provide a prompt, a prompt file, piped stdin, or use --resume-last/--resume-thread.");
655656
}
656657
}
657658

@@ -761,7 +762,7 @@ async function handleReview(argv) {
761762

762763
async function handleTask(argv) {
763764
const { options, positionals } = parseCommandInput(argv, {
764-
valueOptions: ["model", "effort", "cwd", "prompt-file"],
765+
valueOptions: ["model", "effort", "cwd", "prompt-file", "resume-thread"],
765766
booleanOptions: ["json", "write", "resume-last", "resume", "fresh", "background"],
766767
aliasMap: {
767768
m: "model"
@@ -775,19 +776,23 @@ async function handleTask(argv) {
775776
const prompt = readTaskPrompt(cwd, options, positionals);
776777

777778
const resumeLast = Boolean(options["resume-last"] || options.resume);
779+
const resumeThreadId = options["resume-thread"] || null;
778780
const fresh = Boolean(options.fresh);
779781
if (resumeLast && fresh) {
780782
throw new Error("Choose either --resume/--resume-last or --fresh.");
781783
}
784+
if (resumeThreadId && (resumeLast || fresh)) {
785+
throw new Error("Choose either --resume-thread or --resume/--resume-last/--fresh.");
786+
}
782787
const write = Boolean(options.write);
783788
const taskMetadata = buildTaskRunMetadata({
784789
prompt,
785-
resumeLast
790+
resumeLast: resumeLast || Boolean(resumeThreadId)
786791
});
787792

788793
if (options.background) {
789794
ensureCodexAvailable(cwd);
790-
requireTaskRequest(prompt, resumeLast);
795+
requireTaskRequest(prompt, resumeLast || Boolean(resumeThreadId));
791796

792797
const job = buildTaskJob(workspaceRoot, taskMetadata, write);
793798
const request = buildTaskRequest({
@@ -797,6 +802,7 @@ async function handleTask(argv) {
797802
prompt,
798803
write,
799804
resumeLast,
805+
resumeThreadId,
800806
jobId: job.id
801807
});
802808
const { payload } = enqueueBackgroundTask(cwd, job, request);
@@ -815,6 +821,7 @@ async function handleTask(argv) {
815821
prompt,
816822
write,
817823
resumeLast,
824+
resumeThreadId,
818825
jobId: job.id,
819826
onProgress: progress
820827
}),

‎tests/runtime.test.mjs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,58 @@ 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-thread resumes the specified 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+
515+
const firstRun = run("node", [SCRIPT, "task", "--json", "initial task"], {
516+
cwd: repo,
517+
env: buildEnv(binDir)
518+
});
519+
assert.equal(firstRun.status, 0, firstRun.stderr);
520+
const firstPayload = JSON.parse(firstRun.stdout);
521+
assert.ok(firstPayload.threadId, "first run should report a threadId");
522+
523+
const result = run(
524+
"node",
525+
[SCRIPT, "task", "--resume-thread", firstPayload.threadId, "--json", "follow up"],
526+
{
527+
cwd: repo,
528+
env: buildEnv(binDir)
529+
}
530+
);
531+
532+
assert.equal(result.status, 0, result.stderr);
533+
const payload = JSON.parse(result.stdout);
534+
assert.equal(payload.threadId, firstPayload.threadId);
535+
assert.equal(payload.rawOutput, "Resumed the prior run.\nFollow-up prompt accepted.");
536+
});
537+
538+
test("task --resume-thread rejects --resume-last and --fresh", () => {
539+
const repo = makeTempDir();
540+
const binDir = makeTempDir();
541+
installFakeCodex(binDir);
542+
initGitRepo(repo);
543+
544+
for (const conflicting of ["--resume-last", "--fresh"]) {
545+
const result = run(
546+
"node",
547+
[SCRIPT, "task", "--resume-thread", "thr_explicit", conflicting, "follow up"],
548+
{
549+
cwd: repo,
550+
env: buildEnv(binDir)
551+
}
552+
);
553+
assert.equal(result.status, 1);
554+
assert.match(result.stderr, /Choose either --resume-thread/);
555+
}
556+
});
557+
506558
test("task-resume-candidate returns the latest rescue thread from the current session", () => {
507559
const workspace = makeTempDir();
508560
const stateDir = resolveStateDir(workspace);

0 commit comments

Comments
 (0)