Skip to content

fix(repos): worktrees prune removes worktrees in use, refuses squash-merged ones and removes by default #2341

Description

@dan-petty

Problem Description

devops repos worktrees prune removes worktrees that are in use and refuses worktrees whose work is merged. It also removes by default, although its help text and the docs say "dry-run by default". In one run on a host with 126 worktrees it removed 56 and refused 70. The run took 1 m 8 s.

There are five defects in src/devops_cli/git/worktree_inventory.py and src/devops_cli/commands/repos.py.

  1. A new worktree counts as merged. is_tip_contained_in_other_ref asks whether any other local or remote ref contains the tip. A new worktree has no commit yet, so its tip is the tip of main, and refs/heads/main contains it. The worktree is "clean and contained" and prune removes it. The run reported the worktree w-2336 as removed. Its branch was at the tip of origin/main. That worktree exists again and has 6 uncommitted paths, so an agent uses it. test_worktrees_prune_removes_clean_contained_and_refuses_unmerged requires this behavior.
  2. A pushed branch counts as merged. The check excludes only refs/heads/<branch>. The branch's own remote ref, refs/remotes/origin/<branch>, still counts as "another ref". A worktree whose branch has an open pull request is removable.
  3. A squash-merged branch counts as unmerged. The repository squash-merges, so a branch tip is never an ancestor of main, and the branch is refused for ever. I checked four refused tips with GET /repos/{owner}/{repo}/commits/{sha}/pulls. Each tip is the head of a merged pull request (fix(review): a review records its prompt digest when it starts, and a benchmark arm refuses runs whose prompts or code changed #2134, fix(review): a dependency advisory that admission rejects is not reported #2217, fix(ai): spend ledger records every attempt the gateway failed (#1019) #2140, fix(review): a scanner finding that names a kubernetes object the reviewed commit lacks is rejected as object-not-in-commit #2225). Most refused entries have a remote branch that GitHub deleted after the merge ([gone]).
  4. A failed git worktree remove is reported as removed. prune_worktrees falls back to shutil.rmtree(..., ignore_errors=True) and counts the entry as removed. With --force, a locked worktree makes git worktree remove --force fail. The fallback then deletes the directory of the locked worktree, prints "removed", and git worktree list still shows the entry as locked. The "failed to remove" branch of _render_prune_item is never reached.
  5. A failed git status counts as clean. count_dirty_paths returns 0 when git status fails, so the dirty check passes on an error.

Reproduction Steps

  1. Create a repository with main and a remote origin.
  2. Add three worktrees: git worktree add -b fresh ../wt-fresh; a branch with one commit that is pushed to origin; a branch with one commit that is not pushed.
  3. Run devops repos worktrees prune --dry-run -r <repo>.
  4. Lock the unpushed worktree with git worktree lock --reason "agent running" <path>, then run devops repos worktrees prune --force -r <repo>.

Expected vs Actual Behavior

Expected:

  • Step 3 lists no worktree as removable. wt-fresh has no merged work, and the pushed branch has an open pull request.
  • Step 4 keeps the locked worktree and reports it as failed.
  • devops repos worktrees prune without a flag changes nothing.
  • A worktree whose tip is the head of a merged pull request is removable.

Actual:

  • Step 3: would remove ... wt-fresh (clean and contained) and would remove ... wt-pushed (clean and contained). wt-local is refused.
  • Step 4: removed .../wt-local. The directory is gone, and git worktree list still shows it as locked.
  • A run without a flag removes worktrees.
  • Squash-merged worktrees are refused as "unmerged (tip not contained in any other ref)".

Proposed fix

  • Merged means a merged pull request. A worktree is removable only when GitHub reports a merged pull request whose head SHA is the tip of the worktree (GET /repos/{owner}/{repo}/commits/{sha}/pulls, merged_at set, head.sha equal to the tip). This works for squash merges. A new worktree and a branch with an open pull request have no merged pull request, so prune keeps them. A worktree that gets a new commit after the merge has a different tip, so prune keeps it too. Remove the "contained in another ref" test. Send the requests through the existing GitHub session, so that they are paced.
  • Preview by default. Without --confirm, prune lists what it would remove and changes nothing. This matches roadmap intake --confirm.
  • Report real failures. When git worktree remove fails, report failed with the git error, keep the directory, and exit non-zero. Remove the shutil.rmtree fallback.
  • Unknown is not clean. When git status fails, refuse the entry.
  • Make status and prune faster. Each entry runs 3 git commands, and the full run takes 20 s for status and 68 s for prune.

Existing tool considered

git branch --merged and git worktree prune do not see squash merges. git cherry compares patch ids, and it fails when a squash combines several commits (several refused entries are 33, 40 and 50 commits ahead). The GitHub pull request record is the one source that shows the merge.

Related

Issue #2338 asks to remove merged worktrees without a manual prune. It says that prune "exists and is safe" and that it is "a dry run by default". Both statements are false today. Fix this item first, or #2338 would automate the removal of worktrees that are in use.

Environment Diagnostics

devops-cli 0.6.5, Linux (WSL2).

Logs, Traces, or Terminal Output

removed .../worktrees/w-2336                 (new worktree, tip is the tip of origin/main)
refused .../worktrees/implement_issue_1019: unmerged (tip not contained in any other ref)   (head of merged PR #2140)
refused .../worktrees/implement_issue_1050: unmerged (tip not contained in any other ref)   (head of merged PR #2134)
Prune complete: 56 removed, 70 refused
Elapsed: 1m 8s

Activity

  1. added
    source/agent
    type/bug
    on Oct 27, 2026
  2. dan-petty commented on Oct 27, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.

    • Type: type/bug. The candidate describes multiple defects in the worktrees prune command's functionality.
    • Priority: P1-High. The fix addresses critical issues that prevent proper worktree management and can cause data loss or incorrect behavior.
    • Value: High. Fixing these defects improves repository stability and prevents unwanted removal of active worktrees.
    • Effort: Medium. The fix requires modifying logic in two Python files to correctly identify merged worktrees and handle edge cases.
    • Related: feat(repos): remove worktrees whose commits are merged without a manual prune #2338, which asks for a different change. Both items concern worktree management but differ in that the candidate fixes defects in pruning behavior, whereas item 2338 proposes a new feature to automatically remove merged worktrees.
  3. added a commit that references this issue on Oct 27, 2026
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

    source/agenttype/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions