You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enhances /cleanup to identify active worktrees whose branch has
already been merged into the default branch, and offers to remove
them. Previously only orphaned worktrees (stale git entries) were
detected — merged-but-lingering worktrees were invisible to cleanup.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
**User-facing command** - Run this to clean up stale worktrees left behind by interrupted sessions.
19
19
20
-
Scans `.ai-dlc/worktrees/` for worktree directories and removes any that are orphaned (the backing git worktree entry is stale or the directory is left over from a crashed session).
20
+
Scans `.ai-dlc/worktrees/` for worktree directories and removes any that are orphaned (the backing git worktree entry is stale or the directory is left over from a crashed session) or merged (the worktree's branch has already been merged into the default branch).
21
21
22
22
This does not:
23
23
- Clear AI-DLC state (use `/reset` for that)
24
-
- Delete branches or commits
25
-
- Affect active worktrees with running sessions
24
+
- Delete unmerged branches or commits
25
+
- Affect active worktrees whose branches have not been merged
26
26
27
27
## Implementation
28
28
@@ -57,20 +57,48 @@ if [ -z "$DIRS" ]; then
57
57
fi
58
58
```
59
59
60
-
### Step 2: Identify Orphaned Worktrees
60
+
### Step 2: Identify Orphaned, Merged, and Active Worktrees
61
61
62
62
```bash
63
63
# Get list of valid worktree paths from git
64
64
VALID_WORKTREES=$(git worktree list --porcelain | grep '^worktree '| sed 's/^worktree //')
65
65
66
+
# Determine the default branch
67
+
DEFAULT_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's|refs/remotes/origin/||')
68
+
if [ -z"$DEFAULT_BRANCH" ];then
69
+
# Fallback: try common names
70
+
forcandidatein main master;do
71
+
if git rev-parse --verify "refs/heads/$candidate"&>/dev/null;then
72
+
DEFAULT_BRANCH="$candidate"
73
+
break
74
+
fi
75
+
done
76
+
fi
77
+
78
+
# Get the list of branches already merged into the default branch
79
+
MERGED_BRANCHES=$(git branch --merged "$DEFAULT_BRANCH"2>/dev/null | sed 's/^[* ]*//')
80
+
66
81
ORPHANED=()
82
+
MERGED=()
83
+
MERGED_BRANCHES_MAP=() # parallel array: branch name for each merged entry
67
84
ACTIVE=()
68
85
69
86
fordirin$DIRS;do
87
+
slug=$(basename "$dir")
70
88
ifecho"$VALID_WORKTREES"| grep -qF "$dir";then
71
-
ACTIVE+=("$(basename "$dir")")
89
+
# Active worktree — check if its branch is merged
90
+
BRANCH=$(git worktree list --porcelain | awk -v path="$dir"'
if [ -n"$BRANCH" ] &&echo"$MERGED_BRANCHES"| grep -qxF "$BRANCH";then
95
+
MERGED+=("$slug")
96
+
MERGED_BRANCHES_MAP+=("$BRANCH")
97
+
else
98
+
ACTIVE+=("$slug")
99
+
fi
72
100
else
73
-
ORPHANED+=("$(basename "$dir")")
101
+
ORPHANED+=("$slug")
74
102
fi
75
103
done
76
104
```
@@ -82,21 +110,26 @@ Show the user what was found:
82
110
```
83
111
## AI-DLC Worktree Cleanup
84
112
85
-
**Active worktrees:** {count}
86
-
{list of active worktree names, if any}
87
-
88
113
**Orphaned worktrees:** {count}
89
114
{list of orphaned worktree names, if any}
115
+
116
+
**Merged worktrees (safe to remove):** {count}
117
+
{list of merged worktree names with branch names, e.g. "slug (ai-dlc/slug/main)"}
118
+
119
+
**Active worktrees:** {count}
120
+
{list of active worktree names, if any}
90
121
```
91
122
92
123
If there are orphaned worktrees, ask the user to confirm removal using `AskUserQuestion`.
93
124
94
-
If there are no orphaned entries but there are active worktrees, ask whether to force-remove all worktrees (with a warning that this will interrupt any running sessions).
125
+
If there are merged worktrees, ask the user (separately from orphaned confirmation) whether to remove them using `AskUserQuestion`. Explain that their branches are already merged into the default branch so removal is safe.
126
+
127
+
If there are no orphaned or merged entries but there are active worktrees, ask whether to force-remove all worktrees (with a warning that this will interrupt any running sessions).
95
128
96
129
If there is nothing to clean up, output:
97
130
98
131
```
99
-
No orphaned worktrees found. Everything is clean.
132
+
No orphaned or merged worktrees found. Everything is clean.
100
133
```
101
134
102
135
### Step 4: Remove
@@ -109,6 +142,23 @@ for name in "${ORPHANED[@]}"; do
109
142
rm -rf "${REPO_ROOT}/.ai-dlc/worktrees/${name}"
110
143
done
111
144
145
+
# Remove merged worktrees (if user confirmed)
146
+
foriin"${!MERGED[@]}";do
147
+
name="${MERGED[$i]}"
148
+
branch="${MERGED_BRANCHES_MAP[$i]}"
149
+
# Remove the worktree (no --force needed since branch is merged)
0 commit comments