@jeiemgi/cckit 0.9.0 → 0.10.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 (305) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +169 -21
  3. package/README.md +491 -35
  4. package/SECURITY.md +5 -0
  5. package/bin/cckit +182 -17
  6. package/cckit.config.json +1 -1
  7. package/commands/kit-doctor.md +13 -5
  8. package/commands/kit-export-project.md +0 -2
  9. package/commands/kit-export-training.md +6 -9
  10. package/commands/kit-init.md +43 -13
  11. package/commands/kit-routines.md +1 -1
  12. package/commands/kit-update.md +11 -6
  13. package/databases/supabase/guide.html +106 -0
  14. package/databases/supabase/nextjs/lib/supabase/client.ts +9 -0
  15. package/databases/supabase/nextjs/lib/supabase/server.ts +29 -0
  16. package/databases/supabase.json +70 -0
  17. package/integrations/emil-design-eng.json +41 -0
  18. package/integrations/graft.json +81 -0
  19. package/integrations/impeccable.json +42 -0
  20. package/integrations/integration.schema.json +215 -0
  21. package/integrations/lavish.json +43 -0
  22. package/integrations/mempalace.json +28 -0
  23. package/integrations/mlx-lm.json +32 -0
  24. package/integrations/web-design-guidelines.json +42 -0
  25. package/kit.config.schema.json +183 -5
  26. package/package.json +4 -1
  27. package/profiles/automation.json +7 -1
  28. package/profiles/content.json +10 -2
  29. package/profiles/marketing.json +57 -0
  30. package/profiles/minimal.json +7 -1
  31. package/profiles/research.json +7 -1
  32. package/profiles/software.json +7 -1
  33. package/recipes/nextjs-shadcn.json +49 -0
  34. package/scripts/agent-skills-external.txt +45 -0
  35. package/scripts/agent-skills-lint-test.sh +131 -0
  36. package/scripts/agent-skills-lint.sh +115 -0
  37. package/scripts/agent-template-lint-test.sh +42 -0
  38. package/scripts/agent-template-lint.sh +58 -0
  39. package/scripts/annotate-setup.sh +1 -1
  40. package/scripts/autopilot-run-test.sh +338 -0
  41. package/scripts/autopilot-run.sh +448 -0
  42. package/scripts/autopilot-test.sh +8 -0
  43. package/scripts/autopilot.sh +10 -1
  44. package/scripts/benchmark/benchmark-test.sh +85 -0
  45. package/scripts/benchmark/checkpoint-test.sh +314 -0
  46. package/scripts/benchmark/checkpoint.sh +408 -0
  47. package/scripts/benchmark/lib/model.sh +31 -6
  48. package/scripts/benchmark/report.sh +17 -2
  49. package/scripts/benchmark/run-quality.sh +36 -10
  50. package/scripts/benchmark/verb-timings-test.sh +124 -0
  51. package/scripts/benchmark/verb-timings.sh +149 -0
  52. package/scripts/check.sh +29 -19
  53. package/scripts/content-skills-test.sh +144 -0
  54. package/scripts/copilot-loop-test.sh +25 -0
  55. package/scripts/database-supabase-test.sh +121 -0
  56. package/scripts/docs-marketing-words-test.sh +73 -0
  57. package/scripts/docs-record-tables-test.sh +72 -0
  58. package/scripts/hook-adapter-test.sh +193 -0
  59. package/scripts/init-content-test.sh +225 -0
  60. package/scripts/init-guided-test.sh +474 -0
  61. package/scripts/init-recipes-test.sh +104 -0
  62. package/scripts/init-upgrade-test.sh +341 -2
  63. package/scripts/init.sh +746 -95
  64. package/scripts/instruction-budget-baseline.txt +8 -0
  65. package/scripts/instruction-budget-lint-test.sh +166 -0
  66. package/scripts/instruction-budget-lint.sh +219 -0
  67. package/scripts/integrations-check-test.sh +200 -0
  68. package/scripts/integrations-check.sh +157 -0
  69. package/scripts/kit-add-test.sh +214 -0
  70. package/scripts/kit-add.sh +305 -5
  71. package/scripts/kit-adopt-test.sh +16 -0
  72. package/scripts/kit-adopt.sh +3 -2
  73. package/scripts/kit-digest.sh +2 -2
  74. package/scripts/kit-doctor-test.sh +293 -0
  75. package/scripts/kit-doctor.sh +272 -140
  76. package/scripts/kit-export-project-test.sh +3 -3
  77. package/scripts/kit-remove-test.sh +19 -0
  78. package/scripts/kit-routines.sh +2 -2
  79. package/scripts/kit-self-update.sh +1 -1
  80. package/scripts/kit-version-check-test.sh +4 -0
  81. package/scripts/kit-version-check.sh +4 -2
  82. package/scripts/knowledge-lint.sh +4 -4
  83. package/scripts/leak-fence-test.sh +89 -0
  84. package/scripts/lib/adapter-select-test.sh +136 -0
  85. package/scripts/lib/adapter-select.sh +158 -0
  86. package/scripts/lib/agent-def-test.sh +69 -0
  87. package/scripts/lib/agent-def.sh +106 -0
  88. package/scripts/lib/agent-launch-test.sh +212 -0
  89. package/scripts/lib/agent-launch.sh +335 -0
  90. package/scripts/lib/board-view-test.sh +11 -0
  91. package/scripts/lib/board-view.sh +42 -26
  92. package/scripts/lib/branch-owner-test.sh +26 -0
  93. package/scripts/lib/branch-owner.sh +16 -9
  94. package/scripts/lib/captain-test.sh +105 -0
  95. package/scripts/lib/captain.sh +87 -8
  96. package/scripts/lib/codex-hooks-test.sh +100 -0
  97. package/scripts/lib/codex-hooks.sh +69 -0
  98. package/scripts/lib/commitlint.sh +1 -1
  99. package/scripts/lib/content-kinds-gc-test.sh +88 -0
  100. package/scripts/lib/copilot-test.sh +70 -4
  101. package/scripts/lib/copilot.sh +66 -14
  102. package/scripts/lib/effort-dispatch-test.sh +3 -3
  103. package/scripts/lib/effort-metrics-test.sh +178 -1
  104. package/scripts/lib/effort-metrics.sh +175 -35
  105. package/scripts/lib/effort-ops-test.sh +205 -7
  106. package/scripts/lib/effort-ops.sh +198 -43
  107. package/scripts/lib/effort-parity-test.sh +49 -13
  108. package/scripts/lib/effort-plan-test.sh +337 -0
  109. package/scripts/lib/effort-plan.sh +200 -23
  110. package/scripts/lib/effort.sh +56 -11
  111. package/scripts/lib/errors-header-test.sh +1 -1
  112. package/scripts/lib/gh-project-test.sh +35 -1
  113. package/scripts/lib/gh-project.sh +23 -16
  114. package/scripts/lib/gh-rest-test.sh +32 -3
  115. package/scripts/lib/gh-rest.sh +17 -2
  116. package/scripts/lib/hook-refresh.sh +1 -1
  117. package/scripts/lib/init-github.sh +412 -0
  118. package/scripts/lib/init-guide.sh +630 -0
  119. package/scripts/lib/integration-install-test.sh +343 -0
  120. package/scripts/lib/integration-install.sh +257 -0
  121. package/scripts/lib/integrations-test.sh +366 -0
  122. package/scripts/lib/integrations.sh +244 -0
  123. package/scripts/lib/kinds-test.sh +125 -0
  124. package/scripts/lib/kinds.sh +66 -0
  125. package/scripts/lib/kit-brief-test.sh +22 -3
  126. package/scripts/lib/kit-brief.sh +45 -5
  127. package/scripts/lib/kit-config-resolve.sh +2 -2
  128. package/scripts/lib/kit-config-test.sh +64 -1
  129. package/scripts/lib/kit-config.sh +37 -1
  130. package/scripts/lib/kit-events.sh +7 -0
  131. package/scripts/lib/kit-gc-test.sh +173 -1
  132. package/scripts/lib/kit-gc.sh +226 -37
  133. package/scripts/lib/kit-hotfix.sh +216 -0
  134. package/scripts/lib/kit-interview-test.sh +93 -0
  135. package/scripts/lib/kit-interview.sh +117 -8
  136. package/scripts/lib/kit-lib-test.sh +1 -1
  137. package/scripts/lib/kit-lib.sh +3 -3
  138. package/scripts/lib/kit-local.sh +2 -2
  139. package/scripts/lib/kit-manifest.sh +33 -1
  140. package/scripts/lib/kit-profile-test.sh +3 -3
  141. package/scripts/lib/kit-status-test.sh +112 -4
  142. package/scripts/lib/kit-status.sh +246 -16
  143. package/scripts/lib/kit-task-ops-test.sh +37 -0
  144. package/scripts/lib/kit-task-ops.sh +175 -31
  145. package/scripts/lib/kto-hotfix-test.sh +232 -0
  146. package/scripts/lib/kto-task-close-test.sh +1 -1
  147. package/scripts/lib/kto-task-new-test.sh +95 -0
  148. package/scripts/lib/kto-task-pr-merge-test.sh +37 -3
  149. package/scripts/lib/kto-task-pr-test.sh +70 -0
  150. package/scripts/lib/leak-fence.sh +89 -0
  151. package/scripts/lib/light-task-run-test.sh +255 -0
  152. package/scripts/lib/light-task-test.sh +309 -0
  153. package/scripts/lib/light-task.sh +706 -0
  154. package/scripts/lib/next-test.sh +99 -0
  155. package/scripts/lib/next.sh +9 -3
  156. package/scripts/lib/orchestration-runtime-test.sh +72 -0
  157. package/scripts/lib/orchestration-runtime.sh +57 -5
  158. package/scripts/lib/plan-machine-test.sh +15 -2
  159. package/scripts/lib/plan-machine.sh +20 -7
  160. package/scripts/lib/plan-next-test.sh +1 -1
  161. package/scripts/lib/plan-next.sh +4 -4
  162. package/scripts/lib/pr-evidence-test.sh +3 -3
  163. package/scripts/lib/pr-evidence.sh +1 -1
  164. package/scripts/lib/qa-lane-test.sh +626 -0
  165. package/scripts/lib/qa-lane.sh +1357 -0
  166. package/scripts/lib/qa-lanes-test.sh +175 -0
  167. package/scripts/lib/react-detect.sh +1 -1
  168. package/scripts/lib/render-test.sh +1 -1
  169. package/scripts/lib/review-page-test.sh +318 -0
  170. package/scripts/lib/review-page.sh +369 -0
  171. package/scripts/lib/review-stage-test.sh +62 -11
  172. package/scripts/lib/review-stage.sh +42 -60
  173. package/scripts/lib/role-identity-test.sh +65 -0
  174. package/scripts/lib/role-identity.sh +25 -2
  175. package/scripts/lib/secret-guard.sh +3 -3
  176. package/scripts/lib/session-registry-test.sh +111 -0
  177. package/scripts/lib/session-registry.sh +233 -0
  178. package/scripts/lib/stage-receipt-test.sh +14 -1
  179. package/scripts/lib/stage-receipt.sh +13 -8
  180. package/scripts/lib/task-source-file.sh +309 -0
  181. package/scripts/lib/task-source.sh +130 -0
  182. package/scripts/lib/upgrade-report-test.sh +120 -0
  183. package/scripts/lib/upgrade-report.sh +138 -0
  184. package/scripts/lib/version-bump.sh +3 -3
  185. package/scripts/lib/wave-result-test.sh +80 -0
  186. package/scripts/lib/wave-result.sh +127 -0
  187. package/scripts/lib/worker-brief-test.sh +5 -0
  188. package/scripts/lib/worker-brief.sh +2 -2
  189. package/scripts/lib/worktree-issue-test.sh +30 -1
  190. package/scripts/lib/worktree-issue.sh +82 -12
  191. package/scripts/lib/worktree-ports-test.sh +57 -4
  192. package/scripts/lib/worktree-start-test.sh +197 -0
  193. package/scripts/lib/worktree-start.sh +153 -31
  194. package/scripts/lib/zsh-safety-test.sh +19 -3
  195. package/scripts/marketing-walkthrough-test.sh +152 -0
  196. package/scripts/night-grant-test.sh +37 -0
  197. package/scripts/night-grant.sh +448 -0
  198. package/scripts/night-run-test.sh +8 -0
  199. package/scripts/night-run.mjs +275 -0
  200. package/scripts/night-run.test.mjs +324 -0
  201. package/scripts/orchestrate-test.sh +190 -0
  202. package/scripts/orchestrate.sh +79 -8
  203. package/scripts/portable-test.sh +11 -2
  204. package/scripts/publish.sh +28 -3
  205. package/scripts/review-lint-test.sh +7 -7
  206. package/scripts/review-lint.sh +2 -2
  207. package/scripts/review-rules.conf +8 -0
  208. package/scripts/rule-recipes-test.sh +96 -0
  209. package/scripts/self-install-test.sh +39 -9
  210. package/scripts/setup-labels.sh +18 -1
  211. package/scripts/setup-milestones.sh +1 -1
  212. package/scripts/showcase.sh +1 -1
  213. package/scripts/skill-frontmatter-lint-test.sh +4 -4
  214. package/scripts/skill-frontmatter-lint.sh +1 -1
  215. package/scripts/status-test.sh +113 -2
  216. package/scripts/status.sh +16 -3
  217. package/scripts/task-sync-test.sh +154 -0
  218. package/scripts/task-sync.sh +243 -32
  219. package/scripts/test-runner-test.sh +158 -0
  220. package/scripts/test.sh +131 -23
  221. package/scripts/update-path-test.sh +279 -0
  222. package/scripts/ux-ds-audit.sh +1 -1
  223. package/scripts/ux-graph-scan.sh +6 -6
  224. package/scripts/wave-worker.sh +54 -0
  225. package/skills/kit-add/SKILL.md +77 -0
  226. package/skills/kit-autopilot/SKILL.md +123 -12
  227. package/skills/kit-cleanup/SKILL.md +5 -2
  228. package/skills/kit-contribute/SKILL.md +4 -4
  229. package/skills/kit-docs-writer/SKILL.md +16 -6
  230. package/skills/kit-effort-close/SKILL.md +17 -8
  231. package/skills/kit-effort-new/SKILL.md +15 -9
  232. package/skills/kit-effort-pr/SKILL.md +15 -7
  233. package/skills/kit-effort-start/SKILL.md +5 -2
  234. package/skills/kit-gc/SKILL.md +5 -2
  235. package/skills/kit-next/SKILL.md +5 -1
  236. package/skills/kit-onboard/SKILL.md +3 -3
  237. package/skills/kit-page/SKILL.md +93 -0
  238. package/skills/kit-plan-next/SKILL.md +8 -4
  239. package/skills/kit-security-sweep/SKILL.md +1 -3
  240. package/skills/kit-ship/SKILL.md +2 -3
  241. package/skills/kit-status/SKILL.md +35 -4
  242. package/skills/kit-task-new/SKILL.md +3 -3
  243. package/skills/kit-task-pr/SKILL.md +7 -2
  244. package/skills/kit-task-pr-merge/SKILL.md +7 -2
  245. package/skills/kit-task-start/SKILL.md +7 -7
  246. package/skills/kit-task-sync/SKILL.md +19 -3
  247. package/skills/kit-ux-audit/SKILL.md +3 -2
  248. package/templates/agents/auto-dev.md +5 -4
  249. package/templates/agents/backend.md +1 -1
  250. package/templates/agents/designer.md +2 -0
  251. package/templates/agents/devops.md +1 -1
  252. package/templates/agents/editor.md +9 -1
  253. package/templates/agents/frontend.md +1 -1
  254. package/templates/agents/local-delegate.md +1 -1
  255. package/templates/agents/pm.md +5 -5
  256. package/templates/agents/tech-lead.md +1 -1
  257. package/templates/benchmarks/SCHEMA.md +9 -3
  258. package/templates/codex/hooks.json.tmpl +89 -0
  259. package/templates/hooks/guard-base-branch-commit.sh.tmpl +13 -7
  260. package/templates/hooks/guard-raw-git-ops.sh.tmpl +12 -12
  261. package/templates/hooks/mempal_followup.sh.tmpl +2 -2
  262. package/templates/hooks/mempal_session_start.sh.tmpl +1 -1
  263. package/templates/hooks/prepush_gate.sh.tmpl +4 -2
  264. package/templates/hooks/repo-hygiene.sh.tmpl +3 -2
  265. package/templates/instructions.md.tmpl +174 -0
  266. package/templates/kit.config.json.tmpl +3 -3
  267. package/templates/knowledge-INDEX.md.tmpl +1 -1
  268. package/templates/lib/kit-sigil.sh.tmpl +1 -1
  269. package/templates/rules/authoring.md +106 -0
  270. package/templates/rules/branch-naming.md +40 -4
  271. package/templates/rules/code-retrieval.md +63 -0
  272. package/templates/rules/delegation-brief.md +0 -3
  273. package/templates/rules/design-routing.md +3 -3
  274. package/templates/rules/effort-model.md +14 -10
  275. package/templates/rules/gate-and-review.md +94 -0
  276. package/templates/rules/issue-lifecycle.md +88 -0
  277. package/templates/rules/knowledge-base.md +1 -1
  278. package/templates/rules/mempalace.md +1 -1
  279. package/templates/rules/merge-safety.md +108 -0
  280. package/templates/rules/naming-and-ids.md +23 -17
  281. package/templates/rules/plain-language.md +59 -0
  282. package/templates/rules/plan-next.md +9 -11
  283. package/templates/rules/react-annotate.md +6 -0
  284. package/templates/rules/review-pages.md +49 -0
  285. package/templates/rules/risk-tiered-review.md +13 -4
  286. package/templates/rules/shared-checkout.md +106 -0
  287. package/templates/rules/skill-gaps.md +2 -7
  288. package/templates/rules/task-management.md +13 -1
  289. package/templates/skills/brand-voice/SKILL.md +91 -0
  290. package/templates/skills/brand-voice/references/voice-template.md +61 -0
  291. package/templates/skills/campaign/SKILL.md +123 -0
  292. package/templates/skills/campaign/references/plan-template.md +55 -0
  293. package/templates/skills/concrete/SKILL.md +2 -2
  294. package/templates/skills/copywriting/SKILL.md +8 -1
  295. package/templates/skills/feature-build-refine/SKILL.md +2 -4
  296. package/templates/skills/morning-briefing/SKILL.md +2 -2
  297. package/templates/skills/page-design/SKILL.md +145 -0
  298. package/templates/skills/page-design/add.json +5 -0
  299. package/templates/skills/page-design/references/design-file-template.md +40 -0
  300. package/templates/skills/page-design/references/generic-page-patterns.md +43 -0
  301. package/templates/skills/page-design/references/minimum-rules.md +42 -0
  302. package/templates/skills/speckit/SKILL.md +4 -5
  303. package/templates/skills/supabase-patterns/SKILL.md +1 -1
  304. package/commands/kit-add.md +0 -42
  305. package/templates/CLAUDE.md.tmpl +0 -106
