@jeiemgi/cckit 0.7.1 → 0.9.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 (82) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +15 -4
  3. package/README.md +25 -3
  4. package/bin/cckit +91 -8
  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 +73 -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-metrics.sh +14 -1
  25. package/scripts/lib/effort-ops-test.sh +54 -2
  26. package/scripts/lib/effort-ops.sh +243 -23
  27. package/scripts/lib/effort-parity-test.sh +5 -3
  28. package/scripts/lib/effort-slug.sh +16 -2
  29. package/scripts/lib/effort.sh +49 -15
  30. package/scripts/lib/gh-project.sh +14 -0
  31. package/scripts/lib/gh-rest-test.sh +301 -0
  32. package/scripts/lib/gh-rest.sh +289 -0
  33. package/scripts/lib/hook-refresh-test.sh +65 -0
  34. package/scripts/lib/hook-refresh.sh +62 -0
  35. package/scripts/lib/kit-config.sh +6 -0
  36. package/scripts/lib/kit-gc-test.sh +178 -1
  37. package/scripts/lib/kit-gc.sh +251 -55
  38. package/scripts/lib/kit-task-ops-test.sh +15 -0
  39. package/scripts/lib/kit-task-ops.sh +231 -58
  40. package/scripts/lib/kto-task-close-test.sh +83 -0
  41. package/scripts/lib/kto-task-pr-merge-test.sh +129 -0
  42. package/scripts/lib/kto-task-pr-test.sh +59 -0
  43. package/scripts/lib/orchestration-runtime-test.sh +142 -0
  44. package/scripts/lib/orchestration-runtime.sh +171 -0
  45. package/scripts/lib/review-stage-test.sh +241 -0
  46. package/scripts/lib/review-stage.sh +259 -0
  47. package/scripts/lib/stage-receipt-test.sh +252 -0
  48. package/scripts/lib/stage-receipt.sh +442 -0
  49. package/scripts/lib/test-env-test.sh +25 -0
  50. package/scripts/lib/test-env.sh +33 -0
  51. package/scripts/lib/worker-brief-test.sh +120 -0
  52. package/scripts/lib/worker-brief.sh +185 -0
  53. package/scripts/lib/worktree-issue.sh +14 -0
  54. package/scripts/lib/worktree-ports-test.sh +146 -0
  55. package/scripts/lib/worktree-start.sh +153 -8
  56. package/scripts/lib/zsh-safety-test.sh +67 -0
  57. package/scripts/orchestrate.sh +124 -22
  58. package/scripts/portable-test.sh +22 -1
  59. package/scripts/review-lint-baseline.txt +28 -0
  60. package/scripts/review-lint-test.sh +271 -0
  61. package/scripts/review-lint.sh +259 -0
  62. package/scripts/review-rules.conf +55 -0
  63. package/scripts/setup-labels.sh +21 -1
  64. package/scripts/skill-frontmatter-baseline.txt +24 -0
  65. package/scripts/skill-frontmatter-lint-test.sh +156 -0
  66. package/scripts/skill-frontmatter-lint.sh +182 -0
  67. package/scripts/task-sync.sh +8 -0
  68. package/scripts/test.sh +10 -1
  69. package/skills/kit-autopilot/SKILL.md +13 -1
  70. package/skills/kit-cleanup/SKILL.md +5 -3
  71. package/skills/kit-contribute/SKILL.md +6 -5
  72. package/skills/kit-effort-close/SKILL.md +8 -3
  73. package/skills/kit-effort-pr/SKILL.md +16 -95
  74. package/skills/kit-gc/SKILL.md +15 -10
  75. package/skills/kit-security-sweep/SKILL.md +4 -6
  76. package/skills/kit-ship/SKILL.md +1 -1
  77. package/skills/kit-task-pr/SKILL.md +2 -0
  78. package/templates/hooks/guard-raw-git-ops.sh.tmpl +193 -0
  79. package/templates/rules/branch-naming.md +6 -0
  80. package/templates/rules/effort-model.md +3 -0
  81. package/templates/rules/naming-and-ids.md +4 -1
  82. package/templates/settings/settings.json.tmpl +4 -2
@@ -12,12 +12,26 @@
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.
19
24
  # errors: mixed — kit_gc_analyze is read-only and forgiving; the prune path propagates
20
25
 
