@jeiemgi/cckit 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +15 -4
  3. package/README.md +11 -3
  4. package/bin/cckit +86 -7
  5. package/cckit.config.json +1 -1
  6. package/kit.config.schema.json +43 -2
  7. package/package.json +1 -1
  8. package/scripts/autopilot-test.sh +68 -0
  9. package/scripts/autopilot.sh +16 -10
  10. package/scripts/check.sh +14 -1
  11. package/scripts/copilot-loop-test.sh +3 -0
  12. package/scripts/guard-raw-git-ops-test.sh +65 -0
  13. package/scripts/init.sh +68 -8
  14. package/scripts/lib/agent-profile-test.sh +151 -0
  15. package/scripts/lib/agent-profile.sh +195 -0
  16. package/scripts/lib/agent-resolve-test.sh +156 -0
  17. package/scripts/lib/agent-resolve.sh +177 -0
  18. package/scripts/lib/branch-owner-test.sh +99 -0
  19. package/scripts/lib/branch-owner.sh +194 -0
  20. package/scripts/lib/captain-test.sh +86 -0
  21. package/scripts/lib/captain.sh +142 -7
  22. package/scripts/lib/contribute-root-test.sh +40 -0
  23. package/scripts/lib/contribute-root.sh +22 -0
  24. package/scripts/lib/effort-ops-test.sh +42 -2
  25. package/scripts/lib/effort-ops.sh +157 -14
  26. package/scripts/lib/effort-parity-test.sh +5 -3
  27. package/scripts/lib/effort.sh +38 -14
  28. package/scripts/lib/hook-refresh-test.sh +65 -0
  29. package/scripts/lib/hook-refresh.sh +62 -0
  30. package/scripts/lib/kit-config.sh +6 -0
  31. package/scripts/lib/kit-gc-test.sh +178 -1
  32. package/scripts/lib/kit-gc.sh +233 -55
  33. package/scripts/lib/kit-task-ops-test.sh +15 -0
  34. package/scripts/lib/kit-task-ops.sh +65 -21
  35. package/scripts/lib/kto-task-pr-merge-test.sh +72 -0
  36. package/scripts/lib/kto-task-pr-test.sh +59 -0
  37. package/scripts/lib/orchestration-runtime-test.sh +142 -0
  38. package/scripts/lib/orchestration-runtime.sh +171 -0
  39. package/scripts/lib/review-stage-test.sh +241 -0
  40. package/scripts/lib/review-stage.sh +259 -0
  41. package/scripts/lib/stage-receipt-test.sh +252 -0
  42. package/scripts/lib/stage-receipt.sh +442 -0
  43. package/scripts/lib/test-env-test.sh +25 -0
  44. package/scripts/lib/test-env.sh +30 -0
  45. package/scripts/lib/worker-brief-test.sh +120 -0
  46. package/scripts/lib/worker-brief.sh +185 -0
  47. package/scripts/lib/worktree-ports-test.sh +146 -0
  48. package/scripts/lib/worktree-start.sh +137 -7
  49. package/scripts/lib/zsh-safety-test.sh +67 -0
  50. package/scripts/orchestrate.sh +124 -22
  51. package/scripts/portable-test.sh +22 -1
  52. package/scripts/review-lint-baseline.txt +28 -0
  53. package/scripts/review-lint-test.sh +271 -0
  54. package/scripts/review-lint.sh +259 -0
  55. package/scripts/review-rules.conf +55 -0
  56. package/scripts/setup-labels.sh +21 -1
  57. package/scripts/skill-frontmatter-baseline.txt +24 -0
  58. package/scripts/skill-frontmatter-lint-test.sh +156 -0
  59. package/scripts/skill-frontmatter-lint.sh +182 -0
  60. package/scripts/task-sync.sh +8 -0
  61. package/scripts/test.sh +10 -1
  62. package/skills/kit-autopilot/SKILL.md +13 -1
  63. package/skills/kit-cleanup/SKILL.md +5 -3
  64. package/skills/kit-contribute/SKILL.md +6 -5
  65. package/skills/kit-effort-close/SKILL.md +8 -3
  66. package/skills/kit-effort-pr/SKILL.md +16 -95
  67. package/skills/kit-gc/SKILL.md +15 -10
  68. package/skills/kit-security-sweep/SKILL.md +4 -6
  69. package/skills/kit-ship/SKILL.md +1 -1
  70. package/skills/kit-task-pr/SKILL.md +2 -0
  71. package/templates/hooks/guard-raw-git-ops.sh.tmpl +180 -0
  72. package/templates/rules/branch-naming.md +6 -0
  73. package/templates/rules/effort-model.md +3 -0
  74. package/templates/rules/naming-and-ids.md +4 -1
  75. package/templates/settings/settings.json.tmpl +4 -2
@@ -107,7 +107,10 @@ t "kit_gc_prune refuses without worktree-issue.sh (rc)" "$rc" "1"
107
107
  # commits and no PR is ORPHAN. Only the first may ever be deleted, and never without --yes.
108
108
  git branch task/40-merged >/dev/null 2>&1
109
109
  git branch task/41-open >/dev/null 2>&1
110
- git branch task/42-orphan >/dev/null 2>&1
110
+ # The orphan carries ONE commit base never received — a branch level with base would be LANDED (#388).
111
+ git checkout -q -b task/42-orphan
112
+ echo "only here" > orphan.txt; git add orphan.txt; git commit -qm "orphan work"
113
+ git checkout -q "$init_branch"
111
114
  cat > "$stub/gh" <<'SH'
112
115
  #!/usr/bin/env bash
113
116
  echo "$*" >> "$GH_CALLS"
@@ -209,5 +212,179 @@ out="$(PATH="$stub:$PATH" KIT_GC_REPO="o/r" kit_gc_prune --yes 2>&1)"
209
212
  t "merged-but-ahead branch survives prune --yes" "$(git branch --list task/43-ahead | wc -l | tr -d ' ')" "1"
210
213
  has "prune says why it skipped it" "$out" "unpushed commits"
211
214
 