@@ -17,7 +17,10 @@
17
17
  # kit_gc_cleanup [--yes] the guided sweep: plan every bucket, then act on SAFE + LANDED + ZOMBIE.
18
18
  #
19
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.
20
+ # PR of its own, work already on base — #388: effort subs, agent isolation worktrees) · ORPHAN ·
21
+ # UNKNOWN (a gh read FAILED, so the verdict could not be determined — #330; never deletable).
22
+ # A DETACHED worktree has no branch to classify, so it gets its own row (#390): LANDED when it is
23
+ # clean and its HEAD is already on the base branch or `main`, ORPHAN otherwise.
21
24
  #
22
25
  # Requires: git; gh (degrades to "unknown" issue/PR state without it); scripts/lib/worktree-issue.sh.
23
26
  # Portable: bash 3.2+ AND zsh.
@@ -34,7 +37,7 @@ command -v cckit_gh_rest >/dev/null 2>&1 || cckit_gh_rest() { return 1; }
34
37
 
35
38
  KIT_GC_REPO="${KIT_GC_REPO:-${KIT_REPO:-}}"
36
39
 
37
- _kit_gc_root() { git worktree list --porcelain 2>/dev/null | awk '/^worktree /{print $2; exit}'; }
40
+ _kit_gc_root() { git worktree list --porcelain 2>/dev/null | awk '/^worktree / && !hit {print $2; hit=1}'; }
38
41
 
