Skip to content

fix(finishing-a-development-branch): order branch deletion after worktree removal#1072

Open
mtschoen wants to merge 1 commit intoobra:mainfrom
mtschoen:fix/finishing-branch-cleanup-order
Open

fix(finishing-a-development-branch): order branch deletion after worktree removal#1072
mtschoen wants to merge 1 commit intoobra:mainfrom
mtschoen:fix/finishing-branch-cleanup-order

Conversation

@mtschoen
Copy link
Copy Markdown

@mtschoen mtschoen commented Apr 6, 2026

Summary

Fixes three real-world bugs in finishing-a-development-branch that caused stale worktree-agent-* branches to linger after agents finished work:

  1. Wrong order in Options 1 and 4git branch -d/-D ran before git worktree remove, so deletion silently failed with "branch is checked out at ". Branch deletion is now consolidated into Step 5, after worktree removal.
  2. No self-heal for stale registrations — when a worktree dir has been deleted out-of-band, git worktree remove errors and the .git/worktrees/ registration lingers forever. Added git worktree prune -v as a fallback plus a defensive final prune.
  3. Buggy current-branch grepgit 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

  • Validated against a real cleanup of 12 stale worktree-agent-* branches on a Windows host
  • Verified the awk pattern correctly identifies the feature-branch worktree after git checkout <base>
  • Confirmed git worktree prune -v heals registrations whose gitdirs point to missing locations

…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.
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>
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