* fix(#2686): review-fix agent now uses git worktree for isolation The gsd-code-fixer agent operated directly against the main working tree, racing any concurrent foreground session for HEAD, the index, and on-disk files. Added a setup_worktree step (git worktree add /tmp/sv-N-reviewfix HEAD) as the first action before any file operations, with unconditional git worktree remove cleanup on exit. Mirrors the pattern used by all other GSD per-issue agents. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(#2686): address CodeRabbit review — mktemp unique path, branch-aware worktree, tighten test assertions - Use mktemp -d for unique worktree path (prevents concurrent-run collision) - Resolve branch via git branch --show-current before worktree add (prevents detached HEAD) - Error-and-exit on worktree add failure instead of force-removing shared path - Test: use .exec().index for checkout position (not indexOf on match string) - Test: match gsd-sdk query commit as well as git commit for ordering assertion - Test: tighten /tmp path assertion to require actual /tmp/sv- assignment Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -209,6 +209,39 @@ If a finding references multiple files (in Fix section or Issue section):
|
||||
|
||||
<execution_flow>
|
||||
|
||||
<step name="setup_worktree">
|
||||
**Isolation: create a dedicated git worktree BEFORE touching any files.**
|
||||
|
||||
This agent runs as a background process that makes commits. Operating on the main working tree would race the foreground session (shared index, HEAD, and on-disk files). Instead, every instance runs in its own isolated worktree.
|
||||
|
||||
```bash
|
||||
# Derive worktree path from padded_phase (parsed from config in next step,
|
||||
# but the shell snippet below is illustrative — adapt once config is parsed).
|
||||
# In practice: parse padded_phase from config first, then run:
|
||||
branch=$(git branch --show-current)
|
||||
test -n "$branch" || { echo "Detached HEAD is not supported for review-fix (#2686)"; exit 1; }
|
||||
wt=$(mktemp -d "/tmp/sv-${padded_phase}-reviewfix-XXXXXX")
|
||||
git worktree add "$wt" "$branch"
|
||||
cd "$wt"
|
||||
```
|
||||
|
||||
Concrete steps:
|
||||
1. Parse `padded_phase` from the `<config>` block (needed for the path).
|
||||
2. Resolve the current branch: `branch=$(git branch --show-current)`. If empty (detached HEAD), print an error and exit — detached-HEAD state is not supported; commits made in a detached-HEAD worktree would not advance the branch.
|
||||
3. Create a unique worktree path: `wt=$(mktemp -d "/tmp/sv-${padded_phase}-reviewfix-XXXXXX")`. The `mktemp` suffix ensures concurrent runs for the same phase do not collide.
|
||||
4. Run `git worktree add "$wt" "$branch"` — this attaches the worktree to the current branch so commits advance it.
|
||||
5. All subsequent file reads, edits, and commits happen inside `$wt`.
|
||||
|
||||
**If `git worktree add` fails**, surface the error and exit — do not force-remove the path, as another concurrent run may be holding it.
|
||||
|
||||
**Cleanup (ALWAYS — even on failure):** After writing REVIEW-FIX.md and before returning to the orchestrator, run:
|
||||
```bash
|
||||
git worktree remove "$wt" --force
|
||||
```
|
||||
|
||||
This cleanup is unconditional — register it mentally as a finally-block obligation. If the agent exits early (config error, no findings, etc.), still run `git worktree remove "$wt" --force` before exit.
|
||||
</step>
|
||||
|
||||
<step name="load_context">
|
||||
**1. Read mandatory files:** Load all files from `<required_reading>` block if present.
|
||||
|
||||
@@ -437,6 +470,8 @@ _Iteration: {N}_
|
||||
|
||||
<critical_rules>
|
||||
|
||||
**ALWAYS run inside the isolated worktree** — set up via `branch=$(git branch --show-current)` + `wt=$(mktemp -d "/tmp/sv-${padded_phase}-reviewfix-XXXXXX")` + `git worktree add "$wt" "$branch"` at the very start (see `setup_worktree` step). Using `mktemp` ensures concurrent runs do not collide. Attaching to `$branch` (not `HEAD`) ensures commits advance the branch. Every file read, edit, and commit must happen inside `$wt`. Run `git worktree remove "$wt" --force` unconditionally when done (treat it as a finally block). If `git worktree add` fails, exit with an error rather than force-removing a path another run may hold. This prevents racing the foreground session on the shared main working tree (#2686).
|
||||
|
||||
**ALWAYS use the Write tool to create files** — never use `Bash(cat << 'EOF')` or heredoc commands for file creation.
|
||||
|
||||
**DO read the actual source file** before applying any fix — never blindly apply REVIEW.md suggestions without understanding current code state.
|
||||
|
||||
Reference in New Issue
Block a user