215
+ # ── #388: LANDED — no PR of its own, work already on base ──────────────────────────────────────
216
+ # A squash merge rewrites the patch ids, so neither `git cherry` nor `--is-ancestor` can see that
217
+ # a sub branch's work reached base. gc must classify these rows LANDED (not ORPHAN), the plan must
218
+ # list them as their own block, and --yes must delete them — worktree included — while a true
219
+ # orphan (one commit whose content base never received) stays untouched.
220
+ export KIT_BASE_BRANCH="$init_branch"
221
+ # Earlier fixtures that are level with base read as LANDED now (ancestor of base); drop them so the
222
+ # counts below are this section's alone (task/41-open stays PROTECTED, the others ORPHAN).
223
+ git branch -D task/44-behind feat/30-x feat/31-y feat/32-z >/dev/null 2>&1
224
+ git update-ref -d refs/remotes/origin/task/44-behind 2>/dev/null
225
+ # (1) an effort sub, squash-landed: sub/50a commits sub.txt; "the effort PR" lands the same content
226
+ # on base as ONE new commit — check (a), content.
227
+ git checkout -q -b sub/50a-part
228
+ echo "sub work" > sub.txt; git add sub.txt; git commit -qm "sub: part"
229
+ git checkout -q "$init_branch"
230
+ git checkout -q sub/50a-part -- sub.txt; git commit -qm "feat(effort): squash of effort 50"
231
+ # (2) a Claude Code Agent-tool isolation worktree (`worktree-agent-<hash>`), squash-landed too.
232
+ git worktree add -q ../agentwt -b worktree-agent-abc123 >/dev/null 2>&1
233
+ echo "agent work" > ../agentwt/agent.txt; git -C ../agentwt add agent.txt; git -C ../agentwt commit -qm "agent: part"
234
+ git checkout -q worktree-agent-abc123 -- agent.txt; git commit -qm "feat: squash of the agent's work"
235
+ # (3) a second sub whose file base has since CHANGED — content check fails, so only lineage (check
236
+ # b: issue #50 closed + effort/50 PR merged) can land it.
237
+ git checkout -q -b sub/50b-other
238
+ echo "v1" > other.txt; git add other.txt; git commit -qm "sub: other"
239
+ git checkout -q "$init_branch"
240
+ git checkout -q sub/50b-other -- other.txt; git commit -qm "feat(effort): squash of sub 50b"
241
+ echo "v2" > other.txt; git add other.txt; git commit -qm "later change on base"
242
+ # (4) a true orphan: issue #51 is closed but nothing with its content ever reached base.
243
+ git checkout -q -b task/51-orphan
244
+ echo "never landed" > lost.txt; git add lost.txt; git commit -qm "orphan"
245
+ git checkout -q "$init_branch"
246
+ # (5) an agent worktree whose work landed but whose tree is DIRTY — recover-before-prune keeps it.
247
+ git worktree add -q ../agentwt2 -b worktree-agent-def456 >/dev/null 2>&1
248
+ echo "agent two" > ../agentwt2/agent2.txt; git -C ../agentwt2 add agent2.txt; git -C ../agentwt2 commit -qm "agent: two"
249
+ git checkout -q worktree-agent-def456 -- agent2.txt; git commit -qm "feat: squash of agent two"
250
+ echo "uncommitted" > ../agentwt2/scratch.txt
251
+
252
+ cat > "$stub/gh" <<'SH'
253
+ #!/usr/bin/env bash
254
+ case "$1 $2" in
255
+ "pr list") printf 'effort/50-thing\tPR#55 MERGED\n' ;;
256
+ "issue view")
257
+ for a in "$@"; do case "$a" in 41) echo open; exit 0 ;; esac; done
258
+ echo closed ;;
259
+ *) : ;;
260
+ esac
261
+ SH
262
+ chmod +x "$stub/gh"
263
+
264
+ analysis="$(PATH="$stub:$PATH" KIT_GC_REPO="o/r" kit_gc_analyze 2>/dev/null)"
265
+ row() { printf '%s\n' "$analysis" | awk -v b="$1" '$1==b && $2=="->"{sub(/^ [^ ]+ -> /, ""); print; exit}'; }
266
+ has "squash-landed sub is LANDED by content" "$(row sub/50a-part)" "LANDED (content on $init_branch)"
267
+ has "agent worktree branch is LANDED and named" "$(row worktree-agent-abc123)" "LANDED (agent worktree; content on $init_branch)"
268
+ has "sub with later base change is LANDED by lineage" "$(row sub/50b-other)" "LANDED (issue #50 closed, its PR merged)"
269
+ has "true orphan stays ORPHAN" "$(row task/51-orphan)" "ORPHAN"
270
+ has "dirty agent worktree's branch is still LANDED" "$(row worktree-agent-def456)" "LANDED"
271
+ t "has_prunable sees a LANDED row" "$(PATH="$stub:$PATH" KIT_GC_REPO="o/r" kit_gc_has_prunable && echo yes || echo no)" "yes"
272
+
273
+ plan="$(PATH="$stub:$PATH" KIT_GC_REPO="o/r" kit_gc_cleanup 2>&1)"
274
+ has "plan has a LANDED block" "$plan" "LANDED branches"
275
+ t "plan counts the four LANDED rows" \
276
+ "$(printf '%s\n' "$plan" | awk '/^ LANDED branches/{print $3}')" "4"
277
+ has "plan names the sub with its reason" "$plan" "sub/50a-part (content on $init_branch)"
278
+ has "plan names the agent worktree as a worktree" "$plan" "agentwt [worktree-agent-abc123]"
279
+ has "plan flags the dirty agent worktree" "$plan" "agentwt2 [worktree-agent-def456] (DIRTY"
280
+ has "plan keeps the orphan" "$plan" "task/51-orphan"
281
+ # The LANDED block prints BEFORE the ORPHAN block.
282
+ t "LANDED block precedes ORPHAN block" \
283
+ "$(printf '%s\n' "$plan" | awk '/^ LANDED branches/{l=NR} /^ ORPHAN kept/{o=NR} END{print (l<o)?"yes":"no"}')" "yes"
284
+ t "plan-only kept the sub branch" "$(git branch --list sub/50a-part | wc -l | tr -d ' ')" "1"
285
+ [ -d ../agentwt ] && echo "ok: plan-only kept the agent worktree" || { echo "FAIL: plan-only removed the agent worktree"; fail=1; }
286
+
287
+ # kit_gc_prune --only: the scope effort_close uses to collect ONE effort's subs (#388) — the agent
288
+ # branch is also LANDED but outside the glob, so it must survive this pass.
289
+ out="$(PATH="$stub:$PATH" KIT_GC_REPO="o/r" kit_gc_prune --yes --only '^sub/50[a-z]-' 2>&1)"
290
+ t "--only deleted the content-landed sub" "$(git branch --list sub/50a-part | wc -l | tr -d ' ')" "0"
291
+ t "--only deleted the lineage-landed sub" "$(git branch --list sub/50b-other | wc -l | tr -d ' ')" "0"
292
+ t "--only left the agent branch alone" "$(git branch --list worktree-agent-abc123 | wc -l | tr -d ' ')" "1"
293
+ has "prune says why the sub went" "$out" "deleted local branch sub/50a-part (LANDED"
294
+
295
+ applied="$(PATH="$stub:$PATH" KIT_GC_REPO="o/r" kit_gc_cleanup --yes 2>&1)"
296
+ t "--yes deleted the agent branch" "$(git branch --list worktree-agent-abc123 | wc -l | tr -d ' ')" "0"
297
+ [ -d ../agentwt ] && { echo "FAIL: --yes left the agent worktree dir"; fail=1; } || echo "ok: --yes removed the agent worktree"
298
+ t "--yes KEPT the true orphan" "$(git branch --list task/51-orphan | wc -l | tr -d ' ')" "1"
299
+ t "--yes KEPT the dirty agent worktree's branch" "$(git branch --list worktree-agent-def456 | wc -l | tr -d ' ')" "1"
300
+ [ -d ../agentwt2 ] && echo "ok: --yes skipped the dirty agent worktree" || { echo "FAIL: --yes destroyed a dirty worktree"; fail=1; }
301
+ has "cleanup said why it skipped the dirty one" "$applied" "SKIP dirty worktree"
302
+ has "cleanup counts what it landed" "$applied" "landed deleted"
303
+
304
+ # ── lineage when the closing PR is OUTSIDE the 200-PR index window ─────────────────────────────
305
+ # Measured on a consuming repo: the effort PR that closed the issue sat at position 254 of `gh pr list --state all`, so the
306
+ # index never carried it and 21 `worktree-agent-*` branches with `[#2165]` subjects stayed ORPHAN
307
+ # while base had changed their files again (content check fails too). The issue itself knows which
308
+ # PR closed it (`closedByPullRequestsReferences`), exact and not window-bound. Cached per issue: the
309
+ # two branches below both name #60 and must cost ONE `issue view … closedBy` call.
310
+ git checkout -q -b sub/60a-old
311
+ echo "old sub" > old.txt; git add old.txt; git commit -qm "sub: old"
312
+ git checkout -q "$init_branch"
313
+ git checkout -q sub/60a-old -- old.txt; git commit -qm "feat(effort): squash of effort 60"
314
+ echo "changed again" > old.txt; git add old.txt; git commit -qm "later change on base"
315
+ git checkout -q -b worktree-agent-old777
316
+ echo "agent old" > agentold.txt; git add agentold.txt; git commit -qm "feat: agent part [#60]"
317
+ git checkout -q "$init_branch"
318
+ git checkout -q worktree-agent-old777 -- agentold.txt; git commit -qm "squash of the agent's part"
319
+ echo "changed again" > agentold.txt; git add agentold.txt; git commit -qm "later change on base"
320
+ cat > "$stub/gh" <<'SH'
321
+ #!/usr/bin/env bash
322
+ echo "$*" >> "$GH_CALLS"
323
+ case "$1 $2" in
324
+ "pr list") printf 'effort/50-thing\tPR#55 MERGED\n' ;; # nothing for #60: scrolled out
325
+ "issue view")
326
+ case "$*" in
327
+ *closedByPullRequestsReferences*) for a in "$@"; do case "$a" in 60) echo 61; exit 0 ;; esac; done ;;
328
+ *) echo closed ;;
329
+ esac ;;
330
+ "pr view") for a in "$@"; do case "$a" in 61) echo MERGED; exit 0 ;; esac; done; echo CLOSED ;;
331
+ *) : ;;
332
+ esac
333
+ SH
334
+ chmod +x "$stub/gh"
335
+ : > "$GH_CALLS"
336
+ analysis="$(PATH="$stub:$PATH" KIT_GC_REPO="o/r" kit_gc_analyze 2>/dev/null)"
337
+ has "sub landed via the issue's closing PR" "$(row sub/60a-old)" "LANDED (issue #60 closed by merged PR#61)"
338
+ has "agent branch with [#60] subject landed too" "$(row worktree-agent-old777)" "LANDED (agent worktree; issue #60 closed by merged PR#61)"
339
+ t "the closedBy lookup ran ONCE for #60 across both branches" \
340
+ "$(grep -c 'issue view 60 .*closedByPullRequestsReferences' "$GH_CALLS")" "1"
341
+ t "the PR state was confirmed ONCE" "$(grep -c '^pr view 61' "$GH_CALLS")" "1"
342
+ # A fresh run must not trust the previous run's cache (the issue could have been reopened).
343
+ : > "$GH_CALLS"
344
+ PATH="$stub:$PATH" KIT_GC_REPO="o/r" kit_gc_prune >/dev/null 2>&1
345
+ t "a new run looks the issue up again" "$(grep -c 'issue view 60 .*closedByPullRequestsReferences' "$GH_CALLS")" "1"
346
+
347
+ # ── `(#N)` subjects + a sub-issue closed through its parent's PR ──────────────────────────────
348
+ # Measured on a consuming repo: agent branches whose subjects end in `(#N)` — GitHub's own trailing ref,
349
+ # not the kit's `[#N]` — where #2077 is a sub-issue: CLOSED, no closing PR (subs close through
350
+ # `effort close`), parent #2075 closed by merged PR 2130. One climb to the parent lands it.
351
+ # effort/1711: closed NOT_PLANNED, no PR, no parent — must stay ORPHAN.
352
+ git checkout -q -b worktree-agent-sub70
353
+ echo "ontology" > onto.txt; git add onto.txt; git commit -qm "Root vocabulary: classes and predicates (#70)"
354
+ git checkout -q "$init_branch"
355
+ git checkout -q worktree-agent-sub70 -- onto.txt; git commit -qm "squash of the ontology effort"
356
+ echo "changed again" > onto.txt; git add onto.txt; git commit -qm "later change on base"
357
+ git checkout -q -b effort/80-not-planned
358
+ echo "abandoned" > np.txt; git add np.txt; git commit -qm "[Effort 80] 1 · never landed"
359
+ git checkout -q "$init_branch"
360
+ cat > "$stub/gh" <<'SH'
361
+ #!/usr/bin/env bash
362
+ echo "$*" >> "$GH_CALLS"
363
+ case "$1 $2" in
364
+ "pr list") printf 'effort/50-thing\tPR#55 MERGED\n' ;;
365
+ "issue view")
366
+ case "$*" in
367
+ *closedByPullRequestsReferences*) for a in "$@"; do case "$a" in 75) echo 76; exit 0 ;; esac; done ;; # 70, 80: nothing
368
+ *) echo closed ;;
369
+ esac ;;
370
+ "pr view") for a in "$@"; do case "$a" in 76) echo MERGED; exit 0 ;; esac; done; echo CLOSED ;;
371
+ "api repos/o/r/issues/70/parent") echo 75 ;;
372
+ "api repos/o/r/issues/80/parent") echo "gh: Not Found (HTTP 404)" >&2; exit 1 ;;
373
+ *) : ;;
374
+ esac
375
+ SH
376
+ chmod +x "$stub/gh"
377
+ : > "$GH_CALLS"
378
+ analysis="$(PATH="$stub:$PATH" KIT_GC_REPO="o/r" kit_gc_analyze 2>/dev/null)"
379
+ has "(#N) subject lands through the parent's merged PR" "$(row worktree-agent-sub70)" \
380
+ "LANDED (agent worktree; issue #70 closed, parent #75 closed by merged PR#76)"
381
+ has "closed NOT_PLANNED with no PR and no parent stays ORPHAN" "$(row effort/80-not-planned)" "ORPHAN"
382
+ t "the parent was climbed once for #70" "$(grep -c '^api repos/o/r/issues/70/parent' "$GH_CALLS")" "1"
383
+ t "the parent lookup for #80 ran once and was cached as a miss" "$(grep -c '^api repos/o/r/issues/80/parent' "$GH_CALLS")" "1"
384
+ applied="$(PATH="$stub:$PATH" KIT_GC_REPO="o/r" kit_gc_prune --yes --only '^(worktree-agent-sub70|effort/80-)' 2>&1)"
385
+ t "--yes deleted the parent-landed agent branch" "$(git branch --list worktree-agent-sub70 | wc -l | tr -d ' ')" "0"
386
+ t "--yes KEPT the not-planned effort branch" "$(git branch --list effort/80-not-planned | wc -l | tr -d ' ')" "1"
387
+ unset KIT_BASE_BRANCH
388
+
212
389
  [ "$fail" -eq 0 ] && echo "ALL OK (kit-gc)" || echo "kit-gc: FAILURES"
