@jeiemgi/cckit 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (309) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +169 -21
  3. package/README.md +505 -35
  4. package/SECURITY.md +5 -0
  5. package/bin/cckit +189 -20
  6. package/cckit.config.json +1 -1
  7. package/commands/kit-doctor.md +13 -5
  8. package/commands/kit-export-project.md +0 -2
  9. package/commands/kit-export-training.md +6 -9
  10. package/commands/kit-init.md +43 -13
  11. package/commands/kit-routines.md +1 -1
  12. package/commands/kit-update.md +11 -6
  13. package/databases/supabase/guide.html +106 -0
  14. package/databases/supabase/nextjs/lib/supabase/client.ts +9 -0
  15. package/databases/supabase/nextjs/lib/supabase/server.ts +29 -0
  16. package/databases/supabase.json +70 -0
  17. package/integrations/emil-design-eng.json +41 -0
  18. package/integrations/graft.json +81 -0
  19. package/integrations/impeccable.json +42 -0
  20. package/integrations/integration.schema.json +215 -0
  21. package/integrations/lavish.json +43 -0
  22. package/integrations/mempalace.json +28 -0
  23. package/integrations/mlx-lm.json +32 -0
  24. package/integrations/web-design-guidelines.json +42 -0
  25. package/kit.config.schema.json +183 -5
  26. package/package.json +4 -1
  27. package/profiles/automation.json +7 -1
  28. package/profiles/content.json +10 -2
  29. package/profiles/marketing.json +57 -0
  30. package/profiles/minimal.json +7 -1
  31. package/profiles/research.json +7 -1
  32. package/profiles/software.json +7 -1
  33. package/recipes/nextjs-shadcn.json +49 -0
  34. package/scripts/agent-skills-external.txt +45 -0
  35. package/scripts/agent-skills-lint-test.sh +131 -0
  36. package/scripts/agent-skills-lint.sh +115 -0
  37. package/scripts/agent-template-lint-test.sh +42 -0
  38. package/scripts/agent-template-lint.sh +58 -0
  39. package/scripts/annotate-setup.sh +1 -1
  40. package/scripts/autopilot-run-test.sh +338 -0
  41. package/scripts/autopilot-run.sh +448 -0
  42. package/scripts/autopilot-test.sh +8 -0
  43. package/scripts/autopilot.sh +10 -1
  44. package/scripts/benchmark/benchmark-test.sh +85 -0
  45. package/scripts/benchmark/checkpoint-test.sh +314 -0
  46. package/scripts/benchmark/checkpoint.sh +408 -0
  47. package/scripts/benchmark/lib/model.sh +31 -6
  48. package/scripts/benchmark/report.sh +17 -2
  49. package/scripts/benchmark/run-quality.sh +36 -10
  50. package/scripts/benchmark/verb-timings-test.sh +124 -0
  51. package/scripts/benchmark/verb-timings.sh +149 -0
  52. package/scripts/check.sh +29 -19
  53. package/scripts/content-skills-test.sh +144 -0
  54. package/scripts/copilot-loop-test.sh +25 -0
  55. package/scripts/database-supabase-test.sh +121 -0
  56. package/scripts/docs-marketing-words-test.sh +73 -0
  57. package/scripts/docs-record-tables-test.sh +72 -0
  58. package/scripts/guard-raw-git-ops-test.sh +8 -0
  59. package/scripts/hook-adapter-test.sh +193 -0
  60. package/scripts/init-content-test.sh +225 -0
  61. package/scripts/init-guided-test.sh +474 -0
  62. package/scripts/init-recipes-test.sh +104 -0
  63. package/scripts/init-upgrade-test.sh +341 -2
  64. package/scripts/init.sh +746 -95
  65. package/scripts/instruction-budget-baseline.txt +8 -0
  66. package/scripts/instruction-budget-lint-test.sh +166 -0
  67. package/scripts/instruction-budget-lint.sh +219 -0
  68. package/scripts/integrations-check-test.sh +200 -0
  69. package/scripts/integrations-check.sh +157 -0
  70. package/scripts/kit-add-test.sh +214 -0
  71. package/scripts/kit-add.sh +305 -5
  72. package/scripts/kit-adopt-test.sh +16 -0
  73. package/scripts/kit-adopt.sh +3 -2
  74. package/scripts/kit-digest.sh +2 -2
  75. package/scripts/kit-doctor-test.sh +293 -0
  76. package/scripts/kit-doctor.sh +272 -140
  77. package/scripts/kit-export-project-test.sh +3 -3
  78. package/scripts/kit-remove-test.sh +19 -0
  79. package/scripts/kit-routines.sh +2 -2
  80. package/scripts/kit-self-update.sh +1 -1
  81. package/scripts/kit-version-check-test.sh +4 -0
  82. package/scripts/kit-version-check.sh +4 -2
  83. package/scripts/knowledge-lint.sh +4 -4
  84. package/scripts/leak-fence-test.sh +89 -0
  85. package/scripts/lib/adapter-select-test.sh +136 -0
  86. package/scripts/lib/adapter-select.sh +158 -0
  87. package/scripts/lib/agent-def-test.sh +69 -0
  88. package/scripts/lib/agent-def.sh +106 -0
  89. package/scripts/lib/agent-launch-test.sh +212 -0
  90. package/scripts/lib/agent-launch.sh +335 -0
  91. package/scripts/lib/board-view-test.sh +11 -0
  92. package/scripts/lib/board-view.sh +42 -26
  93. package/scripts/lib/branch-owner-test.sh +26 -0
  94. package/scripts/lib/branch-owner.sh +16 -9
  95. package/scripts/lib/captain-test.sh +105 -0
  96. package/scripts/lib/captain.sh +87 -8
  97. package/scripts/lib/codex-hooks-test.sh +100 -0
  98. package/scripts/lib/codex-hooks.sh +69 -0
  99. package/scripts/lib/commitlint.sh +1 -1
  100. package/scripts/lib/content-kinds-gc-test.sh +88 -0
  101. package/scripts/lib/copilot-test.sh +70 -4
  102. package/scripts/lib/copilot.sh +66 -14
  103. package/scripts/lib/effort-dispatch-test.sh +3 -3
  104. package/scripts/lib/effort-metrics-test.sh +178 -1
  105. package/scripts/lib/effort-metrics.sh +188 -35
  106. package/scripts/lib/effort-ops-test.sh +217 -7
  107. package/scripts/lib/effort-ops.sh +281 -49
  108. package/scripts/lib/effort-parity-test.sh +49 -13
  109. package/scripts/lib/effort-plan-test.sh +337 -0
  110. package/scripts/lib/effort-plan.sh +200 -23
  111. package/scripts/lib/effort-slug.sh +16 -2
  112. package/scripts/lib/effort.sh +67 -12
  113. package/scripts/lib/errors-header-test.sh +1 -1
  114. package/scripts/lib/gh-project-test.sh +35 -1
  115. package/scripts/lib/gh-project.sh +37 -16
  116. package/scripts/lib/gh-rest-test.sh +330 -0
  117. package/scripts/lib/gh-rest.sh +304 -0
  118. package/scripts/lib/hook-refresh.sh +1 -1
  119. package/scripts/lib/init-github.sh +412 -0
  120. package/scripts/lib/init-guide.sh +630 -0
  121. package/scripts/lib/integration-install-test.sh +343 -0
  122. package/scripts/lib/integration-install.sh +257 -0
  123. package/scripts/lib/integrations-test.sh +366 -0
  124. package/scripts/lib/integrations.sh +244 -0
  125. package/scripts/lib/kinds-test.sh +125 -0
  126. package/scripts/lib/kinds.sh +66 -0
  127. package/scripts/lib/kit-brief-test.sh +22 -3
  128. package/scripts/lib/kit-brief.sh +45 -5
  129. package/scripts/lib/kit-config-resolve.sh +2 -2
  130. package/scripts/lib/kit-config-test.sh +64 -1
  131. package/scripts/lib/kit-config.sh +37 -1
  132. package/scripts/lib/kit-events.sh +7 -0
  133. package/scripts/lib/kit-gc-test.sh +173 -1
  134. package/scripts/lib/kit-gc.sh +244 -37
  135. package/scripts/lib/kit-hotfix.sh +216 -0
  136. package/scripts/lib/kit-interview-test.sh +93 -0
  137. package/scripts/lib/kit-interview.sh +117 -8
  138. package/scripts/lib/kit-lib-test.sh +1 -1
  139. package/scripts/lib/kit-lib.sh +3 -3
  140. package/scripts/lib/kit-local.sh +2 -2
  141. package/scripts/lib/kit-manifest.sh +33 -1
  142. package/scripts/lib/kit-profile-test.sh +3 -3
  143. package/scripts/lib/kit-status-test.sh +112 -4
  144. package/scripts/lib/kit-status.sh +246 -16
  145. package/scripts/lib/kit-task-ops-test.sh +37 -0
  146. package/scripts/lib/kit-task-ops.sh +362 -89
  147. package/scripts/lib/kto-hotfix-test.sh +232 -0
  148. package/scripts/lib/kto-task-close-test.sh +83 -0
  149. package/scripts/lib/kto-task-new-test.sh +95 -0
  150. package/scripts/lib/kto-task-pr-merge-test.sh +97 -6
  151. package/scripts/lib/kto-task-pr-test.sh +70 -0
  152. package/scripts/lib/leak-fence.sh +89 -0
  153. package/scripts/lib/light-task-run-test.sh +255 -0
  154. package/scripts/lib/light-task-test.sh +309 -0
  155. package/scripts/lib/light-task.sh +706 -0
  156. package/scripts/lib/next-test.sh +99 -0
  157. package/scripts/lib/next.sh +9 -3
  158. package/scripts/lib/orchestration-runtime-test.sh +72 -0
  159. package/scripts/lib/orchestration-runtime.sh +57 -5
  160. package/scripts/lib/plan-machine-test.sh +15 -2
  161. package/scripts/lib/plan-machine.sh +20 -7
  162. package/scripts/lib/plan-next-test.sh +1 -1
  163. package/scripts/lib/plan-next.sh +4 -4
  164. package/scripts/lib/pr-evidence-test.sh +3 -3
  165. package/scripts/lib/pr-evidence.sh +1 -1
  166. package/scripts/lib/qa-lane-test.sh +626 -0
  167. package/scripts/lib/qa-lane.sh +1357 -0
  168. package/scripts/lib/qa-lanes-test.sh +175 -0
  169. package/scripts/lib/react-detect.sh +1 -1
  170. package/scripts/lib/render-test.sh +1 -1
  171. package/scripts/lib/review-page-test.sh +318 -0
  172. package/scripts/lib/review-page.sh +369 -0
  173. package/scripts/lib/review-stage-test.sh +62 -11
  174. package/scripts/lib/review-stage.sh +42 -60
  175. package/scripts/lib/role-identity-test.sh +65 -0
  176. package/scripts/lib/role-identity.sh +25 -2
  177. package/scripts/lib/secret-guard.sh +3 -3
  178. package/scripts/lib/session-registry-test.sh +111 -0
  179. package/scripts/lib/session-registry.sh +233 -0
  180. package/scripts/lib/stage-receipt-test.sh +14 -1
  181. package/scripts/lib/stage-receipt.sh +13 -8
  182. package/scripts/lib/task-source-file.sh +309 -0
  183. package/scripts/lib/task-source.sh +130 -0
  184. package/scripts/lib/test-env-test.sh +3 -3
  185. package/scripts/lib/test-env.sh +7 -4
  186. package/scripts/lib/upgrade-report-test.sh +120 -0
  187. package/scripts/lib/upgrade-report.sh +138 -0
  188. package/scripts/lib/version-bump.sh +3 -3
  189. package/scripts/lib/wave-result-test.sh +80 -0
  190. package/scripts/lib/wave-result.sh +127 -0
  191. package/scripts/lib/worker-brief-test.sh +5 -0
  192. package/scripts/lib/worker-brief.sh +2 -2
  193. package/scripts/lib/worktree-issue-test.sh +30 -1
  194. package/scripts/lib/worktree-issue.sh +93 -9
  195. package/scripts/lib/worktree-ports-test.sh +57 -4
  196. package/scripts/lib/worktree-start-test.sh +197 -0
  197. package/scripts/lib/worktree-start.sh +169 -32
  198. package/scripts/lib/zsh-safety-test.sh +19 -3
  199. package/scripts/marketing-walkthrough-test.sh +152 -0
  200. package/scripts/night-grant-test.sh +37 -0
  201. package/scripts/night-grant.sh +448 -0
  202. package/scripts/night-run-test.sh +8 -0
  203. package/scripts/night-run.mjs +275 -0
  204. package/scripts/night-run.test.mjs +324 -0
  205. package/scripts/orchestrate-test.sh +190 -0
  206. package/scripts/orchestrate.sh +79 -8
  207. package/scripts/portable-test.sh +11 -2
  208. package/scripts/publish.sh +28 -3
  209. package/scripts/review-lint-test.sh +7 -7
  210. package/scripts/review-lint.sh +2 -2
  211. package/scripts/review-rules.conf +8 -0
  212. package/scripts/rule-recipes-test.sh +96 -0
  213. package/scripts/self-install-test.sh +39 -9
  214. package/scripts/setup-labels.sh +18 -1
  215. package/scripts/setup-milestones.sh +1 -1
  216. package/scripts/showcase.sh +1 -1
  217. package/scripts/skill-frontmatter-lint-test.sh +4 -4
  218. package/scripts/skill-frontmatter-lint.sh +1 -1
  219. package/scripts/status-test.sh +113 -2
  220. package/scripts/status.sh +16 -3
  221. package/scripts/task-sync-test.sh +154 -0
  222. package/scripts/task-sync.sh +243 -32
  223. package/scripts/test-runner-test.sh +158 -0
  224. package/scripts/test.sh +131 -23
  225. package/scripts/update-path-test.sh +279 -0
  226. package/scripts/ux-ds-audit.sh +1 -1
  227. package/scripts/ux-graph-scan.sh +6 -6
  228. package/scripts/wave-worker.sh +54 -0
  229. package/skills/kit-add/SKILL.md +77 -0
  230. package/skills/kit-autopilot/SKILL.md +123 -12
  231. package/skills/kit-cleanup/SKILL.md +5 -2
  232. package/skills/kit-contribute/SKILL.md +4 -4
  233. package/skills/kit-docs-writer/SKILL.md +16 -6
  234. package/skills/kit-effort-close/SKILL.md +17 -8
  235. package/skills/kit-effort-new/SKILL.md +15 -9
  236. package/skills/kit-effort-pr/SKILL.md +15 -7
  237. package/skills/kit-effort-start/SKILL.md +5 -2
  238. package/skills/kit-gc/SKILL.md +5 -2
  239. package/skills/kit-next/SKILL.md +5 -1
  240. package/skills/kit-onboard/SKILL.md +3 -3
  241. package/skills/kit-page/SKILL.md +93 -0
  242. package/skills/kit-plan-next/SKILL.md +8 -4
  243. package/skills/kit-security-sweep/SKILL.md +1 -3
  244. package/skills/kit-ship/SKILL.md +2 -3
  245. package/skills/kit-status/SKILL.md +35 -4
  246. package/skills/kit-task-new/SKILL.md +3 -3
  247. package/skills/kit-task-pr/SKILL.md +7 -2
  248. package/skills/kit-task-pr-merge/SKILL.md +7 -2
  249. package/skills/kit-task-start/SKILL.md +7 -7
  250. package/skills/kit-task-sync/SKILL.md +19 -3
  251. package/skills/kit-ux-audit/SKILL.md +3 -2
  252. package/templates/agents/auto-dev.md +5 -4
  253. package/templates/agents/backend.md +1 -1
  254. package/templates/agents/designer.md +2 -0
  255. package/templates/agents/devops.md +1 -1
  256. package/templates/agents/editor.md +9 -1
  257. package/templates/agents/frontend.md +1 -1
  258. package/templates/agents/local-delegate.md +1 -1
  259. package/templates/agents/pm.md +5 -5
  260. package/templates/agents/tech-lead.md +1 -1
  261. package/templates/benchmarks/SCHEMA.md +9 -3
  262. package/templates/codex/hooks.json.tmpl +89 -0
  263. package/templates/hooks/guard-base-branch-commit.sh.tmpl +13 -7
  264. package/templates/hooks/guard-raw-git-ops.sh.tmpl +27 -14
  265. package/templates/hooks/mempal_followup.sh.tmpl +2 -2
  266. package/templates/hooks/mempal_session_start.sh.tmpl +1 -1
  267. package/templates/hooks/prepush_gate.sh.tmpl +4 -2
  268. package/templates/hooks/repo-hygiene.sh.tmpl +3 -2
  269. package/templates/instructions.md.tmpl +174 -0
  270. package/templates/kit.config.json.tmpl +3 -3
  271. package/templates/knowledge-INDEX.md.tmpl +1 -1
  272. package/templates/lib/kit-sigil.sh.tmpl +1 -1
  273. package/templates/rules/authoring.md +106 -0
  274. package/templates/rules/branch-naming.md +40 -4
  275. package/templates/rules/code-retrieval.md +63 -0
  276. package/templates/rules/delegation-brief.md +0 -3
  277. package/templates/rules/design-routing.md +3 -3
  278. package/templates/rules/effort-model.md +14 -10
  279. package/templates/rules/gate-and-review.md +94 -0
  280. package/templates/rules/issue-lifecycle.md +88 -0
  281. package/templates/rules/knowledge-base.md +1 -1
  282. package/templates/rules/mempalace.md +1 -1
  283. package/templates/rules/merge-safety.md +108 -0
  284. package/templates/rules/naming-and-ids.md +23 -17
  285. package/templates/rules/plain-language.md +59 -0
  286. package/templates/rules/plan-next.md +9 -11
  287. package/templates/rules/react-annotate.md +6 -0
  288. package/templates/rules/review-pages.md +49 -0
  289. package/templates/rules/risk-tiered-review.md +13 -4
  290. package/templates/rules/shared-checkout.md +106 -0
  291. package/templates/rules/skill-gaps.md +2 -7
  292. package/templates/rules/task-management.md +13 -1
  293. package/templates/skills/brand-voice/SKILL.md +91 -0
  294. package/templates/skills/brand-voice/references/voice-template.md +61 -0
  295. package/templates/skills/campaign/SKILL.md +123 -0
  296. package/templates/skills/campaign/references/plan-template.md +55 -0
  297. package/templates/skills/concrete/SKILL.md +2 -2
  298. package/templates/skills/copywriting/SKILL.md +8 -1
  299. package/templates/skills/feature-build-refine/SKILL.md +2 -4
  300. package/templates/skills/morning-briefing/SKILL.md +2 -2
  301. package/templates/skills/page-design/SKILL.md +145 -0
  302. package/templates/skills/page-design/add.json +5 -0
  303. package/templates/skills/page-design/references/design-file-template.md +40 -0
  304. package/templates/skills/page-design/references/generic-page-patterns.md +43 -0
  305. package/templates/skills/page-design/references/minimum-rules.md +42 -0
  306. package/templates/skills/speckit/SKILL.md +4 -5
  307. package/templates/skills/supabase-patterns/SKILL.md +1 -1
  308. package/commands/kit-add.md +0 -42
  309. package/templates/CLAUDE.md.tmpl +0 -106
@@ -6,14 +6,50 @@
6
6
  # sync (#811) flushes the buffer to the Postgres graph (schema #810) when the engine is connected.
7
7
  # The record shape below IS that schema.
8
8
  #
9
- # record_effort_start <num> — stamp start epoch + write the pre-build estimate (#808)
9
+ # record_effort_start <num> — stamp start epoch + write the pre-build estimate (#808).
10
+ # Called by effort_start when it creates the branch (#453).
10
11
  # capture_effort_metrics <num> [base] — at close: build-time + signals + token/cost actuals (#807)
11
12
  # + reconcile vs the estimate → append the record
12
13
  #
14
+ # Where each measured field comes from (#453). A field with no source is null and the record says why:
15
+ # build_seconds close time minus <num>.start (build_source "effort-start"); with no stamp, close
16
+ # time minus the author time of the oldest commit in <base>..HEAD ("first-commit");
17
+ # null when the branch has neither.
18
+ # tokens_real 1. sessions listed in <git-common-dir>/kit-usage.jsonl, summed from their Claude
19
+ # transcripts (tokens_source "transcripts", cost at list price). cckit ships no
20
+ # writer for that log; a project that keeps one gets this path.
21
+ # 2. else the `cckit autopilot run` state files of the effort branch and its
22
+ # sub/<N><letter> branches — the usage the runtime printed (tokens_source
23
+ # "autopilot-run", cost_source "runtime" when the runtime printed a cost).
24
+ # Interactive sessions are not in that figure.
25
+ # 3. else null, with tokens_null_reason: "no-usage-source" (no usage log, no
26
+ # autopilot run), "no-session-matched" (a usage log with no session for this
27
+ # effort), "autopilot-usage-unknown" (a run whose rounds printed no usage — a
28
+ # partial sum would be an undercount).
29
+ # No figure is estimated: a null stays null.
30
+ #
13
31
  # Cost = metered API; retroactively $ is computed from current Opus-4.8 list price (no per-call bill