26
+ # The GitHub transport switch (#396): rest mode routes the gh calls below through gh-rest.sh.
27
+ if ! command -v cckit_gh_rest >/dev/null 2>&1; then
28
+ _ghr_lib="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>/dev/null && pwd)/gh-rest.sh"
29
+ # shellcheck source=/dev/null
30
+ [ -f "$_ghr_lib" ] && . "$_ghr_lib"
31
+ unset _ghr_lib
32
+ fi
33
+ command -v cckit_gh_rest >/dev/null 2>&1 || cckit_gh_rest() { return 1; }
34
+
21
35
  KIT_GC_REPO="${KIT_GC_REPO:-${KIT_REPO:-}}"
22
36
 
23
37
  _kit_gc_root() { git worktree list --porcelain 2>/dev/null | awk '/^worktree /{print $2; exit}'; }
@@ -62,6 +76,11 @@ _kit_gc_require_deps() {
62
76
  # scaled badly exactly when gc matters most (many stale branches). #124.
63
77
  _kit_gc_pr_index() {
64
78
  local repo="${1:-$KIT_GC_REPO}"
79
+ if cckit_gh_rest; then
80
+ gh api "repos/$repo/pulls" -X GET -f state=all -f per_page=100 \
81
+ --jq '.[] | "\(.head.ref)\tPR#\(.number) \(if .merged_at then "MERGED" else (.state | ascii_upcase) end)"' 2>/dev/null || true
82
+ return 0
83
+ fi
65
84
  gh pr list --repo "$repo" --state all --limit 200 --json number,state,headRefName \
66
85
  --jq '.[] | "\(.headRefName)\tPR#\(.number) \(.state)"' 2>/dev/null || true
67
86
  }
@@ -71,13 +90,157 @@ _kit_gc_pr_for() {
71
90
  printf '%s\n' "$1" | awk -F'\t' -v want="$2" '$1==want{print $2; exit}'
72
91
  }
73
92
 
93
+ # _kit_gc_base_ref — the ref the LANDED checks compare against: `origin/<base>` when that
94
+ # remote-tracking ref exists (the merge target), else the local base branch (a repo with no origin,
95
+ # e.g. a test fixture). Empty when neither exists — then nothing can be LANDED.
96
+ _kit_gc_base_ref() {
97
+ local base="${KIT_BASE_BRANCH:-main}"
98
+ if git show-ref --verify --quiet "refs/remotes/origin/$base" 2>/dev/null; then printf 'origin/%s' "$base"
99
+ elif git show-ref --verify --quiet "refs/heads/$base" 2>/dev/null; then printf '%s' "$base"
100
+ fi
101
+ }
102
+
103
+ # _kit_gc_landed_reason <branch> <pr_index> [repo] — echo why <branch>'s work is already on the base
104
+ # branch, or nothing when it is not. The rows #388 is about: a branch with NO PR of its own whose work
105
+ # reached base through someone else's PR — an effort's `sub/<N><letter>` (merged into effort/<N>, then
106
+ # squash-merged), an `effort/<N>` whose remote ref is gone, a Claude Code Agent-tool isolation
107
+ # worktree (`worktree-agent-<hash>`, merged into the task branch that had the PR). `git cherry` and
108
+ # `--is-ancestor` cannot see through a squash (it rewrites the patch ids), so two checks, either of
109
+ # which suffices:
110
+ # (a) content — every file the branch changed since its merge base with base now has the same
111
+ # content on base (`git diff --quiet <base> <branch> -- <those files>`): the branch holds no
112
+ # change base lacks, whatever path it took there. A branch with no change at all beyond its
113
+ # merge base (or an ancestor of base) is landed trivially.
114
+ # (b) lineage — the name (`sub/<N>…`, `effort/<N>…`, any `<kind>/<N>-…`) or a `[#N]` in one of its
115
+ # commit subjects names an issue that is CLOSED and whose PR is MERGED: either a head
116
+ # `<kind>/<N>-…` in the PR index, or — when the PR has scrolled out of that 200-PR window — a
117
+ # PR the issue itself reports as having closed it (_kit_gc_issue_merged_pr, cached per issue).
118
+ # Covers a sub whose files base has since changed further, and an agent branch whose `[#N]`
119
+ # names the effort parent while the PR was the effort's.
120
+ # A branch failing both stays ORPHAN. Never called for a branch with a PR of its own.
121
+ _kit_gc_landed_reason() {
122
+ local b="$1" pr_index="$2" repo="${3:-$KIT_GC_REPO}" base_ref mb files shape="" n state pr
123
+ base_ref="$(_kit_gc_base_ref)"; [ -n "$base_ref" ] || return 0
124
+ case "$b" in worktree-agent-*) shape="agent worktree; " ;; esac
125
+ # (a) content
126
+ if git merge-base --is-ancestor "$b" "$base_ref" 2>/dev/null; then
127
+ printf '%sancestor of %s' "$shape" "$base_ref"; return 0
128
+ fi
129
+ mb="$(git merge-base "$base_ref" "$b" 2>/dev/null)"
130
+ if [ -n "$mb" ]; then
131
+ files="$(git diff --name-only "$mb" "$b" 2>/dev/null)"
132
+ if [ -z "$files" ]; then
133
+ printf '%sno change beyond %s' "$shape" "$base_ref"; return 0
134
+ fi
135
+ # NUL-delimited so a path with spaces survives; xargs exits non-zero if any batch differs.
136
+ if git diff --name-only -z "$mb" "$b" 2>/dev/null | xargs -0 git diff --quiet "$base_ref" "$b" -- 2>/dev/null; then
137
+ printf '%scontent on %s' "$shape" "$base_ref"; return 0
138
+ fi
139
+ fi
140
+ # (b) lineage
141
+ n="$(wt_issue_number "$b")"
142
+ # `[#N]` is the kit's token; `(#N)` is GitHub's own trailing ref (what a squash puts on a PR
143
+ # commit, and what a sub-issue's commit subject carried on a consuming repo's effort). Both name issues.
144
+ [ -n "$n" ] || n="$(git log --format=%s "$base_ref..$b" 2>/dev/null | grep -oE '[[(]#[0-9]+[])]' | head -1 | tr -dc '0-9')"
145
+ [ -n "$n" ] || return 0
146
+ state="$(wt_issue_state "$n" "$repo")"
147
+ [ "$state" = closed ] || return 0
148
+ if printf '%s\n' "$pr_index" | grep -qE "^[a-z]+/${n}-[^ ]* PR#[0-9]+ MERGED\$"; then
149
+ printf '%sissue #%s closed, its PR merged' "$shape" "$n"
150
+ elif pr="$(_kit_gc_issue_merged_pr "$n" "$repo")" && [ -n "$pr" ]; then
151
+ # The index is a 200-PR window; on a repo with hundreds of merged PRs the effort PR that closed
152
+ # #N has scrolled out of it (measured on a consuming repo: the closing PR sat at position 254). The issue itself
153
+ # knows which PR closed it, and that lookup is exact and not window-bound. A sub-issue closes
154
+ # through `effort close`, not a PR, so the answer may come from its parent: "M via P".
155
+ case "$pr" in
156
+ *" via "*) printf '%sissue #%s closed, parent #%s closed by merged PR#%s' "$shape" "$n" "${pr#* via }" "${pr%% *}" ;;
157
+ *) printf '%sissue #%s closed by merged PR#%s' "$shape" "$n" "$pr" ;;
158
+ esac
159
+ fi
160
+ }
161
+
162
+ # Per-run cache for _kit_gc_issue_merged_pr. A file, not a shell variable: the lookup runs inside
163
+ # `$(...)` and `| while read` subshells, where a variable written is lost on return, while `$$` is
164
+ # the top shell's PID in every subshell of one verb run. kit_gc_analyze / kit_gc_prune reset it on
165
+ # entry so a result never outlives the run that made it (an issue closed since would read stale).
166
+ _KIT_GC_CACHE_DIR="${TMPDIR:-/tmp}/kit-gc-cache-$$"
167
+ _kit_gc_cache_reset() { rm -rf "$_KIT_GC_CACHE_DIR" 2>/dev/null; mkdir -p "$_KIT_GC_CACHE_DIR" 2>/dev/null || true; }
168
+
169
+ # _kit_gc_issue_merged_pr <issue> [repo] [depth] — echo the number of a MERGED PR that closed
170
+ # <issue>, or nothing. Reads the issue's `closedByPullRequestsReferences` (exact, not bound to the
171
+ # 200-PR index window) and confirms the state of each candidate. When nothing closed <issue> — a
172
+ # sub-issue closes through `effort close`, not a PR — climb ONCE to its parent (the sub-issues API;
173
+ # 404 = no parent) and answer `<M> via <P>` when the parent was closed by merged PR M. Cached per
174
+ # issue number for the run: an effort's subs and agent branches all name the same N, and 21
175
+ # branches must not mean 21 gh round trips. A negative answer is cached too. rc 0 with empty output
176
+ # when nothing merged closed it or its parent (measured: an effort closed NOT_PLANNED, no PR, no
177
+ # parent — a true ORPHAN).
178
+ _kit_gc_issue_merged_pr() {
179
+ local n="$1" repo="${2:-$KIT_GC_REPO}" depth="${3:-0}" cache="" cand result="" parent
180
+ [ -n "$n" ] || return 0
181
+ command -v gh >/dev/null 2>&1 || return 0
182
+ [ -d "$_KIT_GC_CACHE_DIR" ] && cache="$_KIT_GC_CACHE_DIR/issue-$n"
183
+ if [ -n "$cache" ] && [ -f "$cache" ]; then cat "$cache"; return 0; fi
184
+ while IFS= read -r cand; do
185
+ [ -n "$cand" ] || continue
186
+ case "$cand" in *[!0-9]*) continue ;; esac
187
+ if cckit_gh_rest; then
188
+ # REST: the candidate came from a merged cross-reference that closes <n> (gh_rest_wave_rows).
189
+ result="$cand"; break
190
+ elif [ "$(gh pr view "$cand" --repo "$repo" --json state --jq .state 2>/dev/null)" = "MERGED" ]; then
191
+ result="$cand"; break
192
+ fi
193
+ done <<EOF
194
+ $(if cckit_gh_rest; then gh_rest_issue_closing_prs "$repo" "$n" 2>/dev/null || true
195
+ else gh issue view "$n" --repo "$repo" --json closedByPullRequestsReferences \
196
+ --jq '.closedByPullRequestsReferences[].number' 2>/dev/null || true; fi)
197
+ EOF
198
+ if [ -z "$result" ] && [ "$depth" -eq 0 ]; then
199
+ parent="$(gh api "repos/$repo/issues/$n/parent" --jq .number 2>/dev/null || true)"
200
+ case "$parent" in ''|*[!0-9]*) parent="" ;; esac
201
+ if [ -n "$parent" ]; then
202
+ result="$(_kit_gc_issue_merged_pr "$parent" "$repo" 1)"
203
+ [ -n "$result" ] && result="${result%% *} via $parent"
204
+ fi
205
+ fi
206
+ [ -n "$cache" ] && printf '%s' "$result" > "$cache" 2>/dev/null
207
+ printf '%s' "$result"
208
+ }
209
+
210
+ # _kit_gc_classify <branch> <pr_index> [repo] — the ONE verdict for a branch, exactly as
211
+ # kit_gc_analyze prints it after `-> `. kit_gc_prune acts on this same function, so the plan a human
212
+ # approved and the delete that follows can never disagree (Family 1, the failure mode #219 was about).
213
+ # Order: base branch > PROTECTED (issue open) > SAFE (own PR merged) > ACTIVE (own PR open) > a
214
+ # CLOSED-unmerged PR verbatim > LANDED (no PR, work on base) > ORPHAN.
215
+ _kit_gc_classify() {
216
+ local b="$1" pr_index="$2" repo="${3:-$KIT_GC_REPO}" pr prot landed
217
+ case "$b" in "${KIT_BASE_BRANCH:-main}"|develop|main) echo "ACTIVE (base branch)"; return 0 ;; esac
218
+ pr="$(_kit_gc_pr_for "$pr_index" "$b")"
219
+ prot="$(wt_protected_reason "$b" "$repo" 2>/dev/null || true)"
220
+ if [ -n "$prot" ]; then
221
+ echo "PROTECTED: $prot"
222
+ elif printf '%s' "$pr" | grep -q 'MERGED'; then
223
+ echo "SAFE (${pr}, issue closed/absent — verify level with remote before delete)"
224
+ elif printf '%s' "$pr" | grep -q 'OPEN'; then
225
+ echo "ACTIVE (${pr})"
226
+ elif [ -n "$pr" ]; then
227
+ echo "$pr"
228
+ else
229
+ landed="$(_kit_gc_landed_reason "$b" "$pr_index" "$repo")"
230
+ if [ -n "$landed" ]; then echo "LANDED ($landed)"
231
+ else echo "ORPHAN (no PR — surface, never auto-delete)"; fi
232
+ fi
233
+ }
234
+
74
235
  # 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.