39
42
  # This file's own directory, resolved ONCE at load time — the only point where both shells agree.
40
43
  # `BASH_SOURCE` is bash-only; inside a zsh *function* it is unset and `$0` is the function name, so
@@ -74,20 +77,40 @@ _kit_gc_require_deps() {
74
77
  # _kit_gc_pr_index [repo] — echo one "<headRefName>\tPR#<num> <STATE>" line per PR in ONE gh call, so
75
78
  # branch classification is a local lookup instead of a per-branch `gh pr list --head` — an N+1 that
76
79
  # scaled badly exactly when gc matters most (many stale branches). #124.
80
+ #
81
+ # A FAILED read is not an empty list (#330): an empty index means "this repo has no PRs", and every
82
+ # branch then reads as having none. So a failure warns on stderr and emits ONE sentinel line,
83
+ # `?<TAB>UNKNOWN` — `?` cannot be a branch name (git refuses it), so no lookup ever matches it — and
84
+ # _kit_gc_classify answers UNKNOWN instead of ORPHAN/LANDED for a branch it finds no PR for. rc 0
85
+ # always: kit-status.sh calls this in a bare assignment under `set -e`.
77
86
  _kit_gc_pr_index() {
78
- local repo="${1:-$KIT_GC_REPO}"
87
+ local repo="${1:-$KIT_GC_REPO}" out
79
88
  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
89
+ out="$(gh api "repos/$repo/pulls" -X GET -f state=all -f per_page=100 \
90
+ --jq '.[] | "\(.head.ref)\tPR#\(.number) \(if .merged_at then "MERGED" else (.state | ascii_upcase) end)"' 2>/dev/null)" \
91
+ || { _kit_gc_pr_index_failed; return 0; }
92
+ else
93
+ out="$(gh pr list --repo "$repo" --state all --limit 200 --json number,state,headRefName \
94
+ --jq '.[] | "\(.headRefName)\tPR#\(.number) \(.state)"' 2>/dev/null)" \
95
+ || { _kit_gc_pr_index_failed; return 0; }
83
96
  fi