14
32
  # access here) → cost_source="list-price". bash 3.2 compatible. Requires: git, jq.
15
33
  # errors: best-effort — local telemetry must never break the op it measures
16
34
 
35
+ # This file's own directory, resolved ONCE at load time (#381) — the only point where both shells
36
+ # agree. `BASH_SOURCE` is bash-only; inside a zsh *function* it is unset and `$0` is the function
37
+ # name, so a per-call `dirname "${BASH_SOURCE[0]:-$0}"` resolved to the CALLER's directory:
38
+ # engine-adapter.sh and kit-local.sh were never found and the sync buffered without saying why. At
39
+ # FILE scope zsh sets `$0` to the sourced file's path, so one expansion covers both shells. Same
40
+ # resolution as kit-gc.sh's `_KIT_GC_LIB_DIR`, whose comment explains why zsh prompt expansion
41
+ # (`${(%):-%x}`) is avoided and why `cd`'s stdout is discarded.
42
+ _EM_LIB_DIR="$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null 2>&1 && pwd)"
43
+
44
+ # The GitHub transport switch (#396): rest mode routes the gh calls below through gh-rest.sh.
45
+ if ! command -v cckit_gh_rest >/dev/null 2>&1; then
46
+ _ghr_lib="$_EM_LIB_DIR/gh-rest.sh"
47
+ # shellcheck source=/dev/null
48
+ [ -f "$_ghr_lib" ] && . "$_ghr_lib"
49
+ unset _ghr_lib
50
+ fi
51
+ command -v cckit_gh_rest >/dev/null 2>&1 || cckit_gh_rest() { return 1; }
52
+
17
53
  # Opus 4.8 list price, USD per 1M tokens (correct as of 2026-06; update when prices change).
