Skip to content

Commit 153bd63

Browse files
zknprclaude
andcommitted
fix(codex-rescue): watcher-consistent dispatched context + reject --background/--wait conflict
- Hook: the dispatched-token branch now emits the full watcher instruction (buildWatcherContext) instead of a bare "poll /codex:status", matching the state-inference fallback; removed the now-unused buildDispatchedContext. - Companion: handleTask now rejects conflicting --background + --wait with an error, mirroring the --resume/--fresh guard (PR #346 review P2 — previously --background silently won, ignoring the explicit foreground --wait). Tests: 101/101. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 11db7d5 commit 153bd63

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,9 @@ async function handleTask(argv) {
846846
if (resumeLast && fresh) {
847847
throw new Error("Choose either --resume/--resume-last or --fresh.");
848848
}
849+
if (options.background && options.wait) {
850+
throw new Error("Choose either --background or --wait, not both.");
851+
}
849852
const write = Boolean(options.write);
850853
const taskMetadata = buildTaskRunMetadata({
851854
prompt,

‎plugins/codex/scripts/codex-rescue-completion-hook.mjs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ function buildWatcherContext(jobId) {
2626
return `codex-rescue background job ${jobId} is RUNNING — there is no automatic push notification. To be notified, arm a watcher: run this via the Bash tool with run_in_background=true: node "${COMPANION_SCRIPT_PATH}" status ${jobId} --wait --timeout-ms 1800000 — it blocks until the job is terminal, then exits and re-invokes you. If it returns and the job is still running, re-arm the same command. Do NOT treat the job as done until the watcher reports a terminal status.`;
2727
}
2828

29-
function buildDispatchedContext(jobId) {
30-
return `codex-rescue dispatched background job ${jobId}. No automatic notification will arrive; poll /codex:status ${jobId}.`;
31-
}
32-
3329
function extractBashAutoBackgroundOutputPath(text) {
3430
const prefixIndex = text.indexOf(BASH_AUTO_BACKGROUND_OUTPUT_PREFIX);
3531
if (prefixIndex === -1) {
@@ -174,7 +170,7 @@ function buildCompletionContext(input) {
174170
// Status tokens are emitted by this codex-rescue return, so they are more
175171
// authoritative than any task state that may belong to another concurrent run.
176172
if (token?.status === "dispatched" && token.id) {
177-
return buildDispatchedContext(token.id);
173+
return buildWatcherContext(token.id);
178174
}
179175

180176
// The subagent may paraphrase or drop the dispatched sentinel, so companion

‎tests/codex-rescue-hook.test.mjs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,7 @@ test("codex-rescue hook injects a dispatched line when the dispatched token is p
397397
assert.deepEqual(payload, {
398398
hookSpecificOutput: {
399399
hookEventName: "PostToolUse",
400-
additionalContext:
401-
"codex-rescue dispatched background job task-abc123. No automatic notification will arrive; poll /codex:status task-abc123."
400+
additionalContext: expectedWatcherContext("task-abc123")
402401
}
403402
});
404403
});

‎tests/runtime.test.mjs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,27 @@ test("task --background emits a dispatched token without promising notification"
912912
assert.doesNotMatch(launched.stdout, /will notify|will be notified|you will be notified/i);
913913
});
914914

915+
test("task rejects conflicting background and wait flags before dispatching a job", () => {
916+
const repo = makeTempDir();
917+
const binDir = makeTempDir();
918+
installFakeCodex(binDir, "slow-task");
919+
initGitRepo(repo);
920+
fs.writeFileSync(path.join(repo, "README.md"), "hello\n");
921+
run("git", ["add", "README.md"], { cwd: repo });
922+
run("git", ["commit", "-m", "init"], { cwd: repo });
923+
924+
const result = run("node", [SCRIPT, "task", "--background", "--wait", "investigate the failing test"], {
925+
cwd: repo,
926+
env: buildEnv(binDir)
927+
});
928+
929+
const stateFile = path.join(resolveStateDir(repo), "state.json");
930+
const stateJobs = fs.existsSync(stateFile) ? JSON.parse(fs.readFileSync(stateFile, "utf8")).jobs : [];
931+
assert.deepEqual(stateJobs, []);
932+
assert.notEqual(result.status, 0);
933+
assert.match(result.stderr, /Choose either --background or --wait, not both\./);
934+
});
935+
915936
test("review rejects focus text because it is native-review only", () => {
916937
const repo = makeTempDir();
917938
const binDir = makeTempDir();

0 commit comments

Comments
 (0)