@jeiemgi/cckit 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (305) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +169 -21
  3. package/README.md +491 -35
  4. package/SECURITY.md +5 -0
  5. package/bin/cckit +182 -17
  6. package/cckit.config.json +1 -1
  7. package/commands/kit-doctor.md +13 -5
  8. package/commands/kit-export-project.md +0 -2
  9. package/commands/kit-export-training.md +6 -9
  10. package/commands/kit-init.md +43 -13
  11. package/commands/kit-routines.md +1 -1
  12. package/commands/kit-update.md +11 -6
  13. package/databases/supabase/guide.html +106 -0
  14. package/databases/supabase/nextjs/lib/supabase/client.ts +9 -0
  15. package/databases/supabase/nextjs/lib/supabase/server.ts +29 -0
  16. package/databases/supabase.json +70 -0
  17. package/integrations/emil-design-eng.json +41 -0
  18. package/integrations/graft.json +81 -0
  19. package/integrations/impeccable.json +42 -0
  20. package/integrations/integration.schema.json +215 -0
  21. package/integrations/lavish.json +43 -0
  22. package/integrations/mempalace.json +28 -0
  23. package/integrations/mlx-lm.json +32 -0
  24. package/integrations/web-design-guidelines.json +42 -0
  25. package/kit.config.schema.json +183 -5
  26. package/package.json +4 -1
  27. package/profiles/automation.json +7 -1
  28. package/profiles/content.json +10 -2
  29. package/profiles/marketing.json +57 -0
  30. package/profiles/minimal.json +7 -1
  31. package/profiles/research.json +7 -1
  32. package/profiles/software.json +7 -1
  33. package/recipes/nextjs-shadcn.json +49 -0
  34. package/scripts/agent-skills-external.txt +45 -0
  35. package/scripts/agent-skills-lint-test.sh +131 -0
  36. package/scripts/agent-skills-lint.sh +115 -0
  37. package/scripts/agent-template-lint-test.sh +42 -0
  38. package/scripts/agent-template-lint.sh +58 -0
  39. package/scripts/annotate-setup.sh +1 -1
  40. package/scripts/autopilot-run-test.sh +338 -0
  41. package/scripts/autopilot-run.sh +448 -0
  42. package/scripts/autopilot-test.sh +8 -0
  43. package/scripts/autopilot.sh +10 -1
  44. package/scripts/benchmark/benchmark-test.sh +85 -0
  45. package/scripts/benchmark/checkpoint-test.sh +314 -0
  46. package/scripts/benchmark/checkpoint.sh +408 -0
  47. package/scripts/benchmark/lib/model.sh +31 -6
  48. package/scripts/benchmark/report.sh +17 -2
  49. package/scripts/benchmark/run-quality.sh +36 -10
  50. package/scripts/benchmark/verb-timings-test.sh +124 -0
  51. package/scripts/benchmark/verb-timings.sh +149 -0
  52. package/scripts/check.sh +29 -19
  53. package/scripts/content-skills-test.sh +144 -0
  54. package/scripts/copilot-loop-test.sh +25 -0
  55. package/scripts/database-supabase-test.sh +121 -0
  56. package/scripts/docs-marketing-words-test.sh +73 -0
  57. package/scripts/docs-record-tables-test.sh +72 -0
  58. package/scripts/hook-adapter-test.sh +193 -0
  59. package/scripts/init-content-test.sh +225 -0
  60. package/scripts/init-guided-test.sh +474 -0
  61. package/scripts/init-recipes-test.sh +104 -0
  62. package/scripts/init-upgrade-test.sh +341 -2
  63. package/scripts/init.sh +746 -95
  64. package/scripts/instruction-budget-baseline.txt +8 -0
  65. package/scripts/instruction-budget-lint-test.sh +166 -0
  66. package/scripts/instruction-budget-lint.sh +219 -0
  67. package/scripts/integrations-check-test.sh +200 -0
  68. package/scripts/integrations-check.sh +157 -0
  69. package/scripts/kit-add-test.sh +214 -0
  70. package/scripts/kit-add.sh +305 -5
  71. package/scripts/kit-adopt-test.sh +16 -0
  72. package/scripts/kit-adopt.sh +3 -2
  73. package/scripts/kit-digest.sh +2 -2
  74. package/scripts/kit-doctor-test.sh +293 -0
  75. package/scripts/kit-doctor.sh +272 -140
  76. package/scripts/kit-export-project-test.sh +3 -3
  77. package/scripts/kit-remove-test.sh +19 -0
  78. package/scripts/kit-routines.sh +2 -2
  79. package/scripts/kit-self-update.sh +1 -1
  80. package/scripts/kit-version-check-test.sh +4 -0
  81. package/scripts/kit-version-check.sh +4 -2
  82. package/scripts/knowledge-lint.sh +4 -4
  83. package/scripts/leak-fence-test.sh +89 -0
  84. package/scripts/lib/adapter-select-test.sh +136 -0
  85. package/scripts/lib/adapter-select.sh +158 -0
  86. package/scripts/lib/agent-def-test.sh +69 -0
  87. package/scripts/lib/agent-def.sh +106 -0
  88. package/scripts/lib/agent-launch-test.sh +212 -0
  89. package/scripts/lib/agent-launch.sh +335 -0
  90. package/scripts/lib/board-view-test.sh +11 -0
  91. package/scripts/lib/board-view.sh +42 -26
  92. package/scripts/lib/branch-owner-test.sh +26 -0
  93. package/scripts/lib/branch-owner.sh +16 -9
  94. package/scripts/lib/captain-test.sh +105 -0
  95. package/scripts/lib/captain.sh +87 -8
  96. package/scripts/lib/codex-hooks-test.sh +100 -0
  97. package/scripts/lib/codex-hooks.sh +69 -0
  98. package/scripts/lib/commitlint.sh +1 -1
  99. package/scripts/lib/content-kinds-gc-test.sh +88 -0
  100. package/scripts/lib/copilot-test.sh +70 -4
  101. package/scripts/lib/copilot.sh +66 -14
  102. package/scripts/lib/effort-dispatch-test.sh +3 -3
  103. package/scripts/lib/effort-metrics-test.sh +178 -1
  104. package/scripts/lib/effort-metrics.sh +175 -35
  105. package/scripts/lib/effort-ops-test.sh +205 -7
  106. package/scripts/lib/effort-ops.sh +198 -43
  107. package/scripts/lib/effort-parity-test.sh +49 -13
  108. package/scripts/lib/effort-plan-test.sh +337 -0
  109. package/scripts/lib/effort-plan.sh +200 -23
  110. package/scripts/lib/effort.sh +56 -11
  111. package/scripts/lib/errors-header-test.sh +1 -1
  112. package/scripts/lib/gh-project-test.sh +35 -1
  113. package/scripts/lib/gh-project.sh +23 -16
  114. package/scripts/lib/gh-rest-test.sh +32 -3
  115. package/scripts/lib/gh-rest.sh +17 -2
  116. package/scripts/lib/hook-refresh.sh +1 -1
  117. package/scripts/lib/init-github.sh +412 -0
  118. package/scripts/lib/init-guide.sh +630 -0
  119. package/scripts/lib/integration-install-test.sh +343 -0
  120. package/scripts/lib/integration-install.sh +257 -0
  121. package/scripts/lib/integrations-test.sh +366 -0
  122. package/scripts/lib/integrations.sh +244 -0
  123. package/scripts/lib/kinds-test.sh +125 -0
  124. package/scripts/lib/kinds.sh +66 -0
  125. package/scripts/lib/kit-brief-test.sh +22 -3
  126. package/scripts/lib/kit-brief.sh +45 -5
  127. package/scripts/lib/kit-config-resolve.sh +2 -2
  128. package/scripts/lib/kit-config-test.sh +64 -1
  129. package/scripts/lib/kit-config.sh +37 -1
  130. package/scripts/lib/kit-events.sh +7 -0
  131. package/scripts/lib/kit-gc-test.sh +173 -1
  132. package/scripts/lib/kit-gc.sh +226 -37
  133. package/scripts/lib/kit-hotfix.sh +216 -0
  134. package/scripts/lib/kit-interview-test.sh +93 -0
  135. package/scripts/lib/kit-interview.sh +117 -8
  136. package/scripts/lib/kit-lib-test.sh +1 -1
  137. package/scripts/lib/kit-lib.sh +3 -3
  138. package/scripts/lib/kit-local.sh +2 -2
  139. package/scripts/lib/kit-manifest.sh +33 -1
  140. package/scripts/lib/kit-profile-test.sh +3 -3
  141. package/scripts/lib/kit-status-test.sh +112 -4
  142. package/scripts/lib/kit-status.sh +246 -16
  143. package/scripts/lib/kit-task-ops-test.sh +37 -0
  144. package/scripts/lib/kit-task-ops.sh +175 -31
  145. package/scripts/lib/kto-hotfix-test.sh +232 -0
  146. package/scripts/lib/kto-task-close-test.sh +1 -1
  147. package/scripts/lib/kto-task-new-test.sh +95 -0
  148. package/scripts/lib/kto-task-pr-merge-test.sh +37 -3
  149. package/scripts/lib/kto-task-pr-test.sh +70 -0
  150. package/scripts/lib/leak-fence.sh +89 -0
  151. package/scripts/lib/light-task-run-test.sh +255 -0
  152. package/scripts/lib/light-task-test.sh +309 -0
  153. package/scripts/lib/light-task.sh +706 -0
  154. package/scripts/lib/next-test.sh +99 -0
  155. package/scripts/lib/next.sh +9 -3
  156. package/scripts/lib/orchestration-runtime-test.sh +72 -0
  157. package/scripts/lib/orchestration-runtime.sh +57 -5
  158. package/scripts/lib/plan-machine-test.sh +15 -2
  159. package/scripts/lib/plan-machine.sh +20 -7
  160. package/scripts/lib/plan-next-test.sh +1 -1
  161. package/scripts/lib/plan-next.sh +4 -4
  162. package/scripts/lib/pr-evidence-test.sh +3 -3
  163. package/scripts/lib/pr-evidence.sh +1 -1
  164. package/scripts/lib/qa-lane-test.sh +626 -0
  165. package/scripts/lib/qa-lane.sh +1357 -0
  166. package/scripts/lib/qa-lanes-test.sh +175 -0
  167. package/scripts/lib/react-detect.sh +1 -1
  168. package/scripts/lib/render-test.sh +1 -1
  169. package/scripts/lib/review-page-test.sh +318 -0
  170. package/scripts/lib/review-page.sh +369 -0
  171. package/scripts/lib/review-stage-test.sh +62 -11
  172. package/scripts/lib/review-stage.sh +42 -60
  173. package/scripts/lib/role-identity-test.sh +65 -0
  174. package/scripts/lib/role-identity.sh +25 -2
  175. package/scripts/lib/secret-guard.sh +3 -3
  176. package/scripts/lib/session-registry-test.sh +111 -0
  177. package/scripts/lib/session-registry.sh +233 -0
  178. package/scripts/lib/stage-receipt-test.sh +14 -1
  179. package/scripts/lib/stage-receipt.sh +13 -8
  180. package/scripts/lib/task-source-file.sh +309 -0
  181. package/scripts/lib/task-source.sh +130 -0
  182. package/scripts/lib/upgrade-report-test.sh +120 -0
  183. package/scripts/lib/upgrade-report.sh +138 -0
  184. package/scripts/lib/version-bump.sh +3 -3
  185. package/scripts/lib/wave-result-test.sh +80 -0
  186. package/scripts/lib/wave-result.sh +127 -0
  187. package/scripts/lib/worker-brief-test.sh +5 -0
  188. package/scripts/lib/worker-brief.sh +2 -2
  189. package/scripts/lib/worktree-issue-test.sh +30 -1
  190. package/scripts/lib/worktree-issue.sh +82 -12
  191. package/scripts/lib/worktree-ports-test.sh +57 -4
  192. package/scripts/lib/worktree-start-test.sh +197 -0
  193. package/scripts/lib/worktree-start.sh +153 -31
  194. package/scripts/lib/zsh-safety-test.sh +19 -3
  195. package/scripts/marketing-walkthrough-test.sh +152 -0
  196. package/scripts/night-grant-test.sh +37 -0
  197. package/scripts/night-grant.sh +448 -0
  198. package/scripts/night-run-test.sh +8 -0
  199. package/scripts/night-run.mjs +275 -0
  200. package/scripts/night-run.test.mjs +324 -0
  201. package/scripts/orchestrate-test.sh +190 -0
  202. package/scripts/orchestrate.sh +79 -8
  203. package/scripts/portable-test.sh +11 -2
  204. package/scripts/publish.sh +28 -3
  205. package/scripts/review-lint-test.sh +7 -7
  206. package/scripts/review-lint.sh +2 -2
  207. package/scripts/review-rules.conf +8 -0
  208. package/scripts/rule-recipes-test.sh +96 -0
  209. package/scripts/self-install-test.sh +39 -9
  210. package/scripts/setup-labels.sh +18 -1
  211. package/scripts/setup-milestones.sh +1 -1
  212. package/scripts/showcase.sh +1 -1
  213. package/scripts/skill-frontmatter-lint-test.sh +4 -4
  214. package/scripts/skill-frontmatter-lint.sh +1 -1
  215. package/scripts/status-test.sh +113 -2
  216. package/scripts/status.sh +16 -3
  217. package/scripts/task-sync-test.sh +154 -0
  218. package/scripts/task-sync.sh +243 -32
  219. package/scripts/test-runner-test.sh +158 -0
  220. package/scripts/test.sh +131 -23
  221. package/scripts/update-path-test.sh +279 -0
  222. package/scripts/ux-ds-audit.sh +1 -1
  223. package/scripts/ux-graph-scan.sh +6 -6
  224. package/scripts/wave-worker.sh +54 -0
  225. package/skills/kit-add/SKILL.md +77 -0
  226. package/skills/kit-autopilot/SKILL.md +123 -12
  227. package/skills/kit-cleanup/SKILL.md +5 -2
  228. package/skills/kit-contribute/SKILL.md +4 -4
  229. package/skills/kit-docs-writer/SKILL.md +16 -6
  230. package/skills/kit-effort-close/SKILL.md +17 -8
  231. package/skills/kit-effort-new/SKILL.md +15 -9
  232. package/skills/kit-effort-pr/SKILL.md +15 -7
  233. package/skills/kit-effort-start/SKILL.md +5 -2
  234. package/skills/kit-gc/SKILL.md +5 -2
  235. package/skills/kit-next/SKILL.md +5 -1
  236. package/skills/kit-onboard/SKILL.md +3 -3
  237. package/skills/kit-page/SKILL.md +93 -0
  238. package/skills/kit-plan-next/SKILL.md +8 -4
  239. package/skills/kit-security-sweep/SKILL.md +1 -3
  240. package/skills/kit-ship/SKILL.md +2 -3
  241. package/skills/kit-status/SKILL.md +35 -4
  242. package/skills/kit-task-new/SKILL.md +3 -3
  243. package/skills/kit-task-pr/SKILL.md +7 -2
  244. package/skills/kit-task-pr-merge/SKILL.md +7 -2
  245. package/skills/kit-task-start/SKILL.md +7 -7
  246. package/skills/kit-task-sync/SKILL.md +19 -3
  247. package/skills/kit-ux-audit/SKILL.md +3 -2
  248. package/templates/agents/auto-dev.md +5 -4
  249. package/templates/agents/backend.md +1 -1
  250. package/templates/agents/designer.md +2 -0
  251. package/templates/agents/devops.md +1 -1
  252. package/templates/agents/editor.md +9 -1
  253. package/templates/agents/frontend.md +1 -1
  254. package/templates/agents/local-delegate.md +1 -1
  255. package/templates/agents/pm.md +5 -5
  256. package/templates/agents/tech-lead.md +1 -1
  257. package/templates/benchmarks/SCHEMA.md +9 -3
  258. package/templates/codex/hooks.json.tmpl +89 -0
  259. package/templates/hooks/guard-base-branch-commit.sh.tmpl +13 -7
  260. package/templates/hooks/guard-raw-git-ops.sh.tmpl +12 -12
  261. package/templates/hooks/mempal_followup.sh.tmpl +2 -2
  262. package/templates/hooks/mempal_session_start.sh.tmpl +1 -1
  263. package/templates/hooks/prepush_gate.sh.tmpl +4 -2
  264. package/templates/hooks/repo-hygiene.sh.tmpl +3 -2
  265. package/templates/instructions.md.tmpl +174 -0
  266. package/templates/kit.config.json.tmpl +3 -3
  267. package/templates/knowledge-INDEX.md.tmpl +1 -1
  268. package/templates/lib/kit-sigil.sh.tmpl +1 -1
  269. package/templates/rules/authoring.md +106 -0
  270. package/templates/rules/branch-naming.md +40 -4
  271. package/templates/rules/code-retrieval.md +63 -0
  272. package/templates/rules/delegation-brief.md +0 -3
  273. package/templates/rules/design-routing.md +3 -3
  274. package/templates/rules/effort-model.md +14 -10
  275. package/templates/rules/gate-and-review.md +94 -0
  276. package/templates/rules/issue-lifecycle.md +88 -0
  277. package/templates/rules/knowledge-base.md +1 -1
  278. package/templates/rules/mempalace.md +1 -1
  279. package/templates/rules/merge-safety.md +108 -0
  280. package/templates/rules/naming-and-ids.md +23 -17
  281. package/templates/rules/plain-language.md +59 -0
  282. package/templates/rules/plan-next.md +9 -11
  283. package/templates/rules/react-annotate.md +6 -0
  284. package/templates/rules/review-pages.md +49 -0
  285. package/templates/rules/risk-tiered-review.md +13 -4
  286. package/templates/rules/shared-checkout.md +106 -0
  287. package/templates/rules/skill-gaps.md +2 -7
  288. package/templates/rules/task-management.md +13 -1
  289. package/templates/skills/brand-voice/SKILL.md +91 -0
  290. package/templates/skills/brand-voice/references/voice-template.md +61 -0
  291. package/templates/skills/campaign/SKILL.md +123 -0
  292. package/templates/skills/campaign/references/plan-template.md +55 -0
  293. package/templates/skills/concrete/SKILL.md +2 -2
  294. package/templates/skills/copywriting/SKILL.md +8 -1
  295. package/templates/skills/feature-build-refine/SKILL.md +2 -4
  296. package/templates/skills/morning-briefing/SKILL.md +2 -2
  297. package/templates/skills/page-design/SKILL.md +145 -0
  298. package/templates/skills/page-design/add.json +5 -0
  299. package/templates/skills/page-design/references/design-file-template.md +40 -0
  300. package/templates/skills/page-design/references/generic-page-patterns.md +43 -0
  301. package/templates/skills/page-design/references/minimum-rules.md +42 -0
  302. package/templates/skills/speckit/SKILL.md +4 -5
  303. package/templates/skills/supabase-patterns/SKILL.md +1 -1
  304. package/commands/kit-add.md +0 -42
  305. package/templates/CLAUDE.md.tmpl +0 -106
