@jeiemgi/cckit 0.8.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 (309) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +169 -21
  3. package/README.md +505 -35
  4. package/SECURITY.md +5 -0
  5. package/bin/cckit +189 -20
  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/guard-raw-git-ops-test.sh +8 -0
  59. package/scripts/hook-adapter-test.sh +193 -0
  60. package/scripts/init-content-test.sh +225 -0
  61. package/scripts/init-guided-test.sh +474 -0
  62. package/scripts/init-recipes-test.sh +104 -0
  63. package/scripts/init-upgrade-test.sh +341 -2
  64. package/scripts/init.sh +746 -95
  65. package/scripts/instruction-budget-baseline.txt +8 -0
  66. package/scripts/instruction-budget-lint-test.sh +166 -0
  67. package/scripts/instruction-budget-lint.sh +219 -0
  68. package/scripts/integrations-check-test.sh +200 -0
  69. package/scripts/integrations-check.sh +157 -0
  70. package/scripts/kit-add-test.sh +214 -0
  71. package/scripts/kit-add.sh +305 -5
  72. package/scripts/kit-adopt-test.sh +16 -0
  73. package/scripts/kit-adopt.sh +3 -2
  74. package/scripts/kit-digest.sh +2 -2
  75. package/scripts/kit-doctor-test.sh +293 -0
  76. package/scripts/kit-doctor.sh +272 -140
  77. package/scripts/kit-export-project-test.sh +3 -3
  78. package/scripts/kit-remove-test.sh +19 -0
  79. package/scripts/kit-routines.sh +2 -2
  80. package/scripts/kit-self-update.sh +1 -1
  81. package/scripts/kit-version-check-test.sh +4 -0
  82. package/scripts/kit-version-check.sh +4 -2
  83. package/scripts/knowledge-lint.sh +4 -4
  84. package/scripts/leak-fence-test.sh +89 -0
  85. package/scripts/lib/adapter-select-test.sh +136 -0
  86. package/scripts/lib/adapter-select.sh +158 -0
  87. package/scripts/lib/agent-def-test.sh +69 -0
  88. package/scripts/lib/agent-def.sh +106 -0
  89. package/scripts/lib/agent-launch-test.sh +212 -0
  90. package/scripts/lib/agent-launch.sh +335 -0
  91. package/scripts/lib/board-view-test.sh +11 -0
  92. package/scripts/lib/board-view.sh +42 -26
  93. package/scripts/lib/branch-owner-test.sh +26 -0
  94. package/scripts/lib/branch-owner.sh +16 -9
  95. package/scripts/lib/captain-test.sh +105 -0
  96. package/scripts/lib/captain.sh +87 -8
  97. package/scripts/lib/codex-hooks-test.sh +100 -0
  98. package/scripts/lib/codex-hooks.sh +69 -0
  99. package/scripts/lib/commitlint.sh +1 -1
  100. package/scripts/lib/content-kinds-gc-test.sh +88 -0
  101. package/scripts/lib/copilot-test.sh +70 -4
  102. package/scripts/lib/copilot.sh +66 -14
  103. package/scripts/lib/effort-dispatch-test.sh +3 -3
  104. package/scripts/lib/effort-metrics-test.sh +178 -1
  105. package/scripts/lib/effort-metrics.sh +188 -35
  106. package/scripts/lib/effort-ops-test.sh +217 -7
  107. package/scripts/lib/effort-ops.sh +281 -49
  108. package/scripts/lib/effort-parity-test.sh +49 -13
  109. package/scripts/lib/effort-plan-test.sh +337 -0
  110. package/scripts/lib/effort-plan.sh +200 -23
  111. package/scripts/lib/effort-slug.sh +16 -2
  112. package/scripts/lib/effort.sh +67 -12
  113. package/scripts/lib/errors-header-test.sh +1 -1
  114. package/scripts/lib/gh-project-test.sh +35 -1
  115. package/scripts/lib/gh-project.sh +37 -16
  116. package/scripts/lib/gh-rest-test.sh +330 -0
  117. package/scripts/lib/gh-rest.sh +304 -0
  118. package/scripts/lib/hook-refresh.sh +1 -1
  119. package/scripts/lib/init-github.sh +412 -0
  120. package/scripts/lib/init-guide.sh +630 -0
  121. package/scripts/lib/integration-install-test.sh +343 -0
  122. package/scripts/lib/integration-install.sh +257 -0
  123. package/scripts/lib/integrations-test.sh +366 -0
  124. package/scripts/lib/integrations.sh +244 -0
  125. package/scripts/lib/kinds-test.sh +125 -0
  126. package/scripts/lib/kinds.sh +66 -0
  127. package/scripts/lib/kit-brief-test.sh +22 -3
  128. package/scripts/lib/kit-brief.sh +45 -5
  129. package/scripts/lib/kit-config-resolve.sh +2 -2
  130. package/scripts/lib/kit-config-test.sh +64 -1
  131. package/scripts/lib/kit-config.sh +37 -1
  132. package/scripts/lib/kit-events.sh +7 -0
  133. package/scripts/lib/kit-gc-test.sh +173 -1
  134. package/scripts/lib/kit-gc.sh +244 -37
  135. package/scripts/lib/kit-hotfix.sh +216 -0
  136. package/scripts/lib/kit-interview-test.sh +93 -0
  137. package/scripts/lib/kit-interview.sh +117 -8
  138. package/scripts/lib/kit-lib-test.sh +1 -1
  139. package/scripts/lib/kit-lib.sh +3 -3
  140. package/scripts/lib/kit-local.sh +2 -2
  141. package/scripts/lib/kit-manifest.sh +33 -1
  142. package/scripts/lib/kit-profile-test.sh +3 -3
  143. package/scripts/lib/kit-status-test.sh +112 -4
  144. package/scripts/lib/kit-status.sh +246 -16
  145. package/scripts/lib/kit-task-ops-test.sh +37 -0
  146. package/scripts/lib/kit-task-ops.sh +362 -89
  147. package/scripts/lib/kto-hotfix-test.sh +232 -0
  148. package/scripts/lib/kto-task-close-test.sh +83 -0
  149. package/scripts/lib/kto-task-new-test.sh +95 -0
  150. package/scripts/lib/kto-task-pr-merge-test.sh +97 -6
  151. package/scripts/lib/kto-task-pr-test.sh +70 -0
  152. package/scripts/lib/leak-fence.sh +89 -0
  153. package/scripts/lib/light-task-run-test.sh +255 -0
  154. package/scripts/lib/light-task-test.sh +309 -0
  155. package/scripts/lib/light-task.sh +706 -0
  156. package/scripts/lib/next-test.sh +99 -0
  157. package/scripts/lib/next.sh +9 -3
  158. package/scripts/lib/orchestration-runtime-test.sh +72 -0
  159. package/scripts/lib/orchestration-runtime.sh +57 -5
  160. package/scripts/lib/plan-machine-test.sh +15 -2
  161. package/scripts/lib/plan-machine.sh +20 -7
  162. package/scripts/lib/plan-next-test.sh +1 -1
  163. package/scripts/lib/plan-next.sh +4 -4
  164. package/scripts/lib/pr-evidence-test.sh +3 -3
  165. package/scripts/lib/pr-evidence.sh +1 -1
  166. package/scripts/lib/qa-lane-test.sh +626 -0
  167. package/scripts/lib/qa-lane.sh +1357 -0
  168. package/scripts/lib/qa-lanes-test.sh +175 -0
  169. package/scripts/lib/react-detect.sh +1 -1
  170. package/scripts/lib/render-test.sh +1 -1
  171. package/scripts/lib/review-page-test.sh +318 -0
  172. package/scripts/lib/review-page.sh +369 -0
  173. package/scripts/lib/review-stage-test.sh +62 -11
  174. package/scripts/lib/review-stage.sh +42 -60
  175. package/scripts/lib/role-identity-test.sh +65 -0
  176. package/scripts/lib/role-identity.sh +25 -2
  177. package/scripts/lib/secret-guard.sh +3 -3
  178. package/scripts/lib/session-registry-test.sh +111 -0
  179. package/scripts/lib/session-registry.sh +233 -0
  180. package/scripts/lib/stage-receipt-test.sh +14 -1
  181. package/scripts/lib/stage-receipt.sh +13 -8
  182. package/scripts/lib/task-source-file.sh +309 -0
  183. package/scripts/lib/task-source.sh +130 -0
  184. package/scripts/lib/test-env-test.sh +3 -3
  185. package/scripts/lib/test-env.sh +7 -4
  186. package/scripts/lib/upgrade-report-test.sh +120 -0
  187. package/scripts/lib/upgrade-report.sh +138 -0
  188. package/scripts/lib/version-bump.sh +3 -3
  189. package/scripts/lib/wave-result-test.sh +80 -0
  190. package/scripts/lib/wave-result.sh +127 -0
  191. package/scripts/lib/worker-brief-test.sh +5 -0
  192. package/scripts/lib/worker-brief.sh +2 -2
  193. package/scripts/lib/worktree-issue-test.sh +30 -1
  194. package/scripts/lib/worktree-issue.sh +93 -9
  195. package/scripts/lib/worktree-ports-test.sh +57 -4
  196. package/scripts/lib/worktree-start-test.sh +197 -0
  197. package/scripts/lib/worktree-start.sh +169 -32
  198. package/scripts/lib/zsh-safety-test.sh +19 -3
  199. package/scripts/marketing-walkthrough-test.sh +152 -0
  200. package/scripts/night-grant-test.sh +37 -0
  201. package/scripts/night-grant.sh +448 -0
  202. package/scripts/night-run-test.sh +8 -0
  203. package/scripts/night-run.mjs +275 -0
  204. package/scripts/night-run.test.mjs +324 -0
  205. package/scripts/orchestrate-test.sh +190 -0
  206. package/scripts/orchestrate.sh +79 -8
  207. package/scripts/portable-test.sh +11 -2
  208. package/scripts/publish.sh +28 -3
  209. package/scripts/review-lint-test.sh +7 -7
  210. package/scripts/review-lint.sh +2 -2
  211. package/scripts/review-rules.conf +8 -0
  212. package/scripts/rule-recipes-test.sh +96 -0
  213. package/scripts/self-install-test.sh +39 -9
  214. package/scripts/setup-labels.sh +18 -1
  215. package/scripts/setup-milestones.sh +1 -1
  216. package/scripts/showcase.sh +1 -1
  217. package/scripts/skill-frontmatter-lint-test.sh +4 -4
  218. package/scripts/skill-frontmatter-lint.sh +1 -1
  219. package/scripts/status-test.sh +113 -2
  220. package/scripts/status.sh +16 -3
  221. package/scripts/task-sync-test.sh +154 -0
  222. package/scripts/task-sync.sh +243 -32
  223. package/scripts/test-runner-test.sh +158 -0
  224. package/scripts/test.sh +131 -23
  225. package/scripts/update-path-test.sh +279 -0
  226. package/scripts/ux-ds-audit.sh +1 -1
  227. package/scripts/ux-graph-scan.sh +6 -6
  228. package/scripts/wave-worker.sh +54 -0
  229. package/skills/kit-add/SKILL.md +77 -0
  230. package/skills/kit-autopilot/SKILL.md +123 -12
  231. package/skills/kit-cleanup/SKILL.md +5 -2
  232. package/skills/kit-contribute/SKILL.md +4 -4
  233. package/skills/kit-docs-writer/SKILL.md +16 -6
  234. package/skills/kit-effort-close/SKILL.md +17 -8
  235. package/skills/kit-effort-new/SKILL.md +15 -9
  236. package/skills/kit-effort-pr/SKILL.md +15 -7
  237. package/skills/kit-effort-start/SKILL.md +5 -2
  238. package/skills/kit-gc/SKILL.md +5 -2
  239. package/skills/kit-next/SKILL.md +5 -1
  240. package/skills/kit-onboard/SKILL.md +3 -3
  241. package/skills/kit-page/SKILL.md +93 -0
  242. package/skills/kit-plan-next/SKILL.md +8 -4
  243. package/skills/kit-security-sweep/SKILL.md +1 -3
  244. package/skills/kit-ship/SKILL.md +2 -3
  245. package/skills/kit-status/SKILL.md +35 -4
  246. package/skills/kit-task-new/SKILL.md +3 -3
  247. package/skills/kit-task-pr/SKILL.md +7 -2
  248. package/skills/kit-task-pr-merge/SKILL.md +7 -2
  249. package/skills/kit-task-start/SKILL.md +7 -7
  250. package/skills/kit-task-sync/SKILL.md +19 -3
  251. package/skills/kit-ux-audit/SKILL.md +3 -2
  252. package/templates/agents/auto-dev.md +5 -4
  253. package/templates/agents/backend.md +1 -1
  254. package/templates/agents/designer.md +2 -0
  255. package/templates/agents/devops.md +1 -1
  256. package/templates/agents/editor.md +9 -1
  257. package/templates/agents/frontend.md +1 -1
  258. package/templates/agents/local-delegate.md +1 -1
  259. package/templates/agents/pm.md +5 -5
  260. package/templates/agents/tech-lead.md +1 -1
  261. package/templates/benchmarks/SCHEMA.md +9 -3
  262. package/templates/codex/hooks.json.tmpl +89 -0
  263. package/templates/hooks/guard-base-branch-commit.sh.tmpl +13 -7
  264. package/templates/hooks/guard-raw-git-ops.sh.tmpl +27 -14
  265. package/templates/hooks/mempal_followup.sh.tmpl +2 -2
  266. package/templates/hooks/mempal_session_start.sh.tmpl +1 -1
  267. package/templates/hooks/prepush_gate.sh.tmpl +4 -2
  268. package/templates/hooks/repo-hygiene.sh.tmpl +3 -2
  269. package/templates/instructions.md.tmpl +174 -0
  270. package/templates/kit.config.json.tmpl +3 -3
  271. package/templates/knowledge-INDEX.md.tmpl +1 -1
  272. package/templates/lib/kit-sigil.sh.tmpl +1 -1
  273. package/templates/rules/authoring.md +106 -0
  274. package/templates/rules/branch-naming.md +40 -4
  275. package/templates/rules/code-retrieval.md +63 -0
  276. package/templates/rules/delegation-brief.md +0 -3
  277. package/templates/rules/design-routing.md +3 -3
  278. package/templates/rules/effort-model.md +14 -10
  279. package/templates/rules/gate-and-review.md +94 -0
  280. package/templates/rules/issue-lifecycle.md +88 -0
  281. package/templates/rules/knowledge-base.md +1 -1
  282. package/templates/rules/mempalace.md +1 -1
  283. package/templates/rules/merge-safety.md +108 -0
  284. package/templates/rules/naming-and-ids.md +23 -17
  285. package/templates/rules/plain-language.md +59 -0
  286. package/templates/rules/plan-next.md +9 -11
  287. package/templates/rules/react-annotate.md +6 -0
  288. package/templates/rules/review-pages.md +49 -0
  289. package/templates/rules/risk-tiered-review.md +13 -4
  290. package/templates/rules/shared-checkout.md +106 -0
  291. package/templates/rules/skill-gaps.md +2 -7
  292. package/templates/rules/task-management.md +13 -1
  293. package/templates/skills/brand-voice/SKILL.md +91 -0
  294. package/templates/skills/brand-voice/references/voice-template.md +61 -0
  295. package/templates/skills/campaign/SKILL.md +123 -0
  296. package/templates/skills/campaign/references/plan-template.md +55 -0
  297. package/templates/skills/concrete/SKILL.md +2 -2
  298. package/templates/skills/copywriting/SKILL.md +8 -1
  299. package/templates/skills/feature-build-refine/SKILL.md +2 -4
  300. package/templates/skills/morning-briefing/SKILL.md +2 -2
  301. package/templates/skills/page-design/SKILL.md +145 -0
  302. package/templates/skills/page-design/add.json +5 -0
  303. package/templates/skills/page-design/references/design-file-template.md +40 -0
  304. package/templates/skills/page-design/references/generic-page-patterns.md +43 -0
  305. package/templates/skills/page-design/references/minimum-rules.md +42 -0
  306. package/templates/skills/speckit/SKILL.md +4 -5
  307. package/templates/skills/supabase-patterns/SKILL.md +1 -1
  308. package/commands/kit-add.md +0 -42
  309. package/templates/CLAUDE.md.tmpl +0 -106
