Skip to content

Destroying a managed worktree leaves its local branch behind #2724

Description

@plx

Summary

Destroying a managed worktree removes the checkout and its Git worktree metadata but leaves the generated bb/<slug>-<threadId> local branch in the source repository. The worktree guide promises that the branch is removed with the worktree, so every completed managed environment currently leaves a permanent branch behind.

Versions and environment

  • bb 0.40.0 from source at f4bbc2fe81a9b7639ff9a7396e172bddd89109e4 (origin/main)
  • macOS 27.0, Apple silicon
  • Node.js 22.19.0
  • Local host, fresh dev data directory, managed worktree environment
  • Provider: codex CLI 0.151.0; provider choice is not involved in cleanup

Steps to reproduce

  1. Create a scratch Git repository and register it as a project:

    repo=$(mktemp -d /tmp/bb-managed-branch-repro.XXXXXX)
    git -C "$repo" init -b main
    git -C "$repo" config user.name "BB Repro"
    git -C "$repo" config user.email bb-repro@example.com
    touch "$repo/README.md"
    git -C "$repo" add README.md
    git -C "$repo" commit -m initial
    project_id=$(bb project create --name "Managed branch cleanup repro" --root "$repo" --json | jq -r .id)
  2. Spawn a thread in a managed worktree, then wait for it to become idle:

    thread_id=$(bb thread spawn --project "$project_id" --new-environment worktree --provider codex --permission-mode accept-edits --title "Branch cleanup repro" --prompt "Reply only with ok." --json | jq -r .id)
    bb thread show "$thread_id" --json
  3. Record the generated branch, archive the thread, and wait until its environment reaches destroyed after the managed-environment retirement grace and cleanup sweep:

    branch=$(bb thread show "$thread_id" --json | jq -r .environment.branchName)
    bb thread archive "$thread_id" --json
    bb thread show "$thread_id" --json
  4. Inspect the source repository:

    git -C "$repo" worktree list --porcelain
    git -C "$repo" branch --list "$branch"

The smallest direct reproduction is the same createWorktreeremoveWorktree sequence in @bb/host-workspace; no provider process is required for the leak.

Expected vs actual

Expected after environment status becomes destroyed:

$ git -C "$repo" worktree list --porcelain
worktree /private/tmp/bb-managed-branch-repro.BDoFHZ
HEAD 35593bf58c11ea9e593760e003feb05206f10971
branch refs/heads/main

$ git -C "$repo" branch --list 'bb/branch-cleanup-repro-thr_wzberxex69'

Actual:

$ git -C "$repo" worktree list --porcelain
worktree /private/tmp/bb-managed-branch-repro.BDoFHZ
HEAD 35593bf58c11ea9e593760e003feb05206f10971
branch refs/heads/main

$ git -C "$repo" branch --list 'bb/branch-cleanup-repro-thr_wzberxex69'
  bb/branch-cleanup-repro-thr_wzberxex69

The managed worktree is gone, but its local branch remains.

Evidence

What you ruled out

Suggested priority and effort (optional)

Medium — affects every successfully destroyed managed worktree and accumulates stale refs indefinitely; manual git branch -D is the only cleanup workaround. The fix is localized to host-workspace destruction but needs detached-HEAD and non-fatal deletion-failure handling.

Checks

  • I reproduced this on the latest release or on main, or I say above that I could not.
  • I searched open and closed issues for the same problem.
  • If an agent wrote this, the body ends with > AGENT GENERATED and links the thread or report.

AGENT GENERATED

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-reproBug reproduced again from a clean trusted checkout; see linked reportworkspacesWorktrees, environments, git, shells

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions