Skip to content

Commit d17a34b

Browse files
jwaldripclaude
andcommitted
feat(lifecycle): auto-cleanup worktrees at completion milestones
Worktrees and branches are now cleaned up automatically when work is persisted: unit merged into intent, unit PR created, intent MR created, or intent completed. No more stale worktrees accumulating between manual /cleanup runs. Guiding principle: .ai-dlc/worktrees/ should only contain worktrees where agents are actively working. If nothing is in-flight, the directory should be empty — worktrees are a live reflection of active work, not a history of past work. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c0349b7 commit d17a34b

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

plugin/skills/advance/SKILL.md

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,13 @@ ${TICKET_LINE}
148148
EOF
149149
)" 2>/dev/null || echo "PR may already exist for $UNIT_BRANCH"
150150
151-
# Clean up unit worktree
152-
WORKTREE_PATH="${REPO_ROOT}/.ai-dlc/worktrees/${INTENT_SLUG}-${UNIT_SLUG}"
153-
[ -d "$WORKTREE_PATH" ] && git worktree remove "$WORKTREE_PATH"
151+
# Clean up local unit worktree after PR is pushed (work is on remote now)
152+
UNIT_WORKTREE="${REPO_ROOT}/.ai-dlc/worktrees/${INTENT_SLUG}-${UNIT_SLUG}"
153+
if [ -d "$UNIT_WORKTREE" ]; then
154+
git worktree remove "$UNIT_WORKTREE" 2>/dev/null || true
155+
echo "Cleaned up unit worktree for ${CURRENT_UNIT}"
156+
fi
157+
# Keep the branch — it backs the open PR
154158
155159
elif [ "$AUTO_MERGE" = "true" ]; then
156160
# Intent/trunk strategy: merge unit branch into intent branch
@@ -165,9 +169,14 @@ elif [ "$AUTO_MERGE" = "true" ]; then
165169
git merge --no-ff "$UNIT_BRANCH" -m "Merge ${CURRENT_UNIT} into intent branch"
166170
fi
167171

168-
# Clean up unit worktree
169-
WORKTREE_PATH="${REPO_ROOT}/.ai-dlc/worktrees/${INTENT_SLUG}-${UNIT_SLUG}"
170-
[ -d "$WORKTREE_PATH" ] && git worktree remove "$WORKTREE_PATH"
172+
# Clean up unit worktree and branch after merge into intent
173+
UNIT_WORKTREE="${REPO_ROOT}/.ai-dlc/worktrees/${INTENT_SLUG}-${UNIT_SLUG}"
174+
if [ -d "$UNIT_WORKTREE" ]; then
175+
git worktree remove "$UNIT_WORKTREE" 2>/dev/null || true
176+
echo "Cleaned up unit worktree for ${CURRENT_UNIT}"
177+
fi
178+
git branch -d "ai-dlc/${INTENT_SLUG}/${UNIT_SLUG}" 2>/dev/null || true
179+
echo "Cleaned up unit branch for ${CURRENT_UNIT}"
171180
fi
172181
```
173182
@@ -453,7 +462,16 @@ for unit_file in "$INTENT_DIR"/unit-*.md; do
453462
done
454463
```
455464

456-
**If ALL units use unit strategy** (`ALL_UNIT_STRATEGY=true`): Skip the delivery prompt. Each unit already has its own PR. Output:
465+
**If ALL units use unit strategy** (`ALL_UNIT_STRATEGY=true`): Skip the delivery prompt. Each unit already has its own PR. Clean up the intent worktree and output:
466+
467+
```bash
468+
# Clean up intent worktree — all unit PRs are on the remote
469+
INTENT_WORKTREE="${REPO_ROOT}/.ai-dlc/worktrees/${INTENT_SLUG}"
470+
if [ -d "$INTENT_WORKTREE" ]; then
471+
git worktree remove "$INTENT_WORKTREE" 2>/dev/null || true
472+
echo "Cleaned up intent worktree for ${INTENT_SLUG}"
473+
fi
474+
```
457475

458476
```
459477
All unit PRs have been created during construction. Review and merge them individually.
@@ -529,7 +547,19 @@ EOF
529547
)"
530548
```
531549

532-
4. Output the PR URL.
550+
4. Clean up intent worktree after PR is pushed (work is on remote now):
551+
552+
```bash
553+
# Clean up intent worktree after PR is pushed
554+
INTENT_WORKTREE="${REPO_ROOT}/.ai-dlc/worktrees/${INTENT_SLUG}"
555+
if [ -d "$INTENT_WORKTREE" ]; then
556+
git worktree remove "$INTENT_WORKTREE" 2>/dev/null || true
557+
echo "Cleaned up intent worktree for ${INTENT_SLUG}"
558+
fi
559+
# Keep the branch — it backs the open PR
560+
```
561+
562+
5. Output the PR URL.
533563

534564
### If manual:
535565

@@ -546,3 +576,15 @@ To create PR manually:
546576
To clean up:
547577
/reset
548578
```
579+
580+
Clean up intent worktree since all work is committed and pushed:
581+
582+
```bash
583+
# Clean up intent worktree — work is committed on the branch
584+
INTENT_WORKTREE="${REPO_ROOT}/.ai-dlc/worktrees/${INTENT_SLUG}"
585+
if [ -d "$INTENT_WORKTREE" ]; then
586+
git worktree remove "$INTENT_WORKTREE" 2>/dev/null || true
587+
echo "Cleaned up intent worktree for ${INTENT_SLUG}"
588+
fi
589+
# Keep the branch — user may create a PR from it
590+
```

plugin/skills/elaborate/SKILL.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,20 @@ EOF
14511451
)"
14521452
```
14531453

1454-
3. Tell the user:
1454+
3. Clean up elaboration worktree after PR is pushed (work is on remote now):
1455+
1456+
```bash
1457+
# Clean up elaboration worktree — spec is on the remote branch now
1458+
REPO_ROOT=$(git worktree list --porcelain | head -1 | sed 's/^worktree //')
1459+
INTENT_WORKTREE="${REPO_ROOT}/.ai-dlc/worktrees/${INTENT_SLUG}"
1460+
if [ -d "$INTENT_WORKTREE" ]; then
1461+
git worktree remove "$INTENT_WORKTREE" 2>/dev/null || true
1462+
echo "Cleaned up elaboration worktree for ${INTENT_SLUG}"
1463+
fi
1464+
# Keep the branch — it backs the open spec review PR
1465+
```
1466+
1467+
4. Tell the user:
14551468

14561469
```
14571470
Spec PR created: {PR_URL}

0 commit comments

Comments
 (0)