@@ -6,17 +6,44 @@
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
+
17
44
  # The GitHub transport switch (#396): rest mode routes the gh calls below through gh-rest.sh.
18
45
  if ! command -v cckit_gh_rest >/dev/null 2>&1; then
19
- _ghr_lib="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>/dev/null && pwd)/gh-rest.sh"
46
+ _ghr_lib="$_EM_LIB_DIR/gh-rest.sh"
20
47
  # shellcheck source=/dev/null
21
48
  [ -f "$_ghr_lib" ] && . "$_ghr_lib"
22
49
  unset _ghr_lib
@@ -37,8 +64,9 @@ record_effort_start() {
37
64
  [[ -n "$num" ]] || return 0
38
65
  d="$(_em_dir)" || return 0
39
66
  mkdir -p "$d" 2>/dev/null || return 0
40
- date -u +%s > "$d/$num.start" 2>/dev/null || true
41
- 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
42
70
  }
43
71
 
44
72
  # estimate_effort <num> — pre-build prediction from estimator.json + the effort's scope (#808).
@@ -141,7 +169,7 @@ _em_token_sum() {
141
169
  [[ -n "$sids" ]] || { echo "0 0 0 0"; return 0; }
142
170
  while IFS= read -r sid; do
143
171
  [[ -n "$sid" ]] || continue
144
- 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)"
145
173
  [[ -n "$tx" ]] || continue