213
390
  exit "$fail"
@@ -12,7 +12,12 @@
12
12
  # to run anywhere, anytime (it writes nothing), so the cockpit can flip its `gc` verb to runnable.
13
13
  #
14
14
  # kit_gc_analyze print the classification table (read-only). rc 0 always.
15
- # kit_gc_has_prunable rc 0 if anything is safe to delete (for a UI badge / nudge).
15
+ # kit_gc_has_prunable rc 0 if anything is SAFE or LANDED to delete (for a UI badge / nudge).
16
+ # kit_gc_prune [--yes] [--only <regex>] delete the SAFE + LANDED rows (dry-run without --yes).
17
+ # kit_gc_cleanup [--yes] the guided sweep: plan every bucket, then act on SAFE + LANDED + ZOMBIE.
18
+ #
19
+ # Verdicts: PROTECTED (issue open) · SAFE (own PR merged) · ACTIVE (own PR open / base) · LANDED (no
20
+ # PR of its own, work already on base — #388: effort subs, agent isolation worktrees) · ORPHAN.
16
21
  #
17
22
  # Requires: git; gh (degrades to "unknown" issue/PR state without it); scripts/lib/worktree-issue.sh.
18
23
  # Portable: bash 3.2+ AND zsh.
@@ -71,13 +76,153 @@ _kit_gc_pr_for() {
71
76
  printf '%s\n' "$1" | awk -F'\t' -v want="$2" '$1==want{print $2; exit}'
72
77
  }
