Skip to content

fix(agent-branch-start): verify worktree exists before printing Ready:#577

Merged
NagyVikt merged 1 commit into
mainfrom
fix/agent-branch-start-verify-worktree
May 14, 2026
Merged

fix(agent-branch-start): verify worktree exists before printing Ready:#577
NagyVikt merged 1 commit into
mainfrom
fix/agent-branch-start-verify-worktree

Conversation

@NagyVikt
Copy link
Copy Markdown
Collaborator

Summary

git worktree add has been observed (twice in one Claude session on a downstream consumer of this template) to return exit code 0 with the target directory missing on disk. The script then ran OpenSpec / dependency-symlink init against the phantom path, eventually printing `[agent-branch-start] Ready:` and returning success. Callers cd into the vanished directory, claim file locks against it, and silently lose every subsequent edit until `gx branch finish` later complains about a missing tree.

Changes

Two defensive checks in templates/scripts/agent-branch-start.sh:

  1. Immediately after git worktree add returns 0 — fail fast before the init phase runs against a phantom path.
  2. At the top of print_agent_next_steps — covers the case where the dir disappears during init (rare, but cheap to verify).

Both report the branch name + expected path and instruct the operator to run `git worktree prune` before retry.

Test plan

  • Shellcheck via `bash -n` passes.
  • Run `gx branch start --tier T0 "test verify" "claude"` on a clean repo — verify normal happy path still works.
  • Force the failure mode (e.g. `mkdir -p` race or manually removing the worktree dir between `worktree add` and the verify) — confirm exit 1 with the expected error message.

🤖 Generated with Claude Code

`git worktree add` has been observed to return exit code 0 with the
target directory missing on disk. The script then ran OpenSpec /
dependency-symlink init against the phantom path, eventually printing
"[agent-branch-start] Ready:" and returning success. Callers cd into
the vanished directory, claim file locks against it, and silently
lose every subsequent edit until the next finish step complains
about a missing tree.

Two defensive checks added:

1. Right after `git worktree add` returns 0 — fail fast before the
   init phase touches anything in the phantom path.
2. At the top of `print_agent_next_steps` — covers the case where
   the dir gets cleaned up by something during init (less likely,
   but cheap to verify).

Both report the branch name and expected path, and tell the operator
to run `git worktree prune` before retry.
@NagyVikt NagyVikt merged commit 7f4d43d into main May 14, 2026
1 of 2 checks passed
@NagyVikt NagyVikt deleted the fix/agent-branch-start-verify-worktree branch May 14, 2026 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant