@jeiemgi/cckit 0.11.0 → 0.13.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 (549) hide show
  1. package/.claude-plugin/plugin.json +4 -2
  2. package/AGENTS.md +22 -238
  3. package/README.md +66 -56
  4. package/SECURITY.md +4 -4
  5. package/bin/PROMPTS.md +12 -0
  6. package/bin/cckit +181 -140
  7. package/cckit.config.json +1 -1
  8. package/kit/PROMPTS.md +14 -0
  9. package/{commands → kit/commands}/kit-doctor.md +3 -4
  10. package/{commands → kit/commands}/kit-export-project.md +1 -1
  11. package/{commands → kit/commands}/kit-init.md +8 -20
  12. package/{commands → kit/commands}/kit-routines.md +1 -1
  13. package/{commands → kit/commands}/kit-update.md +3 -3
  14. package/kit/completions/cckit.bash +9 -0
  15. package/{databases → kit/databases}/supabase.json +1 -1
  16. package/{integrations → kit/integrations}/integration.schema.json +3 -3
  17. package/kit/interview/global.json +54 -0
  18. package/kit/interview/project.json +133 -0
  19. package/kit/modules/qa.json +63 -0
  20. package/kit/modules/software.json +48 -0
  21. package/{profiles → kit/profiles}/automation.json +1 -3
  22. package/{profiles → kit/profiles}/content.json +0 -2
  23. package/{profiles → kit/profiles}/marketing.json +0 -2
  24. package/{profiles → kit/profiles}/minimal.json +1 -3
  25. package/{profiles → kit/profiles}/research.json +1 -3
  26. package/{profiles → kit/profiles}/software.json +0 -2
  27. package/kit/routines/catalogue.json +84 -0
  28. package/{skills → kit/skills}/kit-add/SKILL.md +4 -4
  29. package/kit/skills/kit-advisor/SKILL.md +75 -0
  30. package/kit/skills/kit-advisor/references/close.md +53 -0
  31. package/kit/skills/kit-advisor/references/grill.md +69 -0
  32. package/kit/skills/kit-advisor/references/orchestrate.md +56 -0
  33. package/kit/skills/kit-advisor/references/plan.md +64 -0
  34. package/kit/skills/kit-advisor/references/review.md +47 -0
  35. package/kit/skills/kit-advisor/references/step-report.md +61 -0
  36. package/{skills → kit/skills}/kit-annotate/SKILL.md +2 -4
  37. package/{skills → kit/skills}/kit-autopilot/SKILL.md +7 -7
  38. package/{skills → kit/skills}/kit-cleanup/SKILL.md +4 -4
  39. package/{skills → kit/skills}/kit-contribute/SKILL.md +8 -12
  40. package/{skills → kit/skills}/kit-customize/SKILL.md +5 -8
  41. package/{skills → kit/skills}/kit-dev/SKILL.md +1 -1
  42. package/{skills → kit/skills}/kit-digest/SKILL.md +3 -3
  43. package/{skills → kit/skills}/kit-effort-close/SKILL.md +20 -12
  44. package/{skills → kit/skills}/kit-effort-new/SKILL.md +17 -14
  45. package/{skills → kit/skills}/kit-effort-pr/SKILL.md +7 -7
  46. package/{skills → kit/skills}/kit-effort-start/SKILL.md +3 -3
  47. package/{skills → kit/skills}/kit-gc/SKILL.md +3 -3
  48. package/{skills → kit/skills}/kit-next/SKILL.md +7 -7
  49. package/{skills → kit/skills}/kit-page/SKILL.md +1 -1
  50. package/{skills → kit/skills}/kit-plan-next/SKILL.md +10 -11
  51. package/{skills → kit/skills}/kit-status/SKILL.md +6 -6
  52. package/{skills → kit/skills}/kit-task-close/SKILL.md +5 -5
  53. package/{skills → kit/skills}/kit-task-new/SKILL.md +5 -5
  54. package/{skills → kit/skills}/kit-task-pr/SKILL.md +5 -5
  55. package/{skills → kit/skills}/kit-task-pr-auto/SKILL.md +1 -1
  56. package/{skills → kit/skills}/kit-task-pr-merge/SKILL.md +10 -10
  57. package/{skills → kit/skills}/kit-task-start/SKILL.md +1 -1
  58. package/{skills → kit/skills}/kit-task-sync/SKILL.md +5 -3
  59. package/{skills → kit/skills}/kit-ux-audit/SKILL.md +16 -35
  60. package/{skills → kit/skills}/kit-ux-audit/references/graph-model.md +7 -26
  61. package/{skills → kit/skills}/kit-ux-audit/references/report.md +2 -3
  62. package/{skills → kit/skills}/routing.yaml +6 -1
  63. package/kit/statusline/kit-statusline.sh +82 -0
  64. package/{templates → kit/templates}/agents/analyst.md +0 -11
  65. package/{templates → kit/templates}/agents/auto-dev.md +0 -13
  66. package/{templates → kit/templates}/agents/backend.md +0 -11
  67. package/{templates → kit/templates}/agents/designer.md +0 -11
  68. package/{templates → kit/templates}/agents/devops.md +0 -11
  69. package/{templates → kit/templates}/agents/editor.md +0 -11
  70. package/{templates → kit/templates}/agents/frontend.md +0 -11
  71. package/{templates → kit/templates}/agents/generalist.md +0 -11
  72. package/{templates → kit/templates}/agents/local-delegate.md +0 -13
  73. package/{templates → kit/templates}/agents/n8n.md +0 -11
  74. package/{templates → kit/templates}/agents/pm.md +0 -11
  75. package/{templates → kit/templates}/agents/qa.md +0 -11
  76. package/{templates → kit/templates}/agents/researcher.md +0 -11
  77. package/{templates → kit/templates}/agents/security.md +0 -11
  78. package/{templates → kit/templates}/agents/tech-lead.md +0 -11
  79. package/{templates → kit/templates}/agents/ux.md +3 -14
  80. package/{templates → kit/templates}/hooks/kit-mail-check.sh.tmpl +1 -1
  81. package/{templates → kit/templates}/instructions.md.tmpl +4 -21
  82. package/{templates → kit/templates}/kit.config.json.tmpl +0 -6
  83. package/{templates → kit/templates}/rules/authoring.md +7 -3
  84. package/{templates → kit/templates}/rules/branch-naming.md +3 -2
  85. package/{templates → kit/templates}/rules/delegation-brief.md +4 -0
  86. package/{templates → kit/templates}/rules/effort-model.md +34 -31
  87. package/{templates → kit/templates}/rules/gate-and-review.md +1 -1
  88. package/{templates → kit/templates}/rules/merge-safety.md +12 -9
  89. package/{templates → kit/templates}/rules/naming-and-ids.md +20 -24
  90. package/{templates → kit/templates}/rules/plan-next.md +5 -5
  91. package/{templates → kit/templates}/rules/react-annotate.md +0 -8
  92. package/{templates → kit/templates}/skills/campaign/SKILL.md +2 -2
  93. package/kit.config.schema.json +11 -11
  94. package/lib/PROMPTS.md +14 -0
  95. package/{scripts/lib → lib/core}/adapter-select-test.sh +7 -7
  96. package/lib/core/advisor-test.sh +55 -0
  97. package/{scripts/lib → lib/core}/agent-def-test.sh +1 -1
  98. package/{scripts/lib → lib/core}/agent-def.sh +2 -2
  99. package/{scripts/lib → lib/core}/agent-launch-test.sh +3 -3
  100. package/{scripts/lib → lib/core}/agent-profile-test.sh +2 -2
  101. package/{scripts/lib → lib/core}/agent-resolve-test.sh +4 -2
  102. package/{scripts/lib → lib/core}/agent-resolve.sh +5 -4
  103. package/{scripts → lib/core}/agent-skills-external.txt +3 -3
  104. package/{scripts → lib/core}/agent-skills-lint-test.sh +17 -17
  105. package/lib/core/agent-skills-lint.sh +115 -0
  106. package/{scripts → lib/core}/agent-template-lint-test.sh +7 -7
  107. package/lib/core/agent-template-lint.sh +58 -0
  108. package/{scripts/lib → lib/core}/board-view-test.sh +3 -3
  109. package/lib/core/board-view.sh +97 -0
  110. package/{scripts/lib → lib/core}/branch-owner-test.sh +3 -3
  111. package/lib/core/branch-owner.sh +201 -0
  112. package/lib/core/check.sh +125 -0
  113. package/{scripts/lib → lib/core}/codex-hooks-test.sh +3 -3
  114. package/{scripts → lib/core}/commitlint-test.sh +6 -6
  115. package/lib/core/commitlint.sh +54 -0
  116. package/{scripts/config-path-test.sh → lib/core/config-path-scan-test.sh} +2 -2
  117. package/{scripts/lib → lib/core}/config-path-test.sh +3 -3
  118. package/lib/core/config-path.sh +30 -0
  119. package/{scripts → lib/core}/content-skills-test.sh +17 -17
  120. package/{scripts → lib/core}/docs-marketing-words-test.sh +2 -2
  121. package/{scripts → lib/core}/docs-record-tables-test.sh +2 -2
  122. package/{scripts/lib → lib/core}/effort-metrics-test.sh +6 -6
  123. package/lib/core/effort-metrics.sh +591 -0
  124. package/{scripts/lib → lib/core}/effort-slug-test.sh +8 -4
  125. package/{scripts/lib → lib/core}/effort-slug.sh +8 -7
  126. package/lib/core/effort.sh +339 -0
  127. package/{scripts/lib → lib/core}/errors-header-test.sh +19 -9
  128. package/lib/core/errors-header.exempt +49 -0
  129. package/{scripts → lib/core}/exec-targets-test.sh +3 -3
  130. package/lib/core/feature-map.sh +76 -0
  131. package/lib/core/force-push-fence-test.sh +44 -0
  132. package/lib/core/force-push-fence.baseline +1 -0
  133. package/lib/core/force-push-fence.sh +51 -0
  134. package/{scripts → lib/core}/forge-fence-test.sh +13 -13
  135. package/{scripts → lib/core}/forge-fence.sh +14 -14
  136. package/{scripts → lib/core}/forge-report-test.sh +5 -5
  137. package/{scripts → lib/core}/forge-report.sh +18 -14
  138. package/{scripts/lib → lib/core}/git-core-test.sh +7 -7
  139. package/{scripts/lib → lib/core}/git-remote-test.sh +3 -3
  140. package/{scripts → lib/core}/guard-raw-git-ops-test.sh +4 -4
  141. package/{scripts → lib/core}/hook-adapter-test.sh +5 -5
  142. package/{scripts/lib → lib/core}/hook-refresh-test.sh +5 -5
  143. package/{scripts → lib/core}/instruction-budget-baseline.txt +2 -2
  144. package/lib/core/instruction-budget-lint.sh +238 -0
  145. package/{scripts/lib → lib/core}/integration-install-test.sh +12 -12
  146. package/lib/core/integration-install.sh +257 -0
  147. package/{scripts/lib → lib/core}/integrations-test.sh +38 -45
  148. package/lib/core/integrations.sh +244 -0
  149. package/{scripts/lib → lib/core}/kinds-test.sh +5 -5
  150. package/lib/core/kinds.sh +66 -0
  151. package/{scripts/lib → lib/core}/kit-cli-test.sh +1 -1
  152. package/{scripts/lib → lib/core}/kit-cli.sh +1 -1
  153. package/{scripts/lib → lib/core}/kit-config-resolve.sh +2 -2
  154. package/{scripts/lib → lib/core}/kit-config-test.sh +11 -11
  155. package/lib/core/kit-config.sh +164 -0
  156. package/{scripts/lib → lib/core}/kit-engine-test.sh +1 -1
  157. package/lib/core/kit-events.sh +72 -0
  158. package/lib/core/kit-local.sh +87 -0
  159. package/{scripts/lib → lib/core}/kit-manifest.sh +1 -1
  160. package/{scripts/lib → lib/core}/kit-mode-test.sh +1 -1
  161. package/{scripts/lib → lib/core}/kit-mode.sh +3 -3
  162. package/{scripts/lib → lib/core}/kit-operate.sh +1 -1
  163. package/{scripts/lib → lib/core}/kit-profile-test.sh +1 -1
  164. package/{scripts/lib → lib/core}/kit-profile.sh +2 -2
  165. package/{scripts/lib → lib/core}/kit-state-test.sh +1 -1
  166. package/lib/core/knowledge-lint.sh +106 -0
  167. package/lib/core/layout-fence-test.sh +35 -0
  168. package/lib/core/layout-fence.allow +15 -0
  169. package/lib/core/layout-fence.baseline +4 -0
  170. package/lib/core/layout-fence.sh +50 -0
  171. package/{scripts → lib/core}/leak-fence-test.sh +10 -10
  172. package/{scripts/lib → lib/core}/leak-fence.sh +1 -1
  173. package/lib/core/loose-issues-test.sh +102 -0
  174. package/lib/core/loose-issues.baseline +5 -0
  175. package/lib/core/loose-issues.sh +65 -0
  176. package/{scripts → lib/core}/marketing-walkthrough-test.sh +7 -7
  177. package/lib/core/naming-test.sh +112 -0
  178. package/lib/core/naming-verbs-test.sh +126 -0
  179. package/lib/core/naming.sh +172 -0
  180. package/{scripts/lib → lib/core}/orchestration-runtime-test.sh +4 -4
  181. package/{scripts → lib/core}/portable-test.sh +11 -7
  182. package/{scripts/lib → lib/core}/pr-evidence-test.sh +5 -5
  183. package/lib/core/pr-evidence.sh +361 -0
  184. package/{scripts/lib → lib/core}/qa-lane-test.sh +7 -7
  185. package/{scripts/lib → lib/core}/qa-lane.sh +4 -4
  186. package/{scripts/lib → lib/core}/qa-lanes-test.sh +5 -5
  187. package/lib/core/react-detect.sh +79 -0
  188. package/{scripts/lib → lib/core}/render-test.sh +1 -1
  189. package/lib/core/resident-context-test.sh +42 -0
  190. package/lib/core/resident-context.baseline +1 -0
  191. package/lib/core/resident-context.sh +56 -0
  192. package/lib/core/review-lint-baseline.txt +28 -0
  193. package/lib/core/review-lint.sh +266 -0
  194. package/{scripts → lib/core}/review-rules.conf +1 -1
  195. package/{scripts/lib → lib/core}/review-stage-test.sh +2 -2
  196. package/{scripts/lib → lib/core}/role-identity-test.sh +3 -3
  197. package/lib/core/role-identity.sh +71 -0
  198. package/{scripts → lib/core}/rule-recipes-test.sh +5 -5
  199. package/lib/core/secret-guard.sh +116 -0
  200. package/{scripts → lib/core}/self-install-test.sh +15 -16
  201. package/{scripts/lib → lib/core}/session-registry-test.sh +1 -1
  202. package/lib/core/session-registry.sh +233 -0
  203. package/{scripts → lib/core}/skill-frontmatter-baseline.txt +1 -1
  204. package/lib/core/skill-frontmatter-lint.sh +189 -0
  205. package/lib/core/spec-lint-test.sh +63 -0
  206. package/lib/core/spec-lint.sh +95 -0
  207. package/{scripts/lib → lib/core}/stage-receipt-test.sh +136 -4
  208. package/{scripts/lib → lib/core}/stage-receipt.sh +186 -7
  209. package/{scripts → lib/core}/terms-check-test.sh +6 -6
  210. package/lib/core/terms-check.sh +244 -0
  211. package/{scripts/lib → lib/core}/test-env-test.sh +2 -2
  212. package/{scripts/lib → lib/core}/test-env.sh +1 -1
  213. package/{scripts → lib/core}/test-runner-test.sh +11 -11
  214. package/{scripts → lib/core}/test.sh +12 -12
  215. package/{scripts → lib/core}/update-path-test.sh +3 -3
  216. package/{scripts → lib/core}/verbs-gen.sh +9 -9
  217. package/{scripts → lib/core}/verbs-help-test.sh +4 -4
  218. package/{scripts → lib/core}/verbs-ratchet-test.sh +24 -19
  219. package/{scripts → lib/core}/verbs-ratchet.sh +8 -8
  220. package/{scripts → lib/core}/verbs-test.sh +23 -23
  221. package/lib/core/verbs.baseline +5 -0
  222. package/{scripts/lib → lib/core}/verbs.sh +3 -3
  223. package/{scripts/lib → lib/core}/verbs.tsv +22 -20
  224. package/{scripts/lib → lib/core}/version-bump-test.sh +29 -8
  225. package/{scripts/lib → lib/core}/version-bump.sh +15 -2
  226. package/{scripts/lib → lib/core}/wave-result-test.sh +2 -2
  227. package/{scripts/lib → lib/core}/wave-result.sh +1 -1
  228. package/{scripts/lib → lib/core}/worker-brief-test.sh +4 -4
  229. package/{scripts/lib → lib/core}/worker-brief.sh +1 -1
  230. package/{scripts/lib → lib/core}/worktree-bootstrap-test.sh +1 -1
  231. package/{scripts/lib → lib/core}/worktree-issue-test.sh +2 -2
  232. package/lib/core/worktree-issue.sh +172 -0
  233. package/{scripts/lib → lib/core}/worktree-ports-test.sh +1 -1
  234. package/{scripts/lib → lib/core}/worktree-start-test.sh +2 -2
  235. package/lib/core/worktree-start.sh +790 -0
  236. package/{scripts/lib → lib/core}/zsh-safety-test.sh +11 -11
  237. package/{scripts/lib → lib/forge}/forge-github.sh +18 -7
  238. package/{scripts/lib → lib/forge}/forge-test.sh +4 -4
  239. package/{scripts/lib → lib/forge}/forge.sh +1 -1
  240. package/lib/forge/gh-board-add-test.sh +118 -0
  241. package/lib/forge/gh-log.sh +59 -0
  242. package/lib/forge/gh-project-test.sh +251 -0
  243. package/lib/forge/gh-project.sh +379 -0
  244. package/{scripts/lib → lib/forge}/gh-rest-test.sh +17 -13
  245. package/{scripts/lib → lib/forge}/gh-rest.sh +190 -16
  246. package/{scripts/lib → lib/forge}/init-github.sh +18 -8
  247. package/{scripts/lib → lib/forge}/test-forge-shim.sh +2 -1
  248. package/lib/verbs/PROMPTS.md +146 -0
  249. package/lib/verbs/annotate-setup.sh +149 -0
  250. package/{scripts → lib/verbs}/autopilot-run-test.sh +9 -9
  251. package/{scripts → lib/verbs}/autopilot-run.sh +9 -9
  252. package/{scripts → lib/verbs}/autopilot-test.sh +7 -6
  253. package/{scripts → lib/verbs}/autopilot.sh +7 -7
  254. package/{scripts/lib → lib/verbs}/captain-test.sh +1 -1
  255. package/{scripts/lib → lib/verbs}/captain.sh +8 -8
  256. package/lib/verbs/capture-project-ids.sh +99 -0
  257. package/{scripts/lib → lib/verbs}/content-kinds-gc-test.sh +7 -7
  258. package/{scripts/lib → lib/verbs}/contribute-root-test.sh +4 -4
  259. package/{scripts/lib → lib/verbs}/contribute-root.sh +1 -1
  260. package/{scripts → lib/verbs}/contribute.sh +8 -8
  261. package/{scripts → lib/verbs}/copilot-loop-test.sh +12 -12
  262. package/{scripts/lib → lib/verbs}/copilot-test.sh +15 -14
  263. package/{scripts/lib → lib/verbs}/copilot.sh +19 -19
  264. package/{scripts → lib/verbs}/database-supabase-test.sh +6 -6
  265. package/lib/verbs/effort-close-subs-test.sh +96 -0
  266. package/{scripts/lib → lib/verbs}/effort-dispatch-test.sh +5 -5
  267. package/{scripts/lib → lib/verbs}/effort-flows-test.sh +5 -5
  268. package/{scripts/lib → lib/verbs}/effort-ops-test.sh +111 -22
  269. package/lib/verbs/effort-ops.sh +1571 -0
  270. package/{scripts/lib → lib/verbs}/effort-parity-test.sh +47 -41
  271. package/{scripts/lib → lib/verbs}/effort-plan-test.sh +7 -7
  272. package/lib/verbs/effort-plan.sh +289 -0
  273. package/lib/verbs/effort-review-test.sh +107 -0
  274. package/lib/verbs/effort-review.sh +93 -0
  275. package/lib/verbs/grill-record-test.sh +49 -0
  276. package/lib/verbs/grill-record.sh +88 -0
  277. package/{scripts/lib → lib/verbs}/handoff.sh +3 -3
  278. package/{scripts → lib/verbs}/init-content-test.sh +14 -14
  279. package/{scripts/lib → lib/verbs}/init-guide.sh +6 -6
  280. package/{scripts → lib/verbs}/init-guided-test.sh +9 -9
  281. package/{scripts → lib/verbs}/init-recipes-test.sh +7 -7
  282. package/{scripts → lib/verbs}/init-upgrade-test.sh +9 -10
  283. package/lib/verbs/init.sh +1598 -0
  284. package/{scripts → lib/verbs}/install-test.sh +5 -5
  285. package/lib/verbs/install.sh +31 -0
  286. package/{scripts → lib/verbs}/integrations-check-test.sh +9 -9
  287. package/lib/verbs/integrations-check.sh +157 -0
  288. package/{scripts → lib/verbs}/kit-add-test.sh +8 -8
  289. package/lib/verbs/kit-add.sh +415 -0
  290. package/{scripts → lib/verbs}/kit-adopt-test.sh +4 -4
  291. package/{scripts → lib/verbs}/kit-adopt.sh +6 -6
  292. package/{scripts/lib → lib/verbs}/kit-brief-test.sh +44 -15
  293. package/{scripts/lib → lib/verbs}/kit-brief.sh +101 -16
  294. package/lib/verbs/kit-bump-version.sh +79 -0
  295. package/lib/verbs/kit-digest.sh +126 -0
  296. package/{scripts → lib/verbs}/kit-doctor-test.sh +57 -32
  297. package/lib/verbs/kit-doctor.sh +991 -0
  298. package/{scripts → lib/verbs}/kit-export-project-test.sh +4 -4
  299. package/lib/verbs/kit-export-project.sh +245 -0
  300. package/{scripts → lib/verbs}/kit-export-training-test.sh +3 -3
  301. package/lib/verbs/kit-export-training.sh +175 -0
  302. package/{scripts/lib → lib/verbs}/kit-gc-test.sh +5 -5
  303. package/lib/verbs/kit-gc.sh +804 -0
  304. package/{scripts/lib → lib/verbs}/kit-hotfix.sh +3 -3
  305. package/{scripts/lib → lib/verbs}/kit-interview-test.sh +2 -2
  306. package/lib/verbs/kit-interview.sh +319 -0
  307. package/{scripts/lib → lib/verbs}/kit-lib-test.sh +2 -2
  308. package/{scripts/lib → lib/verbs}/kit-lib.sh +10 -10
  309. package/{scripts → lib/verbs}/kit-migrate-test.sh +4 -4
  310. package/{scripts → lib/verbs}/kit-migrate.sh +5 -5
  311. package/{scripts/lib → lib/verbs}/kit-msg-test.sh +2 -2
  312. package/{scripts/lib → lib/verbs}/kit-msg.sh +5 -5
  313. package/{scripts → lib/verbs}/kit-onboard-test.sh +2 -3
  314. package/lib/verbs/kit-onboard.sh +69 -0
  315. package/{scripts → lib/verbs}/kit-promote-test.sh +4 -4
  316. package/{scripts → lib/verbs}/kit-promote.sh +4 -4
  317. package/{scripts → lib/verbs}/kit-remove-test.sh +4 -4
  318. package/{scripts → lib/verbs}/kit-remove.sh +5 -5
  319. package/lib/verbs/kit-routines.sh +322 -0
  320. package/lib/verbs/kit-self-update.sh +55 -0
  321. package/{scripts/lib → lib/verbs}/kit-status-test.sh +2 -2
  322. package/lib/verbs/kit-status.sh +650 -0
  323. package/{scripts/lib → lib/verbs}/kit-task-ops-test.sh +25 -2
  324. package/lib/verbs/kit-task-ops.sh +793 -0
  325. package/{scripts → lib/verbs}/kit-version-check-test.sh +3 -3
  326. package/lib/verbs/kit-version-check.sh +121 -0
  327. package/{scripts → lib/verbs}/kit-wire-test.sh +6 -6
  328. package/{scripts → lib/verbs}/kit-wire.sh +8 -8
  329. package/{scripts/lib → lib/verbs}/kto-hotfix-test.sh +3 -3
  330. package/{scripts/lib → lib/verbs}/kto-task-close-test.sh +2 -2
  331. package/{scripts/lib → lib/verbs}/kto-task-new-test.sh +20 -2
  332. package/{scripts/lib → lib/verbs}/kto-task-pr-merge-test.sh +46 -2
  333. package/{scripts/lib → lib/verbs}/kto-task-pr-test.sh +6 -6
  334. package/{scripts/lib → lib/verbs}/light-task-run-test.sh +4 -4
  335. package/{scripts/lib → lib/verbs}/light-task-test.sh +9 -9
  336. package/{scripts/lib → lib/verbs}/light-task.sh +26 -11
  337. package/{scripts/lib → lib/verbs}/next-test.sh +3 -3
  338. package/{scripts/lib → lib/verbs}/next.sh +10 -10
  339. package/{scripts → lib/verbs}/night-grant-test.sh +4 -4
  340. package/lib/verbs/night-grant.sh +454 -0
  341. package/{scripts → lib/verbs}/night-run-test.sh +3 -3
  342. package/{scripts → lib/verbs}/night-run.mjs +2 -2
  343. package/{scripts → lib/verbs}/orchestrate-test.sh +5 -5
  344. package/{scripts → lib/verbs}/orchestrate.sh +15 -15
  345. package/{scripts/lib → lib/verbs}/plan-machine-test.sh +1 -1
  346. package/{scripts/lib → lib/verbs}/plan-machine.sh +13 -13
  347. package/{scripts/lib → lib/verbs}/plan-next-test.sh +1 -1
  348. package/lib/verbs/plan-next.sh +213 -0
  349. package/{scripts → lib/verbs}/publish.sh +10 -10
  350. package/{scripts/lib → lib/verbs}/review-page-test.sh +4 -4
  351. package/lib/verbs/review-page.sh +375 -0
  352. package/lib/verbs/setup-labels.sh +96 -0
  353. package/lib/verbs/setup-milestones.sh +34 -0
  354. package/{scripts → lib/verbs}/showcase.sh +2 -2
  355. package/lib/verbs/sprint-test.sh +96 -0
  356. package/lib/verbs/sprint.sh +216 -0
  357. package/{scripts → lib/verbs}/status-test.sh +8 -8
  358. package/{scripts → lib/verbs}/status.sh +16 -16
  359. package/{scripts → lib/verbs}/task-sync-test.sh +40 -6
  360. package/lib/verbs/task-sync.sh +462 -0
  361. package/{scripts/lib → lib/verbs}/upgrade-report-test.sh +4 -4
  362. package/lib/verbs/ux-a11y-scan.sh +96 -0
  363. package/lib/verbs/ux-ds-audit.sh +146 -0
  364. package/lib/verbs/ux-graph-scan.sh +188 -0
  365. package/{scripts → lib/verbs}/wave-worker.sh +2 -2
  366. package/lib/verbs/web-install.sh +22 -0
  367. package/package.json +3 -10
  368. package/scripts/PROMPTS.md +13 -0
  369. package/scripts/agent-skills-lint.sh +12 -113
  370. package/scripts/agent-template-lint.sh +12 -56
  371. package/scripts/annotate-setup.sh +11 -151
  372. package/scripts/benchmark/checkpoint-test.sh +1 -1
  373. package/scripts/benchmark/checkpoint.sh +1 -1
  374. package/scripts/benchmark/gate.sh +2 -2
  375. package/scripts/benchmark/lib/bench-common.sh +1 -1
  376. package/scripts/capture-project-ids.sh +12 -70
  377. package/scripts/check.sh +12 -106
  378. package/scripts/force-push-fence.sh +14 -0
  379. package/scripts/init.sh +13 -1637
  380. package/scripts/install.sh +12 -29
  381. package/scripts/instruction-budget-lint.sh +13 -218
  382. package/scripts/integrations-check.sh +12 -155
  383. package/scripts/kit-add.sh +12 -413
  384. package/scripts/kit-bump-version.sh +13 -78
  385. package/scripts/kit-digest.sh +13 -125
  386. package/scripts/kit-doctor.sh +13 -971
  387. package/scripts/kit-export-project.sh +12 -243
  388. package/scripts/kit-export-training.sh +12 -173
  389. package/scripts/kit-onboard.sh +12 -67
  390. package/scripts/kit-routines.sh +12 -320
  391. package/scripts/kit-self-update.sh +12 -53
  392. package/scripts/kit-version-check.sh +12 -106
  393. package/scripts/knowledge-lint.sh +13 -98
  394. package/scripts/lib/board-view.sh +13 -96
  395. package/scripts/lib/branch-owner.sh +12 -199
  396. package/scripts/lib/commitlint.sh +12 -52
  397. package/scripts/lib/config-path.sh +13 -29
  398. package/scripts/lib/effort-metrics.sh +12 -589
  399. package/scripts/lib/effort-ops.sh +13 -1243
  400. package/scripts/lib/effort-plan.sh +12 -287
  401. package/scripts/lib/effort.sh +13 -365
  402. package/scripts/lib/gh-log.sh +14 -59
  403. package/scripts/lib/gh-project.sh +13 -254
  404. package/scripts/lib/grill-record.sh +14 -0
  405. package/scripts/lib/integration-install.sh +12 -255
  406. package/scripts/lib/integrations.sh +12 -242
  407. package/scripts/lib/kinds.sh +13 -65
  408. package/scripts/lib/kit-config.sh +12 -165
  409. package/scripts/lib/kit-events.sh +13 -71
  410. package/scripts/lib/kit-gc.sh +12 -802
  411. package/scripts/lib/kit-interview.sh +11 -316
  412. package/scripts/lib/kit-local.sh +12 -85
  413. package/scripts/lib/kit-status.sh +12 -648
  414. package/scripts/lib/kit-task-ops.sh +13 -738
  415. package/scripts/lib/naming.sh +14 -0
  416. package/scripts/lib/plan-next.sh +13 -212
  417. package/scripts/lib/pr-evidence.sh +12 -359
  418. package/scripts/lib/react-detect.sh +12 -77
  419. package/scripts/lib/review-page.sh +11 -372
  420. package/scripts/lib/role-identity.sh +13 -70
  421. package/scripts/lib/secret-guard.sh +12 -114
  422. package/scripts/lib/session-registry.sh +13 -232
  423. package/scripts/lib/spec-lint.sh +14 -0
  424. package/scripts/lib/worktree-issue.sh +12 -170
  425. package/scripts/lib/worktree-start.sh +13 -748
  426. package/scripts/night-grant.sh +12 -452
  427. package/scripts/review-lint.sh +13 -258
  428. package/scripts/setup-labels.sh +11 -82
  429. package/scripts/setup-milestones.sh +12 -21
  430. package/scripts/skill-frontmatter-lint.sh +13 -181
  431. package/scripts/task-sync.sh +13 -370
  432. package/scripts/terms-check.sh +13 -241
  433. package/scripts/ux-a11y-scan.sh +13 -95
  434. package/scripts/ux-ds-audit.sh +12 -144
  435. package/scripts/ux-graph-scan.sh +12 -186
  436. package/scripts/web-install.sh +16 -20
  437. package/docs/kit-annotate/01-explainer.html +0 -225
  438. package/docs/kit-annotate/02-implementation-plan.html +0 -196
  439. package/docs/media/.onboarding-capture.cast +0 -5
  440. package/docs/media/README.md +0 -43
  441. package/docs/media/build-demo.sh +0 -63
  442. package/docs/media/build-kit-init.sh +0 -51
  443. package/docs/media/build-onboarding.sh +0 -51
  444. package/docs/media/kit-dry-run.cast +0 -107
  445. package/docs/media/kit-dry-run.gif +0 -0
  446. package/docs/media/kit-init.cast +0 -56
  447. package/docs/media/kit-init.gif +0 -0
  448. package/docs/media/kit-onboarding.cast +0 -148
  449. package/docs/media/kit-onboarding.gif +0 -0
  450. package/integrations/mempalace.json +0 -28
  451. package/scripts/lib/gh-project-test.sh +0 -135
  452. package/scripts/review-lint-baseline.txt +0 -28
  453. package/scripts/verbs.baseline +0 -5
  454. package/templates/hooks/mempal_followup.sh.tmpl +0 -97
  455. package/templates/hooks/mempal_precompact.sh.tmpl +0 -4
  456. package/templates/hooks/mempal_save.sh.tmpl +0 -4
  457. package/templates/hooks/mempal_session_start.sh.tmpl +0 -42
  458. package/templates/rules/mempalace.md +0 -111
  459. /package/{commands → kit/commands}/kit-docs.md +0 -0
  460. /package/{commands → kit/commands}/kit-export-training.md +0 -0
  461. /package/{databases → kit/databases}/supabase/guide.html +0 -0
  462. /package/{databases → kit/databases}/supabase/nextjs/lib/supabase/client.ts +0 -0
  463. /package/{databases → kit/databases}/supabase/nextjs/lib/supabase/server.ts +0 -0
  464. /package/{githooks → kit/githooks}/pre-commit +0 -0
  465. /package/{integrations → kit/integrations}/emil-design-eng.json +0 -0
  466. /package/{integrations → kit/integrations}/graft.json +0 -0
  467. /package/{integrations → kit/integrations}/impeccable.json +0 -0
  468. /package/{integrations → kit/integrations}/lavish.json +0 -0
  469. /package/{integrations → kit/integrations}/mlx-lm.json +0 -0
  470. /package/{integrations → kit/integrations}/web-design-guidelines.json +0 -0
  471. /package/{recipes → kit/recipes}/nextjs-shadcn.json +0 -0
  472. /package/{skills → kit/skills}/kit-docs-writer/SKILL.md +0 -0
  473. /package/{skills → kit/skills}/kit-onboard/SKILL.md +0 -0
  474. /package/{skills → kit/skills}/kit-security-sweep/SKILL.md +0 -0
  475. /package/{skills → kit/skills}/kit-ship/SKILL.md +0 -0
  476. /package/{skills → kit/skills}/kit-ux-audit/references/a11y.md +0 -0
  477. /package/{skills → kit/skills}/kit-ux-audit/references/consistency.md +0 -0
  478. /package/{skills → kit/skills}/kit-ux-audit/references/heuristics.md +0 -0
  479. /package/{templates → kit/templates}/benchmarks/SCHEMA.md +0 -0
  480. /package/{templates → kit/templates}/benchmarks/abstain.jsonl +0 -0
  481. /package/{templates → kit/templates}/benchmarks/adversarial.jsonl +0 -0
  482. /package/{templates → kit/templates}/benchmarks/benchmark.config.json +0 -0
  483. /package/{templates → kit/templates}/benchmarks/quality.jsonl +0 -0
  484. /package/{templates → kit/templates}/benchmarks/retrieval.jsonl +0 -0
  485. /package/{templates → kit/templates}/codex/hooks.json.tmpl +0 -0
  486. /package/{templates → kit/templates}/hooks/guard-base-branch-commit.sh.tmpl +0 -0
  487. /package/{templates → kit/templates}/hooks/guard-raw-git-ops.sh.tmpl +0 -0
  488. /package/{templates → kit/templates}/hooks/kit-local-status.sh.tmpl +0 -0
  489. /package/{templates → kit/templates}/hooks/kit_version_check.sh.tmpl +0 -0
  490. /package/{templates → kit/templates}/hooks/prepush_gate.sh.tmpl +0 -0
  491. /package/{templates → kit/templates}/hooks/repo-hygiene.sh.tmpl +0 -0
  492. /package/{templates → kit/templates}/knowledge-INDEX.md.tmpl +0 -0
  493. /package/{templates → kit/templates}/lib/kit-sigil.sh.tmpl +0 -0
  494. /package/{templates → kit/templates}/rules/code-retrieval.md +0 -0
  495. /package/{templates → kit/templates}/rules/communication-style.md +0 -0
  496. /package/{templates → kit/templates}/rules/design-routing.md +0 -0
  497. /package/{templates → kit/templates}/rules/issue-lifecycle.md +0 -0
  498. /package/{templates → kit/templates}/rules/knowledge-base.md +0 -0
  499. /package/{templates → kit/templates}/rules/plain-language.md +0 -0
  500. /package/{templates → kit/templates}/rules/plan-output-format.md +0 -0
  501. /package/{templates → kit/templates}/rules/review-pages.md +0 -0
  502. /package/{templates → kit/templates}/rules/risk-tiered-review.md +0 -0
  503. /package/{templates → kit/templates}/rules/shared-checkout.md +0 -0
  504. /package/{templates → kit/templates}/rules/skill-gaps.md +0 -0
  505. /package/{templates → kit/templates}/rules/task-management.md +0 -0
  506. /package/{templates → kit/templates}/settings/settings.json.tmpl +0 -0
  507. /package/{templates → kit/templates}/settings/settings.local.json.tmpl +0 -0
  508. /package/{templates → kit/templates}/skills/NAMESPACED +0 -0
  509. /package/{templates → kit/templates}/skills/brand-voice/SKILL.md +0 -0
  510. /package/{templates → kit/templates}/skills/brand-voice/references/voice-template.md +0 -0
  511. /package/{templates → kit/templates}/skills/campaign/references/plan-template.md +0 -0
  512. /package/{templates → kit/templates}/skills/concrete/SKILL.md +0 -0
  513. /package/{templates → kit/templates}/skills/copywriting/SKILL.md +0 -0
  514. /package/{templates → kit/templates}/skills/copywriting/references/copy-frameworks.md +0 -0
  515. /package/{templates → kit/templates}/skills/copywriting/references/natural-transitions.md +0 -0
  516. /package/{templates → kit/templates}/skills/feature-build-refine/SKILL.md +0 -0
  517. /package/{templates → kit/templates}/skills/karpathy-guidelines/SKILL.md +0 -0
  518. /package/{templates → kit/templates}/skills/morning-briefing/SKILL.md +0 -0
  519. /package/{templates → kit/templates}/skills/page-design/SKILL.md +0 -0
  520. /package/{templates → kit/templates}/skills/page-design/add.json +0 -0
  521. /package/{templates → kit/templates}/skills/page-design/references/design-file-template.md +0 -0
  522. /package/{templates → kit/templates}/skills/page-design/references/generic-page-patterns.md +0 -0
  523. /package/{templates → kit/templates}/skills/page-design/references/minimum-rules.md +0 -0
  524. /package/{templates → kit/templates}/skills/speckit/SKILL.md +0 -0
  525. /package/{templates → kit/templates}/skills/supabase-patterns/SKILL.md +0 -0
  526. /package/{scripts/lib → lib/core}/adapter-select.sh +0 -0
  527. /package/{scripts/lib → lib/core}/agent-launch.sh +0 -0
  528. /package/{scripts/lib → lib/core}/agent-profile.sh +0 -0
  529. /package/{scripts/lib → lib/core}/cckit-output.sh +0 -0
  530. /package/{scripts/lib → lib/core}/codex-hooks.sh +0 -0
  531. /package/{scripts → lib/core}/forge-fence.baseline +0 -0
  532. /package/{scripts/lib → lib/core}/git-remote.sh +0 -0
  533. /package/{scripts/lib → lib/core}/hook-refresh.sh +0 -0
  534. /package/{scripts → lib/core}/instruction-budget-lint-test.sh +0 -0
  535. /package/{scripts/lib → lib/core}/kit-state.sh +0 -0
  536. /package/{scripts/lib → lib/core}/orchestration-runtime.sh +0 -0
  537. /package/{scripts/lib → lib/core}/project-scan.sh +0 -0
  538. /package/{scripts/lib → lib/core}/render.sh +0 -0
  539. /package/{scripts → lib/core}/review-lint-test.sh +0 -0
  540. /package/{scripts/lib → lib/core}/review-stage.sh +0 -0
  541. /package/{scripts → lib/core}/skill-frontmatter-lint-test.sh +0 -0
  542. /package/{scripts/lib → lib/core}/task-source-file.sh +0 -0
  543. /package/{scripts/lib → lib/core}/task-source.sh +0 -0
  544. /package/{scripts/lib → lib/core}/toon.sh +0 -0
  545. /package/{scripts/lib → lib/core}/ui.sh +0 -0
  546. /package/{scripts/lib → lib/forge}/forge-none.sh +0 -0
  547. /package/{scripts → lib/verbs}/debug.sh +0 -0
  548. /package/{scripts → lib/verbs}/night-run.test.mjs +0 -0
  549. /package/{scripts/lib → lib/verbs}/upgrade-report.sh +0 -0