146
174
  # -r (raw) is REQUIRED: without it jq wraps the result string in double-quotes, so $a
147
175
  # becomes `"1015901` and the `$(( ))` math below dies ("bad math expression") — the #829 bug.
@@ -152,7 +180,51 @@ _em_token_sum() {
152
180
  echo "$tin $tout $tcr $tcw"
153
181
  }
154
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
+
155
226
  capture_effort_metrics() {
227
+ local build_src tokens_src null_reason ap ap_tok ap_cost ap_runs first ulog
156
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
157
229
  local toks tin tout tcr tcw tokens_real cost_real cost_source tokens_est cost_est rec
158
230
  local IFS=' ' # force space-split for `set --` below (ambient IFS may carry a NUL)
@@ -160,17 +232,30 @@ capture_effort_metrics() {
160
232
  command -v jq >/dev/null 2>&1 || return 0
161
233
  d="$(_em_dir)" || return 0; mkdir -p "$d" 2>/dev/null || return 0
162
234
 
163
- 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=""
164
238
  if [[ -f "$d/$num.start" ]]; then
165
239
  start="$(cat "$d/$num.start" 2>/dev/null)"
166
- [[ "$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
167
245
  fi
168
246
 
169
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
170
255
  shortstat="$(git diff --shortstat "$base"...HEAD 2>/dev/null)"
171
- files=$(printf '%s' "$shortstat" | grep -oE '[0-9]+ file' | grep -oE '[0-9]+' | head -1)
172
- added=$(printf '%s' "$shortstat" | grep -oE '[0-9]+ insertion' | grep -oE '[0-9]+' | head -1)
173
- 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)
174
259
  commits=$(git rev-list --count "$base"..HEAD 2>/dev/null)
175
260
  files=${files:-0}; added=${added:-0}; removed=${removed:-0}; commits=${commits:-0}
176
261
  churn=$(( added + removed )); diff_auto=$(_em_difficulty "$churn" "$files")
@@ -191,9 +276,19 @@ capture_effort_metrics() {
191
276
  cost_real=$(awk -v i="$tin" -v o="$tout" -v cr="$tcr" -v cw="$tcw" \
192
277
  -v pi="$_EM_PRICE_IN" -v po="$_EM_PRICE_OUT" -v pcr="$_EM_PRICE_CACHE_READ" -v pcw="$_EM_PRICE_CACHE_WRITE" \
193
278
  'BEGIN{printf "%.4f", (i*pi + o*po + cr*pcr + cw*pcw)/1000000}')
194
- cost_source='"list-price"'
279
+ cost_source='"list-price"'; tokens_src='"transcripts"'; null_reason=null
195
280
  else
196
- 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
197
292
  fi
198
293
 
199
294
  # estimate (#808) for reconciliation.
@@ -207,17 +302,19 @@ capture_effort_metrics() {
207
302
  --argjson build "$build_s" --argjson files "$files" --argjson added "$added" \
208
303
  --argjson removed "$removed" --argjson commits "$commits" \
209
304
  --argjson tokens_real "$tokens_real" --argjson cost_real "$cost_real" --argjson cost_source "$cost_source" \
210
- --argjson tokens_est "$tokens_est" --argjson cost_est "$cost_est" --argjson diff "$diff_auto" '
211
- {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,
212
308
  signals:{files:$files, added:$added, removed:$removed, commits:$commits},
213
- 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,
214
311
  tokens_est:$tokens_est, cost_est_usd:$cost_est,
215
312
  difficulty_auto:$diff, score_auto:null, difficulty_judge:null, score_judge:null,
216
313
  synced_to_graph:false}' 2>/dev/null)" || return 0
217
314
  [[ -n "$rec" ]] || return 0
218
315
  printf '%s\n' "$rec" >> "$d/records.jsonl"
219
316
  rm -f "$d/$num.start" "$d/$num.est" 2>/dev/null || true
220
- 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
221
318
  # Self-improving estimator (#812): recalibrate the medians/ratios from the now-larger dataset so the
222
319
  # next effort's pre-build estimate reflects reality (flips tokens_calibrated once ≥5 real-token rows).
223
320
  recalibrate_estimator >/dev/null 2>&1 || true
@@ -266,26 +363,64 @@ _em_judge_goal() {
266
363
  printf '%s\n' "$body" | awk '
267
364
  /^##[ \t]+[Gg]oal[ \t]*$/ { grab=1; next }
268
365
  /^##[ \t]/ { if (grab) exit }
269
- 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
270
367
  }
271
368
 
272
- # _em_judge_call <system> <user> — try CCKIT_MODEL (hosted, OpenAI-compat) then the local model.
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:-}\""
393
+ }
394
+
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).
273
398
  # Echoes "<model_id>\t<reply>" on success; non-zero + nothing on total failure. The model_id is the
274
399
  # hosted model name or the local tag, so the schema's model_id column records who judged.
275
400
  _em_judge_call() {
276
- local system="$1" user="$2" url payload reply model
277
-
278
- # PRIMARY: hosted CCKIT_MODEL (OpenAI-compatible chat completions).
279
- if [[ -n "${CCKIT_MODEL_URL:-}" ]] && command -v curl >/dev/null 2>&1; then
280
- url="${CCKIT_MODEL_URL%/}/chat/completions"
281
- 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)"
282
409
  payload="$(jq -n --arg m "$model" --arg s "$system" --arg p "$user" \
283
410
  '{model:$m, messages:[{role:"system",content:$s},{role:"user",content:$p}], max_tokens:512, temperature:0.1}')" || payload=""
284
411
  if [[ -n "$payload" ]]; then
285
- reply="$(curl -sf -m "${EM_JUDGE_TIMEOUT:-60}" "$url" \
286
- -H 'Content-Type: application/json' \
287
- ${CCKIT_MODEL_TOKEN:+-H "Authorization: Bearer $CCKIT_MODEL_TOKEN"} \
288
- -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
289
424
  if [[ -n "$reply" ]]; then
290
425
  printf '%s\t%s' "$model" "$reply"
291
426
  return 0
@@ -295,8 +430,8 @@ _em_judge_call() {
295
430
 
296
431
  # FALLBACK: local model via kit-local.sh.
297
432
  if ! declare -f kit_local_chat >/dev/null 2>&1; then
298
- local _ldir; _ldir="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
299
- [[ -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"
300
435
  fi
301
436
  if declare -f kit_local_chat >/dev/null 2>&1 && kit_local_alive 2>/dev/null; then
302
437
  reply="$(kit_local_chat "$system" "$user" 512 2>/dev/null)" || reply=""
@@ -351,7 +486,7 @@ ${diff}${truncated_note}"
351
486
  model="${out%%$'\t'*}"; reply="${out#*$'\t'}"
352
487
 
353
488
  # Extract the first {...} object from the reply (models often wrap it in prose/fences).
354
- json="$(printf '%s' "$reply" | tr '\n' ' ' | grep -oE '\{[^{}]*\}' | head -1)"
489
+ json="$(printf '%s' "$reply" | tr '\n' ' ' | grep -oE '\{[^{}]*\}' | sed -n 1p)"
355
490
  if [[ -z "$json" ]]; then
356
491
  echo "[#$num] judge: model reply had no JSON object — leaving *_judge null (model=$model)" >&2
357
492
  return 0
@@ -363,7 +498,7 @@ ${diff}${truncated_note}"
363
498
  echo "[#$num] judge: difficulty_judge='$dj' out of range — leaving *_judge null (model=$model)" >&2
364
499
  return 0
365
500
  fi
366
- 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
367
502
  echo "[#$num] judge: score_judge='$sj' out of range — leaving *_judge null (model=$model)" >&2
368
503
  return 0
369
504
  fi
@@ -417,8 +552,8 @@ ${diff}${truncated_note}"
417
552
  sync_effort_metrics() {
418
553
  command -v jq >/dev/null 2>&1 || return 0
419
554
  if ! declare -f engine_call >/dev/null 2>&1; then
420
- local _adir; _adir="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
421
- [[ -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"
422
557
  fi
423
558
  declare -f engine_enabled >/dev/null 2>&1 || { echo "effort-metrics: engine adapter unavailable — buffered locally" >&2; return 0; }
424
559
  engine_enabled || { echo "effort-metrics: engine off — metrics stay in the local buffer" >&2; return 0; }
@@ -468,7 +603,12 @@ recalibrate_estimator() {
468
603
  median_commits: med([$rows[].commits]),
469
604
  median_build_seconds: med([$rows[]|select(.build!=null)|.build]),
470
605
  tokens_per_changed_line: (if $nrt>=$min then (([$rt[].tok]|add)/([$rt[].lines]|add)) else null end),
471
- 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),
472
612
  recalibrated_at:$stamp }' "$f")"
473
613
  [[ -n "$model" ]] || { echo "effort-metrics: recalibrate produced nothing" >&2; return 1; }
474
614
  if [[ -f "$est" ]]; then