You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 1a9eb11
Browse filesBrowse the repository at this point in the historyBrowse files
feat(worktree): isolated rescue worktree, leave-branch cleanup (based on openai#137) (#12)
* feat(worktree): isolated rescue worktree, leave-branch cleanup (based on openai#137)
Adds git-worktree isolation for write-capable rescue (/codex:rescue --worktree),
the write-race fix tracked in fork#10 / openai#135. This commit ports the
library layer + tests only; companion (--worktree flag) and markdown wiring
follow in subsequent PRs.
Based on @peterdrier's openai#137, refactored to a "leave-branch"
cleanup model. The original capture-then-remove model (snapshot -> apply patch ->
force-remove worktree + delete branch) had a deep fail-open class: `git add -A`
skips ignored files (.env, dist/), dirty submodules and binary content can't be
fully captured, and there is an inherent TOCTOU window between snapshot and
remove. Four adversarial review cycles against the straight port found 10
data-loss paths of this shape, each fix closing one edge and the next cycle
finding another. Removing the Destroy path removes the entire class by
construction.
Design (leave-branch): cleanupWorktreeSession NEVER removes the worktree or its
branch. keep applies the tracked patch into repoRoot (byte-preserving via
`git diff --cached --binary --output`, no JS-string round-trip) and leaves the
worktree + branch for the user to inspect and remove manually. discard is a
no-op. The cost is one `git worktree remove --force <path> && git branch -D
<branch>` per rescue — acceptable for a rare, high-stakes operation whose whole
point is write-safety. TOCTOU and the discard-after-failed-keep trap close for
free because there is no Destroy path to gate.
Files:
- plugins/codex/scripts/lib/git.mjs: createWorktree, getWorktreeDiff (gitChecked,
throw on failure), applyWorktreePatch (byte-preserving patch transfer). No
removeWorktree/deleteWorktreeBranch/hasIgnoredChanges — those belonged to the
removed Destroy path.
- plugins/codex/scripts/lib/worktree.mjs (NEW): createWorktreeSession /
diffWorktreeSession / cleanupWorktreeSession (leave-branch contract).
- plugins/codex/scripts/lib/render.mjs: renderWorktreeTaskResult /
renderWorktreeCleanupResult — always preservation + manual-remove instructions.
- tests/worktree.test.mjs (NEW, 11 tests): session creation, diff capture, and
the leave-branch invariant (worktree + branch survive keep tracked-only /
ignored-only / mixed tracked+ignored, and discard no-op), byte round-trip,
render. The invariant tests replace the old removal-assertion tests and are
the proof the fail-open class is closed.
Verified: worktree 11/11, full npm test 152 pass / 4 pre-existing (unchanged).
Refs fork#10, openai#135, openai#137 (@peterdrier).
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_0189cGaohsYdpkB4otKZnpAt
* 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
---------
Co-authored-by: axisrow <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Copy file name to clipboardExpand all lines: plugins/codex/scripts/lib/git.mjs
+73Lines changed: 73 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -297,6 +297,79 @@ function buildAdversarialCollectionGuidance(options = {}) {
297
297
return"The repository context below is a lightweight summary. Inspect the target diff yourself with read-only git commands before finalizing findings.";
298
298
}
299
299
300
+
// Worktree isolation for write-capable rescue (write-race fix, openai#135).
301
+
// Ported from @peterdrier's openai/codex-plugin-cc#137, refactored to a
302
+
// "leave-branch" cleanup model: the worktree is NEVER auto-removed by this library —
303
+
// keep applies the tracked patch and leaves the worktree + branch for the user to
304
+
// inspect and remove manually. The original capture-then-remove model had a deep
305
+
// fail-open class (git add -A skips ignored files; dirty submodules and
306
+
// binary content can't be fully captured; TOCTOU between snapshot and remove)
307
+
// — removing the Destroy path removes the class by construction.
lines.push("Codex made no tracked file changes in the worktree.");
476
+
lines.push("");
477
+
}
478
+
479
+
lines.push("### Inspect and remove manually");
480
+
lines.push("");
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:");
482
+
lines.push("");
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)`);
485
+
lines.push("");
486
+
lines.push(`Only once you have verified (or copied out) everything you need, remove:`);
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._");
lines.push(`The worktree \`${session.worktreePath}\` and branch \`${session.branch}\` are preserved regardless of this action — remove them manually after inspecting.`);
0 commit comments