18
54
  _EM_PRICE_IN=5.00; _EM_PRICE_OUT=25.00; _EM_PRICE_CACHE_READ=0.50; _EM_PRICE_CACHE_WRITE=6.25
19
55
 
@@ -28,8 +64,9 @@ record_effort_start() {
28
64
  [[ -n "$num" ]] || return 0
29
65
  d="$(_em_dir)" || return 0
30
66
  mkdir -p "$d" 2>/dev/null || return 0
31
- date -u +%s > "$d/$num.start" 2>/dev/null || true
32
- estimate_effort "$num" || true
67
+ # Keep an existing stamp: a re-run of `effort start` must not move the start forward.
68
+ [[ -s "$d/$num.start" ]] || date -u +%s > "$d/$num.start" 2>/dev/null || true
69
+ [[ -f "$d/$num.est" ]] || estimate_effort "$num" || true
33
70
  }
34
71
 
35
72
  # estimate_effort <num> — pre-build prediction from estimator.json + the effort's scope (#808).
@@ -43,8 +80,11 @@ estimate_effort() {
43
80
  command -v jq >/dev/null 2>&1 || return 0
44
81
 
45
82
  repo="${EFFORT_REPO:-${KIT_REPO:-}}"
83
+ if cckit_gh_rest; then subs="$(gh_rest_sub_issue_numbers "$repo" "$num" 2>/dev/null | grep -c .)"
84
+ else
46
85
  subs="$(gh api graphql -f query='query($o:String!,$r:String!,$n:Int!){repository(owner:$o,name:$r){issue(number:$n){subIssues(first:50){totalCount}}}}' \
47
86
  -F o="${repo%/*}" -F r="${repo#*/}" -F n="$num" --jq '.data.repository.issue.subIssues.totalCount' 2>/dev/null)"
