Skip to content

Commit 1465145

Browse files
axisrowclaude
andcommitted
fix(worktree): staged-aware recovery instructions + signal/spawn fail-closed runCommand
Cycle-1 review of PR #12 (Codex + Claude subagent). Two real findings: 1. (Codex) The rendered inspection command was plain `git -C <path> diff`, but rescue changes are STAGED — getWorktreeDiff/applyWorktreePatch run `git add -A`, so `git diff` (unstaged-only) can read empty even when staged tracked/binary/formerly-untracked work exists. A user following the instructions would see "no changes", then force-remove the worktree + branch and irreversibly lose the staged work. Fixed: render a HEAD/base- based diff (`git diff <baseCommit> --binary --submodule=diff`) that includes staged state, plus a `git status --porcelain --ignored -uall` step, and a warning that ignored/submodule work is NOT in the diff. Regression test asserts the inspection command references baseCommit (not a bare `diff`). 2. (Claude) runCommand still normalized a signal-terminated OR spawn-failed child to status:0 (`result.status ?? 0`). A killed git apply (OOM, timeout) or missing git binary (ENOENT) read as success → false-success reporting on worktree capture/apply. Under leave-branch this is no longer data-loss (worktree preserved), but it's a correctness defect with the same root cause. Fixed structurally in process.mjs runCommand: `failed = signal != null || error; status = failed ? 1 : ...`. Regression tests: SIGKILL self-kill and ENOENT both report non-zero status. Codex also flagged "worktree isolation is dead code" — IRRELEVANT to this PR by design: this is the library layer; companion (--worktree wiring) is the next PR. The leave-branch invariant claim (Claude) is confirmed: no production Destroy path exists. Verified: worktree 12/12, process 5/5; full npm test 155 pass / 4 pre-existing. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_0189cGaohsYdpkB4otKZnpAt
1 parent c4cbf36 commit 1465145

4 files changed

Lines changed: 64 additions & 8 deletions

File tree

‎plugins/codex/scripts/lib/process.mjs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@ export function runCommand(command, args = [], options = {}) {
1313
windowsHide: true
1414
});
1515

16+
const succeeded = result.status === 0 && result.signal === null && !result.error;
17+
// A child killed by a signal OR that failed to spawn (ENOENT/EAGAIN/maxBuffer)
18+
// returns status:null from spawnSync. Treat any of those as a non-zero exit so
19+
// callers that check status===0 don't mistake an abnormal process for success —
20+
// for worktree patch capture/apply that would mean reporting a killed git op
21+
// (false-success) while the work holds un-captured changes.
22+
const failed = result.signal !== null || result.error;
23+
const status = failed ? 1 : result.status ?? 0;
24+
1625
return {
1726
command,
1827
args,
19-
status: result.status ?? 0,
28+
status,
2029
signal: result.signal ?? null,
2130
stdout: result.stdout ?? "",
2231
stderr: result.stderr ?? "",
23-
error: result.error ?? null
32+
error: succeeded ? null : result.error ?? null
2433
};
2534
}
2635

