Skip to content

Commit 7f4d43d

Browse files
NagyViktNagyVikt
andauthored
fix(agent-branch-start): verify worktree exists before printing Ready: (#577)
`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. Co-authored-by: NagyVikt <nagy.viktordp@gmail.com>
1 parent afc3428 commit 7f4d43d

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

templates/scripts/agent-branch-start.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,20 @@ print_agent_next_steps() {
417417
base_branch="main"
418418
fi
419419

420+
# Pre-`Ready:` post-condition check. `git worktree add` has been observed
421+
# to return 0 with the worktree dir still missing on disk (race condition
422+
# during the OpenSpec / dependency-symlink phase between `worktree add`
423+
# and now). If we print `Ready:` in that state, callers cd into a
424+
# vanished directory and lose subsequent edits silently. Surface it as
425+
# an exit-1 error instead — operator can retry + `git worktree prune`.
426+
if [[ ! -d "$worktree_path" || ! -e "$worktree_path/.git" ]]; then
427+
printf '[agent-branch-start] ERROR: worktree did not materialize on disk before Ready:\n' >&2
428+
printf '[agent-branch-start] branch: %s\n' "$branch_name" >&2
429+
printf '[agent-branch-start] expected: %s\n' "$worktree_path" >&2
430+
printf '[agent-branch-start] Run `git worktree prune` to clear ghost entries, then retry.\n' >&2
431+
exit 1
432+
fi
433+
420434
echo "[agent-branch-start] Ready:"
421435
echo " branch: ${branch_name}"
422436
echo " worktree: ${worktree_path}"
@@ -864,6 +878,13 @@ if ! worktree_add_output="$(git -C "$repo_root" worktree add -b "$branch_name" "
864878
printf '%s\n' "$worktree_add_output" >&2
865879
exit 1
866880
fi
881+
# git worktree add has been observed to exit 0 while the target dir is
882+
# missing — verify before downstream init runs against a phantom path.
883+
if [[ ! -d "$worktree_path" || ! -e "$worktree_path/.git" ]]; then
884+
printf '[agent-branch-start] ERROR: git worktree add reported success but %s is not a valid worktree.\n' "$worktree_path" >&2
885+
printf '%s\n' "$worktree_add_output" >&2
886+
exit 1
887+
fi
867888
git -C "$repo_root" config "branch.${branch_name}.guardexBase" "$BASE_BRANCH" >/dev/null 2>&1 || true
868889
git -C "$repo_root" config "branch.${branch_name}.guardexWorktreeRoot" "$WORKTREE_ROOT_REL" >/dev/null 2>&1 || true
869890
# Fresh agent branches should start unpublished; clear any inherited base-branch tracking.

0 commit comments

Comments
 (0)