73
78
 
79
+ # _kit_gc_base_ref — the ref the LANDED checks compare against: `origin/<base>` when that
80
+ # remote-tracking ref exists (the merge target), else the local base branch (a repo with no origin,
81
+ # e.g. a test fixture). Empty when neither exists — then nothing can be LANDED.
82
+ _kit_gc_base_ref() {
83
+ local base="${KIT_BASE_BRANCH:-main}"
84
+ if git show-ref --verify --quiet "refs/remotes/origin/$base" 2>/dev/null; then printf 'origin/%s' "$base"
85
+ elif git show-ref --verify --quiet "refs/heads/$base" 2>/dev/null; then printf '%s' "$base"
86
+ fi
87
+ }
88
+
89
+ # _kit_gc_landed_reason <branch> <pr_index> [repo] — echo why <branch>'s work is already on the base
90
+ # branch, or nothing when it is not. The rows #388 is about: a branch with NO PR of its own whose work
91
+ # reached base through someone else's PR — an effort's `sub/<N><letter>` (merged into effort/<N>, then
92
+ # squash-merged), an `effort/<N>` whose remote ref is gone, a Claude Code Agent-tool isolation
93
+ # worktree (`worktree-agent-<hash>`, merged into the task branch that had the PR). `git cherry` and
94
+ # `--is-ancestor` cannot see through a squash (it rewrites the patch ids), so two checks, either of
95
+ # which suffices:
96
+ # (a) content — every file the branch changed since its merge base with base now has the same
97
+ # content on base (`git diff --quiet <base> <branch> -- <those files>`): the branch holds no
98
+ # change base lacks, whatever path it took there. A branch with no change at all beyond its
99
+ # merge base (or an ancestor of base) is landed trivially.
100
+ # (b) lineage — the name (`sub/<N>…`, `effort/<N>…`, any `<kind>/<N>-…`) or a `[#N]` in one of its
101
+ # commit subjects names an issue that is CLOSED and whose PR is MERGED: either a head
102
+ # `<kind>/<N>-…` in the PR index, or — when the PR has scrolled out of that 200-PR window — a
103
+ # PR the issue itself reports as having closed it (_kit_gc_issue_merged_pr, cached per issue).
104
+ # Covers a sub whose files base has since changed further, and an agent branch whose `[#N]`
105
+ # names the effort parent while the PR was the effort's.
106
+ # A branch failing both stays ORPHAN. Never called for a branch with a PR of its own.
107
+ _kit_gc_landed_reason() {
108
+ local b="$1" pr_index="$2" repo="${3:-$KIT_GC_REPO}" base_ref mb files shape="" n state pr
109
+ base_ref="$(_kit_gc_base_ref)"; [ -n "$base_ref" ] || return 0
110
+ case "$b" in worktree-agent-*) shape="agent worktree; " ;; esac
111
+ # (a) content
112
+ if git merge-base --is-ancestor "$b" "$base_ref" 2>/dev/null; then
113
+ printf '%sancestor of %s' "$shape" "$base_ref"; return 0
114
+ fi
115
+ mb="$(git merge-base "$base_ref" "$b" 2>/dev/null)"
116
+ if [ -n "$mb" ]; then
117
+ files="$(git diff --name-only "$mb" "$b" 2>/dev/null)"
118
+ if [ -z "$files" ]; then
119
+ printf '%sno change beyond %s' "$shape" "$base_ref"; return 0
120
+ fi
121
+ # NUL-delimited so a path with spaces survives; xargs exits non-zero if any batch differs.
122
+ if git diff --name-only -z "$mb" "$b" 2>/dev/null | xargs -0 git diff --quiet "$base_ref" "$b" -- 2>/dev/null; then
123
+ printf '%scontent on %s' "$shape" "$base_ref"; return 0
124
+ fi
125
+ fi
126
+ # (b) lineage
127
+ n="$(wt_issue_number "$b")"
128
+ # `[#N]` is the kit's token; `(#N)` is GitHub's own trailing ref (what a squash puts on a PR
129
+ # commit, and what a sub-issue's commit subject carried on a consuming repo's effort). Both name issues.
130
+ [ -n "$n" ] || n="$(git log --format=%s "$base_ref..$b" 2>/dev/null | grep -oE '[[(]#[0-9]+[])]' | head -1 | tr -dc '0-9')"
131
+ [ -n "$n" ] || return 0
132
+ state="$(wt_issue_state "$n" "$repo")"
133
+ [ "$state" = closed ] || return 0
134
+ if printf '%s\n' "$pr_index" | grep -qE "^[a-z]+/${n}-[^ ]* PR#[0-9]+ MERGED\$"; then
135
+ printf '%sissue #%s closed, its PR merged' "$shape" "$n"
136
+ elif pr="$(_kit_gc_issue_merged_pr "$n" "$repo")" && [ -n "$pr" ]; then
137
+ # The index is a 200-PR window; on a repo with hundreds of merged PRs the effort PR that closed
138
+ # #N has scrolled out of it (measured on a consuming repo: the closing PR sat at position 254). The issue itself
139
+ # knows which PR closed it, and that lookup is exact and not window-bound. A sub-issue closes
140
+ # through `effort close`, not a PR, so the answer may come from its parent: "M via P".
141
+ case "$pr" in
142
+ *" via "*) printf '%sissue #%s closed, parent #%s closed by merged PR#%s' "$shape" "$n" "${pr#* via }" "${pr%% *}" ;;
143
+ *) printf '%sissue #%s closed by merged PR#%s' "$shape" "$n" "$pr" ;;
144
+ esac
145
+ fi
146
+ }
147
+
148
+ # Per-run cache for _kit_gc_issue_merged_pr. A file, not a shell variable: the lookup runs inside
149
+ # `$(...)` and `| while read` subshells, where a variable written is lost on return, while `$$` is
150
+ # the top shell's PID in every subshell of one verb run. kit_gc_analyze / kit_gc_prune reset it on
151
+ # entry so a result never outlives the run that made it (an issue closed since would read stale).
152
+ _KIT_GC_CACHE_DIR="${TMPDIR:-/tmp}/kit-gc-cache-$$"
153
+ _kit_gc_cache_reset() { rm -rf "$_KIT_GC_CACHE_DIR" 2>/dev/null; mkdir -p "$_KIT_GC_CACHE_DIR" 2>/dev/null || true; }
154
+
155
+ # _kit_gc_issue_merged_pr <issue> [repo] [depth] — echo the number of a MERGED PR that closed
156
+ # <issue>, or nothing. Reads the issue's `closedByPullRequestsReferences` (exact, not bound to the
157
+ # 200-PR index window) and confirms the state of each candidate. When nothing closed <issue> — a
158
+ # sub-issue closes through `effort close`, not a PR — climb ONCE to its parent (the sub-issues API;
159
+ # 404 = no parent) and answer `<M> via <P>` when the parent was closed by merged PR M. Cached per
160
+ # issue number for the run: an effort's subs and agent branches all name the same N, and 21
161
+ # branches must not mean 21 gh round trips. A negative answer is cached too. rc 0 with empty output
162
+ # when nothing merged closed it or its parent (measured: an effort closed NOT_PLANNED, no PR, no
163
+ # parent — a true ORPHAN).
164
+ _kit_gc_issue_merged_pr() {
165
+ local n="$1" repo="${2:-$KIT_GC_REPO}" depth="${3:-0}" cache="" cand result="" parent
166
+ [ -n "$n" ] || return 0
167
+ command -v gh >/dev/null 2>&1 || return 0
168
+ [ -d "$_KIT_GC_CACHE_DIR" ] && cache="$_KIT_GC_CACHE_DIR/issue-$n"
169
+ if [ -n "$cache" ] && [ -f "$cache" ]; then cat "$cache"; return 0; fi
170
+ while IFS= read -r cand; do
171
+ [ -n "$cand" ] || continue
172
+ case "$cand" in *[!0-9]*) continue ;; esac
173
+ if [ "$(gh pr view "$cand" --repo "$repo" --json state --jq .state 2>/dev/null)" = "MERGED" ]; then
174
+ result="$cand"; break
175
+ fi
176
+ done <<EOF
177
+ $(gh issue view "$n" --repo "$repo" --json closedByPullRequestsReferences \
178
+ --jq '.closedByPullRequestsReferences[].number' 2>/dev/null || true)
179
+ EOF
180
+ if [ -z "$result" ] && [ "$depth" -eq 0 ]; then
181
+ parent="$(gh api "repos/$repo/issues/$n/parent" --jq .number 2>/dev/null || true)"
182
+ case "$parent" in ''|*[!0-9]*) parent="" ;; esac
183
+ if [ -n "$parent" ]; then
184
+ result="$(_kit_gc_issue_merged_pr "$parent" "$repo" 1)"
185
+ [ -n "$result" ] && result="${result%% *} via $parent"
186
+ fi
187
+ fi
188
+ [ -n "$cache" ] && printf '%s' "$result" > "$cache" 2>/dev/null
189
+ printf '%s' "$result"
190
+ }
191
+
192
+ # _kit_gc_classify <branch> <pr_index> [repo] — the ONE verdict for a branch, exactly as
193
+ # kit_gc_analyze prints it after `-> `. kit_gc_prune acts on this same function, so the plan a human
194
+ # approved and the delete that follows can never disagree (Family 1, the failure mode #219 was about).
195
+ # Order: base branch > PROTECTED (issue open) > SAFE (own PR merged) > ACTIVE (own PR open) > a
196
+ # CLOSED-unmerged PR verbatim > LANDED (no PR, work on base) > ORPHAN.
197
+ _kit_gc_classify() {
198
+ local b="$1" pr_index="$2" repo="${3:-$KIT_GC_REPO}" pr prot landed
199
+ case "$b" in "${KIT_BASE_BRANCH:-main}"|develop|main) echo "ACTIVE (base branch)"; return 0 ;; esac
200
+ pr="$(_kit_gc_pr_for "$pr_index" "$b")"
201
+ prot="$(wt_protected_reason "$b" "$repo" 2>/dev/null || true)"
202
+ if [ -n "$prot" ]; then
203
+ echo "PROTECTED: $prot"
204
+ elif printf '%s' "$pr" | grep -q 'MERGED'; then
205
+ echo "SAFE (${pr}, issue closed/absent — verify level with remote before delete)"
206
+ elif printf '%s' "$pr" | grep -q 'OPEN'; then
207
+ echo "ACTIVE (${pr})"
208
+ elif [ -n "$pr" ]; then
209
+ echo "$pr"
210
+ else
211
+ landed="$(_kit_gc_landed_reason "$b" "$pr_index" "$repo")"
212
+ if [ -n "$landed" ]; then echo "LANDED ($landed)"
213
+ else echo "ORPHAN (no PR — surface, never auto-delete)"; fi
214
+ fi
215
+ }
216
+
74
217
  # kit_gc_analyze — read-only classification of worktrees, branches, and stashes. Writes NOTHING.
