Skip to content

Pivot out of agent worktree before prune in branch-finish#423

Merged
NagyVikt merged 1 commit intomainfrom
agent/claude/fix-finish-cwd-after-worktree-remove-2026-04-24-18-56
Apr 24, 2026
Merged

Pivot out of agent worktree before prune in branch-finish#423
NagyVikt merged 1 commit intomainfrom
agent/claude/fix-finish-cwd-after-worktree-remove-2026-04-24-18-56

Conversation

@NagyVikt
Copy link
Copy Markdown
Collaborator

Automated by gx branch finish (PR flow).

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: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@NagyVikt NagyVikt merged commit 166d6c1 into main Apr 24, 2026
@NagyVikt NagyVikt deleted the agent/claude/fix-finish-cwd-after-worktree-remove-2026-04-24-18-56 branch April 24, 2026 17:10
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