@cbartens/bkit 5.5.1 → 6.0.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 (288) hide show
  1. package/.bkit/README.md +30 -20
  2. package/.bkit/core/.agents/docs.md +1 -0
  3. package/.bkit/core/.agents/rules.md +35 -26
  4. package/.bkit/core/.bkit/README.md +29 -19
  5. package/.bkit/core/.bkit/authoring.md +117 -0
  6. package/.bkit/core/.bkit/cloud.md +11 -11
  7. package/.bkit/core/.bkit/comparison.md +5 -5
  8. package/.bkit/core/.bkit/install.md +19 -18
  9. package/.bkit/core/.bkit/jsonl.md +202 -122
  10. package/.bkit/core/.bkit/lite.md +1 -1
  11. package/.bkit/core/.bkit/pipeline.md +49 -52
  12. package/.bkit/core/.bkit/prompts.md +91 -167
  13. package/.bkit/core/.bkit/providers.md +29 -32
  14. package/.bkit/core/.bkit/templates.md +12 -11
  15. package/.bkit/core/.bkit/trackers.md +51 -28
  16. package/.bkit/core/.bkit/workflows.md +394 -355
  17. package/.bkit/core/AGENTS.md +16 -22
  18. package/.bkit/core/docs/templates/bkit-audit-accessibility.md +14 -38
  19. package/.bkit/core/docs/templates/bkit-audit-adoption.md +16 -40
  20. package/.bkit/core/docs/templates/bkit-audit-agents.md +19 -43
  21. package/.bkit/core/docs/templates/bkit-audit-aio-seo.md +16 -40
  22. package/.bkit/core/docs/templates/bkit-audit-analytics.md +14 -38
  23. package/.bkit/core/docs/templates/bkit-audit-annotations.md +11 -35
  24. package/.bkit/core/docs/templates/bkit-audit-app-performance.md +15 -39
  25. package/.bkit/core/docs/templates/bkit-audit-app-store.md +12 -36
  26. package/.bkit/core/docs/templates/bkit-audit-architecture.md +16 -40
  27. package/.bkit/core/docs/templates/bkit-audit-ci-performance.md +16 -40
  28. package/.bkit/core/docs/templates/bkit-audit-config.md +18 -42
  29. package/.bkit/core/docs/templates/bkit-audit-cost.md +16 -40
  30. package/.bkit/core/docs/templates/bkit-audit-coverage.md +15 -39
  31. package/.bkit/core/docs/templates/bkit-audit-database.md +16 -40
  32. package/.bkit/core/docs/templates/bkit-audit-dead-code.md +15 -39
  33. package/.bkit/core/docs/templates/bkit-audit-deferrals.md +15 -38
  34. package/.bkit/core/docs/templates/bkit-audit-dependencies.md +16 -40
  35. package/.bkit/core/docs/templates/bkit-audit-documentation.md +11 -35
  36. package/.bkit/core/docs/templates/bkit-audit-duplication.md +18 -42
  37. package/.bkit/core/docs/templates/bkit-audit-failures.md +13 -37
  38. package/.bkit/core/docs/templates/bkit-audit-flakes.md +12 -36
  39. package/.bkit/core/docs/templates/bkit-audit-gates.md +14 -38
  40. package/.bkit/core/docs/templates/bkit-audit-issues.md +15 -39
  41. package/.bkit/core/docs/templates/bkit-audit-llm.md +16 -40
  42. package/.bkit/core/docs/templates/bkit-audit-logs.md +14 -38
  43. package/.bkit/core/docs/templates/bkit-audit-models.md +12 -36
  44. package/.bkit/core/docs/templates/bkit-audit-privacy.md +12 -36
  45. package/.bkit/core/docs/templates/bkit-audit-prompts.md +15 -39
  46. package/.bkit/core/docs/templates/bkit-audit-red-team.md +15 -39
  47. package/.bkit/core/docs/templates/bkit-audit-reliability.md +15 -39
  48. package/.bkit/core/docs/templates/bkit-audit-repo.md +12 -36
  49. package/.bkit/core/docs/templates/bkit-audit-security.md +13 -37
  50. package/.bkit/core/docs/templates/bkit-audit-slop.md +14 -38
  51. package/.bkit/core/docs/templates/bkit-audit-testing.md +12 -36
  52. package/.bkit/core/docs/templates/bkit-audit-usability.md +16 -40
  53. package/.bkit/core/docs/templates/bkit-plan.md +97 -226
  54. package/.bkit/core/engine/checks/after/ci-yml.ts +39 -29
  55. package/.bkit/core/engine/checks/after/github.ts +95 -17
  56. package/.bkit/core/engine/checks/after/index.ts +10 -7
  57. package/.bkit/core/engine/checks/after/suppression.ts +8 -5
  58. package/.bkit/core/engine/checks/after/tools.ts +46 -91
  59. package/.bkit/core/engine/checks/before/env.ts +2 -2
  60. package/.bkit/core/engine/checks/before/files.ts +5 -4
  61. package/.bkit/core/engine/checks/before/index.ts +18 -1
  62. package/.bkit/core/engine/checks/before/repo.ts +168 -2
  63. package/.bkit/core/engine/checks/tips/catalogue.ts +16 -5
  64. package/.bkit/core/engine/checks/tips/detect.ts +34 -0
  65. package/.bkit/core/engine/checks/tips/index.ts +6 -6
  66. package/.bkit/core/engine/cli.ts +20 -53
  67. package/.bkit/core/engine/commands/analysis.ts +171 -140
  68. package/.bkit/core/engine/commands/ci/ci.ts +6 -6
  69. package/.bkit/core/engine/commands/comply.ts +77 -103
  70. package/.bkit/core/engine/commands/install/doctor.ts +2 -2
  71. package/.bkit/core/engine/commands/install/install.ts +57 -18
  72. package/.bkit/core/engine/commands/install/uninstall.ts +65 -28
  73. package/.bkit/core/engine/commands/land.ts +18 -12
  74. package/.bkit/core/engine/commands/lib.ts +79 -17
  75. package/.bkit/core/engine/commands/registry.ts +4 -0
  76. package/.bkit/core/engine/commands/setup/setup.ts +7 -7
  77. package/.bkit/core/engine/commands/stats.ts +204 -0
  78. package/.bkit/core/engine/commands/tips.ts +10 -5
  79. package/.bkit/core/engine/commands/workflow/branches.ts +741 -62
  80. package/.bkit/core/engine/commands/workflow/challenge.ts +673 -0
  81. package/.bkit/core/engine/commands/workflow/context.ts +853 -29
  82. package/.bkit/core/engine/commands/workflow/counters.ts +267 -84
  83. package/.bkit/core/engine/commands/workflow/decisions.ts +541 -0
  84. package/.bkit/core/engine/commands/workflow/disk.ts +2 -2
  85. package/.bkit/core/engine/commands/workflow/fids.ts +793 -33
  86. package/.bkit/core/engine/commands/workflow/issues.ts +407 -64
  87. package/.bkit/core/engine/commands/workflow/ledgers.ts +884 -27
  88. package/.bkit/core/engine/commands/workflow/locks.ts +26 -2
  89. package/.bkit/core/engine/commands/workflow/plan-blocks.ts +582 -0
  90. package/.bkit/core/engine/commands/workflow/plan-versions.ts +21 -0
  91. package/.bkit/core/engine/commands/workflow/prs.ts +166 -30
  92. package/.bkit/core/engine/commands/workflow/repo.ts +58 -8
  93. package/.bkit/core/engine/commands/workflow/roadmap.ts +16 -7
  94. package/.bkit/core/engine/commands/workflow/run-facts.ts +501 -0
  95. package/.bkit/core/engine/commands/workflow/runs.ts +179 -49
  96. package/.bkit/core/engine/commands/workflow/runtime.ts +22 -1
  97. package/.bkit/core/engine/commands/workflow/status.ts +142 -29
  98. package/.bkit/core/engine/commands/workflow/step-table.ts +339 -0
  99. package/.bkit/core/engine/commands/workflow/steps.ts +213 -88
  100. package/.bkit/core/engine/commands/workflow/summary.ts +398 -0
  101. package/.bkit/core/engine/commands/workflow/tests.ts +1013 -148
  102. package/.bkit/core/engine/commands/workflow/tracker.ts +1195 -0
  103. package/.bkit/core/engine/commands/workflow/workflows.ts +984 -207
  104. package/.bkit/core/engine/commands/workflow/workstreams.ts +11 -5
  105. package/.bkit/core/engine/commands/workflow/worktrees.ts +185 -8
  106. package/.bkit/core/engine/install/manifest.ts +11 -9
  107. package/.bkit/core/engine/install/modules/cloud.ts +21 -13
  108. package/.bkit/core/engine/install/modules/core.ts +250 -3
  109. package/.bkit/core/engine/install/modules/opencode.ts +2 -2
  110. package/.bkit/core/engine/install/version-file.ts +6 -2
  111. package/.bkit/core/engine/ledgers/migrate.ts +92 -0
  112. package/.bkit/core/engine/ledgers/status.ts +92 -15
  113. package/.bkit/core/engine/ledgers/telemetry.ts +1261 -254
  114. package/.bkit/core/engine/render/hosts.ts +4 -0
  115. package/.bkit/core/engine/render/models.ts +12 -0
  116. package/.bkit/core/engine/shared/branches.ts +4 -1
  117. package/.bkit/core/engine/shared/egress.ts +7 -0
  118. package/.bkit/core/engine/shared/json.ts +25 -7
  119. package/.bkit/core/engine/shared/settings.ts +157 -20
  120. package/.bkit/core/engine/shared/text.ts +361 -0
  121. package/.bkit/core/engine/tools/grammar-density.ts +215 -0
  122. package/.bkit/core/engine/tools/instructions-sources.ts +834 -0
  123. package/.bkit/core/engine/tools/instructions.ts +1751 -0
  124. package/.bkit/core/hooks/gateguard-engine.js +24 -23
  125. package/.bkit/core/hooks/guardrails-shell.js +2 -3
  126. package/.bkit/core/hooks/guardrails-tiers.js +7 -1
  127. package/.bkit/core/hooks/readguard-engine.js +2 -2
  128. package/.bkit/core/prompts/bkit-audit-generate.md +62 -25
  129. package/.bkit/core/prompts/bkit-build-challenge-1.md +40 -17
  130. package/.bkit/core/prompts/bkit-build-challenge-2.md +44 -17
  131. package/.bkit/core/prompts/bkit-build-install-skills.md +26 -13
  132. package/.bkit/core/prompts/bkit-build-plan-convert.md +74 -0
  133. package/.bkit/core/prompts/bkit-build-plan-current.md +26 -13
  134. package/.bkit/core/prompts/bkit-build-plan-execute.md +74 -26
  135. package/.bkit/core/prompts/bkit-build-plan-gaps-remediate.md +64 -23
  136. package/.bkit/core/prompts/bkit-build-plan-verify.md +57 -33
  137. package/.bkit/core/prompts/bkit-debug-abort-handover.md +29 -15
  138. package/.bkit/core/prompts/bkit-debug-failure-diagnose.md +34 -14
  139. package/.bkit/core/prompts/bkit-debug-failure-remediate.md +34 -17
  140. package/.bkit/core/prompts/bkit-debug-failure-reproduce.md +26 -13
  141. package/.bkit/core/prompts/bkit-debug-failure-soak.md +31 -16
  142. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-1.md +21 -11
  143. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-2.md +20 -11
  144. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-3.md +20 -11
  145. package/.bkit/core/prompts/bkit-design-generate.md +32 -14
  146. package/.bkit/core/prompts/bkit-design-organise.md +25 -12
  147. package/.bkit/core/prompts/bkit-design-review-1.md +31 -15
  148. package/.bkit/core/prompts/bkit-design-review-2.md +28 -13
  149. package/.bkit/core/prompts/bkit-design-spike.md +30 -13
  150. package/.bkit/core/prompts/bkit-design-update.md +33 -19
  151. package/.bkit/core/prompts/bkit-plan-generate.md +72 -18
  152. package/.bkit/core/prompts/bkit-plan-review-1.md +50 -27
  153. package/.bkit/core/prompts/bkit-plan-review-2.md +59 -28
  154. package/.bkit/core/prompts/bkit-plan-scope-check.md +109 -0
  155. package/.bkit/core/prompts/bkit-plan-update.md +61 -24
  156. package/.bkit/core/prompts/bkit-ship-conflict-resolve.md +27 -15
  157. package/.bkit/core/prompts/bkit-ship-deferred-sweep.md +31 -16
  158. package/.bkit/core/prompts/bkit-ship-update-docs.md +29 -14
  159. package/.bkit/core/prompts/bkit-utility-compress-markdown.md +24 -14
  160. package/.bkit/core/prompts/bkit-utility-update-decisions.md +32 -14
  161. package/.bkit/core/prompts/bkit-utility-update-deferrals.md +29 -14
  162. package/.bkit/core/prompts/bkit-utility-update-documentation.md +27 -12
  163. package/.bkit/core/prompts/bkit-utility-update-failures.md +31 -14
  164. package/.bkit/core/prompts/bkit-utility-update-knowledge.md +33 -14
  165. package/.bkit/core/prompts/bkit-utility-update-roadmap.md +38 -17
  166. package/.bkit/core/prompts/bkit-utility-update-tracker.md +41 -18
  167. package/.bkit/core/workflows/bkit-auto.md +54 -46
  168. package/.bkit/core/workflows/bkit-build.md +577 -458
  169. package/.bkit/core/workflows/bkit-debug.md +348 -333
  170. package/.bkit/core/workflows/bkit-design.md +201 -237
  171. package/.bkit/core/workflows/bkit-plan.md +210 -321
  172. package/.bkit/core/workflows/bkit-ship.md +328 -278
  173. package/.bkit/shared/.bkit/.env.example +2 -2
  174. package/.bkit/shared/.claude/settings.json +11 -0
  175. package/.bkit/shared/.cursor/mcp.json +1 -1
  176. package/.bkit/shared/.cursor/rules/bkit-rules.mdc +1 -1
  177. package/.bkit/shared/.gitattributes +1 -1
  178. package/.bkit/shared/.mcp.json +1 -1
  179. package/.bkit/shared/.opencode/opencode.json +11 -0
  180. package/.bkit/shared/CLAUDE.md +2 -2
  181. package/.bkit/shared/GEMINI.md +2 -2
  182. package/.bkit/shared/docs/architecture.md +1 -1
  183. package/.bkit/shared/docs/data-schema.md +1 -1
  184. package/.bkit/shared/docs/deployments.md +11 -11
  185. package/.bkit/shared/docs/design.md +1 -1
  186. package/.bkit/shared/docs/noise.md +1 -1
  187. package/.bkit/shared/docs/observability.md +2 -2
  188. package/.bkit/shared/docs/security.md +1 -1
  189. package/.bkit/shared/docs/setup.md +1 -1
  190. package/.bkit/shared/docs/technology.md +1 -1
  191. package/.bkit/shared/docs/testing.md +16 -7
  192. package/.bkit/templates/.claude/hooks/bkit-cloud-bootstrap.sh +1 -1
  193. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-change.yml +26 -5
  194. package/.bkit/templates/.github/workflows/bkit-ci.yml +1 -1
  195. package/.bkit/templates/.github/workflows/bkit-cloud.yml +21 -19
  196. package/.bkit/templates/.husky/pre-push +4 -4
  197. package/.bkit/templates/.prettierignore +7 -0
  198. package/.bkit/templates/scripts/cleanup/bkit-disk-prune.mjs +1 -1
  199. package/CHANGELOG.md +112 -0
  200. package/README.md +50 -35
  201. package/dist/checks/after/ci-yml.js +39 -28
  202. package/dist/checks/after/github.js +91 -17
  203. package/dist/checks/after/index.js +11 -8
  204. package/dist/checks/after/suppression.js +8 -5
  205. package/dist/checks/after/tools.js +39 -82
  206. package/dist/checks/before/env.js +2 -2
  207. package/dist/checks/before/files.js +5 -3
  208. package/dist/checks/before/index.js +15 -2
  209. package/dist/checks/before/repo.js +163 -2
  210. package/dist/checks/tips/catalogue.js +14 -5
  211. package/dist/checks/tips/detect.js +32 -0
  212. package/dist/checks/tips/index.js +4 -4
  213. package/dist/cli.js +19 -47
  214. package/dist/commands/analysis.js +109 -100
  215. package/dist/commands/ci/ci.js +6 -6
  216. package/dist/commands/comply.js +54 -91
  217. package/dist/commands/install/doctor.js +2 -2
  218. package/dist/commands/install/install.js +50 -19
  219. package/dist/commands/install/uninstall.js +59 -28
  220. package/dist/commands/land.js +16 -12
  221. package/dist/commands/lib.js +51 -16
  222. package/dist/commands/registry.js +4 -0
  223. package/dist/commands/setup/setup.js +7 -7
  224. package/dist/commands/stats.js +136 -0
  225. package/dist/commands/tips.js +7 -3
  226. package/dist/commands/workflow/branches.js +656 -56
  227. package/dist/commands/workflow/challenge.js +556 -0
  228. package/dist/commands/workflow/context.js +709 -30
  229. package/dist/commands/workflow/counters.js +208 -75
  230. package/dist/commands/workflow/decisions.js +441 -0
  231. package/dist/commands/workflow/disk.js +2 -2
  232. package/dist/commands/workflow/fids.js +689 -33
  233. package/dist/commands/workflow/issues.js +343 -59
  234. package/dist/commands/workflow/ledgers.js +762 -27
  235. package/dist/commands/workflow/locks.js +24 -2
  236. package/dist/commands/workflow/plan-blocks.js +437 -0
  237. package/dist/commands/workflow/plan-versions.js +22 -0
  238. package/dist/commands/workflow/prs.js +142 -32
  239. package/dist/commands/workflow/repo.js +52 -8
  240. package/dist/commands/workflow/roadmap.js +9 -7
  241. package/dist/commands/workflow/run-facts.js +426 -0
  242. package/dist/commands/workflow/runs.js +144 -43
  243. package/dist/commands/workflow/runtime.js +12 -1
  244. package/dist/commands/workflow/status.js +130 -29
  245. package/dist/commands/workflow/step-table.js +276 -0
  246. package/dist/commands/workflow/steps.js +171 -80
  247. package/dist/commands/workflow/summary.js +275 -0
  248. package/dist/commands/workflow/tests.js +834 -136
  249. package/dist/commands/workflow/tracker.js +983 -0
  250. package/dist/commands/workflow/workflows.js +834 -195
  251. package/dist/commands/workflow/workstreams.js +11 -5
  252. package/dist/commands/workflow/worktrees.js +147 -9
  253. package/dist/install/manifest.js +11 -9
  254. package/dist/install/modules/cloud.js +22 -13
  255. package/dist/install/modules/core.js +247 -3
  256. package/dist/install/modules/opencode.js +2 -2
  257. package/dist/install/version-file.js +6 -2
  258. package/dist/ledgers/migrate.js +81 -0
  259. package/dist/ledgers/status.js +61 -8
  260. package/dist/ledgers/telemetry.js +905 -124
  261. package/dist/render/hosts.js +4 -0
  262. package/dist/render/models.js +12 -0
  263. package/dist/shared/branches.js +3 -1
  264. package/dist/shared/egress.js +7 -0
  265. package/dist/shared/json.js +20 -7
  266. package/dist/shared/settings.js +125 -17
  267. package/dist/shared/text.js +333 -0
  268. package/dist/tools/grammar-density.js +183 -0
  269. package/dist/tools/instructions-sources.js +721 -0
  270. package/dist/tools/instructions.js +1579 -0
  271. package/package.json +113 -24
  272. package/.bkit/core/engine/commands/harvest.ts +0 -459
  273. package/.bkit/core/engine/tools/wording.ts +0 -748
  274. package/.bkit/core/prompts/bkit-build-challenge-reconcile.md +0 -69
  275. package/.bkit/core/prompts/bkit-build-challenge-remediate.md +0 -71
  276. package/.bkit/core/prompts/bkit-build-challenge-tests.md +0 -74
  277. package/.bkit/core/prompts/bkit-build-summary.md +0 -94
  278. package/.bkit/core/prompts/bkit-debug-summary.md +0 -96
  279. package/.bkit/core/prompts/bkit-design-summary.md +0 -107
  280. package/.bkit/core/prompts/bkit-plan-complexity-check.md +0 -67
  281. package/.bkit/core/prompts/bkit-plan-complexity-split.md +0 -88
  282. package/.bkit/core/prompts/bkit-plan-review-3.md +0 -93
  283. package/.bkit/core/prompts/bkit-plan-scope-check-1.md +0 -85
  284. package/.bkit/core/prompts/bkit-plan-scope-check-2.md +0 -85
  285. package/.bkit/core/prompts/bkit-plan-summary.md +0 -107
  286. package/.bkit/core/prompts/bkit-ship-summary.md +0 -78
  287. package/dist/commands/harvest.js +0 -353
  288. package/dist/tools/wording.js +0 -680
