-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathagent-worktree-prune.sh
More file actions
698 lines (619 loc) · 21.1 KB
/
agent-worktree-prune.sh
File metadata and controls
698 lines (619 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#!/usr/bin/env bash
set -euo pipefail
BASE_BRANCH="${GUARDEX_BASE_BRANCH:-}"
BASE_BRANCH_EXPLICIT=0
DRY_RUN=0
FORCE_DIRTY=0
DELETE_BRANCHES=0
DELETE_REMOTE_BRANCHES=0
ONLY_DIRTY_WORKTREES=0
INCLUDE_PR_MERGED=0
TARGET_BRANCH=""
IDLE_MINUTES=0
NOW_EPOCH_RAW="${GUARDEX_PRUNE_NOW_EPOCH:-}"
IDLE_SECONDS=0
NOW_EPOCH=0
GH_BIN="${GUARDEX_GH_BIN:-gh}"
PR_MERGED_LOOKUP_DISABLED=0
PR_MERGED_LOOKUP_LOADED=0
declare -A MERGED_PR_BRANCHES=()
WORKTREE_ROOT_RELS=(
".omx/agent-worktrees"
".omx/.tmp-worktrees"
".omc/agent-worktrees"
".omc/.tmp-worktrees"
)
if [[ -n "$BASE_BRANCH" ]]; then
BASE_BRANCH_EXPLICIT=1
fi
while [[ $# -gt 0 ]]; do
case "$1" in
--base)
BASE_BRANCH="${2:-}"
BASE_BRANCH_EXPLICIT=1
shift 2
;;
--dry-run)
DRY_RUN=1
shift
;;
--force-dirty)
FORCE_DIRTY=1
shift
;;
--delete-branches)
DELETE_BRANCHES=1
shift
;;
--delete-remote-branches)
DELETE_REMOTE_BRANCHES=1
shift
;;
--only-dirty-worktrees)
ONLY_DIRTY_WORKTREES=1
shift
;;
--include-pr-merged)
INCLUDE_PR_MERGED=1
shift
;;
--branch)
TARGET_BRANCH="${2:-}"
shift 2
;;
--idle-minutes)
IDLE_MINUTES="${2:-}"
shift 2
;;
*)
echo "[agent-worktree-prune] Unknown argument: $1" >&2
echo "Usage: $0 [--base <branch>] [--dry-run] [--force-dirty] [--delete-branches] [--delete-remote-branches] [--only-dirty-worktrees] [--include-pr-merged] [--branch <agent/...>] [--idle-minutes <minutes>]" >&2
exit 1
;;
esac
done
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "[agent-worktree-prune] Not inside a git repository." >&2
exit 1
fi
repo_root="$(git rev-parse --show-toplevel)"
current_pwd="${GUARDEX_PRUNE_ACTIVE_CWD:-$(pwd -P)}"
if [[ -d "$current_pwd" ]]; then
current_pwd="$(cd "$current_pwd" && pwd -P)"
else
current_pwd=""
fi
repo_common_dir="$(
git -C "$repo_root" rev-parse --git-common-dir \
| awk -v root="$repo_root" '{ if ($0 ~ /^\//) { print $0 } else { print root "/" $0 } }'
)"
repo_common_dir="$(cd "$repo_common_dir" && pwd -P)"
resolve_worktree_root_rel_for_entry() {
local entry="$1"
case "$entry" in
*/.omc/.tmp-worktrees/*)
printf '%s' '.omc/.tmp-worktrees'
;;
*/.omc/agent-worktrees/*)
printf '%s' '.omc/agent-worktrees'
;;
*/.omx/.tmp-worktrees/*)
printf '%s' '.omx/.tmp-worktrees'
;;
*)
printf '%s' '.omx/agent-worktrees'
;;
esac
}
is_managed_worktree_path() {
local entry="$1"
local rel root
for rel in "${WORKTREE_ROOT_RELS[@]}"; do
root="${repo_root}/${rel}"
if [[ "$entry" == "${root}"/* ]]; then
return 0
fi
done
return 1
}
is_temporary_worktree_path() {
local entry="$1"
local name
name="$(basename "$entry")"
[[ "$name" == __agent_integrate-* || "$name" == __source-probe-* ]]
}
resolve_base_branch() {
local configured=""
local current=""
configured="$(git -C "$repo_root" config --get multiagent.baseBranch || true)"
if [[ -n "$configured" ]] && git -C "$repo_root" show-ref --verify --quiet "refs/heads/${configured}"; then
printf '%s' "$configured"
return 0
fi
current="$(git -C "$repo_root" rev-parse --abbrev-ref HEAD 2>/dev/null || true)"
if [[ -n "$current" && "$current" != "HEAD" ]] && git -C "$repo_root" show-ref --verify --quiet "refs/heads/${current}"; then
printf '%s' "$current"
return 0
fi
for fallback in main dev; do
if git -C "$repo_root" show-ref --verify --quiet "refs/heads/${fallback}"; then
printf '%s' "$fallback"
return 0
fi
done
printf '%s' ""
}
load_merged_pr_branches() {
if [[ "$INCLUDE_PR_MERGED" -ne 1 ]]; then
return 1
fi
if [[ "$PR_MERGED_LOOKUP_DISABLED" -eq 1 ]]; then
return 1
fi
if [[ "$PR_MERGED_LOOKUP_LOADED" -eq 1 ]]; then
return 0
fi
if ! command -v "$GH_BIN" >/dev/null 2>&1; then
PR_MERGED_LOOKUP_DISABLED=1
return 1
fi
local merged_branches=""
merged_branches="$(
"$GH_BIN" pr list --state merged --base "$BASE_BRANCH" --limit 200 --json headRefName --jq '.[].headRefName' 2>/dev/null || true
)"
if [[ -n "$merged_branches" ]]; then
while IFS= read -r merged_branch; do
[[ -z "$merged_branch" ]] && continue
MERGED_PR_BRANCHES["$merged_branch"]=1
done <<< "$merged_branches"
fi
PR_MERGED_LOOKUP_LOADED=1
return 0
}
branch_has_merged_pr() {
local branch="$1"
if [[ "$INCLUDE_PR_MERGED" -ne 1 ]]; then
return 1
fi
load_merged_pr_branches || return 1
[[ -n "${MERGED_PR_BRANCHES[$branch]:-}" ]]
}
if [[ "$BASE_BRANCH_EXPLICIT" -eq 1 && -z "$BASE_BRANCH" ]]; then
echo "[agent-worktree-prune] --base requires a non-empty branch name." >&2
exit 1
fi
if [[ -n "$TARGET_BRANCH" && "$TARGET_BRANCH" != agent/* ]]; then
echo "[agent-worktree-prune] --branch must reference an agent/* branch: ${TARGET_BRANCH}" >&2
exit 1
fi
if [[ ! "$IDLE_MINUTES" =~ ^[0-9]+$ ]]; then
echo "[agent-worktree-prune] --idle-minutes must be an integer >= 0." >&2
exit 1
fi
if [[ -n "$NOW_EPOCH_RAW" && ! "$NOW_EPOCH_RAW" =~ ^[0-9]+$ ]]; then
echo "[agent-worktree-prune] GUARDEX_PRUNE_NOW_EPOCH must be a unix timestamp integer." >&2
exit 1
fi
if [[ "$BASE_BRANCH_EXPLICIT" -eq 0 ]]; then
BASE_BRANCH="$(resolve_base_branch)"
fi
if [[ -z "$BASE_BRANCH" ]]; then
echo "[agent-worktree-prune] Unable to infer base branch. Pass --base <branch>." >&2
exit 1
fi
if ! git -C "$repo_root" show-ref --verify --quiet "refs/heads/${BASE_BRANCH}"; then
echo "[agent-worktree-prune] Base branch not found: ${BASE_BRANCH}" >&2
exit 1
fi
IDLE_SECONDS=$((IDLE_MINUTES * 60))
if [[ -n "$NOW_EPOCH_RAW" ]]; then
NOW_EPOCH="$NOW_EPOCH_RAW"
else
NOW_EPOCH="$(date +%s)"
fi
run_cmd() {
if [[ "$DRY_RUN" -eq 1 ]]; then
echo "[agent-worktree-prune] [dry-run] $*"
return 0
fi
"$@"
}
branch_has_worktree() {
local branch="$1"
git -C "$repo_root" worktree list --porcelain | grep -q "^branch refs/heads/${branch}$"
}
# Globs treated as agent state, not real work. Worktrees whose only "dirty"
# content matches these are considered clean for prune purposes. Mirrors the
# auto-transfer + auto-resolve allowlist from PRs #546/#547 (state-file globs
# never carry authoritative content out of an agent branch).
WORKTREE_STATE_EXCLUDE_GLOBS_DEFAULT='.omc/**:.omx/state/**:.dev-ports.json:apps/logs/**:.agents/settings.local.json:.codex/state/**:.claude/state/**'
WORKTREE_STATE_EXCLUDE_GLOBS_RAW="${GUARDEX_PRUNE_STATE_EXCLUDE_GLOBS-$WORKTREE_STATE_EXCLUDE_GLOBS_DEFAULT}"
build_state_exclude_pathspec_args() {
# Emit one ':(exclude,glob)<pat>' arg per non-empty pattern.
if [[ -z "$WORKTREE_STATE_EXCLUDE_GLOBS_RAW" ]]; then
return 0
fi
local -a globs=()
IFS=':' read -ra globs <<< "$WORKTREE_STATE_EXCLUDE_GLOBS_RAW"
local pattern
for pattern in "${globs[@]}"; do
[[ -z "$pattern" ]] && continue
printf '%s\0' ":(exclude,glob)${pattern}"
done
}
# Capture the state-exclude pathspecs once; reused by is_clean_worktree and
# the dirt-summary logger below.
STATE_EXCLUDE_PATHSPEC_ARGS=()
while IFS= read -r -d '' arg; do
STATE_EXCLUDE_PATHSPEC_ARGS+=("$arg")
done < <(build_state_exclude_pathspec_args)
is_clean_worktree() {
local wt="$1"
git -C "$wt" diff --quiet -- . "${STATE_EXCLUDE_PATHSPEC_ARGS[@]}" \
&& git -C "$wt" diff --cached --quiet -- . "${STATE_EXCLUDE_PATHSPEC_ARGS[@]}" \
&& [[ -z "$(git -C "$wt" ls-files --others --exclude-standard -- . "${STATE_EXCLUDE_PATHSPEC_ARGS[@]}")" ]]
}
# Returns a short, human-readable summary of why a worktree is considered dirty:
# up to 3 modified-tracked paths + up to 3 untracked paths + a "(+N more)" tail.
# Used to surface actionable context next to "Skipping dirty worktree" log lines
# (previously gave no clue what was actually dirty).
summarize_worktree_dirt() {
local wt="$1"
local modified_paths untracked_paths
modified_paths="$(git -C "$wt" diff --name-only --diff-filter=AMDR -- . "${STATE_EXCLUDE_PATHSPEC_ARGS[@]}" 2>/dev/null | head -3)"
local modified_count
modified_count="$(git -C "$wt" diff --name-only --diff-filter=AMDR -- . "${STATE_EXCLUDE_PATHSPEC_ARGS[@]}" 2>/dev/null | wc -l | tr -d ' ')"
untracked_paths="$(git -C "$wt" ls-files --others --exclude-standard -- . "${STATE_EXCLUDE_PATHSPEC_ARGS[@]}" 2>/dev/null | head -3)"
local untracked_count
untracked_count="$(git -C "$wt" ls-files --others --exclude-standard -- . "${STATE_EXCLUDE_PATHSPEC_ARGS[@]}" 2>/dev/null | wc -l | tr -d ' ')"
if [[ -n "$modified_paths" ]]; then
while IFS= read -r p; do
[[ -n "$p" ]] && echo " modified: ${p}"
done <<< "$modified_paths"
if [[ "$modified_count" -gt 3 ]]; then
echo " modified: (+$((modified_count - 3)) more)"
fi
fi
if [[ -n "$untracked_paths" ]]; then
while IFS= read -r p; do
[[ -n "$p" ]] && echo " untracked: ${p}"
done <<< "$untracked_paths"
if [[ "$untracked_count" -gt 3 ]]; then
echo " untracked: (+$((untracked_count - 3)) more)"
fi
fi
}
resolve_worktree_common_dir() {
local wt="$1"
local common_dir=""
common_dir="$(git -C "$wt" rev-parse --git-common-dir 2>/dev/null || true)"
if [[ -z "$common_dir" ]]; then
return 1
fi
if [[ "$common_dir" == /* ]]; then
common_dir="$(cd "$common_dir" 2>/dev/null && pwd -P || true)"
else
common_dir="$(cd "$wt/$common_dir" 2>/dev/null && pwd -P || true)"
fi
if [[ -z "$common_dir" ]]; then
return 1
fi
printf '%s' "$common_dir"
}
select_unique_worktree_path() {
local root="$1"
local name="$2"
local candidate="${root}/${name}"
local suffix=2
while [[ -e "$candidate" ]]; do
candidate="${root}/${name}-${suffix}"
suffix=$((suffix + 1))
done
printf '%s' "$candidate"
}
read_branch_activity_epoch() {
local branch="$1"
local wt="${2:-}"
local activity_epoch=""
activity_epoch="$(
git -C "$repo_root" reflog show --format='%ct' -n 1 "refs/heads/${branch}" 2>/dev/null \
| head -n 1 \
| tr -d '[:space:]'
)"
if [[ -z "$activity_epoch" ]]; then
activity_epoch="$(
git -C "$repo_root" log -1 --format='%ct' "$branch" 2>/dev/null \
| head -n 1 \
| tr -d '[:space:]'
)"
fi
if [[ -n "$wt" && -d "$wt" ]]; then
local lock_file="${wt}/.omx/state/agent-file-locks.json"
if [[ -f "$lock_file" ]]; then
local lock_mtime=""
lock_mtime="$(stat -c %Y "$lock_file" 2>/dev/null || stat -f %m "$lock_file" 2>/dev/null || true)"
if [[ "$lock_mtime" =~ ^[0-9]+$ ]]; then
if [[ -z "$activity_epoch" || "$lock_mtime" -gt "$activity_epoch" ]]; then
activity_epoch="$lock_mtime"
fi
fi
fi
fi
printf '%s' "$activity_epoch"
}
skipped_recent=0
has_live_process_in_worktree() {
local wt="$1"
local proc_cwd=""
[[ -d /proc ]] || return 1
for proc_cwd in /proc/[0-9]*/cwd; do
[[ -e "$proc_cwd" ]] || continue
local live_cwd=""
live_cwd="$(readlink "$proc_cwd" 2>/dev/null || true)"
[[ -n "$live_cwd" ]] || continue
live_cwd="${live_cwd% (deleted)}"
if [[ "$live_cwd" == "$wt" || "$live_cwd" == "${wt}"/* ]]; then
return 0
fi
done
return 1
}
branch_idle_gate() {
local branch="$1"
local wt="$2"
local reason="$3"
if [[ "$IDLE_SECONDS" -le 0 ]]; then
return 0
fi
if [[ -z "$branch" ]]; then
return 0
fi
local last_activity_epoch=""
last_activity_epoch="$(read_branch_activity_epoch "$branch" "$wt")"
if [[ ! "$last_activity_epoch" =~ ^[0-9]+$ ]]; then
return 0
fi
local idle_age=$((NOW_EPOCH - last_activity_epoch))
if [[ "$idle_age" -lt 0 ]]; then
idle_age=0
fi
if [[ "$idle_age" -lt "$IDLE_SECONDS" ]]; then
skipped_recent=$((skipped_recent + 1))
echo "[agent-worktree-prune] Skipping recent branch (${reason}): ${branch} (idle=${idle_age}s < ${IDLE_SECONDS}s)"
return 1
fi
return 0
}
relocated_foreign=0
skipped_foreign=0
relocate_foreign_worktree_entries() {
local rel="" worktree_root="" entry=""
for rel in "${WORKTREE_ROOT_RELS[@]}"; do
worktree_root="${repo_root}/${rel}"
[[ -d "$worktree_root" ]] || continue
for entry in "${worktree_root}"/*; do
[[ -d "$entry" ]] || continue
if ! git -C "$entry" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
continue
fi
local entry_common_dir=""
entry_common_dir="$(resolve_worktree_common_dir "$entry" || true)"
[[ -n "$entry_common_dir" ]] || continue
if [[ "$entry_common_dir" == "$repo_common_dir" ]]; then
continue
fi
if [[ "$(basename "$entry_common_dir")" != ".git" ]]; then
skipped_foreign=$((skipped_foreign + 1))
echo "[agent-worktree-prune] Skipping foreign worktree with unsupported git common dir: ${entry}"
continue
fi
local owner_repo_root
owner_repo_root="$(dirname "$entry_common_dir")"
local owner_worktree_root_rel owner_worktree_root
owner_worktree_root_rel="$(resolve_worktree_root_rel_for_entry "$entry")"
owner_worktree_root="${owner_repo_root}/${owner_worktree_root_rel}"
local target_path
target_path="$(select_unique_worktree_path "$owner_worktree_root" "$(basename "$entry")")"
if [[ "$entry" == "$current_pwd" || "$current_pwd" == "${entry}"/* ]]; then
skipped_foreign=$((skipped_foreign + 1))
echo "[agent-worktree-prune] Skipping active foreign worktree: ${entry}"
continue
fi
echo "[agent-worktree-prune] Relocating foreign worktree to owning repo: ${entry} -> ${target_path}"
if [[ "$DRY_RUN" -eq 1 ]]; then
relocated_foreign=$((relocated_foreign + 1))
continue
fi
mkdir -p "$owner_worktree_root"
if git -C "$owner_repo_root" worktree move "$entry" "$target_path" >/dev/null 2>&1; then
relocated_foreign=$((relocated_foreign + 1))
else
skipped_foreign=$((skipped_foreign + 1))
echo "[agent-worktree-prune] Failed to relocate foreign worktree: ${entry}" >&2
fi
done
done
}
removed_worktrees=0
removed_branches=0
skipped_active=0
skipped_dirty=0
relocate_foreign_worktree_entries
process_entry() {
local wt="$1"
local branch_ref="$2"
[[ -z "$wt" ]] && return
if ! is_managed_worktree_path "$wt"; then
return
fi
local branch=""
if [[ -n "$branch_ref" ]]; then
branch="${branch_ref#refs/heads/}"
fi
if [[ -n "$TARGET_BRANCH" && "$branch" != "$TARGET_BRANCH" ]]; then
return
fi
if [[ -n "$current_pwd" && ( "$wt" == "$current_pwd" || "$current_pwd" == "${wt}"/* ) ]]; then
skipped_active=$((skipped_active + 1))
echo "[agent-worktree-prune] Skipping active cwd worktree: ${wt}"
return
fi
if has_live_process_in_worktree "$wt"; then
skipped_active=$((skipped_active + 1))
echo "[agent-worktree-prune] Skipping live process worktree: ${wt}"
return
fi
local remove_reason=""
local branch_delete_mode="safe"
if is_temporary_worktree_path "$wt"; then
remove_reason="temporary-worktree"
elif [[ -z "$branch_ref" ]]; then
remove_reason="detached-worktree"
elif ! git -C "$repo_root" show-ref --verify --quiet "refs/heads/${branch}"; then
remove_reason="missing-branch"
elif [[ "$branch" == agent/* ]]; then
if git -C "$repo_root" merge-base --is-ancestor "$branch" "$BASE_BRANCH"; then
if [[ "$DELETE_BRANCHES" -eq 1 ]]; then
remove_reason="merged-agent-branch"
fi
elif [[ "$DELETE_BRANCHES" -eq 1 ]] && branch_has_merged_pr "$branch"; then
remove_reason="merged-agent-pr"
branch_delete_mode="force"
elif [[ "$ONLY_DIRTY_WORKTREES" -eq 1 ]] && is_clean_worktree "$wt"; then
remove_reason="clean-agent-worktree"
fi
elif [[ "$branch" == __agent_integrate_* || "$branch" == __source-probe-* ]]; then
remove_reason="temporary-worktree"
fi
if [[ -z "$remove_reason" ]]; then
return
fi
if ! branch_idle_gate "$branch" "$wt" "$remove_reason"; then
return
fi
if [[ "$remove_reason" == "temporary-worktree" ]]; then
git -C "$wt" rebase --abort >/dev/null 2>&1 || true
git -C "$wt" merge --abort >/dev/null 2>&1 || true
fi
if [[ "$FORCE_DIRTY" -ne 1 ]] && ! is_clean_worktree "$wt"; then
skipped_dirty=$((skipped_dirty + 1))
echo "[agent-worktree-prune] Skipping dirty worktree (${remove_reason}): ${wt}"
summarize_worktree_dirt "$wt"
return
fi
echo "[agent-worktree-prune] Removing worktree (${remove_reason}): ${wt}"
run_cmd git -C "$repo_root" worktree remove "$wt" --force
removed_worktrees=$((removed_worktrees + 1))
if [[ -z "$branch" ]]; then
return
fi
if git -C "$repo_root" show-ref --verify --quiet "refs/heads/${branch}" && ! branch_has_worktree "$branch"; then
if [[ "$branch" == agent/* && "$DELETE_BRANCHES" -eq 1 ]]; then
local delete_flag="-d"
local deleted_label="merged"
if [[ "$branch_delete_mode" == "force" ]]; then
delete_flag="-D"
deleted_label="merged PR"
fi
if run_cmd git -C "$repo_root" branch "$delete_flag" "$branch" >/dev/null 2>&1; then
removed_branches=$((removed_branches + 1))
echo "[agent-worktree-prune] Deleted ${deleted_label} branch: ${branch}"
if [[ "$DELETE_REMOTE_BRANCHES" -eq 1 ]]; then
if git -C "$repo_root" ls-remote --exit-code --heads origin "$branch" >/dev/null 2>&1; then
run_cmd git -C "$repo_root" push origin --delete "$branch" >/dev/null 2>&1 || true
echo "[agent-worktree-prune] Deleted ${deleted_label} remote branch: ${branch}"
fi
fi
fi
elif [[ "$branch" == __agent_integrate_* || "$branch" == __source-probe-* ]]; then
run_cmd git -C "$repo_root" branch -D "$branch" >/dev/null 2>&1 || true
removed_branches=$((removed_branches + 1))
echo "[agent-worktree-prune] Deleted temporary branch: ${branch}"
fi
fi
}
current_wt=""
current_branch_ref=""
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ -z "$line" ]]; then
process_entry "$current_wt" "$current_branch_ref"
current_wt=""
current_branch_ref=""
continue
fi
case "$line" in
worktree\ *)
current_wt="${line#worktree }"
;;
branch\ *)
current_branch_ref="${line#branch }"
;;
esac
done < <(git -C "$repo_root" worktree list --porcelain)
process_entry "$current_wt" "$current_branch_ref"
if [[ "$DELETE_BRANCHES" -eq 1 ]]; then
while IFS= read -r branch; do
[[ -z "$branch" ]] && continue
if [[ -n "$TARGET_BRANCH" && "$branch" != "$TARGET_BRANCH" ]]; then
continue
fi
if branch_has_worktree "$branch"; then
continue
fi
if [[ "$branch" == __agent_integrate_* || "$branch" == __source-probe-* ]]; then
if ! branch_idle_gate "$branch" "" "temporary-worktree"; then
continue
fi
if run_cmd git -C "$repo_root" branch -D "$branch" >/dev/null 2>&1; then
removed_branches=$((removed_branches + 1))
echo "[agent-worktree-prune] Deleted stale temporary branch: ${branch}"
fi
continue
fi
if [[ "$branch" != agent/* ]]; then
continue
fi
if ! branch_idle_gate "$branch" "" "stale-merged-branch"; then
continue
fi
merged_by_ancestor=0
merged_by_pr=0
if git -C "$repo_root" merge-base --is-ancestor "$branch" "$BASE_BRANCH"; then
merged_by_ancestor=1
elif branch_has_merged_pr "$branch"; then
merged_by_pr=1
fi
if [[ "$merged_by_ancestor" -eq 1 || "$merged_by_pr" -eq 1 ]]; then
delete_flag="-d"
deleted_label="merged"
if [[ "$merged_by_pr" -eq 1 && "$merged_by_ancestor" -eq 0 ]]; then
delete_flag="-D"
deleted_label="merged PR"
fi
if run_cmd git -C "$repo_root" branch "$delete_flag" "$branch" >/dev/null 2>&1; then
removed_branches=$((removed_branches + 1))
echo "[agent-worktree-prune] Deleted stale ${deleted_label} branch: ${branch}"
if [[ "$DELETE_REMOTE_BRANCHES" -eq 1 ]]; then
if git -C "$repo_root" ls-remote --exit-code --heads origin "$branch" >/dev/null 2>&1; then
run_cmd git -C "$repo_root" push origin --delete "$branch" >/dev/null 2>&1 || true
echo "[agent-worktree-prune] Deleted stale ${deleted_label} remote branch: ${branch}"
fi
fi
fi
fi
done < <(git -C "$repo_root" for-each-ref --format='%(refname:short)' refs/heads)
fi
run_cmd git -C "$repo_root" worktree prune
echo "[agent-worktree-prune] Summary: base=${BASE_BRANCH}, idle_minutes=${IDLE_MINUTES}, removed_worktrees=${removed_worktrees}, removed_branches=${removed_branches}, skipped_active=${skipped_active}, skipped_dirty=${skipped_dirty}, skipped_recent=${skipped_recent}"
if [[ "$relocated_foreign" -gt 0 || "$skipped_foreign" -gt 0 ]]; then
echo "[agent-worktree-prune] Foreign routing: relocated=${relocated_foreign}, skipped=${skipped_foreign}"
fi
if [[ "$skipped_active" -gt 0 ]]; then
echo "[agent-worktree-prune] Tip: leave active agent worktree directories, then run this command again for full cleanup." >&2
fi
if [[ "$skipped_dirty" -gt 0 ]]; then
echo "[agent-worktree-prune] Tip: dirty worktrees were preserved. Clean/finish them first, or pass --force-dirty to remove anyway." >&2
fi
if [[ "$IDLE_SECONDS" -gt 0 && "$skipped_recent" -gt 0 ]]; then
echo "[agent-worktree-prune] Tip: recent branches were preserved by --idle-minutes=${IDLE_MINUTES}. Re-run later or lower the threshold." >&2
fi