@@ -0,0 +1,1598 @@
1
+ #!/usr/bin/env bash
2
+ # claude-kit init — scaffold a tailored .claude/ into a target project.
3
+ # Substitutes {{VARS}} and resolves <!-- IF:FLAG --> blocks from a role profile.
4
+ #
5
+ # Usage:
6
+ # scripts/init.sh [<folder>] [flags] # guided setup: detect, ask, scaffold, git + GitHub
7
+ # scripts/init.sh --profile software --no-github # scaffold only, nothing outside the folder
8
+ # scripts/init.sh --upgrade --target <dir> # merge new features into an existing setup
9
+ #
10
+ # Flags (all optional; sensible defaults derived otherwise):
11
+ # <folder> | --target <dir> project folder (created when missing) (default: $PWD)
12
+ # --no-github scaffold only: no git init, no commit, no GitHub write
13
+ # --yes run the git + GitHub setup without asking (required with no terminal)
14
+ # --public create the GitHub repository public (default: private)
15
+ # --answers <file> JSON {"<question key>":"<answer>"} for the guided questions
16
+ # --recipe <id|none> start a NEW project from kit/recipes/<id>.json
17
+ # --database <id|none> add the database option kit/databases/<id>.json
18
+ # --profile <name> role profile, one of kit/profiles/*.json: software · content · marketing ·
19
+ # research · automation · minimal (asked when a terminal or an
20
+ # answers file is there; else required unless --upgrade)
21
+ # --name <str> human project name (default: target basename)
22
+ # --slug <str> short slug (default: lowercased basename)
23
+ # --repo <owner/repo> GitHub repo (default: <owner>/<slug>)
24
+ # --owner-login <login> GitHub user/org login (default: forge_rest user)
25
+ # --owner-name <str> human owner name (default: owner-login)
26
+ # --project-number <int> Projects v2 number (enables board) (default: none -> board off)
27
+ # --plans <mdx|markdown|none> plan file format (default: profile default)
28
+ # --plans-dir <dir> where plans live (default: per format)
29
+ # --memory <on|off> retired: accepted and ignored
30
+ # --speckit <on|off> add the Spec-Driven Development flow (default: off)
31
+ # --plain-language <on|off> install the plain-language rule: the agent says draft, review and
32
+ # approve, and names git mechanics only when asked (default: profile default)
33
+ # --prepush "<command>" install an opt-in pre-push gate that runs <command> before
34
+ # `git push` and blocks on failure (default: none -> no gate)
35
+ # --local <on|off> local model layer (mlx_lm.server) for NL chores at $0 API cost
36
+ # (config + SessionStart status hook) (default: off)
37
+ # --lang <str> working language (default: English)
38
+ # --adapter <claude|codex|both> which agent adapter(s) to install (default: claude; with
39
+ # --upgrade: the stored selection, and a named adapter is ADDED to it)
40
+ # --upgrade merge new kit features into an existing .claude/ (preserves your edits)
41
+ # --dry-run print the scaffold plan and exit, writing nothing
42
+ # --force overwrite without prompting
43
+ # The forge port (E537): every GitHub call goes through forge_* / gh_rest_* (scripts/lib/gh-rest.sh).
44
+ if ! command -v gh_rest_issue_json >/dev/null 2>&1; then
45
+ # shellcheck source=/dev/null
46
+ . "$(cd "$(dirname "$0")/.." && pwd)/forge/gh-rest.sh"
47
+ fi
48
+
49
+ set -euo pipefail
50
+
51
+ # ============================================================================
52
+ # TERMINAL IDENTITY — init banner + golden-angle plant animation #
53
+ # Canonical brief: the cckit docs ( #
54
+ # terminal-identity brief) #
55
+ # Sources kit-sigil.sh for palette/mode detection when available. #
56
+ # ============================================================================
57
+
58
+ # _kit_banner_detect_mode — populate KIT_COLOR, KIT_UNICODE, and palette vars.
59
+ # Tries to source the plugin's kit-sigil template (plain bash, no {{VARS}});
60
+ # falls back to inline detection if the template is unavailable or fails.
61
+ _kit_banner_detect_mode() {
62
+ # Try the plugin's sigil template (lives beside this script in the plugin tree)
63
+ local _sigil
64
+ _sigil="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)/templates/lib/kit-sigil.sh.tmpl"
65
+ if [ -f "$_sigil" ]; then
66
+ # shellcheck disable=SC1090
67
+ if source "$_sigil" 2>/dev/null; then
68
+ kit_color_mode 2>/dev/null && return 0
69
+ fi
70
+ fi
71
+ # Inline fallback — matches the logic in kit-sigil.sh exactly.
72
+ local _loc="${LC_ALL:-${LC_CTYPE:-${LANG:-}}}"
73
+ if [ "${KIT_ASCII:-}" = "1" ]; then
74
+ KIT_UNICODE=0
75
+ elif grep -qiE 'utf-?8' <<<"$_loc"; then
76
+ KIT_UNICODE=1
77
+ else
78
+ KIT_UNICODE=0
79
+ fi
80
+ if [ "${FORCE_COLOR:-}" = "1" ]; then
81
+ KIT_COLOR=1
82
+ elif [ -t 1 ] && [ -z "${NO_COLOR+x}" ]; then
83
+ KIT_COLOR=1
84
+ else
85
+ KIT_COLOR=0
86
+ fi
87
+ if [ "$KIT_COLOR" = 1 ]; then
88
+ KIT_AZAFRAN=$'\033[38;2;201;122;44m'
89
+ KIT_INK=$'\033[2m'
90
+ KIT_POLVO=$'\033[38;2;168;160;151m'
91
+ KIT_RESET=$'\033[0m'
92
+ else
93
+ KIT_AZAFRAN=''; KIT_INK=''; KIT_POLVO=''; KIT_RESET=''
94
+ fi
95
+ }
96
+
97
+ # _kit_print_static_banner — the V4 box-drawing CLAUDE KIT banner (§7 exact form).
98
+ # Always printed: after the animation on TTY, or alone when non-TTY / ASCII mode.
99
+ # Reads: KIT_COLOR KIT_UNICODE KIT_AZAFRAN KIT_INK KIT_POLVO KIT_RESET
100
+ # Arg 1 (optional): version string, no "v" prefix (e.g. "0.7.0").
101
+ _kit_print_static_banner() {
102
+ local _ver="${1:-}"
103
+ printf '\n'
104
+ if [ "${KIT_UNICODE:-0}" = "1" ]; then
105
+ # V4 box-drawing letterforms — ink-dim body, single Azafran seed as I-dot of KIT.
106
+ # Brief §7 exact layout (three rows of box-drawing, then lockup + tagline):
107
+ #
108
+ # ┌─┐┬ ┌─┐┬ ┬┌┬┐┌─┐ ┬┌─┬┌┬┐
109
+ # │ │ ├─┤│ │ ││├┤ ├┴┐│ │ ● <- Azafran seed (I-dot of KIT)
110
+ # └─┘┴─┘┴ ┴└─┘─┴┘└─┘ ┴ ┴┴ ┴
111
+ #
112
+ # ⡶ / claude-kit agentic workflows for your repo
113
+ # v0.7.0 · the seed is planted
114
+ printf ' %s%s%s\n' \
115
+ "${KIT_INK}" \
116
+ '┌─┐┬ ┌─┐┬ ┬┌┬┐┌─┐ ┬┌─┬┌┬┐' \
117
+ "${KIT_RESET}"
118
+ # Row 2: letterforms + Azafran seed to the right (the I-dot of KIT)
119
+ printf ' %s%s%s %s%s%s\n' \
120
+ "${KIT_INK}" '│ │ ├─┤│ │ ││├┤ ├┴┐│ │ ' "${KIT_RESET}" \
121
+ "${KIT_AZAFRAN}" '●' "${KIT_RESET}"
122
+ printf ' %s%s%s\n' \
123
+ "${KIT_INK}" \
124
+ '└─┘┴─┘┴ ┴└─┘─┴┘└─┘ ┴ ┴┴ ┴' \
125
+ "${KIT_RESET}"
126
+ printf '\n'
127
+ # Lockup line: seed-head leader + wordmark + tagline
128
+ printf ' %s%s%s%s%s\n' \
129
+ "${KIT_AZAFRAN}" '⡶' "${KIT_RESET}" \
130
+ "${KIT_INK}" " / claude-kit agentic workflows for your repo${KIT_RESET}"
131
+ if [ -n "$_ver" ]; then
132
+ printf ' %s%s%s\n' "${KIT_POLVO}" "v${_ver} · the seed is planted" "${KIT_RESET}"
133
+ else
134
+ printf ' %s%s%s\n' "${KIT_POLVO}" '· the seed is planted' "${KIT_RESET}"
135
+ fi
136
+ else
137
+ # ASCII fallback — no box-drawing, no Braille (KIT_ASCII=1 or non-UTF-8 locale).
138
+ printf ' CLAUDE KIT\n'
139
+ printf ' ----------\n'
140
+ printf '\n'
141
+ printf ' o / claude-kit agentic workflows for your repo\n'
142
+ if [ -n "$_ver" ]; then
143
+ printf ' v%s . the seed is planted\n' "$_ver"
144
+ else
145
+ printf ' . the seed is planted\n'
146
+ fi
147
+ fi
148
+ printf '\n'
149
+ }
150
+
151
+ # _kit_plant_animation — golden-angle Vogel seed-head plant animation (§7 beat 1).
152
+ #
153
+ # Guard: runs ONLY when stdout is a TTY AND KIT_UNICODE=1 (full Unicode, not ASCII mode).
154
+ # Uses ANSI cursor-up to repaint a 4-row Braille grid in-place as seeds enter.
155
+ #
156
+ # Geometry: n=13, fixed scale, 10x16 dot grid => 5 Braille cols x 4 rows.
157
+ # Seed n at angle n*137.50776 deg, radius sqrt(n). Coordinates locked to the
158
+ # full n=13 scatter so each seed lands in its final position (no rescaling).
159
+ # Pre-computed in Python from the Vogel rule; see brief §2 for derivation.
160
+ #
161
+ # Animation frames (seeds visible: 1, 2, 4, 7, 13):
162
+ # f1 = seed n=0 only (center)
163
+ # f2 = seeds n=0,1
164
+ # f4 = seeds n=0..3
165
+ # f7 = seeds n=0..6
166
+ # ff = seeds n=0..12 (full n=13 hero)
167
+ #
168
+ # Braille rows — each string is 5 Braille chars (U+2800-28FF).
169
+ # Row 0 = top, row 3 = bottom. Center seed (n=0) lives at row 1, col 2.
170
+ _kit_plant_animation() {
171
+ [ -t 1 ] || return 0 # non-TTY: skip, banner will print statically
172
+ [ "${KIT_UNICODE:-0}" = "1" ] || return 0 # ASCII mode: skip
173
+
174
+ local ind=' ' # indent to align with banner text
175
+
176
+ # Frame data — pre-computed Braille rows (5 chars each, 4 rows per frame).
177
+ # Stored as positional arrays via function-local vars (portable; no declare -a needed).
178
+ # Frame f1: seed n=0 (center seed only)
179
+ local f1_0='⠀⠀⠀⠀⠀' f1_1='⠀⠀⢀⠀⠀' f1_2='⠀⠀⠀⠀⠀' f1_3='⠀⠀⠀⠀⠀'
180
+ # Frame f2: seeds n=0,1
181
+ local f2_0='⠀⠀⠀⠀⠀' f2_1='⠀⠀⢀⠀⠀' f2_2='⠀⠀⠂⠀⠀' f2_3='⠀⠀⠀⠀⠀'
182
+ # Frame f4: seeds n=0..3
183
+ local f4_0='⠀⠀⠀⠀⠀' f4_1='⠀⠀⢈⠀⠀' f4_2='⠀⠀⠂⠄⠀' f4_3='⠀⠀⠀⠀⠀'
184
+ # Frame f7: seeds n=0..6
185
+ local f7_0='⠀⠀⠀⠀⠀' f7_1='⠀⠄⢈⠐⠀' f7_2='⠀⠀⠂⠄⠀' f7_3='⠀⠀⠁⠀⠀'
186
+ # Frame ff: seeds n=0..12 (full n=13 hero)
187
+ local ff_0='⢀⠠⠀⠐⠀' ff_1='⠀⠄⢈⠐⠀' ff_2='⠠⠀⠂⠄⠂' ff_3='⠀⠀⠁⠄⠀'
188
+
189
+ # Print one frame: 4 Braille rows in ink-dim.
190
+ # Args: row0 row1 row2 row3
191
+ _print_frame() {
192
+ printf '%s%s%s\n' "${KIT_INK}${ind}" "$1" "${KIT_RESET}"
193
+ printf '%s%s%s\n' "${KIT_INK}${ind}" "$2" "${KIT_RESET}"
194
+ printf '%s%s%s\n' "${KIT_INK}${ind}" "$3" "${KIT_RESET}"
195
+ printf '%s%s%s\n' "${KIT_INK}${ind}" "$4" "${KIT_RESET}"
196
+ }
197
+
198
+ # Cursor up N lines (ANSI CSI A).
199
+ _cur_up() { printf '\033[%dA' "$1"; }
200
+
201
+ # Portable fractional sleep — silently ignores if the system only takes integers.
202
+ _psleep() { sleep "$1" 2>/dev/null || true; }
203
+
204
+ # Print a leading blank line and the first frame (no repaint yet).
205
+ printf '\n'
206
+ _print_frame "$f1_0" "$f1_1" "$f1_2" "$f1_3"
207
+
208
+ # Subsequent frames: cursor-up 4, repaint in-place.
209
+ _psleep 0.12
210
+ _cur_up 4; _print_frame "$f2_0" "$f2_1" "$f2_2" "$f2_3"
211
+
212
+ _psleep 0.12
213
+ _cur_up 4; _print_frame "$f4_0" "$f4_1" "$f4_2" "$f4_3"
214
+
215
+ _psleep 0.12
216
+ _cur_up 4; _print_frame "$f7_0" "$f7_1" "$f7_2" "$f7_3"
217
+
218
+ _psleep 0.18
219
+ _cur_up 4; _print_frame "$ff_0" "$ff_1" "$ff_2" "$ff_3"
220
+
221
+ # Brief pause before the banner snaps in, then clear the animation grid.
222
+ _psleep 0.30
223
+ # Move up past the 4 grid rows + the blank line printed before the grid (5 lines total),
224
+ # then erase from cursor to end of screen so the banner replaces the grid cleanly.
225
+ _cur_up 5
226
+ printf '\033[J' # CSI J — erase from cursor to end of screen
227
+ }
228
+
229
+ # _kit_init_banner — entry point for a fresh init run (not --upgrade, not --dry-run).
230
+ # Arg 1 (optional): plugin version string without "v" prefix.
231
+ _kit_init_banner() {
232
+ _kit_banner_detect_mode
233
+ _kit_plant_animation # no-op when non-TTY / KIT_ASCII=1 / non-UTF-8
234
+ _kit_print_static_banner "${1:-}" # always prints (static or after animation)
235
+ }
236
+
237
+ # ============================================================================
238
+ # END TERMINAL IDENTITY #
239
+ # ============================================================================
240
+
241
+ KIT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
242
+
243
+ # ---- preflight: run kit-doctor for Tier 1 hard deps (jq, perl, gh, git) ----
244
+ # Skip during --upgrade (already initialized) and --dry-run (read-only; doctor
245
+ # would be redundant). In interactive runs the doctor auto-installs what it can;
246
+ # if Tier 1 deps are still missing after it runs, init exits with a clear report.
247
+ _DOCTOR="$KIT_ROOT/lib/verbs/kit-doctor.sh"
248
+ _INIT_SKIP_PREFLIGHT=0
249
+ for _ia in "$@"; do [[ "$_ia" == "--upgrade" || "$_ia" == "--dry-run" ]] && _INIT_SKIP_PREFLIGHT=1; done
250
+ # KIT_INIT_PREFLIGHT=0 skips it too: the fixture tests drive a FRESH init, and the doctor installs
251
+ # packages and checks gh auth — neither belongs in a network-free test.
252
+ [[ "${KIT_INIT_PREFLIGHT:-1}" == "0" ]] && _INIT_SKIP_PREFLIGHT=1
253
+
254
+ if [[ $_INIT_SKIP_PREFLIGHT -eq 0 && -x "$_DOCTOR" ]]; then
255
+ # --yes: init's preflight installs Tier-1 deps non-interactively (no approval prompt here; the
256
+ # user already opted into setup by running init). A human running `cckit doctor` directly is asked.
257
+ bash "$_DOCTOR" --yes || {
258
+ echo "" >&2
259
+ echo "x kit-doctor reported unresolved issues — fix them and re-run /kit-init." >&2
260
+ exit 1
261
+ }
262
+ else
263
+ # Fallback bare checks (upgrade/dry-run path: doctor was already run on init)
264
+ command -v jq >/dev/null 2>&1 || { echo "x jq is required." >&2; exit 1; }
265
+ command -v perl >/dev/null 2>&1 || { echo "x perl is required." >&2; exit 1; }
266
+ fi
267
+
268
+ # ---- parse flags ---------------------------------------------------------
269
+ _INIT_ARGS=("$@") # as typed — the --upgrade dry run replays them against a copy (#448)
270
+ PROFILE=""; TARGET="$PWD"; NAME=""; SLUG=""; REPO=""; GH_OWNER=""; OWNER_NAME=""
271
+ PROJECT_NUMBER=""; PLANS_FORMAT=""; PLANS_DIR=""; LANG_PREF="English"; FORCE=0
272
+ PLAIN_LANGUAGE=""
273
+ TASKS_DEFAULT="" # light | issues — the profile's defaults.tasks; on --upgrade, the stored tasks.default
274
+ SPECKIT=""; PREPUSH_CMD=""; LOCAL_LAYER=""; LOCAL_EXPLICIT=0; DRY_RUN=0; UPGRADE=0
275
+ ADAPTER_ARG="" # --adapter as typed; resolved to ADAPTERS below (stored selection wins on upgrade)
276
+ # Guided setup (#456): the folder as typed, the git + GitHub steps, the advisor's answers.
277
+ FOLDER_GIVEN=0; NO_GITHUB=0; ASSUME_YES=0; REPO_VISIBILITY="private"; INIT_ANSWERS=""
278
+ RECIPE_ARG=""; DATABASE_ARG=""
279
+ SKILL_PREFIX="" # namespace applied to kit-namespaced skill templates (from kit.config .skillPrefix on upgrade)
280
+ while [[ $# -gt 0 ]]; do
281
+ case "$1" in
282
+ --profile) PROFILE="$2"; shift 2 ;;
283
+ --target) TARGET="$2"; FOLDER_GIVEN=1; shift 2 ;;
284
+ --name) NAME="$2"; shift 2 ;;
285
+ --slug) SLUG="$2"; shift 2 ;;
286
+ --repo) REPO="$2"; shift 2 ;;
287
+ --owner-login) GH_OWNER="$2"; shift 2 ;;
288
+ --owner-name) OWNER_NAME="$2"; shift 2 ;;
289
+ --project-number) PROJECT_NUMBER="$2"; shift 2 ;;
290
+ --plans) PLANS_FORMAT="$2"; shift 2 ;;
291
+ --plans-dir) PLANS_DIR="$2"; shift 2 ;;
292
+ --memory) shift 2 ;; # retired: the memory integration is gone
293
+ --speckit) SPECKIT="$2"; shift 2 ;;
294
+ --plain-language) PLAIN_LANGUAGE="$2"; shift 2 ;;
295
+ --prepush) PREPUSH_CMD="$2"; shift 2 ;;
296
+ --local) LOCAL_LAYER="$2"; LOCAL_EXPLICIT=1; shift 2 ;;
297
+ --lang) LANG_PREF="$2"; shift 2 ;;
298
+ --force) FORCE=1; shift ;;
299
+ --upgrade) UPGRADE=1; shift ;;
300
+ --dry-run) DRY_RUN=1; shift ;;
301
+ --adapter) ADAPTER_ARG="$2"; shift 2 ;;
302
+ --no-github) NO_GITHUB=1; shift ;;
303
+ --yes|-y) ASSUME_YES=1; shift ;;
304
+ --public) REPO_VISIBILITY="public"; shift ;;
305
+ --answers) INIT_ANSWERS="$2"; shift 2 ;;
306
+ --recipe) RECIPE_ARG="$2"; shift 2 ;;
307
+ --database) DATABASE_ARG="$2"; shift 2 ;;
308
+ -h|--help) sed -n '2,42p' "$0"; exit 0 ;;
309
+ -*) echo "x unknown flag: $1" >&2; exit 1 ;;
310
+ *) if [[ $FOLDER_GIVEN -eq 1 ]]; then echo "x one folder only (got '$TARGET' and '$1')" >&2; exit 1; fi
311
+ TARGET="$1"; FOLDER_GIVEN=1; shift ;;
312
+ esac
313
+ done
314
+
315
+ # ---- adapter selection (E257.3) -------------------------------------------
316
+ # Validated here, before anything is written: an unknown adapter name refuses the whole run.
317
+ # shellcheck source=/dev/null
318
+ . "$KIT_ROOT/lib/core/adapter-select.sh"
319
+ ADAPTERS_NEW=""
320
+ if [[ -n "$ADAPTER_ARG" ]]; then
321
+ ADAPTERS_NEW="$(adapter_normalize "$ADAPTER_ARG")" || { echo "x --adapter takes claude, codex or both (got '$ADAPTER_ARG')" >&2; exit 1; }
322
+ fi
323
+ ADAPTERS="${ADAPTERS_NEW:-claude}" # fresh init; --upgrade replaces this with the stored selection
324
+ ADAPTER_ADDED=0
325
+
326
+ # ---- guided setup: name the folder, ask what is missing (#456) -------------
327
+ # Before anything is written. `--upgrade` never enters it: an upgrade works on a project that is
328
+ # already set up and asks nothing.
329
+ # shellcheck source=/dev/null
330
+ . "$KIT_ROOT/lib/verbs/init-guide.sh"
331
+ # shellcheck source=/dev/null
332
+ . "$KIT_ROOT/lib/forge/init-github.sh"
333
+ INIT_CLASS=""; INIT_NEW=0; INIT_ALREADY=0; RECIPE_FILE=""; DB_FILE=""; DB_STACK=""; SETUP_RUN=0
334
+ if [[ $UPGRADE -eq 0 ]]; then
335
+ if [[ -n "$INIT_ANSWERS" && ! -f "$INIT_ANSWERS" ]]; then echo "x --answers: no such file: $INIT_ANSWERS" >&2; exit 2; fi
336
+ if [[ -n "$INIT_ANSWERS" ]] && ! jq -e 'type == "object"' "$INIT_ANSWERS" >/dev/null 2>&1; then
337
+ echo "x --answers: $INIT_ANSWERS is not a JSON object of {\"question\": \"answer\"}" >&2; exit 2
338
+ fi
339
+ init_guide_resolve
340
+ fi
341
+
342
+ # Installed plugin version -> recorded into kit.config as kitVersion (drives /kit-update checks).
343
+ PLUGIN_VERSION="$(jq -r '.version // "0.0.0"' "$KIT_ROOT/.claude-plugin/plugin.json" 2>/dev/null || echo "0.0.0")"
344
+
345
+ # ---- upgrade mode: backfill inputs from the project's existing config ----
346
+ if [[ $UPGRADE -eq 1 ]]; then
347
+ UP_TARGET="$(cd "${TARGET:-$PWD}" && pwd)"
348
+ UP_CFG="$(adapter_config_existing "$UP_TARGET" || true)"
349
+ [[ -n "$UP_CFG" && -f "$UP_CFG" ]] || { echo "x --upgrade needs $UP_TARGET/.claude/kit.config.json (run /kit-init first)."; exit 1; }
350
+ # The stored selection is the truth on upgrade. --adapter only ever ADDS to it: dropping an
351
+ # adapter is an uninstall of its files, which is `cckit remove`'s job, not an upgrade side effect.
352
+ ADAPTERS_HAVE="$(adapter_list_from_config "$UP_CFG")" || { echo "x $UP_CFG has an invalid 'adapters' value (expected claude and/or codex)." >&2; exit 1; }
353
+ ADAPTERS="$(adapter_union "$ADAPTERS_HAVE" "$ADAPTERS_NEW")"
354
+ [[ "$ADAPTERS" != "$ADAPTERS_HAVE" ]] && ADAPTER_ADDED=1
355
+
356
+ # ---- guard: never apply the kit over its OWN home, or downgrade ----------
357
+ # The kit is DEVELOPED in-tree in its home repo (packages/claude-kit-plugin/). Running
358
+ # --upgrade there would copy the installed (usually OLDER) plugin snapshot over the live
359
+ # source — the recurring "every time I improve the kit, /kit-update fights me" footgun.
360
+ # Refuse cleanly (exit 0, not an error) so the home repo edits the source directly.
361
+ if [[ -f "$UP_TARGET/packages/claude-kit-plugin/.claude-plugin/plugin.json" ]]; then
362
+ echo "x $UP_TARGET is claude-kit's home repo -- edit packages/claude-kit-plugin/ directly; /kit-update is for consumer projects, not the kit's source."
363
+ exit 0
364
+ fi
365
+ # Nothing to do when the installed plugin is the SAME version as the project (avoids the
366
+ # confusing re-scan + any stray re-add). A genuinely-behind consumer (kitVersion < plugin)
367
+ # still upgrades normally.
368
+ _have_kv="$(jq -r '.kitVersion // "0.0.0"' "$UP_CFG" 2>/dev/null || echo 0.0.0)"
369
+ # ...unless this run enables a second adapter: that is new work at the same version. So is a
370
+ # Codex project whose hooks are not registered (a hooks.json that was unparseable at init, or a
371
+ # project scaffolded before E257.5): `cckit doctor` sends the user here to repair exactly that.
372
+ _cx_unregistered=0
373
+ if adapter_has "$ADAPTERS" codex; then
374
+ # shellcheck source=/dev/null
375
+ . "$KIT_ROOT/lib/core/codex-hooks.sh"
376
+ codex_hooks_registered "$UP_TARGET/.codex/hooks.json" guard-base-branch-commit.sh || _cx_unregistered=1
377
+ fi
378
+ if [[ "$_have_kv" == "$PLUGIN_VERSION" && $ADAPTER_ADDED -eq 0 && $_cx_unregistered -eq 0 ]]; then
379
+ echo "x already at $PLUGIN_VERSION -- nothing to upgrade. (To pull a newer release, refresh the plugin cache: claude plugin update.)"
380
+ exit 0
381
+ fi
382
+
383
+ [[ -z "$PROFILE" ]] && PROFILE="$(jq -r '.profile // "minimal"' "$UP_CFG")"
384
+ [[ -z "$NAME" ]] && NAME="$(jq -r '.project.name // ""' "$UP_CFG")"
385
+ [[ -z "$SLUG" ]] && SLUG="$(jq -r '.project.slug // ""' "$UP_CFG")"
386
+ [[ -z "$REPO" ]] && REPO="$(jq -r '.github.repo // ""' "$UP_CFG")"
387
+ [[ -z "$GH_OWNER" ]] && GH_OWNER="$(jq -r '.github.owner // ""' "$UP_CFG")"
388
+ [[ -z "$SPECKIT" ]] && SPECKIT="$(jq -r 'if (.specKit.enabled // false) then "on" else "off" end' "$UP_CFG")"
389
+ [[ -z "$LOCAL_LAYER" ]] && LOCAL_LAYER="$(jq -r 'if (.local.enabled // false) then "on" else "off" end' "$UP_CFG")"
390
+ # The stored switch is the truth on upgrade: a project that turned plain language off keeps it
391
+ # off whatever its profile defaults to.
392
+ [[ -z "$PLAIN_LANGUAGE" ]] && PLAIN_LANGUAGE="$(jq -r 'if (.plainLanguage.enabled // false) then "on" else "off" end' "$UP_CFG")"
393
+ # Same for the default task list: the project's tasks.default, never the profile default.
394
+ TASKS_DEFAULT="$(jq -r '.tasks.default // "issues"' "$UP_CFG")"
395
+ # A config with NO plans key at all deliberately runs without plan files — upgrade must not
396
+ # resurrect the profile default (and with it the plans rule + config block) (#149).
397
+ [[ -z "$PLANS_FORMAT" ]] && PLANS_FORMAT="$(jq -r 'if has("plans") then (.plans.format // "") else "none" end' "$UP_CFG")"
398
+ [[ "$LANG_PREF" == "English" ]] && LANG_PREF="$(jq -r '.project.language // "English"' "$UP_CFG")"
399
+ # Skill-name prefix: projects that namespace their kit skills (e.g. "kit-task-close") set
400
+ # `skillPrefix` so upgrade scaffolds namespaced templates to the matching name instead of
401
+ # creating bare-name duplicates. Empty = bare names (default).
402
+ [[ -z "$SKILL_PREFIX" ]] && SKILL_PREFIX="$(jq -r '.skillPrefix // ""' "$UP_CFG")"
403
+ _UPNUM="$(jq -r '.github.projectNumber // empty' "$UP_CFG")"
404
+ [[ -z "$PROJECT_NUMBER" && -n "$_UPNUM" && "$_UPNUM" != "null" ]] && PROJECT_NUMBER="$_UPNUM"
405
+ _UPPP="$(jq -r '.prePush.command // ""' "$UP_CFG")"
406
+ [[ -z "$PREPUSH_CMD" && -n "$_UPPP" && "$_UPPP" != "null" ]] && PREPUSH_CMD="$_UPPP"
407
+
408
+ # ---- upgrade exclusion registry (#334) ----------------------------------
409
+ # `upgrade.removed[]` + keys of `upgrade.renamed{}` are paths the project
410
+ # deleted/renamed ON PURPOSE. The upgrade must never re-add them ("add missing
411
+ # files" doesn't know the project's deliberate state). Read from the EXISTING
412
+ # config (pre-merge) into a newline-delimited list consumed by _upgrade_excluded.
413
+ UPGRADE_EXCLUDE="$(jq -r '((.upgrade.removed // []) + ((.upgrade.renamed // {}) | keys)) | .[]' "$UP_CFG" 2>/dev/null || true)"
414
+
415
+ # ---- dirty working tree guard (upgrade mode only) -----------------------
416
+ # Refuse to write files into a project with uncommitted changes.
417
+ # Safe no-op when git is unavailable or UP_TARGET is not a git repo.
418
+ # Pass --force to skip at your own risk.
419
+ if [[ $DRY_RUN -eq 0 && $FORCE -eq 0 ]]; then
420
+ if git -C "$UP_TARGET" rev-parse --git-dir >/dev/null 2>&1; then
421
+ _dirty="$(git -C "$UP_TARGET" status --porcelain 2>/dev/null || true)"
422
+ if [[ -n "$_dirty" ]]; then
423
+ echo "x working tree is dirty -- commit, stash, or discard before running /kit-update" >&2
424
+ echo " (pass --force to override at your own risk)" >&2
425
+ exit 1
426
+ fi
427
+ fi
428
+ fi
429
+ fi
430
+
431
+ # The profile names come from kit/profiles/*.json, so a profile added there is offered here.
432
+ _PROFILE_NAMES="$(for _pf in "$KIT_ROOT"/kit/profiles/*.json; do [[ -f "$_pf" ]] && basename "$_pf" .json; done | paste -sd '|' -)"
433
+ [[ -z "$PROFILE" ]] && { echo "x --profile is required ($_PROFILE_NAMES)"; exit 1; }
434
+ PROFILE_FILE="$KIT_ROOT/kit/profiles/$PROFILE.json"
435
+ [[ -f "$PROFILE_FILE" ]] || { echo "x no such profile: $PROFILE — one of: $_PROFILE_NAMES"; exit 1; }
436
+
437
+ # ---- resolve target + defaults ------------------------------------------
438
+ # A folder that is not there yet is created when the first file is written, not here: a dry run
439
+ # and a declined confirmation leave nothing behind (#456).
440
+ if [[ -d "$TARGET" ]]; then TARGET="$(cd "$TARGET" && pwd)"; else TARGET="$(init_abs_path "$TARGET")"; fi
441
+ BASENAME="$(basename "$TARGET")"
442
+ # Prefer the ACTUAL repo name from the git remote — the local directory is often cloned/renamed to a
443
+ # different name — and only fall back to the directory basename (#73).
444
+ # shellcheck source=/dev/null
445
+ . "$KIT_ROOT/lib/core/git-remote.sh"
446
+ # shellcheck source=/dev/null
447
+ . "$KIT_ROOT/lib/core/hook-refresh.sh" # hook_refresh_state — refresh unmodified hooks (#370)
448
+ # shellcheck source=/dev/null
449
+ . "$KIT_ROOT/lib/verbs/upgrade-report.sh" # measured change list + minimal JSON writes (#448)
450
+ NAME_SRC="$(git_remote_repo_name "$TARGET" 2>/dev/null || true)"; NAME_SRC="${NAME_SRC:-$BASENAME}"
451
+ [[ -z "$NAME" ]] && NAME="$NAME_SRC"
452
+ [[ -z "$SLUG" ]] && SLUG="$(echo "$NAME_SRC" | tr '[:upper:]' '[:lower:]' | tr -cs 'a-z0-9' '-' | sed -E 's/^-|-$//g')"
453
+ if [[ -z "$GH_OWNER" ]]; then GH_OWNER="$(forge_rest user --jq .login 2>/dev/null || echo "")"; fi
454
+ [[ -z "$OWNER_NAME" ]] && OWNER_NAME="${GH_OWNER:-owner}"
455
+ # Prefer the project's ACTUAL GitHub remote over an <owner>/<dir-basename> guess — the local
456
+ # directory name often differs from the repo name (would target a non-existent repo otherwise).
457
+ if [[ -z "$REPO" && $INIT_ALREADY -eq 1 ]]; then
458
+ REPO="$(jq -r '.github.repo // empty' "$(adapter_config_existing "$TARGET")" 2>/dev/null || true)"
459
+ fi
460
+ [[ -z "$REPO" ]] && REPO="$(forge_repo_from_git "$TARGET" 2>/dev/null || true)"
461
+ [[ -z "$REPO" && -n "$GH_OWNER" ]] && REPO="$GH_OWNER/$SLUG"
462
+ [[ -z "$REPO" ]] && REPO="$SLUG"
463
+
464
+ # ---- base branch ---------------------------------------------------------
465
+ # Rules that name the integration branch are TEMPLATED on it ({{BASE_BRANCH}}), so a project that
466
+ # integrates on `develop` does not scaffold a rule telling agents to branch from `main` (#283).
467
+ # Resolved the same way the rest of the kit resolves it (kit-config.sh: baseBranch -> integrationBranch
468
+ # -> flow -> "main"), then WRITTEN into the generated config below — a rendered rule that disagrees
469
+ # with KIT_BASE_BRANCH is the bug this parameterization exists to remove.
470
+ # 1. KIT_BASE_BRANCH from the environment (an explicit caller override)
471
+ # 2. the project's existing config, found by kit_config_path (re-init / --upgrade keeps the
472
+ # project's choice) — the ONE resolver, so this honors $KIT_CONFIG and finds a config in an
473
+ # ancestor directory. Hand-checking two TARGET-local filenames skipped both.
474
+ # 3. the repo's actual GitHub default branch (same source as REPO/GH_OWNER above)
475
+ # 4. "main"
476
+ # Unconditional, not `[[ -z ${BASE_BRANCH:-} ]]`: KIT_BASE_BRANCH is the DOCUMENTED override, and
477
+ # BASE_BRANCH is an internal name this script exports for `emit` — an inherited BASE_BRANCH from a
478
+ # caller's environment must not silently outrank it.
479
+ BASE_BRANCH="${KIT_BASE_BRANCH:-}"
480
+ if [[ -z "$BASE_BRANCH" ]]; then
481
+ # shellcheck source=/dev/null
482
+ . "$KIT_ROOT/lib/core/config-path.sh"
483
+ _bb_cfg="$(kit_config_path "$TARGET" 2>/dev/null || true)"
484
+ if [[ -n "$_bb_cfg" && -f "$_bb_cfg" ]]; then
485
+ BASE_BRANCH="$(jq -r '.github.baseBranch // .github.integrationBranch // .github.flow // empty' "$_bb_cfg" 2>/dev/null || true)"
486
+ fi
487
+ fi
488
+ [[ -z "$BASE_BRANCH" && "$REPO" == */* ]] && BASE_BRANCH="$(gh_rest_repo_default_branch "$REPO" 2>/dev/null || true)"
489
+ # The git + GitHub setup (#456) runs when it is wanted AND confirmed: --yes, or a terminal to ask
490
+ # on. A dry run plans as if it were confirmed. A project with no `origin` remote that the setup is
491
+ # about to give a repository integrates on `develop` (main stays the release branch).
492
+ SETUP_WANTED=0; SETUP_BASE_CHOSEN=0
493
+ if [[ $UPGRADE -eq 0 && $NO_GITHUB -eq 0 ]] && { [[ $ASSUME_YES -eq 1 || $DRY_RUN -eq 1 ]] || _ig_tty; }; then
494
+ SETUP_WANTED=1
495
+ fi
496
+ if [[ -z "$BASE_BRANCH" && $SETUP_WANTED -eq 1 ]] && [[ -z "$(git -C "$TARGET" remote get-url origin 2>/dev/null || true)" ]]; then
497
+ BASE_BRANCH="develop"; SETUP_BASE_CHOSEN=1
498
+ fi
499
+ # No setup, no remote default, no config: `main` when the repository has it (or has no commit yet),
500
+ # else the branch that is checked out — a base branch the repository does not have stops the first
501
+ # `cckit start` / `cckit task start` (#487).
502
+ _init_fallback_base() {
503
+ local cur
504
+ if git -C "$TARGET" rev-parse --verify -q HEAD >/dev/null 2>&1 \
505
+ && ! git -C "$TARGET" show-ref --verify -q refs/heads/main \
506
+ && ! git -C "$TARGET" show-ref --verify -q refs/remotes/origin/main; then
507
+ cur="$(git -C "$TARGET" symbolic-ref --short -q HEAD 2>/dev/null || true)"
508
+ if [[ -n "$cur" ]]; then printf '%s' "$cur"; return 0; fi
509
+ fi
510
+ printf 'main'
511
+ }
512
+ [[ -z "$BASE_BRANCH" ]] && BASE_BRANCH="$(_init_fallback_base)"
513
+
514
+ # profile defaults
515
+ [[ -z "$PLANS_FORMAT" ]] && PLANS_FORMAT="$(jq -r '.defaults.plans_format' "$PROFILE_FILE")"
516
+ [[ -z "$SPECKIT" ]] && SPECKIT="$(jq -r '.defaults.speckit // "off"' "$PROFILE_FILE")"
517
+ [[ -z "$LOCAL_LAYER" ]] && LOCAL_LAYER="$(jq -r '.defaults.local // "off"' "$PROFILE_FILE")"
518
+ # Content work (E415). Profile defaults only — no flag: a project changes them in its config.
519
+ # content_kinds on -> kinds.content: true (setup-labels.sh provisions the content kind labels)
520
+ CONTENT_KINDS="$(jq -r '.defaults.content_kinds // "off"' "$PROFILE_FILE")"
521
+ # pr_body content -> pr.bodyStyle: "content" (draft-and-review wording in generated PR bodies)
522
+ case "$CONTENT_KINDS" in on|true|yes) CONTENT_KINDS_ON="true";; *) CONTENT_KINDS_ON="false";; esac
523
+ # plain_language on -> plainLanguage.enabled: true + the plain-language rule (--plain-language wins)
524
+ PR_BODY_STYLE="$(jq -r '.defaults.pr_body // "code"' "$PROFILE_FILE")"
525
+ [[ -z "$PLAIN_LANGUAGE" ]] && PLAIN_LANGUAGE="$(jq -r '.defaults.plain_language // "off"' "$PROFILE_FILE")"
526
+ case "$PLAIN_LANGUAGE" in on|true|yes) PLAIN_ON="true";; *) PLAIN_ON="false";; esac
527
+ # tasks light -> tasks.default: "light" (a bare `cckit next` answers from `cckit task`)
528
+ [[ -z "$TASKS_DEFAULT" ]] && TASKS_DEFAULT="$(jq -r '.defaults.tasks // "issues"' "$PROFILE_FILE")"
529
+ [[ "$TASKS_DEFAULT" == "light" ]] || TASKS_DEFAULT="issues"
530
+ [[ "$PR_BODY_STYLE" == "content" ]] || PR_BODY_STYLE="code"
531
+ if [[ -z "$PLANS_DIR" ]]; then
532
+ case "$PLANS_FORMAT" in
533
+ mdx) PLANS_DIR="apps/plans/src/briefs" ;;
534
+ *) PLANS_DIR="docs/plans" ;;
535
+ esac
536
+ fi
537
+
538
+ # arrays from profile
539
+ ROLES_NL="$(jq -r '.roles[]' "$PROFILE_FILE")"
540
+ MS_NL="$(jq -r '.milestones[]' "$PROFILE_FILE")"
541
+ AGENTS="$(jq -r '.agents[]' "$PROFILE_FILE")"
542
+ SKILLS="$(jq -r '.skills[]' "$PROFILE_FILE")"
543
+ # page-design (#504) is installed wherever pages are designed: a profile that lists it (marketing),
544
+ # a recipe whose config sets `tasks.design: true` (nextjs-shadcn), or, on --upgrade, a project
545
+ # config that holds that key. One switch, so the skill and the page-task rule of `cckit task run`
546
+ # cannot disagree.
547
+ DESIGN_SKILL_ON=""
548
+ if [[ -n "$RECIPE_FILE" ]]; then DESIGN_SKILL_ON="$(jq -r '.config.tasks.design // empty' "$RECIPE_FILE" 2>/dev/null || true)"; fi
549
+ if [[ $UPGRADE -eq 1 && -n "${UP_CFG:-}" && -f "${UP_CFG:-}" ]]; then DESIGN_SKILL_ON="$(jq -r '.tasks.design // empty' "$UP_CFG" 2>/dev/null || true)"; fi
550
+ if [[ "$DESIGN_SKILL_ON" == "true" ]] && ! grep -qx "page-design" <<<"$SKILLS"; then
551
+ SKILLS="$SKILLS
552
+ page-design"
553
+ fi
554
+ RULES="$(jq -r '.rules[]' "$PROFILE_FILE")"
555
+
556
+ # Kit-namespaced skill set: templates whose names take $SKILL_PREFIX (the workflow skills:
557
+ # task-*, effort-*). Content skills (morning-briefing, karpathy-…) are NOT listed and stay bare.
558
+ # Listed in kit/templates/skills/NAMESPACED (one name per line) so the marker lives outside the artifact.
559
+ NAMESPACED_SKILLS="$(grep -vE '^\s*(#|$)' "$KIT_ROOT/kit/templates/skills/NAMESPACED" 2>/dev/null || true)"
560
+ _is_namespaced() { # <skill-name> -> 0 if it takes the prefix
561
+ grep -qxF "$1" <<<"$NAMESPACED_SKILLS"
562
+ }
563
+ # Rewrite a freshly-scaffolded namespaced skill so its own name: + sibling /command refs + sigil
564
+ # label carry $SKILL_PREFIX (template ships bare; the project namespaces). The (?![\w-]) guard stops
565
+ # /task-pr matching inside /task-pr-merge. perl is a Tier-1 dep (kit-doctor).
566
+ _prefix_skill_file() { # <file>
567
+ local f="$1" n
568
+ printf '%s\n' "$NAMESPACED_SKILLS" | while IFS= read -r n; do
569
+ [[ -n "$n" ]] || continue
570
+ P="$SKILL_PREFIX" N="$n" perl -i -pe '
571
+ my ($p,$n)=($ENV{P},$ENV{N});
572
+ s{^name:\h*\Q$n\E\h*$}{name: $p$n};
573
+ s{/\Q$n\E(?![\w-])}{/$p$n}g;
574
+ s{kit_sigil "\Q$n\E"}{kit_sigil "$p$n"}g;
575
+ ' "$f"
576
+ done
577
+ }
578
+
579
+ ROLES_JSON="$(printf '%s' "$ROLES_NL" | jq -Rs 'split("\n")|map(select(length>0))')"
580
+ MS_JSON="$(printf '%s' "$MS_NL" | jq -Rs 'split("\n")|map(select(length>0))')"
581
+ ROLES_HUMAN="$(printf '%s' "$ROLES_NL" | paste -sd '·' - | sed 's/·/ · /g')"
582
+ MILESTONES_HUMAN="$(printf '%s' "$MS_NL" | paste -sd '·' - | sed 's/·/ · /g')"
583
+
584
+ # flags / booleans
585
+ if [[ -n "$PROJECT_NUMBER" ]]; then PROJECTS_V2="true"; PNUM_JSON="$PROJECT_NUMBER"; else PROJECTS_V2="false"; PNUM_JSON="null"; PROJECT_NUMBER="null"; fi
586
+ if [[ "$PLANS_FORMAT" == "none" ]]; then PLANS_ON="false"; else PLANS_ON="true"; fi
587
+ if grep -qx "Designer" <<<"$ROLES_NL"; then DESIGN_ON="true"; else DESIGN_ON="false"; fi
588
+
589
+ # spec kit
590
+ case "$SPECKIT" in on|true|yes) SPECKIT_ON="true";; *) SPECKIT_ON="false";; esac
591
+ SPECKIT_JSON="$SPECKIT_ON"
592
+
593
+ # pre-push gate (opt-in: enabled when a check command is provided)
594
+ if [[ -n "$PREPUSH_CMD" ]]; then PREPUSH_ENABLED="true"; else PREPUSH_ENABLED="false"; fi
595
+
596
+ # local model layer (opt-in: mlx_lm.server for NL chores)
597
+ case "$LOCAL_LAYER" in on|true|yes) LOCAL_ON="true";; *) LOCAL_ON="false";; esac
598
+
599
+ # stack-gated build skills present in this profile?
600
+ if grep -qE '^(feature-build-refine|supabase-patterns)$' <<<"$SKILLS"; then STACK_SKILLS_ON="true"; else STACK_SKILLS_ON="false"; fi
601
+
602
+ # code graph (an integration, switched on by `cckit add <id>` — never by an init flag). A fresh
603
+ # scaffold therefore has it off; only --upgrade can find it on, in the project's existing config.
604
+ CODEGRAPH_ON="false"
605
+ if [[ $UPGRADE -eq 1 ]]; then
606
+ _cg="$(jq -r '.codeGraph.provider // ""' "$UP_CFG" 2>/dev/null || true)"
607
+ [[ -n "$_cg" && "$_cg" != "none" ]] && CODEGRAPH_ON="true"
608
+ fi
609
+
610
+ # FLAGS_ON drives <!-- IF:X --> resolution
611
+ FLAGS_ON=""
612
+ [[ "$PROJECTS_V2" == "true" ]] && FLAGS_ON="${FLAGS_ON}PROJECTS_V2,"
613
+ [[ "$PLANS_ON" == "true" ]] && FLAGS_ON="${FLAGS_ON}PLANS,"
614
+ [[ "$DESIGN_ON" == "true" ]] && FLAGS_ON="${FLAGS_ON}DESIGN,"
615
+ [[ "$SPECKIT_ON" == "true" ]] && FLAGS_ON="${FLAGS_ON}SPECKIT,"
616
+ [[ "$STACK_SKILLS_ON" == "true" ]] && FLAGS_ON="${FLAGS_ON}STACK_SKILLS,"
617
+ [[ "$CODEGRAPH_ON" == "true" ]] && FLAGS_ON="${FLAGS_ON}CODEGRAPH,"
618
+ [[ "$TASKS_DEFAULT" == "light" ]] && FLAGS_ON="${FLAGS_ON}LIGHT_TASKS,"
619
+ # The counterpart, so the instruction file can say "GitHub issues are the task list" only where
620
+ # that is true (#487).
621
+ [[ "$TASKS_DEFAULT" == "light" ]] || FLAGS_ON="${FLAGS_ON}ISSUE_TASKS,"
622
+
623
+ # ---- build agent/skill tables -------------------------------------------
624
+ # short_desc <file> -> the frontmatter description, at most 92 characters. A folded or literal
625
+ # block (`description: >` with the text on the indented lines below it) is joined into one line;
626
+ # the old one-line read printed the `>` itself in the skills table (#487).
627
+ short_desc() {
628
+ awk '
629
+ /^---[[:space:]]*$/ { fm++; if (fm == 2) exit; next }
630
+ fm != 1 { next }
631
+ inblock {
632
+ if ($0 ~ /^[[:space:]]+[^[:space:]]/) { t = $0; sub(/^[[:space:]]+/, "", t); sub(/[[:space:]]+$/, "", t); out = out (out == "" ? "" : " ") t; next }
633
+ if ($0 ~ /^[[:space:]]*$/) next
634
+ inblock = 0
635
+ }
636
+ /^description:/ && !seen {
637
+ seen = 1; v = $0; sub(/^description:[[:space:]]*/, "", v); sub(/[[:space:]]+$/, "", v)
638
+ if (v == "" || v ~ /^[>|][+-]?$/) { inblock = 1 } else { out = v }
639
+ }
640
+ END { print out }' "$1" | cut -c1-92
641
+ }
642
+ AGENT_TABLE="| Agent | Role |
643
+ | ----- | ---- |"
644
+ for a in $AGENTS; do
645
+ f="$KIT_ROOT/kit/templates/agents/$a.md"
646
+ [[ -f "$f" ]] || { echo " missing agent template: $a" >&2; continue; }
647
+ AGENT_TABLE="$AGENT_TABLE
648
+ | \`$a\` | $(short_desc "$f") |"
649
+ done
650
+ SKILL_TABLE="| Skill | When |
651
+ | ----- | ---- |"
652
+ for s in $SKILLS; do
653
+ f="$KIT_ROOT/kit/templates/skills/$s/SKILL.md"
654
+ [[ -f "$f" ]] || { echo " missing skill template: $s" >&2; continue; }
655
+ SKILL_TABLE="$SKILL_TABLE
656
+ | \`$s\` | $(short_desc "$f") |"
657
+ done
658
+
659
+ # ---- export vars for substitution ---------------------------------------
660
+ export PROJECT_NAME="$NAME" PROJECT_SLUG="$SLUG" OWNER_NAME="$OWNER_NAME" COMMS_LANG="$LANG_PREF"
661
+ export PROFILE GH_REPO="$REPO" GH_OWNER="$GH_OWNER" PROJECT_NUMBER PROJECT_BOARD_TITLE="$NAME"
662
+ export BASE_BRANCH
663
+ export PLANS_DIR PLANS_FORMAT KNOWLEDGE_DIR="knowledge"
664
+ export MILESTONES_HUMAN ROLES_HUMAN AGENT_TABLE SKILL_TABLE
665
+ export KIT_FLAGS_ON="$FLAGS_ON"
666
+
667
+ # ---- per-adapter layout --------------------------------------------------
668
+ # One template set renders for both adapters. What differs is where the files land ({{ADAPTER_DIR}}),
669
+ # which IF:ADAPTER_* blocks survive, and the instruction file name — nothing else.
670
+ KITCFG="$(adapter_config_existing "$TARGET" || true)"
671
+ [[ -n "$KITCFG" ]] || KITCFG="$(adapter_config_default "$TARGET" "$ADAPTERS")"
672
+ CONFIG_REL="${KITCFG#"$TARGET"/}"
673
+ MANIFEST_REL="$(adapter_manifest_rel "$TARGET" "$ADAPTERS")"
674
+ export CONFIG_REL
675
+ _rule_enabled() { # <rule> -> 0 unless its feature is off for this project
676
+ case "$1" in
677
+ design-routing) [[ "$DESIGN_ON" == "true" ]] || return 1 ;;
678
+ plan-output-format) [[ "$PLANS_ON" == "true" ]] || return 1 ;;
679
+ plain-language) [[ "$PLAIN_ON" == "true" ]] || return 1 ;;
680
+ esac
681
+ return 0
682
+ }
683
+ # The plain-language rule follows its switch, not the profile list: a profile that lists it gets it
684
+ # only while the switch is on, and `--plain-language on` adds it to a profile that does not list it.
685
+ if [[ "$PLAIN_ON" == "true" ]] && ! grep -qx 'plain-language' <<<"$RULES"; then
686
+ RULES="$RULES
687
+ plain-language"
688
+ fi
689
+ RULE_LIST=""
690
+ for r in $RULES; do
691
+ _rule_enabled "$r" && [[ -f "$KIT_ROOT/kit/templates/rules/$r.md" ]] || continue
692
+ RULE_LIST="${RULE_LIST:+$RULE_LIST
693
+ }- \`.agents/rules/$r.md\`"
694
+ done
695
+ export RULE_LIST
696
+ use_adapter() { # <claude|codex> — point emit at that adapter's dir + IF blocks
697
+ ADAPTER_DIR="$(adapter_dir "$1")"
698
+ KIT_FLAGS_ON="${FLAGS_ON}ADAPTER_$(printf '%s' "$1" | tr '[:lower:]' '[:upper:]'),"
699
+ ADAPTER_INSTRUCTIONS="$(adapter_instructions "$1")"
700
+ export ADAPTER_DIR KIT_FLAGS_ON ADAPTER_INSTRUCTIONS
701
+ }
702
+ # Shared (adapter-neutral) files that name a rules path point at the first selected adapter's copy.
703
+ use_adapter "${ADAPTERS%% *}"
704
+
705
+ # emit: resolve IF blocks, then substitute {{VARS}}
706
+ render_tpl() { # <src> -> stdout
707
+ perl -0777 -pe '
708
+ my %on = map { $_=>1 } grep { length } split /,/, ($ENV{KIT_FLAGS_ON}//"");
709
+ 1 while s{<!-- IF:(\w+) -->(.*?)<!-- /IF:\1 -->}{ $on{$1} ? $2 : "" }gse;
710
+ s/\{\{(\w+)\}\}/ exists $ENV{$1} ? $ENV{$1} : "{{$1}}" /ge;
711
+ ' "$1"
712
+ }
713
+ emit() { # <src> <dest>
714
+ mkdir -p "$(dirname "$2")"
715
+ render_tpl "$1" > "$2"
716
+ WRITTEN+=("${2#"$TARGET"/}")
717
+ }
718
+ WRITTEN=() # every path this run wrote (not preserved) — the adapter-managed ones go in the manifest
719
+
720
+ # _upgrade_excluded: true when <rel> was removed/renamed by the project on purpose
721
+ # (registered in kit.config.json `upgrade.removed[]` / `upgrade.renamed{}`). Matches an
722
+ # entry exactly OR as a directory prefix (entry "/.claude/skills/foo" covers its SKILL.md
723
+ # + references/). Only meaningful in --upgrade; always false otherwise. (#334)
724
+ _upgrade_excluded() { # <rel>
725
+ [[ $UPGRADE -eq 1 ]] || return 1
726
+ local rel="$1" e
727
+ while IFS= read -r e; do
728
+ [[ -z "$e" ]] && continue
729
+ [[ "$rel" == "$e" || "$rel" == "$e"/* ]] && return 0
730
+ done <<< "${UPGRADE_EXCLUDE:-}"
731
+ return 1
732
+ }
733
+
734
+ # safe_write: in --upgrade, never overwrite an existing file (preserve user edits) and never
735
+ # re-add a config-excluded path — only add genuinely new files. In normal mode it is just emit.
736
+ # Tracks added / preserved / skipped-by-config paths for the upgrade summary. (#334)
737
+ safe_write() { # <src> <dest>
738
+ local rel="${2#"$TARGET"/}"
739
+ if _upgrade_excluded "$rel"; then
740
+ UPGRADE_SKIPPED+=("$rel"); return 0
741
+ fi
742
+ if [[ $UPGRADE -eq 1 && -e "$2" ]]; then
743
+ UPGRADE_PRESERVED+=("$rel"); return 0
744
+ fi
745
+ emit "$1" "$2"
746
+ [[ $UPGRADE -eq 1 ]] && UPGRADE_ADDED+=("$rel")
747
+ return 0
748
+ }
749
+
750
+ # safe_copy: verbatim (non-templated) counterpart of safe_write for kit-owned files copied
751
+ # as-is (scripts/, skill references). Honors the SAME upgrade contract — existing files are
752
+ # PRESERVED, config-excluded paths are SKIPPED — fixing the downgrade where scripts/ used to
753
+ # be unconditionally overwritten with older templates. Pass "exec" as $3 to chmod +x. (#334)
754
+ safe_copy() { # <src> <dest> [exec]
755
+ local rel="${2#"$TARGET"/}"
756
+ [[ -f "$1" ]] || return 0
757
+ if _upgrade_excluded "$rel"; then
758
+ UPGRADE_SKIPPED+=("$rel"); return 0
759
+ fi
760
+ if [[ $UPGRADE -eq 1 && -e "$2" ]]; then
761
+ UPGRADE_PRESERVED+=("$rel"); return 0
762
+ fi
763
+ mkdir -p "$(dirname "$2")"
764
+ cp "$1" "$2"
765
+ WRITTEN+=("$rel")
766
+ [[ "${3:-}" == "exec" ]] && chmod +x "$2" 2>/dev/null
767
+ [[ $UPGRADE -eq 1 ]] && UPGRADE_ADDED+=("$rel")
768
+ return 0
769
+ }
770
+
771
+ # hook_write: safe_write for a kit-owned HOOK (#370). In --upgrade an existing hook is refreshed when
772
+ # it is an unmodified copy of an earlier template version (hook_refresh_state: the manifest hash or
773
+ # cckit's own template history), and reported as drifted — never overwritten — when the repo
774
+ # customized it. Everything else (new file, config-excluded path, normal init) is safe_write.
775
+ hook_write() { # <src> <dest>
776
+ local rel="${2#"$TARGET"/}" state
777
+ if [[ $UPGRADE -ne 1 || ! -e "$2" ]] || _upgrade_excluded "$rel"; then safe_write "$1" "$2"; return 0; fi
778
+ state="$(hook_refresh_state "$2" "$1" "$KIT_ROOT" "$TARGET/$MANIFEST_REL" "$rel")"
779
+ case "$state" in
780
+ refresh) emit "$1" "$2"; UPGRADE_REFRESHED+=("$rel") ;;
781
+ drifted) UPGRADE_DRIFTED+=("$rel") ;;
782
+ *) UPGRADE_PRESERVED+=("$rel") ;;
783
+ esac
784
+ return 0
785
+ }
786
+
787
+ # ---- git + GitHub setup: the whole plan first, then ONE confirmation (#456) ----
788
+ # Nothing has been written yet. --yes confirms; a terminal is asked once; with neither, the setup
789
+ # does not run and the run says so at the end (re-run with --yes). A dry run only prints the plan.
790
+ IG_TARGET="$TARGET"; IG_REPO="$REPO"; IG_BASE="$BASE_BRANCH"; IG_VISIBILITY="$REPO_VISIBILITY"
791
+ IG_PROJECTS_V2="$PROJECTS_V2"; IG_KIT_ROOT="$KIT_ROOT"; IG_DOCTOR=1; IG_CONFIG_REL="$CONFIG_REL"
792
+ [[ "${KIT_INIT_PREFLIGHT:-1}" == "0" ]] && IG_DOCTOR=0 # the fixture tests' switch: no doctor, no network
793
+ SETUP_DECLINED=0
794
+ if [[ $SETUP_WANTED -eq 1 && $DRY_RUN -eq 0 ]]; then
795
+ echo ""
796
+ echo "cckit init is about to set up \"$NAME\" in $TARGET"
797
+ [[ -n "$RECIPE_FILE" ]] && echo " Starting point: $(jq -r .label "$RECIPE_FILE")"
798
+ [[ -n "$DB_FILE" ]] && echo " Database: $(jq -r .label "$DB_FILE")"
799
+ echo " Git and GitHub setup:"
800
+ init_setup_plan | sed 's/^/ /'
801
+ if [[ $ASSUME_YES -eq 1 ]]; then
802
+ SETUP_RUN=1
803
+ else
804
+ printf '\nGo ahead? [Y/n] (n = only the files in the folder; nothing is created on GitHub) '
805
+ IFS= read -r _setup_ok || _setup_ok="n"
806
+ case "$_setup_ok" in ""|y|Y|yes|Yes|YES) SETUP_RUN=1 ;; *) SETUP_DECLINED=1 ;; esac
807
+ fi
808
+ # Declined: the project keeps the base branch it would have had without the setup.
809
+ if [[ $SETUP_RUN -eq 0 && $SETUP_BASE_CHOSEN -eq 1 ]]; then BASE_BRANCH="$(_init_fallback_base)"; IG_BASE="$BASE_BRANCH"; fi
810
+ fi
811
+
812
+ # _init_finish — the closing lines of a guided run: the database guide, why the setup did not run
813
+ # (when it did not), the step totals. Exits 1 when a step failed, 0 otherwise.
814
+ _init_finish() {
815
+ local _d _s _f
816
+ if [[ -n "$DB_FILE" ]]; then database_guide "$DB_FILE" "$TARGET"; fi
817
+ if [[ $UPGRADE -eq 0 && $NO_GITHUB -eq 0 && $SETUP_RUN -eq 0 ]]; then
818
+ echo ""
819
+ if [[ $SETUP_DECLINED -eq 1 ]]; then
820
+ echo "Git and GitHub setup: not run (you answered no). To run it later: cckit init --yes (inside $TARGET)"
821
+ else
822
+ echo "Git and GitHub setup: not run — there is no terminal to confirm on."
823
+ echo " To run it: cckit init --yes (inside $TARGET). To leave it out on purpose: --no-github."
824
+ fi
825
+ fi
826
+ # A run without the setup says what it left undone, with the command that does it (#487).
827
+ if [[ $UPGRADE -eq 0 && $SETUP_RUN -eq 0 ]]; then
828
+ if ! git -C "$TARGET" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
829
+ if [[ $NO_GITHUB -eq 0 ]]; then
830
+ echo " Not done: $TARGET is not a git repository yet, so no task can be started in it."
831
+ echo " cckit init --yes starts version control, saves the cckit files and creates the branch '$BASE_BRANCH'."
832
+ fi
833
+ else
834
+ if init_base_missing; then
835
+ echo ""
836
+ echo "Not done: the branch '$BASE_BRANCH' (github.baseBranch in $CONFIG_REL) does not exist in this repository."
837
+ echo " cckit start and cckit task start cut their work from it and stop until it exists."
838
+ echo " To create it and push it: cckit init --yes (inside $TARGET)"
839
+ fi
840
+ if init_scaffold_pending; then
841
+ echo ""
842
+ echo "Not done: the cckit files are not committed. A task started now gets a folder without them,"
843
+ echo " and a teammate who clones the project gets no setup."
844
+ if [[ $NO_GITHUB -eq 1 ]]; then
845
+ echo " To commit them, on the branch '$BASE_BRANCH': $(init_scaffold_command)"
846
+ else
847
+ echo " To commit them on '$BASE_BRANCH' and push: cckit init --yes (inside $TARGET)"
848
+ fi
849
+ fi
850
+ fi
851
+ fi
852
+ if [[ -n "$INIT_STEP_LOG" ]]; then
853
+ _d="$(printf '%s' "$INIT_STEP_LOG" | grep -c '^ \[done' || true)"
854
+ _s="$(printf '%s' "$INIT_STEP_LOG" | grep -c '^ \[skipped' || true)"
855
+ _f="$(printf '%s' "$INIT_STEP_LOG" | grep -c '^ \[failed' || true)"
856
+ echo ""
857
+ echo "Result: $_d done · $_s skipped (already there, or not available) · $_f failed"
858
+ if [[ "$_f" -gt 0 ]]; then
859
+ printf '%s' "$INIT_STEP_LOG" | grep '^ \[failed'
860
+ echo "Fix what failed and run cckit init again: finished steps are skipped."
861
+ fi
862
+ fi
863
+ if [[ -n "$RECIPE_FILE" && $INIT_FAILED -eq 0 ]]; then
864
+ _s="$(jq -r '.start // empty' "$RECIPE_FILE")"
865
+ [[ -n "$_s" ]] && echo "To see the project running: cd $TARGET && $_s"
866
+ fi
867
+ [[ $INIT_FAILED -gt 0 ]] && exit 1
868
+ exit 0
869
+ }
870
+
871
+ # ---- dry run: print the plan and exit, writing nothing -------------------
872
+ if [[ $DRY_RUN -eq 1 ]]; then
873
+ echo "-> DRY RUN — nothing will be written. Plan for $TARGET:"
874
+ [[ $UPGRADE -eq 1 ]] && echo " (UPGRADE: existing files are PRESERVED; only genuinely-missing files are added; config keys merged.)"
875
+ [[ $UPGRADE -eq 1 && -n "${UPGRADE_EXCLUDE:-}" ]] && { echo " (SKIPPED by kit.config upgrade.removed/renamed — never re-added:)"; printf '%s\n' "${UPGRADE_EXCLUDE}" | sed 's/^/ - /'; }
876
+ echo ""
877
+ echo " Profile : $PROFILE"
878
+ echo " Project : $NAME ($SLUG)"
879
+ echo " Repo : $REPO"
880
+ echo " Base : $BASE_BRANCH (rules that name the integration branch render this)"
881
+ echo " Adapters: $ADAPTERS$([[ $ADAPTER_ADDED -eq 1 ]] && echo " (adding to stored: $ADAPTERS_HAVE)")"
882
+ echo " Board : $([[ "$PROJECTS_V2" == "true" ]] && echo "Projects v2 #$PROJECT_NUMBER" || echo "off (gh issues only)")"
883
+ echo " Plans : $PLANS_FORMAT Lang: $LANG_PREF"
884
+ echo " Spec Kit: $SPECKIT_ON Pre-push gate: $PREPUSH_ENABLED Local model: $LOCAL_ON kitVersion: $PLUGIN_VERSION"
885
+ [[ "$CONTENT_KINDS_ON" == "true" ]] && echo " Content : kind labels post · campaign · brand · research"
886
+ [[ "$PR_BODY_STYLE" == "content" ]] && echo " PR text : content (draft and review wording)"
887
+ [[ "$PLAIN_ON" == "true" ]] && echo " Wording : plain language (draft, review, approve)"
888
+ [[ "$TASKS_DEFAULT" == "light" ]] && echo " Tasks : light tasks are the default list (cckit task; no GitHub issue per task)"
889
+ echo ""
890
+ if [[ $UPGRADE -eq 1 ]]; then
891
+ # An upgrade's dry run is the upgrade itself, run on a temporary copy of the kit-managed paths
892
+ # and measured (#448) — not the list below, which names everything a FRESH init can write.
893
+ upgrade_dry_run "$TARGET" "$KIT_ROOT/lib/verbs/init.sh" "${_INIT_ARGS[@]}" || exit $?
894
+ echo ""
895
+ echo " A = added · M = modified. Measured by running the upgrade on a temporary copy; $TARGET was not written."
896
+ echo "Re-run without --dry-run to apply."
897
+ exit 0
898
+ fi
899
+ # A project that already has the kit files (#456): a real run leaves them alone, so the plan is
900
+ # the database option and the setup steps, not the file list.
901
+ if [[ $INIT_ALREADY -eq 1 && ( $SETUP_WANTED -eq 1 || -n "$DB_FILE" ) ]]; then
902
+ echo " cckit files: already there ($CONFIG_REL) — a run leaves them as they are (--upgrade merges in new kit features)."
903
+ echo ""
904
+ if [[ -n "$DB_FILE" ]]; then
905
+ echo " Database option $(jq -r .id "$DB_FILE") would add what is missing of:"
906
+ database_plan "$DB_FILE" "$DB_STACK"
907
+ echo ""
908
+ fi
909
+ if [[ $SETUP_WANTED -eq 1 ]]; then
910
+ echo " Git and GitHub setup would run these steps (nothing is written to GitHub by a dry run):"
911
+ init_setup_plan | sed 's/^/ /'
912
+ echo ""
913
+ fi
914
+ echo "Re-run without --dry-run to apply."
915
+ exit 0
916
+ fi
917
+ echo " Would write:"
918
+ echo " $CONFIG_REL"
919
+ echo " $MANIFEST_REL (ownership manifest, gitignored)"
920
+ if adapter_has "$ADAPTERS" claude; then
921
+ echo " .claude/settings.local.json"
922
+ echo " .claude/hooks/repo-hygiene.sh (SessionStart: read-only repo-hygiene report)"
923
+ echo " .claude/hooks/guard-base-branch-commit.sh (PreToolUse Bash: block commits to base branch)"
924
+ echo " .claude/hooks/guard-raw-git-ops.sh (PreToolUse Bash, tracked settings.json: raw gh/git -> cckit verb)"
925
+ echo " .claude/hooks/kit_version_check.sh (SessionStart: update check)"
926
+ echo " .claude/hooks/kit-mail-check.sh (session mail, tracked settings.json: PostToolUse / UserPromptSubmit / SessionStart / Stop)"
927
+ for a in $AGENTS; do
928
+ [[ -f "$KIT_ROOT/kit/templates/agents/$a.md" ]] && echo " .claude/agents/$a/AGENT.md"
929
+ done
930
+ fi
931
+ for _ad in $ADAPTERS; do
932
+ _add="$(adapter_dir "$_ad")"
933
+ echo " $(adapter_instructions "$_ad")"
934
+ for r in $RULES; do
935
+ _rule_enabled "$r" || continue
936
+ [[ -f "$KIT_ROOT/kit/templates/rules/$r.md" ]] && echo " $_add/rules/$r.md"
937
+ done
938
+ [[ "$CODEGRAPH_ON" == "true" ]] && echo " $_add/rules/code-retrieval.md (codeGraph.provider is set)"
939
+ for s in $SKILLS; do
940
+ _dn="$s"; { [[ -n "$SKILL_PREFIX" ]] && _is_namespaced "$s"; } && _dn="${SKILL_PREFIX}${s}"
941
+ [[ -f "$KIT_ROOT/kit/templates/skills/$s/SKILL.md" ]] && echo " $_add/skills/$_dn/SKILL.md"
942
+ [[ -d "$KIT_ROOT/kit/templates/skills/$s/references" ]] && echo " $_add/skills/$_dn/references/"
943
+ done
944
+ done
945
+ if adapter_has "$ADAPTERS" codex; then
946
+ echo " .codex/hooks/ (repo-hygiene.sh, kit_version_check.sh, kit-mail-check.sh, guard-base-branch-commit.sh, guard-raw-git-ops.sh$([[ "$PREPUSH_ENABLED" == "true" ]] && echo ", prepush_gate.sh")$([[ "$LOCAL_ON" == "true" ]] && echo ", kit-local-status.sh"))"
947
+ echo " .codex/hooks.json (merged: cckit's registrations are added, yours are kept; trust them in Codex with /hooks)"
948
+ for a in $AGENTS; do
949
+ [[ -f "$KIT_ROOT/kit/templates/agents/$a.md" ]] && echo " .codex/agents/$a.toml"
950
+ done
951
+ fi
952
+ echo " scripts/ (kit-config.sh, gh-project.sh, kit-version-check.sh, setup-labels.sh, setup-milestones.sh, capture-project-ids.sh, task-sync.sh, knowledge-lint.sh, review-lint.sh + review-rules.conf, skill-frontmatter-lint.sh, instruction-budget-lint.sh)"
953
+ echo " knowledge/INDEX.md (knowledge-base manifest — rules/knowledge-base.md)"
954
+ if adapter_has "$ADAPTERS" claude; then
955
+ echo " .claude/lib/kit-sigil.sh (claude-kit attribution sigil helper, sourced by task-* footers)"
956
+ [[ "$PREPUSH_ENABLED" == "true" ]] && echo " .claude/hooks/prepush_gate.sh (PreToolUse Bash gate: $PREPUSH_CMD)"
957
+ [[ "$LOCAL_ON" == "true" ]] && echo " .claude/hooks/kit-local-status.sh (SessionStart: announce local model layer when alive)"
958
+ fi
959
+ echo ""
960
+ echo " Onboarding would offer (opt-in): gh auth · gws (if you use Google Workspace) · local model layer (--local on: NL chores at \$0 via mlx_lm.server)"
961
+ echo ""
962
+ if [[ -n "$RECIPE_FILE" ]]; then
963
+ echo " Recipe $(jq -r .id "$RECIPE_FILE") would run, in $TARGET:"
964
+ recipe_plan "$RECIPE_FILE"
965
+ echo ""
966
+ fi
967
+ if [[ -n "$DB_FILE" ]]; then
968
+ echo " Database option $(jq -r .id "$DB_FILE") would add:"
969
+ database_plan "$DB_FILE" "$DB_STACK"
970
+ echo ""
971
+ fi
972
+ if [[ $SETUP_WANTED -eq 1 ]]; then
973
+ echo " Git and GitHub setup would run these steps (nothing is written to GitHub by a dry run):"
974
+ init_setup_plan | sed 's/^/ /'
975
+ echo ""
976
+ elif [[ $NO_GITHUB -eq 1 ]]; then
977
+ echo " Git and GitHub setup: off (--no-github)."
978
+ echo ""
979
+ fi
980
+ echo "Re-run without --dry-run to write these files."
981
+ exit 0
982
+ fi
983
+
984
+ # ---- guard ---------------------------------------------------------------
985
+ if [[ -e "$KITCFG" && $FORCE -eq 0 && $UPGRADE -eq 0 ]]; then
986
+ # A re-run of the guided setup on a project that already has the kit files (#456): the files are
987
+ # left as they are, and only the steps that are still missing run.
988
+ if [[ $SETUP_RUN -eq 1 || -n "$DB_FILE" ]]; then
989
+ echo ""
990
+ echo "-> $TARGET already has the cckit files ($CONFIG_REL)"
991
+ init_step skipped "cckit files" "already there (cckit init --upgrade merges in new kit features)"
992
+ if [[ -n "$DB_FILE" ]]; then database_apply "$DB_FILE" "$DB_STACK" "$TARGET"; fi
993
+ if [[ $SETUP_RUN -eq 1 ]]; then init_setup_run; fi
994
+ _init_finish
995
+ fi
996
+ echo " $TARGET already initialized ($CONFIG_REL). Re-run with --upgrade to merge in new features (preserves your edits), or --force to overwrite generated files."
997
+ exit 1
998
+ fi
999
+
1000
+ # ---- recipe + database: the project itself, before the kit files (#456) ---
1001
+ # The recipe's generators want an empty folder, so they run first; the kit files land on top.
1002
+ if [[ -n "$RECIPE_FILE" ]]; then
1003
+ echo ""
1004
+ echo "-> Starting point: $(jq -r .label "$RECIPE_FILE") (this downloads packages and can take a few minutes)"
1005
+ if ! recipe_run "$RECIPE_FILE" "$TARGET"; then
1006
+ echo "" >&2
1007
+ echo "x The starting point could not be built, so nothing else was set up." >&2
1008
+ echo " Fix the problem shown above, delete the folder $TARGET, and run cckit init again." >&2
1009
+ exit 1
1010
+ fi
1011
+ fi
1012
+ if [[ -n "$DB_FILE" ]]; then
1013
+ echo ""
1014
+ echo "-> Database: $(jq -r .label "$DB_FILE")"
1015
+ mkdir -p "$TARGET"
1016
+ database_apply "$DB_FILE" "$DB_STACK" "$TARGET"
1017
+ fi
1018
+
1019
+ # ---- init banner (fresh init only; not on --upgrade) ---------------------
1020
+ # Beat 1: golden-angle plant animation (TTY + Unicode only, else skipped).
1021
+ # Beat 2: V4 box-drawing CLAUDE KIT banner snap (always).
1022
+ if [[ $UPGRADE -eq 0 ]]; then
1023
+ _kit_init_banner "$PLUGIN_VERSION"
1024
+ fi
1025
+
1026
+ UPGRADE_ADDED=(); UPGRADE_PRESERVED=(); UPGRADE_SKIPPED=(); UPGRADE_REFRESHED=(); UPGRADE_DRIFTED=()
1027
+ # Checksums of the kit-managed paths before the first write: the summary prints what differs after
1028
+ # the last one, so a file a later step merges into is reported as modified, not as preserved (#448).
1029
+ _UP_SNAP=""
1030
+ if [[ $UPGRADE -eq 1 && -z "${KIT_UPGRADE_SIMULATED:-}" ]]; then
1031
+ _UP_SNAP="$(mktemp -d)" && upgrade_snapshot "$TARGET" > "$_UP_SNAP/before"
1032
+ fi
1033
+ echo "-> $([[ $UPGRADE -eq 1 ]] && echo Upgrading || echo Scaffolding) $PROFILE profile $([[ $UPGRADE -eq 1 ]] && echo in || echo into) $TARGET"
1034
+ mkdir -p "$TARGET/scripts/lib" "$(dirname "$KITCFG")"
1035
+ adapter_has "$ADAPTERS" claude && mkdir -p "$TARGET/.claude/agents" "$TARGET/.claude/skills" "$TARGET/.claude/rules"
1036
+
1037
+ # ---- kit.config.json (built with jq for correctness) --------------------
1038
+ RECIPE_CFG='{}'
1039
+ [[ -n "$RECIPE_FILE" ]] && RECIPE_CFG="$(jq -c '.config // {}' "$RECIPE_FILE")"
1040
+ # Location: .claude/kit.config.json when the Claude adapter is selected (the layout every existing
1041
+ # project has); the root cckit.config.json for a Codex-only project, which gets no .claude/ tree.
1042
+ jq -n \
1043
+ --argjson adapters "$(adapter_json "$ADAPTERS")" \
1044
+ --arg kitver "$PLUGIN_VERSION" \
1045
+ --arg name "$NAME" --arg slug "$SLUG" --arg owner "$OWNER_NAME" --arg lang "$LANG_PREF" --arg path "$TARGET" \
1046
+ --arg profile "$PROFILE" --argjson roles "$ROLES_JSON" \
1047
+ --arg repo "$REPO" --arg ghowner "$GH_OWNER" \
1048
+ --arg base "$BASE_BRANCH" \
1049
+ --argjson pv2 "$PROJECTS_V2" --argjson pnum "$PNUM_JSON" --arg ptitle "$NAME" \
1050
+ --argjson milestones "$MS_JSON" \
1051
+ --arg pfmt "$PLANS_FORMAT" --arg pdir "$PLANS_DIR" \
1052
+ --argjson speckit "$SPECKIT_JSON" \
1053
+ --argjson ppen "$PREPUSH_ENABLED" --arg ppcmd "$PREPUSH_CMD" \
1054
+ --argjson localon "$LOCAL_ON" \
1055
+ --argjson ckinds "$CONTENT_KINDS_ON" --arg prbody "$PR_BODY_STYLE" --argjson plain "$PLAIN_ON" \
1056
+ --arg tasksdef "$TASKS_DEFAULT" \
1057
+ --argjson recipecfg "$RECIPE_CFG" \
1058
+ '{kitVersion:$kitver,
1059
+ adapters:$adapters,
1060
+ project:{name:$name,slug:$slug,owner:$owner,language:$lang,path:$path},
1061
+ profile:$profile, roles:$roles,
1062
+ github:{repo:$repo,owner:$ghowner,baseBranch:$base,projectsV2:$pv2,projectNumber:$pnum,projectTitle:$ptitle},
1063
+ milestones:$milestones,
1064
+ plans:{format:$pfmt,dir:$pdir},
1065
+ knowledge:{dir:"knowledge"},
1066
+ specKit:{enabled:$speckit},
1067
+ prePush:{enabled:$ppen,command:$ppcmd},
1068
+ local:{enabled:$localon,port:8080,model:"mlx-community/Qwen3-8B-4bit"}}
1069
+ # Content-work keys are written only when the profile turns them on, so the config of a
1070
+ # software project does not gain keys it never reads (E415).
1071
+ | if $ckinds then .kinds = {content:true} else . end
1072
+ | if $prbody == "content" then .pr = {bodyStyle:"content"} else . end
1073
+ | if $plain then .plainLanguage = {enabled:true} else . end
1074
+ | if $tasksdef == "light" then .tasks = {default:"light"} else . end
1075
+ # Config keys the recipe carries (`config` in kit/recipes/<id>.json), merged over the above: the
1076
+ # recipe knows the commands of the project it made (tasks.check, the gate of `cckit task run`).
1077
+ | . * $recipecfg
1078
+ # The config is committed by the guided setup, so it carries no absolute machine path (#490).
1079
+ | del(.project.path)' \
1080
+ > "$KITCFG.kit-new"
1081
+ if [[ $UPGRADE -eq 1 && -f "$KITCFG" ]]; then
1082
+ # deep-merge: existing values win (preserve user choices), new REQUIRED keys added, version
1083
+ # bumped. FEATURE blocks (plans/specKit/prePush/local) are additive only when the
1084
+ # project's config ALREADY has them — a missing feature key is a deliberate absence, and an
1085
+ # upgrade must never inject profile defaults over it (#149). `adapters` follows the same rule: a
1086
+ # config without the key means Claude, and an upgrade that changes nothing must not write it.
1087
+ # The project's key order is kept: a key the template adds goes after the keys already there, at
1088
+ # every depth. (`$tmpl * $have` gave the same values in the TEMPLATE's order, which moved the
1089
+ # project's own blocks around on every upgrade — #448.)
1090
+ jq -s --arg kitver "$PLUGIN_VERSION" '
1091
+ def fill($h; $t):
1092
+ if ($h | type) == "object" and ($t | type) == "object"
1093
+ then reduce ($t | keys_unsorted[]) as $k ($h;
1094
+ if has($k) then .[$k] = fill($h[$k]; $t[$k]) else .[$k] = $t[$k] end)
1095
+ else $h end;
1096
+ .[1] as $have
1097
+ | (.[0] | with_entries(.key as $k | select(
1098
+ (["plans","specKit","prePush","local","adapters","plainLanguage"] | index($k)) == null
1099
+ or ($have | has($k))))) as $tmpl
1100
+ | fill($have; $tmpl) | .kitVersion = $kitver' "$KITCFG.kit-new" "$KITCFG" \
1101
+ > "$KITCFG.merged"
1102
+ rm -f "$KITCFG.kit-new"
1103
+ # Existing values win in the merge — but an EXPLICIT --local flag is a user decision now,
1104
+ # so it overrides the preserved value (port/model stay as the user configured them).
1105
+ if [[ $LOCAL_EXPLICIT -eq 1 ]]; then
1106
+ jq --argjson v "$LOCAL_ON" '.local = ((.local // {port:8080,model:"mlx-community/Qwen3-8B-4bit"}) | .enabled = $v)' \
1107
+ "$KITCFG.merged" > "$KITCFG.tmp" && mv "$KITCFG.tmp" "$KITCFG.merged"
1108
+ fi
1109
+ # An explicit --adapter is a decision made now, so it is written even where the key was absent.
1110
+ if [[ -n "$ADAPTERS_NEW" ]]; then
1111
+ jq --argjson v "$(adapter_json "$ADAPTERS")" '.adapters = $v' "$KITCFG.merged" > "$KITCFG.tmp" && mv "$KITCFG.tmp" "$KITCFG.merged"
1112
+ fi
1113
+ # Written with the fewest changed lines: a version bump alone edits the one `kitVersion` line and
1114
+ # leaves the file's formatting as the project keeps it. A merge that adds keys re-serialises it.
1115
+ _cfg_how="$(json_write_minimal "$KITCFG.merged" "$KITCFG")"
1116
+ echo " + $CONFIG_REL (merged · kitVersion -> $PLUGIN_VERSION · $_cfg_how)"
1117
+ else
1118
+ mv "$KITCFG.kit-new" "$KITCFG"
1119
+ echo " + $CONFIG_REL"
1120
+ fi
1121
+
1122
+ # ---- agents: one role source, one definition per selected runtime (E257.6) ----
1123
+ # kit/templates/agents/<role>.md is the role. Claude Code reads it as markdown; Codex reads a custom
1124
+ # agent as TOML, so the SAME rendered role is translated by agent_def_codex (scripts/lib/agent-def.sh).
1125
+ if adapter_has "$ADAPTERS" claude; then
1126
+ use_adapter claude
1127
+ for a in $AGENTS; do
1128
+ src="$KIT_ROOT/kit/templates/agents/$a.md"
1129
+ [[ -f "$src" ]] || continue
1130
+ safe_write "$src" "$TARGET/.claude/agents/$a/AGENT.md"
1131
+ echo " + .claude/agents/$a/AGENT.md"
1132
+ done
1133
+ fi
1134
+ if adapter_has "$ADAPTERS" codex; then
1135
+ use_adapter codex
1136
+ # shellcheck source=/dev/null
1137
+ . "$KIT_ROOT/lib/core/agent-def.sh"
1138
+ for a in $AGENTS; do
1139
+ src="$KIT_ROOT/kit/templates/agents/$a.md"
1140
+ [[ -f "$src" ]] || continue
1141
+ _ca_rel=".codex/agents/$a.toml"; _ca_dst="$TARGET/$_ca_rel"
1142
+ if _upgrade_excluded "$_ca_rel"; then UPGRADE_SKIPPED+=("$_ca_rel"); continue; fi
1143
+ if [[ $UPGRADE -eq 1 && -e "$_ca_dst" ]]; then UPGRADE_PRESERVED+=("$_ca_rel"); continue; fi
1144
+ if [[ $UPGRADE -eq 0 && $FORCE -eq 0 && -e "$_ca_dst" ]]; then
1145
+ # A fresh init over a .codex/agents/ the user already keeps: their file wins, as AGENTS.md does.
1146
+ echo " = $_ca_rel (already there — kept; --force replaces it)"; continue
1147
+ fi
1148
+ _ca_tmp="$(mktemp)"
1149
+ render_tpl "$src" > "$_ca_tmp"
1150
+ mkdir -p "$TARGET/.codex/agents"
1151
+ if agent_def_codex "$_ca_tmp" > "$_ca_dst.kit-new"; then
1152
+ mv "$_ca_dst.kit-new" "$_ca_dst"
1153
+ WRITTEN+=("$_ca_rel")
1154
+ [[ $UPGRADE -eq 1 ]] && UPGRADE_ADDED+=("$_ca_rel")
1155
+ echo " + $_ca_rel"
1156
+ else
1157
+ rm -f "$_ca_dst.kit-new"
1158
+ echo " ! $_ca_rel not written — kit/templates/agents/$a.md has no name or description" >&2
1159
+ fi
1160
+ rm -f "$_ca_tmp"
1161
+ done
1162
+ fi
1163
+ use_adapter "${ADAPTERS%% *}"
1164
+
1165
+ # ---- instructions + rules + skills, once per selected adapter ------------
1166
+ # Same templates, same loop: the adapter decides the destination dir and the IF:ADAPTER_* blocks.
1167
+ # A second adapter's pass never touches the first one's tree.
1168
+ for _ad in $ADAPTERS; do
1169
+ use_adapter "$_ad"
1170
+ AD="$ADAPTER_DIR"; _instr="$(adapter_instructions "$_ad")"
1171
+
1172
+ # CLAUDE.md / AGENTS.md — one source (kit/templates/instructions.md.tmpl). A pre-existing AGENTS.md is
1173
+ # usually the project's own contract, so a fresh init keeps it unless --force (CLAUDE.md keeps its
1174
+ # historical behavior: a fresh init writes it).
1175
+ if [[ "$_ad" == "codex" && -e "$TARGET/$_instr" && $UPGRADE -eq 0 && $FORCE -eq 0 ]]; then
1176
+ echo " = $_instr exists — kept (re-run with --force to replace it with the cckit instructions)"
1177
+ else
1178
+ safe_write "$KIT_ROOT/kit/templates/instructions.md.tmpl" "$TARGET/$_instr"
1179
+ echo " + $_instr"
1180
+ fi
1181
+
1182
+ # ---- rules (filtered by enabled flags) ----------------------------------
1183
+ for r in $RULES; do
1184
+ # Skip rules whose feature is disabled for this project.
1185
+ _rule_enabled "$r" || continue
1186
+ src="$KIT_ROOT/kit/templates/rules/$r.md"
1187
+ [[ -f "$src" ]] || { echo " missing rule: $r"; continue; }
1188
+ safe_write "$src" "$TARGET/$AD/rules/$r.md"
1189
+ echo " + $AD/rules/$r.md"
1190
+ done
1191
+ # code-retrieval is in no profile: `cckit add <id>` writes it when a code-graph integration is
1192
+ # switched on. An upgrade re-adds it only for a project whose config already has one on.
1193
+ if [[ "$CODEGRAPH_ON" == "true" ]]; then
1194
+ safe_write "$KIT_ROOT/kit/templates/rules/code-retrieval.md" "$TARGET/$AD/rules/code-retrieval.md"
1195
+ echo " + $AD/rules/code-retrieval.md"
1196
+ fi
1197
+
1198
+ # ---- skills --------------------------------------------------------------
1199
+ for s in $SKILLS; do
1200
+ src="$KIT_ROOT/kit/templates/skills/$s/SKILL.md"
1201
+ [[ -f "$src" ]] || continue
1202
+ # Namespaced skills take $SKILL_PREFIX so we scaffold to the project's name (kit-task-close)
1203
+ # instead of a bare-name duplicate (task-close) alongside it.
1204
+ dest_name="$s"
1205
+ if [[ -n "$SKILL_PREFIX" ]] && _is_namespaced "$s"; then
1206
+ dest_name="${SKILL_PREFIX}${s}"
1207
+ fi
1208
+ safe_write "$src" "$TARGET/$AD/skills/$dest_name/SKILL.md"
1209
+ # If prefixed AND this was a FRESH scaffold (frontmatter still bare), rewrite name + sibling refs.
1210
+ # An existing (preserved) skill already reads `name: $dest_name`, so the guard skips it.
1211
+ if [[ "$dest_name" != "$s" && -f "$TARGET/$AD/skills/$dest_name/SKILL.md" ]] \
1212
+ && grep -qE "^name:[[:space:]]*${s}[[:space:]]*\$" "$TARGET/$AD/skills/$dest_name/SKILL.md"; then
1213
+ _prefix_skill_file "$TARGET/$AD/skills/$dest_name/SKILL.md"
1214
+ fi
1215
+ echo " + $AD/skills/$dest_name/SKILL.md"
1216
+ # bundled references for multi-file skills (copied verbatim, no templating).
1217
+ # Per-file safe_copy so the upgrade contract (preserve existing, skip excluded) applies.
1218
+ if [[ -d "$KIT_ROOT/kit/templates/skills/$s/references" ]]; then
1219
+ for _ref in "$KIT_ROOT/kit/templates/skills/$s/references/"*; do
1220
+ [[ -f "$_ref" ]] || continue
1221
+ safe_copy "$_ref" "$TARGET/$AD/skills/$dest_name/references/$(basename "$_ref")"
1222
+ done
1223
+ echo " + $AD/skills/$dest_name/references/"
1224
+ fi
1225
+ done
1226
+ done # adapters
1227
+ use_adapter "${ADAPTERS%% *}"
1228
+
1229
+ # ---- scripts (libs + setup + sync; kit-owned) ---------------------------
1230
+ # safe_copy honors the upgrade contract: on --upgrade an EXISTING script is PRESERVED, never
1231
+ # overwritten with an older template (the #334 downgrade that erased #307/#313/#314). Fresh
1232
+ # inits get the current scripts; to refresh a kit-owned script later, delete it then re-upgrade.
1233
+ safe_copy "$KIT_ROOT/lib/core/kit-config.sh" "$TARGET/scripts/lib/kit-config.sh"
1234
+ # kit-config.sh and kit-version-check.sh both source this resolver from beside themselves; without it
1235
+ # they only ever look at .claude/kit.config.json and miss a Codex-only project's root config.
1236
+ safe_copy "$KIT_ROOT/lib/core/config-path.sh" "$TARGET/scripts/lib/config-path.sh"
1237
+ safe_copy "$KIT_ROOT/lib/forge/gh-project.sh" "$TARGET/scripts/lib/gh-project.sh"
1238
+ safe_copy "$KIT_ROOT/lib/core/worktree-issue.sh" "$TARGET/scripts/lib/worktree-issue.sh"
1239
+ safe_copy "$KIT_ROOT/lib/core/role-identity.sh" "$TARGET/scripts/lib/role-identity.sh"
1240
+ safe_copy "$KIT_ROOT/lib/core/kit-local.sh" "$TARGET/scripts/lib/kit-local.sh"
1241
+ safe_copy "$KIT_ROOT/lib/core/effort-metrics.sh" "$TARGET/scripts/lib/effort-metrics.sh"
1242
+ safe_copy "$KIT_ROOT/lib/forge/gh-log.sh" "$TARGET/scripts/lib/gh-log.sh"
1243
+ safe_copy "$KIT_ROOT/lib/core/kit-events.sh" "$TARGET/scripts/lib/kit-events.sh"
1244
+ # task-sync.sh sources board-view.sh unguarded (set -e) and effort-plan.sh for `--plan`.
1245
+ safe_copy "$KIT_ROOT/lib/core/board-view.sh" "$TARGET/scripts/lib/board-view.sh"
1246
+ safe_copy "$KIT_ROOT/lib/verbs/effort-plan.sh" "$TARGET/scripts/lib/effort-plan.sh"
1247
+ safe_copy "$KIT_ROOT/lib/core/worktree-start.sh" "$TARGET/scripts/lib/worktree-start.sh"
1248
+ # pr-evidence.sh is the helper rules/gate-and-review.md tells a project to source; without this copy
1249
+ # `cckit agent lib` resolves to the project's own scripts/lib, where the helper would be missing.
1250
+ safe_copy "$KIT_ROOT/lib/core/pr-evidence.sh" "$TARGET/scripts/lib/pr-evidence.sh"
1251
+ safe_copy "$KIT_ROOT/lib/core/session-registry.sh" "$TARGET/scripts/lib/session-registry.sh"
1252
+ safe_copy "$KIT_ROOT/lib/core/kinds.sh" "$TARGET/scripts/lib/kinds.sh"
1253
+ safe_copy "$KIT_ROOT/lib/verbs/kit-version-check.sh" "$TARGET/scripts/kit-version-check.sh" exec
1254
+ for sc in setup-labels.sh setup-milestones.sh capture-project-ids.sh task-sync.sh knowledge-lint.sh \
1255
+ review-lint.sh skill-frontmatter-lint.sh instruction-budget-lint.sh; do
1256
+ _src="$KIT_ROOT/lib/verbs/$sc"; [[ -f "$_src" ]] || _src="$KIT_ROOT/lib/core/$sc" # #602: lints in core, the rest in verbs
1257
+ safe_copy "$_src" "$TARGET/scripts/$sc" exec
1258
+ done
1259
+ # review-lint is table-driven: the engine is useless without a rule table, so the starter table
1260
+ # travels with it. Every lint self-disables when it does not apply (no rule table / no skills dir /
1261
+ # no CLAUDE.md and no rules dir), so shipping them to every project is safe.
1262
+ safe_copy "$KIT_ROOT/lib/core/review-rules.conf" "$TARGET/scripts/review-rules.conf"
1263
+ # Seed each baseline ONCE so a fresh project starts green and only *new* defects fail. Never
1264
+ # regenerate an existing baseline - that would silently re-grandfather drift on upgrade.
1265
+ #
1266
+ # ASK for the path, never reconstruct it. This used to test "$_lint-baseline.txt", which is right
1267
+ # for review-lint and wrong for skill-frontmatter-lint (it writes skill-frontmatter-baseline.txt, no
1268
+ # "-lint"). The guessed name never existed, so the "already seeded" guard never matched and every
1269
+ # upgrade re-baselined it - exactly what the line above forbids.
1270
+ #
1271
+ # KIT_LINT_WALK=1 so the seed sees the files just written. init.sh usually scaffolds into a repo
1272
+ # that already has commits, where `git ls-files` is non-empty and the untracked kit files are
1273
+ # invisible to it; the baseline would come out empty and the gate would go red on the user's first
1274
+ # commit of the scaffold.
1275
+ for _lint in review-lint skill-frontmatter-lint instruction-budget-lint; do
1276
+ [[ -f "$TARGET/scripts/$_lint.sh" ]] || continue
1277
+ _baseline="$( cd "$TARGET" && bash "scripts/$_lint.sh" --baseline-path 2>/dev/null )"
1278
+ [[ -n "$_baseline" ]] || continue
1279
+ [[ -e "$TARGET/$_baseline" ]] && continue
1280
+ ( cd "$TARGET" && KIT_LINT_WALK=1 bash "scripts/$_lint.sh" --update ) >/dev/null 2>&1 || true
1281
+ done
1282
+ echo " + scripts/ (libs + setup + task-sync + version-check + knowledge-lint + review-lint + skill-frontmatter-lint + instruction-budget-lint)"
1283
+
1284
+ # ---- benchmark harness (generic machinery; kit-owned) + example dataset ----
1285
+ # The harness measures how well THIS project's own canonical docs can be found + used by a cold
1286
+ # agent (retrieval / adversarial / abstain / quality tiers). Machinery is kit-owned (refreshed on
1287
+ # upgrade); the datasets under benchmarks/ are the project's own — we scaffold an EXAMPLE set +
1288
+ # schema only, and safe_write preserves it on upgrade so growth is never clobbered.
1289
+ for bs in run.sh run-quality.sh report.sh gate.sh stop-check.sh benchmark-test.sh; do
1290
+ safe_copy "$KIT_ROOT/scripts/benchmark/$bs" "$TARGET/scripts/benchmark/$bs" exec
1291
+ done
1292
+ safe_copy "$KIT_ROOT/scripts/benchmark/lib/model.sh" "$TARGET/scripts/benchmark/lib/model.sh"
1293
+ safe_copy "$KIT_ROOT/scripts/benchmark/lib/bench-common.sh" "$TARGET/scripts/benchmark/lib/bench-common.sh"
1294
+ echo " + scripts/benchmark/ (doc-retrieval benchmark harness — see benchmarks/SCHEMA.md)"
1295
+
1296
+ mkdir -p "$TARGET/benchmarks"
1297
+ for bf in SCHEMA.md benchmark.config.json retrieval.jsonl adversarial.jsonl abstain.jsonl quality.jsonl .gitignore; do
1298
+ safe_write "$KIT_ROOT/kit/templates/benchmarks/$bf" "$TARGET/benchmarks/$bf"
1299
+ done
1300
+ echo " + benchmarks/ (example dataset + schema — grow your own; run: scripts/benchmark/report.sh)"
1301
+
1302
+ # ---- knowledge base (governed dir: INDEX manifest; upgrade-safe) ---------
1303
+ mkdir -p "$TARGET/knowledge"
1304
+ safe_write "$KIT_ROOT/kit/templates/knowledge-INDEX.md.tmpl" "$TARGET/knowledge/INDEX.md"
1305
+ echo " + knowledge/INDEX.md (manifest — see $ADAPTER_DIR/rules/knowledge-base.md)"
1306
+
1307
+ # ==== Claude adapter only: sigil lib, settings, hooks, statusline wiring ====
1308
+ # Everything from here to the matching `fi` is Claude Code configuration. A Codex-only project gets
1309
+ # none of it — Codex hook registration is E257.5 — and the body is left unindented on purpose so the
1310
+ # hook blocks keep their history.
1311
+ if adapter_has "$ADAPTERS" claude; then
1312
+
1313
+ # ---- .claude/lib (kit-sigil attribution helper; all projects; idempotent) ----
1314
+ # Sourced by the task-* skill footers + repo-hygiene to emit one "claude-kit"
1315
+ # attribution sigil per kit-action boundary (see knowledge/brand.md §6).
1316
+ mkdir -p "$TARGET/.claude/lib"
1317
+ safe_write "$KIT_ROOT/kit/templates/lib/kit-sigil.sh.tmpl" "$TARGET/.claude/lib/kit-sigil.sh"
1318
+ chmod +x "$TARGET/.claude/lib/kit-sigil.sh" 2>/dev/null || true
1319
+ echo " + .claude/lib/kit-sigil.sh"
1320
+
1321
+ # ---- settings.json (versioned — travels to worktrees + collaborators) ---
1322
+ # Unlike settings.local.json (gitignored, per-checkout), this one is CHECKED IN so the
1323
+ # captain's pre-authorized wave-merge (gh pr merge under an approved plan) works in every
1324
+ # worktree and for every collaborator without re-prompting. safe_write = preserve-if-exists.
1325
+ mkdir -p "$TARGET/.claude"
1326
+ safe_write "$KIT_ROOT/kit/templates/settings/settings.json.tmpl" "$TARGET/.claude/settings.json"
1327
+ echo " + .claude/settings.json (versioned captain merge permission)"
1328
+
1329
+ # ---- settings.local.json (+ hooks, additive) ----------------------------
1330
+ mkdir -p "$TARGET/.claude"
1331
+ safe_write "$KIT_ROOT/kit/templates/settings/settings.local.json.tmpl" "$TARGET/.claude/settings.local.json"
1332
+ SETTINGS="$TARGET/.claude/settings.local.json"
1333
+ # A merge that leaves the value as it was does not rewrite the file (json_write_minimal, #448).
1334
+ merge_settings() { jq "$@" "$SETTINGS" > "$SETTINGS.tmp" && json_write_minimal "$SETTINGS.tmp" "$SETTINGS" >/dev/null; }
1335
+ HOOK_NOTE=""
1336
+
1337
+ if [[ "$PREPUSH_ENABLED" == "true" && $UPGRADE -eq 0 ]]; then
1338
+ mkdir -p "$TARGET/.claude/hooks"
1339
+ emit "$KIT_ROOT/kit/templates/hooks/prepush_gate.sh.tmpl" "$TARGET/.claude/hooks/prepush_gate.sh"
1340
+ chmod +x "$TARGET/.claude/hooks/prepush_gate.sh"
1341
+ merge_settings --arg gate "$TARGET/.claude/hooks/prepush_gate.sh" \
1342
+ '.hooks.PreToolUse = [{matcher:"Bash",hooks:[{type:"command",command:$gate,timeout:120}]}]'
1343
+ HOOK_NOTE="$HOOK_NOTE + pre-push gate"
1344
+ fi
1345
+
1346
+ # ---- local model layer SessionStart hook (opt-in; idempotent, also on --upgrade) ----
1347
+ if [[ "$LOCAL_ON" == "true" ]]; then
1348
+ mkdir -p "$TARGET/.claude/hooks"
1349
+ hook_write "$KIT_ROOT/kit/templates/hooks/kit-local-status.sh.tmpl" "$TARGET/.claude/hooks/kit-local-status.sh"
1350
+ chmod +x "$TARGET/.claude/hooks/kit-local-status.sh" 2>/dev/null || true
1351
+ merge_settings --arg ls "$TARGET/.claude/hooks/kit-local-status.sh" \
1352
+ '.hooks.SessionStart = (((.hooks.SessionStart // []) | map(select((.hooks[0].command // "") != $ls))) + [{matcher:"",hooks:[{type:"command",command:$ls,timeout:10}]}])'
1353
+ HOOK_NOTE="$HOOK_NOTE + local-status"
1354
+ fi
1355
+
1356
+ # ---- repo-hygiene SessionStart hook (all projects; read-only; idempotent) ----
1357
+ mkdir -p "$TARGET/.claude/hooks"
1358
+ hook_write "$KIT_ROOT/kit/templates/hooks/repo-hygiene.sh.tmpl" "$TARGET/.claude/hooks/repo-hygiene.sh"
1359
+ chmod +x "$TARGET/.claude/hooks/repo-hygiene.sh" 2>/dev/null || true
1360
+ merge_settings --arg rh "$TARGET/.claude/hooks/repo-hygiene.sh" \
1361
+ '.hooks.SessionStart = (((.hooks.SessionStart // []) | map(select((.hooks[0].command // "") != $rh))) + [{matcher:"",hooks:[{type:"command",command:$rh,timeout:10}]}])'
1362
+ HOOK_NOTE="$HOOK_NOTE + repo-hygiene"
1363
+
1364
+ # ---- base-branch commit guard PreToolUse(Bash) hook (all projects; idempotent) ----
1365
+ mkdir -p "$TARGET/.claude/hooks"
1366
+ hook_write "$KIT_ROOT/kit/templates/hooks/guard-base-branch-commit.sh.tmpl" "$TARGET/.claude/hooks/guard-base-branch-commit.sh"
1367
+ chmod +x "$TARGET/.claude/hooks/guard-base-branch-commit.sh" 2>/dev/null || true
1368
+ merge_settings --arg gd "$TARGET/.claude/hooks/guard-base-branch-commit.sh" \
1369
+ '.hooks.PreToolUse = (((.hooks.PreToolUse // []) | map(select((.hooks[0].command // "") != $gd))) + [{matcher:"Bash",hooks:[{type:"command",command:$gd,timeout:10}]}])'
1370
+ HOOK_NOTE="$HOOK_NOTE + commit-guard"
1371
+
1372
+ # ---- raw-git guard PreToolUse(Bash) hook (all projects; init AND --upgrade; idempotent) ----
1373
+ # Blocks raw gh issue create / pr create / pr merge / issue close and branch-from-base, and names the
1374
+ # cckit verb to run instead (#364). Wired in the TRACKED settings.json with $CLAUDE_PROJECT_DIR, not
1375
+ # settings.local.json: the local file is per-checkout and gitignored, so a guard wired there never
1376
+ # reaches a worktree — which is exactly where agents work.
1377
+ hook_write "$KIT_ROOT/kit/templates/hooks/guard-raw-git-ops.sh.tmpl" "$TARGET/.claude/hooks/guard-raw-git-ops.sh"
1378
+ chmod +x "$TARGET/.claude/hooks/guard-raw-git-ops.sh" 2>/dev/null || true
1379
+ _TRACKED="$TARGET/.claude/settings.json"
1380
+ [[ -f "$_TRACKED" ]] || echo '{}' > "$_TRACKED"
1381
+ # A registration that is already there — in its own block or inside one the project arranged — is
1382
+ # left where it is; the file is rewritten only when the guard had to be added (#448: the upgrade
1383
+ # moved it into a new block on every run and still listed settings.json as preserved).
1384
+ # shellcheck disable=SC2016 # $CLAUDE_PROJECT_DIR is expanded by Claude Code at hook time
1385
+ jq --arg g '$CLAUDE_PROJECT_DIR/.claude/hooks/guard-raw-git-ops.sh' \
1386
+ 'if ([ (.hooks.PreToolUse // [])[] | (.hooks // [])[] | .command ] | index($g)) != null then .
1387
+ else .hooks.PreToolUse = ((.hooks.PreToolUse // []) + [{matcher:"Bash",hooks:[{type:"command",command:$g,timeout:10}]}])
1388
+ end' "$_TRACKED" > "$_TRACKED.tmp" && json_write_minimal "$_TRACKED.tmp" "$_TRACKED" >/dev/null
1389
+ echo " + .claude/hooks/guard-raw-git-ops.sh (PreToolUse Bash, wired in tracked settings.json: raw gh/git -> cckit verb)"
1390
+
1391
+ # ---- session-mail hook (all projects; init AND --upgrade; idempotent) ----
1392
+ # One script on four events delivers `cckit agent msg` mail to a running session (E257.5). Wired in the
1393
+ # TRACKED settings.json for the same reason as the raw-git guard: mail has to reach worktrees. A
1394
+ # registration that is already there is left as it is (its timeout or position may be the user's),
1395
+ # and a project that listed the hook in `upgrade.removed` is not re-wired.
1396
+ if ! _upgrade_excluded ".claude/hooks/kit-mail-check.sh"; then
1397
+ hook_write "$KIT_ROOT/kit/templates/hooks/kit-mail-check.sh.tmpl" "$TARGET/.claude/hooks/kit-mail-check.sh"
1398
+ chmod +x "$TARGET/.claude/hooks/kit-mail-check.sh" 2>/dev/null || true
1399
+ # shellcheck disable=SC2016 # $CLAUDE_PROJECT_DIR is expanded by Claude Code at hook time
1400
+ jq --arg m 'bash "$CLAUDE_PROJECT_DIR/.claude/hooks/kit-mail-check.sh"' '
1401
+ reduce (["PostToolUse","*"], ["UserPromptSubmit",""], ["SessionStart",""], ["Stop",""]) as [$e, $mt] (.;
1402
+ if ([ (.hooks[$e] // [])[] | (.hooks // [])[] | .command ] | index($m)) != null then .
1403
+ else .hooks[$e] = ((.hooks[$e] // [])
1404
+ + [ (if $mt == "" then {} else {matcher: $mt} end) + {hooks: [{type: "command", command: $m, timeout: 10}]} ])
1405
+ end)' "$_TRACKED" > "$_TRACKED.tmp" && json_write_minimal "$_TRACKED.tmp" "$_TRACKED" >/dev/null
1406
+ echo " + .claude/hooks/kit-mail-check.sh (session mail, wired in tracked settings.json on 4 events)"
1407
+ fi
1408
+
1409
+ # ---- version-check SessionStart hook (all projects; idempotent) ---------
1410
+ mkdir -p "$TARGET/.claude/hooks"
1411
+ hook_write "$KIT_ROOT/kit/templates/hooks/kit_version_check.sh.tmpl" "$TARGET/.claude/hooks/kit_version_check.sh"
1412
+ chmod +x "$TARGET/.claude/hooks/kit_version_check.sh" 2>/dev/null || true
1413
+ merge_settings --arg vc "$TARGET/.claude/hooks/kit_version_check.sh" \
1414
+ '.hooks.SessionStart = (((.hooks.SessionStart // []) | map(select((.hooks[0].command // "") != $vc))) + [{matcher:"",hooks:[{type:"command",command:$vc,timeout:10}]}])'
1415
+ HOOK_NOTE="$HOOK_NOTE + version-check"
1416
+
1417
+ echo " + .claude/settings.local.json${HOOK_NOTE:+ (}${HOOK_NOTE# + }${HOOK_NOTE:+)}"
1418
+ else
1419
+ echo " · Codex adapter only: no .claude/ tree"
1420
+ fi # ==== end Claude adapter only ====
1421
+
1422
+ # ==== Codex adapter: hook scripts + their registration in .codex/hooks.json (E257.5) ====
1423
+ # The SAME hook templates as the Claude adapter, rendered into .codex/hooks/: Codex sends the event
1424
+ # fields they read (hook_event_name, cwd, tool_input.command) and honours the same answers (exit 2 +
1425
+ # stderr blocks a tool, decision:block continues a Stop, additionalContext injects text).
1426
+ CODEX_HOOKS_CHANGED=0
1427
+ if adapter_has "$ADAPTERS" codex; then
1428
+ # shellcheck source=/dev/null
1429
+ . "$KIT_ROOT/lib/core/codex-hooks.sh"
1430
+ # shellcheck source=/dev/null
1431
+ . "$KIT_ROOT/lib/core/kit-manifest.sh"
1432
+ mkdir -p "$TARGET/.codex/hooks"
1433
+ _CX_SRC="$(mktemp)"
1434
+ cp "$KIT_ROOT/kit/templates/codex/hooks.json.tmpl" "$_CX_SRC"
1435
+ _CX_HOOKS="repo-hygiene.sh kit_version_check.sh kit-mail-check.sh guard-base-branch-commit.sh guard-raw-git-ops.sh"
1436
+ _cx_optional() { # <event> <matcher> <script> <timeout> — an opt-in hook joins the set to register
1437
+ local frag; frag="$(mktemp)"
1438
+ codex_hooks_fragment "$1" "$2" "$3" "$4" > "$frag" && codex_hooks_merge "$_CX_SRC" "$frag" >/dev/null
1439
+ rm -f "$frag"
1440
+ _CX_HOOKS="$_CX_HOOKS $3"
1441
+ }
1442
+ [[ "$PREPUSH_ENABLED" == "true" ]] && _cx_optional PreToolUse Bash prepush_gate.sh 120
1443
+ [[ "$LOCAL_ON" == "true" ]] && _cx_optional SessionStart "" kit-local-status.sh 10
1444
+ for _h in $_CX_HOOKS; do
1445
+ _cx_dst="$TARGET/.codex/hooks/$_h"
1446
+ _cx_n=$(( ${#WRITTEN[@]} + ${#UPGRADE_REFRESHED[@]} ))
1447
+ if [[ $UPGRADE -eq 0 && $FORCE -eq 0 && -e "$_cx_dst" ]]; then
1448
+ # A fresh init over a .codex/hooks/ the user already keeps: their file wins, as AGENTS.md does.
1449
+ echo " = .codex/hooks/$_h (already there — kept; --force replaces it)"
1450
+ else
1451
+ hook_write "$KIT_ROOT/kit/templates/hooks/$_h.tmpl" "$_cx_dst"
1452
+ chmod +x "$_cx_dst" 2>/dev/null || true
1453
+ fi
1454
+ [[ $(( ${#WRITTEN[@]} + ${#UPGRADE_REFRESHED[@]} )) -gt $_cx_n ]] && CODEX_HOOKS_CHANGED=1
1455
+ done
1456
+ echo " + .codex/hooks/ ($(printf '%s' "$_CX_HOOKS" | sed 's/ /, /g'))"
1457
+ # hooks.json is the project's file. cckit's handlers are merged in (a handler already registered for
1458
+ # the event is skipped, everything else in the file is kept) and a merge that changes nothing does
1459
+ # not rewrite it. It is recorded as kit-owned only when cckit created it or the manifest still
1460
+ # proves it unmodified — a hooks.json holding the user's own entries is never claimed.
1461
+ _CX_JSON="$TARGET/.codex/hooks.json"; _cx_own=0
1462
+ if [[ ! -e "$_CX_JSON" ]]; then _cx_own=1
1463
+ elif [[ "$( cd "$TARGET" && KIT_MANIFEST="$MANIFEST_REL" kit_manifest_verify .codex/hooks.json 2>/dev/null )" == "intact" ]]; then _cx_own=1
1464
+ fi
1465
+ if _upgrade_excluded ".codex/hooks.json"; then
1466
+ UPGRADE_SKIPPED+=(".codex/hooks.json")
1467
+ elif _cx_state="$(codex_hooks_merge "$_CX_JSON" "$_CX_SRC")"; then
1468
+ case "$_cx_state" in
1469
+ created) echo " + .codex/hooks.json (SessionStart / PreToolUse Bash / PostToolUse / UserPromptSubmit / Stop)"
1470
+ [[ $UPGRADE -eq 1 ]] && UPGRADE_ADDED+=(".codex/hooks.json") ;;
1471
+ merged) echo " ~ .codex/hooks.json (cckit's hooks added; the entries already there are kept)" ;;
1472
+ *) [[ $UPGRADE -eq 1 ]] && UPGRADE_PRESERVED+=(".codex/hooks.json") ;;
1473
+ esac
1474
+ if [[ "$_cx_state" != "unchanged" ]]; then
1475
+ CODEX_HOOKS_CHANGED=1
1476
+ [[ $_cx_own -eq 1 ]] && WRITTEN+=(".codex/hooks.json")
1477
+ fi
1478
+ else
1479
+ echo " ! .codex/hooks.json was left untouched — cckit's Codex hooks are NOT registered. Fix the file, then run: cckit init --upgrade" >&2
1480
+ fi
1481
+ rm -f "$_CX_SRC"
1482
+ fi # ==== end Codex adapter hooks ====
1483
+
1484
+ # ---- recommended .gitignore entries (idempotent) ------------------------
1485
+ # kit.manifest.json is kit-managed bookkeeping (content hashes + timestamps, re-stamped on every
1486
+ # wire) — tracking it would dirty the tree on every session/upgrade, the exact churn #334 removes.
1487
+ GI="$TARGET/.gitignore"
1488
+ _GI_LOCAL=""; adapter_has "$ADAPTERS" claude && _GI_LOCAL=".claude/settings.local.json"
1489
+ # .cckit/ is cckit's local state (consent, mail, handoff, autopilot runs) and .claude/worktrees/ holds
1490
+ # the checkouts `cckit start` and `cckit task start` create: neither is ever committed (#487).
1491
+ for _ig in "$_GI_LOCAL" "$MANIFEST_REL" ".kann/" ".cckit/" ".claude/worktrees/"; do
1492
+ [[ -n "$_ig" ]] || continue
1493
+ if [[ -f "$GI" ]]; then grep -qxF "$_ig" "$GI" || printf '%s\n' "$_ig" >> "$GI"; else printf '%s\n' "$_ig" > "$GI"; fi
1494
+ done
1495
+
1496
+ # ---- wire (converge) -----------------------------------------------------
1497
+ # Runs on BOTH scaffold and --upgrade so every update re-wires (fixes the class of bug where
1498
+ # /kit-update refreshed files but never re-ran the wiring → statusline/settings drifted). #369
1499
+ if [[ -f "$KIT_ROOT/lib/verbs/kit-wire.sh" ]] && adapter_has "$ADAPTERS" claude; then
1500
+ # On --upgrade an EXISTING .claude/statusline.sh follows the same contract as safe_write: the
1501
+ # conffiles guard in kit-operate keeps a customized (or untracked) shim; only a shim the kit
1502
+ # provably owns intact is refreshed (#149).
1503
+ _SHIM="$TARGET/.claude/statusline.sh"
1504
+ _shim_before=""; [[ -f "$_SHIM" ]] && _shim_before="$(cat "$_SHIM" 2>/dev/null)"
1505
+ if ( cd "$TARGET" && KIT_MANIFEST="$MANIFEST_REL" CLAUDE_PLUGIN_ROOT="$KIT_ROOT" KIT_ASSUME_YES=1 bash "$KIT_ROOT/lib/verbs/kit-wire.sh" >/dev/null 2>&1 ); then
1506
+ echo " + wired: statusline shim + settings.statusLine (kit-wire)"
1507
+ fi
1508
+ if [[ $UPGRADE -eq 1 && -n "$_shim_before" && "$_shim_before" == "$(cat "$_SHIM" 2>/dev/null)" ]]; then
1509
+ UPGRADE_PRESERVED+=(".claude/statusline.sh")
1510
+ fi
1511
+ fi
1512
+
1513
+ # ---- ownership manifest: record what this run wrote ----------------------
1514
+ # Only the adapter files written NOW (never a preserved one — its content may be the user's), hashed
1515
+ # after every merge above so the recorded hash is the file as it was left. Keyed by path, so a
1516
+ # repeated upgrade updates nothing and adds nothing. This is what lets `cckit remove` and a later
1517
+ # upgrade tell a kit-owned file from an edited one for BOTH adapters.
1518
+ # shellcheck source=/dev/null
1519
+ . "$KIT_ROOT/lib/core/kit-manifest.sh"
1520
+ if [[ ${#WRITTEN[@]} -gt 0 ]]; then
1521
+ ( cd "$TARGET" && for _w in "${WRITTEN[@]}"; do
1522
+ if adapter_managed "$_w"; then printf '%s\t%s\n' "$(adapter_tier "$_w")" "$_w"; fi
1523
+ done | sort -u | KIT_MANIFEST="$MANIFEST_REL" kit_manifest_record_batch init "$PLUGIN_VERSION" ) \
1524
+ || echo " ! could not record the ownership manifest ($MANIFEST_REL)" >&2
1525
+ fi
1526
+
1527
+ # ---- summary -------------------------------------------------------------
1528
+ echo ""
1529
+ echo "claude-kit $([[ $UPGRADE -eq 1 ]] && echo upgraded || echo scaffolded) — profile: $PROFILE"
1530
+ echo " Project : $NAME ($SLUG)"
1531
+ echo " Repo : $REPO"
1532
+ echo " Adapters: $ADAPTERS"
1533
+ echo " Board : $([[ "$PROJECTS_V2" == "true" ]] && echo "Projects v2 #$PROJECT_NUMBER" || echo "off (gh issues only)")"
1534
+ echo " Agents : $(echo $AGENTS | tr '\n' ' ')"
1535
+ echo " Plans : $PLANS_FORMAT Lang: $LANG_PREF kitVersion: $PLUGIN_VERSION"
1536
+ echo " Spec Kit: $SPECKIT_ON Pre-push gate: $PREPUSH_ENABLED Local model: $LOCAL_ON Stack skills self-gate by package.json"
1537
+ [[ "$PLAIN_ON" == "true" ]] && echo " Wording : plain language (draft, review, approve)"
1538
+ [[ "$TASKS_DEFAULT" == "light" ]] && echo " Tasks : light tasks are the default list (cckit task; no GitHub issue per task)"
1539
+
1540
+ if [[ $UPGRADE -eq 1 ]]; then
1541
+ echo ""
1542
+ echo " Upgrade -> kitVersion $PLUGIN_VERSION"
1543
+ # What changed on disk, measured (the same block `--upgrade --dry-run` prints as "Would change").
1544
+ # A file the helpers left alone but a later step merged into — .claude/settings.json gaining a hook
1545
+ # registration — is in this block as M and is taken out of the Preserved list below.
1546
+ if [[ -n "$_UP_SNAP" && -f "$_UP_SNAP/before" ]]; then
1547
+ upgrade_snapshot "$TARGET" > "$_UP_SNAP/after"
1548
+ upgrade_changes "$_UP_SNAP/before" "$_UP_SNAP/after" > "$_UP_SNAP/changes"
1549
+ upgrade_changes_print "$_UP_SNAP/changes" "Changed on disk"
1550
+ _UP_MEASURED=1
1551
+ if [[ ${#UPGRADE_PRESERVED[@]} -gt 0 ]]; then
1552
+ _up_kept=()
1553
+ for _p in "${UPGRADE_PRESERVED[@]}"; do
1554
+ grep -qxF "M $_p" "$_UP_SNAP/changes" || _up_kept+=("$_p")
1555
+ done
1556
+ UPGRADE_PRESERVED=(); [[ ${#_up_kept[@]} -gt 0 ]] && UPGRADE_PRESERVED=("${_up_kept[@]}")
1557
+ fi
1558
+ rm -rf "$_UP_SNAP"
1559
+ fi
1560
+ # The A rows above already name every added file; the helper's own list is printed only when no
1561
+ # measurement was taken.
1562
+ if [[ ${_UP_MEASURED:-0} -eq 0 && ${#UPGRADE_ADDED[@]} -gt 0 ]]; then echo " Added (new in this kit version):"; printf ' + %s\n' "${UPGRADE_ADDED[@]}"; fi
1563
+ if [[ ${#UPGRADE_PRESERVED[@]} -gt 0 ]]; then echo " Preserved (your edits, untouched):"; printf ' = %s\n' "${UPGRADE_PRESERVED[@]}"; fi
1564
+ if [[ ${#UPGRADE_SKIPPED[@]} -gt 0 ]]; then echo " Skipped (removed/renamed via kit.config upgrade):"; printf ' - %s\n' "${UPGRADE_SKIPPED[@]}"; fi
1565
+ if [[ ${#UPGRADE_REFRESHED[@]} -gt 0 ]]; then echo " Refreshed (unmodified hook, now the current template):"; printf ' ~ %s\n' "${UPGRADE_REFRESHED[@]}"; fi
1566
+ if [[ ${#UPGRADE_DRIFTED[@]} -gt 0 ]]; then echo " Drifted (customized hook, kept — diff it against kit/templates/hooks/ by hand):"; printf ' ! %s\n' "${UPGRADE_DRIFTED[@]}"; fi
1567
+ [[ $CODEX_HOOKS_CHANGED -eq 1 ]] && echo " Codex hooks changed: Codex skips a new or changed hook until you trust it — run /hooks in Codex."
1568
+ else
1569
+ echo ""
1570
+ echo "Next steps:"
1571
+ echo " 1. Review $(adapter_instructions "${ADAPTERS%% *}") and $CONFIG_REL"
1572
+ # Labels and milestones belong to GitHub issues. A project whose task list is light tasks is not
1573
+ # told to seed them (#487); `cckit init --yes` still creates them with the rest of the setup.
1574
+ if [[ $SETUP_RUN -eq 0 && "$TASKS_DEFAULT" != "light" ]]; then
1575
+ echo " 2. ./scripts/setup-labels.sh && ./scripts/setup-milestones.sh # seed the repo"
1576
+ if [[ "$PROJECTS_V2" == "true" ]]; then echo " 3. ./scripts/capture-project-ids.sh # cache Projects v2 field IDs"; fi
1577
+ fi
1578
+ if [[ "$TASKS_DEFAULT" == "light" ]]; then
1579
+ echo " 2. Add the first task: cckit task new \"<title>\" then: cckit next"
1580
+ echo " 3. Start it: cckit task start <id>"
1581
+ fi
1582
+ [[ "$LOCAL_ON" == "true" ]] && echo " 4. /kit-doctor # local model layer: installs mlx-lm (recipe: kit/integrations/mlx-lm.json) + starts mlx_lm.server"
1583
+ # The page-design skill coordinates three third-party design skills. They are offered, never
1584
+ # installed by init: each is an integration the person switches on and consents to (#504).
1585
+ if grep -qx "page-design" <<<"$SKILLS"; then
1586
+ echo " · Pages: the page-design skill is installed. It works with three third-party design skills, off until you add them:"
1587
+ echo " cckit add impeccable && cckit add emil-design-eng && cckit add web-design-guidelines && cckit doctor --fix"
1588
+ fi
1589
+ adapter_has "$ADAPTERS" codex && echo " · Codex: commit .codex/, then run /hooks in Codex and trust the project hooks — Codex skips them until you do."
1590
+ fi
1591
+
1592
+ # ---- git + GitHub setup, then the closing report (#456) --------------------
1593
+ if [[ $UPGRADE -eq 0 ]]; then
1594
+ if [[ $SETUP_RUN -eq 1 ]]; then init_setup_run; fi
1595
+ _init_finish
1596
+ fi
1597
+
1598
+ exit 0