84
- gh pr list --repo "$repo" --state all --limit 200 --json number,state,headRefName \
85
- --jq '.[] | "\(.headRefName)\tPR#\(.number) \(.state)"' 2>/dev/null || true
97
+ [ -n "$out" ] && printf '%s\n' "$out"
98
+ return 0
99
+ }
100
+
101
+ _kit_gc_pr_index_failed() {
102
+ echo "kit-gc: could not read the PR list from GitHub (gh failed) — a branch whose PR is not known is reported UNKNOWN and kept" >&2
103
+ printf '?\tUNKNOWN\n'
104
+ }
105
+
106
+ # _kit_gc_pr_index_unknown <index> — rc 0 when <index> carries the failed-read sentinel.
107
+ _kit_gc_pr_index_unknown() {
108
+ grep -q "^?$(printf '\t')UNKNOWN\$" <<<"$1"
86
109
  }
87
110
 
88
111
  # _kit_gc_pr_for <index> <branch> — first PR line matching <branch> as head (mirrors the old `.[0]`).
89
112
  _kit_gc_pr_for() {
90
- printf '%s\n' "$1" | awk -F'\t' -v want="$2" '$1==want{print $2; exit}'
113
+ printf '%s\n' "$1" | awk -F'\t' -v want="$2" '$1==want && !hit {print $2; hit=1}'
91
114
  }
92
115
 
93
116
  # _kit_gc_base_ref — the ref the LANDED checks compare against: `origin/<base>` when that