236
+ # Each row is tagged PROTECTED / SAFE / ACTIVE / LANDED / ORPHAN so a human or UI can decide what
237
+ # to prune.
76
238
  kit_gc_analyze() {
77
239
  _kit_gc_require_deps || return 1
240
+ _kit_gc_cache_reset
78
241
  # `wtpath`, not `path`: under zsh `path` is tied to PATH (special array), so a bare `path` local
79
242
  # 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
243
+ local repo="$KIT_GC_REPO" b ref wtpath reason
81
244
  git fetch origin --prune --quiet 2>/dev/null || true
82
245
 
83
246
  echo "# worktrees"
@@ -96,18 +259,7 @@ kit_gc_analyze() {
96
259
  local pr_index; pr_index="$(_kit_gc_pr_index "$repo")"
97
260
  git branch --format='%(refname:short)' 2>/dev/null | while IFS= read -r b; do
98
261
  [ -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
262
+ echo " $b -> $(_kit_gc_classify "$b" "$pr_index" "$repo")"
111
263
  done
112
264
 
113
265
  echo "# stashes"
@@ -129,9 +281,10 @@ EOF
129
281
  [ "$found" -eq 1 ] || echo " (none)"
130
282
  }
131
283
 
132
- # kit_gc_has_prunable — rc 0 if at least one branch is SAFE to delete (a merged, unprotected branch).
284
+ # kit_gc_has_prunable — rc 0 if at least one branch is deletable: SAFE (its own PR merged) or LANDED
285
+ # (no PR of its own, work already on base).
133
286
  kit_gc_has_prunable() {
134
- kit_gc_analyze 2>/dev/null | grep -q '> SAFE '
287
+ kit_gc_analyze 2>/dev/null | grep -qE '> (SAFE|LANDED) '
135
288
  }
136
289
 
137
290
  # _kit_gc_zombies — echo "<name>\t<branch>\t<staged:yes|no>" for each ZOMBIE worktree: its working
@@ -222,38 +375,68 @@ _kit_gc_remote_ahead() {
222
375
  [ "$(git rev-list --count "$b..origin/$b" 2>/dev/null || echo 1)" != "0" ]
223
376
  }
224
377
 
225
- # kit_gc_prune [--yes] - remove worktrees + local branches whose PR is MERGED (the SAFE rows).
378
+ # _kit_gc_only_match <regex> <branch> — rc 0 when <regex> is empty or <branch> matches it (ERE).
379
+ _kit_gc_only_match() {
380
+ [ -n "$1" ] || return 0
381
+ printf '%s\n' "$2" | grep -qE -- "$1"
382
+ }
383
+
384
+ # _kit_gc_prune_why <verdict> — echo the reason a prune row prints, or nothing when the verdict is
385
+ # not a deletable one. SAFE (own PR merged) and LANDED (no PR, work on base — #388) are the only two.
386
+ _kit_gc_prune_why() {
387
+ case "$1" in
388
+ SAFE*) echo "PR MERGED" ;;
389
+ LANDED*) echo "$1" ;;
390
+ *) : ;;
391
+ esac
392
+ }
393
+
394
+ # kit_gc_prune [--yes] [--only <regex>] - remove worktrees + local branches whose PR is MERGED (the
395
+ # SAFE rows) or whose work is already on base with no PR of their own (the LANDED rows, #388).
226
396
  # DRY-RUN by default (lists what it WOULD remove); --yes performs the deletions. Never touches a
