@cbartens/bkit 5.5.1 → 6.1.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 (425) hide show
  1. package/.bkit/README.md +167 -154
  2. package/.bkit/core/.agents/context.md +95 -29
  3. package/.bkit/core/.agents/docs.md +18 -15
  4. package/.bkit/core/.agents/plugins/bkit/hooks/bkit-gateguard.cjs +1 -0
  5. package/.bkit/core/.agents/plugins/bkit/hooks/bkit-guardrails.cjs +6 -5
  6. package/.bkit/core/.agents/plugins/bkit/hooks/bkit-readguard.cjs +1 -0
  7. package/.bkit/core/.agents/plugins/bkit/hooks/bkit-rules.cjs +65 -0
  8. package/.bkit/core/.agents/rules.md +112 -102
  9. package/.bkit/core/.agents/skills.md +63 -15
  10. package/.bkit/core/.bkit/README.md +167 -154
  11. package/.bkit/core/.bkit/authoring.md +747 -0
  12. package/.bkit/core/.bkit/cli.md +407 -0
  13. package/.bkit/core/.bkit/cloud.md +283 -195
  14. package/.bkit/core/.bkit/comparison.md +45 -43
  15. package/.bkit/core/.bkit/install.md +166 -95
  16. package/.bkit/core/.bkit/jsonl.md +339 -196
  17. package/.bkit/core/.bkit/pipeline.md +142 -145
  18. package/.bkit/core/.bkit/prompts.md +295 -264
  19. package/.bkit/core/.bkit/providers.md +460 -277
  20. package/.bkit/core/.bkit/router.md +69 -0
  21. package/.bkit/core/.bkit/templates.md +530 -256
  22. package/.bkit/core/.bkit/trackers.md +116 -65
  23. package/.bkit/core/.bkit/workflows.md +742 -618
  24. package/.bkit/core/.claude/hooks/bkit-gateguard.cjs +2 -2
  25. package/.bkit/core/.claude/hooks/bkit-guardrails.cjs +3 -3
  26. package/.bkit/core/.claude/hooks/bkit-preflight.sh +45 -14
  27. package/.bkit/core/.codex/hooks/bkit-guardrails.cjs +3 -3
  28. package/.bkit/core/.codex/hooks/bkit-rules.cjs +57 -0
  29. package/.bkit/core/.cursor/hooks/bkit-guardrails.cjs +7 -7
  30. package/.bkit/core/.cursor/hooks/bkit-preflight.sh +47 -12
  31. package/.bkit/core/.opencode/plugin/bkit-rules.js +89 -0
  32. package/.bkit/core/AGENTS.md +6 -80
  33. package/.bkit/core/docs/templates/bkit-audit-accessibility.md +128 -130
  34. package/.bkit/core/docs/templates/bkit-audit-adoption.md +104 -94
  35. package/.bkit/core/docs/templates/bkit-audit-agents.md +96 -98
  36. package/.bkit/core/docs/templates/bkit-audit-aio-seo.md +100 -103
  37. package/.bkit/core/docs/templates/bkit-audit-analytics.md +141 -107
  38. package/.bkit/core/docs/templates/bkit-audit-annotations.md +99 -97
  39. package/.bkit/core/docs/templates/bkit-audit-app-performance.md +122 -121
  40. package/.bkit/core/docs/templates/bkit-audit-app-store.md +158 -101
  41. package/.bkit/core/docs/templates/bkit-audit-architecture.md +114 -96
  42. package/.bkit/core/docs/templates/bkit-audit-ci-performance.md +76 -97
  43. package/.bkit/core/docs/templates/bkit-audit-config.md +123 -101
  44. package/.bkit/core/docs/templates/bkit-audit-cost.md +86 -99
  45. package/.bkit/core/docs/templates/bkit-audit-coverage.md +76 -93
  46. package/.bkit/core/docs/templates/bkit-audit-database.md +102 -95
  47. package/.bkit/core/docs/templates/bkit-audit-dead-code.md +165 -108
  48. package/.bkit/core/docs/templates/bkit-audit-deferrals.md +115 -109
  49. package/.bkit/core/docs/templates/bkit-audit-dependencies.md +265 -113
  50. package/.bkit/core/docs/templates/bkit-audit-documentation.md +119 -118
  51. package/.bkit/core/docs/templates/bkit-audit-duplication.md +140 -111
  52. package/.bkit/core/docs/templates/bkit-audit-failures.md +111 -106
  53. package/.bkit/core/docs/templates/bkit-audit-flakes.md +100 -99
  54. package/.bkit/core/docs/templates/bkit-audit-gates.md +107 -103
  55. package/.bkit/core/docs/templates/bkit-audit-issues.md +105 -105
  56. package/.bkit/core/docs/templates/bkit-audit-llm.md +134 -85
  57. package/.bkit/core/docs/templates/bkit-audit-logs.md +124 -125
  58. package/.bkit/core/docs/templates/bkit-audit-models.md +99 -78
  59. package/.bkit/core/docs/templates/bkit-audit-privacy.md +56 -76
  60. package/.bkit/core/docs/templates/bkit-audit-prompts.md +63 -78
  61. package/.bkit/core/docs/templates/bkit-audit-red-team.md +125 -93
  62. package/.bkit/core/docs/templates/bkit-audit-reliability.md +97 -96
  63. package/.bkit/core/docs/templates/bkit-audit-repo.md +170 -98
  64. package/.bkit/core/docs/templates/bkit-audit-security.md +207 -121
  65. package/.bkit/core/docs/templates/bkit-audit-slop.md +78 -95
  66. package/.bkit/core/docs/templates/bkit-audit-testing.md +163 -139
  67. package/.bkit/core/docs/templates/bkit-audit-usability.md +114 -96
  68. package/.bkit/core/docs/templates/bkit-plan.md +158 -281
  69. package/.bkit/core/engine/checks/after/auth.ts +40 -37
  70. package/.bkit/core/engine/checks/after/ci-yml.ts +104 -31
  71. package/.bkit/core/engine/checks/after/github.ts +95 -17
  72. package/.bkit/core/engine/checks/after/index.ts +21 -8
  73. package/.bkit/core/engine/checks/after/suppression.ts +8 -5
  74. package/.bkit/core/engine/checks/after/tools.ts +125 -123
  75. package/.bkit/core/engine/checks/before/context-fetch.ts +227 -33
  76. package/.bkit/core/engine/checks/before/env.ts +3 -3
  77. package/.bkit/core/engine/checks/before/files.ts +5 -4
  78. package/.bkit/core/engine/checks/before/index.ts +25 -1
  79. package/.bkit/core/engine/checks/before/repo.ts +208 -2
  80. package/.bkit/core/engine/checks/status-holes.ts +916 -123
  81. package/.bkit/core/engine/checks/tips/catalogue.ts +82 -20
  82. package/.bkit/core/engine/checks/tips/detect.ts +66 -0
  83. package/.bkit/core/engine/checks/tips/index.ts +52 -11
  84. package/.bkit/core/engine/cli.ts +251 -130
  85. package/.bkit/core/engine/commands/analysis.ts +282 -199
  86. package/.bkit/core/engine/commands/ci/ci.ts +363 -29
  87. package/.bkit/core/engine/commands/ci/pins.ts +172 -0
  88. package/.bkit/core/engine/commands/ci/push-gate.ts +223 -0
  89. package/.bkit/core/engine/commands/comply.ts +77 -103
  90. package/.bkit/core/engine/commands/install/doctor.ts +2 -2
  91. package/.bkit/core/engine/commands/install/install.ts +57 -18
  92. package/.bkit/core/engine/commands/install/uninstall.ts +86 -55
  93. package/.bkit/core/engine/commands/install/upgrade.ts +32 -15
  94. package/.bkit/core/engine/commands/land.ts +139 -18
  95. package/.bkit/core/engine/commands/lib.ts +381 -22
  96. package/.bkit/core/engine/commands/registry.ts +10 -2
  97. package/.bkit/core/engine/commands/setup/setup.ts +8 -7
  98. package/.bkit/core/engine/commands/stats.ts +260 -0
  99. package/.bkit/core/engine/commands/tips.ts +10 -5
  100. package/.bkit/core/engine/commands/workflow/branches.ts +2215 -176
  101. package/.bkit/core/engine/commands/workflow/challenge.ts +748 -0
  102. package/.bkit/core/engine/commands/workflow/completeness.ts +109 -0
  103. package/.bkit/core/engine/commands/workflow/context.ts +1249 -47
  104. package/.bkit/core/engine/commands/workflow/counters.ts +483 -95
  105. package/.bkit/core/engine/commands/workflow/decisions.ts +585 -0
  106. package/.bkit/core/engine/commands/workflow/disk.ts +238 -56
  107. package/.bkit/core/engine/commands/workflow/fids.ts +811 -33
  108. package/.bkit/core/engine/commands/workflow/issues.ts +413 -64
  109. package/.bkit/core/engine/commands/workflow/ledgers.ts +1333 -36
  110. package/.bkit/core/engine/commands/workflow/locks.ts +28 -2
  111. package/.bkit/core/engine/commands/workflow/plan-blocks.ts +691 -0
  112. package/.bkit/core/engine/commands/workflow/plan-versions.ts +21 -0
  113. package/.bkit/core/engine/commands/workflow/plans.ts +257 -0
  114. package/.bkit/core/engine/commands/workflow/prs.ts +178 -33
  115. package/.bkit/core/engine/commands/workflow/repo.ts +66 -8
  116. package/.bkit/core/engine/commands/workflow/roadmap.ts +72 -22
  117. package/.bkit/core/engine/commands/workflow/run-facts.ts +492 -0
  118. package/.bkit/core/engine/commands/workflow/runs.ts +827 -79
  119. package/.bkit/core/engine/commands/workflow/runtime.ts +25 -1
  120. package/.bkit/core/engine/commands/workflow/skills.ts +1 -0
  121. package/.bkit/core/engine/commands/workflow/status.ts +329 -33
  122. package/.bkit/core/engine/commands/workflow/step-table.ts +381 -0
  123. package/.bkit/core/engine/commands/workflow/steps.ts +841 -105
  124. package/.bkit/core/engine/commands/workflow/summary-findings.ts +177 -0
  125. package/.bkit/core/engine/commands/workflow/summary.ts +544 -0
  126. package/.bkit/core/engine/commands/workflow/tasks.ts +409 -0
  127. package/.bkit/core/engine/commands/workflow/test-blocks.ts +254 -0
  128. package/.bkit/core/engine/commands/workflow/tests-changed.ts +201 -0
  129. package/.bkit/core/engine/commands/workflow/tests.ts +2124 -330
  130. package/.bkit/core/engine/commands/workflow/tracker.ts +1195 -0
  131. package/.bkit/core/engine/commands/workflow/workflows.ts +1197 -235
  132. package/.bkit/core/engine/commands/workflow/worktrees.ts +543 -24
  133. package/.bkit/core/engine/install/cloud-env.ts +251 -121
  134. package/.bkit/core/engine/install/context-ci-workflow.ts +102 -0
  135. package/.bkit/core/engine/install/manifest.ts +67 -17
  136. package/.bkit/core/engine/install/modules/antigravity.ts +3 -2
  137. package/.bkit/core/engine/install/modules/cloud.ts +24 -16
  138. package/.bkit/core/engine/install/modules/codex.ts +15 -2
  139. package/.bkit/core/engine/install/modules/core.ts +1196 -45
  140. package/.bkit/core/engine/install/modules/cursor.ts +6 -6
  141. package/.bkit/core/engine/install/modules/opencode.ts +18 -6
  142. package/.bkit/core/engine/install/template-notes.ts +130 -0
  143. package/.bkit/core/engine/install/version-file.ts +9 -4
  144. package/.bkit/core/engine/install/write.ts +5 -0
  145. package/.bkit/core/engine/ledgers/compact.ts +124 -0
  146. package/.bkit/core/engine/ledgers/journal.ts +291 -0
  147. package/.bkit/core/engine/ledgers/migrate.ts +92 -0
  148. package/.bkit/core/engine/ledgers/status.ts +249 -21
  149. package/.bkit/core/engine/ledgers/telemetry.ts +1397 -253
  150. package/.bkit/core/engine/render/hosts.ts +118 -14
  151. package/.bkit/core/engine/render/models.ts +12 -0
  152. package/.bkit/core/engine/shared/bkit-latest.ts +26 -4
  153. package/.bkit/core/engine/shared/branches.ts +4 -1
  154. package/.bkit/core/engine/shared/dep-updater.ts +60 -0
  155. package/.bkit/core/engine/shared/egress.ts +8 -1
  156. package/.bkit/core/engine/shared/json.ts +25 -7
  157. package/.bkit/core/engine/shared/package-manager.ts +9 -0
  158. package/.bkit/core/engine/shared/settings.ts +243 -31
  159. package/.bkit/core/engine/shared/text.ts +361 -0
  160. package/.bkit/core/engine/shared/timeouts.ts +2 -0
  161. package/.bkit/core/engine/tools/cli-doc.ts +111 -0
  162. package/.bkit/core/engine/tools/grammar-density.ts +215 -0
  163. package/.bkit/core/engine/tools/instructions-sources.ts +832 -0
  164. package/.bkit/core/engine/tools/instructions.ts +1802 -0
  165. package/.bkit/core/hooks/antigravity-hook.js +25 -10
  166. package/.bkit/core/hooks/gateguard-engine.js +54 -29
  167. package/.bkit/core/hooks/guardrails-shell.js +4 -5
  168. package/.bkit/core/hooks/guardrails-tiers.js +10 -4
  169. package/.bkit/core/hooks/preflight-linear.sh +9 -9
  170. package/.bkit/core/hooks/preflight-rules.sh +8 -0
  171. package/.bkit/core/hooks/readguard-engine.js +29 -10
  172. package/.bkit/core/hooks/session-rules-advisory.js +61 -0
  173. package/.bkit/core/prompts/bkit-audit-generate.md +202 -94
  174. package/.bkit/core/prompts/bkit-build-challenge-1.md +76 -34
  175. package/.bkit/core/prompts/bkit-build-challenge-2.md +85 -38
  176. package/.bkit/core/prompts/bkit-build-install-skills.md +50 -28
  177. package/.bkit/core/prompts/bkit-build-plan-convert.md +85 -0
  178. package/.bkit/core/prompts/bkit-build-plan-current.md +59 -36
  179. package/.bkit/core/prompts/bkit-build-plan-gaps-remediate.md +101 -36
  180. package/.bkit/core/prompts/bkit-build-plan-tasks.md +149 -0
  181. package/.bkit/core/prompts/bkit-build-plan-tests.md +120 -0
  182. package/.bkit/core/prompts/bkit-build-plan-verify.md +104 -43
  183. package/.bkit/core/prompts/bkit-debug-abort-handover.md +47 -32
  184. package/.bkit/core/prompts/bkit-debug-failure-diagnose.md +109 -57
  185. package/.bkit/core/prompts/bkit-debug-failure-remediate.md +65 -39
  186. package/.bkit/core/prompts/bkit-debug-failure-reproduce.md +53 -31
  187. package/.bkit/core/prompts/bkit-debug-failure-soak.md +65 -34
  188. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-1.md +46 -31
  189. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-2.md +45 -31
  190. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-3.md +43 -30
  191. package/.bkit/core/prompts/bkit-design-generate.md +80 -46
  192. package/.bkit/core/prompts/bkit-design-organise.md +83 -32
  193. package/.bkit/core/prompts/bkit-design-review-1.md +70 -50
  194. package/.bkit/core/prompts/bkit-design-review-2.md +65 -46
  195. package/.bkit/core/prompts/bkit-design-spike.md +64 -40
  196. package/.bkit/core/prompts/bkit-design-update.md +53 -28
  197. package/.bkit/core/prompts/bkit-plan-generate.md +101 -29
  198. package/.bkit/core/prompts/bkit-plan-review-1.md +78 -41
  199. package/.bkit/core/prompts/bkit-plan-review-2.md +78 -35
  200. package/.bkit/core/prompts/bkit-plan-scope-check.md +116 -0
  201. package/.bkit/core/prompts/bkit-plan-update.md +86 -31
  202. package/.bkit/core/prompts/bkit-ship-conflict-resolve.md +94 -50
  203. package/.bkit/core/prompts/bkit-ship-deferred-sweep.md +91 -41
  204. package/.bkit/core/prompts/bkit-ship-update-docs.md +45 -24
  205. package/.bkit/core/prompts/bkit-utility-compress-markdown.md +65 -49
  206. package/.bkit/core/prompts/bkit-utility-update-decisions.md +87 -42
  207. package/.bkit/core/prompts/bkit-utility-update-deferrals.md +81 -37
  208. package/.bkit/core/prompts/bkit-utility-update-documentation.md +43 -25
  209. package/.bkit/core/prompts/bkit-utility-update-failures.md +84 -37
  210. package/.bkit/core/prompts/bkit-utility-update-knowledge.md +88 -42
  211. package/.bkit/core/prompts/bkit-utility-update-roadmap.md +92 -39
  212. package/.bkit/core/prompts/bkit-utility-update-tracker.md +159 -60
  213. package/.bkit/core/workflows/bkit-auto.md +89 -58
  214. package/.bkit/core/workflows/bkit-build.md +967 -517
  215. package/.bkit/core/workflows/bkit-debug.md +555 -387
  216. package/.bkit/core/workflows/bkit-design.md +319 -283
  217. package/.bkit/core/workflows/bkit-plan.md +319 -342
  218. package/.bkit/core/workflows/bkit-ship.md +525 -328
  219. package/.bkit/shared/.agents/plugins/bkit/hooks.json +10 -0
  220. package/.bkit/shared/.bkit/.env.example +2 -2
  221. package/.bkit/shared/.claude/bkit-cloud.md +12 -10
  222. package/.bkit/shared/.claude/settings.json +14 -0
  223. package/.bkit/shared/.codex/bkit-cloud.md +98 -0
  224. package/.bkit/shared/.codex/hooks.json +20 -0
  225. package/.bkit/shared/.cursor/bkit-cloud.md +3 -3
  226. package/.bkit/shared/.cursor/mcp.json +1 -1
  227. package/.bkit/shared/.cursor/rules/bkit-rules.mdc +1 -1
  228. package/.bkit/shared/.gitattributes +1 -1
  229. package/.bkit/shared/.mcp.json +1 -1
  230. package/.bkit/shared/.opencode/opencode.json +12 -1
  231. package/.bkit/shared/CLAUDE.md +6 -2
  232. package/.bkit/shared/GEMINI.md +5 -2
  233. package/.bkit/shared/docs/architecture.md +4 -7
  234. package/.bkit/shared/docs/data-schema.md +4 -7
  235. package/.bkit/shared/docs/deployments.md +241 -168
  236. package/.bkit/shared/docs/design.md +11 -12
  237. package/.bkit/shared/docs/noise.md +8 -7
  238. package/.bkit/shared/docs/observability.md +5 -8
  239. package/.bkit/shared/docs/security.md +35 -17
  240. package/.bkit/shared/docs/setup.md +29 -18
  241. package/.bkit/shared/docs/technology.md +3 -6
  242. package/.bkit/shared/docs/testing.md +289 -174
  243. package/.bkit/shared/gitignore +6 -2
  244. package/.bkit/templates/.claude/hooks/bkit-cloud-bootstrap.sh +15 -16
  245. package/.bkit/templates/.cursor/Dockerfile +12 -8
  246. package/.bkit/templates/.cursor/environment.json +1 -1
  247. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-bug.yml +10 -8
  248. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-change.yml +45 -12
  249. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-idea.yml +3 -3
  250. package/.bkit/templates/.github/renovate.json5 +34 -6
  251. package/.bkit/templates/.github/workflows/bkit-ci.yml +54 -51
  252. package/.bkit/templates/.github/workflows/bkit-cloud.yml +34 -33
  253. package/.bkit/templates/.github/workflows/bkit-health.yml +97 -0
  254. package/.bkit/templates/.github/workflows/bkit-renovate.yml +5 -4
  255. package/.bkit/templates/.github/workflows/bkit-trivy.yml +5 -4
  256. package/.bkit/templates/.husky/pre-commit +2 -2
  257. package/.bkit/templates/.husky/pre-push +96 -36
  258. package/.bkit/templates/.prettierignore +6 -0
  259. package/.bkit/templates/.vale.ini +27 -27
  260. package/.bkit/templates/context/bkit-cloudrun.md +23 -15
  261. package/.bkit/templates/context/bkit-expo.md +70 -52
  262. package/.bkit/templates/context/bkit-github.md +93 -56
  263. package/.bkit/templates/context/bkit-inngest.md +21 -15
  264. package/.bkit/templates/context/bkit-maestro.md +22 -16
  265. package/.bkit/templates/context/bkit-playwright.md +23 -17
  266. package/.bkit/templates/context/bkit-resend.md +12 -10
  267. package/.bkit/templates/context/bkit-sentry.md +30 -20
  268. package/.bkit/templates/context/bkit-serena.md +8 -7
  269. package/.bkit/templates/context/bkit-stripe.md +14 -13
  270. package/.bkit/templates/context/bkit-supabase.md +16 -13
  271. package/.bkit/templates/context/bkit-tavily.md +14 -11
  272. package/.bkit/templates/context/bkit-vercel.md +27 -18
  273. package/.bkit/templates/docs/capabilities.md +15 -14
  274. package/.bkit/templates/scripts/ci/{check-accepted-divergences.mjs → bkit-check-accepted-divergences.mjs} +6 -3
  275. package/.bkit/templates/scripts/ci/{check-accepted-divergences.test.mjs → bkit-check-accepted-divergences.test.mjs} +3 -3
  276. package/.bkit/templates/scripts/ci/{check-capstone.mjs → bkit-check-capstone.mjs} +2 -2
  277. package/.bkit/templates/scripts/ci/{check-capstone.test.mjs → bkit-check-capstone.test.mjs} +4 -4
  278. package/.bkit/templates/scripts/ci/{check-dependency-audit.mjs → bkit-check-dependency-audit.mjs} +103 -18
  279. package/.bkit/templates/scripts/ci/{check-dependency-audit.test.mjs → bkit-check-dependency-audit.test.mjs} +31 -3
  280. package/.bkit/templates/scripts/ci/bkit-machine-lock.sh +387 -0
  281. package/.bkit/templates/scripts/ci/bkit-test-awake.mjs +75 -0
  282. package/.bkit/templates/scripts/ci/bkit-test-awake.test.mjs +160 -0
  283. package/.bkit/templates/scripts/ci/lanes/{resolve-push-base.mjs → bkit-resolve-push-base.mjs} +3 -3
  284. package/.bkit/templates/scripts/ci/lanes/{resolve-push-base.test.mjs → bkit-resolve-push-base.test.mjs} +4 -4
  285. package/.bkit/templates/scripts/cleanup/bkit-disk-prune.mjs +10 -2
  286. package/.bkit/templates/scripts/graphify/bkit-graph-drift.py +1 -1
  287. package/.bkit/templates/scripts/graphify/bkit-graph-eager.py +3 -3
  288. package/.bkit/templates/scripts/graphify/bkit-graph-embed.py +63 -0
  289. package/.bkit/templates/scripts/graphify/bkit-graph-imports.py +2 -2
  290. package/.bkit/templates/scripts/graphify/bkit-graph-pre-push.sh +69 -18
  291. package/.bkit/templates/scripts/graphify/bkit-graph-repair.py +3 -3
  292. package/.bkit/templates/scripts/graphify/bkit-graph-surface.py +1 -1
  293. package/.bkit/templates/scripts/graphify/bkit-graphify-graphml.py +1 -1
  294. package/.bkit/templates/scripts/graphify/bkit-graphify-rollup.py +197 -126
  295. package/.bkit/templates/scripts/graphify/bkit-graphify.sh +64 -41
  296. package/.bkit/templates/scripts/slop/bkit-slop-type-suppression.mjs +23 -7
  297. package/.bkit/templates/slop-scan.config.json +9 -2
  298. package/CHANGELOG.md +231 -0
  299. package/README.md +278 -89
  300. package/dist/checks/after/auth.js +36 -33
  301. package/dist/checks/after/ci-yml.js +91 -30
  302. package/dist/checks/after/github.js +91 -17
  303. package/dist/checks/after/index.js +22 -11
  304. package/dist/checks/after/suppression.js +8 -5
  305. package/dist/checks/after/tools.js +111 -114
  306. package/dist/checks/before/context-fetch.js +218 -33
  307. package/dist/checks/before/env.js +3 -3
  308. package/dist/checks/before/files.js +5 -3
  309. package/dist/checks/before/index.js +20 -3
  310. package/dist/checks/before/repo.js +197 -2
  311. package/dist/checks/status-holes.js +739 -114
  312. package/dist/checks/tips/catalogue.js +70 -19
  313. package/dist/checks/tips/detect.js +62 -0
  314. package/dist/checks/tips/index.js +51 -9
  315. package/dist/cli.js +234 -124
  316. package/dist/commands/analysis.js +174 -141
  317. package/dist/commands/ci/ci.js +302 -25
  318. package/dist/commands/ci/pins.js +142 -0
  319. package/dist/commands/ci/push-gate.js +194 -0
  320. package/dist/commands/comply.js +54 -91
  321. package/dist/commands/install/doctor.js +2 -2
  322. package/dist/commands/install/install.js +50 -19
  323. package/dist/commands/install/uninstall.js +82 -55
  324. package/dist/commands/install/upgrade.js +21 -11
  325. package/dist/commands/land.js +137 -17
  326. package/dist/commands/lib.js +295 -21
  327. package/dist/commands/registry.js +10 -2
  328. package/dist/commands/setup/setup.js +8 -7
  329. package/dist/commands/stats.js +175 -0
  330. package/dist/commands/tips.js +7 -3
  331. package/dist/commands/workflow/branches.js +1873 -152
  332. package/dist/commands/workflow/challenge.js +609 -0
  333. package/dist/commands/workflow/completeness.js +97 -0
  334. package/dist/commands/workflow/context.js +1045 -46
  335. package/dist/commands/workflow/counters.js +385 -86
  336. package/dist/commands/workflow/decisions.js +484 -0
  337. package/dist/commands/workflow/disk.js +214 -56
  338. package/dist/commands/workflow/fids.js +706 -33
  339. package/dist/commands/workflow/issues.js +349 -59
  340. package/dist/commands/workflow/ledgers.js +1123 -37
  341. package/dist/commands/workflow/locks.js +26 -2
  342. package/dist/commands/workflow/plan-blocks.js +501 -0
  343. package/dist/commands/workflow/plan-versions.js +22 -0
  344. package/dist/commands/workflow/plans.js +205 -0
  345. package/dist/commands/workflow/prs.js +154 -35
  346. package/dist/commands/workflow/repo.js +58 -8
  347. package/dist/commands/workflow/roadmap.js +58 -23
  348. package/dist/commands/workflow/run-facts.js +421 -0
  349. package/dist/commands/workflow/runs.js +670 -72
  350. package/dist/commands/workflow/runtime.js +15 -1
  351. package/dist/commands/workflow/skills.js +1 -0
  352. package/dist/commands/workflow/status.js +282 -34
  353. package/dist/commands/workflow/step-table.js +308 -0
  354. package/dist/commands/workflow/steps.js +703 -96
  355. package/dist/commands/workflow/summary-findings.js +117 -0
  356. package/dist/commands/workflow/summary.js +388 -0
  357. package/dist/commands/workflow/tasks.js +378 -0
  358. package/dist/commands/workflow/test-blocks.js +231 -0
  359. package/dist/commands/workflow/tests-changed.js +172 -0
  360. package/dist/commands/workflow/tests.js +1695 -305
  361. package/dist/commands/workflow/tracker.js +983 -0
  362. package/dist/commands/workflow/workflows.js +1014 -222
  363. package/dist/commands/workflow/worktrees.js +432 -26
  364. package/dist/install/cloud-env.js +233 -99
  365. package/dist/install/context-ci-workflow.js +85 -0
  366. package/dist/install/manifest.js +64 -17
  367. package/dist/install/modules/antigravity.js +3 -2
  368. package/dist/install/modules/cloud.js +25 -16
  369. package/dist/install/modules/codex.js +12 -2
  370. package/dist/install/modules/core.js +1090 -48
  371. package/dist/install/modules/cursor.js +6 -6
  372. package/dist/install/modules/opencode.js +13 -7
  373. package/dist/install/template-notes.js +116 -0
  374. package/dist/install/version-file.js +9 -4
  375. package/dist/ledgers/compact.js +87 -0
  376. package/dist/ledgers/journal.js +233 -0
  377. package/dist/ledgers/migrate.js +81 -0
  378. package/dist/ledgers/status.js +176 -13
  379. package/dist/ledgers/telemetry.js +1010 -125
  380. package/dist/render/hosts.js +104 -14
  381. package/dist/render/models.js +12 -0
  382. package/dist/shared/bkit-latest.js +26 -4
  383. package/dist/shared/branches.js +3 -1
  384. package/dist/shared/dep-updater.js +49 -0
  385. package/dist/shared/egress.js +8 -1
  386. package/dist/shared/json.js +20 -7
  387. package/dist/shared/package-manager.js +6 -0
  388. package/dist/shared/settings.js +181 -24
  389. package/dist/shared/text.js +333 -0
  390. package/dist/shared/timeouts.js +2 -0
  391. package/dist/tools/cli-doc.js +97 -0
  392. package/dist/tools/grammar-density.js +183 -0
  393. package/dist/tools/instructions-sources.js +720 -0
  394. package/dist/tools/instructions.js +1623 -0
  395. package/package.json +156 -51
  396. package/.bkit/core/.bkit/lite.md +0 -73
  397. package/.bkit/core/engine/commands/harvest.ts +0 -459
  398. package/.bkit/core/engine/commands/workflow/workstreams.ts +0 -166
  399. package/.bkit/core/engine/tools/wording.ts +0 -748
  400. package/.bkit/core/prompts/bkit-build-challenge-reconcile.md +0 -69
  401. package/.bkit/core/prompts/bkit-build-challenge-remediate.md +0 -71
  402. package/.bkit/core/prompts/bkit-build-challenge-tests.md +0 -74
  403. package/.bkit/core/prompts/bkit-build-plan-execute.md +0 -76
  404. package/.bkit/core/prompts/bkit-build-summary.md +0 -94
  405. package/.bkit/core/prompts/bkit-debug-summary.md +0 -96
  406. package/.bkit/core/prompts/bkit-design-summary.md +0 -107
  407. package/.bkit/core/prompts/bkit-plan-complexity-check.md +0 -67
  408. package/.bkit/core/prompts/bkit-plan-complexity-split.md +0 -88
  409. package/.bkit/core/prompts/bkit-plan-review-3.md +0 -93
  410. package/.bkit/core/prompts/bkit-plan-scope-check-1.md +0 -85
  411. package/.bkit/core/prompts/bkit-plan-scope-check-2.md +0 -85
  412. package/.bkit/core/prompts/bkit-plan-summary.md +0 -107
  413. package/.bkit/core/prompts/bkit-ship-summary.md +0 -78
  414. package/.bkit/templates/scripts/ci/bkit-local-stack-lock.sh +0 -194
  415. package/dist/commands/harvest.js +0 -353
  416. package/dist/commands/workflow/workstreams.js +0 -143
  417. package/dist/tools/wording.js +0 -680
  418. /package/.bkit/templates/.vale/styles/{House → Bkit}/Filler.yml +0 -0
  419. /package/.bkit/templates/.vale/styles/{House → Bkit}/Hedging.yml +0 -0
  420. /package/.bkit/templates/.vale/styles/{House → Bkit}/LLMSlop.yml +0 -0
  421. /package/.bkit/templates/.vale/styles/{House → Bkit}/Verbose.yml +0 -0
  422. /package/.bkit/templates/scripts/ci/{accepted-advisories.json → bkit-accepted-advisories.json} +0 -0
  423. /package/.bkit/templates/scripts/ci/{accepted-divergences.json → bkit-accepted-divergences.json} +0 -0
  424. /package/.bkit/templates/scripts/launchd/{launchd.env.example → bkit-launchd.env.example} +0 -0
  425. /package/.bkit/templates/scripts/launchd/{ai.bkit.launchd.plist → bkit-launchd.plist} +0 -0