@@ -141,11 +164,11 @@ _kit_gc_landed_reason() {
141
164
  n="$(wt_issue_number "$b")"
142
165
  # `[#N]` is the kit's token; `(#N)` is GitHub's own trailing ref (what a squash puts on a PR
143
166
  # 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')"
167
+ [ -n "$n" ] || n="$(git log --format=%s "$base_ref..$b" 2>/dev/null | grep -oE '[[(]#[0-9]+[])]' | sed -n 1p | tr -dc '0-9')"
145
168
  [ -n "$n" ] || return 0
146
169
  state="$(wt_issue_state "$n" "$repo")"
147
170
  [ "$state" = closed ] || return 0
148
- if printf '%s\n' "$pr_index" | grep -qE "^[a-z]+/${n}-[^ ]* PR#[0-9]+ MERGED\$"; then
171
+ if grep -qE "^[a-z]+/${n}-[^ ]* PR#[0-9]+ MERGED\$" <<<"$pr_index"; then
149
172
  printf '%sissue #%s closed, its PR merged' "$shape" "$n"
150
173
  elif pr="$(_kit_gc_issue_merged_pr "$n" "$repo")" && [ -n "$pr" ]; then
151
174
  # The index is a 200-PR window; on a repo with hundreds of merged PRs the effort PR that closed
@@ -210,21 +233,45 @@ EOF
210
233
  # _kit_gc_classify <branch> <pr_index> [repo] — the ONE verdict for a branch, exactly as
211
234
  # kit_gc_analyze prints it after `-> `. kit_gc_prune acts on this same function, so the plan a human
212
235
  # 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.
236
+ # Order: base branch > PROTECTED (issue open, or light task open) > UNKNOWN (issue or light-task
237
+ # state read failed) > SAFE (own PR
238
+ # merged) > ACTIVE (own PR open) > a CLOSED-unmerged PR verbatim > UNKNOWN (PR list read failed) >
239
+ # LANDED (no PR, work on base) > ORPHAN.
240
+ #
241
+ # The two UNKNOWN rungs are #330. A failed issue-state read sits ABOVE SAFE and LANDED because the
242
+ # issue may be open: with gh failing, an open issue's branch with no commits beyond base used to
243
+ # lose PROTECTED and be classified LANDED by the content check, which needs no gh at all — and
244
+ # LANDED is deletable. A failed PR-list read sits above LANDED/ORPHAN because "found no PR" is not
245
+ # "has no PR" when the list never arrived. Neither string says "no PR", and neither is deletable
246
+ # (_kit_gc_prune_why answers nothing for it).
215
247
  _kit_gc_classify() {
216
- local b="$1" pr_index="$2" repo="${3:-$KIT_GC_REPO}" pr prot landed
248
+ local b="$1" pr_index="$2" repo="${3:-$KIT_GC_REPO}" pr n state="" landed
217
249
  case "$b" in "${KIT_BASE_BRANCH:-main}"|develop|main) echo "ACTIVE (base branch)"; return 0 ;; esac
218
250
  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
251
+ # The same read wt_protected_reason makes, kept as a state so `unknown` is not flattened to "".
252
+ n="$(wt_issue_number "$b" 2>/dev/null || true)"
253
+ [ -n "$n" ] && state="$(wt_issue_state "$n" "$repo" 2>/dev/null || true)"
254
+ # A light task's branch (`light/<id>-<slug>`, #450) names no issue; its task source answers. A
255
+ # copy of worktree-issue.sh that predates wt_task_id leaves both empty, as before.
256
+ local tid="" tstate=""
257
+ if command -v wt_task_id >/dev/null 2>&1; then tid="$(wt_task_id "$b" 2>/dev/null || true)"; fi
258
+ [ -n "$tid" ] && tstate="$(wt_task_state "$tid" "$b" 2>/dev/null || true)"
259
+ if [ "$state" = open ]; then
260
+ echo "PROTECTED: issue #$n still OPEN"
261
+ elif [ "$tstate" = open ]; then
262
+ echo "PROTECTED: light task $tid still OPEN"
263
+ elif [ "$state" = unknown ]; then
264
+ echo "UNKNOWN (could not determine — the state read for issue #$n failed; kept, never auto-deleted)"
265
+ elif [ "$tstate" = unknown ]; then
266
+ echo "UNKNOWN (could not determine — the state read for light task $tid failed; kept, never auto-deleted)"
267
+ elif grep -q 'MERGED' <<<"$pr"; then
223
268
  echo "SAFE (${pr}, issue closed/absent — verify level with remote before delete)"
224
- elif printf '%s' "$pr" | grep -q 'OPEN'; then
269
+ elif grep -q 'OPEN' <<<"$pr"; then
225
270
  echo "ACTIVE (${pr})"
226
271
  elif [ -n "$pr" ]; then
227
272
  echo "$pr"
273
+ elif _kit_gc_pr_index_unknown "$pr_index"; then
274
+ echo "UNKNOWN (could not determine — the PR list read failed; kept, never auto-deleted)"
228
275
  else
229
276
  landed="$(_kit_gc_landed_reason "$b" "$pr_index" "$repo")"
230
277
  if [ -n "$landed" ]; then echo "LANDED ($landed)"
@@ -232,9 +279,54 @@ _kit_gc_classify() {
232
279
  fi
233
280
  }
234
281
 
282
+ # _kit_gc_detached_verdict <worktree-path> <head-sha> — the ONE verdict for a live worktree whose HEAD
283
+ # is detached (#390), exactly as kit_gc_analyze prints it after `-> `. It has no branch, so none of
284
+ # the branch checks apply (no name to carry an issue number, no PR head to look up):
285
+ # LANDED the tree is clean AND HEAD is an ancestor of the base ref or of `main` — every commit it
286
+ # can reach is already there and no file differs, so removing it loses nothing. The shape
287
+ # an agent tool leaves behind when the `claude/*` branch it made the worktree for is gone,
288
+ # and a temp worktree a review built at a base commit.
289
+ # ORPHAN anything else: uncommitted changes, a status that cannot be read, or a HEAD that is on
290
+ # neither ref. A commit made on a detached HEAD is on no branch, so the worktree is the
291
+ # only handle on it — never deleted, surfaced by path.
292
+ # An ignored file does not make the tree dirty (same rule as the branch worktrees above).
293
+ _kit_gc_detached_verdict() {
294
+ local wtpath="$1" sha="$2" ref dirt
295
+ dirt="$(git -C "$wtpath" status --porcelain 2>/dev/null)" \
296
+ || { echo "ORPHAN (detached, status unreadable — surface, never auto-delete)"; return 0; }
297
+ if [ -n "$dirt" ]; then
298
+ echo "ORPHAN (detached, uncommitted changes — surface, never auto-delete)"; return 0
299
+ fi
300
+ for ref in "$(_kit_gc_base_ref)" origin/main main; do
301
+ [ -n "$ref" ] || continue
302
+ git rev-parse --verify --quiet "${ref}^{commit}" >/dev/null 2>&1 || continue
303
+ if git merge-base --is-ancestor "$sha" "$ref" 2>/dev/null; then
304
+ echo "LANDED (detached; ancestor of $ref)"; return 0
305
+ fi
306
+ done
307
+ echo "ORPHAN (detached, HEAD on neither the base branch nor main — surface, never auto-delete)"
308
+ }
309
+
310
+ # _kit_gc_detached_rows — echo "<path>\t<head-sha>\t<verdict>" for each LIVE linked worktree whose
311
+ # HEAD is detached. The first porcelain record is the main checkout (or a bare repo): it cannot be
312
+ # removed as a worktree, so it is never a row. A detached worktree whose directory is gone is a
313
+ # ZOMBIE and belongs to _kit_gc_zombies, not here. `substr`, not `$2`: a path may hold a space.
314
+ # Writes NOTHING. analyze, prune, cleanup and `cckit status` all read this one list.
315
+ _kit_gc_detached_rows() {
316
+ local wtpath sha
317
+ git worktree list --porcelain 2>/dev/null \
318
+ | awk '/^worktree / { n++; w = substr($0, 10); h = ""; next }
319
+ /^HEAD / { h = substr($0, 6); next }
320
+ /^detached$/ { if (n > 1) print w "\t" h }' \
321
+ | while IFS="$(printf '\t')" read -r wtpath sha; do
322
+ [ -n "$wtpath" ] && [ -d "$wtpath" ] || continue
323
+ printf '%s\t%s\t%s\n' "$wtpath" "$sha" "$(_kit_gc_detached_verdict "$wtpath" "$sha")"
324
+ done
325
+ }
326
+
235
327
  # kit_gc_analyze — read-only classification of worktrees, branches, and stashes. Writes NOTHING.
236
- # Each row is tagged PROTECTED / SAFE / ACTIVE / LANDED / ORPHAN so a human or UI can decide what
237
- # to prune.
328
+ # Each row is tagged PROTECTED / SAFE / ACTIVE / LANDED / ORPHAN / UNKNOWN so a human or UI can
329
+ # decide what to prune.
238
330
  kit_gc_analyze() {
239
331
  _kit_gc_require_deps || return 1
240
332
  _kit_gc_cache_reset
@@ -252,6 +344,14 @@ kit_gc_analyze() {
252
344
  if [ -n "$reason" ]; then echo " $wtpath [$b] -> PROTECTED: $reason"
253
345
  else echo " $wtpath [$b] -> prunable if PR merged"; fi
254
346
  done
347
+ # Detached worktrees: no `branch` line, so the loop above never sees them (#390).
348
+ local dsha dverdict
349
+ while IFS="$(printf '\t')" read -r wtpath dsha dverdict; do
350
+ [ -n "$wtpath" ] || continue
351
+ echo " $wtpath [detached@$(printf '%.12s' "$dsha")] -> $dverdict"
352
+ done <<EOF
353
+ $(_kit_gc_detached_rows)
354
+ EOF
255
355
 
256
356
  echo "# branches"
257
357
  # All PRs in ONE call, indexed locally by head branch (#124). while-read (NOT `for b in $(...)`) so
@@ -270,7 +370,9 @@ kit_gc_analyze() {
270
370
  while IFS="$(printf '\t')" read -r zname zbranch zstaged; do
271
371
  [ -n "$zname" ] || continue
272
372
  found=1
273
- if [ "$zstaged" = yes ]; then
373
+ if [ "$zstaged" = yes ] && _kit_gc_zombie_detached "$zbranch"; then
374
+ echo " $zname [$zbranch] -> ZOMBIE with STAGED work, detached — kept, never pruned (no branch to recover onto; recover by hand)"
375
+ elif [ "$zstaged" = yes ]; then
274
376
  echo " $zname [$zbranch] -> ZOMBIE with STAGED work — recover-before-prune (gc --prune --yes recovers it to a commit)"
275
377
  else
276
378
  echo " $zname [$zbranch] -> ZOMBIE (no staged delta) — prunable"
@@ -281,18 +383,20 @@ EOF
281
383
  [ "$found" -eq 1 ] || echo " (none)"
282
384
  }
283
385
 
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).
386
+ # kit_gc_has_prunable — rc 0 if at least one row is deletable: a branch that is SAFE (its own PR
387
+ # merged) or LANDED (no PR of its own, work already on base), or a LANDED detached worktree.
286
388
  kit_gc_has_prunable() {
287
- kit_gc_analyze 2>/dev/null | grep -qE '> (SAFE|LANDED) '
389
+ grep -qE '> (SAFE|LANDED) ' <<<"$(kit_gc_analyze 2>/dev/null)"
288
390
  }
289
391
 
290
392
  # _kit_gc_zombies — echo "<name>\t<branch>\t<staged:yes|no>" for each ZOMBIE worktree: its working
291
393
  # dir is gone but its admin metadata (<git-common-dir>/worktrees/<name>/) lingers. staged=yes when
292
394
  # the admin index holds a tree that differs from the branch tip (staged-but-uncommitted work that
293
- # `git worktree prune` would orphan). Writes NOTHING. bash 3.2.
395
+ # `git worktree prune` would orphan). A DETACHED zombie (its admin HEAD is a raw commit id, not a
396
+ # `ref:`) is compared to THAT commit's tree and labelled `detached@<sha12>` (#390): comparing it to
397
+ # an empty branch tip called every clean one "staged". Writes NOTHING. bash 3.2.
294
398
  _kit_gc_zombies() {
295
- local common wdir name gitdir wtpath branch tree tip tiptree staged
399
+ local common wdir name gitdir wtpath branch tree tip tiptree staged dsha
296
400
  common="$(git rev-parse --git-common-dir 2>/dev/null)" || return 0
297
401
  case "$common" in /*) : ;; *) common="$(cd "$common" 2>/dev/null && pwd)" || return 0 ;; esac
298
402
  [ -d "$common/worktrees" ] || return 0
@@ -304,20 +408,44 @@ _kit_gc_zombies() {
304
408
  name="$(basename "$wdir")"
305
409
  branch="$(sed -n 's#^ref: refs/heads/##p' "$wdir/HEAD" 2>/dev/null)"
306
410
  tree=""; [ -f "$wdir/index" ] && tree="$(GIT_INDEX_FILE="$wdir/index" git write-tree 2>/dev/null)"
307
- tip="$(git rev-parse --verify --quiet "refs/heads/${branch}" 2>/dev/null)"
411
+ dsha=""; [ -n "$branch" ] || dsha="$(_kit_gc_zombie_head_sha "$wdir")"
412
+ if [ -n "$dsha" ]; then
413
+ tip="$(git rev-parse --verify --quiet "${dsha}^{commit}" 2>/dev/null)"
414
+ branch="detached@$(printf '%.12s' "$dsha")"
415
+ else
416
+ tip="$(git rev-parse --verify --quiet "refs/heads/${branch}" 2>/dev/null)"
417
+ fi
308
418
  tiptree=""; [ -n "$tip" ] && tiptree="$(git rev-parse --verify --quiet "${tip}^{tree}" 2>/dev/null)"
309
419
  if [ -n "$tree" ] && [ "$tree" != "$tiptree" ]; then staged=yes; else staged=no; fi
310
420
  printf '%s\t%s\t%s\n' "$name" "${branch:-?}" "$staged"
311
421
  done
312
422
  }
313
423
 
424
+ # _kit_gc_zombie_head_sha <admin-dir> — echo the commit id a worktree's admin HEAD holds when it is
425
+ # DETACHED (a bare 40- or 64-hex line), nothing when HEAD is a `ref:`.
426
+ _kit_gc_zombie_head_sha() {
427
+ sed -n '1p' "$1/HEAD" 2>/dev/null | grep -E '^[0-9a-f]{40}([0-9a-f]{24})?$' || true
428
+ }
429
+
430
+ # _kit_gc_zombie_detached <label> — rc 0 when a _kit_gc_zombies branch column names a detached HEAD.
431
+ _kit_gc_zombie_detached() {
432
+ case "$1" in detached@*) return 0 ;; *) return 1 ;; esac
433
+ }
434
+
314
435
  # kit_gc_recover_zombies <yes> — the recover-before-prune mechanic (#111). For each ZOMBIE worktree
315
436
  # holding staged-but-uncommitted work, recover that delta to its branch via plumbing BEFORE any
316
437
  # prune: GIT_INDEX_FILE=<admin>/index git write-tree -> git commit-tree -p <tip> -> update-ref. With
317
438
  # <yes>=1 it performs the recovery + sweeps that zombie's stale locks; otherwise it reports what it
318
439
  # WOULD recover (dry-run). Never prunes; that stays with the caller (gated on --yes). bash 3.2.
440
+ #
441
+ # A DETACHED zombie (#390) has no branch to commit onto. With no staged delta beyond its HEAD's tree
442
+ # it is skipped like any clean zombie and the caller's prune drops it. With one, the admin index is
443
+ # the only blob->path map and there is nowhere to recover it, so under <yes>=1 the admin dir is
444
+ # LOCKED (git's own `locked` file, which `git worktree prune` honors) — the caller's prune then
445
+ # leaves that one entry in place while it drops the rest. Before this, the message said "leaving it
446
+ # for manual recovery" and the prune that followed deleted the index anyway.
319
447
  kit_gc_recover_zombies() {
320
- local yes="${1:-0}" common wdir name gitdir wtpath branch tree tip tiptree newc msg
448
+ local yes="${1:-0}" common wdir name gitdir wtpath branch tree tip tiptree newc msg dsha
321
449
  common="$(git rev-parse --git-common-dir 2>/dev/null)" || return 0
322
450
  case "$common" in /*) : ;; *) common="$(cd "$common" 2>/dev/null && pwd)" || return 0 ;; esac
323
451
  [ -d "$common/worktrees" ] || return 0
@@ -330,11 +458,26 @@ kit_gc_recover_zombies() {
330
458
  [ -f "$wdir/index" ] || continue
331
459
  tree="$(GIT_INDEX_FILE="$wdir/index" git write-tree 2>/dev/null)" || continue
332
460
  [ -n "$tree" ] || continue
333
- tip="$(git rev-parse --verify --quiet "refs/heads/${branch}" 2>/dev/null)"
461
+ dsha=""; [ -n "$branch" ] || dsha="$(_kit_gc_zombie_head_sha "$wdir")"
462
+ if [ -n "$dsha" ]; then tip="$(git rev-parse --verify --quiet "${dsha}^{commit}" 2>/dev/null)"
463
+ else tip="$(git rev-parse --verify --quiet "refs/heads/${branch}" 2>/dev/null)"; fi
334
464
  tiptree=""; [ -n "$tip" ] && tiptree="$(git rev-parse --verify --quiet "${tip}^{tree}" 2>/dev/null)"
335
465
  # No staged delta beyond the tip → nothing to recover; the zombie is safe to prune as-is.
336
466
  [ "$tree" = "$tiptree" ] && continue
337
- [ -n "$branch" ] || { echo " gc: zombie $name is detached with staged work — leaving it for manual recovery" >&2; continue; }
467
+ if [ -z "$branch" ]; then
468
+ if [ "$yes" -ne 1 ]; then
469
+ echo " would KEEP zombie worktree $name — detached with staged work (staged tree $tree), no branch to recover onto" >&2
470
+ continue
471
+ fi
472
+ [ -e "$wdir/locked" ] \
473
+ || printf '%s\n' "kit gc: detached zombie holds staged work (tree $tree) — recover before pruning" > "$wdir/locked" 2>/dev/null
474
+ if [ -e "$wdir/locked" ]; then
475
+ echo " KEPT zombie worktree $name — detached with staged work, locked so the prune skips it. Recover: git branch <name> \$(git commit-tree $tree${tip:+ -p $tip} -m recovered); then rm '${wdir%/}/locked' && git worktree prune" >&2
476
+ else
477
+ echo " gc: could not lock detached zombie $name — its staged work (tree $tree) is NOT protected from the prune" >&2
478
+ fi
479
+ continue
480
+ fi
338
481
  if [ "$yes" -ne 1 ]; then
339
482
  echo " would RECOVER staged work from zombie worktree $name -> a commit on $branch (staged tree $tree)" >&2
340
483
  continue
@@ -378,11 +521,12 @@ _kit_gc_remote_ahead() {
378
521
  # _kit_gc_only_match <regex> <branch> — rc 0 when <regex> is empty or <branch> matches it (ERE).
379
522
  _kit_gc_only_match() {
380
523
  [ -n "$1" ] || return 0
381
- printf '%s\n' "$2" | grep -qE -- "$1"
524
+ grep -qE -- "$1" <<<"$2"
382
525
  }
383
526
 
384
527
  # _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.
528
+ # not a deletable one. SAFE (own PR merged) and LANDED (no PR, work on base — #388) are the only two;
529
+ # UNKNOWN (#330, a failed gh read) falls through with every other kept verdict.
386
530
  _kit_gc_prune_why() {
387
531
  case "$1" in
388
532
  SAFE*) echo "PR MERGED" ;;
@@ -393,6 +537,7 @@ _kit_gc_prune_why() {
393
537
 
394
538
  # kit_gc_prune [--yes] [--only <regex>] - remove worktrees + local branches whose PR is MERGED (the
395
539
  # SAFE rows) or whose work is already on base with no PR of their own (the LANDED rows, #388).
540
+ # A detached worktree rated LANDED (#390) goes the same way; one rated ORPHAN never does.
396
541
  # DRY-RUN by default (lists what it WOULD remove); --yes performs the deletions. Never touches a
397
542
  # PROTECTED/ACTIVE/ORPHAN branch, and never a DIRTY worktree (recover-before-prune): a worktree with
398
543
  # staged/unstaged/untracked changes is skipped with a warning, not destroyed. The remote branch is
@@ -440,6 +585,28 @@ kit_gc_prune() {
440
585
  fi
441
586
  done
442
587
 
588
+ # Detached worktrees (#390): only the LANDED ones, by the same verdict analyze printed. No
589
+ # `--force`: the verdict already required a clean tree, so a refusal here means it changed since
590
+ # (or is locked) and the worktree is left exactly as it is. Not branch-scoped, so skipped under
591
+ # --only like the zombie pass below.
592
+ if [ -z "$only" ]; then
593
+ local dsha dlabel
594
+ while IFS="$(printf '\t')" read -r wtpath dsha verdict; do
595
+ [ -n "$wtpath" ] || continue
596
+ why="$(_kit_gc_prune_why "$verdict")"; [ -n "$why" ] || continue
597
+ dlabel="detached@$(printf '%.12s' "$dsha")"
598
+ if [ "$yes" -ne 1 ]; then
599
+ echo " would remove worktree $wtpath [$dlabel] ($why)"
600
+ elif git worktree remove "$wtpath" 2>/dev/null; then
601
+ echo " removed worktree $wtpath [$dlabel] ($why)"
602
+ else
603
+ echo " SKIP worktree $wtpath [$dlabel] - git refused to remove it (locked, or changed since the plan)" >&2
604
+ fi
605
+ done <<EOF
606
+ $(_kit_gc_detached_rows)
607
+ EOF
608
+ fi
609
+
443
610
  # recover-before-prune (#111): a zombie worktree (working dir gone, admin metadata lingers) may
444
611
  # hold staged-but-uncommitted work in its admin index — the ONLY blob->path map. `git worktree
445
612
  # prune` deletes that index, orphaning the blobs for a later `git gc` to reap — real data loss in a
@@ -506,6 +673,8 @@ kit_gc_cleanup() {
506
673
  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
507
674
  local zname zbranch zstaged
508
675
  local safe_list landed_list orphan_list prot_list stash_list wt_list zomb_list level_ok n_zomb n_landed
676
+ local unknown_list n_unknown
677
+ local det_rows det_wt det_orphan wtpath dsha dverdict n_zkept
509
678
  for a in "$@"; do case "$a" in --yes|-y) yes=1 ;; esac; done
510
679
 
511
680
  out="$(kit_gc_analyze)" || return 1
@@ -516,6 +685,7 @@ kit_gc_cleanup() {
516
685
  in_s && / -> LANDED / { sub(/^ /, ""); sub(/ -> LANDED /, " "); print }')"
517
686
  orphan_list="$(_kit_gc_names "$out" branches ' -> ORPHAN')"
518
687
  prot_list="$(_kit_gc_names "$out" branches ' -> PROTECTED:')"
688
+ unknown_list="$(_kit_gc_names "$out" branches ' -> UNKNOWN ')"
519
689
  stash_list="$(printf '%s\n' "$out" | awk '/^# /{in_s=($0=="# stashes"); next} in_s && NF' | sed 's/^ //')"
520
690
 
521
691
  # Worktrees kit_gc_prune would remove: the ones whose branch is itself SAFE or LANDED (the same
@@ -524,8 +694,8 @@ kit_gc_cleanup() {
524
694
  | awk '/^worktree /{w=$2} /^branch /{print w" "$2}' \
525
695
  | while read -r wtpath ref; do
526
696
  b="${ref#refs/heads/}"
527
- printf '%s\n' "$safe_list" | grep -qxF "$b" \
528
- || printf '%s\n' "$landed_list" | sed 's/ (.*//' | grep -qxF "$b" || continue
697
+ grep -qxF "$b" <<<"$safe_list" \
698
+ || grep -qxF "$b" <<<"$(printf '%s\n' "$landed_list" | sed 's/ (.*//')" || continue
529
699
  if [ -n "$(git -C "$wtpath" status --porcelain 2>/dev/null)" ]; then
530
700
  echo "$wtpath [$b] (DIRTY — will be skipped, not destroyed)"
531
701
  else
@@ -533,13 +703,28 @@ kit_gc_cleanup() {
533
703
  fi
534
704
  done)"
535
705
 
706
+ # Detached worktrees (#390) have no branch, so neither list above carries them. The LANDED ones
707
+ # join the worktrees --yes removes; the ORPHAN ones join the kept rows, named by path. Same helper
708
+ # as the analyze report and the prune pass, so the three cannot diverge.
709
+ det_rows="$(_kit_gc_detached_rows)"
710
+ det_wt="$(printf '%s\n' "$det_rows" | while IFS="$(printf '\t')" read -r wtpath dsha dverdict; do
711
+ case "$dverdict" in LANDED*) echo "$wtpath [detached@$(printf '%.12s' "$dsha")] ${dverdict#LANDED }" ;; *) : ;; esac
712
+ done)"
713
+ det_orphan="$(printf '%s\n' "$det_rows" | while IFS="$(printf '\t')" read -r wtpath dsha dverdict; do
714
+ case "$dverdict" in ORPHAN*) echo "$wtpath [detached@$(printf '%.12s' "$dsha")] ${dverdict#ORPHAN }" ;; *) : ;; esac
715
+ done)"
716
+ [ -n "$det_wt" ] && wt_list="$(printf '%s\n%s' "$wt_list" "$det_wt" | grep . || true)"
717
+ [ -n "$det_orphan" ] && orphan_list="$(printf '%s\n%s' "$orphan_list" "$det_orphan" | grep . || true)"
718
+
536
719
  # ZOMBIE worktrees (dir gone, admin metadata lingers). kit_gc_prune --yes acts on these two ways