@@ -17,15 +17,27 @@
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.
24
27
  # errors: mixed — kit_gc_analyze is read-only and forgiving; the prune path propagates
25
28
 
29
+ # The GitHub transport switch (#396): rest mode routes the gh calls below through gh-rest.sh.
30
+ if ! command -v cckit_gh_rest >/dev/null 2>&1; then
31
+ _ghr_lib="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>/dev/null && pwd)/gh-rest.sh"
32
+ # shellcheck source=/dev/null
33
+ [ -f "$_ghr_lib" ] && . "$_ghr_lib"
34
+ unset _ghr_lib
35
+ fi
36
+ command -v cckit_gh_rest >/dev/null 2>&1 || cckit_gh_rest() { return 1; }
37
+
26
38
  KIT_GC_REPO="${KIT_GC_REPO:-${KIT_REPO:-}}"
27
39
 
28
- _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}'; }
29
41
 
30
42
  # This file's own directory, resolved ONCE at load time — the only point where both shells agree.
31
43
  # `BASH_SOURCE` is bash-only; inside a zsh *function* it is unset and `$0` is the function name, so
@@ -65,15 +77,40 @@ _kit_gc_require_deps() {
65
77
  # _kit_gc_pr_index [repo] — echo one "<headRefName>\tPR#<num> <STATE>" line per PR in ONE gh call, so
66
78
  # branch classification is a local lookup instead of a per-branch `gh pr list --head` — an N+1 that
67
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`.
68
86
  _kit_gc_pr_index() {
69
- local repo="${1:-$KIT_GC_REPO}"
70
- gh pr list --repo "$repo" --state all --limit 200 --json number,state,headRefName \
71
- --jq '.[] | "\(.headRefName)\tPR#\(.number) \(.state)"' 2>/dev/null || true
87
+ local repo="${1:-$KIT_GC_REPO}" out
88
+ if cckit_gh_rest; then
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; }
96
+ fi
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"
72
109
  }
73
110
 
74
111
  # _kit_gc_pr_for <index> <branch> — first PR line matching <branch> as head (mirrors the old `.[0]`).
75
112
  _kit_gc_pr_for() {
76
- 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}'
77
114
  }
78
115
 
79
116
  # _kit_gc_base_ref — the ref the LANDED checks compare against: `origin/<base>` when that
@@ -127,11 +164,11 @@ _kit_gc_landed_reason() {
127
164
  n="$(wt_issue_number "$b")"
128
165
  # `[#N]` is the kit's token; `(#N)` is GitHub's own trailing ref (what a squash puts on a PR
129
166
  # 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')"
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')"
131
168
  [ -n "$n" ] || return 0
132
169
  state="$(wt_issue_state "$n" "$repo")"
133
170
  [ "$state" = closed ] || return 0
134
- 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
135
172
  printf '%sissue #%s closed, its PR merged' "$shape" "$n"
136
173
  elif pr="$(_kit_gc_issue_merged_pr "$n" "$repo")" && [ -n "$pr" ]; then
137
174
  # The index is a 200-PR window; on a repo with hundreds of merged PRs the effort PR that closed
@@ -170,12 +207,16 @@ _kit_gc_issue_merged_pr() {
170
207
  while IFS= read -r cand; do
171
208
  [ -n "$cand" ] || continue
172
209
  case "$cand" in *[!0-9]*) continue ;; esac
173
- if [ "$(gh pr view "$cand" --repo "$repo" --json state --jq .state 2>/dev/null)" = "MERGED" ]; then
210
+ if cckit_gh_rest; then
211
+ # REST: the candidate came from a merged cross-reference that closes <n> (gh_rest_wave_rows).
212
+ result="$cand"; break
213
+ elif [ "$(gh pr view "$cand" --repo "$repo" --json state --jq .state 2>/dev/null)" = "MERGED" ]; then
174
214
  result="$cand"; break
175
215
  fi
176
216
  done <<EOF
177
- $(gh issue view "$n" --repo "$repo" --json closedByPullRequestsReferences \
178
- --jq '.closedByPullRequestsReferences[].number' 2>/dev/null || true)
217
+ $(if cckit_gh_rest; then gh_rest_issue_closing_prs "$repo" "$n" 2>/dev/null || true
218
+ else gh issue view "$n" --repo "$repo" --json closedByPullRequestsReferences \
219
+ --jq '.closedByPullRequestsReferences[].number' 2>/dev/null || true; fi)
179
220
  EOF
180
221
  if [ -z "$result" ] && [ "$depth" -eq 0 ]; then
181
222
  parent="$(gh api "repos/$repo/issues/$n/parent" --jq .number 2>/dev/null || true)"
@@ -192,21 +233,45 @@ EOF
192
233
  # _kit_gc_classify <branch> <pr_index> [repo] — the ONE verdict for a branch, exactly as
193
234
  # kit_gc_analyze prints it after `-> `. kit_gc_prune acts on this same function, so the plan a human
194
235
  # 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.
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).
197
247
  _kit_gc_classify() {
198
- 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
199
249
  case "$b" in "${KIT_BASE_BRANCH:-main}"|develop|main) echo "ACTIVE (base branch)"; return 0 ;; esac
200
250
  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
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
205
268
  echo "SAFE (${pr}, issue closed/absent — verify level with remote before delete)"
206
- elif printf '%s' "$pr" | grep -q 'OPEN'; then
269
+ elif grep -q 'OPEN' <<<"$pr"; then
207
270
  echo "ACTIVE (${pr})"
208
271
  elif [ -n "$pr" ]; then
209
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)"
210
275
  else