75
- # Each row is tagged PROTECTED / SAFE / ACTIVE / ORPHAN so a human or UI can decide what to prune.
218
+ # Each row is tagged PROTECTED / SAFE / ACTIVE / LANDED / ORPHAN so a human or UI can decide what
219
+ # to prune.
76
220
  kit_gc_analyze() {
77
221
  _kit_gc_require_deps || return 1
222
+ _kit_gc_cache_reset
78
223
  # `wtpath`, not `path`: under zsh `path` is tied to PATH (special array), so a bare `path` local
79
224
  # here would clobber the command search path on assignment. A namespaced name is inert.
80
- local repo="$KIT_GC_REPO" b ref wtpath reason pr prot
225
+ local repo="$KIT_GC_REPO" b ref wtpath reason
81
226
  git fetch origin --prune --quiet 2>/dev/null || true
82
227
 
83
228
  echo "# worktrees"
@@ -96,18 +241,7 @@ kit_gc_analyze() {
96
241
  local pr_index; pr_index="$(_kit_gc_pr_index "$repo")"
97
242
  git branch --format='%(refname:short)' 2>/dev/null | while IFS= read -r b; do
98
243
  [ -n "$b" ] || continue
99
- case "$b" in "${KIT_BASE_BRANCH:-main}"|develop|main) echo " $b -> ACTIVE (base branch)"; continue;; esac
100
- pr="$(_kit_gc_pr_for "$pr_index" "$b")"
101
- prot="$(wt_protected_reason "$b" "$repo" 2>/dev/null || true)"
102
- if [ -n "$prot" ]; then
103
- echo " $b -> PROTECTED: $prot"
104
- elif printf '%s' "$pr" | grep -q 'MERGED'; then
105
- echo " $b -> SAFE (${pr}, issue closed/absent — verify level with remote before delete)"
106
- elif printf '%s' "$pr" | grep -q 'OPEN'; then
107
- echo " $b -> ACTIVE (${pr})"
108
- else
109
- echo " $b -> ${pr:-ORPHAN (no PR — surface, never auto-delete)}"
110
- fi
244
+ echo " $b -> $(_kit_gc_classify "$b" "$pr_index" "$repo")"
111
245
  done
112
246
 
113
247
  echo "# stashes"
@@ -129,9 +263,10 @@ EOF
129
263
  [ "$found" -eq 1 ] || echo " (none)"
130
264
  }