537
720
  # — kit_gc_recover_zombies commits any staged delta to the branch, then `git worktree prune`
538
721
  # drops the metadata — so the plan MUST name them or --yes would touch a row nobody approved
539
722
  # (#227 review). Same helper as the analyze report, so the two cannot diverge.
540
723
  zomb_list="$(while IFS="$(printf '\t')" read -r zname zbranch zstaged; do
541
724
  [ -n "$zname" ] || continue
542
- if [ "$zstaged" = yes ]; then
725
+ if [ "$zstaged" = yes ] && _kit_gc_zombie_detached "$zbranch"; then
726
+ echo "$zname [$zbranch] (STAGED work, detached — kept, not pruned: no branch to recover onto)"
727
+ elif [ "$zstaged" = yes ]; then
543
728
  echo "$zname [$zbranch] (STAGED work — recovered to a commit on $zbranch, then metadata pruned)"
544
729
  else
545
730
  echo "$zname [$zbranch] (no staged delta — metadata pruned)"
@@ -553,9 +738,11 @@ ZEOF
553
738
  n_landed="$(printf '%s' "$landed_list" | grep -c . || true)"
554
739
  n_orphan="$(printf '%s' "$orphan_list" | grep -c . || true)"
555
740
  n_prot="$(printf '%s' "$prot_list" | grep -c . || true)"