227
397
  # PROTECTED/ACTIVE/ORPHAN branch, and never a DIRTY worktree (recover-before-prune): a worktree with
228
398
  # staged/unstaged/untracked changes is skipped with a warning, not destroyed. The remote branch is
229
399
  # already deleted at merge time (gh pr merge --delete-branch); this cleans up the local side.
400
+ # `--only <regex>` restricts both loops to branches matching the ERE (effort_close passes
401
+ # `^sub/<N>[a-z]-` to collect an effort's subs; a regex, not a glob — zsh does not expand a glob
402
+ # held in a variable inside `case`, bash does, and this file is sourced by both); the zombie pass is skipped under --only, since it
403
+ # is not scoped to a branch — that stays with a full `gc --prune` / `cleanup`.
230
404
  kit_gc_prune() {
231
405
  _kit_gc_require_deps || return 1
406
+ _kit_gc_cache_reset
232
407
  # `wtpath`, not `path`: under zsh `path` is tied to PATH (special array), so assigning to a bare
233
408
  # `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
409
+ local repo="$KIT_GC_REPO" yes=0 wtpath ref b pr_index verdict why only=""
410
+ while [ $# -gt 0 ]; do
411
+ case "$1" in
412
+ --yes|-y) yes=1 ;;
413
+ --only) only="${2:-}"; shift ;;
414
+ --only=*) only="${1#--only=}" ;;
415
+ esac
416
+ shift
417
+ done
236
418
 