131
265
 
132
- # kit_gc_has_prunable — rc 0 if at least one branch is SAFE to delete (a merged, unprotected branch).
266
+ # kit_gc_has_prunable — rc 0 if at least one branch is deletable: SAFE (its own PR merged) or LANDED
267
+ # (no PR of its own, work already on base).
133
268
  kit_gc_has_prunable() {
134
- kit_gc_analyze 2>/dev/null | grep -q '> SAFE '
269
+ kit_gc_analyze 2>/dev/null | grep -qE '> (SAFE|LANDED) '
135
270
  }
136
271
 
137
272
  # _kit_gc_zombies — echo "<name>\t<branch>\t<staged:yes|no>" for each ZOMBIE worktree: its working
@@ -222,38 +357,68 @@ _kit_gc_remote_ahead() {
222
357
  [ "$(git rev-list --count "$b..origin/$b" 2>/dev/null || echo 1)" != "0" ]
223
358
  }
224
359
 
225
- # kit_gc_prune [--yes] - remove worktrees + local branches whose PR is MERGED (the SAFE rows).
360
+ # _kit_gc_only_match <regex> <branch> — rc 0 when <regex> is empty or <branch> matches it (ERE).
361
+ _kit_gc_only_match() {
362
+ [ -n "$1" ] || return 0
363
+ printf '%s\n' "$2" | grep -qE -- "$1"
364
+ }
365
+
366
+ # _kit_gc_prune_why <verdict> — echo the reason a prune row prints, or nothing when the verdict is
367
+ # not a deletable one. SAFE (own PR merged) and LANDED (no PR, work on base — #388) are the only two.
368
+ _kit_gc_prune_why() {
369
+ case "$1" in
370
+ SAFE*) echo "PR MERGED" ;;
371
+ LANDED*) echo "$1" ;;
372
+ *) : ;;
373
+ esac
374
+ }
375
+
376
+ # kit_gc_prune [--yes] [--only <regex>] - remove worktrees + local branches whose PR is MERGED (the
377
+ # SAFE rows) or whose work is already on base with no PR of their own (the LANDED rows, #388).
226
378
  # DRY-RUN by default (lists what it WOULD remove); --yes performs the deletions. Never touches a
227
379
  # PROTECTED/ACTIVE/ORPHAN branch, and never a DIRTY worktree (recover-before-prune): a worktree with
228
380
  # staged/unstaged/untracked changes is skipped with a warning, not destroyed. The remote branch is
229
381
  # already deleted at merge time (gh pr merge --delete-branch); this cleans up the local side.
382
+ # `--only <regex>` restricts both loops to branches matching the ERE (effort_close passes
383
+ # `^sub/<N>[a-z]-` to collect an effort's subs; a regex, not a glob — zsh does not expand a glob
384
+ # held in a variable inside `case`, bash does, and this file is sourced by both); the zombie pass is skipped under --only, since it
385
+ # is not scoped to a branch — that stays with a full `gc --prune` / `cleanup`.
230
386
  kit_gc_prune() {
231
387
  _kit_gc_require_deps || return 1
388
+ _kit_gc_cache_reset
232
389
  # `wtpath`, not `path`: under zsh `path` is tied to PATH (special array), so assigning to a bare
233
390
  # `path` local would clobber the command search path. A namespaced name is inert.
234
- local repo="$KIT_GC_REPO" yes=0 a wtpath ref b pr pr_index
235
- for a in "$@"; do case "$a" in --yes|-y) yes=1 ;; esac; done
391
+ local repo="$KIT_GC_REPO" yes=0 wtpath ref b pr_index verdict why only=""
392
+ while [ $# -gt 0 ]; do
393
+ case "$1" in
394
+ --yes|-y) yes=1 ;;
395
+ --only) only="${2:-}"; shift ;;
396
+ --only=*) only="${1#--only=}" ;;
397
+ esac
398
+ shift
399
+ done
236
400
 
237
401
  # All PRs in ONE call, indexed by head branch (#124) — shared by both loops below instead of a
238
402
  # per-branch `gh pr list --head` (the N+1 that scaled badly with many stale branches).
239
403
  pr_index="$(_kit_gc_pr_index "$repo")"
240
404
 