741
+ n_unknown="$(printf '%s' "$unknown_list" | grep -c . || true)"
556
742
  n_stash="$(printf '%s' "$stash_list" | grep -c . || true)"
557
743
  n_wt="$(printf '%s' "$wt_list" | grep -c . || true)"
558
744
  n_zomb="$(printf '%s' "$zomb_list" | grep -c . || true)"
745
+ n_zkept="$(printf '%s' "$zomb_list" | grep -c 'kept, not pruned' || true)"
559
746
 
560
747
  echo "# cleanup plan"
561
748
  printf ' SAFE branches %3s merged PR, issue closed/absent\n' "$n_safe"
@@ -570,6 +757,8 @@ ZEOF
570
757
  printf '%s\n' "$orphan_list" | grep . | sed 's/^/ /' || true
571
758
  printf ' PROTECTED kept %3s issue still OPEN\n' "$n_prot"
572
759
  printf '%s\n' "$prot_list" | grep . | sed 's/^/ /' || true
760
+ printf ' UNKNOWN kept %3s a GitHub read failed — could not determine, rerun when gh answers\n' "$n_unknown"
761
+ printf '%s\n' "$unknown_list" | grep . | sed 's/^/ /' || true
573
762
  printf ' stashes kept %3s irreversible — drop by hand after reading each diff\n' "$n_stash"
574
763
  printf '%s\n' "$stash_list" | grep . | sed 's/^/ /' || true
575
764
 
@@ -613,6 +802,6 @@ EOF
613
802
  $level_ok
614
803
  EOF
615
804
  printf ' remote deletes failed %3s\n' "$n_rfail"
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."
805
+ echo "cleanup: done — $n_safe safe + $n_landed landed deleted / $((n_zomb - n_zkept)) zombie pruned / $n_orphan orphan / $n_prot protected / $n_unknown unknown / $n_stash stash left untouched."
617
806
  return "$rc"
618
807
  }