237
419
  # All PRs in ONE call, indexed by head branch (#124) — shared by both loops below instead of a
238
420
  # per-branch `gh pr list --head` (the N+1 that scaled badly with many stale branches).
239
421
  pr_index="$(_kit_gc_pr_index "$repo")"
240
422
 
241
- # Worktrees first - a branch's worktree must be removed before the branch can be deleted.
423
+ # Worktrees first - a branch's worktree must be removed before the branch can be deleted. The
424
+ # verdict comes from the ONE classifier (_kit_gc_classify), the same function analyze prints.
242
425
  git worktree list --porcelain 2>/dev/null \
243
426
  | awk '/^worktree /{w=$2} /^branch /{print w" "$2}' \
244
427
  | while read -r wtpath ref; do
245
428
  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
429
+ [ -n "$b" ] || continue
430
+ _kit_gc_only_match "$only" "$b" || continue
431
+ verdict="$(_kit_gc_classify "$b" "$pr_index" "$repo")"
432
+ why="$(_kit_gc_prune_why "$verdict")"; [ -n "$why" ] || continue
250
433
  if [ -n "$(git -C "$wtpath" status --porcelain 2>/dev/null)" ]; then
251
434
  echo " SKIP dirty worktree $wtpath [$b] - commit/recover before pruning" >&2; continue
