@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,591 @@
1
+ #!/usr/bin/env bash
2
+ # effort-metrics.sh — local-first effort telemetry capture (#796).
3
+ #
4
+ # Writes ONE record per effort to <git-common-dir>/effort-metrics/records.jsonl. The file is the
5
+ # whole store: nothing pushes it anywhere (the engine sync was retired in #561). `synced_to_graph`
6
+ # stays in the record shape so older rows and newer rows read the same; nothing sets it true.
7
+ #
8
+ # record_effort_start <num> — stamp start epoch + write the pre-build estimate (#808).
9
+ # Called by effort_start when it creates the branch (#453).
10
+ # capture_effort_metrics <num> [base] — at close: build-time + signals + token/cost actuals (#807)
11
+ # + reconcile vs the estimate → append the record
12
+ #
13
+ # Where each measured field comes from (#453). A field with no source is null and the record says why:
14
+ # build_seconds close time minus <num>.start (build_source "effort-start"); with no stamp, close
15
+ # time minus the author time of the oldest commit in <base>..HEAD ("first-commit");
16
+ # null when the branch has neither.
17
+ # tokens_real 1. sessions listed in <git-common-dir>/kit-usage.jsonl, summed from their Claude
18
+ # transcripts (tokens_source "transcripts", cost at list price). cckit ships no
19
+ # writer for that log; a project that keeps one gets this path.
20
+ # 2. else the `cckit autopilot run` state files of the effort branch and its
21
+ # sub/<N><letter> branches — the usage the runtime printed (tokens_source
22
+ # "autopilot-run", cost_source "runtime" when the runtime printed a cost).
23
+ # Interactive sessions are not in that figure.
24
+ # 3. else null, with tokens_null_reason: "no-usage-source" (no usage log, no
25
+ # autopilot run), "no-session-matched" (a usage log with no session for this
26
+ # effort), "autopilot-usage-unknown" (a run whose rounds printed no usage — a
27
+ # partial sum would be an undercount).
28
+ # No figure is estimated: a null stays null.
29
+ #
30
+ # Cost = metered API; retroactively $ is computed from current Opus-4.8 list price (no per-call bill
31
+ # access here) → cost_source="list-price". bash 3.2 compatible. Requires: git, jq.
32
+ # errors: best-effort — local telemetry must never break the op it measures
33
+
34
+ # This file's own directory, resolved ONCE at load time (#381) — the only point where both shells
35
+ # agree. `BASH_SOURCE` is bash-only; inside a zsh *function* it is unset and `$0` is the function
36
+ # name, so a per-call `dirname "${BASH_SOURCE[0]:-$0}"` resolved to the CALLER's directory:
37
+ # kit-local.sh was never found and the judge's local fallback was skipped without saying why. At
38
+ # FILE scope zsh sets `$0` to the sourced file's path, so one expansion covers both shells. Same
39
+ # resolution as kit-gc.sh's `_KIT_GC_LIB_DIR`, whose comment explains why zsh prompt expansion
40
+ # (`${(%):-%x}`) is avoided and why `cd`'s stdout is discarded.
41
+ # The forge port (E537): every GitHub call goes through forge_* / gh_rest_* (scripts/lib/gh-rest.sh).
42
+ if ! command -v gh_rest_issue_json >/dev/null 2>&1; then
43
+ # shellcheck source=/dev/null
44
+ . "$(_d="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"; [ -f "$_d/gh-rest.sh" ] || _d="$_d/../forge"; printf %s "$_d/gh-rest.sh")"
45
+ fi
46
+
47
+ _EM_LIB_DIR="$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null 2>&1 && pwd)"
48
+
49
+ # The GitHub transport switch (#396): rest mode routes the gh calls below through gh-rest.sh.
50
+ if ! command -v cckit_gh_rest >/dev/null 2>&1; then
51
+ _ghr_lib="$(_d="$_EM_LIB_DIR"; [ -f "$_d/gh-rest.sh" ] || _d="$_d/../forge"; printf %s "$_d/gh-rest.sh")"
52
+ # shellcheck source=/dev/null
53
+ [ -f "$_ghr_lib" ] && . "$_ghr_lib"
54
+ unset _ghr_lib
55
+ fi
56
+ command -v cckit_gh_rest >/dev/null 2>&1 || cckit_gh_rest() { return 1; }
57
+
58
+ # Opus 4.8 list price, USD per 1M tokens (correct as of 2026-06; update when prices change).
59
+ _EM_PRICE_IN=5.00; _EM_PRICE_OUT=25.00; _EM_PRICE_CACHE_READ=0.50; _EM_PRICE_CACHE_WRITE=6.25
60
+
61
+ _em_dir() {
62
+ local c; c="$(git rev-parse --git-common-dir 2>/dev/null)" || return 1
63
+ case "$c" in /*) : ;; *) c="$PWD/$c" ;; esac
64
+ printf '%s/effort-metrics' "$c"
65
+ }
66
+
67
+ record_effort_start() {
68
+ local num="$1" d
69
+ [[ -n "$num" ]] || return 0
70
+ d="$(_em_dir)" || return 0
71
+ mkdir -p "$d" 2>/dev/null || return 0
72
+ # Keep an existing stamp: a re-run of `effort start` must not move the start forward.
73
+ [[ -s "$d/$num.start" ]] || date -u +%s > "$d/$num.start" 2>/dev/null || true
74
+ [[ -f "$d/$num.est" ]] || estimate_effort "$num" || true
75
+ }
76
+
77
+ # estimate_effort <num> — pre-build prediction from estimator.json + the effort's scope (#808).
78
+ # Predicts changed-lines from the sub-issue count (crude v1), then tokens/cost/difficulty from the
79
+ # derived ratios. Writes <num>.est; capture merges it in for est-vs-real reconciliation.
80
+ estimate_effort() {
81
+ local num="$1" d est repo subs cpl med_changed med_commits changed_est tok_per_line tokens_est cost_est diff_est
82
+ [[ -n "$num" ]] || return 0
83
+ d="$(_em_dir)" || return 0; mkdir -p "$d" 2>/dev/null || return 0
84
+ est="$d/estimator.json"; [[ -f "$est" ]] || return 0
85
+ command -v jq >/dev/null 2>&1 || return 0
86
+
87
+ repo="${EFFORT_REPO:-${KIT_REPO:-}}"
88
+ if cckit_gh_rest; then subs="$(gh_rest_sub_issue_numbers "$repo" "$num" 2>/dev/null | grep -c .)"
89
+ else
90
+ subs="$(forge_rest graphql -f query='query($o:String!,$r:String!,$n:Int!){repository(owner:$o,name:$r){issue(number:$n){subIssues(first:50){totalCount}}}}' \
91
+ -F o="${repo%/*}" -F r="${repo#*/}" -F n="$num" --jq '.data.repository.issue.subIssues.totalCount' 2>/dev/null)"
92
+ fi
93
+ [[ "$subs" =~ ^[0-9]+$ ]] || subs=1; [[ "$subs" -lt 1 ]] && subs=1
94
+
95
+ cpl="$(jq -r '.model.cost_per_changed_line_usd // .model.cost_per_changed_line // 0.21' "$est")"
96
+ med_changed="$(jq -r '.model.median_changed_lines // 73' "$est")"
97
+ med_commits="$(jq -r '.model.median_commits // 1' "$est")"
98
+ tok_per_line="$(jq -r '.model.tokens_per_changed_line // 0' "$est")"
99
+ # tokens/cost are emitted ONLY when the estimator has real per-effort token data to calibrate
100
+ # against (#812). Until then `tokens_per_changed_line` is an aggregate UPPER BOUND (windowed
101
+ # transcripts incl. unrelated work ÷ a tiny line count) → wildly inflated (#787 saw ~110M). Emit
102
+ # null instead of a fabricated number; the changed-lines + difficulty estimate stays (reliable).
103
+ local tokens_calibrated; tokens_calibrated="$(jq -r '.model.tokens_calibrated // false' "$est")"
104
+ [[ "$med_commits" =~ ^[0-9]+$ && "$med_commits" -gt 0 ]] || med_commits=1
105
+
106
+ # changed-lines ≈ subs scaled against the median effort (median_changed per median_commits).
107
+ changed_est=$(awk -v s="$subs" -v mc="$med_changed" -v mk="$med_commits" 'BEGIN{printf "%d", s*(mc/mk)}')
108
+ diff_est=$(_em_difficulty "$changed_est" 0)
109
+ if [[ "$tokens_calibrated" == "true" ]]; then
110
+ cost_est=$(awk -v c="$changed_est" -v p="$cpl" 'BEGIN{printf "%.2f", c*p}')
111
+ tokens_est=$(awk -v c="$changed_est" -v t="$tok_per_line" 'BEGIN{printf "%d", c*t}')
112
+ else
113
+ cost_est=null; tokens_est=null
114
+ fi
115
+
116
+ jq -cn --argjson num "$num" --argjson ce "$changed_est" --argjson te "${tokens_est:-null}" \
117
+ --argjson ce_usd "${cost_est:-null}" --argjson de "$diff_est" \
118
+ '{effort:$num, changed_lines_est:$ce, tokens_est:$te, cost_est_usd:$ce_usd, difficulty_est:$de}' \
119
+ > "$d/$num.est" 2>/dev/null || true
120
+ }
121
+
122
+ # _em_difficulty <changed-lines> <files> — the shared 1–5 bucket.
123
+ _em_difficulty() {
124
+ local churn="${1:-0}" files="${2:-0}" d=1
125
+ if (( churn > 1500 || files > 30 )); then d=5
126
+ elif (( churn > 600 || files > 15 )); then d=4
127
+ elif (( churn > 200 || files > 6 )); then d=3
128
+ elif (( churn > 40 || files > 2 )); then d=2
129
+ fi
130
+ printf '%s' "$d"
131
+ }
132
+
133
+ # effort_ctx_bucket <difficulty-1-5> [subcount] — map the estimate to a session-weight bucket.
134
+ # ctx:S fits inline with others; ctx:L / ctx:XL want their subs delegated to sub-agents so the main
135
+ # session stays light (the lever that widens a session — rules/agent-execution-routing.md). Echoes
136
+ # `ctx:S|M|L|XL` (a ready-to-apply GitHub label).
137
+ effort_ctx_bucket() {
138
+ local diff="${1:-1}" subs="${2:-1}" b
139
+ [[ "$diff" =~ ^[0-9]+$ ]] || diff=1
140
+ [[ "$subs" =~ ^[0-9]+$ ]] || subs=1
141
+ if (( diff >= 5 )); then b=XL
142
+ elif (( diff == 4 || subs >= 5 )); then b=L
143
+ elif (( diff == 3 || subs >= 3 )); then b=M
144
+ else b=S
145
+ fi
146
+ printf 'ctx:%s' "$b"
147
+ }
148
+
149
+ # _em_token_sum <branch> — sum transcript usage for an effort's sessions (#807). Sessions are looked
150
+ # up in <gcd>/kit-usage.jsonl by branch (#784); each session's transcript is found under
151
+ # ~/.claude/projects/*/. Echoes "in out cache_read cache_write" (0s when unmappable). Going-forward
152
+ # only — the usage log is empty for the past (backtrace left tokens null, by design).
153
+ _em_token_sum() {
154
+ # <branch> [start-epoch] [end-epoch] — attribute usage sessions to the effort by branch UNION a
155
+ # time window (#124). Branch alone misses sessions logged before the branch field was recorded or
156
+ # from a detached/worktree checkout; the [start,end] window (the effort's build span) recovers them.
157
+ local branch="$1" start="${2:-0}" end="${3:-0}" gcd ulog sids sid tx tin=0 tout=0 tcr=0 tcw=0 sums
158
+ local IFS=' ' # force space-split for `set --` (ambient IFS may carry a NUL — see ifs landmine)
159
+ [[ "$start" =~ ^[0-9]+$ ]] || start=0; [[ "$end" =~ ^[0-9]+$ ]] || end=0
160
+ gcd="$(git rev-parse --git-common-dir 2>/dev/null)" || { echo "0 0 0 0"; return 0; }
161
+ ulog="$gcd/kit-usage.jsonl"
162
+ [[ -f "$ulog" ]] || { echo "0 0 0 0"; return 0; }
163
+ # A session counts when it matches the branch OR falls inside the [start,end] window (when given).
164
+ # `try … catch 0` keeps a malformed .ts row from aborting the whole filter.
165
+ sids="$(jq -r --arg b "$branch" --argjson s "$start" --argjson e "$end" '
166
+ select(.session != "")
167
+ | select(
168
+ ($b != "" and .branch == $b)
169
+ or ($s > 0 and $e > 0
170
+ and (try (.ts | fromdateiso8601) catch 0) >= $s
171
+ and (try (.ts | fromdateiso8601) catch 0) <= $e)
172
+ )
173
+ | .session' "$ulog" 2>/dev/null | sort -u)"
174
+ [[ -n "$sids" ]] || { echo "0 0 0 0"; return 0; }
175
+ while IFS= read -r sid; do
176
+ [[ -n "$sid" ]] || continue
177
+ tx="$(find "$HOME/.claude/projects" -name "$sid.jsonl" -type f 2>/dev/null | sed -n 1p)"
178
+ [[ -n "$tx" ]] || continue
179
+ # -r (raw) is REQUIRED: without it jq wraps the result string in double-quotes, so $a
180
+ # becomes `"1015901` and the `$(( ))` math below dies ("bad math expression") — the #829 bug.
181
+ sums="$(jq -rs '[.[].message.usage // empty] | {i:(map(.input_tokens//0)|add), o:(map(.output_tokens//0)|add), cr:(map(.cache_read_input_tokens//0)|add), cw:(map(.cache_creation_input_tokens//0)|add)} | "\(.i) \(.o) \(.cr) \(.cw)"' "$tx" 2>/dev/null)"
182
+ local a b c cw_; read -r a b c cw_ <<< "$sums"
183
+ tin=$(( tin + ${a:-0} )); tout=$(( tout + ${b:-0} )); tcr=$(( tcr + ${c:-0} )); tcw=$(( tcw + ${cw_:-0} ))
184
+ done <<< "$sids"
185
+ echo "$tin $tout $tcr $tcw"
186
+ }
187
+
188
+ # _em_usage_log — absolute path of the session usage log (it may not exist).
189
+ _em_usage_log() {
190
+ local c; c="$(git rev-parse --git-common-dir 2>/dev/null)" || return 1
191
+ case "$c" in /*) : ;; *) c="$PWD/$c" ;; esac
192
+ printf '%s/kit-usage.jsonl' "$c"
193
+ }
194
+
195
+ # _em_autopilot_usage <num> <branch> — "<tokens> <cost-usd> <runs>" from the `cckit autopilot run`
196
+ # state files (<state-dir>/autopilot/*.json, scripts/autopilot-run.sh) whose branch is <branch> or a
197
+ # sub/<num><letter>-… branch of the effort. Those hold what the runtime itself printed (al_usage).
198
+ # tokens a sum · `unknown` when any matching run reads unknown · `none` when no run matched
199
+ # cost a sum · `unknown` when any matching run printed no cost
200
+ # A run with zero rounds spent nothing and is skipped.
201
+ _em_autopilot_usage() {
202
+ local num="$1" branch="$2" sdir f out
203
+ local files=()
204
+ if ! command -v kit_state_dir >/dev/null 2>&1; then
205
+ # shellcheck source=/dev/null
206
+ [[ -f "$_EM_LIB_DIR/kit-state.sh" ]] && . "$_EM_LIB_DIR/kit-state.sh"
207
+ fi
208
+ command -v kit_state_dir >/dev/null 2>&1 || { echo "none unknown 0"; return 0; }
209
+ sdir="$(kit_state_dir 2>/dev/null)/autopilot"
210
+ [[ -d "$sdir" ]] || { echo "none unknown 0"; return 0; }
211
+ # find, not a glob: an unmatched glob is an error under zsh.
212
+ while IFS= read -r f; do [[ -n "$f" ]] && files+=("$f"); done \
213
+ < <(find "$sdir" -maxdepth 1 -type f -name '*.json' 2>/dev/null)
214
+ (( ${#files[@]} > 0 )) || { echo "none unknown 0"; return 0; }
215
+ out="$(jq -rs --arg b "$branch" --arg n "$num" '
216
+ [ .[] | objects
217
+ | select((.branch? // "") as $x
218
+ | ($b != "" and $x == $b) or ($x | test("^sub/" + $n + "[a-z]-")))
219
+ | select((.rounds? // 0) > 0) ] as $runs
220
+ | if ($runs | length) == 0 then "none unknown 0" else
221
+ ( if any($runs[]; (.usage.tokens? | type) != "number") then "unknown"
222
+ else ([$runs[].usage.tokens] | add) end ) as $tok
223
+ | ( if any($runs[]; (.usage.cost_usd? | type) != "number") then "unknown"
224
+ else ([$runs[].usage.cost_usd] | add) end ) as $cost
225
+ | "\($tok) \($cost) \($runs | length)"
226
+ end' "${files[@]}" 2>/dev/null)"
227
+ [[ -n "$out" ]] || out="none unknown 0"
228
+ echo "$out"
229
+ }
230
+
231
+ capture_effort_metrics() {
232
+ local build_src tokens_src null_reason ap ap_tok ap_cost ap_runs first ulog
233
+ local num="$1" base="${2:-origin/${KIT_BASE_BRANCH:-main}}" d start now build_s shortstat files added removed commits churn diff_auto branch
234
+ local toks tin tout tcr tcw tokens_real cost_real cost_source tokens_est cost_est rec
235
+ local IFS=' ' # force space-split for `set --` below (ambient IFS may carry a NUL)
236
+ [[ -n "$num" ]] || return 0
237
+ command -v jq >/dev/null 2>&1 || return 0
238
+ d="$(_em_dir)" || return 0; mkdir -p "$d" 2>/dev/null || return 0
239
+
240
+ # build time (#453): the stamp effort_start wrote, else the oldest commit on the branch (below,
241
+ # once the base is fetched). Never a guess: with neither, build_seconds stays null.
242
+ now="$(date -u +%s 2>/dev/null)"; build_s="null"; build_src="null"; start=""
243
+ if [[ -f "$d/$num.start" ]]; then
244
+ start="$(cat "$d/$num.start" 2>/dev/null)"
245
+ if [[ "$start" =~ ^[0-9]+$ && "$now" =~ ^[0-9]+$ ]] && (( now >= start )); then
246
+ build_s=$(( now - start )); build_src='"effort-start"'
247
+ else
248
+ start=""
249
+ fi
250
+ fi
251
+
252
+ git fetch origin --quiet 2>/dev/null || true
253
+ if [[ "$build_s" == "null" && "$now" =~ ^[0-9]+$ ]]; then
254
+ # Author time, not committer time: a rebase rewrites the second and keeps the first.
255
+ first="$(git log --format=%at "$base"..HEAD 2>/dev/null | sort -n | sed -n 1p)"
256
+ if [[ "$first" =~ ^[0-9]+$ ]] && (( now >= first )); then
257
+ start="$first"; build_s=$(( now - first )); build_src='"first-commit"'
258
+ fi
259
+ fi
260
+ shortstat="$(git diff --shortstat "$base"...HEAD 2>/dev/null)"
261
+ files=$(printf '%s' "$shortstat" | grep -oE '[0-9]+ file' | grep -oE '[0-9]+' | sed -n 1p)
262
+ added=$(printf '%s' "$shortstat" | grep -oE '[0-9]+ insertion' | grep -oE '[0-9]+' | sed -n 1p)
263
+ removed=$(printf '%s' "$shortstat" | grep -oE '[0-9]+ deletion' | grep -oE '[0-9]+' | sed -n 1p)
264
+ commits=$(git rev-list --count "$base"..HEAD 2>/dev/null)
265
+ files=${files:-0}; added=${added:-0}; removed=${removed:-0}; commits=${commits:-0}
266
+ churn=$(( added + removed )); diff_auto=$(_em_difficulty "$churn" "$files")
267
+ branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo '')"
268
+
269
+ # Post-close footgun guard (#829). capture MUST run PRE-squash (kit-effort-close step a, on the
270
+ # live effort branch). If every signal is zero on a base branch, the effort already merged+GC'd —
271
+ # warn loudly instead of silently recording a meaningless all-zero row (the #787 manual-close bug).
272
+ if [[ "$files" -eq 0 && "$commits" -eq 0 && "$branch" =~ ^(develop|main)$ ]]; then
273
+ echo "effort-metrics: WARN #$num — capture on '$branch' with an empty diff; run it PRE-close (the effort branch is gone). Recording zeros." >&2
274
+ fi
275
+
276
+ # token/cost actuals (#807) — attribute by branch UNION the effort's build window [start,now] (#124).
277
+ toks="$(_em_token_sum "$branch" "${start:-0}" "${now:-0}")"; read -r tin tout tcr tcw <<< "$toks"
278
+ tin=${tin:-0}; tout=${tout:-0}; tcr=${tcr:-0}; tcw=${tcw:-0}
279
+ if (( tin + tout + tcr + tcw > 0 )); then
280
+ tokens_real=$(( tin + tout + tcr + tcw ))
281
+ cost_real=$(awk -v i="$tin" -v o="$tout" -v cr="$tcr" -v cw="$tcw" \
282
+ -v pi="$_EM_PRICE_IN" -v po="$_EM_PRICE_OUT" -v pcr="$_EM_PRICE_CACHE_READ" -v pcw="$_EM_PRICE_CACHE_WRITE" \
283
+ 'BEGIN{printf "%.4f", (i*pi + o*po + cr*pcr + cw*pcw)/1000000}')
284
+ cost_source='"list-price"'; tokens_src='"transcripts"'; null_reason=null
285
+ else
286
+ tokens_real=null; cost_real=null; cost_source=null; tokens_src=null
287
+ ulog="$(_em_usage_log 2>/dev/null)"
288
+ if [[ -n "$ulog" && -f "$ulog" ]]; then null_reason='"no-session-matched"'; else null_reason='"no-usage-source"'; fi
289
+ # No transcript figure: read what `cckit autopilot run` recorded for this effort's branches (#453).
290
+ ap="$(_em_autopilot_usage "$num" "$branch")"; read -r ap_tok ap_cost ap_runs <<< "$ap"
291
+ if [[ "$ap_tok" =~ ^[0-9]+$ ]]; then
292
+ tokens_real="$ap_tok"; tokens_src='"autopilot-run"'; null_reason=null
293
+ if [[ "$ap_cost" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then cost_real="$ap_cost"; cost_source='"runtime"'; fi
294
+ elif [[ "$ap_tok" == "unknown" ]]; then
295
+ null_reason='"autopilot-usage-unknown"'
296
+ fi
297
+ fi
298
+
299
+ # estimate (#808) for reconciliation.
300
+ tokens_est=null; cost_est=null
301
+ if [[ -f "$d/$num.est" ]]; then
302
+ tokens_est="$(jq -r '.tokens_est // "null"' "$d/$num.est")"; [[ "$tokens_est" =~ ^[0-9]+$ ]] || tokens_est=null
303
+ cost_est="$(jq -r '.cost_est_usd // "null"' "$d/$num.est")"; [[ "$cost_est" =~ ^[0-9.]+$ ]] || cost_est=null
304
+ fi
305
+
306
+ rec="$(jq -cn --argjson num "$num" --arg branch "$branch" --arg at "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
307
+ --argjson build "$build_s" --argjson files "$files" --argjson added "$added" \
308
+ --argjson removed "$removed" --argjson commits "$commits" \
309
+ --argjson tokens_real "$tokens_real" --argjson cost_real "$cost_real" --argjson cost_source "$cost_source" \
310
+ --argjson tokens_est "$tokens_est" --argjson cost_est "$cost_est" --argjson diff "$diff_auto" \
311
+ --argjson build_src "$build_src" --argjson tokens_src "$tokens_src" --argjson null_reason "$null_reason" '
312
+ {effort:$num, branch:$branch, captured_at:$at, build_seconds:$build, build_source:$build_src,
313
+ signals:{files:$files, added:$added, removed:$removed, commits:$commits},
314
+ tokens_real:$tokens_real, tokens_source:$tokens_src, tokens_null_reason:$null_reason,
315
+ cost_real_usd:$cost_real, cost_source:$cost_source,
316
+ tokens_est:$tokens_est, cost_est_usd:$cost_est,
317
+ difficulty_auto:$diff, score_auto:null, difficulty_judge:null, score_judge:null,
318
+ synced_to_graph:false}' 2>/dev/null)" || return 0
319
+ [[ -n "$rec" ]] || return 0
320
+ printf '%s\n' "$rec" >> "$d/records.jsonl"
321
+ rm -f "$d/$num.start" "$d/$num.est" 2>/dev/null || true
322
+ echo "[#$num] metrics: build=${build_s}s (source=${build_src//\"/}) files=$files +$added/-$removed commits=$commits diff=$diff_auto tokens_real=$tokens_real (source=${tokens_src//\"/} reason=${null_reason//\"/}) cost_real=$cost_real est_tokens=$tokens_est → records.jsonl" >&2
323
+ # Self-improving estimator (#812): recalibrate the medians/ratios from the now-larger dataset so the
324
+ # next effort's pre-build estimate reflects reality (flips tokens_calibrated once ≥5 real-token rows).
325
+ recalibrate_estimator >/dev/null 2>&1 || true
326
+ }
327
+
328
+ # ── LLM judge (#809) ─────────────────────────────────────────────────────────────────────────
329
+ # judge_effort_metrics <num> [base] — score an effort's quality + difficulty with a model.
330
+ #
331
+ # Sends the effort GOAL + (truncated) diff/trace to CCKIT_MODEL (OpenAI-compat, PRIMARY) with a
332
+ # rubric → STRICT JSON {difficulty_judge:1..5, score_judge:0..1, rationale}. Falls back to the LOCAL
333
+ # model (kit_local_chat) when the hosted endpoint is unreachable/empty. If NEITHER is available the
334
+ # fields stay null and close is NEVER blocked (logs "skipped (no model)", returns 0). Idempotent:
335
+ # re-running overwrites the row's *_judge fields + model_id. Requires: jq, git, curl (for the
336
+ # hosted path).
337
+
338
+ # Truncate budget for the diff handed to the model (keep the prompt under the context window).
339
+ _EM_JUDGE_MAX_CHARS="${EM_JUDGE_MAX_CHARS:-12000}"
340
+
341
+ # _em_judge_diff <num> <base> — the effort diff for the judge. Prefers the durable trace dir
342
+ # (<git-common-dir>/traces/effort-<N>/*.diff, written pre-squash by effort_snapshot_subs) and falls
343
+ # back to `git diff <base>...HEAD`. Echoes the (untruncated) diff; truncation happens in the caller.
344
+ _em_judge_diff() {
345
+ local num="$1" base="${2:-origin/${KIT_BASE_BRANCH:-main}}" common tdir
346
+ common="$(git rev-parse --git-common-dir 2>/dev/null)" || true
347
+ if [[ -n "$common" ]]; then
348
+ case "$common" in /*) : ;; *) common="$PWD/$common" ;; esac
349
+ tdir="$common/traces/effort-$num"
350
+ if [[ -d "$tdir" ]] && ls "$tdir"/*.diff >/dev/null 2>&1; then
351
+ cat "$tdir"/*.diff 2>/dev/null
352
+ return 0
353
+ fi
354
+ fi
355
+ git diff "$base"...HEAD 2>/dev/null
356
+ }
357
+
358
+ # _em_judge_goal <num> — best-effort effort GOAL (the `## Goal` section of the issue body). Empty
359
+ # string when gh is unavailable or the section is absent — the judge still runs on the diff alone.
360
+ _em_judge_goal() {
361
+ local num="$1" repo body
362
+ forge_available || { forge_note_none "effort close" "the effort's goal is not read; the judge reads the diff alone"; printf ''; return 0; }
363
+ repo="${EFFORT_REPO:-${KIT_REPO:-}}"
364
+ body="$(forge_rest "repos/$repo/issues/$num" --jq '.body // ""' 2>/dev/null)" || { printf ''; return 0; }
365
+ [[ -n "$body" ]] || { printf ''; return 0; }
366
+ # Extract the lines after "## Goal" up to the next "## " heading.
367
+ printf '%s\n' "$body" | awk '
368
+ /^##[ \t]+[Gg]oal[ \t]*$/ { grab=1; next }
369
+ /^##[ \t]/ { if (grab) exit }
370
+ grab { print }' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | grep -v '^$' | sed -n 1,20p
371
+ }
372
+
373
+ # _em_model_env <config-key> <default-name> — the VALUE of the env var the hosted judge reads for
374
+ # one setting (#381). The config may NAME the variable (`model.urlEnv` / `model.modelEnv` /
375
+ # `model.tokenEnv`) so a project that already exports its own model variables needs no fork; unset,
376
+ # the built-in name is used. Names only, never values — the same contract as `engine.token_env`.
377
+ # The name is expanded through `eval` (the one indirection bash 3.2 and zsh share), so anything
378
+ # that is not a plain shell identifier is refused with a warning and the default name is used.
379
+ _em_model_env() {
380
+ local key="$1" name="$2" cfg="" named=""
381
+ if ! command -v kit_config_path >/dev/null 2>&1 && [[ -f "$_EM_LIB_DIR/config-path.sh" ]]; then
382
+ # shellcheck source=/dev/null
383
+ source "$_EM_LIB_DIR/config-path.sh"
384
+ fi
385
+ command -v kit_config_path >/dev/null 2>&1 && cfg="$(kit_config_path 2>/dev/null || true)"
386
+ if [[ -n "$cfg" && -f "$cfg" ]]; then
387
+ named="$(jq -r --arg k "$key" '.model[$k] // empty | strings' "$cfg" 2>/dev/null)" || named=""
388
+ fi
389
+ if [[ -n "$named" ]]; then
390
+ if grep -qE '^[A-Za-z_][A-Za-z0-9_]*$' <<<"$named" && [[ "$named" != *$'\n'* ]]; then
391
+ name="$named"
392
+ else
393
+ echo "effort-metrics: model.$key is not an env var name — using $name" >&2
394
+ fi
395
+ fi
396
+ eval "printf '%s' \"\${$name:-}\""
397
+ }
398
+
399
+ # _em_judge_call <system> <user> — try the hosted model (OpenAI-compat) then the local model.
400
+ # The hosted endpoint, model name and token come from CCKIT_MODEL_URL / CCKIT_MODEL /
401
+ # CCKIT_MODEL_TOKEN, or from the env vars the config names (see _em_model_env).
402
+ # Echoes "<model_id>\t<reply>" on success; non-zero + nothing on total failure. The model_id is the
403
+ # hosted model name or the local tag, so the schema's model_id column records who judged.
404
+ _em_judge_call() {
405
+ local system="$1" user="$2" url payload reply model base token
406
+
407
+ # PRIMARY: hosted model (OpenAI-compatible chat completions).
408
+ base="$(_em_model_env urlEnv CCKIT_MODEL_URL)"
409
+ if [[ -n "$base" ]] && command -v curl >/dev/null 2>&1; then
410
+ url="${base%/}/chat/completions"
411
+ model="$(_em_model_env modelEnv CCKIT_MODEL)"; [[ -n "$model" ]] || model="default"
412
+ token="$(_em_model_env tokenEnv CCKIT_MODEL_TOKEN)"
413
+ payload="$(jq -n --arg m "$model" --arg s "$system" --arg p "$user" \
414
+ '{model:$m, messages:[{role:"system",content:$s},{role:"user",content:$p}], max_tokens:512, temperature:0.1}')" || payload=""
415
+ if [[ -n "$payload" ]]; then
416
+ # Two explicit calls, not `${token:+-H "…"}`: zsh does not word-split that expansion, so the
417
+ # flag and its value reached curl as ONE argument and the token was never sent.
418
+ if [[ -n "$token" ]]; then
419
+ reply="$(curl -sf -m "${EM_JUDGE_TIMEOUT:-60}" "$url" \
420
+ -H 'Content-Type: application/json' \
421
+ -H "Authorization: Bearer $token" \
422
+ -d "$payload" 2>/dev/null | jq -r '.choices[0].message.content // empty' 2>/dev/null)" || reply=""
423
+ else
424
+ reply="$(curl -sf -m "${EM_JUDGE_TIMEOUT:-60}" "$url" \
425
+ -H 'Content-Type: application/json' \
426
+ -d "$payload" 2>/dev/null | jq -r '.choices[0].message.content // empty' 2>/dev/null)" || reply=""
427
+ fi
428
+ if [[ -n "$reply" ]]; then
429
+ printf '%s\t%s' "$model" "$reply"
430
+ return 0
431
+ fi
432
+ fi
433
+ fi
434
+
435
+ # FALLBACK: local model via kit-local.sh.
436
+ if ! declare -f kit_local_chat >/dev/null 2>&1; then
437
+ # shellcheck source=/dev/null
438
+ [[ -f "$_EM_LIB_DIR/kit-local.sh" ]] && source "$_EM_LIB_DIR/kit-local.sh"
439
+ fi
440
+ if declare -f kit_local_chat >/dev/null 2>&1 && kit_local_alive 2>/dev/null; then
441
+ reply="$(kit_local_chat "$system" "$user" 512 2>/dev/null)" || reply=""
442
+ if [[ -n "$reply" ]]; then
443
+ model="$(declare -f kit_local_model_tag >/dev/null 2>&1 && kit_local_model_tag || printf 'local')"
444
+ printf '%s\t%s' "$model" "$reply"
445
+ return 0
446
+ fi
447
+ fi
448
+ return 1
449
+ }
450
+
451
+ judge_effort_metrics() {
452
+ local num="$1" base="${2:-origin/${KIT_BASE_BRANCH:-main}}" d f goal diff truncated_note system user out model reply json dj sj
453
+ [[ -n "$num" ]] || return 0
454
+ command -v jq >/dev/null 2>&1 || return 0
455
+ d="$(_em_dir)" || return 0; f="$d/records.jsonl"
456
+ [[ -f "$f" ]] || { echo "[#$num] judge: no records.jsonl — capture must run first; skipping" >&2; return 0; }
457
+
458
+ # Gather + truncate the diff.
459
+ diff="$(_em_judge_diff "$num" "$base")"
460
+ if [[ -z "$diff" ]]; then
461
+ echo "[#$num] judge: empty diff — nothing to judge; leaving *_judge null" >&2
462
+ return 0
463
+ fi
464
+ truncated_note=""
465
+ if (( ${#diff} > _EM_JUDGE_MAX_CHARS )); then
466
+ diff="${diff:0:$_EM_JUDGE_MAX_CHARS}"
467
+ truncated_note=$'\n\n[NOTE: the diff above was TRUNCATED to fit the context budget — judge from the visible portion.]'
468
+ fi
469
+ goal="$(_em_judge_goal "$num")"
470
+
471
+ # Rubric.
472
+ system='You are a senior engineering reviewer scoring a completed unit of work ("effort") in a software project. Reply with STRICT JSON ONLY (no prose, no markdown fences): {"difficulty_judge": <integer 1-5>, "score_judge": <float 0.0-1.0>, "rationale": "<one sentence>"}.
473
+
474
+ difficulty_judge (1-5) = how hard the work was, judged from the change:
475
+ 1 = trivial (tiny/mechanical edit), 2 = easy (small, localized), 3 = moderate (multi-file or some design), 4 = hard (broad blast radius, non-trivial design), 5 = very hard (cross-cutting, intricate, risky).
476
+ score_judge (0.0-1.0) = quality of the work vs its stated goal:
477
+ near 1.0 = clean, complete, idiomatic, matches the goal; ~0.5 = partial or rough; near 0.0 = wrong, incomplete, or off-goal.
478
+ Judge only from the evidence given. Output the JSON object and nothing else.'
479
+
480
+ user="GOAL:
481
+ ${goal:-(no goal text available — judge difficulty/quality from the diff alone)}
482
+
483
+ DIFF (unified):
484
+ ${diff}${truncated_note}"
485
+
486
+ out="$(_em_judge_call "$system" "$user")" || {
487
+ echo "[#$num] judge: skipped (no model) — CCKIT_MODEL unreachable and local model unavailable; *_judge stay null" >&2
488
+ return 0
489
+ }
490
+ model="${out%%$'\t'*}"; reply="${out#*$'\t'}"
491
+
492
+ # Extract the first {...} object from the reply (models often wrap it in prose/fences).
493
+ json="$(printf '%s' "$reply" | tr '\n' ' ' | grep -oE '\{[^{}]*\}' | sed -n 1p)"
494
+ if [[ -z "$json" ]]; then
495
+ echo "[#$num] judge: model reply had no JSON object — leaving *_judge null (model=$model)" >&2
496
+ return 0
497
+ fi
498
+ dj="$(printf '%s' "$json" | jq -r '.difficulty_judge // empty' 2>/dev/null)"
499
+ sj="$(printf '%s' "$json" | jq -r '.score_judge // empty' 2>/dev/null)"
500
+ # Validate: difficulty integer 1..5, score float 0..1.
501
+ if ! [[ "$dj" =~ ^[0-9]+$ ]] || (( dj < 1 || dj > 5 )); then
502
+ echo "[#$num] judge: difficulty_judge='$dj' out of range — leaving *_judge null (model=$model)" >&2
503
+ return 0
504
+ fi
505
+ if ! grep -qE '^[0-9]+(\.[0-9]+)?$' <<<"$sj" || ! awk -v s="$sj" 'BEGIN{exit !(s>=0 && s<=1)}'; then
506
+ echo "[#$num] judge: score_judge='$sj' out of range — leaving *_judge null (model=$model)" >&2
507
+ return 0
508
+ fi
509
+
510
+ # Update the effort's row: the one with .effort==num that still has a null judge (idempotent — if
511
+ # all rows are already judged, re-judge the most recent one).
512
+ local tmp rec; tmp="$f.judge.$$"; : > "$tmp"
513
+ local applied=0
514
+ # Line-wise so we touch exactly ONE row (jq has no cross-line "first match only" state). First
515
+ # choice: a row for this effort that still has a null judge.
516
+ while IFS= read -r rec; do
517
+ [[ -n "$rec" ]] || continue
518
+ if (( applied == 0 )) && [[ "$(jq -r '.effort' <<<"$rec" 2>/dev/null)" == "$num" ]] \
519
+ && [[ "$(jq -r '.difficulty_judge' <<<"$rec" 2>/dev/null)" == "null" ]]; then
520
+ rec="$(jq -c --argjson dj "$dj" --argjson sj "$sj" --arg model "$model" \
521
+ '.difficulty_judge=$dj | .score_judge=$sj | .model_id=$model | .synced_to_graph=false' <<<"$rec")"
522
+ applied=1
523
+ fi
524
+ printf '%s\n' "$rec" >> "$tmp"
525
+ done < "$f"
526
+ # Idempotent re-judge: no null-judge row found → overwrite the LAST row for this effort.
527
+ if (( applied == 0 )); then
528
+ : > "$tmp"
529
+ local lastline; lastline="$(grep -n "\"effort\":${num}[,}]" "$f" 2>/dev/null | tail -1 | cut -d: -f1)"
530
+ local i=0
531
+ while IFS= read -r rec; do
532
+ i=$((i+1))
533
+ [[ -n "$rec" ]] || { printf '%s\n' "$rec" >> "$tmp"; continue; }
534
+ if [[ -n "$lastline" && "$i" == "$lastline" ]]; then
535
+ rec="$(jq -c --argjson dj "$dj" --argjson sj "$sj" --arg model "$model" \
536
+ '.difficulty_judge=$dj | .score_judge=$sj | .model_id=$model | .synced_to_graph=false' <<<"$rec")"
537
+ applied=1
538
+ fi
539
+ printf '%s\n' "$rec" >> "$tmp"
540
+ done < "$f"
541
+ fi
542
+ if (( applied == 0 )); then
543
+ echo "[#$num] judge: no matching record row to update — leaving file unchanged (model=$model)" >&2
544
+ rm -f "$tmp" 2>/dev/null
545
+ return 0
546
+ fi
547
+ mv "$tmp" "$f"
548
+ echo "[#$num] judge: difficulty_judge=$dj score_judge=$sj model=$model → records.jsonl" >&2
549
+ }
550
+
551
+ # recalibrate_estimator [stamp] — recompute estimator.json from the real captured dataset
552
+ # (records.jsonl), so the pre-build estimate self-improves as efforts accrue (#812). Median signals
553
+ # come from rows with real diff signals (0-churn post-GC rows dropped); the token/cost ratios are
554
+ # recomputed ONLY from rows with real per-effort token actuals, and `tokens_calibrated` flips true
555
+ # once there are >= EM_TOKEN_MIN (default 5) such rows. Merges over the existing estimator.json so
556
+ # pricing/notes/token_share are preserved. [stamp] = ISO time (defaults to `date -u`).
557
+ recalibrate_estimator() {
558
+ command -v jq >/dev/null 2>&1 || return 0
559
+ local d est f stamp min model
560
+ d="$(_em_dir)" || return 0; f="$d/records.jsonl"; est="$d/estimator.json"
561
+ [[ -f "$f" ]] || { echo "effort-metrics: no records.jsonl to calibrate from" >&2; return 0; }
562
+ stamp="${1:-$(date -u +%Y-%m-%dT%H:%M:%SZ)}"
563
+ min="${EM_TOKEN_MIN:-5}"
564
+ model="$(jq -s --argjson min "$min" --arg stamp "$stamp" '
565
+ def med(a): (a|sort) as $s | ($s|length) as $l
566
+ | if $l==0 then null elif $l%2==1 then $s[($l-1)/2] else (($s[$l/2-1]+$s[$l/2])/2) end;
567
+ [ .[] | { lines:((.signals.added//0)+(.signals.removed//0)), files:(.signals.files//0),
568
+ commits:(.signals.commits//0), build:.build_seconds, tok:.tokens_real, cost:.cost_real_usd } ]
569
+ | map(select(.lines>0)) as $rows
570
+ | ($rows | map(select(.tok!=null and .tok>0))) as $rt
571
+ | ($rt|length) as $nrt
572
+ | { n_efforts:($rows|length), n_real_token_efforts:$nrt, tokens_calibrated:($nrt>=$min),
573
+ median_changed_lines: med([$rows[].lines]), median_files: med([$rows[].files]),
574
+ median_commits: med([$rows[].commits]),
575
+ median_build_seconds: med([$rows[]|select(.build!=null)|.build]),
576
+ tokens_per_changed_line: (if $nrt>=$min then (([$rt[].tok]|add)/([$rt[].lines]|add)) else null end),
577
+ cost_per_changed_line_usd: (
578
+ # Only rows that carry a cost: an autopilot-run row from a runtime that prints none has
579
+ # tokens and a null cost (#453), and counting it as 0 would lower the ratio.
580
+ ($rt | map(select(.cost != null))) as $rc
581
+ | if $nrt>=$min and ($rc|length) > 0 and ([$rc[].lines]|add) > 0
582
+ then (([$rc[]|(.cost|tonumber? // 0)]|add)/([$rc[].lines]|add)) else null end),
583
+ recalibrated_at:$stamp }' "$f")"
584
+ [[ -n "$model" ]] || { echo "effort-metrics: recalibrate produced nothing" >&2; return 1; }
585
+ if [[ -f "$est" ]]; then
586
+ jq --argjson m "$model" '.model = ((.model // {}) * $m) | .recalibrated_at = $m.recalibrated_at' "$est" > "$est.tmp" && mv "$est.tmp" "$est"
587
+ else
588
+ jq -n --argjson m "$model" '{model:$m, recalibrated_at:$m.recalibrated_at}' > "$est"
589
+ fi
590
+ echo "effort-metrics: recalibrated from $(jq -r '.model.n_efforts' "$est") efforts (tokens_calibrated=$(jq -r '.model.tokens_calibrated' "$est"), real-token efforts=$(jq -r '.model.n_real_token_efforts' "$est"))" >&2
591
+ }
@@ -3,11 +3,11 @@
3
3
  # effort-slug-test.sh — covers the effort slug layer (#93/#94/#96): slug derivation (doubling fix),
4
4
  # `slug #N` display, and effort_slug_resolve (number passthrough, branch match, ambiguity, no-match,
5
5
  # slug:<slug> label vs title-derived precedence). Hermetic: a throwaway git repo with effort/* branches
6
- # + the forge shim (no network/auth). Run: bash scripts/lib/effort-slug-test.sh
6
+ # + the forge shim (no network/auth). Run: bash lib/core/effort-slug-test.sh
7
7
  # errors: strict — a test runner: rc 1 on any failed assertion
8
8
  set -u
9
9
  ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
10
- LIB="$ROOT/scripts/lib"
10
+ LIB="$ROOT/lib"
11
11
  fail=0
12
12
  t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
13
13
  rc() { # <label> <expected-rc> ; reads the just-run command's rc from $?
@@ -21,7 +21,7 @@ tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
21
21
  # answers the slug:explicit-handle label query with #207; any other slug: label matches nothing.
22
22
  stub="$tmp/bin"
23
23
  # shellcheck source=/dev/null
24
- . "$LIB/test-forge-shim.sh"; forge_shim_install "$stub"
24
+ . "$LIB/forge/test-forge-shim.sh"; forge_shim_install "$stub"
25
25
  export PATH="$stub:$PATH" GH_TOKEN=test-token CCKIT_FORGE=github GH_LOG="$tmp/gh.log"
26
26
  export GH_ISSUES='[{"number":205,"title":"[Effort] 205 · [Core] Title Only Effort","state":"open","labels":[]},{"number":206,"title":"plain issue","state":"open","labels":[]}]'
27
27
  export GH_FIXTURE="$tmp/fixture.sh"
@@ -34,7 +34,7 @@ SH
34
34
  export EFFORT_REPO="o/r" KIT_REPO="o/r"
35
35
 
36
36
  # shellcheck source=/dev/null
37
- source "$LIB/effort-slug.sh"
37
+ source "$LIB/core/effort-slug.sh"
38
38
 
39
39
  # ── slug derivation (the #93 doubling fix) ─────────────────────────────────────────────────────
40
40
  t "_eff_title_slug peels prefix + flow (parent)" \
@@ -43,6 +43,10 @@ t "_eff_title_slug peels prefix + flow (sub)" \
43
43
  "$(_eff_title_slug '[Effort 93] 2 · [UI] Accept slug in commands')" "accept-slug-in-commands"
44
44
  t "_eff_title_slug without a flow tag" \
45
45
  "$(_eff_title_slug '[Effort] 12 · plain outcome name')" "plain-outcome-name"
46
+ t "_eff_title_slug peels the [E<n>] prefix" \
47
+ "$(_eff_title_slug '[E93] Slug handles for efforts')" "slug-handles-for-efforts"
48
+ t "_eff_title_slug peels the [E<n>.<step>] prefix" \
49
+ "$(_eff_title_slug '[E93.2] Accept slug in commands')" "accept-slug-in-commands"
46
50
  t "_eff_slug lowercases + dashes arbitrary text" \
47
51
  "$(_eff_slug 'Some Mixed CASE!!')" "some-mixed-case"
48
52