fix(finishing-a-development-branch): order branch deletion after worktree removal#1072
Open
fix(finishing-a-development-branch): order branch deletion after worktree removal#1072
Conversation
…tree removal Three real-world bugs caused stale `worktree-agent-*` branches to linger after agents finished work: 1. **Wrong order in Options 1 and 4.** `git branch -d/-D` ran *before* `git worktree remove`, so deletion failed with "branch is checked out at <worktree>" — silently in some shells, leaving the branch behind. Branch deletion is now consolidated into Step 5, after worktree removal. 2. **No self-heal for stale registrations.** When a worktree directory has been deleted out-of-band (cleanup script, OS reclaim), `git worktree remove` errors and the registration in `.git/worktrees/` lingers forever, blocking future branch deletion. Added `git worktree prune -v` as a fallback and a defensive final prune. 3. **Buggy current-branch grep.** `git worktree list | grep $(git branch --show-current)` matched the *base* branch (which is current after Step 4's `git checkout <base>`), not the feature branch — and was substring-fragile besides. Replaced with an awk match on column 3 of `git worktree list`, anchored to `[<feature-branch>]`. Also adds a Windows note: this skill must be invoked from the parent context (the main repo), not from inside the worktree being cleaned up, because Windows holds a filesystem lock on a process's own cwd. Validated against a real cleanup of 12 stale worktree-agent branches on a Windows host.
5 tasks
arittr
added a commit
that referenced
this pull request
Apr 10, 2026
- Step 1a is the load-bearing assumption, not just a risk — if it fails, the entire design needs rework. TDD validation must be first impl task. - #1009 resolution depends on Step 1a working, stated explicitly - #574 honestly deferred, not "partially addressed" - Add hooks symlink to Step 1b (PR #965 idea, prevents silent hook loss) - Add stale worktree pruning to Step 5 (PR #1072 idea, one-line self-heal) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes three real-world bugs in
finishing-a-development-branchthat caused staleworktree-agent-*branches to linger after agents finished work:git branch -d/-Dran beforegit worktree remove, so deletion silently failed with "branch is checked out at ". Branch deletion is now consolidated into Step 5, after worktree removal.git worktree removeerrors and the.git/worktrees/registration lingers forever. Addedgit worktree prune -vas a fallback plus a defensive final prune.git worktree list | grep \$(git branch --show-current)matched the base branch (current after Step 4's checkout), not the feature branch. Replaced with an awk match on column 3, anchored to[<feature-branch>].Also added a Windows note: this skill must be invoked from the parent context (main repo), not from inside the worktree being cleaned up — Windows holds a filesystem lock on a process's own cwd.
Test Plan
worktree-agent-*branches on a Windows hostgit checkout <base>git worktree prune -vheals registrations whose gitdirs point to missing locations