Skip to content

feat: git worktree isolation for write-capable rescue tasks #135

Open

Description

@peterdrier

Problem

When /codex:rescue runs with --write, it uses Codex's native workspace-write sandbox, which edits files directly in the current working directory. This creates conflicts when:

  1. Concurrent uncommitted work — The user has staged/unstaged changes in their working tree. Codex's edits interleave with the user's in-progress work, making it hard to separate what Codex changed from what the user was already doing.
  2. Parallel Claude + Codex work — Claude Code may be actively editing files in the same directory while Codex rescue is running in the background. Both agents writing to the same files creates race conditions and merge conflicts.
  3. Safe rollback — If Codex's changes are unwanted, the user has to manually git checkout individual files or use git stash tricks. With a worktree, you just git worktree remove and it's gone.

Proposed Solution

For write-capable rescue tasks, optionally create a git worktree so Codex operates in an isolated copy of the repository:

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

This could be exposed as a --worktree flag on /codex:rescue, or made the default for background write tasks where the risk of conflict is highest.

Prior Art

I built a custom Codex skill for Claude Code that uses this exact pattern — subagent creates a worktree, runs codex exec -s workspace-write inside it, then presents a diff for review before applying. It works well in practice and eliminates the concurrent-edit problem entirely.

Alternatives Considered

  • Stash-based isolation: git stash before Codex runs, restore after. Fragile and loses staged vs unstaged distinction.
  • Branch-based: Just create a branch. Doesn't help — still editing the same working directory.
  • Do nothing: Users accept the risk. Works for simple cases but breaks down with background tasks or concurrent editing.

Activity

  1. added a commit that references this issue on Apr 3, 2026
    7eaace5
  2. axisrow commented on Jul 19, 2026

    @axisrow

    +1 — hit this concretely. A Codex adversarial review of a rescue change in our fork (axisrow/codex-plugin-cc#8, defaulting rescue to --background) flagged exactly this: rescue defaults to --write (agents/codex-rescue.md) and runs Codex in the caller's checkout via workspace-write, with no writer isolation, so a background rescue races the main Claude thread / user editing the same files. Verified in code — no per-workspace writer lock, no worktree.

    The race is pre-existing (independent of foreground/background); defaulting rescue to background just makes it reachable from one more path. Confirmed the proposing PR doesn't touch the --write default.

    Tracked in our fork as axisrow/codex-plugin-cc#10. We're porting @peterdrier's #137 (the --worktree flag + isolation) into the fork to validate and adapt it — including the #493 (forward worktree cwd to the detached task) and #198 (don't clean up the worktree before Codex finishes) intersections. Happy to feed back anything we find that #137 needs upstream.

    cc @peterdrier — your #137 looks like the right shape; planning to dogfood it in the fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions