@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
@@ -6,37 +6,90 @@
6
6
  // order is the contract). Nothing declared is its own exit code, never an empty
7
7
  // command that exits 0 and reads as a clean run. A gate that exits 126 / 127 never
8
8
  // ran (not executable / not found), so it is not recorded as a failing test.
9
- //
10
- // bkit tests red / green — test-first evidence for build tasks. Both run one test
11
- // file through the consumer's `bkit:test:file` script and judge its JUnit report,
12
- // never its exit code alone: a misspelled path, a syntax error and an import of
13
- // unwritten code all exit non-zero exactly like a real failure. A red counts only
14
- // when the NAMED test ran and failed; green only when it ran and passed.
9
+ // The failure count comes from the JUnit reports the gate's runners write into
10
+ // `{log}.reports/` (`BKIT_TEST_REPORT_DIR`), never from reading the log.
11
+ // bkit tests red / green — test-first evidence for build tasks. `red` runs every TEST FIRST
12
+ // test of one group's open tasks after the tests step wrote them and before any task code
13
+ // exists: one run per test file through the consumer's `bkit:test:file` script, each test
14
+ // judged by the JUnit report, never the exit code alone (a misspelled path, a syntax error
15
+ // and an import of unwritten code all exit non-zero exactly like a real failure). A red is
16
+ // the NAMED test failing, or its file failing to load; `green` reads the group's red row and
17
+ // needs the named test to run and pass.
15
18
 
16
19
  import { spawnSync } from "node:child_process";
20
+ import { createHash } from "node:crypto";
17
21
  import * as fs from "node:fs";
18
22
  import * as os from "node:os";
19
23
  import * as path from "node:path";
20
24
 
21
- import { BKIT_TEST_FILE_SCRIPT } from "../../install/manifest.js";
22
- import { CommandError, EXIT, git, type CommandSpec } from "../lib.js";
25
+ import { BKIT_LINT_FILE_SCRIPT, BKIT_TEST_FILE_SCRIPT } from "../../install/manifest.js";
26
+ import { clampLogPath } from "../../ledgers/clamp-log.js";
27
+ import {
28
+ appendLine,
29
+ statusTimestamp,
30
+ validateCiSelection,
31
+ testRunProblem,
32
+ } from "../../ledgers/status.js";
33
+ import {
34
+ classifyLockExit,
35
+ CommandError,
36
+ EXIT,
37
+ git,
38
+ runGit,
39
+ testsLockHolder,
40
+ wholeNumber,
41
+ type CommandSpec,
42
+ } from "../lib.js";
43
+ import { parseJson } from "../../shared/json.js";
23
44
  import { readPackageJson } from "../../shared/package-json.js";
24
- import { COULD_NOT_RUN_EXIT_CODES, scriptCommand } from "../../shared/package-manager.js";
45
+ import {
46
+ COULD_NOT_RUN_EXIT_CODES,
47
+ scriptCommand,
48
+ testCommand,
49
+ } from "../../shared/package-manager.js";
50
+ import { CONFIG_DEFAULTS, readBkitConfig, type TaskCaps } from "../../shared/settings.js";
25
51
  import {
26
52
  FILE_FLAG,
27
- SUB_PLAN_FLAG,
53
+ GROUP_FLAG,
28
54
  WORKFLOW_FLAG,
29
55
  positiveInt,
30
56
  readStatusRows,
57
+ readRuns,
31
58
  statusFile,
32
59
  str,
33
60
  } from "./status.js";
61
+ import {
62
+ NO_MARKERS_REASON,
63
+ derivedTestMatrix,
64
+ fenceNeverClosed,
65
+ parsePlanBlocks,
66
+ reuseInventory,
67
+ supersessionInventory,
68
+ type PlanTaskBlock,
69
+ } from "./plan-blocks.js";
34
70
  import { writeStepRow } from "./steps.js";
71
+ import { appendRecord, clampFile, type ClampRecord } from "./context.js";
72
+ import { testsChangedVerb } from "./tests-changed.js";
35
73
 
36
74
  /** Exit code: no candidate gate is declared in package.json. */
37
75
  export const EXIT_NO_GATE = 3;
38
76
  /** Exit code: the gate could not run (exit 126 / 127, or the package runner is missing). */
39
77
  export const EXIT_COULD_NOT_RUN = 4;
78
+ /** Exit code: --artifact could not be written, trimmed or recorded. */
79
+ export const EXIT_ARTIFACT_STEP = 5;
80
+
81
+ /** The status row type `tests run --file` appends, one per run. */
82
+ export const TEST_RUN_ROW = "test-run";
83
+
84
+ /** The env var naming the folder every runner a gate calls writes its JUnit XML into. */
85
+ export const REPORT_DIR_ENV = "BKIT_TEST_REPORT_DIR";
86
+
87
+ export { classifyLockExit } from "../lib.js";
88
+
89
+ const NO_REPORT_HINT =
90
+ `no JUnit report in $${REPORT_DIR_ENV} — failures unknown; turn on each test runner's JUnit ` +
91
+ `reporter in the gate and point its output into $${REPORT_DIR_ENV} ` +
92
+ "(.bkit/workflows.md § Local gate scripts)";
40
93
 
41
94
  const DEFAULT_TAIL = 40;
42
95
 
@@ -76,7 +129,8 @@ export function runToLog(
76
129
  ): number {
77
130
  fs.mkdirSync(path.dirname(log), { recursive: true });
78
131
  const fd = fs.openSync(log, "w");
79
- const res = spawnSync(argv[0]!, argv.slice(1), {
132
+ const protectedArgv = testCommand(argv);
133
+ const res = spawnSync(protectedArgv[0]!, protectedArgv.slice(1), {
80
134
  cwd,
81
135
  stdio: ["ignore", fd, fd],
82
136
  env: env === undefined ? process.env : { ...process.env, ...env },
@@ -90,8 +144,84 @@ export function runToLog(
90
144
  return 128 + ((res.signal ? os.constants.signals[res.signal] : undefined) ?? 1);
91
145
  }
92
146
 
147
+ /** `{log}.reports`, emptied: a previous attempt's reports never count toward this one. */
148
+ function freshReportDir(log: string): string {
149
+ const dir = `${log}.reports`;
150
+ fs.rmSync(dir, { recursive: true, force: true });
151
+ fs.mkdirSync(dir, { recursive: true });
152
+ return dir;
153
+ }
154
+
155
+ /** Failing plus erroring test cases across every `*.xml` under `dir`; null when there is none. */
156
+ export function countReportFailures(dir: string): number | null {
157
+ const xml = (fs.readdirSync(dir, { recursive: true }) as string[]).filter(
158
+ (f) => f.endsWith(".xml") && fs.statSync(path.join(dir, f)).isFile(),
159
+ );
160
+ if (xml.length === 0) return null;
161
+ return xml
162
+ .flatMap((f) => parseJunit(fs.readFileSync(path.join(dir, f), "utf-8")))
163
+ .filter((c) => c.state === "failure" || c.state === "error").length;
164
+ }
165
+
93
166
  const tailLines = (file: string, n: number) =>
94
- fs.readFileSync(file, "utf-8").replace(/\n$/, "").split("\n").slice(-n);
167
+ fs
168
+ .readFileSync(file, "utf-8")
169
+ .replace(/\r?\n$/, "")
170
+ .split(/\r?\n/)
171
+ .slice(-n);
172
+
173
+ /**
174
+ * Append one `test-run` row to `file`. `run` numbers this step's test-run rows in the
175
+ * workflow's active run (since its last workflow-started row), this one included, so a
176
+ * gate re-run under the same attempt still shows.
177
+ */
178
+ function appendTestRun(
179
+ file: string,
180
+ r: {
181
+ workflow: string;
182
+ step: string;
183
+ attempt: number | undefined;
184
+ verdict: string | null;
185
+ exitCode: number | null;
186
+ failures: number | null;
187
+ scope: string;
188
+ log: string;
189
+ artifact: string | undefined;
190
+ source?: "ci" | "local";
191
+ evidence?: Record<string, unknown>;
192
+ },
193
+ ): Record<string, unknown> {
194
+ const rows = readStatusRows(file);
195
+ let start = -1;
196
+ rows.forEach((x, i) => {
197
+ if (x.type === "workflow-started" && x.workflow === r.workflow) start = i;
198
+ });
199
+ const run =
200
+ rows
201
+ .slice(start + 1)
202
+ .filter((x) => x.type === TEST_RUN_ROW && x.workflow === r.workflow && x.step === r.step)
203
+ .length + 1;
204
+ const row = {
205
+ type: TEST_RUN_ROW,
206
+ workflow: r.workflow,
207
+ at: statusTimestamp(),
208
+ step: r.step,
209
+ ...(r.attempt === undefined ? {} : { attempt: r.attempt }),
210
+ run,
211
+ verdict: r.verdict,
212
+ exitCode: r.exitCode,
213
+ failures: r.failures,
214
+ scope: r.scope,
215
+ log: r.log,
216
+ ...(r.artifact === undefined ? {} : { artifact: r.artifact }),
217
+ ...(r.source === undefined ? {} : { source: r.source }),
218
+ ...r.evidence,
219
+ };
220
+ const problem = testRunProblem(row);
221
+ if (problem !== null) throw new CommandError(EXIT.USAGE, problem);
222
+ fs.appendFileSync(file, appendLine(row));
223
+ return row;
224
+ }
95
225
 
96
226
  // ---------------------------------------------------------------------------
97
227
  // Test-first: JUnit reading, plan `Test first:` lines, red / green judgement
@@ -99,8 +229,310 @@ const tailLines = (file: string, n: number) =>
99
229
 
100
230
  /** The env var naming the JUnit report path `bkit:test:file` must write. */
101
231
  export const REPORT_ENV = "BKIT_TEST_REPORT";
102
- /** The status row step a recorded red writes. */
103
- export const RED_STEP = "build-task-red";
232
+ /** The status row step a group's red writes: one row per `tests red` run, a state and blob per planned test. */
233
+ export const RED_STEP = "build-tests-red";
234
+
235
+ /** How a planned test stood at the group's red. */
236
+ export type RedState =
237
+ | "red"
238
+ | "red-not-loaded"
239
+ | "passed-accepted"
240
+ | "updated"
241
+ | "vacuous"
242
+ | "missing"
243
+ | "no-report"
244
+ | "lint"
245
+ | "unknown";
246
+
247
+ /** One planned test on a red row: its state, and the blob of its file as it stood then. */
248
+ export interface RedTest {
249
+ task: number;
250
+ test: string;
251
+ testName: string;
252
+ state: RedState;
253
+ /** `git hash-object` of the test file, written to the object store; null when the file is missing. */
254
+ blob: string | null;
255
+ /**
256
+ * Hash of the test's own lines (`testBlock`) as they stood; null when the file holds no
257
+ * block for the name. Null, and absent on rows written before per-test locking, lock by `blob`.
258
+ */
259
+ block?: string | null;
260
+ /** The tests step's reason, on `passed-accepted`. */
261
+ reason?: string;
262
+ /** On `updated`: the commit the group started at, where the test already existed. */
263
+ base?: string;
264
+ }
265
+
266
+ /**
267
+ * The states that count as a red: the test failed, its file did not load, the tests step
268
+ * vouched for its pass, or it is an existing test updated and passing, whose diff the
269
+ * verifier judges (it may add expectations, never remove or loosen one).
270
+ */
271
+ export const RED_PROVEN: ReadonlySet<RedState> = new Set([
272
+ "red",
273
+ "red-not-loaded",
274
+ "passed-accepted",
275
+ "updated",
276
+ ]);
277
+
278
+ /** The `build-tests-red` rows of `workflow` in status.jsonl, in file order; `group` narrows. */
279
+ function redRows(file: string, workflow: string, group?: number): Record<string, unknown>[] {
280
+ if (!fs.existsSync(file)) return [];
281
+ return (readStatusRows(file) as Array<Record<string, unknown>>).filter(
282
+ (r) =>
283
+ r.type === "step-done" &&
284
+ r.workflow === workflow &&
285
+ r.step === RED_STEP &&
286
+ typeof r.group === "number" &&
287
+ (group === undefined || r.group === group),
288
+ );
289
+ }
290
+
291
+ /** A red row's `tests`, as written. */
292
+ export const redTestsOf = (row: Record<string, unknown>): RedTest[] =>
293
+ Array.isArray(row.tests) ? (row.tests as RedTest[]) : [];
294
+
295
+ /** The key a planned test is looked up by: task numbers are unique across the plan. */
296
+ export const redKey = (task: number, test: string, name: string) =>
297
+ JSON.stringify([task, test, name]);
298
+
299
+ /** The number this group's next red row carries: its rows so far, plus one. */
300
+ function redAttempt(file: string, workflow: string, group: number): number {
301
+ if (!fs.existsSync(file)) return 1;
302
+ return (
303
+ readStatusRows(file).filter((r) => {
304
+ const row = r as Record<string, unknown>;
305
+ return (
306
+ row.type === "step-done" &&
307
+ row.workflow === workflow &&
308
+ row.step === RED_STEP &&
309
+ row.group === group
310
+ );
311
+ }).length + 1
312
+ );
313
+ }
314
+
315
+ /**
316
+ * Every planned test the red rows recorded, keyed by task, file and name. A later row's entry
317
+ * wins, so a retry or a later task of the group overrides only the tests it ran, except that a
318
+ * proven red is never replaced by an unproven result for the same test lines. `group` narrows
319
+ * to one group.
320
+ */
321
+ export function redEntries(file: string, workflow: string, group?: number): Map<string, RedTest> {
322
+ const out = new Map<string, RedTest>();
323
+ for (const row of redRows(file, workflow, group))
324
+ for (const t of redTestsOf(row)) {
325
+ const key = redKey(t.task, t.test, t.testName);
326
+ const earlier = out.get(key);
327
+ const keepsProof =
328
+ earlier !== undefined &&
329
+ RED_PROVEN.has(earlier.state) &&
330
+ !RED_PROVEN.has(t.state) &&
331
+ typeof earlier.block === "string" &&
332
+ earlier.block === t.block;
333
+ if (!keepsProof) out.set(key, t);
334
+ }
335
+ return out;
336
+ }
337
+
338
+ /** A planned test that differs from how the group's tests step left it. */
339
+ export interface TouchedTest {
340
+ test: string;
341
+ name: string;
342
+ }
343
+
344
+ /**
345
+ * The planned tests of `group` a code step changed since their red: a test whose own lines
346
+ * (`testBlock`) no longer hash to the recorded block. A row with no block (written before
347
+ * per-test locking, or when no line carried the name) derives the block from the recorded
348
+ * blob; when that holds no block either, the whole file's blob is compared. Other edits to a
349
+ * test file are free.
350
+ */
351
+ export function touchedTests(
352
+ top: string,
353
+ file: string,
354
+ workflow: string,
355
+ group: number,
356
+ ): TouchedTest[] {
357
+ const touched: TouchedTest[] = [];
358
+ const texts = new Map<string, string | null>();
359
+ const textOf = (test: string) => {
360
+ if (!texts.has(test)) {
361
+ const abs = path.join(top, test);
362
+ texts.set(test, fs.existsSync(abs) ? fs.readFileSync(abs, "utf-8") : null);
363
+ }
364
+ return texts.get(test)!;
365
+ };
366
+ for (const t of redEntries(file, workflow, group).values()) {
367
+ let block = t.block ?? null;
368
+ if (block === null) block = blockHash(blobText(top, t.blob), t.testName);
369
+ if (block === null) {
370
+ if (git(top, ["hash-object", "--", t.test]) !== t.blob)
371
+ touched.push({ test: t.test, name: t.testName });
372
+ continue;
373
+ }
374
+ if (blockHash(textOf(t.test), t.testName) !== block)
375
+ touched.push({ test: t.test, name: t.testName });
376
+ }
377
+ return touched;
378
+ }
379
+
380
+ /** The text a recorded blob holds, untrimmed; null when the object store lacks it. */
381
+ function blobText(top: string, blob: string | null): string | null {
382
+ if (blob === null) return null;
383
+ const res = spawnSync("git", ["cat-file", "-p", blob], { cwd: top, encoding: "utf-8" });
384
+ return res.status === 0 ? res.stdout : null;
385
+ }
386
+
387
+ /** Leading whitespace width, a tab counting as one column. */
388
+ const indentOf = (line: string) => line.length - line.trimStart().length;
389
+
390
+ /** A line that only closes the block opened at its own indent. */
391
+ const CLOSER = /^(?:[)\]}]|end\b)/;
392
+
393
+ const escapeRegExp = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
394
+
395
+ /** A comment line: a name in it is prose, not the test. */
396
+ const COMMENT = /^(?:\/\/|\/\*|\*|#|--|<!--)/;
397
+
398
+ /** A line opening a suite (`describe`, `context`, `suite`, a class), not a test. */
399
+ const SUITE = /^(?:[\w$]+\.)?(?:describe|context|suite|class)\b/;
400
+
401
+ /**
402
+ * The first line `matches` that is no comment and opens no suite; else the first matching
403
+ * suite line, so a suite that alone carries the name still yields a block; else -1.
404
+ */
405
+ function testStart(lines: string[], matches: (line: string) => boolean): number {
406
+ let suite = -1;
407
+ for (let i = 0; i < lines.length; i++) {
408
+ const line = lines[i]!.trimStart();
409
+ if (!matches(lines[i]!) || COMMENT.test(line)) continue;
410
+ if (!SUITE.test(line)) return i;
411
+ if (suite < 0) suite = i;
412
+ }
413
+ return suite;
414
+ }
415
+
416
+ /**
417
+ * The lines of the test named `name` in `text`, or null when no line names it. The test
418
+ * starts on the first line naming it as a quoted string (else as a whole word) that is no
419
+ * comment and opens no suite, or on the line before when the name opens its line and that
420
+ * line ends with `(`; it runs through every deeper-indented line after the start, and one
421
+ * closing line (`}`, `)`, `]`, `end`) at the start's indent. Framework-agnostic: it needs
422
+ * only a name and indentation.
423
+ */
424
+ export function testBlock(text: string | null, name: string): string | null {
425
+ if (text === null) return null;
426
+ const span = testSpan(text, name);
427
+ return span === null
428
+ ? null
429
+ : text
430
+ .split("\n")
431
+ .slice(span.start - 1, span.end)
432
+ .join("\n");
433
+ }
434
+
435
+ /**
436
+ * The 1-based first and last line of the test named `name` in `text` (the lines
437
+ * `testBlock` returns), or null when no line names it.
438
+ */
439
+ export function testSpan(text: string, name: string): { start: number; end: number } | null {
440
+ const lines = text.split("\n");
441
+ const quoted = [`"${name}"`, `'${name}'`, `\`${name}\``];
442
+ let at = testStart(lines, (l) => quoted.some((q) => l.includes(q)));
443
+ if (at < 0) {
444
+ const word = new RegExp(`(^|[^\\w])${escapeRegExp(name)}([^\\w]|$)`);
445
+ at = testStart(lines, (l) => word.test(l));
446
+ }
447
+ if (at < 0) {
448
+ // A suite path (`suite > test`, as a JUnit report names it): the last segment is the test.
449
+ const last = name.split(" > ").pop()!;
450
+ return last === name ? null : testSpan(text, last);
451
+ }
452
+ let start = at;
453
+ while (
454
+ start > 0 &&
455
+ /^["'`]/.test(lines[start]!.trimStart()) &&
456
+ /\($/.test(lines[start - 1]!.trimEnd())
457
+ )
458
+ start -= 1;
459
+ const base = indentOf(lines[start]!);
460
+ let end = start;
461
+ for (let i = start + 1; i < lines.length; i++) {
462
+ const line = lines[i]!;
463
+ if (line.trim() === "") continue;
464
+ if (indentOf(line) > base) {
465
+ end = i;
466
+ continue;
467
+ }
468
+ if (indentOf(line) === base && CLOSER.test(line.trimStart())) end = i;
469
+ break;
470
+ }
471
+ return { start: start + 1, end: end + 1 };
472
+ }
473
+
474
+ /**
475
+ * The line diff of a test's own lines, `before` to `after`: unified lines (` `, `-`, `+`)
476
+ * over a longest common subsequence, and the counts of lines removed and added. A change
477
+ * that removes no line cannot have dropped an expectation; it can still have disabled one,
478
+ * which only a read of the added lines tells.
479
+ */
480
+ export function lineDiff(
481
+ before: string,
482
+ after: string,
483
+ ): { text: string; removed: number; added: number } {
484
+ const a = before.split("\n");
485
+ const b = after.split("\n");
486
+ const lcs: number[][] = Array.from({ length: a.length + 1 }, () =>
487
+ new Array<number>(b.length + 1).fill(0),
488
+ );
489
+ for (let i = a.length - 1; i >= 0; i--)
490
+ for (let j = b.length - 1; j >= 0; j--)
491
+ lcs[i]![j] =
492
+ a[i] === b[j] ? lcs[i + 1]![j + 1]! + 1 : Math.max(lcs[i + 1]![j]!, lcs[i]![j + 1]!);
493
+ const out: string[] = [];
494
+ let removed = 0;
495
+ let added = 0;
496
+ let i = 0;
497
+ let j = 0;
498
+ while (i < a.length || j < b.length) {
499
+ if (i < a.length && j < b.length && a[i] === b[j]) {
500
+ out.push(` ${a[i]}`);
501
+ i++;
502
+ j++;
503
+ } else if (i < a.length && (j >= b.length || lcs[i + 1]![j]! >= lcs[i]![j + 1]!)) {
504
+ out.push(`-${a[i]}`);
505
+ removed++;
506
+ i++;
507
+ } else {
508
+ out.push(`+${b[j]}`);
509
+ added++;
510
+ j++;
511
+ }
512
+ }
513
+ return { text: out.join("\n"), removed, added };
514
+ }
515
+
516
+ /** The sha-1 of a test's own lines (`testBlock`), or null when `text` holds no such test. */
517
+ export function blockHash(text: string | null, name: string): string | null {
518
+ const block = testBlock(text, name);
519
+ return block === null ? null : createHash("sha1").update(block).digest("hex");
520
+ }
521
+
522
+ /** The commit the group started at: its `build-group-start` row's `parentSha`. */
523
+ function groupStartSha(file: string, workflow: string, group: number): string | undefined {
524
+ if (!fs.existsSync(file)) return undefined;
525
+ const row = (readStatusRows(file) as Array<Record<string, unknown>>)
526
+ .filter(
527
+ (r) =>
528
+ r.type === "step-done" &&
529
+ r.workflow === workflow &&
530
+ r.step === "build-group-start" &&
531
+ r.group === group,
532
+ )
533
+ .at(-1);
534
+ return typeof row?.parentSha === "string" ? row.parentSha : undefined;
535
+ }
104
536
 
105
537
  const NO_TEST_FILE_HINT =
106
538
  `declare "${BKIT_TEST_FILE_SCRIPT}" in package.json: it runs the one test file passed as its ` +
@@ -111,7 +543,10 @@ export type CaseState = "passed" | "failure" | "error" | "skipped";
111
543
  export interface JunitCase {
112
544
  name: string;
113
545
  state: CaseState;
546
+ /** The failure element's `message` attribute: the runner's one-line summary. */
114
547
  message: string;
548
+ /** The failure element's text: the assertion output, where some runners put the detail. */
549
+ detail: string;
115
550
  }
116
551
 
117
552
  const XML_ENTITIES: Record<string, string> = {
@@ -137,18 +572,27 @@ function attr(attrs: string, name: string): string | undefined {
137
572
  return m ? decodeXml(m[1] ?? m[2] ?? "") : undefined;
138
573
  }
139
574
 
140
- /** Every `<testcase>` in a JUnit report: its name, outcome and first failure message. */
575
+ /** CDATA text as escaped character data, so a tag inside it never ends an element early. */
576
+ const escapeCdata = (xml: string) =>
577
+ xml.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, (_, text: string) =>
578
+ text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;"),
579
+ );
580
+
581
+ /** Every `<testcase>` in a JUnit report: its name, outcome, first failure message and detail. */
141
582
  export function parseJunit(xml: string): JunitCase[] {
142
- const body = xml.replace(/<!\[CDATA\[[\s\S]*?\]\]>/g, "").replace(/<!--[\s\S]*?-->/g, "");
583
+ const body = escapeCdata(xml).replace(/<!--[\s\S]*?-->/g, "");
143
584
  const cases: JunitCase[] = [];
144
585
  const testcase = new RegExp(String.raw`<testcase${ATTRS}\s*(?:\/>|>([\s\S]*?)<\/testcase>)`, "g");
145
- const outcome = new RegExp(String.raw`<(failure|error|skipped)${ATTRS}`);
586
+ const outcome = new RegExp(
587
+ String.raw`<(failure|error|skipped)${ATTRS}\s*(?:\/>|>([\s\S]*?)<\/\1>)`,
588
+ );
146
589
  for (const m of body.matchAll(testcase)) {
147
590
  const child = outcome.exec(m[2] ?? "");
148
591
  cases.push({
149
592
  name: attr(m[1] ?? "", "name") ?? "",
150
593
  state: child ? (child[1] as CaseState) : "passed",
151
594
  message: child ? (attr(child[2] ?? "", "message") ?? "") : "",
595
+ detail: child ? decodeXml((child[3] ?? "").replace(/<[^>]*>/g, "")).trim() : "",
152
596
  });
153
597
  }
154
598
  return cases;
@@ -166,36 +610,51 @@ const caseNames = (cases: JunitCase[]) =>
166
610
  .map((c) => `"${c.name}"`)
167
611
  .join(", ")}${cases.length > 5 ? ", …" : ""}`;
168
612
 
169
- /** Why a run is not a red: `passed` means the code under test already exists. */
170
- export type RedRefusal = "no-report" | "not-run" | "passed" | "skipped" | "exit-mismatch";
613
+ /** The stem a load error names: the test file's base name without its extension. */
614
+ const fileStem = (file: string) => path.posix.basename(file).replace(/\.[^.]*$/, "");
615
+
616
+ /** The red states `judgeRed` tells apart. */
617
+ export type JudgedRed = Extract<
618
+ RedState,
619
+ "red" | "red-not-loaded" | "vacuous" | "missing" | "no-report"
620
+ >;
171
621
 
172
- /** Why a run is not a valid red for `want` (kind + reason), or null when it is one. */
622
+ /**
623
+ * How `want` stood in a red run of `file`, from its JUnit report: `red` (the named test
624
+ * failed or errored), `red-not-loaded` (the file ran no test, or the only cases are failures
625
+ * named after the file itself: a load error, which an import of unwritten code is),
626
+ * `vacuous` (the named test passed, so it proves nothing before the task's code), `missing`
627
+ * (it did not run, or was skipped) or `no-report`.
628
+ */
173
629
  export function judgeRed(
174
- exitCode: number,
175
630
  cases: JunitCase[] | null,
176
631
  want: string,
177
- ): { kind: RedRefusal; reason: string } | null {
632
+ file: string,
633
+ ): { state: JudgedRed; detail: string } {
178
634
  if (cases === null)
179
635
  return {
180
- kind: "no-report",
181
- reason: `no JUnit report at $${REPORT_ENV} — ${BKIT_TEST_FILE_SCRIPT} must write one`,
636
+ state: "no-report",
637
+ detail: `no JUnit report at $${REPORT_ENV} — ${BKIT_TEST_FILE_SCRIPT} must write one`,
182
638
  };
183
639
  const named = cases.filter((c) => isNamedCase(c.name, want));
184
- if (named.length === 0)
185
- return { kind: "not-run", reason: `the named test "${want}" did not run; ${caseNames(cases)}` };
640
+ if (named.length === 0) {
641
+ const stem = fileStem(file);
642
+ if (cases.length === 0 || cases.every((c) => c.state !== "passed" && c.name.includes(stem)))
643
+ return {
644
+ state: "red-not-loaded",
645
+ detail: `\`${file}\` did not load${cases[0]?.message ? `: ${cases[0].message}` : ""}`,
646
+ };
647
+ return { state: "missing", detail: `"${want}" did not run; ${caseNames(cases)}` };
648
+ }
186
649
  if (named.some((c) => c.state === "passed"))
187
650
  return {
188
- kind: "passed",
189
- reason: `"${want}" passed — a red must fail before the task's code exists`,
651
+ state: "vacuous",
652
+ detail: `"${want}" passes at its red, so it proves nothing`,
190
653
  };
191
654
  if (named.some((c) => c.state === "skipped"))
192
- return { kind: "skipped", reason: `"${want}" was skipped, not run` };
193
- if (exitCode === 0)
194
- return {
195
- kind: "exit-mismatch",
196
- reason: `"${want}" failed in the report but the script exited 0`,
197
- };
198
- return null;
655
+ return { state: "missing", detail: `"${want}" was skipped, not run` };
656
+ const failed = named.find((c) => c.state !== "passed")!;
657
+ return { state: "red", detail: `${failed.state}${failed.message ? `: ${failed.message}` : ""}` };
199
658
  }
200
659
 
201
660
  /** Why a run is not green for `want`, or null when every case of the named test passed. */
@@ -215,78 +674,448 @@ export interface PlannedTest {
215
674
  name: string;
216
675
  }
217
676
 
677
+ /** A human-recorded acceptance of a task's missing reds: the tests it covers, its 1-based plan lines. */
678
+ export interface TestOrderException {
679
+ tests: PlannedTest[];
680
+ lines: number[];
681
+ }
682
+
218
683
  export type TestFirst =
219
- | { task: number; kind: "test"; tests: PlannedTest[] }
684
+ | { task: number; kind: "test"; tests: PlannedTest[]; exception?: TestOrderException }
220
685
  | { task: number; kind: "na"; reason: string }
221
686
  | { task: number; kind: "missing" | "malformed" };
222
687
 
223
- const TASK_LINE = /^\s*- \[[ xX]\] \*\*Task (\d+):\*\*/;
224
- const TEST_FIRST = "**Test first:**";
688
+ /** The task field naming a task's tests, and the one recording an accepted missing red. */
689
+ export const TEST_FIRST = "**TEST FIRST:**";
690
+ export const TEST_EXCEPTION = "**TEST EXCEPTION:**";
225
691
 
226
- /** `text` split on `;` outside backticks. */
227
- function splitEntries(text: string): string[] {
228
- const out: string[] = [];
229
- let cur = "";
230
- let quoted = false;
231
- for (const ch of text) {
232
- if (ch === "`") quoted = !quoted;
233
- if (ch === ";" && !quoted) {
234
- out.push(cur);
235
- cur = "";
236
- } else cur += ch;
692
+ /** The plan caps `package.json` `bkit.workflows.tasks` sets for the checkout `cwd` sits in. */
693
+ export const planCaps = (cwd: string): TaskCaps => readBkitConfig(toplevel(cwd)).tasks;
694
+
695
+ /** The caps as the plan-check report and help state them. */
696
+ export const capsText = (c: TaskCaps) =>
697
+ `${c.maxPerGroup} tasks and ${c.maxTestsPerGroup} TEST FIRST tests per group; ${c.maxTestsPerTask} tests across ${c.maxTestFilesPerTask} test files per task`;
698
+
699
+ /** A task block's TEST FIRST read as one `TestFirst`; a TEST EXCEPTION with no TEST FIRST is its `n/a`. */
700
+ /**
701
+ * Planned tests of open tasks whose name a test in their file already carries: the tests
702
+ * step cannot write a new test under that name, so it either updates the existing one or
703
+ * finds it passing. Reported only — the plan names the update, or a new name.
704
+ */
705
+ function existingTestNames(
706
+ text: string,
707
+ top: string,
708
+ ): Array<{ line: number; task: number; test: string; name: string }> {
709
+ const out: Array<{ line: number; task: number; test: string; name: string }> = [];
710
+ for (const t of parsePlanBlocks(text).tasks) {
711
+ if (t.done) continue;
712
+ const s = testFirstOf(t);
713
+ if (s.kind !== "test") continue;
714
+ for (const x of s.tests) {
715
+ const abs = path.join(top, x.test);
716
+ if (!fs.existsSync(abs)) continue;
717
+ if (testBlock(fs.readFileSync(abs, "utf-8"), x.name) !== null)
718
+ out.push({ line: t.testFirst?.line ?? t.line, task: t.task, test: x.test, name: x.name });
719
+ }
237
720
  }
238
- return [...out, cur];
721
+ return out;
722
+ }
723
+
724
+ function testFirstOf(t: PlanTaskBlock): TestFirst {
725
+ const { task, testFirst: tf, testException: exc } = t;
726
+ const plain = (tests: PlannedTest[]) => tests.map(({ test, name }) => ({ test, name }));
727
+ if (tf === undefined)
728
+ return exc === undefined ? { task, kind: "missing" } : { task, kind: "na", reason: exc.text };
729
+ if (tf.malformed.length > 0 || (tf.tests.length === 0 && tf.na === undefined))
730
+ return { task, kind: "malformed" };
731
+ if (tf.tests.length === 0) return { task, kind: "na", reason: tf.na! };
732
+ const tests = plain(tf.tests);
733
+ return exc === undefined || exc.tests.length === 0
734
+ ? { task, kind: "test", tests }
735
+ : { task, kind: "test", tests, exception: { tests: plain(exc.tests), lines: exc.lines } };
239
736
  }
240
737
 
241
738
  /**
242
- * Each runbook task's `Test first:` line: one or more tests separated by `;` (per entry,
243
- * first backticked span = file, second = test name), `n/a — {reason}`, missing, or
244
- * malformed (an entry with one span, or no test at all). A task's text runs from its
245
- * bullet to the next bullet or heading.
739
+ * Each task block's TEST FIRST field (`plan-blocks.ts`): one test per sub-bullet (first
740
+ * backticked span = file, second = test name), `n/a — {reason}`, missing, or malformed
741
+ * (an entry with fewer than two spans, or no entry). A TEST EXCEPTION field names, in
742
+ * the same entry form, the tests whose missing reds it accepts. `group` narrows to
743
+ * one group's tasks.
246
744
  */
247
- export function readTestFirst(plan: string): TestFirst[] {
248
- const out: TestFirst[] = [];
249
- const lines = plan.split("\n");
250
- for (let i = 0; i < lines.length; i++) {
251
- const head = TASK_LINE.exec(lines[i]!);
252
- if (!head) continue;
253
- let text = lines[i]!;
254
- for (let j = i + 1; j < lines.length && !/^\s*(- |#)/.test(lines[j]!); j++)
255
- text += ` ${lines[j]}`;
256
- const task = Number(head[1]);
257
- const at = text.indexOf(TEST_FIRST);
258
- if (at < 0) {
259
- out.push({ task, kind: "missing" });
260
- continue;
745
+ export function readTestFirst(plan: string, group?: number): TestFirst[] {
746
+ return parsePlanBlocks(plan)
747
+ .tasks.filter((t) => group === undefined || t.group === group)
748
+ .map(testFirstOf);
749
+ }
750
+
751
+ export interface TaskSize {
752
+ task: number;
753
+ group: number;
754
+ tests: number;
755
+ files: number;
756
+ }
757
+
758
+ /** Per task, the planned tests and distinct test files it names (`n/a`, missing and malformed count 0). */
759
+ export const taskSizes = (specs: TestFirst[]): Array<Omit<TaskSize, "group">> =>
760
+ specs.map((s) =>
761
+ s.kind === "test"
762
+ ? { task: s.task, tests: s.tests.length, files: new Set(s.tests.map((t) => t.test)).size }
763
+ : { task: s.task, tests: 0, files: 0 },
764
+ );
765
+
766
+ export interface GroupSize {
767
+ group: number;
768
+ title: string;
769
+ tasks: number;
770
+ /** Planned TEST FIRST tests, summed over the group's tasks. */
771
+ tests: number;
772
+ /** The group block's size, KB to one decimal. */
773
+ kb: number;
774
+ }
775
+
776
+ export interface PlanProblem {
777
+ /** 1-based plan line. */
778
+ line: number;
779
+ kind:
780
+ | "format"
781
+ | "stray"
782
+ | "field"
783
+ | "depends"
784
+ | "cap"
785
+ | "inventory"
786
+ | "matrix"
787
+ | "supersession"
788
+ | "markers";
789
+ message: string;
790
+ }
791
+
792
+ export interface PlanCheck {
793
+ groups: GroupSize[];
794
+ sizes: TaskSize[];
795
+ /** Tasks naming more than `maxTestsPerTask` tests or `maxTestFilesPerTask` test files. */
796
+ overCap: TaskSize[];
797
+ /** Every reason the plan is refused, in line order; the existence check is `planExistence`. */
798
+ problems: PlanProblem[];
799
+ }
800
+
801
+ /**
802
+ * Files with a NEW row in the plan's Reuse Inventory table (`reuseInventory`): the
803
+ * paths in the `File` cell of each row whose `Decision` cell reads NEW.
804
+ */
805
+ export function inventoryNewFiles(text: string): Set<string> {
806
+ const rows = reuseInventory(text)?.rows ?? [];
807
+ return new Set(rows.filter((r) => /^\**new\b/i.test(r.decision)).flatMap((r) => r.files));
808
+ }
809
+
810
+ const kb1 = (bytes: number) => Math.round((bytes / 1024) * 10) / 10;
811
+
812
+ /**
813
+ * One plan's groups and tasks against the caps and the block format: parse errors and
814
+ * task-like text outside every block, a task without DONE WHEN or without TEST FIRST and
815
+ * TEST EXCEPTION, a malformed TEST FIRST, a DEPENDS ON naming a missing task or one in a
816
+ * later group, a group or task over a cap in `caps`, a CREATE file with no NEW Reuse
817
+ * Inventory row, a `Derived Test Matrix` row naming no task / an undefined task / two
818
+ * tasks or a test-bearing task no row names, and a DELETE file with no Supersession row.
819
+ */
820
+ export function checkPlanText(text: string, caps: TaskCaps = CONFIG_DEFAULTS.tasks): PlanCheck {
821
+ const blocks = parsePlanBlocks(text);
822
+ const lines = text.split(/\r?\n/);
823
+ const problems: PlanProblem[] = blocks.errors.map((e) => ({
824
+ line: e.line,
825
+ kind: e.kind,
826
+ message: e.message,
827
+ }));
828
+ if (blocks.tasks.length === 0 && blocks.fenceOpen === undefined)
829
+ problems.push({
830
+ line: 1,
831
+ kind: "markers",
832
+ message:
833
+ "no task markers: write each task as a `<!-- bkit:task N start -->` block inside a `<!-- bkit:group N start -->` block",
834
+ });
835
+ const specs = new Map(blocks.tasks.map((t) => [t.task, testFirstOf(t)]));
836
+ const sizes: TaskSize[] = blocks.tasks.map((t) => {
837
+ const s = specs.get(t.task)!;
838
+ const tests = s.kind === "test" ? s.tests : [];
839
+ return {
840
+ task: t.task,
841
+ group: t.group,
842
+ tests: tests.length,
843
+ files: new Set(tests.map((x) => x.test)).size,
844
+ };
845
+ });
846
+ const overCap = sizes.filter(
847
+ (t) => t.tests > caps.maxTestsPerTask || t.files > caps.maxTestFilesPerTask,
848
+ );
849
+ const byTask = new Map(blocks.tasks.map((t) => [t.task, t]));
850
+ const newFiles = inventoryNewFiles(text);
851
+ for (const t of blocks.tasks) {
852
+ const field = (message: string, line = t.line) =>
853
+ problems.push({ line, kind: "field", message: `TASK ${t.task}: ${message}` });
854
+ if (t.doneWhen === undefined || t.doneWhen === "") field("has no **DONE WHEN:** field");
855
+ const s = specs.get(t.task)!;
856
+ if (s.kind === "missing") field("has neither **TEST FIRST:** nor **TEST EXCEPTION:**");
857
+ for (const line of t.testFirst?.malformed ?? [])
858
+ field("TEST FIRST entry is neither `test file` `test name` nor `n/a — {reason}`", line);
859
+ if (s.kind === "malformed" && (t.testFirst?.malformed.length ?? 0) === 0)
860
+ field("TEST FIRST names no test", t.testFirst!.line);
861
+ for (const d of t.dependsOn) {
862
+ const dep = byTask.get(d);
863
+ const message =
864
+ d === t.task
865
+ ? `TASK ${t.task} depends on itself`
866
+ : dep === undefined
867
+ ? `TASK ${t.task} depends on TASK ${d}, but TASK ${d} does not exist`
868
+ : dep.group > t.group
869
+ ? `TASK ${t.task} depends on TASK ${d}, which is in a later group (GROUP ${dep.group}); groups build in order`
870
+ : undefined;
871
+ if (message !== undefined) problems.push({ line: t.line, kind: "depends", message });
261
872
  }
262
- const rest = text.slice(at + TEST_FIRST.length).trim();
263
- if (/^n\/a\b/i.test(rest)) {
264
- out.push({ task, kind: "na", reason: rest.replace(/^n\/a\s*[—–-]?\s*/i, "") });
265
- continue;
873
+ const size = sizes.find((x) => x.task === t.task)!;
874
+ if (size.tests > caps.maxTestsPerTask)
875
+ problems.push({
876
+ line: t.line,
877
+ kind: "cap",
878
+ message: `TASK ${t.task} names ${size.tests} TEST FIRST tests, over the cap of ${caps.maxTestsPerTask}`,
879
+ });
880
+ if (size.files > caps.maxTestFilesPerTask)
881
+ problems.push({
882
+ line: t.line,
883
+ kind: "cap",
884
+ message: `TASK ${t.task} names ${size.files} test files, over the cap of ${caps.maxTestFilesPerTask}`,
885
+ });
886
+ for (const c of t.create)
887
+ if (!newFiles.has(path.posix.normalize(c.file)))
888
+ problems.push({
889
+ line: c.line,
890
+ kind: "inventory",
891
+ message: `TASK ${t.task}: CREATE in \`${c.file}\` has no NEW row naming it in the Reuse Inventory's File column`,
892
+ });
893
+ }
894
+ // The matrix maps each row to one task and each test-bearing task to a row. A plan with no
895
+ // matrix section yields no problem: the refusal is on a row, and issue-form plans carry none.
896
+ const matrix = derivedTestMatrix(text);
897
+ if (matrix !== null) {
898
+ if (matrix.taskColumn === null)
899
+ problems.push({
900
+ line: matrix.line,
901
+ kind: "matrix",
902
+ message:
903
+ "the Derived Test Matrix header has no `Task` column; add one naming the task each row's test belongs to",
904
+ });
905
+ else {
906
+ const mapped = new Set<number>();
907
+ for (const row of matrix.rows) {
908
+ const named = row.tasks.map((n) => `TASK ${n}`);
909
+ if (row.tasks.length === 0)
910
+ problems.push({
911
+ line: row.number,
912
+ kind: "matrix",
913
+ message: `Derived Test Matrix row maps to no task: name one in its \`Task\` cell`,
914
+ });
915
+ else if (row.tasks.length > 1)
916
+ problems.push({
917
+ line: row.number,
918
+ kind: "matrix",
919
+ message: `Derived Test Matrix row maps to ${named.join(" and ")}; name exactly one task`,
920
+ });
921
+ else {
922
+ const n = row.tasks[0]!;
923
+ if (!byTask.has(n))
924
+ problems.push({
925
+ line: row.number,
926
+ kind: "matrix",
927
+ message: `Derived Test Matrix row maps to TASK ${n}, which the plan does not define`,
928
+ });
929
+ mapped.add(n);
930
+ }
931
+ }
932
+ for (const t of blocks.tasks)
933
+ if (specs.get(t.task)!.kind === "test" && !mapped.has(t.task))
934
+ problems.push({
935
+ line: t.line,
936
+ kind: "matrix",
937
+ message: `TASK ${t.task} names TEST FIRST tests but no Derived Test Matrix row maps to it`,
938
+ });
266
939
  }
267
- const entries = splitEntries(rest).map((e) =>
268
- [...e.matchAll(/`([^`]+)`/g)].map((m) => m[1]!.trim()),
269
- );
270
- const tests = entries
271
- .filter((spans) => spans.length >= 2)
272
- .map((spans) => ({ test: path.posix.normalize(spans[0]!), name: spans[1]! }));
273
- out.push(
274
- tests.length > 0 && !entries.some((spans) => spans.length === 1)
275
- ? { task, kind: "test", tests }
276
- : { task, kind: "malformed" },
277
- );
278
940
  }
279
- return out;
941
+ const superseded = new Set((supersessionInventory(text) ?? []).flatMap((r) => r.files));
942
+ for (const t of blocks.tasks)
943
+ for (const d of t.delete)
944
+ if (!superseded.has(path.posix.normalize(d.file)))
945
+ problems.push({
946
+ line: d.line,
947
+ kind: "supersession",
948
+ message: `TASK ${t.task}: DELETE in \`${d.file}\` has no row naming it in the Supersession Inventory`,
949
+ });
950
+ const groups: GroupSize[] = blocks.groups.map((g) => {
951
+ const tests = sizes.filter((s) => s.group === g.group).reduce((n, s) => n + s.tests, 0);
952
+ const bytes = Buffer.byteLength(lines.slice(g.line - 1, g.end).join("\n"));
953
+ return { group: g.group, title: g.title, tasks: g.tasks.length, tests, kb: kb1(bytes) };
954
+ });
955
+ for (const g of groups) {
956
+ const at = blocks.groups.find((x) => x.group === g.group)!.line;
957
+ if (g.tasks > caps.maxPerGroup)
958
+ problems.push({
959
+ line: at,
960
+ kind: "cap",
961
+ message: `GROUP ${g.group} has ${g.tasks} tasks, over the cap of ${caps.maxPerGroup}`,
962
+ });
963
+ if (g.tests > caps.maxTestsPerGroup)
964
+ problems.push({
965
+ line: at,
966
+ kind: "cap",
967
+ message: `GROUP ${g.group} has ${g.tests} TEST FIRST tests, over the cap of ${caps.maxTestsPerGroup}`,
968
+ });
969
+ }
970
+ problems.sort((a, b) => a.line - b.line);
971
+ return { groups, sizes, overCap, problems };
972
+ }
973
+
974
+ export interface ExistenceFinding {
975
+ line: number;
976
+ task: number;
977
+ message: string;
280
978
  }
281
979
 
282
- /** `test` as a repo-relative path inside `top`; usage error when absolute, outside, or absent. */
283
- function repoTestPath(top: string, test: string): string {
284
- const rel = path.relative(top, path.resolve(top, test));
285
- if (path.isAbsolute(test) || rel.startsWith("..") || rel === "")
286
- throw new CommandError(EXIT.USAGE, `--test must be a path inside the repo, got "${test}"`);
287
- if (!fs.statSync(path.join(top, rel), { throwIfNoEntry: false })?.isFile())
288
- throw new CommandError(EXIT.USAGE, `--test ${rel} is not a file`);
289
- return rel.split(path.sep).join("/");
980
+ const escapeRe = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
981
+
982
+ const wordIn = (body: string, word: string) =>
983
+ new RegExp(`(?<![\\w$])${escapeRe(word)}(?![\\w$])`).test(body);
984
+
985
+ /** The 1-based lines of `body` that are a Markdown heading reading exactly `heading`. */
986
+ const headingLines = (body: string, heading: string): number[] =>
987
+ body.split(/\r?\n/).flatMap((l, i) => {
988
+ const h = /^#{1,6}\s+(.*?)(?:\s+#+)?\s*$/.exec(l);
989
+ return h !== null && h[1]!.trim() === heading ? [i + 1] : [];
990
+ });
991
+
992
+ /** A key path's segments nested in indented `key:` lines (YAML): each one inside the block of the one before. */
993
+ function nestedKeys(body: string, segs: string[]): boolean {
994
+ const lines = body.split(/\r?\n/);
995
+ const indent = (l: string) => /^\s*/.exec(l)![0].length;
996
+ const walk = (from: number, to: number, k: number, above: number): boolean => {
997
+ const key = new RegExp(`^\\s*["']?${escapeRe(segs[k]!)}["']?\\s*:`);
998
+ for (let i = from; i < to; i++) {
999
+ const l = lines[i]!;
1000
+ if (l.trim() === "" || indent(l) <= above || !key.test(l)) continue;
1001
+ if (k === segs.length - 1) return true;
1002
+ let end = i + 1;
1003
+ while (end < to && (lines[end]!.trim() === "" || indent(lines[end]!) > indent(l))) end++;
1004
+ if (walk(i + 1, end, k + 1, indent(l))) return true;
1005
+ }
1006
+ return false;
1007
+ };
1008
+ return walk(0, lines.length, 0, -1);
1009
+ }
1010
+
1011
+ /** A key path as a `[table]` header holding its first segments and a `key =` line for the rest (TOML). */
1012
+ function tableKey(body: string, segs: string[]): boolean {
1013
+ const lines = body.split(/\r?\n/);
1014
+ for (let k = 1; k < segs.length; k++) {
1015
+ const table = segs.slice(0, k).join(".");
1016
+ const key = segs.slice(k).join(".");
1017
+ const start = lines.findIndex((l) => l.trim() === `[${table}]`);
1018
+ if (start < 0) continue;
1019
+ const end = lines.findIndex((l, i) => i > start && /^\s*\[/.test(l));
1020
+ const keyRe = new RegExp(`^\\s*${escapeRe(key)}\\s*=`);
1021
+ if (lines.slice(start + 1, end < 0 ? undefined : end).some((l) => keyRe.test(l))) return true;
1022
+ }
1023
+ return false;
1024
+ }
1025
+
1026
+ /** Config files whose dotted keys must be there as the whole key path. */
1027
+ const CONFIG_EXTS = new Set([".json", ".jsonc", ".yaml", ".yml", ".toml"]);
1028
+
1029
+ /** Whether parsed JSON `value` holds `segs` as nested object keys. */
1030
+ function jsonPath(value: unknown, segs: string[]): boolean {
1031
+ let at = value;
1032
+ for (const s of segs) {
1033
+ if (typeof at !== "object" || at === null || Array.isArray(at) || !Object.hasOwn(at, s))
1034
+ return false;
1035
+ at = (at as Record<string, unknown>)[s];
1036
+ }
1037
+ return true;
1038
+ }
1039
+
1040
+ /**
1041
+ * Whether config `body` of `file` holds the dotted key `bare` as the whole key path: a
1042
+ * `.json` file that parses is walked as JSON; otherwise the key is written whole, nested
1043
+ * in indented `key:` / `"key":` lines (YAML, indented JSON), or a `[table]` and its
1044
+ * `key =` line (TOML).
1045
+ */
1046
+ function hasDottedKey(file: string, body: string, bare: string): boolean {
1047
+ const segs = bare.split(".");
1048
+ const json =
1049
+ path.posix.extname(file).toLowerCase() === ".json" ? parseJson(body) : { ok: false as const };
1050
+ if (json.ok) return jsonPath(json.value, segs);
1051
+ return wordIn(body, bare) || nestedKeys(body, segs) || tableKey(body, segs);
1052
+ }
1053
+
1054
+ /**
1055
+ * Each READ / UPDATE / DELETE line of an unticked task whose file or symbol is not
1056
+ * there, read from `root`: a plain word-boundary search in the named file. `§ heading`
1057
+ * must equal one of the file's heading lines; a dotted key in a config file (`CONFIG_EXTS`)
1058
+ * must be there as the whole key path, and in any other file matches on its last segment;
1059
+ * a file-only line checks the file exists. A line naming
1060
+ * what the task itself or a task it DEPENDS ON creates is not checked: it does not
1061
+ * exist yet.
1062
+ */
1063
+ export function planExistence(text: string, root: string): ExistenceFinding[] {
1064
+ const blocks = parsePlanBlocks(text);
1065
+ const byTask = new Map(blocks.tasks.map((t) => [t.task, t]));
1066
+ const out: ExistenceFinding[] = [];
1067
+ const cache = new Map<string, string | null>();
1068
+ const read = (file: string) => {
1069
+ if (!cache.has(file)) {
1070
+ const abs = path.resolve(root, file);
1071
+ const inside = !path.relative(root, abs).startsWith("..");
1072
+ cache.set(
1073
+ file,
1074
+ inside && fs.statSync(abs, { throwIfNoEntry: false })?.isFile()
1075
+ ? fs.readFileSync(abs, "utf-8")
1076
+ : null,
1077
+ );
1078
+ }
1079
+ return cache.get(file)!;
1080
+ };
1081
+ for (const t of blocks.tasks) {
1082
+ if (t.done) continue;
1083
+ const created = [t, ...t.dependsOn.map((d) => byTask.get(d)).filter((d) => d !== undefined)]
1084
+ .flatMap((d) => d.create)
1085
+ .map((c) => ({ file: path.posix.normalize(c.file), symbol: c.symbol }));
1086
+ for (const ref of [...t.read, ...t.update, ...t.delete]) {
1087
+ const file = path.posix.normalize(ref.file);
1088
+ if (
1089
+ created.some((c) => c.file === file && (c.symbol === undefined || c.symbol === ref.symbol))
1090
+ )
1091
+ continue;
1092
+ const body = read(file);
1093
+ const miss = (message: string) => out.push({ line: ref.line, task: t.task, message });
1094
+ if (body === null) {
1095
+ miss(`TASK ${t.task}: \`${ref.file}\` does not exist`);
1096
+ continue;
1097
+ }
1098
+ if (ref.symbol === undefined) continue;
1099
+ if (ref.symbol.startsWith("§")) {
1100
+ const heading = ref.symbol.replace(/^§\s*/, "").trim();
1101
+ if (headingLines(body, heading).length === 0)
1102
+ miss(`TASK ${t.task}: no heading \`${heading}\` in \`${ref.file}\``);
1103
+ continue;
1104
+ }
1105
+ const bare = ref.symbol.replace(/\(\)$/, "");
1106
+ const key = bare.split(".").at(-1)!;
1107
+ if (bare.includes(".") && CONFIG_EXTS.has(path.posix.extname(file).toLowerCase())) {
1108
+ if (!hasDottedKey(file, body, bare))
1109
+ miss(`TASK ${t.task}: \`${bare}\` is not in \`${ref.file}\``);
1110
+ } else if (!wordIn(body, key))
1111
+ miss(
1112
+ key === bare
1113
+ ? `TASK ${t.task}: \`${key}\` is not in \`${ref.file}\``
1114
+ : `TASK ${t.task}: \`${key}\` (last segment of \`${ref.symbol}\`) is not in \`${ref.file}\``,
1115
+ );
1116
+ }
1117
+ }
1118
+ return out.sort((a, b) => a.line - b.line);
290
1119
  }
291
1120
 
292
1121
  /** Run one test file through `bkit:test:file`; the report is read and removed. */
@@ -308,40 +1137,405 @@ function runTestFile(
308
1137
  const couldNotRunMessage = (exitCode: number) =>
309
1138
  `${gateScope(BKIT_TEST_FILE_SCRIPT)} could not run (exit ${exitCode}: ${exitCode === 126 ? "not executable" : "command not found"}) — no test executed`;
310
1139
 
311
- /** The copy of the test file a red keeps beside its log. */
312
- export const snapshotPath = (log: string) => `${log.replace(/\.md$/, "")}.snapshot`;
313
-
314
1140
  const toplevel = (cwd: string) => git(cwd, ["rev-parse", "--show-toplevel"]) ?? cwd;
315
1141
 
316
1142
  /** A fence longer than any backtick run in `text`. */
317
1143
  const fenceFor = (text: string) =>
318
1144
  "`".repeat(Math.max(3, ...[...text.matchAll(/`+/g)].map((m) => m[0].length + 1)));
319
1145
 
1146
+ /**
1147
+ * Every gap the audit emits, and the verifier's `Fixable` for it: `null` = the
1148
+ * answer needs a diff read, which only the verifier can make. A test with no proven red
1149
+ * is no gap: the audit lists it as unproven, and the owner answers every unproven test
1150
+ * once, at the end of the build.
1151
+ */
1152
+ const GAP_FIXABLE = {
1153
+ "not-green": true,
1154
+ "delete-still-referenced": null,
1155
+ "no-test-first": false,
1156
+ "test-first-malformed": false,
1157
+ "test-first-unread": false,
1158
+ "exception-refused": false,
1159
+ } as const satisfies Record<string, boolean | null>;
1160
+
1161
+ type GapKind = keyof typeof GAP_FIXABLE;
1162
+
320
1163
  interface Gap {
321
- task: number;
322
- kind: string;
1164
+ /** null for a plan-level gap (`test-first-unread`). */
1165
+ task: number | null;
1166
+ kind: GapKind;
323
1167
  detail: string;
1168
+ fixable: boolean | null;
1169
+ /** The line the verifier appends, carrying `Fixable: null` for a decision it makes itself. */
1170
+ logLine: string;
1171
+ }
1172
+
1173
+ /** One gap, with the `fixable` its kind settles and the ready-to-append log line. */
1174
+ function gapOf(
1175
+ task: number | null,
1176
+ kind: GapKind,
1177
+ detail: string,
1178
+ test?: { file: string; name: string },
1179
+ ): Gap {
1180
+ const fixable = GAP_FIXABLE[kind];
1181
+ const subject = test === undefined ? "" : `\`${test.file}\` \`${test.name}\` — `;
1182
+ return {
1183
+ task,
1184
+ kind,
1185
+ detail,
1186
+ fixable,
1187
+ logLine: `${task === null ? "Plan" : `TASK ${task}`}: ${subject}${kind} — Fixable: ${fixable}`,
1188
+ };
1189
+ }
1190
+
1191
+ /** The report's verdict from its gaps: a decision one gap still needs (`null`) outranks one it refuses. */
1192
+ const verdictOf = (gaps: Gap[]): "PASS" | "FAIL" | "CONCERNS" | null =>
1193
+ gaps.length === 0
1194
+ ? "PASS"
1195
+ : gaps.some((g) => g.fixable === null)
1196
+ ? null
1197
+ : gaps.some((g) => g.fixable === false)
1198
+ ? "FAIL"
1199
+ : "CONCERNS";
1200
+
1201
+ /** The gap for a task whose only test field is a TEST EXCEPTION that is not honoured. */
1202
+ export const EXCEPTION_REFUSED = "exception-refused";
1203
+
1204
+ /** The status row step plan-execute's finish writes; its commit carries the code of each task its log records DONE. */
1205
+ export const PLAN_TASKS_STEP = "build-plan-tasks";
1206
+
1207
+ const repoRel = (top: string, cwd: string, p: string) =>
1208
+ path.relative(top, path.resolve(cwd, p)).split(path.sep).join("/");
1209
+
1210
+ /**
1211
+ * The task numbers a plan-execute log records `DONE — TASK N` for, in log order. `verbs`
1212
+ * widens the reader to the log's other closing lines (`BLOCKED`), so one parser serves
1213
+ * every caller; the default reads `DONE` only, which is what "the task is done" means.
1214
+ */
1215
+ export function doneTasks(log: string, verbs: readonly string[] = ["DONE"]): number[] {
1216
+ const closing = verbs.map((v) => v.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|");
1217
+ return [...log.matchAll(new RegExp(`^(?:${closing})\\s*[—–-]+\\s*TASK\\s+(\\d+)\\b`, "gim"))].map(
1218
+ (m) => Number(m[1]),
1219
+ );
1220
+ }
1221
+
1222
+ /**
1223
+ * Why a task's `TEST EXCEPTION` lines do not count, or null when they do: every
1224
+ * line committed, and last changed by a commit that does not write the status file.
1225
+ * Every workflow step commit writes it, so a line riding one is refused; agents are
1226
+ * barred from committing, so the remaining commits are human-made.
1227
+ */
1228
+ function exceptionRefusal(
1229
+ top: string,
1230
+ plan: string,
1231
+ lines: number[],
1232
+ statusRel: string,
1233
+ ): string | null {
1234
+ const blame = git(top, ["blame", "--porcelain", "-L", `${lines[0]},${lines.at(-1)}`, "--", plan]);
1235
+ if (blame === null) return "the plan file is not committed";
1236
+ const shas = new Set([...blame.matchAll(/^([0-9a-f]{40}) \d+ \d+/gm)].map((m) => m[1]!));
1237
+ if ([...shas].some((s) => /^0+$/.test(s)))
1238
+ return "the line is not committed — a human commits it before the build re-runs";
1239
+ for (const sha of shas) {
1240
+ const files = git(top, ["show", "--name-only", "--format=", sha])?.split("\n") ?? [];
1241
+ if (files.includes(statusRel))
1242
+ return `last changed by ${sha.slice(0, 7)}, a workflow step commit (it also writes \`${statusRel}\`) — commit the line in a commit that does not touch it`;
1243
+ }
1244
+ return null;
1245
+ }
1246
+
1247
+ const withoutExt = (p: string) => p.replace(/\.[^./]*$/, "");
1248
+
1249
+ /**
1250
+ * Whether a grep hit for `stem` in `file` refers to something other than the files of
1251
+ * `targets` (repo-relative): false for a hit inside a target, or a line whose every
1252
+ * path-like mention of `stem` resolves to a target. A `./` or `../` path resolves
1253
+ * against `file`'s folder; any other path with a `/` matches a target ending in it.
1254
+ */
1255
+ function namesOldPath(stem: string, targets: string[]) {
1256
+ const bare = targets.map(withoutExt);
1257
+ const isTarget = (token: string, file: string) => {
1258
+ const t = withoutExt(token);
1259
+ if (t.startsWith("./") || t.startsWith("../"))
1260
+ return bare.includes(path.posix.join(path.posix.dirname(file), t));
1261
+ return t.includes("/") && bare.some((b) => b === t || b.endsWith(`/${t}`));
1262
+ };
1263
+ return (file: string, text: string) => {
1264
+ if (targets.includes(file)) return false;
1265
+ const mentions = text
1266
+ .split(/[^\w./@-]+/)
1267
+ .filter((tok) => path.posix.basename(withoutExt(tok)) === stem);
1268
+ return mentions.length === 0 || mentions.some((tok) => !isTarget(tok, file));
1269
+ };
1270
+ }
1271
+
1272
+ /** The gap for a DELETE line whose file or symbol the tree still holds or refers to. */
1273
+ export const DELETE_STILL_REFERENCED = "delete-still-referenced";
1274
+
1275
+ /**
1276
+ * One `delete-still-referenced` gap per DELETE line of `tasks` the tree at `top` still
1277
+ * holds: a file-only line whose file exists, or whose file name (no extension) a tracked
1278
+ * or untracked file names as a word; a symbol (a dotted key's last segment) named as a
1279
+ * word; a `§ heading` written as `§ heading`, or still a heading of its file. The plan
1280
+ * file and the folder it sits in are not searched. A line naming what a task of
1281
+ * `planTasks` CREATEs is a move, not checked. A file-only line whose file name a CREATE
1282
+ * of `planTasks` reuses is a file move: the created file, and lines whose every mention
1283
+ * of the name is a path to the created file, are not references. A word search hits
1284
+ * other things of the same name, so this is a gap for verify to judge, never a refusal.
1285
+ */
1286
+ function deleteGaps(
1287
+ top: string,
1288
+ planRel: string,
1289
+ tasks: PlanTaskBlock[],
1290
+ planTasks: PlanTaskBlock[],
1291
+ ): Gap[] {
1292
+ const created = planTasks.flatMap((t) => t.create);
1293
+ const planDir = path.posix.dirname(planRel);
1294
+ const excludes = [`:(exclude)${planRel}`, ...(planDir === "." ? [] : [`:(exclude)${planDir}`])];
1295
+ const grep = (term: string, word: boolean, keep?: (file: string, text: string) => boolean) => {
1296
+ const res = spawnSync(
1297
+ "git",
1298
+ [
1299
+ "grep",
1300
+ "-n",
1301
+ "-z",
1302
+ "-I",
1303
+ "--untracked",
1304
+ ...(word ? ["-w"] : []),
1305
+ "-F",
1306
+ "-e",
1307
+ term,
1308
+ "--",
1309
+ ".",
1310
+ ...excludes,
1311
+ ],
1312
+ { cwd: top, encoding: "utf-8" },
1313
+ );
1314
+ if (res.status === 1) return { hits: [] as string[] };
1315
+ if (res.status !== 0)
1316
+ return {
1317
+ failed: `git grep exited ${res.status ?? res.signal}: ${(res.stderr ?? "").trim()}`,
1318
+ };
1319
+ const hits = res.stdout
1320
+ .split("\n")
1321
+ .filter(Boolean)
1322
+ .map((l) => l.split("\0"))
1323
+ .filter(([file, , text]) => keep === undefined || keep(file!, text ?? ""))
1324
+ .map((parts) => parts.slice(0, 2).join(":"));
1325
+ return { hits };
1326
+ };
1327
+ const listed = (hits: string[]) =>
1328
+ hits
1329
+ .slice(0, 5)
1330
+ .map((h) => `\`${h}\``)
1331
+ .join(", ") + (hits.length > 5 ? `, and ${hits.length - 5} more` : "");
1332
+ const gaps: Gap[] = [];
1333
+ for (const t of tasks)
1334
+ for (const ref of t.delete) {
1335
+ const file = path.posix.normalize(ref.file);
1336
+ const moved = created.some((c) =>
1337
+ ref.symbol === undefined ? path.posix.normalize(c.file) === file : c.symbol === ref.symbol,
1338
+ );
1339
+ if (moved) continue;
1340
+ const movedTo =
1341
+ ref.symbol === undefined
1342
+ ? created
1343
+ .filter(
1344
+ (c) =>
1345
+ c.symbol === undefined &&
1346
+ path.posix.basename(path.posix.normalize(c.file)) === path.posix.basename(file),
1347
+ )
1348
+ .map((c) => path.posix.normalize(c.file))
1349
+ : [];
1350
+ const gap = (detail: string) => gaps.push(gapOf(t.task, DELETE_STILL_REFERENCED, detail));
1351
+ const abs = path.join(top, file);
1352
+ const exists = fs.statSync(abs, { throwIfNoEntry: false })?.isFile() === true;
1353
+ let what: string;
1354
+ let found: { hits?: string[]; failed?: string };
1355
+ if (ref.symbol === undefined) {
1356
+ if (exists) {
1357
+ gap(`DELETE \`${ref.file}\`: the file still exists`);
1358
+ continue;
1359
+ }
1360
+ const stem = path.posix.basename(file).replace(/\.[^.]*$/, "");
1361
+ what = `DELETE \`${ref.file}\` is still referenced as \`${stem}\``;
1362
+ found = grep(stem, true, movedTo.length === 0 ? undefined : namesOldPath(stem, movedTo));
1363
+ } else if (ref.symbol.startsWith("§")) {
1364
+ const heading = ref.symbol.replace(/^§\s*/, "").trim();
1365
+ what = `DELETE ${ref.symbol} in \`${ref.file}\` is still referenced`;
1366
+ found = grep(`§ ${heading}`, false);
1367
+ if (exists && found.hits !== undefined)
1368
+ found.hits.push(
1369
+ ...headingLines(fs.readFileSync(abs, "utf-8"), heading).map((n) => `${file}:${n}`),
1370
+ );
1371
+ } else {
1372
+ const bare = ref.symbol.replace(/\(\)$/, "");
1373
+ what = `DELETE \`${ref.symbol}\` in \`${ref.file}\` is still referenced`;
1374
+ found = grep(bare.split(".").at(-1)!, true);
1375
+ }
1376
+ if (found.failed !== undefined)
1377
+ gap(
1378
+ `${what.replace(/ is still referenced.*$/, "")}: references not checked — ${found.failed}`,
1379
+ );
1380
+ else if (found.hits!.length > 0) gap(`${what}: ${listed([...new Set(found.hits)].sort())}`);
1381
+ }
1382
+ return gaps;
1383
+ }
1384
+
1385
+ interface ExceptionResult {
1386
+ task: number;
1387
+ refusal: string | null;
1388
+ cleared: string[];
324
1389
  }
325
1390
 
326
1391
  interface AuditRow {
327
1392
  task: number;
328
1393
  test: string;
329
1394
  red: string;
330
- changed: string;
331
1395
  green: string;
332
1396
  }
333
1397
 
334
- /** The red rows of status.jsonl for this workflow and sub-plan, oldest first. */
335
- function redRows(file: string, workflow: string, subPlan: number | undefined) {
336
- return readStatusRows(file).filter((r) => {
337
- const row = r as Record<string, unknown>;
338
- return (
339
- row.type === "step-done" &&
340
- row.workflow === workflow &&
341
- row.step === RED_STEP &&
342
- (subPlan === undefined || row.subPlan === subPlan)
343
- );
344
- }) as Array<Record<string, unknown>>;
1398
+ /** A planned test whose pass the tests step accepted: no red is due for it. */
1399
+ interface Landed {
1400
+ task: number;
1401
+ test: string;
1402
+ name: string;
1403
+ reason: string;
1404
+ }
1405
+
1406
+ /** A planned test with no proven red: the owner answers it at the end of the build. */
1407
+ interface Unproven {
1408
+ task: number;
1409
+ test: string;
1410
+ name: string;
1411
+ detail: string;
1412
+ }
1413
+
1414
+ /**
1415
+ * An existing test the tests step changed: the verifier judges its diff since `base`.
1416
+ * `additive` = the test's own lines since `base` gained lines and lost none (null when
1417
+ * either side cannot be read); `diff` = those lines' unified diff.
1418
+ */
1419
+ interface Updated {
1420
+ task: number;
1421
+ test: string;
1422
+ name: string;
1423
+ base: string | null;
1424
+ additive: boolean | null;
1425
+ diff: string | null;
1426
+ }
1427
+
1428
+ /** The `Updated` entry for a planned test: its own lines at `base` against the file now. */
1429
+ function updatedEntry(
1430
+ top: string,
1431
+ task: number,
1432
+ t: { test: string; name: string },
1433
+ base: string | null,
1434
+ ): Updated {
1435
+ const entry: Updated = { task, test: t.test, name: t.name, base, additive: null, diff: null };
1436
+ if (base === null) return entry;
1437
+ const shown = runGit(top, ["show", `${base}:${t.test}`]);
1438
+ const abs = path.join(top, t.test);
1439
+ if (!shown.ok || !fs.existsSync(abs)) return entry;
1440
+ const was = testBlock(shown.stdout, t.name);
1441
+ const now = testBlock(fs.readFileSync(abs, "utf-8"), t.name);
1442
+ if (was === null || now === null) return entry;
1443
+ const d = lineDiff(was, now);
1444
+ return { ...entry, additive: d.removed === 0, diff: d.text };
1445
+ }
1446
+
1447
+ /** A planned test the group's red did not prove, and why. */
1448
+ interface RedGap {
1449
+ task: number;
1450
+ test: string;
1451
+ name: string;
1452
+ kind: Extract<RedState, "vacuous" | "missing" | "no-report" | "lint">;
1453
+ detail: string;
1454
+ }
1455
+
1456
+ /** A `PASSES — TASK N: \`file\` \`name\`: reason` line of the tests step's log. */
1457
+ interface PassesLine extends PlannedTest {
1458
+ task: number;
1459
+ reason: string;
1460
+ }
1461
+
1462
+ /** Every PASSES line of a tests step's log: the tests the writer saw passing and vouches for. */
1463
+ function passesLines(log: string): PassesLine[] {
1464
+ return [
1465
+ ...log.matchAll(/^PASSES\s*[—–-]+\s*TASK\s+(\d+):\s*`([^`]+)`\s+`([^`]+)`\s*:\s*(.+)$/gm),
1466
+ ].map((m) => ({
1467
+ task: Number(m[1]),
1468
+ test: m[2]!.trim(),
1469
+ name: m[3]!.trim(),
1470
+ reason: m[4]!.trim(),
1471
+ }));
1472
+ }
1473
+
1474
+ /** Why a plan cannot be read for its tests (no file, no task markers, a fence never closed), or undefined. */
1475
+ const planRefusal = (
1476
+ plan: string | undefined,
1477
+ parsed: ReturnType<typeof parsePlanBlocks>,
1478
+ ): string | undefined =>
1479
+ plan === undefined
1480
+ ? `no plan file; ${NO_MARKERS_REASON}`
1481
+ : parsed.tasks.length === 0
1482
+ ? parsed.fenceOpen === undefined
1483
+ ? NO_MARKERS_REASON
1484
+ : fenceNeverClosed(parsed.fenceOpen)
1485
+ : undefined;
1486
+
1487
+ /** A table cell: pipes escaped, line breaks folded. */
1488
+ const cell = (s: string) => s.replace(/\s+/g, " ").replace(/\|/g, "\\|").trim();
1489
+
1490
+ function writeRedReport(
1491
+ log: string,
1492
+ o: {
1493
+ enforced: boolean;
1494
+ reason?: string;
1495
+ plan?: string;
1496
+ group: number;
1497
+ attempt: number;
1498
+ tests: RedTest[];
1499
+ details: Map<RedTest, string>;
1500
+ gaps: RedGap[];
1501
+ outputs: Array<{ title: string; text: string }>;
1502
+ },
1503
+ ): void {
1504
+ const red = o.tests.filter((t) => t.state === "red" || t.state === "red-not-loaded").length;
1505
+ const accepted = o.tests.filter((t) => t.state === "passed-accepted").length;
1506
+ const updated = o.tests.filter((t) => t.state === "updated").length;
1507
+ const out = [
1508
+ `Enforced: ${o.enforced}`,
1509
+ `Red: ${red}`,
1510
+ `Accepted: ${accepted}`,
1511
+ `Updated: ${updated}`,
1512
+ `Gaps: ${o.gaps.length}`,
1513
+ "",
1514
+ "<!-- verdict:end -->",
1515
+ "",
1516
+ `# Tests Red — group ${o.group}, attempt ${o.attempt}`,
1517
+ "",
1518
+ ];
1519
+ if (o.plan) out.push(`Plan: \`${o.plan}\``, "");
1520
+ if (!o.enforced) out.push(`Not enforced: ${o.reason}.`, "");
1521
+ if (o.tests.length > 0) {
1522
+ out.push("| Task | Test | State | Detail |", "| --- | --- | --- | --- |");
1523
+ for (const t of o.tests)
1524
+ out.push(
1525
+ `| ${t.task} | \`${t.test}\` "${cell(t.testName)}" | ${t.state} | ${cell(o.details.get(t) ?? "")} |`,
1526
+ );
1527
+ out.push("");
1528
+ }
1529
+ out.push("## Gaps", "");
1530
+ if (o.gaps.length === 0) out.push("None.");
1531
+ for (const g of o.gaps)
1532
+ out.push(`- TASK ${g.task} \`${g.test}\` "${g.name}" \`${g.kind}\` — ${g.detail}`);
1533
+ for (const t of o.outputs) {
1534
+ const fence = fenceFor(t.text);
1535
+ out.push("", `## ${t.title}`, "", fence, t.text.replace(/\n$/, ""), fence);
1536
+ }
1537
+ fs.mkdirSync(path.dirname(log), { recursive: true });
1538
+ fs.writeFileSync(log, `${out.join("\n")}\n`);
345
1539
  }
346
1540
 
347
1541
  function writeAuditReport(
@@ -350,39 +1544,73 @@ function writeAuditReport(
350
1544
  enforced: boolean;
351
1545
  reason?: string;
352
1546
  plan?: string;
1547
+ verdict: "PASS" | "FAIL" | "CONCERNS" | null;
353
1548
  rows: AuditRow[];
354
1549
  gaps: Gap[];
355
- diffs: Array<{ task: number; test: string; diff: string }>;
1550
+ exceptions: ExceptionResult[];
1551
+ landed: Landed[];
1552
+ unproven: Unproven[];
1553
+ updated: Updated[];
356
1554
  outputs: Array<{ test: string; tail: string }>;
357
1555
  },
358
1556
  ): void {
359
- const out = [`Enforced: ${o.enforced}`, `Gaps: ${o.gaps.length}`, "", "<!-- verdict:end -->", ""];
1557
+ const out = [
1558
+ `Enforced: ${o.enforced}`,
1559
+ `Verdict: ${o.verdict}`,
1560
+ `Gaps: ${o.gaps.length}`,
1561
+ `Unproven: ${o.unproven.length}`,
1562
+ "",
1563
+ "<!-- verdict:end -->",
1564
+ "",
1565
+ ];
360
1566
  out.push("# Test-First Audit", "");
361
1567
  if (o.plan) out.push(`Plan: \`${o.plan}\``, "");
362
1568
  if (!o.enforced) out.push(`Not enforced: ${o.reason}.`, "");
363
1569
  if (o.rows.length > 0) {
364
- out.push(
365
- "| Task | Test | Red | Changed since red | Green |",
366
- "| --- | --- | --- | --- | --- |",
367
- );
368
- for (const r of o.rows)
369
- out.push(`| ${r.task} | ${r.test} | ${r.red} | ${r.changed} | ${r.green} |`);
1570
+ out.push("| Task | Test | Red | Green |", "| --- | --- | --- | --- |");
1571
+ for (const r of o.rows) out.push(`| ${r.task} | ${r.test} | ${r.red} | ${r.green} |`);
370
1572
  out.push("");
371
1573
  }
372
1574
  out.push("## Gaps", "");
373
1575
  if (o.gaps.length === 0) out.push("None.");
374
- for (const g of o.gaps) out.push(`- Task ${g.task} \`${g.kind}\` — ${g.detail}`);
375
- for (const d of o.diffs) {
376
- const fence = fenceFor(d.diff);
1576
+ for (const g of o.gaps)
1577
+ out.push(
1578
+ `- ${g.task === null ? "Plan" : `Task ${g.task}`} \`${g.kind}\` — ${g.detail} — Fixable: ${g.fixable} — log: ${g.logLine}`,
1579
+ );
1580
+ if (o.exceptions.length > 0) out.push("", "## Test exceptions", "");
1581
+ for (const e of o.exceptions)
1582
+ out.push(
1583
+ e.refusal === null
1584
+ ? `- Task ${e.task} — honoured; cleared ${e.cleared.length > 0 ? e.cleared.join(", ") : "nothing"}`
1585
+ : `- Task ${e.task} — not honoured: ${e.refusal}`,
1586
+ );
1587
+ if (o.landed.length > 0) out.push("", "## Already landed", "");
1588
+ for (const l of o.landed)
1589
+ out.push(
1590
+ `- Task ${l.task} \`${l.test}\` "${l.name}" — passed at the group's red, and the tests step accepted it: ${l.reason}; no red is due`,
1591
+ );
1592
+ if (o.updated.length > 0) out.push("", "## Updated tests", "");
1593
+ for (const u of o.updated) {
1594
+ const since = u.base === null ? "" : ` since \`${u.base.slice(0, 9)}\``;
1595
+ const judged =
1596
+ u.additive === null
1597
+ ? "its lines at the base commit could not be read, so read the group diff"
1598
+ : u.additive
1599
+ ? "additive: no line of the test was removed, so no expectation was dropped — read the added lines for one that disables an earlier expectation"
1600
+ : "not additive: a line of the test was removed or changed — read the diff for an expectation removed or loosened";
377
1601
  out.push(
378
- "",
379
- `## Diff since red — Task ${d.task} \`${d.test}\``,
380
- "",
381
- `${fence}diff`,
382
- d.diff,
383
- fence,
1602
+ `- Task ${u.task} \`${u.test}\` "${u.name}" — an existing test the tests step changed${since}; the update may add expectations, never remove or loosen one. ${judged}`,
384
1603
  );
1604
+ if (u.diff !== null) {
1605
+ const fence = fenceFor(u.diff);
1606
+ out.push("", ` ${fence}diff`, ...u.diff.split("\n").map((l) => ` ${l}`), ` ${fence}`, "");
1607
+ }
385
1608
  }
1609
+ if (o.unproven.length > 0) out.push("", "## Unproven", "");
1610
+ for (const u of o.unproven)
1611
+ out.push(
1612
+ `- Task ${u.task} \`${u.test}\` "${u.name}" — ${u.detail}; the owner answers it at the end of the build`,
1613
+ );
386
1614
  for (const t of o.outputs) {
387
1615
  const fence = fenceFor(t.tail);
388
1616
  out.push("", `## Runner output — \`${t.test}\``, "", fence, t.tail, fence);
@@ -396,8 +1624,11 @@ export const testsCommand: CommandSpec = {
396
1624
  summary:
397
1625
  "Run the deepest declared local gate, or one test file to record a red and audit a green.",
398
1626
  verbs: {
1627
+ changed: testsChangedVerb,
399
1628
  run: {
400
- summary: "Run the first declared --gate; exit 0 PASS, 1 FAIL, 4 could not run.",
1629
+ summary:
1630
+ "Run the first declared --gate, and with --file append a test-run status row for the run; exit 0 PASS, 1 FAIL, 4 could not run, 81 tests lock held.",
1631
+ use: "by-hand",
401
1632
  flags: [
402
1633
  {
403
1634
  name: "gate",
@@ -417,24 +1648,186 @@ export const testsCommand: CommandSpec = {
417
1648
  type: "string",
418
1649
  help: `lines of the log echoed to stderr afterwards (default ${DEFAULT_TAIL}; 0 for none)`,
419
1650
  },
1651
+ {
1652
+ name: "lock-wait-s",
1653
+ type: "string",
1654
+ help: "whole-number lock wait >= 0, passed as LOCAL_STACK_LOCK_WAIT_S (default BKIT_LOCK_WAIT_S, else 60)",
1655
+ },
1656
+ {
1657
+ name: "artifact",
1658
+ type: "string",
1659
+ help: "copy the log here, trim the copy (debug-evidence) and record the clamp; needs --fid and --step",
1660
+ },
1661
+ { name: "fid", type: "string", help: "--artifact: feature id, for the clamp log" },
1662
+ {
1663
+ name: "step",
1664
+ type: "string",
1665
+ help: "step name: the test-run row's (--file) and the clamp log's (--artifact)",
1666
+ },
1667
+ {
1668
+ name: "attempt",
1669
+ type: "string",
1670
+ help: "attempt: on the test-run row (--file) and in the clamp log's name (--artifact)",
1671
+ },
1672
+ {
1673
+ name: "file",
1674
+ type: "string",
1675
+ help: "status.jsonl: append one test-run row for this run, pass or fail; needs --workflow and --step",
1676
+ },
1677
+ { name: "workflow", type: "string", help: "--file: workflow name, for the test-run row" },
1678
+ { name: "ci-run", type: "string", help: "selected GitHub evidence run ID" },
1679
+ { name: "ci-url", type: "string", help: "selected GitHub evidence attempt URL" },
420
1680
  ],
421
1681
  output: {
1682
+ source: "ci for pinned red evidence; local for every executed gate",
1683
+ runAttempt: "selected CI attempt for a CI evidence round",
1684
+ ciGeneration: "active selection generation for a CI evidence round",
1685
+ ciRun: "selected CI run ID for a CI evidence round",
1686
+ ciUrl: "selected CI attempt URL for a CI evidence round",
1687
+ repo: "selected repository for a CI evidence round",
1688
+ workflowId: "selected workflow ID for a CI evidence round",
1689
+ workflowPath: "selected workflow path for a CI evidence round",
1690
+ headSha: "selected commit for a CI evidence round",
422
1691
  verdict: "PASS | FAIL (null when the gate could not run)",
423
1692
  exitCode: "the gate's exit code",
424
1693
  gate: "the script that ran",
425
1694
  scope: "the command that ran, as status rows record it",
426
- failures: "0 on PASS; null otherwise — count failing suites from the log",
427
- log: "where the full output was saved (as given)",
1695
+ failures: `0 on PASS; on FAIL the failing + erroring test cases in the JUnit reports, or null when no report counts any (unknown, never 0)`,
1696
+ reports: `{log}.reports — emptied before the run and passed to the gate as $${REPORT_DIR_ENV}`,
1697
+ log: "where the full output was saved (as given); the untouched original",
428
1698
  logBytes: "size of the saved output",
1699
+ artifact: "--artifact as given, else null",
1700
+ clamp: "the artifact's clamp record (raw = the log, source = scope), else null",
1701
+ row: `the ${TEST_RUN_ROW} row appended to --file ({type, workflow, at, step, attempt?, run, verdict, exitCode, failures, scope, log, artifact?}; run = this step's test-run rows in the workflow's active run, this one included), else null; a tests-lock wait writes no row`,
1702
+ lockHolder: "tests-lock holder when the wait gave up, else null",
429
1703
  },
1704
+ textOmit: ["clamp", "row"],
430
1705
  exitCodes: {
431
1706
  [EXIT_NO_GATE]: "no candidate gate is declared in package.json",
432
1707
  [EXIT_COULD_NOT_RUN]: "the gate could not run (exit 126 / 127): not a test failure",
1708
+ [EXIT_ARTIFACT_STEP]: "--artifact could not be written, trimmed or recorded",
433
1709
  },
434
1710
  run: (ctx) => {
1711
+ const waitSource = ctx.flags.lockWaitS ?? process.env.BKIT_LOCK_WAIT_S;
1712
+ const waitS = wholeNumber(
1713
+ waitSource,
1714
+ ctx.flags.lockWaitS === undefined && process.env.BKIT_LOCK_WAIT_S !== undefined
1715
+ ? "lock-wait-s (BKIT_LOCK_WAIT_S)"
1716
+ : "lock-wait-s",
1717
+ 60,
1718
+ );
435
1719
  const candidates = ctx.flags.gate as string[];
436
1720
  const logArg = String(ctx.flags.log);
437
1721
  if (logArg === "") throw new CommandError(EXIT.USAGE, "--log must be non-empty");
1722
+ const artifact = str(ctx.flags.artifact);
1723
+ const fid = str(ctx.flags.fid);
1724
+ const step = str(ctx.flags.step);
1725
+ const attempt = positiveInt(ctx.flags.attempt, "attempt");
1726
+ if (artifact !== undefined && (!fid || !step))
1727
+ throw new CommandError(EXIT.USAGE, "--artifact needs --fid and --step");
1728
+ const statusArg = str(ctx.flags.file);
1729
+ const workflow = str(ctx.flags.workflow);
1730
+ if (statusArg !== undefined && (!workflow || !step))
1731
+ throw new CommandError(EXIT.USAGE, "--file needs --workflow and --step");
1732
+ const status = statusArg === undefined ? undefined : path.resolve(ctx.cwd, statusArg);
1733
+ if (status !== undefined && !fs.existsSync(status))
1734
+ throw new CommandError(
1735
+ EXIT.USAGE,
1736
+ `--file ${statusArg} does not exist (a test-run row never starts a log)`,
1737
+ );
1738
+ if (ctx.flags.ciRun !== undefined || ctx.flags.ciUrl !== undefined) {
1739
+ const active = status === undefined ? undefined : readRuns(status).at(-1);
1740
+ const selection = active?.ciSelection;
1741
+ if (
1742
+ !status ||
1743
+ workflow !== "debug" ||
1744
+ active?.workflow !== "debug" ||
1745
+ active.status !== "IN_PROGRESS" ||
1746
+ !validateCiSelection(selection) ||
1747
+ selection.mode !== "ci" ||
1748
+ ctx.flags.ciRun !== selection.runId ||
1749
+ ctx.flags.ciUrl !== selection.runUrl ||
1750
+ !Number.isSafeInteger(active.ciGeneration) ||
1751
+ Number(active.ciGeneration) < 1 ||
1752
+ !artifact
1753
+ )
1754
+ throw new CommandError(
1755
+ EXIT.USAGE,
1756
+ "CI round needs an active matching debug selection and artifact",
1757
+ );
1758
+ const fixed = active.steps.some(
1759
+ (s) =>
1760
+ s.status === "DONE" &&
1761
+ s.ciGeneration === active.ciGeneration &&
1762
+ ["debug-failure-remediate", "debug-failure-soak"].includes(s.step),
1763
+ );
1764
+ if (!fixed) {
1765
+ const scope = `ci:${selection.runId}`;
1766
+ const text = [
1767
+ "Verdict: FAIL",
1768
+ "Source: ci",
1769
+ `Scope: ${scope}`,
1770
+ `Repository: ${selection.repo}`,
1771
+ `Workflow: ${selection.workflowPath} (${selection.workflowId})`,
1772
+ `Run: ${selection.runId}`,
1773
+ `Run attempt: ${selection.runAttempt}`,
1774
+ `CI generation: ${active.ciGeneration}`,
1775
+ `Commit: ${selection.headSha}`,
1776
+ `URL: ${selection.runUrl}`,
1777
+ "",
1778
+ "<!-- verdict:end -->",
1779
+ "",
1780
+ "# CI evidence",
1781
+ "",
1782
+ "The local gate was not run.",
1783
+ "",
1784
+ ].join("\n");
1785
+ for (const dest of new Set([artifact, logArg])) {
1786
+ const out = path.resolve(ctx.cwd, dest);
1787
+ fs.mkdirSync(path.dirname(out), { recursive: true });
1788
+ fs.writeFileSync(out, text);
1789
+ }
1790
+ const evidence = {
1791
+ gate: null,
1792
+ ciRun: selection.runId,
1793
+ ciUrl: selection.runUrl,
1794
+ runAttempt: selection.runAttempt,
1795
+ ciGeneration: active.ciGeneration,
1796
+ repo: selection.repo,
1797
+ workflowId: selection.workflowId,
1798
+ workflowPath: selection.workflowPath,
1799
+ headSha: selection.headSha,
1800
+ };
1801
+ const row = appendTestRun(status, {
1802
+ workflow,
1803
+ step: step!,
1804
+ attempt,
1805
+ source: "ci",
1806
+ verdict: "FAIL",
1807
+ exitCode: null,
1808
+ failures: null,
1809
+ scope,
1810
+ log: logArg,
1811
+ artifact,
1812
+ evidence,
1813
+ });
1814
+ ctx.emit({
1815
+ source: "ci",
1816
+ verdict: "FAIL",
1817
+ scope,
1818
+ exitCode: null,
1819
+ failures: null,
1820
+ ...evidence,
1821
+ artifact,
1822
+ log: logArg,
1823
+ logBytes: Buffer.byteLength(text),
1824
+ reports: null,
1825
+ clamp: null,
1826
+ row,
1827
+ });
1828
+ return EXIT.NO;
1829
+ }
1830
+ }
438
1831
  const tailRaw = str(ctx.flags.tail) ?? String(DEFAULT_TAIL);
439
1832
  if (!/^[0-9]+$/.test(tailRaw)) {
440
1833
  throw new CommandError(
@@ -456,24 +1849,96 @@ export const testsCommand: CommandSpec = {
456
1849
  }
457
1850
  const scope = gateScope(gate);
458
1851
  const log = path.resolve(ctx.cwd, logArg);
1852
+ const reports = freshReportDir(log);
459
1853
  ctx.log(`running ${scope} — full output in ${logArg}`);
460
- const exitCode = runToLog(scriptCommand(gate), top, log);
461
- for (const line of Number(tailRaw) > 0 ? tailLines(log, Number(tailRaw)) : [])
462
- ctx.log(line);
463
- const couldNotRun = COULD_NOT_RUN_EXIT_CODES.has(exitCode);
464
- const verdict = couldNotRun ? null : exitCode === 0 ? "PASS" : "FAIL";
1854
+ const fetchedAt = new Date().toISOString().replace(/\.\d{3}Z$/, "Z");
1855
+ const who = ["bkit tests run", gate, fid, step].filter(Boolean).join(" ");
1856
+ const exitCode = runToLog(scriptCommand(gate), top, log, {
1857
+ [REPORT_DIR_ENV]: reports,
1858
+ LOCAL_STACK_LOCK_WAIT_S: String(waitS),
1859
+ LOCAL_STACK_LOCK_WHO: who,
1860
+ });
1861
+ const logText = fs.readFileSync(log, "utf-8");
1862
+ const lockExit = classifyLockExit({ exitCode, log: logText, who });
1863
+ const lockWait = lockExit === "lock-wait";
1864
+ const lockHolder = lockWait ? testsLockHolder(logText, who) : null;
1865
+ const lines =
1866
+ lockExit === "gate"
1867
+ ? Number(tailRaw) > 0
1868
+ ? tailLines(log, Number(tailRaw))
1869
+ : []
1870
+ : logText.split(/\r?\n/).filter((line) => line.startsWith("[machine-lock]"));
1871
+ for (const line of lines) ctx.log(line);
1872
+ const couldNotRun = lockExit === "could-not-run" || COULD_NOT_RUN_EXIT_CODES.has(exitCode);
1873
+ const verdict = couldNotRun || lockWait ? null : exitCode === 0 ? "PASS" : "FAIL";
1874
+ const counted = lockExit === "gate" ? countReportFailures(reports) : null;
1875
+ if (counted === null && lockExit === "gate") ctx.log(NO_REPORT_HINT);
1876
+ else if (verdict === "FAIL" && counted === 0)
1877
+ ctx.log(
1878
+ `the JUnit reports in $${REPORT_DIR_ENV} hold no failing case — the gate failed outside its reported tests; failures unknown`,
1879
+ );
1880
+ const failures = verdict === "PASS" ? 0 : counted !== null && counted > 0 ? counted : null;
1881
+ let clamp: ClampRecord | null = null;
1882
+ let artifactFailed: string | null = null;
1883
+ if (artifact !== undefined && !lockWait) {
1884
+ try {
1885
+ const out = path.resolve(ctx.cwd, artifact);
1886
+ fs.mkdirSync(path.dirname(out), { recursive: true });
1887
+ fs.copyFileSync(log, out);
1888
+ const clampLog = clampLogPath(ctx.cwd, fid!, step!, attempt);
1889
+ fs.mkdirSync(path.dirname(clampLog), { recursive: true });
1890
+ fs.writeFileSync(clampLog, "");
1891
+ clamp = clampFile(out, "debug-evidence", artifact, {
1892
+ rawDir: null,
1893
+ source: scope,
1894
+ fetchedAt,
1895
+ original: log,
1896
+ });
1897
+ appendRecord(clampLog, clamp);
1898
+ } catch (err) {
1899
+ // The verdict stands; the step's artifact is what failed.
1900
+ clamp = null;
1901
+ artifactFailed = (err as Error).message;
1902
+ }
1903
+ }
1904
+ const row =
1905
+ status === undefined || lockWait
1906
+ ? null
1907
+ : appendTestRun(status, {
1908
+ workflow: workflow!,
1909
+ step: step!,
1910
+ attempt,
1911
+ verdict,
1912
+ exitCode,
1913
+ failures,
1914
+ scope,
1915
+ log: logArg,
1916
+ artifact,
1917
+ source: "local",
1918
+ });
465
1919
  ctx.emit({
1920
+ source: "local",
466
1921
  verdict,
467
1922
  exitCode,
468
1923
  gate,
469
1924
  scope,
470
- failures: verdict === "PASS" ? 0 : null,
1925
+ failures,
1926
+ reports: `${logArg}.reports`,
471
1927
  log: logArg,
472
1928
  logBytes: fs.statSync(log).size,
1929
+ artifact: lockWait ? null : (artifact ?? null),
1930
+ clamp,
1931
+ row,
1932
+ lockHolder,
473
1933
  });
1934
+ if (artifactFailed !== null) {
1935
+ ctx.log(`--artifact ${artifact}: ${artifactFailed}`);
1936
+ return EXIT_ARTIFACT_STEP;
1937
+ }
1938
+ if (lockWait) return EXIT.AGENT_WAIT;
474
1939
  if (couldNotRun) {
475
1940
  ctx.log(
476
- `${scope} could not run (exit ${exitCode}: ${exitCode === 126 ? "not executable" : "command not found"}) — no tests executed; this is not a FAIL verdict`,
1941
+ `${scope} could not run (exit ${exitCode}: ${lockExit === "could-not-run" ? "tests lock refused" : exitCode === 126 ? "not executable" : "command not found"}) — no tests executed; this is not a FAIL verdict`,
477
1942
  );
478
1943
  return EXIT_COULD_NOT_RUN;
479
1944
  }
@@ -483,130 +1948,287 @@ export const testsCommand: CommandSpec = {
483
1948
  },
484
1949
  red: {
485
1950
  summary:
486
- "Run a task's new test before its code exists; exit 0 red recorded, 1 not a red (the named test did not run, passed, or was skipped).",
1951
+ "Run every TEST FIRST test of one group's open tasks before any task code exists, and write one build-tests-red row holding a state and blob per planned test; a test an earlier row of the group proved red, its file unchanged since, is not run again. Exit 0 every test red, 1 a gap (a test passing, absent from its report, failing lint, or its file missing), 3 bkit:test:file undeclared (blobs recorded, nothing ran), 4 a script could not run.",
1952
+ use: "workflow",
487
1953
  flags: [
488
1954
  FILE_FLAG,
489
1955
  WORKFLOW_FLAG,
490
- SUB_PLAN_FLAG,
491
1956
  {
492
- name: "task",
493
- type: "string",
1957
+ ...GROUP_FLAG,
494
1958
  required: true,
495
- help: "plan task number (positive integer)",
1959
+ help: "the group whose planned tests run (positive integer)",
496
1960
  },
497
1961
  {
498
- name: "test",
1962
+ name: "plan",
499
1963
  type: "string",
500
1964
  required: true,
501
- help: "the test file, repo-relative — passed whole to bkit:test:file",
1965
+ repeatable: true,
1966
+ help: "candidate plan file; the first that exists is read",
502
1967
  },
503
1968
  {
504
- name: "name",
1969
+ name: "log",
505
1970
  type: "string",
506
1971
  required: true,
507
- help: "the test's name as the runner reports it, or its last segment",
1972
+ help: "file the red report is written to (directories are created)",
508
1973
  },
509
1974
  {
510
- name: "log",
1975
+ name: "writer-log",
511
1976
  type: "string",
512
- required: true,
513
- help: "file the runner output is saved to; the test file's copy lands beside it as .snapshot",
1977
+ help: "the tests step's log: a `PASSES — TASK N: `file` `name`: reason` line there accepts that test's pass as passed-accepted",
514
1978
  },
515
1979
  ],
516
1980
  output: {
517
- recorded: "true when the red row was written, or already recorded for this unchanged test",
518
- already:
519
- "true when an earlier red for this task, test and name stands (test unchanged); nothing ran",
520
- refused: "no-report | not-run | passed | skipped | exit-mismatch (when recorded is false)",
521
- reason: "why the run is not a red (when recorded is false)",
522
- exitCode: "the script's exit code",
523
- row: "the build-task-red row written (when recorded)",
524
- log: "where the runner output was saved (as given)",
1981
+ enforced:
1982
+ "false when bkit:test:file is not declared (the row records the blobs, every state unknown, exit 3) or the plan could not be read (no row, exit 1)",
1983
+ reason: "why test-first is not enforced",
1984
+ attempt: "this group's build-tests-red rows so far, this one included",
1985
+ tests:
1986
+ "[{task, test, testName, state, blob, reason?}] in plan order, the tests run now: state red | red-not-loaded | passed-accepted | vacuous | missing | no-report | lint | unknown; blob the test file's git blob as it stood (null when the file is missing); reason the tests step's, on passed-accepted",
1987
+ skipped:
1988
+ "planned tests an earlier row of the group proved red, their file unchanged since: not run again, and not on this row",
1989
+ gaps: "[{task, test, name, kind, detail}]: every planned test whose state is not red, red-not-loaded or passed-accepted; kind is the state",
1990
+ row: "the build-tests-red row written",
1991
+ log: "where the report was written (as given)",
525
1992
  },
526
1993
  exitCodes: {
527
- [EXIT_NO_GATE]: `${BKIT_TEST_FILE_SCRIPT} is not declared in package.json`,
528
- [EXIT_COULD_NOT_RUN]: "the script could not run (exit 126 / 127)",
1994
+ [EXIT_NO_GATE]: `${BKIT_TEST_FILE_SCRIPT} is not declared in package.json: the row holds the blobs with enforced false, and no test ran`,
1995
+ [EXIT_COULD_NOT_RUN]:
1996
+ "bkit:lint:file or bkit:test:file could not run (exit 126 / 127); nothing written",
529
1997
  },
530
1998
  run: (ctx) => {
531
1999
  const workflow = String(ctx.flags.workflow);
532
- const subPlan = positiveInt(ctx.flags.subPlan, "sub-plan");
533
- const task = positiveInt(ctx.flags.task, "task")!;
534
- const name = String(ctx.flags.name).trim();
535
- if (name === "") throw new CommandError(EXIT.USAGE, "--name must be non-empty");
2000
+ const group = positiveInt(ctx.flags.group, "group")!;
536
2001
  const logArg = String(ctx.flags.log);
537
2002
  if (logArg === "") throw new CommandError(EXIT.USAGE, "--log must be non-empty");
2003
+ const log = path.resolve(ctx.cwd, logArg);
538
2004
  const top = toplevel(ctx.cwd);
539
- if (resolveGate(top, [BKIT_TEST_FILE_SCRIPT]) === null) {
540
- ctx.log(`${BKIT_TEST_FILE_SCRIPT} is not declared — ${NO_TEST_FILE_HINT}`);
541
- return EXIT_NO_GATE;
542
- }
543
- const test = repoTestPath(top, String(ctx.flags.test));
544
- const blob = git(top, ["hash-object", "--", test]);
2005
+ const exists = (p: string) => fs.existsSync(path.resolve(ctx.cwd, p));
2006
+ const plan = (ctx.flags.plan as string[]).find(exists);
2007
+ const planText = plan ? fs.readFileSync(path.resolve(ctx.cwd, plan), "utf-8") : "";
2008
+ const parsed = parsePlanBlocks(planText);
545
2009
  const status = statusFile(ctx);
546
- const prior = fs.existsSync(status)
547
- ? redRows(status, workflow, subPlan)
548
- .reverse()
549
- .find((r) => r.task === task && r.test === test && r.testName === name)
550
- : undefined;
551
- if (prior !== undefined && prior.testBlob === blob) {
552
- const priorLog = Array.isArray(prior.artifacts) ? String(prior.artifacts[0]) : null;
553
- ctx.emit({ recorded: true, already: true, row: prior, log: priorLog });
554
- ctx.log(`red already recorded for task ${task}: "${name}" (test unchanged since)`);
555
- return EXIT.OK;
556
- }
557
- const log = path.resolve(ctx.cwd, logArg);
558
- const { exitCode, cases } = runTestFile(top, test, log);
559
- if (COULD_NOT_RUN_EXIT_CODES.has(exitCode)) {
560
- ctx.log(couldNotRunMessage(exitCode));
561
- return EXIT_COULD_NOT_RUN;
562
- }
563
- const refusal = judgeRed(exitCode, cases, name);
564
- if (refusal !== null) {
565
- ctx.emit({
566
- recorded: false,
567
- refused: refusal.kind,
568
- reason: refusal.reason,
569
- exitCode,
570
- log: logArg,
2010
+ const attempt = redAttempt(status, workflow, group);
2011
+ const refusal = planRefusal(plan, parsed);
2012
+ if (refusal !== undefined) {
2013
+ writeRedReport(log, {
2014
+ enforced: false,
2015
+ reason: refusal,
2016
+ plan,
2017
+ group,
2018
+ attempt,
2019
+ tests: [],
2020
+ details: new Map(),
2021
+ gaps: [],
2022
+ outputs: [],
571
2023
  });
572
- ctx.log(`not a red (${refusal.kind}): ${refusal.reason} — full output in ${logArg}`);
2024
+ ctx.emit({ enforced: false, reason: refusal, attempt, tests: [], gaps: [], log: logArg });
2025
+ ctx.log(`test-first not enforced: ${refusal}`);
573
2026
  return EXIT.NO;
574
2027
  }
575
- const snapshot = snapshotPath(log);
576
- fs.copyFileSync(path.join(top, test), snapshot);
577
- const failed = cases!.find((c) => isNamedCase(c.name, name))!;
2028
+ const g = parsed.groups.find((x) => x.group === group);
2029
+ if (g === undefined) throw new CommandError(EXIT.USAGE, `the plan has no GROUP ${group}`);
2030
+ if (g.tasks.length === 0)
2031
+ throw new CommandError(EXIT.USAGE, `GROUP ${group} has no tasks; nothing to run`);
2032
+ const done = new Set(parsed.tasks.filter((t) => t.done).map((t) => t.task));
2033
+ // A red an earlier row proved stands while the test's own lines are unchanged, so a
2034
+ // retry, a handover or a task added to the group runs only what is left, and an edit
2035
+ // to another test of the same file costs no proof. A test whose lines could not be
2036
+ // found, and rows from before per-test locking, stand while the whole file is unchanged.
2037
+ const proven = redEntries(status, workflow, group);
2038
+ const textOf = (test: string) => {
2039
+ const abs = path.join(top, test);
2040
+ return fs.existsSync(abs) ? fs.readFileSync(abs, "utf-8") : null;
2041
+ };
2042
+ const standing = (t: RedTest) => {
2043
+ const earlier = proven.get(redKey(t.task, t.test, t.testName));
2044
+ if (earlier === undefined || !RED_PROVEN.has(earlier.state)) return false;
2045
+ if (earlier.block === undefined || earlier.block === null)
2046
+ return git(top, ["hash-object", "--", t.test]) === earlier.blob;
2047
+ return blockHash(textOf(t.test), t.testName) === earlier.block;
2048
+ };
2049
+ const startSha = groupStartSha(status, workflow, group);
2050
+ /** The test's lines at the group's start commit: it existed before this group's tests step. */
2051
+ const blockAtStart = (t: RedTest) => {
2052
+ if (startSha === undefined) return null;
2053
+ const shown = runGit(top, ["show", `${startSha}:${t.test}`]);
2054
+ return shown.ok ? blockHash(shown.stdout, t.testName) : null;
2055
+ };
2056
+ const tests: RedTest[] = [];
2057
+ let skipped = 0;
2058
+ for (const s of readTestFirst(planText, group)) {
2059
+ if (s.kind !== "test" || done.has(s.task)) continue;
2060
+ for (const t of s.tests) {
2061
+ const planned: RedTest = {
2062
+ task: s.task,
2063
+ test: t.test,
2064
+ testName: t.name,
2065
+ state: "unknown",
2066
+ blob: null,
2067
+ };
2068
+ if (standing(planned)) skipped += 1;
2069
+ else tests.push(planned);
2070
+ }
2071
+ }
2072
+ if (skipped > 0)
2073
+ ctx.log(`tests red: ${skipped} planned test(s) already red, not run again`);
2074
+ const enforced = resolveGate(top, [BKIT_TEST_FILE_SCRIPT]) !== null;
2075
+ const lint = resolveGate(top, [BKIT_LINT_FILE_SCRIPT]) !== null;
2076
+ if (enforced && !lint && tests.length > 0)
2077
+ ctx.log(
2078
+ `${BKIT_LINT_FILE_SCRIPT} is not declared — the tests are not linted before their red`,
2079
+ );
2080
+ const writerLog = str(ctx.flags.writerLog);
2081
+ const writerPath = writerLog === undefined ? undefined : path.resolve(ctx.cwd, writerLog);
2082
+ const passes =
2083
+ writerPath !== undefined && fs.existsSync(writerPath)
2084
+ ? passesLines(fs.readFileSync(writerPath, "utf-8"))
2085
+ : [];
2086
+ const details = new Map<RedTest, string>();
2087
+ const outputs: Array<{ title: string; text: string }> = [];
2088
+ const couldNotRun = (script: string, exitCode: number) =>
2089
+ `${gateScope(script)} could not run (exit ${exitCode}: ${exitCode === 126 ? "not executable" : "command not found"}) — no test executed`;
2090
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bkit-tests-red-"));
2091
+ try {
2092
+ for (const file of [...new Set(tests.map((t) => t.test))]) {
2093
+ const mine = tests.filter((t) => t.test === file);
2094
+ if (!fs.existsSync(path.join(top, file))) {
2095
+ for (const t of mine) {
2096
+ t.state = "missing";
2097
+ details.set(t, `\`${file}\` does not exist`);
2098
+ }
2099
+ continue;
2100
+ }
2101
+ // `-w` stores the file in git: green and `steps finish` read the blob back later.
2102
+ const blob = git(top, ["hash-object", "-w", "--", file]);
2103
+ const text = textOf(file);
2104
+ for (const t of mine) {
2105
+ t.blob = blob;
2106
+ t.block = blockHash(text, t.testName);
2107
+ }
2108
+ if (!enforced) continue;
2109
+ const runLog = path.join(dir, "run.log");
2110
+ if (lint) {
2111
+ const linted = runToLog([...scriptCommand(BKIT_LINT_FILE_SCRIPT), file], top, runLog);
2112
+ outputs.push({
2113
+ title: `Lint output — \`${file}\``,
2114
+ text: fs.readFileSync(runLog, "utf-8"),
2115
+ });
2116
+ if (COULD_NOT_RUN_EXIT_CODES.has(linted)) {
2117
+ ctx.log(couldNotRun(BKIT_LINT_FILE_SCRIPT, linted));
2118
+ return EXIT_COULD_NOT_RUN;
2119
+ }
2120
+ if (linted !== 0) {
2121
+ for (const t of mine) {
2122
+ t.state = "lint";
2123
+ details.set(
2124
+ t,
2125
+ `${BKIT_LINT_FILE_SCRIPT} exited ${linted} on \`${file}\`: fix the test until it lints clean (output below)`,
2126
+ );
2127
+ }
2128
+ continue;
2129
+ }
2130
+ }
2131
+ const { exitCode, cases } = runTestFile(top, file, runLog);
2132
+ outputs.push({
2133
+ title: `Runner output — \`${file}\``,
2134
+ text: fs.readFileSync(runLog, "utf-8"),
2135
+ });
2136
+ if (COULD_NOT_RUN_EXIT_CODES.has(exitCode)) {
2137
+ ctx.log(couldNotRun(BKIT_TEST_FILE_SCRIPT, exitCode));
2138
+ return EXIT_COULD_NOT_RUN;
2139
+ }
2140
+ for (const t of mine) {
2141
+ const judged = judgeRed(cases, t.testName, file);
2142
+ const accepted =
2143
+ judged.state === "vacuous"
2144
+ ? passes.find(
2145
+ (p) => p.task === t.task && p.test === t.test && p.name === t.testName,
2146
+ )
2147
+ : undefined;
2148
+ const before = judged.state === "vacuous" ? blockAtStart(t) : null;
2149
+ if (accepted !== undefined) {
2150
+ t.state = "passed-accepted";
2151
+ t.reason = accepted.reason;
2152
+ details.set(t, `passes; accepted by the tests step: ${accepted.reason}`);
2153
+ } else if (before !== null && before !== t.block) {
2154
+ t.state = "updated";
2155
+ t.base = startSha;
2156
+ details.set(
2157
+ t,
2158
+ `an existing test, changed since the group started (\`${startSha!.slice(0, 9)}\`) and passing: the verifier judges its diff — it may add expectations, never remove or loosen one`,
2159
+ );
2160
+ } else {
2161
+ t.state = judged.state;
2162
+ details.set(t, judged.detail);
2163
+ }
2164
+ }
2165
+ }
2166
+ } finally {
2167
+ fs.rmSync(dir, { recursive: true, force: true });
2168
+ }
2169
+ const gaps: RedGap[] = [];
2170
+ for (const t of tests) {
2171
+ if (RED_PROVEN.has(t.state) || t.state === "unknown") continue;
2172
+ const gap: RedGap = {
2173
+ task: t.task,
2174
+ test: t.test,
2175
+ name: t.testName,
2176
+ kind: t.state as RedGap["kind"],
2177
+ detail: details.get(t) ?? t.state,
2178
+ };
2179
+ gaps.push(gap);
2180
+ ctx.log(
2181
+ `not a red (${gap.kind}): TASK ${gap.task} \`${gap.test}\` "${gap.name}" — ${gap.detail}`,
2182
+ );
2183
+ }
2184
+ writeRedReport(log, {
2185
+ enforced,
2186
+ reason: enforced
2187
+ ? undefined
2188
+ : `\`${BKIT_TEST_FILE_SCRIPT}\` is not declared in package.json`,
2189
+ plan,
2190
+ group,
2191
+ attempt,
2192
+ tests,
2193
+ details,
2194
+ gaps,
2195
+ outputs,
2196
+ });
578
2197
  const written = writeStepRow(
579
2198
  ctx.cwd,
580
2199
  {
581
- file: statusFile(ctx),
2200
+ file: status,
582
2201
  workflow,
583
2202
  step: RED_STEP,
584
- subPlan,
585
- artifacts: [path.relative(ctx.cwd, log), path.relative(ctx.cwd, snapshot)],
586
- extras: {
587
- task,
588
- test,
589
- testName: name,
590
- testBlob: blob,
591
- exitCode,
592
- failure: failed.message.slice(0, 200),
593
- },
2203
+ group,
2204
+ attempt,
2205
+ artifacts: [path.relative(ctx.cwd, log)],
2206
+ extras: { enforced, tests },
594
2207
  commit: "none",
595
2208
  },
596
2209
  ctx.log,
597
2210
  );
598
- ctx.emit({ recorded: true, exitCode, row: written.row, log: logArg });
599
- ctx.log(`red recorded for task ${task}: "${name}" failed (${failed.state})`);
2211
+ ctx.emit({ enforced, attempt, tests, skipped, gaps, row: written.row, log: logArg });
2212
+ if (gaps.length > 0) {
2213
+ ctx.log(`tests red: ${gaps.length} gap(s) — report in ${logArg}`);
2214
+ return EXIT.NO;
2215
+ }
2216
+ if (!enforced) {
2217
+ ctx.log(`${BKIT_TEST_FILE_SCRIPT} is not declared — ${NO_TEST_FILE_HINT}`);
2218
+ return EXIT_NO_GATE;
2219
+ }
2220
+ ctx.log(`tests red: ${tests.length} planned test(s) red — report in ${logArg}`);
600
2221
  return EXIT.OK;
601
2222
  },
602
2223
  },
603
2224
  green: {
604
2225
  summary:
605
- "Audit a sub-plan's test-first evidence (a red per planned test, the test unchanged since, the named test passing now); exit 0 no gaps, 1 gaps.",
2226
+ "Audit a plan's test-first evidence (each TEST FIRST test red at its group's tests step, by the build-tests-red row, or accepted there as passing before any task code; the named test passing now); exit 0 no gaps, 1 gaps or a plan with no task markers.",
2227
+ use: "workflow",
606
2228
  flags: [
607
2229
  FILE_FLAG,
608
2230
  WORKFLOW_FLAG,
609
- SUB_PLAN_FLAG,
2231
+ { ...GROUP_FLAG, help: "audit only this task group's tasks (positive integer)" },
610
2232
  {
611
2233
  name: "plan",
612
2234
  type: "string",
@@ -614,11 +2236,6 @@ export const testsCommand: CommandSpec = {
614
2236
  repeatable: true,
615
2237
  help: "candidate plan file; the first that exists is audited",
616
2238
  },
617
- {
618
- name: "brief",
619
- type: "string",
620
- help: "the build input, audited when no plan file exists and it carries `**Task N:**` lines with `Test first:`",
621
- },
622
2239
  {
623
2240
  name: "log",
624
2241
  type: "string",
@@ -627,10 +2244,19 @@ export const testsCommand: CommandSpec = {
627
2244
  },
628
2245
  ],
629
2246
  output: {
630
- enforced: "false when there is nothing to audit (reason says why)",
2247
+ enforced:
2248
+ "false when the audit could not run: no plan file, a plan with no task markers (exit 1), or the test-file script undeclared (exit 0)",
631
2249
  reason: "why test-first is not enforced",
632
- plan: "the plan file audited (the brief when no plan file exists and it carries Test first lines)",
633
- gaps: "[{task, kind, detail}]: no-test-first | test-first-malformed | no-red | red-mismatch | red-evidence-missing | test-changed | not-green",
2250
+ plan: "the plan file audited",
2251
+ verdict:
2252
+ "PASS no gaps | FAIL a gap's fixable is false | CONCERNS every gap's fixable is true | null when a gap needs a diff read, or the audit could not run",
2253
+ gaps: "[{task, kind, detail, fixable, logLine}]: no-test-first | test-first-malformed | test-first-unread (task null: a plan-level format problem) | not-green | exception-refused (a task whose only test field is a TEST EXCEPTION not honoured) | delete-still-referenced (a DELETE line's file still exists, or its name is still found in the tree); fixable is null when the gap needs a diff read; logLine is the line the verifier appends",
2254
+ alreadyLanded:
2255
+ "[{task, test, name, reason}]: tests the tests step accepted as passing before any task code (passed-accepted on the red row); no red is due for them",
2256
+ unproven:
2257
+ "[{task, test, name, detail}]: tests with no proven red (no build-tests-red row, or a state other than red, red-not-loaded, updated or passed-accepted) and no honoured TEST EXCEPTION; never a gap — the owner answers them once, at the end of the build",
2258
+ updated:
2259
+ "[{task, test, name, base, additive, diff}]: existing tests the tests step changed (updated on the red row); base is the group's start commit; additive = the test's own lines since base gained lines and lost none (null when a side cannot be read), diff = their unified line diff; the verifier judges each diff — it may add expectations, never remove or loosen one",
634
2260
  log: "where the report was written (as given)",
635
2261
  },
636
2262
  exitCodes: {
@@ -638,44 +2264,66 @@ export const testsCommand: CommandSpec = {
638
2264
  },
639
2265
  run: (ctx) => {
640
2266
  const workflow = String(ctx.flags.workflow);
641
- const subPlan = positiveInt(ctx.flags.subPlan, "sub-plan");
2267
+ const group = positiveInt(ctx.flags.group, "group");
642
2268
  const logArg = String(ctx.flags.log);
643
2269
  if (logArg === "") throw new CommandError(EXIT.USAGE, "--log must be non-empty");
644
2270
  const log = path.resolve(ctx.cwd, logArg);
645
2271
  const top = toplevel(ctx.cwd);
646
2272
  const exists = (p: string) => fs.existsSync(path.resolve(ctx.cwd, p));
647
- const planFile = (ctx.flags.plan as string[]).find(exists);
648
- const briefArg = str(ctx.flags.brief);
649
- const brief =
650
- planFile === undefined && briefArg !== undefined && exists(briefArg)
651
- ? briefArg
652
- : undefined;
653
- const plan = planFile ?? brief;
654
- const specs = plan
655
- ? readTestFirst(fs.readFileSync(path.resolve(ctx.cwd, plan), "utf-8"))
656
- : [];
2273
+ const plan = (ctx.flags.plan as string[]).find(exists);
2274
+ const planText = plan ? fs.readFileSync(path.resolve(ctx.cwd, plan), "utf-8") : "";
2275
+ const parsed = parsePlanBlocks(planText);
2276
+ const specs = readTestFirst(planText, group);
2277
+ // A plan with no task markers is an old-format plan: nothing it says can be audited,
2278
+ // and passing it would let its tests go unchecked.
2279
+ const refusal = planRefusal(plan, parsed);
2280
+ if (refusal === undefined && group !== undefined) {
2281
+ const g = parsed.groups.find((x) => x.group === group);
2282
+ if (g === undefined) throw new CommandError(EXIT.USAGE, `the plan has no GROUP ${group}`);
2283
+ if (g.tasks.length === 0)
2284
+ throw new CommandError(EXIT.USAGE, `GROUP ${group} has no tasks; nothing to audit`);
2285
+ }
657
2286
  const reason =
658
- plan === undefined
659
- ? "no plan file — the build runs from its brief"
660
- : !specs.some((s) => s.kind !== "missing")
661
- ? brief !== undefined
662
- ? "no plan file, and the brief has no `**Task N:**` lines with `Test first:`"
663
- : "the plan has no `Test first:` lines"
664
- : resolveGate(top, [BKIT_TEST_FILE_SCRIPT]) === null
665
- ? `\`${BKIT_TEST_FILE_SCRIPT}\` is not declared in package.json`
666
- : undefined;
667
- const empty = { rows: [], gaps: [], diffs: [], outputs: [] };
2287
+ refusal ??
2288
+ (resolveGate(top, [BKIT_TEST_FILE_SCRIPT]) === null
2289
+ ? `\`${BKIT_TEST_FILE_SCRIPT}\` is not declared in package.json`
2290
+ : undefined);
2291
+ const empty = {
2292
+ verdict: null,
2293
+ rows: [],
2294
+ gaps: [],
2295
+ exceptions: [],
2296
+ landed: [],
2297
+ unproven: [],
2298
+ updated: [],
2299
+ outputs: [],
2300
+ };
668
2301
  if (reason !== undefined) {
669
2302
  writeAuditReport(log, { enforced: false, reason, plan, ...empty });
670
- ctx.emit({ enforced: false, reason, plan: plan ?? null, gaps: [], log: logArg });
2303
+ ctx.emit({
2304
+ enforced: false,
2305
+ reason,
2306
+ plan: plan ?? null,
2307
+ verdict: null,
2308
+ gaps: [],
2309
+ alreadyLanded: [],
2310
+ unproven: [],
2311
+ updated: [],
2312
+ log: logArg,
2313
+ });
671
2314
  ctx.log(`test-first not enforced: ${reason}`);
672
- return EXIT.OK;
2315
+ return refusal === undefined ? EXIT.OK : EXIT.NO;
673
2316
  }
674
2317
 
675
- const reds = redRows(statusFile(ctx), workflow, subPlan);
2318
+ const reds = redEntries(statusFile(ctx), workflow);
2319
+ const groupOf = (task: number) => parsed.tasks.find((b) => b.task === task)?.group;
2320
+ const landed: Landed[] = [];
2321
+ const unproven: Unproven[] = [];
2322
+ const updated: Updated[] = [];
2323
+ const statusRel = repoRel(top, ctx.cwd, statusFile(ctx));
2324
+ const exceptions: ExceptionResult[] = [];
676
2325
  const gaps: Gap[] = [];
677
2326
  const rows: AuditRow[] = [];
678
- const diffs: Array<{ task: number; test: string; diff: string }> = [];
679
2327
  const outputs: Array<{ test: string; tail: string }> = [];
680
2328
  const greens = new Map<string, { cases: JunitCase[] | null; tail: string }>();
681
2329
  /** One run per test file; null when the script could not run. */
@@ -696,103 +2344,97 @@ export const testsCommand: CommandSpec = {
696
2344
  return run;
697
2345
  };
698
2346
  for (const s of specs) {
699
- if (s.kind === "na") continue;
700
- if (s.kind !== "test") {
701
- gaps.push({
2347
+ if (s.kind === "na") {
2348
+ const block = parsed.tasks.find((t) => t.task === s.task)!;
2349
+ if (block.testFirst !== undefined || block.testException === undefined) continue;
2350
+ const refused = exceptionRefusal(
2351
+ top,
2352
+ repoRel(top, ctx.cwd, plan!),
2353
+ block.testException.lines,
2354
+ statusRel,
2355
+ );
2356
+ exceptions.push({
702
2357
  task: s.task,
703
- kind: s.kind === "missing" ? "no-test-first" : "test-first-malformed",
704
- detail:
705
- s.kind === "missing"
706
- ? "the task has no `Test first:` line"
707
- : "`Test first:` is neither `n/a — {reason}` nor `;`-separated entries of a backticked test file and test name",
2358
+ refusal: refused,
2359
+ cleared: refused === null ? ["the task's `TEST FIRST` check"] : [],
708
2360
  });
2361
+ if (refused !== null)
2362
+ gaps.push(
2363
+ gapOf(
2364
+ s.task,
2365
+ EXCEPTION_REFUSED,
2366
+ `the task's only test field is a \`**TEST EXCEPTION:**\`, not honoured: ${refused}`,
2367
+ ),
2368
+ );
709
2369
  continue;
710
2370
  }
711
- const taskReds = reds.filter((r) => r.task === s.task);
712
- const planned = (r: Record<string, unknown>) =>
713
- s.tests.some((t) => t.test === r.test && t.name === r.testName);
2371
+ if (s.kind !== "test") {
2372
+ gaps.push(
2373
+ gapOf(
2374
+ s.task,
2375
+ s.kind === "missing" ? "no-test-first" : "test-first-malformed",
2376
+ s.kind === "missing"
2377
+ ? "the task has neither a `**TEST FIRST:**` nor a `**TEST EXCEPTION:**` field"
2378
+ : "`**TEST FIRST:**` is neither `n/a — {reason}` nor one sub-bullet per test naming a backticked test file and test name",
2379
+ ),
2380
+ );
2381
+ continue;
2382
+ }
2383
+ const excepted = s.exception;
2384
+ const exception: ExceptionResult | undefined =
2385
+ excepted === undefined
2386
+ ? undefined
2387
+ : {
2388
+ task: s.task,
2389
+ refusal: exceptionRefusal(
2390
+ top,
2391
+ repoRel(top, ctx.cwd, plan!),
2392
+ excepted.lines,
2393
+ statusRel,
2394
+ ),
2395
+ cleared: [],
2396
+ };
2397
+ if (exception !== undefined) exceptions.push(exception);
714
2398
  for (const t of s.tests) {
715
2399
  const row: AuditRow = {
716
2400
  task: s.task,
717
2401
  test: `\`${t.test}\` "${t.name}"`,
718
- red: "missing",
719
- changed: "—",
2402
+ red: "no row",
720
2403
  green: "—",
721
2404
  };
722
2405
  rows.push(row);
723
- const red = [...taskReds]
724
- .reverse()
725
- .find((r) => r.test === t.test && r.testName === t.name);
726
- const unplanned = taskReds.filter((r) => !planned(r));
727
- if (red === undefined && unplanned.length > 0) {
728
- const recorded: string[] = [];
729
- for (const r of unplanned) {
730
- const rTest = String(r.test);
731
- const rName = String(r.testName);
732
- let now = "file missing";
733
- if (fs.existsSync(path.join(top, rTest))) {
734
- const run = greenRun(rTest);
735
- if (run === null) return EXIT_COULD_NOT_RUN;
736
- const why = judgeGreen(run.cases, rName);
737
- now = why === null ? "passes now" : `not passing: ${why}`;
738
- }
739
- recorded.push(`\`${rTest}\` "${rName}" (${now})`);
740
- }
741
- gaps.push({
742
- task: s.task,
743
- kind: "red-mismatch",
744
- detail: `no red for \`${t.test}\` "${t.name}"; reds recorded for ${recorded.join(", ")}`,
745
- });
746
- } else if (red === undefined) {
747
- gaps.push({
748
- task: s.task,
749
- kind: "no-red",
750
- detail: `no red recorded for "${t.name}" — the test was never seen failing before the code`,
751
- });
2406
+ const clearing =
2407
+ exception?.refusal === null &&
2408
+ excepted!.tests.some((e) => e.test === t.test && e.name === t.name)
2409
+ ? exception
2410
+ : undefined;
2411
+ const gap = (kind: GapKind, detail: string) =>
2412
+ gaps.push(gapOf(s.task, kind, detail, { file: t.test, name: t.name }));
2413
+ const noRed = (detail: string) => {
2414
+ if (clearing !== undefined)
2415
+ clearing.cleared.push(`the red for \`${t.test}\` "${t.name}"`);
2416
+ else unproven.push({ task: s.task, test: t.test, name: t.name, detail });
2417
+ };
2418
+ const red = reds.get(redKey(s.task, t.test, t.name));
2419
+ if (red === undefined)
2420
+ noRed(
2421
+ `no tests step recorded a red for it (\`bkit tests red --group=${groupOf(s.task) ?? "N"}\`)`,
2422
+ );
2423
+ else if (red.state === "passed-accepted") {
2424
+ row.red = red.state;
2425
+ landed.push({ task: s.task, test: t.test, name: t.name, reason: red.reason ?? "" });
2426
+ } else if (RED_PROVEN.has(red.state)) {
2427
+ row.red = red.state;
2428
+ if (red.state === "updated")
2429
+ updated.push(updatedEntry(top, s.task, t, red.base ?? null));
752
2430
  } else {
753
- row.red = `recorded ${String(red.at)}`;
754
- const snap = (Array.isArray(red.artifacts) ? red.artifacts : [])
755
- .map(String)
756
- .find((a) => a.endsWith(".snapshot"));
757
- const snapAbs = snap === undefined ? undefined : path.resolve(ctx.cwd, snap);
758
- const testAbs = path.join(top, t.test);
759
- if (snapAbs === undefined || !fs.existsSync(snapAbs)) {
760
- row.changed = "unknown";
761
- gaps.push({
762
- task: s.task,
763
- kind: "red-evidence-missing",
764
- detail: `the red's test snapshot ${snap ?? "(none recorded)"} is missing`,
765
- });
766
- } else if (!fs.existsSync(testAbs)) {
767
- row.changed = "deleted";
768
- gaps.push({
769
- task: s.task,
770
- kind: "test-changed",
771
- detail: `\`${t.test}\` no longer exists`,
772
- });
773
- } else if (!fs.readFileSync(snapAbs).equals(fs.readFileSync(testAbs))) {
774
- row.changed = "yes";
775
- gaps.push({
776
- task: s.task,
777
- kind: "test-changed",
778
- detail: `\`${t.test}\` differs from its red snapshot for "${t.name}" (diff below)`,
779
- });
780
- const diff = spawnSync(
781
- "git",
782
- ["diff", "--no-index", "--no-color", "--", path.relative(top, snapAbs), t.test],
783
- { cwd: top, encoding: "utf-8" },
784
- );
785
- diffs.push({ task: s.task, test: t.test, diff: diff.stdout.trimEnd() });
786
- } else row.changed = "no";
2431
+ row.red = red.state;
2432
+ noRed(`the tests step left it as \`${red.state}\`, not red`);
787
2433
  }
788
2434
 
789
2435
  if (!fs.existsSync(path.join(top, t.test))) {
790
2436
  row.green = "file missing";
791
- gaps.push({
792
- task: s.task,
793
- kind: "not-green",
794
- detail: `\`${t.test}\` does not exist`,
795
- });
2437
+ gap("not-green", `\`${t.test}\` does not exist`);
796
2438
  continue;
797
2439
  }
798
2440
  const green = greenRun(t.test);
@@ -800,17 +2442,169 @@ export const testsCommand: CommandSpec = {
800
2442
  const why = judgeGreen(green.cases, t.name);
801
2443
  row.green = why === null ? "passed" : "not passed";
802
2444
  if (why !== null) {
803
- gaps.push({ task: s.task, kind: "not-green", detail: why });
2445
+ gap("not-green", why);
804
2446
  if (!outputs.some((o) => o.test === t.test))
805
2447
  outputs.push({ test: t.test, tail: green.tail });
806
2448
  }
807
2449
  }
808
2450
  }
809
- writeAuditReport(log, { enforced: true, plan, rows, gaps, diffs, outputs });
810
- ctx.emit({ enforced: true, plan, gaps, log: logArg });
811
- ctx.log(`test-first audit: ${gaps.length} gap(s) — report in ${logArg}`);
2451
+ gaps.push(
2452
+ ...deleteGaps(
2453
+ top,
2454
+ repoRel(top, ctx.cwd, plan!),
2455
+ parsed.tasks.filter((t) => group === undefined || t.group === group),
2456
+ parsed.tasks,
2457
+ ),
2458
+ );
2459
+ for (const e of parsed.errors)
2460
+ gaps.push(
2461
+ gapOf(
2462
+ null,
2463
+ "test-first-unread",
2464
+ `\`${plan}:${e.line}\` ${e.message} — tests there were not audited; the plan's blocks need fixing`,
2465
+ ),
2466
+ );
2467
+ const verdict = verdictOf(gaps);
2468
+ writeAuditReport(log, {
2469
+ enforced: true,
2470
+ plan,
2471
+ verdict,
2472
+ rows,
2473
+ gaps,
2474
+ exceptions,
2475
+ landed,
2476
+ unproven,
2477
+ updated,
2478
+ outputs,
2479
+ });
2480
+ ctx.emit({
2481
+ enforced: true,
2482
+ plan,
2483
+ verdict,
2484
+ gaps,
2485
+ alreadyLanded: landed,
2486
+ unproven,
2487
+ updated,
2488
+ log: logArg,
2489
+ });
2490
+ ctx.log(
2491
+ `test-first audit: ${gaps.length} gap(s), ${unproven.length} unproven — report in ${logArg}`,
2492
+ );
812
2493
  return gaps.length === 0 ? EXIT.OK : EXIT.NO;
813
2494
  },
814
2495
  },
2496
+ "plan-check": {
2497
+ summary: `Check a plan's group and task blocks: the block format, DONE WHEN and TEST FIRST on every task, DEPENDS ON order, the caps from package.json \`bkit.workflows.tasks\` (defaults: ${capsText(CONFIG_DEFAULTS.tasks)}), a Reuse Inventory NEW row per CREATE file, a Supersession Inventory row per DELETE, a \`Task\` on every Derived Test Matrix row and a row per test-bearing task, and that each READ / UPDATE / DELETE file and symbol exists; exit 0 no problems, 1 refused.`,
2498
+ use: "by-hand",
2499
+ flags: [
2500
+ {
2501
+ name: "plan",
2502
+ type: "string",
2503
+ required: true,
2504
+ help: "the plan file checked",
2505
+ },
2506
+ {
2507
+ name: "existence",
2508
+ type: "string",
2509
+ help: "refuse (default): a missing READ / UPDATE / DELETE file or symbol refuses the plan; report: it is listed only",
2510
+ },
2511
+ {
2512
+ name: "log",
2513
+ type: "string",
2514
+ required: true,
2515
+ help: "file the report is written to, `Problems:` header first (directories are created)",
2516
+ },
2517
+ ],
2518
+ output: {
2519
+ plan: "the plan file checked (as given)",
2520
+ groups:
2521
+ "[{group, title, tasks, tests, kb}]: each group's tasks, planned TEST FIRST tests and size",
2522
+ tasks: "[{task, group, tests, files}]: each task's planned tests and distinct test files",
2523
+ problems:
2524
+ "[{line, kind, message}]: format | stray | field | depends | cap | inventory | matrix | supersession | markers — each refuses the plan",
2525
+ existence:
2526
+ "[{line, task, message}]: READ / UPDATE / DELETE files or symbols not found; refuses only with --existence=refuse",
2527
+ existing:
2528
+ "[{line, task, test, name}]: planned tests of open tasks whose name a test in their file already carries; reported only, never refuses",
2529
+ log: "where the report was written (as given)",
2530
+ },
2531
+ run: (ctx) => {
2532
+ const logArg = String(ctx.flags.log);
2533
+ if (logArg === "") throw new CommandError(EXIT.USAGE, "--log must be non-empty");
2534
+ const existenceArg = str(ctx.flags.existence) ?? "refuse";
2535
+ if (existenceArg !== "refuse" && existenceArg !== "report")
2536
+ throw new CommandError(
2537
+ EXIT.USAGE,
2538
+ `--existence must be refuse or report, got "${existenceArg}"`,
2539
+ );
2540
+ const plan = String(ctx.flags.plan);
2541
+ const planAbs = path.resolve(ctx.cwd, plan);
2542
+ if (!fs.statSync(planAbs, { throwIfNoEntry: false })?.isFile())
2543
+ throw new CommandError(EXIT.USAGE, `--plan ${plan} does not exist`);
2544
+ const text = fs.readFileSync(planAbs, "utf-8");
2545
+ const caps = planCaps(ctx.cwd);
2546
+ const { groups, sizes, problems } = checkPlanText(text, caps);
2547
+ const existence = planExistence(text, toplevel(ctx.cwd));
2548
+ const existing = existingTestNames(text, toplevel(ctx.cwd));
2549
+ const refused = problems.length > 0 || (existenceArg === "refuse" && existence.length > 0);
2550
+
2551
+ const out = [
2552
+ `Problems: ${problems.length}`,
2553
+ `Existence: ${existence.length}`,
2554
+ "",
2555
+ "<!-- verdict:end -->",
2556
+ "",
2557
+ "# Plan Check",
2558
+ "",
2559
+ `Plan: \`${plan}\`. Caps: ${capsText(caps)}.`,
2560
+ "",
2561
+ "## Groups",
2562
+ "",
2563
+ ];
2564
+ if (groups.length === 0) out.push("No group blocks read.");
2565
+ else {
2566
+ out.push(
2567
+ "| Group | Title | Tasks | TEST FIRST tests | KB |",
2568
+ "| --- | --- | --- | --- | --- |",
2569
+ );
2570
+ for (const g of groups)
2571
+ out.push(`| ${g.group} | ${g.title} | ${g.tasks} | ${g.tests} | ${g.kb.toFixed(1)} |`);
2572
+ }
2573
+ if (sizes.length > 0) {
2574
+ out.push(
2575
+ "",
2576
+ "## Tasks",
2577
+ "",
2578
+ "| Task | Group | Tests | Test files |",
2579
+ "| --- | --- | --- | --- |",
2580
+ );
2581
+ for (const t of sizes) out.push(`| ${t.task} | ${t.group} | ${t.tests} | ${t.files} |`);
2582
+ }
2583
+ out.push("", "## Problems", "");
2584
+ if (problems.length === 0) out.push("None.");
2585
+ for (const p of problems) out.push(`- \`${plan}:${p.line}\` \`${p.kind}\` — ${p.message}`);
2586
+ out.push(
2587
+ "",
2588
+ `## Existence (${existenceArg === "refuse" ? "refuses the plan" : "reported only"})`,
2589
+ "",
2590
+ );
2591
+ if (existence.length === 0) out.push("None.");
2592
+ for (const e of existence) out.push(`- \`${plan}:${e.line}\` — ${e.message}`);
2593
+ if (existing.length > 0)
2594
+ out.push("", "## Planned test names already in use (reported only)", "");
2595
+ for (const e of existing)
2596
+ out.push(
2597
+ `- \`${plan}:${e.line}\` TASK ${e.task}: \`${e.test}\` already holds a test named "${e.name}" — name the update to it in TEST FIRST, or give the new test a new name`,
2598
+ );
2599
+ const log = path.resolve(ctx.cwd, logArg);
2600
+ fs.mkdirSync(path.dirname(log), { recursive: true });
2601
+ fs.writeFileSync(log, `${out.join("\n")}\n`);
2602
+ ctx.emit({ plan, groups, tasks: sizes, problems, existence, existing, log: logArg });
2603
+ ctx.log(
2604
+ `plan check: ${problems.length} problem(s), ${existence.length} existence finding(s)${refused ? " — refused" : ""}; report in ${logArg}`,
2605
+ );
2606
+ return refused ? EXIT.NO : EXIT.OK;
2607
+ },
2608
+ },
815
2609
  },
816
2610
  };