‎plugins/codex/scripts/lib/render.mjs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,16 @@ export function renderWorktreeTaskResult(execution, session, diff, { jobId = nul
478478

479479
lines.push("### Inspect and remove manually");
480480
lines.push("");
481-
lines.push("The worktree and branch are kept for inspection. Review the diff, then remove:");
481+
lines.push("The worktree and branch are kept for inspection. Rescue changes are staged here — review with a HEAD-based diff that includes staged work:");
482482
lines.push("");
483-
lines.push(`- \`git -C ${session.worktreePath} diff\` (review)`);
484-
lines.push(`- \`git worktree remove --force ${session.worktreePath} && git branch -D ${session.branch}\` (discard)`);
483+
lines.push(`- \`git -C ${session.worktreePath} diff ${session.baseCommit} --binary --submodule=diff\` (review tracked changes vs base, including staged)`);
484+
lines.push(`- \`git -C ${session.worktreePath} status --porcelain --ignored -uall\` (also check for ignored/submodule work the diff will not show)`);
485485
lines.push("");
486-
lines.push("_Ignored files (e.g. .env, dist/) and submodule changes are NOT captured by the patch — copy them out of the worktree before removing._");
486+
lines.push(`Only once you have verified (or copied out) everything you need, remove:`);
487+
lines.push("");
488+
lines.push(`- \`git worktree remove --force ${session.worktreePath} && git branch -D ${session.branch}\``);
489+
lines.push("");
490+
lines.push("_Warning: ignored files (e.g. .env, dist/) and submodule changes are NOT in the diff above and are NOT captured by the apply patch — copy them out of the worktree before removing, or they are lost._");
487491

488492
return `${lines.join("\n").trimEnd()}\n`;
489493
}

‎tests/process.test.mjs‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from "node:test";
22
import assert from "node:assert/strict";
33

4-
import { terminateProcessTree } from "../plugins/codex/scripts/lib/process.mjs";
4+
import { terminateProcessTree, runCommand } from "../plugins/codex/scripts/lib/process.mjs";
55

66
test("terminateProcessTree uses taskkill on Windows", () => {
77
let captured = null;
@@ -84,3 +84,20 @@ test("terminateProcessTree recognizes a missing Windows process with localized o
8484
assert.equal(outcome.delivered, false);
8585
assert.equal(outcome.method, "taskkill");
8686
});
87+
88+
// Regression: a child killed by a signal OR that failed to spawn returns
89+
// status:null from spawnSync. runCommand must NOT normalize that to status:0 —
90+
// callers checking status===0 would mistake a killed/missing git op (e.g. git
91+
// apply killed mid-run, or git missing) for success. For worktree patch capture/
92+
// apply that means false-success while the work holds un-captured changes.
93+
test("runCommand reports a signal-terminated child as failed (non-zero status)", () => {
94+
const result = runCommand(process.execPath, ["-e", "process.kill(process.pid, 'SIGKILL')"]);
95+
assert.ok(result.signal !== null, `expected a signal, got signal=${result.signal}`);
96+
assert.notEqual(result.status, 0, `signal-terminated child must not report status 0, got ${result.status}`);
97+
});
98+
99+
test("runCommand reports a spawn failure (ENOENT) as failed (non-zero status)", () => {
100+
const result = runCommand("/nonexistent-codex-binary-xyz", []);
101+
assert.ok(result.error, `expected an error, got ${result.error}`);
102+
assert.notEqual(result.status, 0, `spawn failure must not report status 0, got ${result.status}`);
103+
});

‎tests/worktree.test.mjs‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,36 @@ test("renderWorktreeTaskResult renders the manual-remove instructions (no destru
275275
assert.match(output, /Worktree \(preserved\)/);
276276
assert.match(output, /git worktree remove --force/);
277277
assert.match(output, /git branch -D/);
278-
assert.match(output, /Ignored files/);
278+
// Inspection command must be HEAD/base-based (shows staged work) — plain
279+
// `git diff` hides staged changes and a user could force-remove thinking
280+
// the worktree is empty (openai#137 review finding).
281+
assert.match(output, /diff .*--binary/);
282+
assert.match(output, new RegExp(`diff ${session.baseCommit}`));
283+
assert.match(output, /ignored/i);
279284
// No keep/discard CLI commands — leave-branch has no destructive action.
280285
assert.doesNotMatch(output, /--action (keep|discard)/);
281286
} finally {
282287
removeSession(session);
283288
}
284289
});
290+
291+
// Regression: rescue changes are STAGED (getWorktreeDiff/applyWorktreePatch run
292+
// git add -A). The rendered inspection command must be HEAD/base-based so it
293+
// shows staged work — plain `git diff` shows only unstaged and could read empty,
294+
// tricking the user into force-removing a worktree that holds real staged work
295+
// (openai#137 review finding). Assert the command references baseCommit, not a
296+
// bare `diff`.
297+
test("renderWorktreeTaskResult inspection command references baseCommit (staged-aware)", () => {
298+
const { repoRoot } = createRepoWithInitialCommit();
299+
const session = createWorktreeSession(repoRoot);
300+
301+
try {
302+
const output = renderWorktreeTaskResult({ rendered: "" }, session, { stat: "", patch: "" });
303+
// The bare `git diff` (unstaged-only) must NOT be the inspection command.
304+
assert.doesNotMatch(output, /git -C [^\s]+ diff\b(?! )/);
305+
// baseCommit must appear in a diff command (HEAD-based => shows staged).
306+
assert.match(output, new RegExp(`diff ${session.baseCommit}`));
307+
} finally {
308+
removeSession(session);
309+
}
310+
});

0 commit comments

Comments
 (0)