Skip to content

Commit 166d6c1

Browse files
NagyViktNagyViktclaude
authored
Pivot out of agent worktree before prune in branch-finish (#423)
When `gx branch finish --cleanup` is invoked from inside the agent worktree being cleaned up, the later `gx worktree prune` subprocess inherits `cwd=repo_root` from the CLI, so its own active-cwd check can't see that the calling shell is still sitting in the worktree about to be deleted. The prune then removes the directory, the next subprocess spawn fails with `ENOENT uv_cwd`, and `set -e` flips the script exit code to 1 even though the merge and push already succeeded. Pivot the shell to `repo_root` right before prune when the caller's cwd equals the source worktree, so the worktree can be removed without invalidating the caller's cwd. Also guard the trailing "still exists because it is the active shell cwd" message behind an on-disk `-d "$source_worktree"` check so we only print the leave-directory reminder when the worktree really is still there. Co-authored-by: NagyVikt <nagy.viktordp@gmail.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 43aecbe commit 166d6c1

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

scripts/agent-branch-finish.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,13 +830,24 @@ if [[ "$CLEANUP_AFTER_MERGE" -eq 1 ]]; then
830830
if [[ "$DELETE_REMOTE_BRANCH" -eq 1 ]]; then
831831
prune_args+=(--delete-remote-branches)
832832
fi
833+
834+
# Pivot out of the agent worktree before the prune may remove it. The prune
835+
# subprocess inherits cwd=repo_root from the CLI caller, so its own active
836+
# cwd check can't see that our shell is sitting in the worktree it is about
837+
# to delete. Without this pivot the directory disappears mid-call, every
838+
# subsequent subprocess spawn fails with ENOENT uv_cwd, and `set -e` flips
839+
# the script exit code to 1 even though the merge succeeded.
840+
if [[ "$current_worktree" == "$source_worktree" && "$source_worktree" == "${agent_worktree_root}"/* ]]; then
841+
cd "$repo_root" 2>/dev/null || true
842+
fi
843+
833844
if ! run_guardex_cli worktree prune "${prune_args[@]}"; then
834845
echo "[agent-branch-finish] Warning: automatic worktree prune failed." >&2
835846
echo "[agent-branch-finish] You can run manual cleanup: gx cleanup --base ${BASE_BRANCH}" >&2
836847
fi
837848

838849
echo "[agent-branch-finish] Merged '${SOURCE_BRANCH}' into '${BASE_BRANCH}' via ${merge_status} flow and cleaned source branch/worktree."
839-
if [[ "$source_worktree" == "$current_worktree" && "$source_worktree" == "${agent_worktree_root}"/* ]]; then
850+
if [[ "$source_worktree" == "$current_worktree" && "$source_worktree" == "${agent_worktree_root}"/* && -d "$source_worktree" ]]; then
840851
echo "[agent-branch-finish] Current worktree '${source_worktree}' still exists because it is the active shell cwd." >&2
841852
echo "[agent-branch-finish] Leave this directory, then run: gx cleanup --base ${BASE_BRANCH}" >&2
842853
fi

0 commit comments

Comments
 (0)