252
435
  fi
253
436
  if [ "$yes" -eq 1 ]; then
254
- git worktree remove --force "$wtpath" 2>/dev/null && echo " removed worktree $wtpath [$b]"
437
+ git worktree remove --force "$wtpath" 2>/dev/null && echo " removed worktree $wtpath [$b] ($why)"
255
438
  else
256
- echo " would remove worktree $wtpath [$b] (PR MERGED)"
439
+ echo " would remove worktree $wtpath [$b] ($why)"
257
440
  fi
258
441
  done
259
442
 
@@ -262,26 +445,27 @@ kit_gc_prune() {
262
445
  # prune` deletes that index, orphaning the blobs for a later `git gc` to reap — real data loss in a
263
446
  # documented incident. Recover any staged delta to its branch FIRST, and gate the prune sweep
264
447
  # 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; }
448
+ if [ -z "$only" ]; then
449
+ kit_gc_recover_zombies "$yes"
450
+ [ "$yes" -eq 1 ] && { git worktree prune 2>/dev/null || true; }
451
+ fi
267
452
 
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).
453
+ # Then local branches whose PR merged, or whose work landed (worktree now gone). while-read (NOT
454
+ # `for b in $(...)`) so branch iteration survives a NUL-polluted IFS (#124).
270
455
  git branch --format='%(refname:short)' 2>/dev/null | while IFS= read -r b; do