87
+ fi
48
88
  [[ "$subs" =~ ^[0-9]+$ ]] || subs=1; [[ "$subs" -lt 1 ]] && subs=1
49
89
 
50
90
  cpl="$(jq -r '.model.cost_per_changed_line_usd // .model.cost_per_changed_line // 0.21' "$est")"
@@ -129,7 +169,7 @@ _em_token_sum() {
129
169
  [[ -n "$sids" ]] || { echo "0 0 0 0"; return 0; }
130
170
  while IFS= read -r sid; do
131
171
  [[ -n "$sid" ]] || continue
132
- tx="$(find "$HOME/.claude/projects" -name "$sid.jsonl" -type f 2>/dev/null | head -1)"
172
+ tx="$(find "$HOME/.claude/projects" -name "$sid.jsonl" -type f 2>/dev/null | sed -n 1p)"
133
173
  [[ -n "$tx" ]] || continue
134
174
  # -r (raw) is REQUIRED: without it jq wraps the result string in double-quotes, so $a
135
175
  # becomes `"1015901` and the `$(( ))` math below dies ("bad math expression") — the #829 bug.
@@ -140,7 +180,51 @@ _em_token_sum() {
140
180
  echo "$tin $tout $tcr $tcw"
141
181
  }
142
182
 
183
+ # _em_usage_log — absolute path of the session usage log (it may not exist).
184
+ _em_usage_log() {
185
+ local c; c="$(git rev-parse --git-common-dir 2>/dev/null)" || return 1
186
+ case "$c" in /*) : ;; *) c="$PWD/$c" ;; esac
187
+ printf '%s/kit-usage.jsonl' "$c"
188
+ }
189
+
190
+ # _em_autopilot_usage <num> <branch> — "<tokens> <cost-usd> <runs>" from the `cckit autopilot run`
191
+ # state files (<state-dir>/autopilot/*.json, scripts/autopilot-run.sh) whose branch is <branch> or a
192
+ # sub/<num><letter>-… branch of the effort. Those hold what the runtime itself printed (al_usage).
193
+ # tokens a sum · `unknown` when any matching run reads unknown · `none` when no run matched
194
+ # cost a sum · `unknown` when any matching run printed no cost
195
+ # A run with zero rounds spent nothing and is skipped.
196
+ _em_autopilot_usage() {
197
+ local num="$1" branch="$2" sdir f out
198
+ local files=()
199
+ if ! command -v kit_state_dir >/dev/null 2>&1; then
200
+ # shellcheck source=/dev/null
201
+ [[ -f "$_EM_LIB_DIR/kit-state.sh" ]] && . "$_EM_LIB_DIR/kit-state.sh"
202
+ fi
203
+ command -v kit_state_dir >/dev/null 2>&1 || { echo "none unknown 0"; return 0; }
204
+ sdir="$(kit_state_dir 2>/dev/null)/autopilot"
205
+ [[ -d "$sdir" ]] || { echo "none unknown 0"; return 0; }
206
+ # find, not a glob: an unmatched glob is an error under zsh.
207
+ while IFS= read -r f; do [[ -n "$f" ]] && files+=("$f"); done \
208
+ < <(find "$sdir" -maxdepth 1 -type f -name '*.json' 2>/dev/null)
209
+ (( ${#files[@]} > 0 )) || { echo "none unknown 0"; return 0; }
210
+ out="$(jq -rs --arg b "$branch" --arg n "$num" '
211
+ [ .[] | objects
212
+ | select((.branch? // "") as $x
213
+ | ($b != "" and $x == $b) or ($x | test("^sub/" + $n + "[a-z]-")))
214
+ | select((.rounds? // 0) > 0) ] as $runs
215
+ | if ($runs | length) == 0 then "none unknown 0" else
216
+ ( if any($runs[]; (.usage.tokens? | type) != "number") then "unknown"
217
+ else ([$runs[].usage.tokens] | add) end ) as $tok
218
+ | ( if any($runs[]; (.usage.cost_usd? | type) != "number") then "unknown"
219
+ else ([$runs[].usage.cost_usd] | add) end ) as $cost
220
+ | "\($tok) \($cost) \($runs | length)"
221
+ end' "${files[@]}" 2>/dev/null)"
222
+ [[ -n "$out" ]] || out="none unknown 0"
223
+ echo "$out"
224
+ }
225
+
143
226
  capture_effort_metrics() {
227
+ local build_src tokens_src null_reason ap ap_tok ap_cost ap_runs first ulog
144
228
  local num="$1" base="${2:-origin/${KIT_BASE_BRANCH:-main}}" d start now build_s shortstat files added removed commits churn diff_auto branch
145
229
  local toks tin tout tcr tcw tokens_real cost_real cost_source tokens_est cost_est rec
146
230
  local IFS=' ' # force space-split for `set --` below (ambient IFS may carry a NUL)
@@ -148,17 +232,30 @@ capture_effort_metrics() {
148
232
  command -v jq >/dev/null 2>&1 || return 0
149
233
  d="$(_em_dir)" || return 0; mkdir -p "$d" 2>/dev/null || return 0
150
234
 
151
- now="$(date -u +%s 2>/dev/null)"; build_s="null"
235
+ # build time (#453): the stamp effort_start wrote, else the oldest commit on the branch (below,
236
+ # once the base is fetched). Never a guess: with neither, build_seconds stays null.
237
+ now="$(date -u +%s 2>/dev/null)"; build_s="null"; build_src="null"; start=""
152
238
  if [[ -f "$d/$num.start" ]]; then
153
239
  start="$(cat "$d/$num.start" 2>/dev/null)"
154
- [[ "$start" =~ ^[0-9]+$ && "$now" =~ ^[0-9]+$ ]] && build_s=$(( now - start ))
240
+ if [[ "$start" =~ ^[0-9]+$ && "$now" =~ ^[0-9]+$ ]] && (( now >= start )); then
241
+ build_s=$(( now - start )); build_src='"effort-start"'
242
+ else
243
+ start=""
244
+ fi
155
245
  fi
156
246
 
157
247
  git fetch origin --quiet 2>/dev/null || true
248
+ if [[ "$build_s" == "null" && "$now" =~ ^[0-9]+$ ]]; then
249
+ # Author time, not committer time: a rebase rewrites the second and keeps the first.
250
+ first="$(git log --format=%at "$base"..HEAD 2>/dev/null | sort -n | sed -n 1p)"
251
+ if [[ "$first" =~ ^[0-9]+$ ]] && (( now >= first )); then
252
+ start="$first"; build_s=$(( now - first )); build_src='"first-commit"'
253
+ fi
254
+ fi
158
255
  shortstat="$(git diff --shortstat "$base"...HEAD 2>/dev/null)"
159
- files=$(printf '%s' "$shortstat" | grep -oE '[0-9]+ file' | grep -oE '[0-9]+' | head -1)
160
- added=$(printf '%s' "$shortstat" | grep -oE '[0-9]+ insertion' | grep -oE '[0-9]+' | head -1)
161
- removed=$(printf '%s' "$shortstat" | grep -oE '[0-9]+ deletion' | grep -oE '[0-9]+' | head -1)
256
+ files=$(printf '%s' "$shortstat" | grep -oE '[0-9]+ file' | grep -oE '[0-9]+' | sed -n 1p)
257
+ added=$(printf '%s' "$shortstat" | grep -oE '[0-9]+ insertion' | grep -oE '[0-9]+' | sed -n 1p)
258
+ removed=$(printf '%s' "$shortstat" | grep -oE '[0-9]+ deletion' | grep -oE '[0-9]+' | sed -n 1p)
162
259
  commits=$(git rev-list --count "$base"..HEAD 2>/dev/null)
163
260
  files=${files:-0}; added=${added:-0}; removed=${removed:-0}; commits=${commits:-0}
164
261
  churn=$(( added + removed )); diff_auto=$(_em_difficulty "$churn" "$files")
@@ -179,9 +276,19 @@ capture_effort_metrics() {
179
276
  cost_real=$(awk -v i="$tin" -v o="$tout" -v cr="$tcr" -v cw="$tcw" \
180
277
  -v pi="$_EM_PRICE_IN" -v po="$_EM_PRICE_OUT" -v pcr="$_EM_PRICE_CACHE_READ" -v pcw="$_EM_PRICE_CACHE_WRITE" \
181
278
  'BEGIN{printf "%.4f", (i*pi + o*po + cr*pcr + cw*pcw)/1000000}')
182
- cost_source='"list-price"'
279
+ cost_source='"list-price"'; tokens_src='"transcripts"'; null_reason=null
183
280
  else
184
- tokens_real=null; cost_real=null; cost_source=null
281
+ tokens_real=null; cost_real=null; cost_source=null; tokens_src=null
282
+ ulog="$(_em_usage_log 2>/dev/null)"
283
+ if [[ -n "$ulog" && -f "$ulog" ]]; then null_reason='"no-session-matched"'; else null_reason='"no-usage-source"'; fi
284
+ # No transcript figure: read what `cckit autopilot run` recorded for this effort's branches (#453).
285
+ ap="$(_em_autopilot_usage "$num" "$branch")"; read -r ap_tok ap_cost ap_runs <<< "$ap"
286
+ if [[ "$ap_tok" =~ ^[0-9]+$ ]]; then
287
+ tokens_real="$ap_tok"; tokens_src='"autopilot-run"'; null_reason=null
288
+ if [[ "$ap_cost" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then cost_real="$ap_cost"; cost_source='"runtime"'; fi
289
+ elif [[ "$ap_tok" == "unknown" ]]; then
290
+ null_reason='"autopilot-usage-unknown"'
291
+ fi
185
292
  fi
186
293
 
187
294
  # estimate (#808) for reconciliation.
@@ -195,17 +302,19 @@ capture_effort_metrics() {
195
302
  --argjson build "$build_s" --argjson files "$files" --argjson added "$added" \
196
303
  --argjson removed "$removed" --argjson commits "$commits" \
197
304
  --argjson tokens_real "$tokens_real" --argjson cost_real "$cost_real" --argjson cost_source "$cost_source" \
198
- --argjson tokens_est "$tokens_est" --argjson cost_est "$cost_est" --argjson diff "$diff_auto" '
199
- {effort:$num, branch:$branch, captured_at:$at, build_seconds:$build,
305
+ --argjson tokens_est "$tokens_est" --argjson cost_est "$cost_est" --argjson diff "$diff_auto" \
306
+ --argjson build_src "$build_src" --argjson tokens_src "$tokens_src" --argjson null_reason "$null_reason" '
307
+ {effort:$num, branch:$branch, captured_at:$at, build_seconds:$build, build_source:$build_src,
200
308
  signals:{files:$files, added:$added, removed:$removed, commits:$commits},
201
- tokens_real:$tokens_real, cost_real_usd:$cost_real, cost_source:$cost_source,
309
+ tokens_real:$tokens_real, tokens_source:$tokens_src, tokens_null_reason:$null_reason,
310
+ cost_real_usd:$cost_real, cost_source:$cost_source,
202
311
  tokens_est:$tokens_est, cost_est_usd:$cost_est,
203
312
  difficulty_auto:$diff, score_auto:null, difficulty_judge:null, score_judge:null,
204
313
  synced_to_graph:false}' 2>/dev/null)" || return 0
205
314
  [[ -n "$rec" ]] || return 0
206
315
  printf '%s\n' "$rec" >> "$d/records.jsonl"
207
316
  rm -f "$d/$num.start" "$d/$num.est" 2>/dev/null || true
208
- echo "[#$num] metrics: build=${build_s}s files=$files +$added/-$removed commits=$commits diff=$diff_auto tokens_real=$tokens_real cost_real=$cost_real est_tokens=$tokens_est → records.jsonl" >&2
317
+ 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
209
318
  # Self-improving estimator (#812): recalibrate the medians/ratios from the now-larger dataset so the
210
319
  # next effort's pre-build estimate reflects reality (flips tokens_calibrated once ≥5 real-token rows).
211
320
  recalibrate_estimator >/dev/null 2>&1 || true
@@ -247,32 +356,71 @@ _em_judge_goal() {
247
356
  local num="$1" repo body
248
357
  command -v gh >/dev/null 2>&1 || { printf ''; return 0; }
249
358
  repo="${EFFORT_REPO:-${KIT_REPO:-}}"
250
- body="$(gh issue view "$num" --repo "$repo" --json body --jq '.body' 2>/dev/null)" || { printf ''; return 0; }
359
+ if cckit_gh_rest; then body="$(gh api "repos/$repo/issues/$num" --jq '.body // ""' 2>/dev/null)" || { printf ''; return 0; }
360
+ else body="$(gh issue view "$num" --repo "$repo" --json body --jq '.body' 2>/dev/null)" || { printf ''; return 0; }; fi
251
361
  [[ -n "$body" ]] || { printf ''; return 0; }
252
362
  # Extract the lines after "## Goal" up to the next "## " heading.
253
363
  printf '%s\n' "$body" | awk '
254
364
  /^##[ \t]+[Gg]oal[ \t]*$/ { grab=1; next }
255
365
  /^##[ \t]/ { if (grab) exit }
256
- grab { print }' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | grep -v '^$' | head -20
366
+ grab { print }' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | grep -v '^$' | sed -n 1,20p
367
+ }
368
+
369
+ # _em_model_env <config-key> <default-name> — the VALUE of the env var the hosted judge reads for
370
+ # one setting (#381). The config may NAME the variable (`model.urlEnv` / `model.modelEnv` /
371
+ # `model.tokenEnv`) so a project that already exports its own model variables needs no fork; unset,
372
+ # the built-in name is used. Names only, never values — the same contract as `engine.token_env`.
373
+ # The name is expanded through `eval` (the one indirection bash 3.2 and zsh share), so anything
374
+ # that is not a plain shell identifier is refused with a warning and the default name is used.
375
+ _em_model_env() {
376
+ local key="$1" name="$2" cfg="" named=""
377
+ if ! command -v kit_config_path >/dev/null 2>&1 && [[ -f "$_EM_LIB_DIR/config-path.sh" ]]; then
378
+ # shellcheck source=/dev/null
379
+ source "$_EM_LIB_DIR/config-path.sh"
380
+ fi
381
+ command -v kit_config_path >/dev/null 2>&1 && cfg="$(kit_config_path 2>/dev/null || true)"
382
+ if [[ -n "$cfg" && -f "$cfg" ]]; then
383
+ named="$(jq -r --arg k "$key" '.model[$k] // empty | strings' "$cfg" 2>/dev/null)" || named=""
384
+ fi
385
+ if [[ -n "$named" ]]; then
386
+ if grep -qE '^[A-Za-z_][A-Za-z0-9_]*$' <<<"$named" && [[ "$named" != *$'\n'* ]]; then
387
+ name="$named"
388
+ else
389
+ echo "effort-metrics: model.$key is not an env var name — using $name" >&2
390
+ fi
391
+ fi
392
+ eval "printf '%s' \"\${$name:-}\""
257
393
  }
258
394
 
259
- # _em_judge_call <system> <user> — try CCKIT_MODEL (hosted, OpenAI-compat) then the local model.
395
+ # _em_judge_call <system> <user> — try the hosted model (OpenAI-compat) then the local model.
396
+ # The hosted endpoint, model name and token come from CCKIT_MODEL_URL / CCKIT_MODEL /
397
+ # CCKIT_MODEL_TOKEN, or from the env vars the config names (see _em_model_env).
260
398
  # Echoes "<model_id>\t<reply>" on success; non-zero + nothing on total failure. The model_id is the
261
399
  # hosted model name or the local tag, so the schema's model_id column records who judged.
262
400
  _em_judge_call() {
263
- local system="$1" user="$2" url payload reply model
264
-
265
- # PRIMARY: hosted CCKIT_MODEL (OpenAI-compatible chat completions).
266
- if [[ -n "${CCKIT_MODEL_URL:-}" ]] && command -v curl >/dev/null 2>&1; then
267
- url="${CCKIT_MODEL_URL%/}/chat/completions"
268
- model="${CCKIT_MODEL:-default}"
401
+ local system="$1" user="$2" url payload reply model base token
402
+
403
+ # PRIMARY: hosted model (OpenAI-compatible chat completions).
404
+ base="$(_em_model_env urlEnv CCKIT_MODEL_URL)"
405
+ if [[ -n "$base" ]] && command -v curl >/dev/null 2>&1; then
406
+ url="${base%/}/chat/completions"
407
+ model="$(_em_model_env modelEnv CCKIT_MODEL)"; [[ -n "$model" ]] || model="default"
408
+ token="$(_em_model_env tokenEnv CCKIT_MODEL_TOKEN)"
269
409
  payload="$(jq -n --arg m "$model" --arg s "$system" --arg p "$user" \
270
410
  '{model:$m, messages:[{role:"system",content:$s},{role:"user",content:$p}], max_tokens:512, temperature:0.1}')" || payload=""
271
411
  if [[ -n "$payload" ]]; then
272
- reply="$(curl -sf -m "${EM_JUDGE_TIMEOUT:-60}" "$url" \
273
- -H 'Content-Type: application/json' \
274
- ${CCKIT_MODEL_TOKEN:+-H "Authorization: Bearer $CCKIT_MODEL_TOKEN"} \
275
- -d "$payload" 2>/dev/null | jq -r '.choices[0].message.content // empty' 2>/dev/null)" || reply=""
412
+ # Two explicit calls, not `${token:+-H "…"}`: zsh does not word-split that expansion, so the
413
+ # flag and its value reached curl as ONE argument and the token was never sent.
414
+ if [[ -n "$token" ]]; then
415
+ reply="$(curl -sf -m "${EM_JUDGE_TIMEOUT:-60}" "$url" \
416
+ -H 'Content-Type: application/json' \
417
+ -H "Authorization: Bearer $token" \
418
+ -d "$payload" 2>/dev/null | jq -r '.choices[0].message.content // empty' 2>/dev/null)" || reply=""
419
+ else
420
+ reply="$(curl -sf -m "${EM_JUDGE_TIMEOUT:-60}" "$url" \
421
+ -H 'Content-Type: application/json' \
422
+ -d "$payload" 2>/dev/null | jq -r '.choices[0].message.content // empty' 2>/dev/null)" || reply=""
423
+ fi
276
424
  if [[ -n "$reply" ]]; then
277
425
  printf '%s\t%s' "$model" "$reply"
278
426
  return 0
@@ -282,8 +430,8 @@ _em_judge_call() {
282
430
 
283
431
  # FALLBACK: local model via kit-local.sh.
284
432
  if ! declare -f kit_local_chat >/dev/null 2>&1; then
285
- local _ldir; _ldir="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
286
- [[ -f "$_ldir/kit-local.sh" ]] && source "$_ldir/kit-local.sh"
433
+ # shellcheck source=/dev/null
434
+ [[ -f "$_EM_LIB_DIR/kit-local.sh" ]] && source "$_EM_LIB_DIR/kit-local.sh"
287
435
  fi
288
436
  if declare -f kit_local_chat >/dev/null 2>&1 && kit_local_alive 2>/dev/null; then
289
437
  reply="$(kit_local_chat "$system" "$user" 512 2>/dev/null)" || reply=""
@@ -338,7 +486,7 @@ ${diff}${truncated_note}"
338
486
  model="${out%%$'\t'*}"; reply="${out#*$'\t'}"
339
487
 
340
488
  # Extract the first {...} object from the reply (models often wrap it in prose/fences).
341
- json="$(printf '%s' "$reply" | tr '\n' ' ' | grep -oE '\{[^{}]*\}' | head -1)"
489
+ json="$(printf '%s' "$reply" | tr '\n' ' ' | grep -oE '\{[^{}]*\}' | sed -n 1p)"
342
490
  if [[ -z "$json" ]]; then
343
491
  echo "[#$num] judge: model reply had no JSON object — leaving *_judge null (model=$model)" >&2
344
492
  return 0
@@ -350,7 +498,7 @@ ${diff}${truncated_note}"
350
498
  echo "[#$num] judge: difficulty_judge='$dj' out of range — leaving *_judge null (model=$model)" >&2
351
499
  return 0
352
500
  fi
353
- if ! printf '%s' "$sj" | grep -qE '^[0-9]+(\.[0-9]+)?$' || ! awk -v s="$sj" 'BEGIN{exit !(s>=0 && s<=1)}'; then
501
+ if ! grep -qE '^[0-9]+(\.[0-9]+)?$' <<<"$sj" || ! awk -v s="$sj" 'BEGIN{exit !(s>=0 && s<=1)}'; then
354
502
  echo "[#$num] judge: score_judge='$sj' out of range — leaving *_judge null (model=$model)" >&2
355
503
  return 0
356
504
  fi
@@ -404,8 +552,8 @@ ${diff}${truncated_note}"
404
552
  sync_effort_metrics() {
405
553
  command -v jq >/dev/null 2>&1 || return 0
406
554
  if ! declare -f engine_call >/dev/null 2>&1; then
407
- local _adir; _adir="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
408
- [[ -f "$_adir/engine-adapter.sh" ]] && source "$_adir/engine-adapter.sh"
555
+ # shellcheck source=/dev/null
556
+ [[ -f "$_EM_LIB_DIR/engine-adapter.sh" ]] && source "$_EM_LIB_DIR/engine-adapter.sh"
409
557
  fi
410
558
  declare -f engine_enabled >/dev/null 2>&1 || { echo "effort-metrics: engine adapter unavailable — buffered locally" >&2; return 0; }
411
559
  engine_enabled || { echo "effort-metrics: engine off — metrics stay in the local buffer" >&2; return 0; }
@@ -455,7 +603,12 @@ recalibrate_estimator() {
455
603
  median_commits: med([$rows[].commits]),
456
604
  median_build_seconds: med([$rows[]|select(.build!=null)|.build]),
457
605
  tokens_per_changed_line: (if $nrt>=$min then (([$rt[].tok]|add)/([$rt[].lines]|add)) else null end),
458
- cost_per_changed_line_usd: (if $nrt>=$min then (([$rt[]|(.cost|tonumber? // 0)]|add)/([$rt[].lines]|add)) else null end),
606
+ cost_per_changed_line_usd: (
607
+ # Only rows that carry a cost: an autopilot-run row from a runtime that prints none has
608
+ # tokens and a null cost (#453), and counting it as 0 would lower the ratio.
609
+ ($rt | map(select(.cost != null))) as $rc
610
+ | if $nrt>=$min and ($rc|length) > 0 and ([$rc[].lines]|add) > 0
611
+ then (([$rc[]|(.cost|tonumber? // 0)]|add)/([$rc[].lines]|add)) else null end),
459
612
  recalibrated_at:$stamp }' "$f")"
460
613
  [[ -n "$model" ]] || { echo "effort-metrics: recalibrate produced nothing" >&2; return 1; }
461
614
  if [[ -f "$est" ]]; then