@@ -0,0 +1,398 @@
1
+ // The summary a phase workflow writes at its Summary step (`bkit workflows summary`):
2
+ // verdict, reasons and the recommended next command, from the run's status rows,
3
+ // its log files, the plan and the ledger rows it added. Pure: the caller reads the
4
+ // disk and passes `read` for the log files.
5
+
6
+ import type { Workstream } from "./workstreams.js";
7
+ import { issueIdOf, type FidTracker } from "./fids.js";
8
+ import { parseDecisionItems } from "./decisions.js";
9
+ import { parsePlanBlocks } from "./plan-blocks.js";
10
+
11
+ export const SUMMARY_WORKFLOWS = ["plan", "design", "build", "debug", "ship"] as const;
12
+ export type SummaryWorkflow = (typeof SUMMARY_WORKFLOWS)[number];
13
+ export type SummaryVerdict = "PASS" | "CONCERNS" | "FAIL";
14
+
15
+ /** A next step: a command (followed by the ref), an alternative command, or a fixed line. */
16
+ export interface NextStep {
17
+ command?: string;
18
+ or?: string;
19
+ line?: string;
20
+ }
21
+
22
+ /** Per workflow and verdict, the next command the summary recommends. */
23
+ export const NEXT_STEP: Readonly<Record<SummaryWorkflow, Record<SummaryVerdict, NextStep>>> = {
24
+ plan: {
25
+ PASS: { command: "/bkit-build" },
26
+ CONCERNS: { command: "/bkit-build", or: "/bkit-plan" },
27
+ FAIL: { command: "/bkit-plan" },
28
+ },
29
+ design: {
30
+ PASS: { command: "/bkit-plan" },
31
+ CONCERNS: { command: "/bkit-plan", or: "/bkit-design" },
32
+ FAIL: { command: "/bkit-design" },
33
+ },
34
+ debug: {
35
+ PASS: { command: "/bkit-ship" },
36
+ CONCERNS: { command: "/bkit-ship", or: "/bkit-debug" },
37
+ FAIL: { line: "Manual handover — no bkit command; see the debug handover." },
38
+ },
39
+ build: {
40
+ PASS: { command: "/bkit-ship" },
41
+ CONCERNS: { command: "/bkit-ship" },
42
+ FAIL: { command: "/bkit-debug" },
43
+ },
44
+ ship: {
45
+ PASS: { line: "No further bkit command — feature shipped." },
46
+ CONCERNS: { line: "No further bkit command — feature shipped." },
47
+ FAIL: { command: "/bkit-ship" },
48
+ },
49
+ };
50
+
51
+ /**
52
+ * The issue reference a FID carries: `160-slug` → `#160`, `abc-123-slug` →
53
+ * `ABC-123`; undefined when the FID has no issue segment for `tracker`.
54
+ */
55
+ export function refOf(fid: string, tracker: FidTracker = "any"): string | undefined {
56
+ const id = issueIdOf(fid, tracker);
57
+ if (id === null) return undefined;
58
+ return /^[0-9]+$/.test(id) ? `#${id}` : id;
59
+ }
60
+
61
+ /**
62
+ * A critique step whose FAIL a later step of the same workflow answers; the run
63
+ * carried on past it. A build challenge is answered by its fix tasks running
64
+ * (`build-plan-execute`) or by a Decision Pause (`build-decision`).
65
+ */
66
+ const ANSWERED_BY: Readonly<Record<string, readonly string[]>> = {
67
+ "plan/plan-review": ["plan-apply-review"],
68
+ "design/design-review": ["design-apply-review"],
69
+ "design/design-spike": ["design-apply-review"],
70
+ "build/build-challenge": ["build-plan-execute", "build-decision"],
71
+ };
72
+
73
+ const RUN_TESTS = new Set(["build-run-tests", "debug-run-tests"]);
74
+ const SEVERITY = /\*\*Severity:\*\*\s*(CRITICAL|HIGH|MEDIUM|LOW)\b/;
75
+ const TEST_EXCEPTION = "**TEST EXCEPTION:**";
76
+
77
+ type Row = Record<string, unknown>;
78
+
79
+ export interface SummaryInput {
80
+ workflow: SummaryWorkflow;
81
+ fid: string;
82
+ /** `refOf(fid)`; undefined = no tracker reference. */
83
+ ref?: string;
84
+ /** This run's status rows, in `at` order. */
85
+ rows: Row[];
86
+ /** The plan files the run worked from, repo-relative path and text. */
87
+ plans: Array<{ path: string; text: string }>;
88
+ /** Deferral rows this workflow added since the run started. */
89
+ deferrals: Row[];
90
+ /** Distinct deferral keys this workflow added since the run started. */
91
+ deferralsAdded: number;
92
+ /** build and ship: every `build-decision-*.md` in the log folder, repo-relative. */
93
+ decisionFiles: string[];
94
+ /** A repo-relative file's text, or null when it is missing. */
95
+ read: (rel: string) => string | null;
96
+ /** design: the workstreams `workstreams parse` reads from the organised design. */
97
+ workstreams?: Workstream[];
98
+ /** design: the organised design's text; its `## Risks & Open Questions` becomes `## Open Questions`. */
99
+ organised?: string;
100
+ /** ship: the PR's size; absent when `gh` could not read it. */
101
+ pr?: { number: number; additions: number; deletions: number; changedFiles: number };
102
+ }
103
+
104
+ export interface Summary {
105
+ verdict: SummaryVerdict;
106
+ failures: number;
107
+ reasons: string[];
108
+ nextStep: string;
109
+ markdown: string;
110
+ }
111
+
112
+ interface StepGroup {
113
+ label: string;
114
+ key: string;
115
+ step: string;
116
+ rows: Array<{ row: Row; index: number }>;
117
+ }
118
+
119
+ const str = (v: unknown) => (typeof v === "string" ? v : undefined);
120
+ const artifactsOf = (r: Row) =>
121
+ Array.isArray(r.artifacts) ? r.artifacts.filter((a): a is string => typeof a === "string") : [];
122
+
123
+ /** The run's step rows grouped per step (and plan group), in first-seen order. */
124
+ function stepGroups(rows: Row[]): StepGroup[] {
125
+ const groups = new Map<string, StepGroup>();
126
+ rows.forEach((row, index) => {
127
+ const step = str(row.step);
128
+ if (row.type !== "step-done" || step === undefined) return;
129
+ const planGroup = row.group ?? row.subPlan;
130
+ const sub = typeof planGroup === "number" ? planGroup : undefined;
131
+ const key = sub === undefined ? step : `${step}#${sub}`;
132
+ const g = groups.get(key) ?? {
133
+ label: sub === undefined ? step : `${step} (group ${sub})`,
134
+ key,
135
+ step,
136
+ rows: [],
137
+ };
138
+ g.rows.push({ row, index });
139
+ groups.set(key, g);
140
+ });
141
+ return [...groups.values()];
142
+ }
143
+
144
+ const lastOf = (g: StepGroup) => g.rows.at(-1)!;
145
+
146
+ /** A level-2 section whose heading matches `title`, minus the template's non-content subsections. */
147
+ function planSection(text: string, title: RegExp): string | undefined {
148
+ const lines = text.split("\n");
149
+ const start = lines.findIndex((l) => /^## /.test(l) && title.test(l));
150
+ if (start < 0) return undefined;
151
+ const out: string[] = [];
152
+ let skip = false;
153
+ for (const line of lines.slice(start + 1)) {
154
+ if (/^## /.test(line)) break;
155
+ if (/^### /.test(line)) skip = /Derived Test Matrix|Human Confirmation/.test(line);
156
+ if (!skip) out.push(line);
157
+ }
158
+ return out.join("\n").trim();
159
+ }
160
+
161
+ export function buildSummary(input: SummaryInput): Summary {
162
+ const { workflow } = input;
163
+ const groups = stepGroups(input.rows);
164
+ const fails: string[] = [];
165
+ const concerns: string[] = [];
166
+ const read = (rel: string) => input.read(rel);
167
+
168
+ // Debug: a reproduce step that could not reproduce is a re-run, not a failure.
169
+ const reproduce = groups.find((g) => g.step === "debug-failure-reproduce");
170
+ const reproText = reproduce ? read(artifactsOf(lastOf(reproduce).row)[0] ?? "") : null;
171
+ const notReproduced = reproText !== null && /^Reproduced:\s*NO\b/m.test(reproText);
172
+ if (notReproduced) concerns.push("Reproduced: NO");
173
+
174
+ // Plan tasks: every unticked task of the plan files; a build always has a plan.
175
+ const tasks = input.plans.flatMap((p) =>
176
+ parsePlanBlocks(p.text).tasks.map((t) => ({ ...t, path: p.path })),
177
+ );
178
+ if (workflow === "build" && input.plans.length === 0) fails.push("No plan file");
179
+ if (workflow === "build")
180
+ for (const t of tasks.filter((x) => !x.done)) {
181
+ const sev = SEVERITY.exec(t.text)?.[1];
182
+ fails.push(
183
+ `Task ${t.task} not done (${t.path})${sev === undefined ? "" : ` — ${sev} challenge finding`}`,
184
+ );
185
+ }
186
+
187
+ for (const g of groups) {
188
+ const { row, index } = lastOf(g);
189
+ if (RUN_TESTS.has(g.step)) {
190
+ if (row.verdict === "FAIL") fails.push(`${g.label} FAIL`);
191
+ continue;
192
+ }
193
+ if (row.verdict === "CONCERNS") concerns.push(`${g.label} CONCERNS`);
194
+ if (row.verdict !== "FAIL") continue;
195
+ if (g.step === "debug-failure-reproduce" && notReproduced) continue;
196
+ const answers = ANSWERED_BY[`${workflow}/${g.step}`] ?? [];
197
+ const answeredBy = input.rows.find(
198
+ (r, i) => i > index && r.type === "step-done" && answers.includes(String(r.step)),
199
+ )?.step;
200
+ if (answeredBy !== undefined)
201
+ concerns.push(`${g.label} FAIL, answered by ${String(answeredBy)}`);
202
+ else fails.push(`${g.label} FAIL`);
203
+ }
204
+
205
+ // The Decision Pause: accepted items in this run's decision files.
206
+ const decided = groups
207
+ .filter((g) => g.step === "build-decision")
208
+ .flatMap((g) => g.rows.flatMap(({ row }) => artifactsOf(row)));
209
+ const acceptLines = decided.flatMap((rel) =>
210
+ (read(rel) ?? "")
211
+ .split("\n")
212
+ .filter((l) => /^Decision:\s*Accept\b/.test(l.trim()))
213
+ .map((l) => `${rel}: ${l.trim()}`),
214
+ );
215
+ // `Accept (assumed)`: taken without the owner (`ask: false` or headless); ship asks to confirm.
216
+ const assumed = acceptLines.filter((a) => /Decision:\s*Accept \(assumed\)/.test(a));
217
+ const accepted = acceptLines.filter((a) => !assumed.includes(a));
218
+ if (accepted.length > 0)
219
+ concerns.push(`${accepted.length} item(s) accepted at the Decision Pause`);
220
+ if (assumed.length > 0)
221
+ concerns.push(
222
+ `${assumed.length} item(s) accepted without the owner (assumed; ship asks to confirm)`,
223
+ );
224
+ // Ship: assumed Accepts the owner rejected or never confirmed; ship merged anyway.
225
+ const unconfirmed =
226
+ workflow === "ship"
227
+ ? input.decisionFiles.flatMap((rel) =>
228
+ parseDecisionItems(read(rel) ?? "")
229
+ .filter((i) => i.assumed)
230
+ .map((i) => `\`${rel}\` Q${i.n} — ${i.title}${i.rejected ? " (rejected)" : ""}`),
231
+ )
232
+ : [];
233
+ if (unconfirmed.length > 0)
234
+ concerns.push(`shipped with ${unconfirmed.length} assumed item(s) the owner did not confirm`);
235
+ if (input.deferralsAdded > 0)
236
+ concerns.push(`${input.deferralsAdded} deferral(s) written this run`);
237
+
238
+ const verdict: SummaryVerdict =
239
+ fails.length > 0 ? "FAIL" : concerns.length > 0 ? "CONCERNS" : "PASS";
240
+ const reasons = [...fails, ...concerns];
241
+
242
+ // Next step.
243
+ const step = NEXT_STEP[workflow][verdict];
244
+ const withRef = (cmd: string) => (input.ref === undefined ? cmd : `${cmd} ${input.ref}`);
245
+ let nextStep = step.command === undefined ? (step.line ?? "") : withRef(step.command);
246
+ if (workflow === "debug" && notReproduced) nextStep = `${withRef("/bkit-debug")} [repro]`;
247
+ const streams = input.workstreams ?? [];
248
+ if (workflow === "design" && step.command === "/bkit-plan" && streams.length > 1)
249
+ nextStep =
250
+ "File the Suggested Workstreams issues, then /bkit-plan <first root workstream's new issue>";
251
+
252
+ // Sections.
253
+ const out: string[] = [
254
+ `Verdict: ${verdict}`,
255
+ `Failures: ${fails.length}`,
256
+ "<!-- verdict:end -->",
257
+ "",
258
+ `# ${workflow[0]!.toUpperCase()}${workflow.slice(1)} Summary`,
259
+ "",
260
+ "## Verdict",
261
+ "",
262
+ verdict,
263
+ "",
264
+ ...(reasons.length > 0
265
+ ? reasons.map((r) => `- ${r}`)
266
+ : ["- No failing step, open task, accepted item or new deferral."]),
267
+ ];
268
+ if (workflow === "build")
269
+ out.push(
270
+ "",
271
+ input.plans.length === 0
272
+ ? "Plan tasks: no plan file."
273
+ : `Plan tasks: ${tasks.filter((t) => t.done).length} of ${tasks.length} ticked in ${input.plans.map((p) => `\`${p.path}\``).join(", ")}.`,
274
+ );
275
+
276
+ out.push(
277
+ "",
278
+ "## Steps",
279
+ "",
280
+ "| Step | Verdict | Failures | Attempts | Artifact |",
281
+ "| --- | --- | --- | --- | --- |",
282
+ );
283
+ for (const g of groups) {
284
+ const { row } = lastOf(g);
285
+ const artifact = artifactsOf(row)
286
+ .map((a) => `\`${a}\``)
287
+ .join(", ");
288
+ out.push(
289
+ `| ${g.label} | ${str(row.verdict) ?? "-"} | ${typeof row.failures === "number" ? row.failures : "-"} | ${g.rows.length} | ${artifact || "-"} |`,
290
+ );
291
+ }
292
+
293
+ if (workflow === "build") {
294
+ const appended = tasks.filter((t) => SEVERITY.test(t.text));
295
+ const low = input.deferrals.filter(
296
+ (r) => (r.by as Row | undefined)?.step === "build-challenge",
297
+ );
298
+ const gate = input.rows.filter((r) => r.type === "challenge-gate").at(-1);
299
+ const skipped = gate?.skipped === true;
300
+ out.push("", "## Challenge", "");
301
+ if (skipped)
302
+ out.push(
303
+ `- challenge-1 skipped: small change (${String(gate.linesChanged)} lines, ${String(gate.filesChanged)} files)`,
304
+ );
305
+ if (appended.length === 0 && low.length === 0 && !skipped) out.push("None");
306
+ for (const t of appended)
307
+ out.push(
308
+ `- Task ${t.task} — ${SEVERITY.exec(t.text)![1]} — ${t.done ? "ticked" : "not ticked"} (\`${t.path}\`)`,
309
+ );
310
+ for (const r of low)
311
+ out.push(`- LOW, deferred: ${String(r.key ?? "")} — ${String(r.description ?? "")}`);
312
+ }
313
+
314
+ if (workflow === "build" || workflow === "debug") {
315
+ const runs = groups.filter((g) => RUN_TESTS.has(g.step)).flatMap((g) => g.rows);
316
+ out.push("", "## Tests", "");
317
+ if (runs.length === 0) out.push("None recorded.");
318
+ for (const { row } of runs)
319
+ out.push(
320
+ `- Attempt ${String(row.attempt ?? "?")}: ${str(row.verdict) ?? "-"}, failures ${typeof row.failures === "number" ? row.failures : "-"}, ran \`${str(row.scope) ?? "unknown"}\``,
321
+ );
322
+ }
323
+
324
+ if (workflow === "plan") {
325
+ const final = input.plans.at(-1)?.text ?? "";
326
+ out.push(
327
+ "",
328
+ "## Acceptance Scenario",
329
+ "",
330
+ planSection(final, /Acceptance Scenario/) || "None",
331
+ "",
332
+ "## Open Questions",
333
+ "",
334
+ planSection(final, /Open Questions/) || "None",
335
+ );
336
+ }
337
+
338
+ if (workflow === "design") {
339
+ out.push("", "## Suggested Workstreams", "");
340
+ if (streams.length === 0) out.push("None");
341
+ for (const w of streams)
342
+ out.push(`### ${w.id}: ${w.name}`, "", `Requires: ${w.requiresText || "None"}`, "");
343
+ if (streams.length > 1)
344
+ out.push(
345
+ "File these as one issue each? Body per .bkit/trackers.md § Issue shapes; file in Requires order.",
346
+ );
347
+ out.push(
348
+ "",
349
+ "## Open Questions",
350
+ "",
351
+ planSection(input.organised ?? "", /Risks & Open Questions/) || "None",
352
+ );
353
+ }
354
+
355
+ if (workflow === "ship" && input.pr !== undefined) {
356
+ const pr = input.pr;
357
+ out.push(
358
+ "",
359
+ "## Pull Request",
360
+ "",
361
+ `PR #${pr.number}: +${pr.additions} / -${pr.deletions} lines across ${pr.changedFiles} files.`,
362
+ );
363
+ }
364
+ if (unconfirmed.length > 0)
365
+ out.push(
366
+ "",
367
+ "## Ship Completed with Assumed Items",
368
+ "",
369
+ "Accepted without the owner and not confirmed; each is a `needs-decision` deferral.",
370
+ "",
371
+ ...unconfirmed.map((u) => `- ${u}`),
372
+ );
373
+
374
+ // Risks and action items.
375
+ const items: string[] = [];
376
+ for (const g of groups) {
377
+ for (const rel of artifactsOf(lastOf(g).row).filter((a) => a.endsWith("-log.md"))) {
378
+ for (const line of (read(rel) ?? "").split("\n").filter((l) => /^BLOCKED\b/.test(l)))
379
+ items.push(`- ${line} (\`${rel}\`)`);
380
+ }
381
+ }
382
+ items.push(...accepted.map((a) => `- Accepted: ${a}`));
383
+ items.push(...assumed.map((a) => `- Assumed, not confirmed: ${a}`));
384
+ for (const rel of workflow === "build" ? input.decisionFiles : []) {
385
+ if (!/^Decision:/m.test(read(rel) ?? "")) items.push(`- \`${rel}\`: no decision recorded`);
386
+ }
387
+ for (const p of input.plans) {
388
+ for (const line of p.text.split("\n").filter((l) => l.includes(TEST_EXCEPTION)))
389
+ items.push(`- \`${p.path}\`: ${line.trim()}`);
390
+ }
391
+ out.push("", "## Risks and Action Items", "", ...(items.length > 0 ? items : ["None"]));
392
+
393
+ out.push("", "## Recommended Next Step", "", "```text", nextStep, "```");
394
+ if (step.or !== undefined && !notReproduced)
395
+ out.push("", `Or, to address the reasons first: \`${withRef(step.or)}\``);
396
+
397
+ return { verdict, failures: fails.length, reasons, nextStep, markdown: `${out.join("\n")}\n` };
398
+ }