241
- # Worktrees first - a branch's worktree must be removed before the branch can be deleted.
405
+ # Worktrees first - a branch's worktree must be removed before the branch can be deleted. The
406
+ # verdict comes from the ONE classifier (_kit_gc_classify), the same function analyze prints.
242
407
  git worktree list --porcelain 2>/dev/null \
243
408
  | awk '/^worktree /{w=$2} /^branch /{print w" "$2}' \
244
409
  | while read -r wtpath ref; do
245
410
  b="${ref#refs/heads/}"
246
- case "$b" in "${KIT_BASE_BRANCH:-main}"|develop|main|"") continue ;; esac
247
- [ -n "$(wt_protected_reason "$b" "$repo" 2>/dev/null || true)" ] && continue
248
- pr="$(_kit_gc_pr_for "$pr_index" "$b")"
249
- printf '%s' "$pr" | grep -q 'MERGED' || continue
411
+ [ -n "$b" ] || continue
412
+ _kit_gc_only_match "$only" "$b" || continue
413
+ verdict="$(_kit_gc_classify "$b" "$pr_index" "$repo")"
414
+ why="$(_kit_gc_prune_why "$verdict")"; [ -n "$why" ] || continue
250
415
  if [ -n "$(git -C "$wtpath" status --porcelain 2>/dev/null)" ]; then
251
416
  echo " SKIP dirty worktree $wtpath [$b] - commit/recover before pruning" >&2; continue
252
417
  fi
253
418
  if [ "$yes" -eq 1 ]; then
254
- git worktree remove --force "$wtpath" 2>/dev/null && echo " removed worktree $wtpath [$b]"
419
+ git worktree remove --force "$wtpath" 2>/dev/null && echo " removed worktree $wtpath [$b] ($why)"
255
420
  else
256
- echo " would remove worktree $wtpath [$b] (PR MERGED)"
421
+ echo " would remove worktree $wtpath [$b] ($why)"
257
422
  fi
258
423
  done
259
424
 
@@ -262,26 +427,27 @@ kit_gc_prune() {
262
427
  # prune` deletes that index, orphaning the blobs for a later `git gc` to reap — real data loss in a
263
428
  # documented incident. Recover any staged delta to its branch FIRST, and gate the prune sweep
264
429
  # behind --yes so it NEVER runs in a dry-run (the previous unconditional prune was the hazard).
265
- kit_gc_recover_zombies "$yes"
266
- [ "$yes" -eq 1 ] && { git worktree prune 2>/dev/null || true; }
430
+ if [ -z "$only" ]; then
431
+ kit_gc_recover_zombies "$yes"
432
+ [ "$yes" -eq 1 ] && { git worktree prune 2>/dev/null || true; }
433
+ fi
267
434
 
268
- # Then local branches whose PR merged (worktree now gone). while-read (NOT `for b in $(...)`) so
269
- # branch iteration survives a NUL-polluted IFS (#124).
435
+ # Then local branches whose PR merged, or whose work landed (worktree now gone). while-read (NOT
436
+ # `for b in $(...)`) so branch iteration survives a NUL-polluted IFS (#124).
270
437
  git branch --format='%(refname:short)' 2>/dev/null | while IFS= read -r b; do
271
438
  [ -n "$b" ] || continue
272
- case "$b" in "${KIT_BASE_BRANCH:-main}"|develop|main) continue ;; esac
273
- [ -n "$(wt_protected_reason "$b" "$repo" 2>/dev/null || true)" ] && continue
274
- pr="$(_kit_gc_pr_for "$pr_index" "$b")"
275
- printf '%s' "$pr" | grep -q 'MERGED' || continue
276
- # A merged PR is not on its own sufficient: the local ref may carry commits that were never
277
- # pushed, and `git branch -D` would be their last rites.
439
+ _kit_gc_only_match "$only" "$b" || continue
440
+ verdict="$(_kit_gc_classify "$b" "$pr_index" "$repo")"
441
+ why="$(_kit_gc_prune_why "$verdict")"; [ -n "$why" ] || continue
442
+ # A merged PR (or landed content) is not on its own sufficient: the local ref may carry commits
443
+ # that were never pushed, and `git branch -D` would be their last rites.
278
444
  if _kit_gc_has_unpushed "$b"; then
279
445
  echo " SKIP $b — ahead of origin/$b (unpushed commits); recover before pruning" >&2; continue
280
446
  fi
281
447
  if [ "$yes" -eq 1 ]; then
282
- git branch -D "$b" >/dev/null 2>&1 && echo " deleted local branch $b (PR MERGED)"
448
+ git branch -D "$b" >/dev/null 2>&1 && echo " deleted local branch $b ($why)"
283
449
  else
284
- echo " would delete local branch $b (PR MERGED)"
450
+ echo " would delete local branch $b ($why)"
285
451
  fi
286
452
  done
287
453
  [ "$yes" -eq 1 ] && echo "gc prune: done" || echo "gc prune: DRY RUN - pass --yes to delete"
