Problem
/codex:rescue defaults to a write-capable Codex run (--write, agents/codex-rescue.md:36) operating directly in the caller's checkout via the workspace-write sandbox (plugins/codex/scripts/codex-companion.mjs:532). There is no writer isolation (no per-workspace writer lock, no worktree). When Codex runs — especially now that rescue defaults to background (PR #8) — concurrent writers to the same checkout can corrupt each other's work:
- Concurrent uncommitted work — the user has staged/unstaged changes; Codex's edits interleave with them, making it hard to separate what Codex changed.
- Parallel Claude + Codex — Claude Code edits the same files while background rescue is writing → race conditions, merge conflicts, overwritten uncommitted work.
- No safe rollback — unwanted Codex changes require manual
git checkout of individual files or git stash tricks.
How this surfaced
Found by a Codex adversarial review of PR #8 (rescue default → --background). PR #8 fixes the rescue hang (issue #7) but does not touch the --write default, so the race is pre-existing, not introduced by #8. Confirmed by code: gh pr diff 8 --name-only touches only commands/rescue.md, agents/codex-rescue.md, tests/commands.test.mjs — the --write default and workspace-write sandbox predate it. PR #8 merely makes an existing race reachable from one more code path.
Proposed solution
For write-capable rescue tasks, optionally run Codex in an isolated git worktree:
git worktree add .worktrees/codex-<timestamp> -b codex/<timestamp>
Then run the Codex task against the worktree path instead of the main working directory. After completion:
- Keep — generate a patch from the worktree and apply it to the main tree, or let the user merge the branch.
- Discard —
git worktree remove + git branch -D.
Exposed as a --worktree flag on /codex:rescue, default for background write tasks (highest conflict risk). Must account for two related upstream issues:
Upstream
This is a fork-side tracking issue for openai#135, which has a ready closing PR: openai#137 (@peterdrier, feat: add --worktree flag for isolated write-capable rescue tasks, +496/−10, 12 tests in tests/worktree.test.mjs, OPEN 3 months, 0 human reviews). Plan: port openai#137 into this fork, adapt to openai#493/openai#198, open a fork PR.
Cross-links: openai#135 · openai#137 · openai#493 · openai#198 · fork#7 (hang, fixed by fork#8) · fork#8.
Problem
/codex:rescuedefaults to a write-capable Codex run (--write,agents/codex-rescue.md:36) operating directly in the caller's checkout via theworkspace-writesandbox (plugins/codex/scripts/codex-companion.mjs:532). There is no writer isolation (no per-workspace writer lock, no worktree). When Codex runs — especially now that rescue defaults to background (PR #8) — concurrent writers to the same checkout can corrupt each other's work:git checkoutof individual files orgit stashtricks.How this surfaced
Found by a Codex adversarial review of PR #8 (rescue default →
--background). PR #8 fixes the rescue hang (issue #7) but does not touch the--writedefault, so the race is pre-existing, not introduced by #8. Confirmed by code:gh pr diff 8 --name-onlytouches onlycommands/rescue.md,agents/codex-rescue.md,tests/commands.test.mjs— the--writedefault andworkspace-writesandbox predate it. PR #8 merely makes an existing race reachable from one more code path.Proposed solution
For write-capable rescue tasks, optionally run Codex in an isolated git worktree:
Then run the Codex task against the worktree path instead of the main working directory. After completion:
git worktree remove+git branch -D.Exposed as a
--worktreeflag on/codex:rescue, default for background write tasks (highest conflict risk). Must account for two related upstream issues:Upstream
This is a fork-side tracking issue for openai#135, which has a ready closing PR: openai#137 (@peterdrier,
feat: add --worktree flag for isolated write-capable rescue tasks, +496/−10, 12 tests intests/worktree.test.mjs, OPEN 3 months, 0 human reviews). Plan: port openai#137 into this fork, adapt to openai#493/openai#198, open a fork PR.Cross-links: openai#135 · openai#137 · openai#493 · openai#198 · fork#7 (hang, fixed by fork#8) · fork#8.