211
276
  landed="$(_kit_gc_landed_reason "$b" "$pr_index" "$repo")"
212
277
  if [ -n "$landed" ]; then echo "LANDED ($landed)"
@@ -214,9 +279,54 @@ _kit_gc_classify() {
214
279
  fi
215
280
  }
216
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
+
217
327
  # kit_gc_analyze — read-only classification of worktrees, branches, and stashes. Writes NOTHING.
218
- # Each row is tagged PROTECTED / SAFE / ACTIVE / LANDED / ORPHAN so a human or UI can decide what
219
- # to prune.
328
+ # Each row is tagged PROTECTED / SAFE / ACTIVE / LANDED / ORPHAN / UNKNOWN so a human or UI can
329
+ # decide what to prune.
220
330
  kit_gc_analyze() {
221
331
  _kit_gc_require_deps || return 1
222
332
  _kit_gc_cache_reset
@@ -234,6 +344,14 @@ kit_gc_analyze() {
234
344
  if [ -n "$reason" ]; then echo " $wtpath [$b] -> PROTECTED: $reason"
235
345
  else echo " $wtpath [$b] -> prunable if PR merged"; fi
236
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
237
355
 
238
356
  echo "# branches"
239
357
  # All PRs in ONE call, indexed locally by head branch (#124). while-read (NOT `for b in $(...)`) so
@@ -252,7 +370,9 @@ kit_gc_analyze() {
252
370
  while IFS="$(printf '\t')" read -r zname zbranch zstaged; do
253
371
  [ -n "$zname" ] || continue
254
372
  found=1
255
- 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
256
376
  echo " $zname [$zbranch] -> ZOMBIE with STAGED work — recover-before-prune (gc --prune --yes recovers it to a commit)"
257
377
  else
258
378
  echo " $zname [$zbranch] -> ZOMBIE (no staged delta) — prunable"
@@ -263,18 +383,20 @@ EOF
263
383
  [ "$found" -eq 1 ] || echo " (none)"
264
384
  }
265
385
 
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).
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.
268
388
  kit_gc_has_prunable() {
269
- kit_gc_analyze 2>/dev/null | grep -qE '> (SAFE|LANDED) '
389
+ grep -qE '> (SAFE|LANDED) ' <<<"$(kit_gc_analyze 2>/dev/null)"
270
390
  }
271
391
 
272
392
  # _kit_gc_zombies — echo "<name>\t<branch>\t<staged:yes|no>" for each ZOMBIE worktree: its working
273
393
  # dir is gone but its admin metadata (<git-common-dir>/worktrees/<name>/) lingers. staged=yes when
274
394
  # the admin index holds a tree that differs from the branch tip (staged-but-uncommitted work that
275
- # `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.
276
398
  _kit_gc_zombies() {
277
- local common wdir name gitdir wtpath branch tree tip tiptree staged
399
+ local common wdir name gitdir wtpath branch tree tip tiptree staged dsha
278
400
  common="$(git rev-parse --git-common-dir 2>/dev/null)" || return 0
279
401
  case "$common" in /*) : ;; *) common="$(cd "$common" 2>/dev/null && pwd)" || return 0 ;; esac
280
402
  [ -d "$common/worktrees" ] || return 0
@@ -286,20 +408,44 @@ _kit_gc_zombies() {
286
408
  name="$(basename "$wdir")"
287
409
  branch="$(sed -n 's#^ref: refs/heads/##p' "$wdir/HEAD" 2>/dev/null)"
288
410
  tree=""; [ -f "$wdir/index" ] && tree="$(GIT_INDEX_FILE="$wdir/index" git write-tree 2>/dev/null)"
289
- 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
290
418
  tiptree=""; [ -n "$tip" ] && tiptree="$(git rev-parse --verify --quiet "${tip}^{tree}" 2>/dev/null)"
291
419
  if [ -n "$tree" ] && [ "$tree" != "$tiptree" ]; then staged=yes; else staged=no; fi
292
420
  printf '%s\t%s\t%s\n' "$name" "${branch:-?}" "$staged"
293
421
  done
294
422
  }
295
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
+
296
435
  # kit_gc_recover_zombies <yes> — the recover-before-prune mechanic (#111). For each ZOMBIE worktree
297
436
  # holding staged-but-uncommitted work, recover that delta to its branch via plumbing BEFORE any
298
437
  # prune: GIT_INDEX_FILE=<admin>/index git write-tree -> git commit-tree -p <tip> -> update-ref. With
299
438
  # <yes>=1 it performs the recovery + sweeps that zombie's stale locks; otherwise it reports what it
300
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.
301
447
  kit_gc_recover_zombies() {
302
- 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
303
449
  common="$(git rev-parse --git-common-dir 2>/dev/null)" || return 0
304
450
  case "$common" in /*) : ;; *) common="$(cd "$common" 2>/dev/null && pwd)" || return 0 ;; esac
305
451
  [ -d "$common/worktrees" ] || return 0
@@ -312,11 +458,26 @@ kit_gc_recover_zombies() {
312
458
  [ -f "$wdir/index" ] || continue
313
459
  tree="$(GIT_INDEX_FILE="$wdir/index" git write-tree 2>/dev/null)" || continue
314
460
  [ -n "$tree" ] || continue
315
- 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
316
464
  tiptree=""; [ -n "$tip" ] && tiptree="$(git rev-parse --verify --quiet "${tip}^{tree}" 2>/dev/null)"
317
465
  # No staged delta beyond the tip → nothing to recover; the zombie is safe to prune as-is.
318
466
  [ "$tree" = "$tiptree" ] && continue
319
- [ -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
320
481
  if [ "$yes" -ne 1 ]; then
321
482
  echo " would RECOVER staged work from zombie worktree $name -> a commit on $branch (staged tree $tree)" >&2
322
483
  continue
@@ -360,11 +521,12 @@ _kit_gc_remote_ahead() {
360
521
  # _kit_gc_only_match <regex> <branch> — rc 0 when <regex> is empty or <branch> matches it (ERE).
361
522
  _kit_gc_only_match() {
362
523
  [ -n "$1" ] || return 0
363
- printf '%s\n' "$2" | grep -qE -- "$1"
524
+ grep -qE -- "$1" <<<"$2"
364
525
  }
365
526
 
366
527
  # _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.
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.
368
530
  _kit_gc_prune_why() {
369
531
  case "$1" in
370
532
  SAFE*) echo "PR MERGED" ;;
@@ -375,6 +537,7 @@ _kit_gc_prune_why() {
375
537
 
376
538
  # kit_gc_prune [--yes] [--only <regex>] - remove worktrees + local branches whose PR is MERGED (the
377
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.
378
541
  # DRY-RUN by default (lists what it WOULD remove); --yes performs the deletions. Never touches a
379
542
  # PROTECTED/ACTIVE/ORPHAN branch, and never a DIRTY worktree (recover-before-prune): a worktree with
380
543
  # staged/unstaged/untracked changes is skipped with a warning, not destroyed. The remote branch is
@@ -422,6 +585,28 @@ kit_gc_prune() {
422
585
  fi
423
586
  done
424
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
+
425
610
  # recover-before-prune (#111): a zombie worktree (working dir gone, admin metadata lingers) may
426
611
  # hold staged-but-uncommitted work in its admin index — the ONLY blob->path map. `git worktree
427
612
  # prune` deletes that index, orphaning the blobs for a later `git gc` to reap — real data loss in a
@@ -488,6 +673,8 @@ kit_gc_cleanup() {
488
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
489
674
  local zname zbranch zstaged
490
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
491
678
  for a in "$@"; do case "$a" in --yes|-y) yes=1 ;; esac; done
492
679
 
493
680
  out="$(kit_gc_analyze)" || return 1
@@ -498,6 +685,7 @@ kit_gc_cleanup() {
498
685
  in_s && / -> LANDED / { sub(/^ /, ""); sub(/ -> LANDED /, " "); print }')"
499
686
  orphan_list="$(_kit_gc_names "$out" branches ' -> ORPHAN')"
500
687
  prot_list="$(_kit_gc_names "$out" branches ' -> PROTECTED:')"
688
+ unknown_list="$(_kit_gc_names "$out" branches ' -> UNKNOWN ')"
501
689
  stash_list="$(printf '%s\n' "$out" | awk '/^# /{in_s=($0=="# stashes"); next} in_s && NF' | sed 's/^ //')"
502
690
 
503
691
  # Worktrees kit_gc_prune would remove: the ones whose branch is itself SAFE or LANDED (the same
@@ -506,8 +694,8 @@ kit_gc_cleanup() {
506
694
  | awk '/^worktree /{w=$2} /^branch /{print w" "$2}' \
507
695
  | while read -r wtpath ref; do
508
696
  b="${ref#refs/heads/}"
509
- printf '%s\n' "$safe_list" | grep -qxF "$b" \
510
- || 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
511
699
  if [ -n "$(git -C "$wtpath" status --porcelain 2>/dev/null)" ]; then
512
700
  echo "$wtpath [$b] (DIRTY — will be skipped, not destroyed)"
513
701
  else
@@ -515,13 +703,28 @@ kit_gc_cleanup() {
515
703
  fi
516
704
  done)"
517
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
+
518
719
  # ZOMBIE worktrees (dir gone, admin metadata lingers). kit_gc_prune --yes acts on these two ways
519
720
  # — kit_gc_recover_zombies commits any staged delta to the branch, then `git worktree prune`
520
721
  # drops the metadata — so the plan MUST name them or --yes would touch a row nobody approved
521
722
  # (#227 review). Same helper as the analyze report, so the two cannot diverge.
522
723
  zomb_list="$(while IFS="$(printf '\t')" read -r zname zbranch zstaged; do
523
724
  [ -n "$zname" ] || continue
524
- 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
525
728
  echo "$zname [$zbranch] (STAGED work — recovered to a commit on $zbranch, then metadata pruned)"
526
729
  else
527
730
  echo "$zname [$zbranch] (no staged delta — metadata pruned)"
@@ -535,9 +738,11 @@ ZEOF
535
738
  n_landed="$(printf '%s' "$landed_list" | grep -c . || true)"
536
739
  n_orphan="$(printf '%s' "$orphan_list" | grep -c . || true)"
537
740
  n_prot="$(printf '%s' "$prot_list" | grep -c . || true)"
741
+ n_unknown="$(printf '%s' "$unknown_list" | grep -c . || true)"
538
742
  n_stash="$(printf '%s' "$stash_list" | grep -c . || true)"
539
743
  n_wt="$(printf '%s' "$wt_list" | grep -c . || true)"
540
744
  n_zomb="$(printf '%s' "$zomb_list" | grep -c . || true)"
745
+ n_zkept="$(printf '%s' "$zomb_list" | grep -c 'kept, not pruned' || true)"
541
746
 
542
747
  echo "# cleanup plan"
543
748
  printf ' SAFE branches %3s merged PR, issue closed/absent\n' "$n_safe"
@@ -552,6 +757,8 @@ ZEOF
552
757
  printf '%s\n' "$orphan_list" | grep . | sed 's/^/ /' || true
553
758
  printf ' PROTECTED kept %3s issue still OPEN\n' "$n_prot"
554
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
555
762
  printf ' stashes kept %3s irreversible — drop by hand after reading each diff\n' "$n_stash"
556
763
  printf '%s\n' "$stash_list" | grep . | sed 's/^/ /' || true
557
764
 
@@ -595,6 +802,6 @@ EOF
595
802
  $level_ok
596
803
  EOF
597
804
  printf ' remote deletes failed %3s\n' "$n_rfail"
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."
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."
599
806
  return "$rc"
600
807
  }