@@ -306,11 +472,13 @@ _kit_gc_names() {
306
472
  # omits is a deletion the user did not get to veto, so the plan must name EVERYTHING --yes can touch:
307
473
  # worktrees as well as branches.
308
474
  #
309
- # What --yes deletes: merged worktrees, merged local branches (via kit_gc_prune, which also runs the
310
- # recover-before-prune zombie pass), and the REMOTE ref of a merged branch that is exactly level with
311
- # it. What it NEVER deletes: an ORPHAN branch (local commits not on the base remote), a PROTECTED
312
- # branch (its issue is still open), or a stash. Those three are irreversible or hold the only copy of
313
- # work, so they are surfaced BY NAME for a human and left exactly where they are.
475
+ # What --yes deletes: SAFE and LANDED worktrees and local branches (via kit_gc_prune, which also runs
476
+ # the recover-before-prune zombie pass) — SAFE = its own PR merged, LANDED = no PR of its own but its
477
+ # work is already on base (#388: effort subs, agent isolation worktrees) — and the REMOTE ref of such
478
+ # a branch when it is exactly level with it. What it NEVER deletes: an ORPHAN branch (local commits
479
+ # not on the base remote), a PROTECTED branch (its issue is still open), or a stash. Those three are
480
+ # irreversible or hold the only copy of work, so they are surfaced BY NAME for a human and left
481
+ # exactly where they are.
314
482
  #
315
483
  # NOTE: the underlying kit_gc_analyze refreshes remote-tracking refs (`git fetch --prune`) so the
316
484
  # classification is not made against a stale view of the remote. That is the only write a plan-only
@@ -319,22 +487,27 @@ kit_gc_cleanup() {
319
487
  _kit_gc_require_deps || return 1
320
488
  local repo="$KIT_GC_REPO" yes=0 a out b n_safe n_orphan n_prot n_stash n_wt rc=0 n_rfail=0
321
489
  local zname zbranch zstaged
322
- local safe_list orphan_list prot_list stash_list wt_list zomb_list level_ok n_zomb
490
+ local safe_list landed_list orphan_list prot_list stash_list wt_list zomb_list level_ok n_zomb n_landed
323
491
  for a in "$@"; do case "$a" in --yes|-y) yes=1 ;; esac; done
324
492
 
325
493
  out="$(kit_gc_analyze)" || return 1
326
494
  safe_list="$(_kit_gc_names "$out" branches ' -> SAFE ')"
495
+ # LANDED rows keep their reason (`name (reason)`) so the plan says WHY each one may go (#388).
496
+ landed_list="$(printf '%s\n' "$out" | awk '
497
+ /^# / { in_s = ($0 == "# branches"); next }
498
+ in_s && / -> LANDED / { sub(/^ /, ""); sub(/ -> LANDED /, " "); print }')"
327
499
  orphan_list="$(_kit_gc_names "$out" branches ' -> ORPHAN')"
328
500
  prot_list="$(_kit_gc_names "$out" branches ' -> PROTECTED:')"
329
501
  stash_list="$(printf '%s\n' "$out" | awk '/^# /{in_s=($0=="# stashes"); next} in_s && NF' | sed 's/^ //')"
330
502
 
331
- # Worktrees kit_gc_prune would remove: the ones whose branch is itself SAFE (same condition prune
332
- # applies — unprotected + PR MERGED). Derived from the SAFE list so the two cannot diverge.
503
+ # Worktrees kit_gc_prune would remove: the ones whose branch is itself SAFE or LANDED (the same
504
+ # verdicts prune acts on). Derived from those lists so the two cannot diverge.
333
505
  wt_list="$(git worktree list --porcelain 2>/dev/null \
334
506
  | awk '/^worktree /{w=$2} /^branch /{print w" "$2}' \
335
507
  | while read -r wtpath ref; do
336
508
  b="${ref#refs/heads/}"
337
- printf '%s\n' "$safe_list" | grep -qxF "$b" || continue
509
+ printf '%s\n' "$safe_list" | grep -qxF "$b" \
510
+ || printf '%s\n' "$landed_list" | sed 's/ (.*//' | grep -qxF "$b" || continue
338
511
  if [ -n "$(git -C "$wtpath" status --porcelain 2>/dev/null)" ]; then
339
512
  echo "$wtpath [$b] (DIRTY — will be skipped, not destroyed)"
340
513
  else
@@ -359,6 +532,7 @@ ZEOF
359
532
  )"
360
533
 
361
534
  n_safe="$(printf '%s' "$safe_list" | grep -c . || true)"
535
+ n_landed="$(printf '%s' "$landed_list" | grep -c . || true)"
362
536
  n_orphan="$(printf '%s' "$orphan_list" | grep -c . || true)"
363
537
  n_prot="$(printf '%s' "$prot_list" | grep -c . || true)"
364
538
  n_stash="$(printf '%s' "$stash_list" | grep -c . || true)"
@@ -368,10 +542,12 @@ ZEOF
368
542
  echo "# cleanup plan"
369
543
  printf ' SAFE branches %3s merged PR, issue closed/absent\n' "$n_safe"
370
544
  printf '%s\n' "$safe_list" | grep . | sed 's/^/ /' || true
371
- printf ' SAFE worktrees %3s removed with their branch\n' "$n_wt"
545
+ printf ' SAFE worktrees %3s removed with their branch (SAFE or LANDED)\n' "$n_wt"
372
546
  printf '%s\n' "$wt_list" | grep . | sed 's/^/ /' || true
373
547
  printf ' ZOMBIE worktrees %3s staged work recovered to its branch, then metadata pruned\n' "$n_zomb"
374
548
  printf '%s\n' "$zomb_list" | grep . | sed 's/^/ /' || true
549
+ printf ' LANDED branches %3s no PR of its own, work already on the base branch\n' "$n_landed"
550
+ printf '%s\n' "$landed_list" | grep . | sed 's/^/ /' || true
375
551
  printf ' ORPHAN kept %3s commits not on the base remote — never auto-deleted\n' "$n_orphan"
376
552
  printf '%s\n' "$orphan_list" | grep . | sed 's/^/ /' || true
377
553
  printf ' PROTECTED kept %3s issue still OPEN\n' "$n_prot"
@@ -380,14 +556,15 @@ ZEOF
380
556
  printf '%s\n' "$stash_list" | grep . | sed 's/^/ /' || true
381
557
 
382
558
  if [ "$yes" -ne 1 ]; then
383
- echo "cleanup: PLAN ONLY — nothing deleted. Pass --yes to delete the SAFE rows and prune the ZOMBIE rows."
559
+ echo "cleanup: PLAN ONLY — nothing deleted. Pass --yes to delete the SAFE and LANDED rows and prune the ZOMBIE rows."
384
560
  return 0
385
561
  fi
386
- [ "$n_safe" -eq 0 ] && [ "$n_wt" -eq 0 ] && [ "$n_zomb" -eq 0 ] && { echo "cleanup: nothing SAFE to delete."; return 0; }
562
+ [ "$n_safe" -eq 0 ] && [ "$n_landed" -eq 0 ] && [ "$n_wt" -eq 0 ] && [ "$n_zomb" -eq 0 ] \
563
+ && { echo "cleanup: nothing SAFE or LANDED to delete."; return 0; }
387
564
 
388
- # Which SAFE branches may have their REMOTE deleted — decided BEFORE the local prune, because once
389
- # the local ref is gone there is nothing left to compare. Requires exact equality in BOTH
390
- # directions: local ⊆ remote alone would let a remote-ahead branch lose remote-only history.
565
+ # Which SAFE / LANDED branches may have their REMOTE deleted — decided BEFORE the local prune,
566
+ # because once the local ref is gone there is nothing left to compare. Requires exact equality in
567
+ # BOTH directions: local ⊆ remote alone would let a remote-ahead branch lose remote-only history.
391
568
  level_ok=""
392
569
  while IFS= read -r b; do
393
570
  [ -n "$b" ] || continue
@@ -398,6 +575,7 @@ ZEOF
398
575
  "
399
576
  done <<EOF
400
577
  $safe_list
578
+ $(printf '%s\n' "$landed_list" | sed 's/ (.*//')
401
579
  EOF
402
580
 
403
581
  kit_gc_prune --yes || rc=1
@@ -417,6 +595,6 @@ EOF
417
595
  $level_ok
418
596
  EOF
419
597
  printf ' remote deletes failed %3s\n' "$n_rfail"
420
- echo "cleanup: done — $n_zomb zombie pruned / $n_orphan orphan / $n_prot protected / $n_stash stash left untouched."
598
+ echo "cleanup: done — $n_safe safe + $n_landed landed deleted / $n_zomb zombie pruned / $n_orphan orphan / $n_prot protected / $n_stash stash left untouched."
421
599
  return "$rc"
422
600
  }