@@ -13,8 +13,8 @@
13
13
  // (b) `validate` REJECTS unknown `kind` before write. No reject path in status.ts.
14
14
  // (c) ms `ts` via now*1000|floor, NOT ISO `at`.
15
15
  //
16
- // Version provenance: every row records the emitting `bkitVersion`. Readers ingest
17
- // ALL well-formed rows (no version gating) and report row counts per `bkitVersion`
16
+ // Version provenance: every row records the emitting `bkit.version`. Readers ingest
17
+ // ALL well-formed rows (no version gating) and report row counts per `bkit.version`
18
18
  // so an unexpected version surfaces for triage.
19
19
  import { spawnSync } from "node:child_process";
20
20
  import * as fs from "node:fs";
@@ -99,6 +99,16 @@ export function resolveRepoSlug(cwd) {
99
99
  return "";
100
100
  return parseRemoteUrlToSlug(res.stdout);
101
101
  }
102
+ // ---------------------------------------------------------------------------
103
+ // Shared envelope (A2-mandatory fields on EVERY row, every kind)
104
+ // ---------------------------------------------------------------------------
105
+ /**
106
+ * Row format version. Rows written without `schema` are read through {@link normalize}.
107
+ * A schema-2 row differs only in its outcome `plan` block ({@link StoredPlanBlock}).
108
+ */
109
+ export const SCHEMA = 3;
110
+ /** The schema {@link normalize} stamps on a row written without one: the shape it builds. */
111
+ const NORMALIZED_SCHEMA = 2;
102
112
  export const VALID_KINDS = new Set([
103
113
  "compliance",
104
114
  "outcome",
@@ -109,6 +119,8 @@ export const VALID_KINDS = new Set([
109
119
  "knowledge",
110
120
  "decision",
111
121
  "feature",
122
+ "chat",
123
+ "run",
112
124
  ]);
113
125
  export const VALID_SEVERITIES = new Set(["CRITICAL", "HIGH", "MEDIUM", "LOW"]);
114
126
  /**
@@ -121,6 +133,17 @@ export const SEVERITY_KINDS = new Set(["friction", "deferral", "failure"]);
121
133
  export const TRIAGE_STATUSES = new Set(["OPEN", "TRIAGED", "CLOSED"]);
122
134
  /** The `ACTIVE` / `SUPERSEDED` lifecycle shared by `decision` and `knowledge`. */
123
135
  export const DECISION_STATUSES = new Set(["ACTIVE", "SUPERSEDED"]);
136
+ /**
137
+ * The kinds whose rows carry `firstTs` + `updates` (`.bkit/jsonl.md` § Compaction):
138
+ * the keyed kinds of the ledgers `bkit ledgers compact` rewrites to their fold winners.
139
+ */
140
+ export const AGED_KINDS = new Set([
141
+ "deferral",
142
+ "failure",
143
+ "knowledge",
144
+ "decision",
145
+ "feature",
146
+ ]);
124
147
  export const FEATURE_STATUSES = new Set([
125
148
  "DESIGNED",
126
149
  "PLANNED",
@@ -134,6 +157,7 @@ export const DEFERRAL_REASONS = new Set([
134
157
  "hard-blocked",
135
158
  "needs-decision",
136
159
  "own-cycle",
160
+ "low-severity",
137
161
  "unjustified",
138
162
  ]);
139
163
  /**
@@ -151,7 +175,7 @@ export const UNJUSTIFIED_REASON = "unjustified";
151
175
  * § Ledgers & Writers). EXHAUSTIVE: a subagent absent from this set does not
152
176
  * write the kind at all.
153
177
  *
154
- * `@bkit-build-plan-execute` and `@bkit-build-plan-gaps-remediate` are not in it: a
178
+ * `@bkit-build-plan-tasks` and `@bkit-build-plan-gaps-remediate` are not in it: a
155
179
  * blocked plan task's record is its `BLOCKED` log line, which the next verify pass reads.
156
180
  *
157
181
  * Orchestrator-written rows carry no `subagent` and are outside this check.
@@ -160,38 +184,698 @@ export const DEFERRAL_WRITERS = new Set([
160
184
  "bkit-design-generate",
161
185
  "bkit-plan-generate",
162
186
  "bkit-plan-update",
163
- "bkit-build-challenge-remediate",
164
187
  "bkit-debug-abort-handover",
165
188
  "bkit-ship-deferred-sweep",
166
189
  "bkit-utility-update-deferrals",
167
190
  ]);
191
+ /** A writer list that admits every name (`.bkit/jsonl.md` § Ledgers & Writers: "any agent"). */
192
+ export const ANY_WRITER = "any";
193
+ /**
194
+ * The ledgers `bkit ledgers append` lands rows in, and the kinds each takes.
195
+ * `feature` rows go through `bkit roadmap feature` (it re-renders the board);
196
+ * `chat` / `run` rows are written by bkit's harvest script, to `harvest/stats.jsonl`.
197
+ */
198
+ export const APPEND_LEDGERS = {
199
+ decisions: { file: "docs/decisions.jsonl", kinds: ["decision"] },
200
+ deferrals: { file: "docs/deferrals.jsonl", kinds: ["deferral"] },
201
+ failures: { file: "docs/failures.jsonl", kinds: ["failure"] },
202
+ knowledge: { file: "docs/knowledge.jsonl", kinds: ["knowledge"] },
203
+ friction: { file: ".bkit/friction.jsonl", kinds: ["friction"] },
204
+ stats: { file: ".bkit/stats.jsonl", kinds: ["compliance", "counter", "outcome"] },
205
+ };
206
+ /** The workflows whose close writes `.bkit/stats.jsonl` rows (`bkit workflows close`, `bkit comply`). */
207
+ const CLOSING_WORKFLOWS = new Set(["design", "plan", "build", "debug", "ship"]);
208
+ /**
209
+ * The stats writers: the closing workflows, plus `utility`, a direct chat outside any
210
+ * workflow (`by.step` `manual`), the owner deciding in it.
211
+ */
212
+ const STATS_WRITERS = new Set([...CLOSING_WORKFLOWS, "utility"]);
213
+ /**
214
+ * `.bkit/jsonl.md` § Ledgers & Writers as code, for the kinds in
215
+ * {@link APPEND_LEDGERS}. EXHAUSTIVE: a name absent from a set does not write the
216
+ * kind. `telemetry.test.ts` holds this to the doc table.
217
+ */
218
+ export const KIND_WRITERS = {
219
+ decision: {
220
+ subagents: new Set([
221
+ "bkit-debug-failure-remediate",
222
+ "bkit-plan-generate",
223
+ "bkit-plan-update",
224
+ "bkit-utility-update-decisions",
225
+ ]),
226
+ // Build: Decision Pause accept; plan: an open question answered after close; design: an
227
+ // open question answered by the owner at close (the human decides, the orchestrator
228
+ // records); ship: an assumed accept the owner confirms (`bkit decisions apply --by=owner`);
229
+ // utility: a direct chat outside any workflow (`by.step` `manual`), the owner deciding in it.
230
+ // All ONLY after explicit human approval.
231
+ orchestrators: new Set(["design", "build", "plan", "ship", "utility"]),
232
+ },
233
+ // Build: `bkit challenge merge` writes each LOW challenge finding (`low-severity`). Ship:
234
+ // `bkit decisions pending --defer` writes each assumed accept nobody confirmed (`needs-decision`).
235
+ // Utility: a direct chat outside any workflow (`by.step` `manual`), the owner deciding in it.
236
+ deferral: { subagents: DEFERRAL_WRITERS, orchestrators: new Set(["build", "ship", "utility"]) },
237
+ failure: { subagents: ANY_WRITER, orchestrators: ANY_WRITER },
238
+ knowledge: {
239
+ subagents: new Set([
240
+ "bkit-build-plan-tests",
241
+ "bkit-build-plan-tasks",
242
+ "bkit-build-plan-gaps-remediate",
243
+ "bkit-debug-failure-remediate",
244
+ "bkit-utility-update-knowledge",
245
+ ]),
246
+ // Utility: a direct chat outside any workflow (`by.step` `manual`), the owner deciding in it.
247
+ orchestrators: new Set(["utility"]),
248
+ },
249
+ friction: {
250
+ subagents: new Set(),
251
+ // Utility: a direct chat outside any workflow (`by.step` `manual`) that meets friction.
252
+ orchestrators: new Set(["design", "plan", "build", "debug", "ship", "auto", "utility"]),
253
+ },
254
+ compliance: { subagents: new Set(), orchestrators: STATS_WRITERS },
255
+ counter: { subagents: new Set(), orchestrators: STATS_WRITERS },
256
+ outcome: { subagents: new Set(), orchestrators: STATS_WRITERS },
257
+ };
258
+ const writerList = (w) => w === ANY_WRITER ? "any" : w.size === 0 ? "none" : [...w].join(", ");
259
+ /** A row's `by` block, or an empty one when it has none. */
260
+ const byOf = (row) => row["by"] !== null && typeof row["by"] === "object" ? row["by"] : {};
261
+ /**
262
+ * Why `row` may not be written by its author, or undefined when it may. The author
263
+ * is `by.subagent` when set (with its `by.model`), else the orchestrator in
264
+ * `by.workflow` (`.bkit/jsonl.md` § Who Wrote It).
265
+ */
266
+ export function writerProblem(row) {
267
+ const kind = row["kind"];
268
+ const writers = KIND_WRITERS[kind];
269
+ if (writers === undefined)
270
+ return `${String(kind)} rows have no append writers`;
271
+ const by = byOf(row);
272
+ const subagent = by["subagent"];
273
+ if (subagent !== undefined) {
274
+ if (typeof subagent !== "string" || subagent === "") {
275
+ return "by.subagent must be a non-empty string";
276
+ }
277
+ if (typeof by["model"] !== "string" || by["model"] === "") {
278
+ return `subagent ${subagent} row missing by.model (its prompt's frontmatter model:)`;
279
+ }
280
+ if (writers.subagents === ANY_WRITER || writers.subagents.has(subagent))
281
+ return undefined;
282
+ return `subagent ${subagent} does not write ${kind} rows (subagents: ${writerList(writers.subagents)})`;
283
+ }
284
+ if (by["model"] !== undefined)
285
+ return "by.model is set only on subagent rows";
286
+ if (writers.orchestrators === ANY_WRITER)
287
+ return undefined;
288
+ const workflow = by["workflow"];
289
+ if (typeof workflow === "string" && writers.orchestrators.has(workflow))
290
+ return undefined;
291
+ return `the ${typeof workflow === "string" ? workflow : "(no workflow)"} orchestrator does not write ${kind} rows (orchestrators: ${writerList(writers.orchestrators)})`;
292
+ }
293
+ /**
294
+ * A `ts` minted now: `Date.now()`, or `after + 1` when the clock has not passed
295
+ * `after`, moved 1 ms off a whole second. The live-ledger gate fails whole-second
296
+ * values as hand-typed (`audits/repo/ledger-integrity.test.ts`), and a real mint
297
+ * must never trip it.
298
+ */
299
+ export function mintTs(after) {
300
+ let ts = Date.now();
301
+ if (after !== undefined && ts <= after)
302
+ ts = after + 1;
303
+ return ts % 1000 === 0 ? ts + 1 : ts;
304
+ }
305
+ /**
306
+ * The context one subagent may fill before it hands over (the plan template's "about 160k
307
+ * tokens" per build group). Rows store peaks only; readers judge them against this.
308
+ */
309
+ export const AGENT_CONTEXT_CAP = 160_000;
310
+ export const OUTCOME_ENDINGS = new Set(["done", "close-failed", "held"]);
311
+ export const RUN_ENDINGS = new Set([
312
+ "done",
313
+ "close-failed",
314
+ "failed",
315
+ "decision-pause",
316
+ "stopped",
317
+ "held",
318
+ ]);
168
319
  // ---------------------------------------------------------------------------
169
320
  // Identity — per-kind composite tuple for dedup (NOT bare ts)
170
321
  // Mirrors ledgers/status.ts's tuple dedupe approach.
171
322
  // ---------------------------------------------------------------------------
172
323
  export function identity(event) {
324
+ const workflow = event.by?.workflow;
173
325
  switch (event.kind) {
174
326
  case "compliance":
175
- return `compliance ${event.repo} ${event.workflow} ${event.parentSha} ${event.rule}`;
327
+ return `compliance ${event.repo} ${workflow} ${event.run?.parentSha} ${event.rule?.id}`;
176
328
  case "outcome": {
177
- // One run row per workflow run: keyed by (repo, workflow, parentSha).
178
- // A ship row with no parentSha falls back to prNumber (then ts), so it stays
329
+ // One run row per workflow run: keyed by (repo, workflow, run.parentSha).
330
+ // A ship row with no parentSha falls back to run.pr (then ts), so it stays
179
331
  // uniquely keyed per PR.
180
- const anchor = event.parentSha ?? (event.prNumber !== undefined ? `pr${event.prNumber}` : `ts${event.ts}`);
181
- return `outcome ${event.repo} ${event.workflow} ${anchor}`;
332
+ const anchor = event.run?.parentSha ??
333
+ (event.run?.pr !== undefined ? `pr${event.run.pr}` : `ts${event.ts}`);
334
+ return `outcome ${event.repo} ${workflow} ${anchor}`;
182
335
  }
183
336
  case "counter":
184
- return `counter ${event.repo} ${event.workflow} ${event.parentSha} ${event.counterName}`;
337
+ return `counter ${event.repo} ${workflow} ${event.run?.parentSha} ${event.counter?.name}`;
185
338
  case "friction":
186
339
  case "deferral":
187
340
  case "failure":
188
341
  case "knowledge":
189
342
  case "decision":
190
343
  case "feature":
191
- // instinct-loop kinds (+ feature): identity = kind + key
344
+ case "chat":
345
+ case "run":
346
+ // keyed kinds: identity = kind + key
192
347
  return `${event.kind} ${event.repo} ${event.key ?? event.ts}`;
193
348
  }
194
349
  }
350
+ const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
351
+ /** `o` without its undefined fields, so an absent value stays absent. */
352
+ const defined = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined));
353
+ const tenthOf = (n) => Math.round(n * 10) / 10;
354
+ /** Fields of a row written without `schema` that the current shape moves or drops, per kind. */
355
+ const MOVED = {
356
+ compliance: ["rule", "result", "evidence", "phase"],
357
+ counter: ["counterName", "counterValue", "counterSource", "phase"],
358
+ outcome: [
359
+ "phase",
360
+ "prNumber",
361
+ "linesAdded",
362
+ "linesDeleted",
363
+ "linesChanged",
364
+ "filesChanged",
365
+ "taskCount",
366
+ "durationSeconds",
367
+ "ciTrySuccess",
368
+ "correctiveCommits",
369
+ "reviewIterations",
370
+ "deferrals",
371
+ "failures",
372
+ "frictionAdded",
373
+ "deferralsAdded",
374
+ "failuresAdded",
375
+ "planSource",
376
+ "coverage",
377
+ "redTests",
378
+ ],
379
+ };
380
+ const TOP_MOVED = ["schema", "bkitVersion", "workflow", "step", "subagent", "model", "parentSha"];
381
+ /**
382
+ * A row in a `schema` shape. A row that carries `schema` is returned as it is; a
383
+ * row written without one is stamped `schema: 2` and has its top section (`bkitVersion` -> `bkit.version`,
384
+ * `workflow` / `step` / `subagent` / `model` -> `by.*`) and the stats kinds' run
385
+ * fields moved into their blocks as far as the row holds them. Fields with no
386
+ * place in the schema-2 shape are dropped, and any kind's `parentSha` moves to
387
+ * `run.parentSha`. `bkit install` / `bkit upgrade` rewrite old rows through it
388
+ * (`./migrate.ts`); readers still call it, as a union merge can bring an old line back.
389
+ */
390
+ export function normalize(raw) {
391
+ if (!isObj(raw))
392
+ return {};
393
+ if (raw["schema"] !== undefined)
394
+ return raw;
395
+ const kind = String(raw["kind"]);
396
+ const moved = new Set([...TOP_MOVED, ...(MOVED[kind] ?? [])]);
397
+ const rest = Object.fromEntries(Object.entries(raw).filter(([k]) => !moved.has(k)));
398
+ const { ts, kind: _kind, key, repo, references, ...payload } = rest;
399
+ const out = defined({
400
+ schema: NORMALIZED_SCHEMA,
401
+ ts,
402
+ kind: raw["kind"],
403
+ key,
404
+ repo,
405
+ // "none": the harvest script found no bkit version for the row.
406
+ bkit: {
407
+ version: raw["bkitVersion"] === undefined || raw["bkitVersion"] === "none"
408
+ ? null
409
+ : raw["bkitVersion"],
410
+ },
411
+ // An old row's null or empty `workflow` / `subagent` / `model` meant "none": left out.
412
+ by: Object.fromEntries(["workflow", "step", "subagent", "model"]
413
+ .map((f) => [f, raw[f]])
414
+ .filter(([, v]) => v !== undefined && v !== null && v !== "")),
415
+ references,
416
+ });
417
+ const num = (f) => typeof raw[f] === "number" ? raw[f] : undefined;
418
+ switch (kind) {
419
+ case "compliance":
420
+ out["run"] = { parentSha: raw["parentSha"] };
421
+ out["rule"] = defined({ id: raw["rule"], verdict: raw["result"], evidence: raw["evidence"] });
422
+ break;
423
+ case "counter":
424
+ out["run"] = { parentSha: raw["parentSha"] };
425
+ out["counter"] = {
426
+ name: raw["counterName"],
427
+ value: raw["counterValue"],
428
+ source: raw["counterSource"],
429
+ };
430
+ break;
431
+ case "outcome": {
432
+ out["run"] = defined({ parentSha: raw["parentSha"], pr: raw["prNumber"] });
433
+ const seconds = num("durationSeconds");
434
+ if (seconds !== undefined)
435
+ out["time"] = { totalMinutes: tenthOf(seconds / 60) };
436
+ const added = num("linesAdded");
437
+ const deleted = num("linesDeleted");
438
+ if (added !== undefined && deleted !== undefined) {
439
+ out["lines"] = {
440
+ total: { added, deleted, changed: added + deleted, files: num("filesChanged") ?? 0 },
441
+ };
442
+ }
443
+ out["ledgers"] = {
444
+ deferrals: defined({ repo: num("deferrals"), added: num("deferralsAdded") }),
445
+ failures: defined({ repo: num("failures"), added: num("failuresAdded") }),
446
+ friction: defined({ added: num("frictionAdded") }),
447
+ };
448
+ if (raw["planSource"] === "brief")
449
+ out["plan"] = { source: "brief" };
450
+ if (isObj(raw["coverage"]))
451
+ out["ship"] = { coverage: raw["coverage"] };
452
+ if (num("redTests") !== undefined)
453
+ out["build"] = { redTests: num("redTests") };
454
+ break;
455
+ }
456
+ case "chat":
457
+ return { ...out, ...oldChat(raw) };
458
+ case "run":
459
+ return {
460
+ ...out,
461
+ by: defined({ workflow: raw["workflow"], step: raw["step"] ?? "scan" }),
462
+ ...oldRun(raw),
463
+ };
464
+ }
465
+ const run = out["run"] ?? (raw["parentSha"] !== undefined ? { parentSha: raw["parentSha"] } : undefined);
466
+ return { ...out, ...payload, ...defined({ run }) };
467
+ }
468
+ // The harvest script's own kinds, mapped in full: every schema-1 field has a
469
+ // place in the current shape or is dropped. `scripts/harvest/bkit-harvest.mjs`
470
+ // `normalize` maps them the same way.
471
+ const objOf = (v) => (isObj(v) ? v : {});
472
+ const numOr = (v, or = 0) => typeof v === "number" ? v : or;
473
+ const arrOf = (v) => (Array.isArray(v) ? v : []);
474
+ const TOKENS = ["input", "cacheWrite", "cacheRead", "output", "outputEst", "read"];
475
+ const oldTokens = (t) => Object.fromEntries(TOKENS.map((k) => [k, numOr(objOf(t)[k])]));
476
+ const oldMix = (m) => {
477
+ if (!isObj(m))
478
+ return undefined;
479
+ return {
480
+ read: numOr(m["read"]),
481
+ edit: numOr(m["edit"]),
482
+ grep: numOr(m["grep"]),
483
+ shellRead: numOr(m["sedCat"]),
484
+ shellList: numOr(m["findLs"]),
485
+ tests: numOr(m["tests"]),
486
+ serena: numOr(m["serena"]),
487
+ };
488
+ };
489
+ const sumNums = (xs) => xs.reduce((a, x) => a + (typeof x === "number" ? x : 0), 0);
490
+ /** Shell tools by host: the tool a schema-1 chat row's per-chat shell categories move under. */
491
+ const OLD_SHELL_TOOLS = [
492
+ "Bash",
493
+ "bash",
494
+ "Shell",
495
+ "shell",
496
+ "shell_command",
497
+ "local_shell",
498
+ "exec_command",
499
+ "run_command",
500
+ "run_terminal_cmd",
501
+ ];
502
+ function oldChat(r) {
503
+ const byAgent = {};
504
+ for (const [type, av] of Object.entries(objOf(r["agents"]))) {
505
+ const a = objOf(av);
506
+ const peaks = arrOf(a["ctxPeaks"]).filter((p) => typeof p === "number");
507
+ const n = numOr(a["n"], peaks.length);
508
+ byAgent[type] = defined({
509
+ n,
510
+ errors: numOr(a["errors"]),
511
+ // Per-instance turns were stored only as a sum: known when the type ran once.
512
+ instances: peaks.map((p) => ({
513
+ turns: n === 1 ? numOr(a["turns"], null) : null,
514
+ contextPeak: p,
515
+ })),
516
+ usage: {
517
+ turns: numOr(a["turns"]),
518
+ tokens: oldTokens(a),
519
+ context: { peak: numOr(a["ctxPeakMax"], Math.max(0, ...peaks)) },
520
+ compactions: numOr(a["compactions"]),
521
+ },
522
+ mix: oldMix(a["mix"]),
523
+ });
524
+ }
525
+ const byTool = {};
526
+ for (const [k, tv] of Object.entries(objOf(r["tools"]))) {
527
+ const t = objOf(tv);
528
+ byTool[k] = { calls: numOr(t["calls"]), errors: numOr(t["errors"]) };
529
+ }
530
+ const shellTool = OLD_SHELL_TOOLS.find((k) => byTool[k] !== undefined);
531
+ const bash = objOf(r["bash"]);
532
+ if (shellTool !== undefined && Object.keys(bash).length > 0)
533
+ byTool[shellTool]["byCategory"] = { ...bash };
534
+ const byModel = Object.fromEntries(Object.entries(objOf(r["models"])).map(([k, m]) => [
535
+ k,
536
+ { turns: numOr(objOf(m)["turns"]), tokens: oldTokens(m) },
537
+ ]));
538
+ const m = objOf(r["main"]);
539
+ const tokens = objOf(r["tokens"]);
540
+ const byVerb = objOf(r["bkitCli"]);
541
+ const agents = Object.values(byAgent).map(objOf);
542
+ const tools = Object.values(byTool);
543
+ const start = r["start"];
544
+ const end = r["end"];
545
+ return {
546
+ by: defined({ workflow: r["workflow"] ?? "chat-stats", step: r["step"] ?? "scan" }),
547
+ bkit: {
548
+ version: r["bkitVersion"] === undefined || r["bkitVersion"] === "none" ? null : r["bkitVersion"],
549
+ versionSource: r["bkitVersionSource"] ?? "none",
550
+ via: arrOf(r["bkitVia"]),
551
+ workflows: arrOf(r["bkitWorkflows"]),
552
+ prompts: arrOf(r["bkitKinds"]),
553
+ cli: { total: sumNums(Object.values(byVerb)), byVerb },
554
+ },
555
+ session: {
556
+ platform: r["platform"] ?? "claude-code",
557
+ id: r["sessionId"] ?? null,
558
+ hostVersion: r["hostVersion"] ?? null,
559
+ entrypoint: r["entrypoint"] ?? null,
560
+ prompts: numOr(r["userPrompts"]),
561
+ },
562
+ time: defined({
563
+ start: start ?? null,
564
+ end: end ?? null,
565
+ totalMinutes: typeof r["minutes"] === "number"
566
+ ? r["minutes"]
567
+ : typeof start === "number" && typeof end === "number"
568
+ ? tenthOf((end - start) / 60000)
569
+ : null,
570
+ activeMinutes: numOr(r["activeMinutes"], null),
571
+ spans: arrOf(r["active"]),
572
+ lastActivity: r["lastActivityAt"],
573
+ }),
574
+ issue: {
575
+ id: r["issue"] ?? null,
576
+ all: arrOf(r["issues"]),
577
+ mentioned: arrOf(r["mentioned"]),
578
+ source: r["issueSource"] ?? "none",
579
+ },
580
+ run: { parentShas: arrOf(r["parentShas"]) },
581
+ effort: objOf(r["effort"]),
582
+ usage: {
583
+ total: {
584
+ turns: numOr(r["turns"]),
585
+ // Summed from the models, as a fresh scan writes it: the old row's own total
586
+ // estimated output once over the whole chat, so it can differ from its parts.
587
+ tokens: Object.keys(byModel).length > 0
588
+ ? Object.fromEntries(TOKENS.map((k) => [k, sumNums(Object.values(byModel).map((x) => x.tokens[k]))]))
589
+ : oldTokens(tokens),
590
+ context: { start: numOr(tokens["ctxStart"]), peak: numOr(tokens["ctxPeak"]) },
591
+ compactions: numOr(r["compactions"]),
592
+ },
593
+ main: defined({
594
+ turns: numOr(m["turns"]),
595
+ tokens: oldTokens(m),
596
+ context: { peak: numOr(m["ctxPeak"]) },
597
+ compactions: numOr(m["compactions"]),
598
+ mix: oldMix(m["mix"]),
599
+ }),
600
+ byModel,
601
+ },
602
+ agents: {
603
+ total: {
604
+ n: numOr(r["subagents"], sumNums(agents.map((a) => a["n"]))),
605
+ errors: sumNums(agents.map((a) => a["errors"])),
606
+ },
607
+ byAgent,
608
+ },
609
+ tools: {
610
+ total: {
611
+ calls: sumNums(tools.map((t) => t["calls"])),
612
+ errors: sumNums(tools.map((t) => t["errors"])),
613
+ },
614
+ byTool,
615
+ filesRead: numOr(r["filesRead"]),
616
+ },
617
+ scan: defined({
618
+ files: r["sourceFiles"],
619
+ bytes: r["sourceBytes"],
620
+ settled: r["settled"],
621
+ tokensRecorded: r["tokensRecorded"],
622
+ }),
623
+ };
624
+ }
625
+ /** A schema-1 run row's plan: its per-task lists, grouped when unsplit. */
626
+ function oldPlan(p) {
627
+ const n = numOr(p["subPlans"], 1);
628
+ const tests = arrOf(p["testsPerTask"]);
629
+ const files = arrOf(p["testFilesPerTask"]);
630
+ const tasks = numOr(p["tasks"], tests.length);
631
+ const kb = numOr(p["kb"]);
632
+ const out = { source: "file", total: { tasks, kb } };
633
+ out["subPlans"] =
634
+ n === 1
635
+ ? [
636
+ {
637
+ kb,
638
+ tasks: Array.from({ length: tasks }, (_, i) => ({
639
+ tests: numOr(tests[i], null),
640
+ testFiles: numOr(files[i], null),
641
+ })),
642
+ },
643
+ ]
644
+ : // A split plan's per-task lists were stored flat: which sub-plan each task was in is not known.
645
+ Array.from({ length: n }, () => ({ kb: null, tasks: null }));
646
+ if (isObj(p["complexity"]))
647
+ out["complexity"] = p["complexity"];
648
+ return out;
649
+ }
650
+ function oldRun(r) {
651
+ const steps = Object.entries(objOf(r["steps"])).map(([k, v]) => [k, objOf(v)]);
652
+ const byStep = Object.fromEntries(steps.map(([k, st]) => [
653
+ k,
654
+ {
655
+ n: numOr(st["n"]),
656
+ totalMinutes: numOr(st["minutes"]),
657
+ activeMinutes: numOr(st["active"], null),
658
+ },
659
+ ]));
660
+ const parts = steps.map(([, st]) => st);
661
+ const isBuild = r["workflow"] === "build" ||
662
+ ["verify", "gapsRemediate", "holistic", "challenge", "redTests"].some((k) => k in r);
663
+ const ch = isObj(r["challenge"]) ? r["challenge"] : undefined;
664
+ const f = isObj(ch?.["findings"]) ? ch["findings"] : undefined;
665
+ const lines = objOf(r["lines"]);
666
+ const lineParts = ["code", "tests", "docs"].map((k) => objOf(lines[k]));
667
+ const linesOut = isObj(r["lines"]) && isObj(lines["code"])
668
+ ? (() => {
669
+ const added = sumNums(lineParts.map((p) => p["added"]));
670
+ const deleted = sumNums(lineParts.map((p) => p["deleted"]));
671
+ return {
672
+ total: {
673
+ added,
674
+ deleted,
675
+ changed: added + deleted,
676
+ files: sumNums(lineParts.map((p) => p["files"])),
677
+ },
678
+ code: lines["code"],
679
+ tests: lines["tests"],
680
+ docs: lines["docs"],
681
+ merges: lines["merges"],
682
+ };
683
+ })()
684
+ : r["lines"];
685
+ return defined({
686
+ run: {
687
+ fid: r["fid"] ?? null,
688
+ issue: r["issue"] ?? null,
689
+ n: numOr(r["run"], null),
690
+ parentSha: r["parentSha"] ?? null,
691
+ parentShas: arrOf(r["parentShas"]),
692
+ },
693
+ time: defined({
694
+ start: r["start"] ?? null,
695
+ end: r["end"] ?? null,
696
+ totalMinutes: numOr(r["minutes"], null),
697
+ activeMinutes: numOr(r["activeMinutes"], null),
698
+ activeFrom: r["activeFrom"] ?? null,
699
+ lastActivity: r["lastActivityAt"],
700
+ }),
701
+ ending: r["ending"],
702
+ ci: arrOf(r["closeCi"]).map((cv) => {
703
+ const c = objOf(cv);
704
+ return defined({
705
+ status: c["outcome"] ?? null,
706
+ reason: c["reason"] ?? undefined,
707
+ attempts: numOr(c["attempts"], null),
708
+ });
709
+ }),
710
+ steps: {
711
+ total: {
712
+ n: sumNums(parts.map((st) => st["n"])),
713
+ totalMinutes: tenthOf(sumNums(parts.map((st) => st["minutes"]))),
714
+ activeMinutes: parts.some((st) => typeof st["active"] !== "number")
715
+ ? null
716
+ : tenthOf(sumNums(parts.map((st) => st["active"]))),
717
+ },
718
+ byStep,
719
+ },
720
+ lines: linesOut,
721
+ plan: isObj(r["plan"]) ? oldPlan(r["plan"]) : r["plan"],
722
+ build: isBuild
723
+ ? defined({
724
+ verify: arrOf(r["verify"]),
725
+ gapsRemediate: arrOf(r["gapsRemediate"]),
726
+ holistic: r["holistic"] ?? undefined,
727
+ challenge: ch
728
+ ? {
729
+ verdict: ch["verdict"] ?? null,
730
+ findings: f ? { total: sumNums(Object.values(f)), ...f } : null,
731
+ }
732
+ : undefined,
733
+ redTests: numOr(r["redTests"]),
734
+ })
735
+ : undefined,
736
+ scan: defined({ rows: r["sourceRows"], afterEnd: r["afterEnd"], settled: r["settled"] }),
737
+ });
738
+ }
739
+ /** `normalize` typed for readers: every row a fold or report sees. */
740
+ export const normalizeEvent = (raw) => normalize(raw);
741
+ // ---------------------------------------------------------------------------
742
+ // Totals — every total beside its parts must equal them
743
+ // ---------------------------------------------------------------------------
744
+ const sumOf = (xs) => xs.reduce((a, x) => a + (typeof x === "number" ? x : 0), 0);
745
+ const field = (o, ...pathTo) => pathTo.reduce((v, k) => (isObj(v) ? v[k] : undefined), o);
746
+ const TOKEN_FIELDS = ["input", "cacheWrite", "cacheRead", "output", "outputEst", "read"];
747
+ /**
748
+ * The first total on `row` that does not equal its parts, or undefined. Only blocks
749
+ * the row carries are checked. Minute totals are summed from unrounded values, so
750
+ * each part may be off by its rounding: +-0.1 per part.
751
+ */
752
+ export function totalsProblem(row) {
753
+ const eq = (what, total, parts, tol = 0) => typeof total === "number" && Math.abs(total - parts) <= tol + 1e-9
754
+ ? undefined
755
+ : `${what} is ${String(total)}, its parts sum to ${tenthOf(parts)}`;
756
+ const checks = [];
757
+ const lines = row["lines"];
758
+ if (isObj(lines) && isObj(lines["total"])) {
759
+ const t = lines["total"];
760
+ checks.push(() => eq("lines.total.changed", t["changed"], sumOf([t["added"], t["deleted"]])));
761
+ if (["code", "tests", "docs"].some((p) => isObj(lines[p]))) {
762
+ for (const f of ["added", "deleted", "files"]) {
763
+ checks.push(() => eq(`lines.total.${f}`, t[f], sumOf(["code", "tests", "docs"].map((p) => field(lines, p, f)))));
764
+ }
765
+ }
766
+ }
767
+ const time = row["time"];
768
+ if (isObj(time) && typeof time["start"] === "number" && typeof time["end"] === "number") {
769
+ checks.push(() => eq("time.totalMinutes", time["totalMinutes"], (time["end"] - time["start"]) / 60000, 0.1));
770
+ const spans = time["spans"];
771
+ if (row["kind"] === "chat" &&
772
+ Array.isArray(spans) &&
773
+ typeof time["activeMinutes"] === "number") {
774
+ const ms = sumOf(spans.map((s) => (Array.isArray(s) ? Number(s[1]) - Number(s[0]) : 0)));
775
+ checks.push(() => eq("time.activeMinutes", time["activeMinutes"], ms / 60000, 0.1));
776
+ }
777
+ }
778
+ const steps = row["steps"];
779
+ if (isObj(steps) && isObj(steps["byStep"]) && isObj(steps["total"])) {
780
+ const parts = Object.values(steps["byStep"]);
781
+ for (const f of ["n", "totalMinutes", "activeMinutes"]) {
782
+ const total = steps["total"][f];
783
+ if (total === undefined || total === null)
784
+ continue;
785
+ const tol = f === "n" ? 0 : 0.1 * Math.max(1, parts.length);
786
+ checks.push(() => eq(`steps.total.${f}`, total, sumOf(parts.map((p) => field(p, f))), tol));
787
+ }
788
+ }
789
+ const plan = row["plan"];
790
+ if (isObj(plan) && isObj(plan["total"]) && Array.isArray(plan["groups"])) {
791
+ const groups = plan["groups"];
792
+ const total = plan["total"];
793
+ checks.push(() => eq("plan.total.tasks", total["tasks"], sumOf(groups.map((g) => arrOf(field(g, "tasks")).length))));
794
+ }
795
+ const build = row["build"];
796
+ if (isObj(build)) {
797
+ const findings = field(build, "challenge", "findings");
798
+ if (isObj(findings)) {
799
+ checks.push(() => eq("build.challenge.findings.total", findings["total"], sumOf(["CRITICAL", "HIGH", "MEDIUM", "LOW"].map((s) => findings[s]))));
800
+ }
801
+ if (typeof build["redTests"] === "number" && isObj(field(steps, "byStep"))) {
802
+ checks.push(() => eq("build.redTests", build["redTests"], Number(field(steps, "byStep", "build-tests-red", "n") ?? 0)));
803
+ }
804
+ }
805
+ const usage = row["usage"];
806
+ const agents = row["agents"];
807
+ const byAgent = isObj(agents) && isObj(agents["byAgent"]) ? Object.values(agents["byAgent"]) : [];
808
+ if (isObj(usage)) {
809
+ const blocks = [
810
+ ["usage.total", usage["total"]],
811
+ ["usage.main", usage["main"]],
812
+ ...Object.entries(isObj(usage["byModel"]) ? usage["byModel"] : {}).map(([m, u]) => [`usage.byModel.${m}`, u]),
813
+ ...Object.entries(isObj(agents) && isObj(agents["byAgent"]) ? agents["byAgent"] : {}).map(([a, v]) => [`agents.byAgent.${a}.usage`, field(v, "usage")]),
814
+ ];
815
+ for (const [name, u] of blocks) {
816
+ const tokens = field(u, "tokens");
817
+ if (!isObj(tokens) || tokens["read"] === undefined)
818
+ continue;
819
+ checks.push(() => eq(`${name}.tokens.read`, tokens["read"], sumOf([tokens["input"], tokens["cacheWrite"], tokens["cacheRead"]])));
820
+ }
821
+ const total = usage["total"];
822
+ const models = isObj(usage["byModel"]) ? Object.values(usage["byModel"]) : [];
823
+ if (isObj(total) && models.length > 0) {
824
+ checks.push(() => eq("usage.total.turns", total["turns"], sumOf(models.map((m) => field(m, "turns")))));
825
+ for (const f of TOKEN_FIELDS) {
826
+ if (field(total, "tokens", f) === undefined)
827
+ continue;
828
+ checks.push(() => eq(`usage.total.tokens.${f}`, field(total, "tokens", f), sumOf(models.map((m) => field(m, "tokens", f)))));
829
+ }
830
+ }
831
+ if (isObj(total) && total["compactions"] !== undefined) {
832
+ checks.push(() => eq("usage.total.compactions", total["compactions"], sumOf([
833
+ field(usage, "main", "compactions"),
834
+ ...byAgent.map((a) => field(a, "usage", "compactions")),
835
+ ])));
836
+ }
837
+ const peak = field(total, "context", "peak");
838
+ if (typeof peak === "number") {
839
+ const peaks = [
840
+ field(usage, "main", "context", "peak"),
841
+ ...byAgent.flatMap((a) => Array.isArray(field(a, "instances"))
842
+ ? field(a, "instances").map((i) => field(i, "contextPeak"))
843
+ : []),
844
+ ].filter((v) => typeof v === "number");
845
+ if (peaks.length > 0)
846
+ checks.push(() => eq("usage.total.context.peak", peak, Math.max(...peaks)));
847
+ }
848
+ }
849
+ if (isObj(agents) && isObj(agents["total"]) && isObj(agents["byAgent"])) {
850
+ const total = agents["total"];
851
+ for (const f of ["n", "errors"]) {
852
+ checks.push(() => eq(`agents.total.${f}`, total[f], sumOf(byAgent.map((a) => field(a, f)))));
853
+ }
854
+ for (const [name, a] of Object.entries(agents["byAgent"])) {
855
+ const instances = field(a, "instances");
856
+ if (Array.isArray(instances)) {
857
+ checks.push(() => eq(`agents.byAgent.${name}.n`, field(a, "n"), instances.length));
858
+ }
859
+ }
860
+ }
861
+ const tools = row["tools"];
862
+ if (isObj(tools) && isObj(tools["total"]) && isObj(tools["byTool"])) {
863
+ const parts = Object.values(tools["byTool"]);
864
+ for (const f of ["calls", "errors"]) {
865
+ checks.push(() => eq(`tools.total.${f}`, field(tools, "total", f), sumOf(parts.map((p) => field(p, f)))));
866
+ }
867
+ }
868
+ const cli = field(row, "bkit", "cli");
869
+ if (isObj(cli) && isObj(cli["byVerb"])) {
870
+ checks.push(() => eq("bkit.cli.total", cli["total"], sumOf(Object.values(cli["byVerb"]))));
871
+ }
872
+ for (const check of checks) {
873
+ const problem = check();
874
+ if (problem !== undefined)
875
+ return problem;
876
+ }
877
+ return undefined;
878
+ }
195
879
  // ---------------------------------------------------------------------------
196
880
  // Validate — reject malformed + unknown kind before write
197
881
  // ---------------------------------------------------------------------------
@@ -200,13 +884,19 @@ export function validate(event) {
200
884
  return { ok: false, reason: "event must be an object" };
201
885
  }
202
886
  const e = event;
203
- // A2-mandatory envelope fields
204
- const mandatory = ["ts", "kind", "repo", "bkitVersion", "step"];
205
- for (const field of mandatory) {
887
+ if (e["schema"] !== SCHEMA) {
888
+ return { ok: false, reason: `schema must be ${SCHEMA} (got: ${String(e["schema"])})` };
889
+ }
890
+ for (const field of ["ts", "kind", "repo", "bkit", "by"]) {
206
891
  if (e[field] === undefined || e[field] === null) {
207
892
  return { ok: false, reason: `missing mandatory field: ${field}` };
208
893
  }
209
894
  }
895
+ for (const flat of ["bkitVersion", "workflow", "step", "subagent", "model"]) {
896
+ if (e[flat] !== undefined) {
897
+ return { ok: false, reason: `${flat} is not a top-level field (bkit.version, by.*)` };
898
+ }
899
+ }
210
900
  if (typeof e["ts"] !== "number") {
211
901
  return { ok: false, reason: "ts must be a number (ms unix)" };
212
902
  }
@@ -219,11 +909,35 @@ export function validate(event) {
219
909
  if (typeof e["repo"] !== "string" || e["repo"] === "") {
220
910
  return { ok: false, reason: "repo must be a non-empty string" };
221
911
  }
222
- if (typeof e["bkitVersion"] !== "string" || e["bkitVersion"] === "") {
223
- return { ok: false, reason: "bkitVersion must be a non-empty string" };
912
+ const bkit = e["bkit"];
913
+ // null on chat / run rows: the script found no bkit version in the repo's history at that time.
914
+ const unknownVersion = bkit !== undefined &&
915
+ isObj(bkit) &&
916
+ bkit["version"] === null &&
917
+ (e["kind"] === "chat" || e["kind"] === "run");
918
+ if (!isObj(bkit) ||
919
+ ((typeof bkit["version"] !== "string" || bkit["version"] === "") && !unknownVersion)) {
920
+ return {
921
+ ok: false,
922
+ reason: "bkit.version must be a non-empty string (null on chat / run rows)",
923
+ };
224
924
  }
225
- if (typeof e["step"] !== "string" || e["step"] === "") {
226
- return { ok: false, reason: "step must be a non-empty string" };
925
+ const by = e["by"];
926
+ if (!isObj(by))
927
+ return { ok: false, reason: "by must be an object" };
928
+ if (typeof by["step"] !== "string" || by["step"] === "") {
929
+ return { ok: false, reason: "by.step must be a non-empty string" };
930
+ }
931
+ for (const f of ["workflow", "subagent", "model"]) {
932
+ if (by[f] !== undefined && (typeof by[f] !== "string" || by[f] === "")) {
933
+ return { ok: false, reason: `by.${f} must be a non-empty string when present` };
934
+ }
935
+ }
936
+ if (by["model"] !== undefined && by["subagent"] === undefined) {
937
+ return { ok: false, reason: "by.model is set only on subagent rows" };
938
+ }
939
+ if (e["references"] !== undefined && !Array.isArray(e["references"])) {
940
+ return { ok: false, reason: "references must be an array when present" };
227
941
  }
228
942
  // kind-specific payload validation (minimal — structural check)
229
943
  const kind = e["kind"];
@@ -293,80 +1007,183 @@ export function validate(event) {
293
1007
  else if (e["reason"] !== undefined) {
294
1008
  return { ok: false, reason: `${kind} rows do not carry reason` };
295
1009
  }
1010
+ // firstTs / updates — the entry's age and row count, set by the writer (withLineage).
1011
+ if (AGED_KINDS.has(kind)) {
1012
+ const first = e["firstTs"];
1013
+ if (first !== undefined && (typeof first !== "number" || !(first <= e["ts"]))) {
1014
+ return { ok: false, reason: "firstTs must be a number no later than ts" };
1015
+ }
1016
+ const updates = e["updates"];
1017
+ if (updates !== undefined && (!Number.isInteger(updates) || updates < 1)) {
1018
+ return { ok: false, reason: "updates must be a positive integer" };
1019
+ }
1020
+ }
1021
+ else {
1022
+ for (const f of ["firstTs", "updates"]) {
1023
+ if (e[f] !== undefined)
1024
+ return { ok: false, reason: `${kind} rows do not carry ${f}` };
1025
+ }
1026
+ }
1027
+ const run = isObj(e["run"]) ? e["run"] : undefined;
1028
+ if (kind === "compliance" || kind === "counter") {
1029
+ if (typeof run?.["parentSha"] !== "string" || run["parentSha"] === "") {
1030
+ return { ok: false, reason: `${kind} row missing run.parentSha` };
1031
+ }
1032
+ }
296
1033
  if (kind === "compliance") {
297
- if (!e["rule"] || !e["result"] || !e["phase"] || !e["parentSha"]) {
298
- return { ok: false, reason: "compliance row missing required payload fields" };
1034
+ const rule = e["rule"];
1035
+ if (!isObj(rule) || typeof rule["id"] !== "string" || rule["id"] === "") {
1036
+ return { ok: false, reason: "compliance row missing rule.id" };
1037
+ }
1038
+ if (!["pass", "fail", "n/a"].includes(rule["verdict"])) {
1039
+ return {
1040
+ ok: false,
1041
+ reason: `compliance rule.verdict must be pass|fail|n/a (got: ${String(rule["verdict"])})`,
1042
+ };
1043
+ }
1044
+ }
1045
+ if (kind === "counter") {
1046
+ const c = e["counter"];
1047
+ if (!isObj(c) ||
1048
+ typeof c["name"] !== "string" ||
1049
+ c["name"] === "" ||
1050
+ typeof c["value"] !== "number") {
1051
+ return { ok: false, reason: "counter row needs counter.name and a numeric counter.value" };
1052
+ }
1053
+ if (c["source"] !== "model-self-report" && c["source"] !== "shell-bus") {
1054
+ return { ok: false, reason: "counter.source must be model-self-report or shell-bus" };
299
1055
  }
300
1056
  }
301
1057
  if (kind === "outcome") {
302
- // Every workflow run carries these; `prNumber` is ship-only (see below).
303
- const numFields = [
304
- "linesAdded",
305
- "linesDeleted",
306
- "linesChanged",
307
- "filesChanged",
308
- "taskCount",
309
- "durationSeconds",
310
- "deferrals",
311
- "failures",
312
- ];
313
- for (const f of numFields) {
314
- if (typeof e[f] !== "number") {
315
- return { ok: false, reason: `outcome missing or invalid numeric field: ${f}` };
316
- }
1058
+ if (run === undefined)
1059
+ return { ok: false, reason: "outcome row missing run" };
1060
+ if (run["parentSha"] !== undefined && typeof run["parentSha"] !== "string") {
1061
+ return { ok: false, reason: "outcome run.parentSha must be a string when present" };
1062
+ }
1063
+ if (run["pr"] !== undefined && typeof run["pr"] !== "number") {
1064
+ return { ok: false, reason: "outcome run.pr must be a number when present" };
1065
+ }
1066
+ const time = e["time"];
1067
+ // null: the run's start row is missing, so its time could not be measured.
1068
+ if (time !== null &&
1069
+ (!isObj(time) || ["start", "end", "totalMinutes"].some((f) => typeof time[f] !== "number"))) {
1070
+ return {
1071
+ ok: false,
1072
+ reason: "outcome row needs time.start, time.end and time.totalMinutes, or time: null",
1073
+ };
1074
+ }
1075
+ if (!OUTCOME_ENDINGS.has(e["ending"])) {
1076
+ return {
1077
+ ok: false,
1078
+ reason: `outcome ending must be ${[...OUTCOME_ENDINGS].join("|")} (got: ${String(e["ending"])})`,
1079
+ };
1080
+ }
1081
+ if (e["publish"] !== undefined && typeof e["publish"] !== "boolean") {
1082
+ return {
1083
+ ok: false,
1084
+ reason: `outcome publish must be a boolean (got: ${String(e["publish"])})`,
1085
+ };
317
1086
  }
318
- // prNumber — ship-only, optional; validate only when present.
319
- if (e["prNumber"] !== undefined && typeof e["prNumber"] !== "number") {
320
- return { ok: false, reason: "outcome prNumber must be a number when present" };
321
- }
322
- // parentSha — the run's identity anchor; optional (absent on some ship rows).
323
- if (e["parentSha"] !== undefined && typeof e["parentSha"] !== "string") {
324
- return { ok: false, reason: "outcome parentSha must be a string when present" };
325
- }
326
- // Optional per-run numeric metrics: validate type only when present.
327
- for (const f of [
328
- "frictionAdded",
329
- "deferralsAdded",
330
- "failuresAdded",
331
- "correctiveCommits",
332
- "reviewIterations",
333
- "ciTrySuccess",
334
- ]) {
335
- if (e[f] !== undefined && typeof e[f] !== "number") {
336
- return { ok: false, reason: `outcome ${f} must be a number when present` };
1087
+ const ledgers = e["ledgers"];
1088
+ if (typeof field(ledgers, "deferrals", "repo") !== "number" ||
1089
+ typeof field(ledgers, "failures", "repo") !== "number") {
1090
+ return {
1091
+ ok: false,
1092
+ reason: "outcome row needs ledgers.deferrals.repo and ledgers.failures.repo",
1093
+ };
1094
+ }
1095
+ for (const l of ["deferrals", "failures", "friction"]) {
1096
+ const added = field(ledgers, l, "added");
1097
+ if (added !== undefined && typeof added !== "number") {
1098
+ return { ok: false, reason: `outcome ledgers.${l}.added must be a number when present` };
337
1099
  }
338
1100
  }
339
- if (e["planSource"] !== undefined && e["planSource"] !== "brief") {
340
- return { ok: false, reason: 'outcome planSource must be "brief" when present' };
1101
+ const source = field(e["plan"], "source");
1102
+ if (e["plan"] !== undefined && source !== "file" && source !== "converted") {
1103
+ return {
1104
+ ok: false,
1105
+ reason: `outcome plan.source must be file|converted (got: ${String(source)})`,
1106
+ };
341
1107
  }
342
- // coverage is optional + per-tier; validate structure only when present.
343
- if (e["coverage"] !== undefined) {
344
- const cov = e["coverage"];
345
- if (typeof cov !== "object" || cov === null) {
346
- return { ok: false, reason: "outcome coverage must be an object" };
1108
+ const context = e["context"];
1109
+ if (context !== undefined) {
1110
+ const byAgent = field(context, "byAgent");
1111
+ if (!isObj(context) || typeof context["host"] !== "string" || !isObj(byAgent)) {
1112
+ return { ok: false, reason: "outcome context needs host and a byAgent object" };
1113
+ }
1114
+ for (const [agent, a] of Object.entries(byAgent)) {
1115
+ if (typeof field(a, "peak") !== "number" || typeof field(a, "runs") !== "number") {
1116
+ return {
1117
+ ok: false,
1118
+ reason: `outcome context.byAgent.${agent} needs numeric peak and runs`,
1119
+ };
1120
+ }
347
1121
  }
1122
+ }
1123
+ const cov = field(e["ship"], "coverage");
1124
+ if (cov !== undefined) {
1125
+ if (!isObj(cov))
1126
+ return { ok: false, reason: "outcome ship.coverage must be an object" };
348
1127
  for (const tier of ["unit", "integration", "e2e"]) {
349
1128
  const t = cov[tier];
350
1129
  if (t === undefined)
351
1130
  continue;
352
- if (typeof t !== "object" || t === null) {
353
- return { ok: false, reason: `outcome coverage.${tier} must be an object` };
354
- }
1131
+ if (!isObj(t))
1132
+ return { ok: false, reason: `outcome ship.coverage.${tier} must be an object` };
355
1133
  for (const m of ["lines", "branches", "functions", "statements"]) {
356
- const v = t[m];
357
- if (v !== undefined && typeof v !== "number") {
358
- return { ok: false, reason: `outcome coverage.${tier}.${m} must be a number` };
1134
+ if (t[m] !== undefined && typeof t[m] !== "number") {
1135
+ return { ok: false, reason: `outcome ship.coverage.${tier}.${m} must be a number` };
359
1136
  }
360
1137
  }
361
1138
  }
362
1139
  }
363
1140
  }
364
- if (kind === "counter") {
365
- if (!e["counterName"] || typeof e["counterValue"] !== "number" || !e["parentSha"]) {
366
- return { ok: false, reason: "counter row missing required payload fields" };
1141
+ if (kind === "chat") {
1142
+ if (!e["key"] || typeof e["key"] !== "string") {
1143
+ return { ok: false, reason: "chat row missing required field: key (<platform>:<sessionId>)" };
1144
+ }
1145
+ if (typeof field(e, "time", "start") !== "number" ||
1146
+ typeof field(e, "time", "end") !== "number") {
1147
+ return { ok: false, reason: "chat row needs time.start and time.end" };
367
1148
  }
368
- if (e["counterSource"] !== "model-self-report" && e["counterSource"] !== "shell-bus") {
369
- return { ok: false, reason: "counter counterSource must be model-self-report or shell-bus" };
1149
+ if (typeof field(e, "scan", "bytes") !== "number") {
1150
+ return { ok: false, reason: "chat row needs scan.bytes" };
1151
+ }
1152
+ if (!Array.isArray(field(e, "run", "parentShas")) || !Array.isArray(field(e, "issue", "all"))) {
1153
+ return {
1154
+ ok: false,
1155
+ reason: "chat row needs run.parentShas and issue.all arrays (the join keys)",
1156
+ };
1157
+ }
1158
+ }
1159
+ if (kind === "run") {
1160
+ if (!e["key"] || typeof e["key"] !== "string") {
1161
+ return {
1162
+ ok: false,
1163
+ reason: "run row missing required field: key (<fid fp>:<workflow>:<start>)",
1164
+ };
1165
+ }
1166
+ if (typeof run?.["fid"] !== "string" || run["fid"] === "") {
1167
+ return { ok: false, reason: "run row missing required field: run.fid" };
1168
+ }
1169
+ if (typeof field(e, "time", "start") !== "number" ||
1170
+ typeof field(e, "time", "end") !== "number") {
1171
+ return { ok: false, reason: "run row needs time.start and time.end" };
1172
+ }
1173
+ if (typeof field(e, "scan", "rows") !== "number") {
1174
+ return { ok: false, reason: "run row needs scan.rows" };
1175
+ }
1176
+ if (!RUN_ENDINGS.has(e["ending"])) {
1177
+ return {
1178
+ ok: false,
1179
+ reason: `run ending must be one of ${[...RUN_ENDINGS].join("|")} (got: ${String(e["ending"])})`,
1180
+ };
1181
+ }
1182
+ if (run["parentSha"] !== null && typeof run["parentSha"] !== "string") {
1183
+ return { ok: false, reason: "run run.parentSha must be a string or null" };
1184
+ }
1185
+ if (!Array.isArray(run["parentShas"])) {
1186
+ return { ok: false, reason: "run row needs a run.parentShas array (the join keys)" };
370
1187
  }
371
1188
  }
372
1189
  if (kind === "feature") {
@@ -380,13 +1197,17 @@ export function validate(event) {
380
1197
  return { ok: false, reason: "feature row missing required payload field: description" };
381
1198
  }
382
1199
  }
1200
+ const totals = totalsProblem(e);
1201
+ if (totals !== undefined)
1202
+ return { ok: false, reason: `totals: ${totals}` };
383
1203
  return { ok: true };
384
1204
  }
385
1205
  /**
386
1206
  * Tolerant parse — per-line try/catch, skips + counts unparseable lines.
387
1207
  * NEVER throws.
388
1208
  *
389
- * No version gating — ALL well-formed rows are kept regardless of `bkitVersion`.
1209
+ * No version gating — ALL well-formed rows are kept as written, `schema` or not;
1210
+ * {@link fold} reads each through {@link normalize}.
390
1211
  */
391
1212
  export function parse(raw) {
392
1213
  const result = {
@@ -429,13 +1250,13 @@ export function compareTs(a, b) {
429
1250
  return x < y ? -1 : x > y ? 1 : 0;
430
1251
  }
431
1252
  /**
432
- * Fold events into a Map keyed by per-kind composite identity.
433
- * Later rows (higher ts) win for the same key — last-write-wins fold.
434
- * Mirrors ledgers/status.ts's dedupe approach but with per-kind composite tuples.
1253
+ * Fold events into a Map keyed by per-kind composite identity, each row in the
1254
+ * current shape ({@link normalize}). Later rows (higher ts) win for the same key —
1255
+ * last-write-wins fold.
435
1256
  */
436
1257
  export function fold(events) {
437
1258
  // Sort by ts ascending so later rows overwrite earlier ones
438
- const sorted = [...events].sort(compareTs);
1259
+ const sorted = events.map(normalizeEvent).sort(compareTs);
439
1260
  const state = new Map();
440
1261
  for (const event of sorted) {
441
1262
  const key = identity(event);
@@ -443,6 +1264,64 @@ export function fold(events) {
443
1264
  }
444
1265
  return state;
445
1266
  }
1267
+ const positiveInt = (v) => Number.isInteger(v) && v > 0 ? v : undefined;
1268
+ /**
1269
+ * `firstTs` + `updates` of one entry from all of its rows, in any order. A row
1270
+ * carrying `updates` stands for that many rows, so the count survives compaction;
1271
+ * each later row adds one. Rows sharing a numeric `ts` are one write: a compacted
1272
+ * winner beside the pre-compaction copy a union merge brought back.
1273
+ */
1274
+ export function lineage(rows) {
1275
+ let firstTs;
1276
+ let updates = 0;
1277
+ let prev;
1278
+ for (const r of [...rows].sort((a, b) => compareTs({ ts: a.ts }, { ts: b.ts }))) {
1279
+ for (const t of [r.firstTs, r.ts]) {
1280
+ if (typeof t === "number" && Number.isFinite(t) && (firstTs === undefined || t < firstTs)) {
1281
+ firstTs = t;
1282
+ }
1283
+ }
1284
+ const own = positiveInt(r.updates);
1285
+ const at = tsOrder(r.ts);
1286
+ updates =
1287
+ at === prev && Number.isFinite(at)
1288
+ ? Math.max(updates, own ?? updates)
1289
+ : Math.max(own ?? 0, updates + 1);
1290
+ prev = at;
1291
+ }
1292
+ return firstTs === undefined ? { updates } : { firstTs, updates };
1293
+ }
1294
+ /** `row` with `firstTs` + `updates` set to `l`, placed right after `ts` (at the end when it has none). */
1295
+ export function placeLineage(row, l) {
1296
+ const fields = l.firstTs === undefined ? {} : { firstTs: l.firstTs };
1297
+ fields["updates"] = l.updates;
1298
+ const out = {};
1299
+ for (const [k, v] of Object.entries(row)) {
1300
+ if (k === "firstTs" || k === "updates")
1301
+ continue;
1302
+ out[k] = v;
1303
+ if (k === "ts")
1304
+ Object.assign(out, fields);
1305
+ }
1306
+ return "ts" in row ? out : { ...out, ...fields };
1307
+ }
1308
+ /** True for a row `bkit ledgers compact` folds: an {@link AGED_KINDS} kind with a non-empty string `key`. */
1309
+ export const isAgedRow = (row) => AGED_KINDS.has(String(row["kind"])) && typeof row["key"] === "string" && row["key"] !== "";
1310
+ /**
1311
+ * `row` about to be appended, with `firstTs` + `updates` carried forward from
1312
+ * `prior` (rows already in its ledger) and any supplied values replaced. A row
1313
+ * {@link isAgedRow} rejects is returned unchanged.
1314
+ */
1315
+ export function withLineage(prior, row) {
1316
+ if (!isAgedRow(row))
1317
+ return row;
1318
+ const id = identity(normalizeEvent(row));
1319
+ const same = prior
1320
+ .map(normalizeEvent)
1321
+ .filter((p) => isAgedRow(p) && identity(p) === id);
1322
+ const { firstTs: _first, updates: _updates, ...fresh } = row;
1323
+ return placeLineage(row, lineage([...same, fresh]));
1324
+ }
446
1325
  /** Reads every existing file in `paths` and folds all events together. */
447
1326
  export function foldFrom(paths) {
448
1327
  let allEvents = [];
@@ -456,8 +1335,9 @@ export function foldFrom(paths) {
456
1335
  totalMalformed += parsed.malformedLineCount;
457
1336
  }
458
1337
  const versionCounts = {};
459
- for (const e of allEvents) {
460
- const v = typeof e.bkitVersion === "string" && e.bkitVersion !== "" ? e.bkitVersion : "unknown";
1338
+ for (const e of allEvents.map(normalizeEvent)) {
1339
+ const version = e.bkit?.version;
1340
+ const v = typeof version === "string" && version !== "" ? version : "unknown";
461
1341
  versionCounts[v] = (versionCounts[v] ?? 0) + 1;
462
1342
  }
463
1343
  const rowCount = allEvents.length;
@@ -536,7 +1416,7 @@ export function workflowTelemetry(result, workflow) {
536
1416
  // Folded ENTITY count per workflow (not raw lines) — an updated row folds to one.
537
1417
  const workflowCounts = {};
538
1418
  for (const ev of events) {
539
- const w = ev.workflow || "unknown";
1419
+ const w = ev.by?.workflow || "unknown";
540
1420
  workflowCounts[w] = (workflowCounts[w] ?? 0) + 1;
541
1421
  }
542
1422
  const base = {
@@ -558,14 +1438,17 @@ export function workflowTelemetry(result, workflow) {
558
1438
  }
559
1439
  // This workflow's run rows (generalized outcome), oldest → newest.
560
1440
  const runs = events
561
- .filter((e) => e.kind === "outcome" && e.workflow === workflow)
1441
+ .filter((e) => e.kind === "outcome" && e.by?.workflow === workflow)
562
1442
  .sort(compareTs);
563
1443
  const cur = runs.at(-1);
564
- // Baseline only against runs of the same plan source — a brief run (no plan)
565
- // has no tasks and a different shape, so mixing them skews every median.
1444
+ // Baseline only against runs of the same plan source — a converted run's plan was
1445
+ // written by the build from its input, so mixing it with file runs skews every median.
1446
+ // An old `brief` row (no plan file) counts as converted.
1447
+ const oldBrief = (r) => r?.plan?.source === "brief";
1448
+ const converted = (r) => r?.plan?.source === "converted" || oldBrief(r);
566
1449
  const prev = runs
567
1450
  .slice(0, -1)
568
- .filter((r) => r.planSource === cur?.planSource)
1451
+ .filter((r) => converted(r) === converted(cur))
569
1452
  .slice(-RUN_BASELINE_WINDOW);
570
1453
  const repo = cur?.repo ?? events[0]?.repo ?? "unknown/repo";
571
1454
  // Rendered as a Markdown table so it drops straight into the tracker comment
@@ -578,61 +1461,63 @@ export function workflowTelemetry(result, workflow) {
578
1461
  if (cur) {
579
1462
  // med·N: median of the previous runs that carry the field, `—` until there are
580
1463
  // at least RUN_BASELINE_MIN of them (1–2 samples is noise, not a baseline).
581
- const medCell = (key, fmt = String) => {
582
- const vals = prev.map((r) => r[key]).filter((v) => typeof v === "number");
1464
+ const medCell = (get, fmt = String) => {
1465
+ const vals = prev.map(get).filter((v) => typeof v === "number");
583
1466
  if (vals.length < RUN_BASELINE_MIN)
584
1467
  return "—";
585
1468
  const m = median(vals);
586
1469
  return m === null ? "—" : fmt(m);
587
1470
  };
588
- // Core metrics — always shown.
589
- row("durationSeconds", `${cur.durationSeconds} (${humanizeDuration(cur.durationSeconds)})`, medCell("durationSeconds", (n) => `${n} (${humanizeDuration(n)})`));
590
- row("linesChanged", `${cur.linesChanged} (+${cur.linesAdded}/-${cur.linesDeleted})`, medCell("linesChanged"));
591
- row("filesChanged", `${cur.filesChanged}`, medCell("filesChanged"));
592
- // Conditional metrics — shown only when this run carries them.
593
- if (cur.planSource)
594
- row("planSource", cur.planSource, "—");
595
- if (cur.taskCount > 0)
596
- row("taskCount", `${cur.taskCount}`, medCell("taskCount"));
597
- if (typeof cur.reviewIterations === "number")
598
- row("reviewIterations", `${cur.reviewIterations}`, medCell("reviewIterations"));
599
- if (typeof cur.correctiveCommits === "number")
600
- row("correctiveCommits", `${cur.correctiveCommits}`, medCell("correctiveCommits"));
601
- if (typeof cur.ciTrySuccess === "number")
602
- row("ciTrySuccess", `${cur.ciTrySuccess}${cur.ciTrySuccess === 1 ? " (first-try)" : ""}`, medCell("ciTrySuccess"));
603
- // Instinct-loop deltas — frictionAdded is the featured signal (shown even at 0).
604
- if (typeof cur.frictionAdded === "number")
605
- row("frictionAdded", `${cur.frictionAdded}`, medCell("frictionAdded"));
606
- if (typeof cur.deferralsAdded === "number")
607
- row("deferralsAdded", `${cur.deferralsAdded}`, medCell("deferralsAdded"));
608
- if (typeof cur.failuresAdded === "number")
609
- row("failuresAdded", `${cur.failuresAdded}`, medCell("failuresAdded"));
610
- }
611
- // Compliance — this run's rows. The `outcome` and `compliance` rows are written
612
- // at different points in a close (different HEADs → different parentSha), so we
613
- // DON'T match on the run row's parentSha: we take the newest same-workflow
614
- // compliance group (its parentSha) as "this run".
615
- const complianceRows = events.filter((e) => e.kind === "compliance" && e.workflow === workflow);
616
- const compSha = complianceRows.slice().sort((a, b) => compareTs(b, a))[0]?.parentSha;
1471
+ const mins = (n) => `${n} (${humanizeDuration(n * 60)})`;
1472
+ const show = (label, get, fmt) => {
1473
+ const v = get(cur);
1474
+ if (typeof v === "number")
1475
+ row(label, fmt ? fmt(v) : String(v), medCell(get, fmt));
1476
+ };
1477
+ show("time.totalMinutes", (r) => r.time?.totalMinutes, mins);
1478
+ const total = cur.lines?.total;
1479
+ if (total) {
1480
+ row("lines.total.changed", `${total.changed} (+${total.added}/-${total.deleted})`, medCell((r) => r.lines?.total?.changed));
1481
+ show("lines.total.files", (r) => r.lines?.total?.files);
1482
+ }
1483
+ show("plan.total.tasks", (r) => r.plan?.total?.tasks);
1484
+ if (cur.ending === "close-failed" || cur.ending === "held")
1485
+ row("ending", cur.ending, "—");
1486
+ // Instinct-loop deltas — friction is the featured signal (shown even at 0).
1487
+ show("ledgers.friction.added", (r) => r.ledgers?.friction?.added);
1488
+ show("ledgers.deferrals.added", (r) => r.ledgers?.deferrals?.added);
1489
+ show("ledgers.failures.added", (r) => r.ledgers?.failures?.added);
1490
+ }
1491
+ // Compliance — this run's rows: the run row's run.parentSha, else the newest
1492
+ // same-workflow compliance group.
1493
+ const complianceRows = events.filter((e) => e.kind === "compliance" && e.by?.workflow === workflow);
1494
+ const newestSha = complianceRows.slice().sort((a, b) => compareTs(b, a))[0]?.run?.parentSha;
1495
+ const curSha = cur?.run?.parentSha;
1496
+ const compSha = complianceRows.some((c) => c.run?.parentSha === curSha) ? curSha : newestSha;
617
1497
  if (compSha) {
618
- const comp = complianceRows.filter((c) => c.parentSha === compSha);
619
- const pass = comp.filter((c) => c.result === "pass").length;
620
- const fail = comp.filter((c) => c.result === "fail").length;
621
- const na = comp.filter((c) => c.result === "n/a").length;
1498
+ const comp = complianceRows.filter((c) => c.run?.parentSha === compSha);
1499
+ const pass = comp.filter((c) => c.rule?.verdict === "pass").length;
1500
+ const fail = comp.filter((c) => c.rule?.verdict === "fail").length;
1501
+ const na = comp.filter((c) => c.rule?.verdict === "n/a").length;
622
1502
  const denom = pass + fail + na;
623
1503
  const rate = denom > 0 ? Math.round((pass / denom) * 100) : 0;
624
1504
  row("compliance", `${pass} pass · ${fail} fail · ${na} n/a (${rate}%)`, "—");
625
1505
  }
626
- // Attempts — the newest same-workflow *_ATTEMPT counter (this run).
627
- const attempt = events
628
- .filter((e) => e.kind === "counter" && e.workflow === workflow && /_ATTEMPT$/.test(e.counterName))
629
- .sort((a, b) => compareTs(b, a))[0];
1506
+ // Attempts — this run's *_ATTEMPT counter, else the newest same-workflow one.
1507
+ const attempts = events
1508
+ .filter((e) => e.kind === "counter" &&
1509
+ e.by?.workflow === workflow &&
1510
+ /_ATTEMPT$/.test(e.counter?.name ?? ""))
1511
+ .sort((a, b) => compareTs(b, a));
1512
+ const attempt = attempts.find((c) => c.run?.parentSha === curSha) ?? attempts[0];
630
1513
  if (attempt)
631
- row("attempts", `${attempt.counterName} ${attempt.counterValue}`, "—");
1514
+ row("attempts", `${attempt.counter.name} ${attempt.counter.value}`, "—");
632
1515
  if (tableRows.length > 0) {
633
1516
  lines.push(`| metric | this run | med·${RUN_BASELINE_WINDOW} |`, "| --- | --- | --- |", ...tableRows, "");
634
1517
  }
635
- lines.push(`\`med·${RUN_BASELINE_WINDOW}\` = median of the previous ${RUN_BASELINE_WINDOW} ${workflow}${cur?.planSource ? ` ${cur.planSource}` : ""} runs (— if < ${RUN_BASELINE_MIN}) · ledger ${result.rowCount} rows, ${result.malformedLineCount} malformed`);
1518
+ lines.push(`\`med·${RUN_BASELINE_WINDOW}\` = median of the previous ${RUN_BASELINE_WINDOW} ${workflow}${converted(cur) ? " converted" : ""} runs (— if < ${RUN_BASELINE_MIN}) · ledger ${result.rowCount} rows, ${result.malformedLineCount} malformed`);
1519
+ if ([cur, ...prev].some(oldBrief))
1520
+ lines.push("Old `plan.source: brief` rows count as `converted`.");
636
1521
  lines.push(...maxRowsLines);
637
1522
  return { ...base, lines };
638
1523
  }