Repository navigation
Description
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.
- A new worktree counts as merged.
is_tip_contained_in_other_refasks whether any other local or remote ref contains the tip. A new worktree has no commit yet, so its tip is the tip ofmain, andrefs/heads/maincontains it. The worktree is "clean and contained" and prune removes it. The run reported the worktreew-2336as removed. Its branch was at the tip oforigin/main. That worktree exists again and has 6 uncommitted paths, so an agent uses it.test_worktrees_prune_removes_clean_contained_and_refuses_unmergedrequires this behavior. - 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. - 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 withGET /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]). - A failed
git worktree removeis reported as removed.prune_worktreesfalls back toshutil.rmtree(..., ignore_errors=True)and counts the entry as removed. With--force, a locked worktree makesgit worktree remove --forcefail. The fallback then deletes the directory of the locked worktree, prints "removed", andgit worktree liststill shows the entry as locked. The "failed to remove" branch of_render_prune_itemis never reached. - A failed
git statuscounts as clean.count_dirty_pathsreturns 0 whengit statusfails, so the dirty check passes on an error.
Reproduction Steps
- Create a repository with
mainand a remoteorigin. - Add three worktrees:
git worktree add -b fresh ../wt-fresh; a branch with one commit that is pushed toorigin; a branch with one commit that is not pushed. - Run
devops repos worktrees prune --dry-run -r <repo>. - Lock the unpushed worktree with
git worktree lock --reason "agent running" <path>, then rundevops repos worktrees prune --force -r <repo>.
Expected vs Actual Behavior
Expected:
- Step 3 lists no worktree as removable.
wt-freshhas 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 prunewithout 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)andwould remove ... wt-pushed (clean and contained).wt-localis refused. - Step 4:
removed .../wt-local. The directory is gone, andgit worktree liststill shows it aslocked. - 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_atset,head.shaequal 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 matchesroadmap intake --confirm. - Report real failures. When
git worktree removefails, reportfailedwith the git error, keep the directory, and exit non-zero. Remove theshutil.rmtreefallback. - Unknown is not clean. When
git statusfails, refuse the entry. - Make
statusandprunefaster. Each entry runs 3 git commands, and the full run takes 20 s forstatusand 68 s forprune.
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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNew
Activity
dan-petty commentedon Oct 27, 2026
Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.
The candidate describes multiple defects in the worktrees prune command's functionality.The fix addresses critical issues that prevent proper worktree management and can cause data loss or incorrect behavior.Fixing these defects improves repository stability and prevents unwanted removal of active worktrees.The fix requires modifying logic in two Python files to correctly identify merged worktrees and handle edge cases.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.chore(main): release 0.7.0 (#2329)