Skip to content

Delete managed worktree branches on cleanup - #2726

Open
plx wants to merge 1 commit into
get-bb:mainfrom
plx:plx/delete-managed-worktree-branch
Open

Delete managed worktree branches on cleanup#2726
plx wants to merge 1 commit into
get-bb:mainfrom
plx:plx/delete-managed-worktree-branch

Conversation

@plx

@plx plx commented Aug 30, 2026

Copy link
Copy Markdown

Human comments

Like #2725, this was discovered by Fable while I had it onboarding me to the codebase and assessing the route to some adjustments/plugins I was curious about making for myself.

Since this bug also seemed to check out, I had Codex verify and then open this PR, too.

What was wrong

removeWorktree removed managed checkout metadata and the directory but never deleted the branch created by git worktree add -B. Successful cleanup therefore left every generated bb/<slug>-<threadId> branch in the source repository, contrary to the worktree guide's cleanup contract reported in #2724.

What changed

  • Read the worktree's symbolic HEAD immediately before removal while holding the existing checkout and worktree-metadata locks.
  • After a successful git worktree remove, delete that branch against the resolved common Git directory while also coordinating through the shared Git-ref mutation lock.
  • Skip branch deletion for detached HEAD or failed symbolic-ref lookup, and never delete the branch when worktree removal itself fails.
  • Report branch deletion failures and Git diagnostics in the environment destroy transcript without failing worktree destruction.
  • Cover successful cleanup and same-name reprovision, detached HEAD, a branch checked out by another worktree, failed setup rollback, and failed worktree removal.
  • Document the non-fatal branch-deletion failure behavior.

The server↔daemon command and result schemas did not change, so HOST_DAEMON_PROTOCOL_VERSION is unchanged.

How you verified

  • Fail-before: the new successful-destroy/reprovision test, failed-setup rollback assertion, and deletion-failure transcript test failed against the old implementation; detached-HEAD and failed-worktree-removal guard tests passed as safety invariants. All pass with this change.
  • TMPDIR=/private/tmp pnpm exec turbo run test --filter=@bb/host-workspace --force --env-mode=loose — 9 files, 233 tests passed. Loose env mode forwards the normalized macOS temp path around the existing /tmp vs /private/tmp assertion on main.
  • pnpm exec turbo run typecheck --filter=@bb/host-workspace --force — passed.
  • pnpm exec turbo run test --filter=@bb/server --force -- --run test/environments/environment-provisioning.test.ts — 11 tests passed, including preservation of the stored branch name during managed reprovision.
  • Live source QA on origin/main: after managed environment destruction, the worktree disappeared but git branch --list still returned bb/branch-cleanup-repro-thr_wzberxex69.
  • Live source QA with this branch: after the same spawn/archive/destroy flow, both the worktree and bb/branch-cleanup-fixed-thr_x4z2wru6gz were gone.
  • pnpm exec oxfmt --check docs/worktrees.md packages/host-workspace/src/provisioning.ts packages/host-workspace/test/provisioning.test.ts
  • git diff origin/main...HEAD --check

Fixes #2724

AGENT GENERATED

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.

Destroying a managed worktree leaves its local branch behind

1 participant