271
456
  [ -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.
457
+ _kit_gc_only_match "$only" "$b" || continue
458
+ verdict="$(_kit_gc_classify "$b" "$pr_index" "$repo")"
459
+ why="$(_kit_gc_prune_why "$verdict")"; [ -n "$why" ] || continue
460
+ # A merged PR (or landed content) is not on its own sufficient: the local ref may carry commits
461
+ # that were never pushed, and `git branch -D` would be their last rites.
278
462
  if _kit_gc_has_unpushed "$b"; then
279
463
  echo " SKIP $b — ahead of origin/$b (unpushed commits); recover before pruning" >&2; continue
280
464
  fi
281
465
  if [ "$yes" -eq 1 ]; then
282
- git branch -D "$b" >/dev/null 2>&1 && echo " deleted local branch $b (PR MERGED)"
466
+ git branch -D "$b" >/dev/null 2>&1 && echo " deleted local branch $b ($why)"
283
467
  else
284
- echo " would delete local branch $b (PR MERGED)"
468
+ echo " would delete local branch $b ($why)"
285
469
  fi
286
470
  done
287
471
  [ "$yes" -eq 1 ] && echo "gc prune: done" || echo "gc prune: DRY RUN - pass --yes to delete"
@@ -306,11 +490,13 @@ _kit_gc_names() {
306
490
  # omits is a deletion the user did not get to veto, so the plan must name EVERYTHING --yes can touch:
307
491
  # worktrees as well as branches.
308
492
  #
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.
493
+ # What --yes deletes: SAFE and LANDED worktrees and local branches (via kit_gc_prune, which also runs
494
+ # the recover-before-prune zombie pass) — SAFE = its own PR merged, LANDED = no PR of its own but its
495
+ # work is already on base (#388: effort subs, agent isolation worktrees) — and the REMOTE ref of such
496
+ # a branch when it is exactly level with it. What it NEVER deletes: an ORPHAN branch (local commits
497
+ # not on the base remote), a PROTECTED branch (its issue is still open), or a stash. Those three are
498
+ # irreversible or hold the only copy of work, so they are surfaced BY NAME for a human and left
499
+ # exactly where they are.
314
500
  #
315
501
  # NOTE: the underlying kit_gc_analyze refreshes remote-tracking refs (`git fetch --prune`) so the
316
502
  # classification is not made against a stale view of the remote. That is the only write a plan-only
@@ -319,22 +505,27 @@ kit_gc_cleanup() {
319
505
  _kit_gc_require_deps || return 1
320
506
  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
507
  local zname zbranch zstaged
322
- local safe_list orphan_list prot_list stash_list wt_list zomb_list level_ok n_zomb
508
+ local safe_list landed_list orphan_list prot_list stash_list wt_list zomb_list level_ok n_zomb n_landed
323
509
  for a in "$@"; do case "$a" in --yes|-y) yes=1 ;; esac; done
324
510
 
325
511
  out="$(kit_gc_analyze)" || return 1
326
512
  safe_list="$(_kit_gc_names "$out" branches ' -> SAFE ')"
513
+ # LANDED rows keep their reason (`name (reason)`) so the plan says WHY each one may go (#388).
514
+ landed_list="$(printf '%s\n' "$out" | awk '
515
+ /^# / { in_s = ($0 == "# branches"); next }
516
+ in_s && / -> LANDED / { sub(/^ /, ""); sub(/ -> LANDED /, " "); print }')"
327
517
  orphan_list="$(_kit_gc_names "$out" branches ' -> ORPHAN')"
328
518
  prot_list="$(_kit_gc_names "$out" branches ' -> PROTECTED:')"
329
519
  stash_list="$(printf '%s\n' "$out" | awk '/^# /{in_s=($0=="# stashes"); next} in_s && NF' | sed 's/^ //')"
330
520
 
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.
521
+ # Worktrees kit_gc_prune would remove: the ones whose branch is itself SAFE or LANDED (the same
522
+ # verdicts prune acts on). Derived from those lists so the two cannot diverge.
333
523
  wt_list="$(git worktree list --porcelain 2>/dev/null \
334
524
  | awk '/^worktree /{w=$2} /^branch /{print w" "$2}' \
335
525
  | while read -r wtpath ref; do
336
526
  b="${ref#refs/heads/}"
337
- printf '%s\n' "$safe_list" | grep -qxF "$b" || continue
527
+ printf '%s\n' "$safe_list" | grep -qxF "$b" \
528
+ || printf '%s\n' "$landed_list" | sed 's/ (.*//' | grep -qxF "$b" || continue
338
529
  if [ -n "$(git -C "$wtpath" status --porcelain 2>/dev/null)" ]; then
339
530
  echo "$wtpath [$b] (DIRTY — will be skipped, not destroyed)"
340
531
  else
@@ -359,6 +550,7 @@ ZEOF
359
550
  )"
360
551
 
361
552
  n_safe="$(printf '%s' "$safe_list" | grep -c . || true)"
553
+ n_landed="$(printf '%s' "$landed_list" | grep -c . || true)"
362
554
  n_orphan="$(printf '%s' "$orphan_list" | grep -c . || true)"
363
555
  n_prot="$(printf '%s' "$prot_list" | grep -c . || true)"
364
556
  n_stash="$(printf '%s' "$stash_list" | grep -c . || true)"
@@ -368,10 +560,12 @@ ZEOF
368
560
  echo "# cleanup plan"
369
561
  printf ' SAFE branches %3s merged PR, issue closed/absent\n' "$n_safe"
370
562
  printf '%s\n' "$safe_list" | grep . | sed 's/^/ /' || true
371
- printf ' SAFE worktrees %3s removed with their branch\n' "$n_wt"
563
+ printf ' SAFE worktrees %3s removed with their branch (SAFE or LANDED)\n' "$n_wt"
372
564
  printf '%s\n' "$wt_list" | grep . | sed 's/^/ /' || true
373
565
  printf ' ZOMBIE worktrees %3s staged work recovered to its branch, then metadata pruned\n' "$n_zomb"
374
566
  printf '%s\n' "$zomb_list" | grep . | sed 's/^/ /' || true
567
+ printf ' LANDED branches %3s no PR of its own, work already on the base branch\n' "$n_landed"
568
+ printf '%s\n' "$landed_list" | grep . | sed 's/^/ /' || true
375
569
  printf ' ORPHAN kept %3s commits not on the base remote — never auto-deleted\n' "$n_orphan"
376
570
  printf '%s\n' "$orphan_list" | grep . | sed 's/^/ /' || true
377
571
  printf ' PROTECTED kept %3s issue still OPEN\n' "$n_prot"
@@ -380,14 +574,15 @@ ZEOF
380
574
  printf '%s\n' "$stash_list" | grep . | sed 's/^/ /' || true
381
575
 
382
576
  if [ "$yes" -ne 1 ]; then
383
- echo "cleanup: PLAN ONLY — nothing deleted. Pass --yes to delete the SAFE rows and prune the ZOMBIE rows."
577
+ echo "cleanup: PLAN ONLY — nothing deleted. Pass --yes to delete the SAFE and LANDED rows and prune the ZOMBIE rows."
384
578
  return 0
385
579
  fi
386
- [ "$n_safe" -eq 0 ] && [ "$n_wt" -eq 0 ] && [ "$n_zomb" -eq 0 ] && { echo "cleanup: nothing SAFE to delete."; return 0; }
580
+ [ "$n_safe" -eq 0 ] && [ "$n_landed" -eq 0 ] && [ "$n_wt" -eq 0 ] && [ "$n_zomb" -eq 0 ] \
581
+ && { echo "cleanup: nothing SAFE or LANDED to delete."; return 0; }
387
582
 
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.
583
+ # Which SAFE / LANDED branches may have their REMOTE deleted — decided BEFORE the local prune,
584
+ # because once the local ref is gone there is nothing left to compare. Requires exact equality in
585
+ # BOTH directions: local ⊆ remote alone would let a remote-ahead branch lose remote-only history.
391
586
  level_ok=""
392
587
  while IFS= read -r b; do
393
588
  [ -n "$b" ] || continue
@@ -398,6 +593,7 @@ ZEOF
398
593
  "
399
594
  done <<EOF
400
595
  $safe_list
596
+ $(printf '%s\n' "$landed_list" | sed 's/ (.*//')
401
597
  EOF
402
598
 
403
599
  kit_gc_prune --yes || rc=1
@@ -417,6 +613,6 @@ EOF
417
613
  $level_ok
418
614
  EOF
419
615
  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."
616
+ 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
617
  return "$rc"
422
618
  }
@@ -55,10 +55,25 @@ if [ -n "${KTO_TEST_INNER:-}" ]; then
55
55
  eq "conventional summary wins over derivation" \
56
56
  "$(kto_pr_title '[Effort 182] 2 · Publish to npm' 'ci: publish via OIDC' 'kind:task')" \
57
57
  "ci: publish via OIDC"
58
+ eq "titleStyle issue-title → the issue title verbatim" \
59
+ "$(KIT_PR_TITLE_STYLE=issue-title kto_pr_title '[Effort 182] 1 · Enforce it' 'ci: x' 'kind:task')" \
60
+ "[Effort 182] 1 · Enforce it"
61
+ eq "titleStyle conventional (explicit) → derive" \
62
+ "$(KIT_PR_TITLE_STYLE=conventional kto_pr_title 'mail hook goes silent' '' 'bug')" \
63
+ "fix: mail hook goes silent"
58
64
  eq "non-conventional summary ignored → derive" \
59
65
  "$(kto_pr_title 'Benchmark harness' 'ported a harness' 'kind:feat')" \
60
66
  "feat: Benchmark harness"
61
67
 
68
+ # pr.orchestratorSection: off by default, a 2-line section first when on
69
+ ob="$(kto_compose_pr_body 42 'S.' 'tech-lead' 'Fix the thing')"
70
+ case "$ob" in *"For the orchestrator"*) echo "FAIL($KTO_TEST_INNER): orchestrator section present by default"; fail=1 ;; *) ;; esac
71
+ ob="$(KIT_PR_ORCHESTRATOR_SECTION=true kto_compose_pr_body 42 'S.' 'tech-lead' 'Fix the thing')"
72
+ eq "orchestrator section opens the body" "$(printf '%s\n' "$ob" | sed -n 1p)" "## For the orchestrator"
73
+ eq "orchestrator line 1 = id + title" "$(printf '%s\n' "$ob" | sed -n 3p)" "#42 · Fix the thing"
74
+ eq "orchestrator line 2 = what to review" "$(printf '%s\n' "$ob" | sed -n 4p)" "Review: the summary below and the diff."
75
+ case "$ob" in *"Closes #42"*) ;; *) echo "FAIL($KTO_TEST_INNER): Closes line lost with the section on"; fail=1 ;; esac
76
+
62
77
  # kto_compose_pr_body must carry Closes #N and the Summary
63
78
  body="$(kto_compose_pr_body 42 'Did the thing.' 'tech-lead')"
64
79
  case "$body" in *"Closes #42"*) ;; *) echo "FAIL($KTO_TEST_INNER): pr body missing Closes #42"; fail=1 ;; esac