@cbartens/bkit 5.5.0 → 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 (297) 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 +51 -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 +20 -20
  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 +106 -167
  13. package/.bkit/core/.bkit/providers.md +29 -32
  14. package/.bkit/core/.bkit/templates.md +44 -18
  15. package/.bkit/core/.bkit/trackers.md +60 -28
  16. package/.bkit/core/.bkit/workflows.md +399 -359
  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 -30
  55. package/.bkit/core/engine/checks/after/github.ts +101 -72
  56. package/.bkit/core/engine/checks/after/index.ts +10 -12
  57. package/.bkit/core/engine/checks/after/suppression.ts +8 -5
  58. package/.bkit/core/engine/checks/after/tools.ts +47 -92
  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 +10 -7
  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 +427 -105
  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 +59 -7
  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 +276 -22
  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 +325 -7
  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 +202 -33
  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 +30 -14
  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 +33 -13
  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 +75 -19
  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 +30 -15
  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 +28 -13
  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 +43 -20
  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 +16 -28
  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 +27 -10
  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 +22 -84
  195. package/.bkit/templates/.github/workflows/bkit-cloud.yml +21 -19
  196. package/.bkit/templates/.husky/pre-push +52 -13
  197. package/.bkit/templates/.prettierignore +7 -0
  198. package/.bkit/templates/scripts/ci/bkit-local-stack-lock.sh +194 -0
  199. package/.bkit/templates/scripts/ci/check-capstone.mjs +2 -4
  200. package/.bkit/templates/scripts/ci/check-capstone.test.mjs +4 -23
  201. package/.bkit/templates/scripts/ci/lanes/resolve-push-base.mjs +104 -23
  202. package/.bkit/templates/scripts/ci/lanes/resolve-push-base.test.mjs +103 -6
  203. package/.bkit/templates/scripts/cleanup/bkit-disk-prune.mjs +1 -1
  204. package/.bkit/templates/scripts/graphify/bkit-graph-pre-push.sh +3 -1
  205. package/.bkit/templates/scripts/prettier/bkit-insert-ignore.mjs +49 -29
  206. package/CHANGELOG.md +142 -0
  207. package/README.md +58 -34
  208. package/dist/checks/after/ci-yml.js +39 -29
  209. package/dist/checks/after/github.js +99 -72
  210. package/dist/checks/after/index.js +11 -13
  211. package/dist/checks/after/suppression.js +8 -5
  212. package/dist/checks/after/tools.js +40 -83
  213. package/dist/checks/before/env.js +2 -2
  214. package/dist/checks/before/files.js +5 -3
  215. package/dist/checks/before/index.js +15 -2
  216. package/dist/checks/before/repo.js +163 -2
  217. package/dist/checks/tips/catalogue.js +14 -5
  218. package/dist/checks/tips/detect.js +32 -0
  219. package/dist/checks/tips/index.js +7 -5
  220. package/dist/cli.js +19 -47
  221. package/dist/commands/analysis.js +109 -100
  222. package/dist/commands/ci/ci.js +6 -6
  223. package/dist/commands/comply.js +54 -91
  224. package/dist/commands/install/doctor.js +2 -2
  225. package/dist/commands/install/install.js +50 -19
  226. package/dist/commands/install/uninstall.js +59 -28
  227. package/dist/commands/land.js +16 -12
  228. package/dist/commands/lib.js +51 -16
  229. package/dist/commands/registry.js +4 -0
  230. package/dist/commands/setup/setup.js +7 -7
  231. package/dist/commands/stats.js +136 -0
  232. package/dist/commands/tips.js +7 -3
  233. package/dist/commands/workflow/branches.js +656 -56
  234. package/dist/commands/workflow/challenge.js +556 -0
  235. package/dist/commands/workflow/context.js +709 -30
  236. package/dist/commands/workflow/counters.js +208 -75
  237. package/dist/commands/workflow/decisions.js +441 -0
  238. package/dist/commands/workflow/disk.js +2 -2
  239. package/dist/commands/workflow/fids.js +689 -33
  240. package/dist/commands/workflow/issues.js +362 -99
  241. package/dist/commands/workflow/ledgers.js +762 -27
  242. package/dist/commands/workflow/locks.js +24 -2
  243. package/dist/commands/workflow/plan-blocks.js +437 -0
  244. package/dist/commands/workflow/plan-versions.js +22 -0
  245. package/dist/commands/workflow/prs.js +142 -32
  246. package/dist/commands/workflow/repo.js +52 -7
  247. package/dist/commands/workflow/roadmap.js +9 -7
  248. package/dist/commands/workflow/run-facts.js +426 -0
  249. package/dist/commands/workflow/runs.js +144 -43
  250. package/dist/commands/workflow/runtime.js +12 -1
  251. package/dist/commands/workflow/status.js +130 -29
  252. package/dist/commands/workflow/step-table.js +276 -0
  253. package/dist/commands/workflow/steps.js +171 -80
  254. package/dist/commands/workflow/summary.js +275 -0
  255. package/dist/commands/workflow/tests.js +834 -136
  256. package/dist/commands/workflow/tracker.js +983 -0
  257. package/dist/commands/workflow/workflows.js +834 -195
  258. package/dist/commands/workflow/workstreams.js +11 -5
  259. package/dist/commands/workflow/worktrees.js +221 -22
  260. package/dist/install/manifest.js +11 -9
  261. package/dist/install/modules/cloud.js +22 -13
  262. package/dist/install/modules/core.js +326 -7
  263. package/dist/install/modules/opencode.js +2 -2
  264. package/dist/install/version-file.js +6 -2
  265. package/dist/ledgers/migrate.js +81 -0
  266. package/dist/ledgers/status.js +61 -8
  267. package/dist/ledgers/telemetry.js +905 -124
  268. package/dist/render/hosts.js +4 -0
  269. package/dist/render/models.js +12 -0
  270. package/dist/shared/branches.js +3 -1
  271. package/dist/shared/egress.js +7 -0
  272. package/dist/shared/json.js +20 -7
  273. package/dist/shared/settings.js +147 -26
  274. package/dist/shared/text.js +333 -0
  275. package/dist/tools/grammar-density.js +183 -0
  276. package/dist/tools/instructions-sources.js +721 -0
  277. package/dist/tools/instructions.js +1579 -0
  278. package/package.json +114 -23
  279. package/.bkit/core/engine/commands/harvest.ts +0 -459
  280. package/.bkit/core/engine/tools/wording.ts +0 -748
  281. package/.bkit/core/prompts/bkit-build-challenge-reconcile.md +0 -69
  282. package/.bkit/core/prompts/bkit-build-challenge-remediate.md +0 -71
  283. package/.bkit/core/prompts/bkit-build-challenge-tests.md +0 -74
  284. package/.bkit/core/prompts/bkit-build-summary.md +0 -94
  285. package/.bkit/core/prompts/bkit-debug-summary.md +0 -96
  286. package/.bkit/core/prompts/bkit-design-summary.md +0 -107
  287. package/.bkit/core/prompts/bkit-plan-complexity-check.md +0 -67
  288. package/.bkit/core/prompts/bkit-plan-complexity-split.md +0 -88
  289. package/.bkit/core/prompts/bkit-plan-review-3.md +0 -93
  290. package/.bkit/core/prompts/bkit-plan-scope-check-1.md +0 -85
  291. package/.bkit/core/prompts/bkit-plan-scope-check-2.md +0 -85
  292. package/.bkit/core/prompts/bkit-plan-summary.md +0 -107
  293. package/.bkit/core/prompts/bkit-ship-summary.md +0 -78
  294. package/.bkit/templates/scripts/ci/lanes/check-tree-cache.mjs +0 -138
  295. package/.bkit/templates/scripts/ci/lanes/check-tree-cache.test.mjs +0 -181
  296. package/dist/commands/harvest.js +0 -353
  297. package/dist/tools/wording.js +0 -680
@@ -31,6 +31,7 @@ import {
31
31
  type FlagSpec,
32
32
  type RunContext,
33
33
  } from "../lib.js";
34
+ import { removeRecord } from "./ledgers.js";
34
35
  import { str } from "./status.js";
35
36
 
36
37
  /** A workflow lock whose holder shows no sign of life for this long is stale. */
@@ -177,7 +178,7 @@ function liveReason(
177
178
  * Fold a status log for the lock cross-check. A missing or malformed log shows no
178
179
  * run: there is no evidence the holder is alive, so the lock is reclaimed.
179
180
  */
180
- function readRunsLenient(file: string): WorkflowRun[] {
181
+ export function readRunsLenient(file: string): WorkflowRun[] {
181
182
  if (!fs.existsSync(file)) return [];
182
183
  const raw = fs.readFileSync(file, "utf-8").trim();
183
184
  const events: StatusEvent[] = [];
@@ -190,6 +191,26 @@ function readRunsLenient(file: string): WorkflowRun[] {
190
191
  return fold(events);
191
192
  }
192
193
 
194
+ /** Whether `fid` holds a workflow lock whose holder is still stepping (not reclaimable). */
195
+ export function workflowLockLive(root: string, fid: string): boolean {
196
+ return monthBuckets(root, fid).some((month) => {
197
+ const holder = readHolder(workflowLockPath(root, month, fid));
198
+ return holder !== undefined && "live" in liveReason(root, month, fid, holder, Date.now());
199
+ });
200
+ }
201
+
202
+ /**
203
+ * Whether `fid` has a workflow lock file younger than the idle limit, whatever
204
+ * its run's state: a run that has closed but not yet released its lock (it still
205
+ * pushes and waits for CI) still owns its checkout.
206
+ */
207
+ export function workflowLockFresh(root: string, fid: string): boolean {
208
+ return monthBuckets(root, fid).some((month) => {
209
+ const stat = fs.statSync(workflowLockPath(root, month, fid), { throwIfNoEntry: false });
210
+ return stat !== undefined && (Date.now() - stat.mtimeMs) / 1000 < WORKFLOW_LOCK_IDLE_S;
211
+ });
212
+ }
213
+
193
214
  export function acquireWorkflowLock(opts: {
194
215
  cwd: string;
195
216
  workflow: string;
@@ -310,7 +331,8 @@ export const locksCommand: CommandSpec = {
310
331
  },
311
332
  },
312
333
  release: {
313
- summary: "Remove a lock this caller owns; another owner's lock is left in place.",
334
+ summary:
335
+ "Remove a lock this caller owns (and the feature's shared-checkout record); another owner's lock is left in place.",
314
336
  flags: [
315
337
  KIND_FLAG,
316
338
  LOCK_WORKFLOW_FLAG,
@@ -348,6 +370,8 @@ export const locksCommand: CommandSpec = {
348
370
  who.fid,
349
371
  );
350
372
  const { removeFailed, ...result } = releaseLock(file, who);
373
+ // The run is over once its lock is released: so is its shared-checkout record.
374
+ if (!removeFailed && result.heldBy === null) removeRecord(root, who.fid);
351
375
  ctx.emit({ kind, ...result });
352
376
  if (removeFailed) {
353
377
  ctx.log(`lock removal failed: ${file}`);
@@ -0,0 +1,582 @@
1
+ // The one reader of a plan's (or a change-shape issue's) task section: groups and
2
+ // task blocks between lowercase HTML-comment markers, each task's uppercase fields.
3
+ //
4
+ // <!-- bkit:group N start -->
5
+ // ### GROUP N: {title}
6
+ // <!-- bkit:task M start -->
7
+ // - [ ] **TASK M:** {title}
8
+ // - **UPDATE:** `symbol` in `file`: what changes
9
+ // - **DONE WHEN:** ...
10
+ // - **TEST FIRST:**
11
+ // - `test file` `test name` fails on ...
12
+ // <!-- bkit:task M end -->
13
+ // <!-- bkit:group N end -->
14
+ //
15
+ // Pure text in, groups / tasks / line-numbered errors out; no file-system access, so
16
+ // `tests`, `challenge` and the run facts share it. Fenced code is skipped. Task-like
17
+ // text outside every task block is an error of kind `stray`, never silently unread.
18
+
19
+ import * as path from "node:path";
20
+
21
+ /** The task fields, written in uppercase as `**LABEL:**`. */
22
+ export const PLAN_FIELDS = [
23
+ "CREATE",
24
+ "READ",
25
+ "UPDATE",
26
+ "DELETE",
27
+ "DEPENDS ON",
28
+ "DONE WHEN",
29
+ "TEST FIRST",
30
+ "TEST EXCEPTION",
31
+ ] as const;
32
+ export type PlanField = (typeof PLAN_FIELDS)[number];
33
+
34
+ /** A CREATE / READ / UPDATE / DELETE line: `` `symbol` in `file`: what ``, or `` `file`: what ``. */
35
+ export interface PlanRef {
36
+ /** Absent on the file-only form. A Markdown section is `§ heading text`. */
37
+ symbol?: string;
38
+ file: string;
39
+ what: string;
40
+ /** 1-based plan line. */
41
+ line: number;
42
+ }
43
+
44
+ export interface PlanTest {
45
+ test: string;
46
+ name: string;
47
+ line: number;
48
+ }
49
+
50
+ export interface PlanTestFirst {
51
+ line: number;
52
+ /** The `n/a — {reason}` reason, when the task names no test. */
53
+ na?: string;
54
+ tests: PlanTest[];
55
+ /** 1-based lines of entries that are neither a test nor `n/a`. */
56
+ malformed: number[];
57
+ }
58
+
59
+ /** An owner-accepted test-order gap: the tests it covers and its 1-based plan lines. */
60
+ export interface PlanTestException {
61
+ tests: PlanTest[];
62
+ lines: number[];
63
+ /** The field's text, entries joined: the reason when the task names no test. */
64
+ text: string;
65
+ }
66
+
67
+ export interface PlanTaskBlock {
68
+ task: number;
69
+ group: number;
70
+ title: string;
71
+ /** The task's box is ticked `[x]`. */
72
+ done: boolean;
73
+ /** 1-based lines of its start and end markers. */
74
+ line: number;
75
+ end: number;
76
+ create: PlanRef[];
77
+ read: PlanRef[];
78
+ update: PlanRef[];
79
+ delete: PlanRef[];
80
+ dependsOn: number[];
81
+ doneWhen?: string;
82
+ testFirst?: PlanTestFirst;
83
+ testException?: PlanTestException;
84
+ /** The block's lines, markers included. */
85
+ text: string;
86
+ }
87
+
88
+ export interface PlanGroup {
89
+ group: number;
90
+ title: string;
91
+ line: number;
92
+ end: number;
93
+ tasks: PlanTaskBlock[];
94
+ }
95
+
96
+ export interface PlanBlockError {
97
+ line: number;
98
+ /** `format`: a malformed block; `stray`: task-like text outside every task block. */
99
+ kind: "format" | "stray";
100
+ message: string;
101
+ }
102
+
103
+ export interface PlanBlocks {
104
+ groups: PlanGroup[];
105
+ tasks: PlanTaskBlock[];
106
+ errors: PlanBlockError[];
107
+ /** 1-based line of a code fence that never closes: nothing after it is read. */
108
+ fenceOpen?: number;
109
+ }
110
+
111
+ /** One Reuse Inventory row: the line as written, its `File` cell's paths and its `Decision` cell. */
112
+ export interface InventoryRow {
113
+ line: string;
114
+ files: string[];
115
+ decision: string;
116
+ }
117
+
118
+ /** The Reuse Inventory table: its header and separator lines, and its rows. */
119
+ export interface ReuseInventory {
120
+ head: string[];
121
+ rows: InventoryRow[];
122
+ }
123
+
124
+ const tableCells = (row: string) =>
125
+ row
126
+ .split("|")
127
+ .slice(1, -1)
128
+ .map((c) => c.trim());
129
+
130
+ /**
131
+ * The plan's Reuse Inventory: the first table under the first heading naming Reuse
132
+ * Inventory, before the next heading of the same or a higher level. Each row's `File`
133
+ * cell gives its backticked paths, normalised. No heading, table or `File` column → null.
134
+ */
135
+ export function reuseInventory(text: string): ReuseInventory | null {
136
+ const lines = text.split(/\r?\n/);
137
+ const start = lines.findIndex((l) => /^#{2,4} .*Reuse Inventory/i.test(l));
138
+ if (start < 0) return null;
139
+ const level = /^#+/.exec(lines[start]!)![0].length;
140
+ const ends = (l: string) => new RegExp(`^#{1,${level}} `).test(l);
141
+ let head = -1;
142
+ for (let i = start + 1; i < lines.length && !ends(lines[i]!); i++)
143
+ if (/^\s*\|/.test(lines[i]!)) {
144
+ head = i;
145
+ break;
146
+ }
147
+ if (head < 0) return null;
148
+ const cols = tableCells(lines[head]!);
149
+ const file = cols.findIndex((c) => /^file\b/i.test(c));
150
+ const decision = cols.findIndex((c) => /^decision\b/i.test(c));
151
+ if (file < 0) return null;
152
+ const rows: InventoryRow[] = [];
153
+ for (let i = head + 2; i < lines.length && /^\s*\|/.test(lines[i]!); i++) {
154
+ const cells = tableCells(lines[i]!);
155
+ rows.push({
156
+ line: lines[i]!,
157
+ files: [...(cells[file] ?? "").matchAll(/`([^`]+)`/g)].map((m) =>
158
+ path.posix.normalize(m[1]!),
159
+ ),
160
+ decision: decision < 0 ? "" : (cells[decision] ?? ""),
161
+ });
162
+ }
163
+ return { head: lines.slice(head, head + 2), rows };
164
+ }
165
+
166
+ /** The error for a code fence opened at `line` and never closed. */
167
+ export const fenceNeverClosed = (line: number) =>
168
+ `the code fence at line ${line} is never closed; nothing after it is read`;
169
+
170
+ /** The fields a task writes at most once; CREATE / READ / UPDATE / DELETE repeat, one line per ref. */
171
+ const SINGLE_FIELDS: ReadonlySet<PlanField> = new Set([
172
+ "DEPENDS ON",
173
+ "DONE WHEN",
174
+ "TEST FIRST",
175
+ "TEST EXCEPTION",
176
+ ]);
177
+
178
+ const MARKER = /^\s*<!--\s*bkit:(group|task)\s+(\d+)\s+(start|end)\s*-->\s*$/;
179
+ const GROUP_HEAD = /^###\s+GROUP\s+(\d+):\s*(.*)$/;
180
+ const TASK_HEAD = /^\s*- \[([ xX])\] \*\*TASK (\d+):\*\*\s*(.*)$/;
181
+ const LABEL_LINE = /^(\s*)(?:- )?\*\*([A-Za-z][A-Za-z -]*?):\*\*\s*(.*)$/;
182
+ const SUB_BULLET = /^(\s*)- (.*)$/;
183
+ const FENCE = /^\s*(`{3,}|~{3,})/;
184
+ const FENCE_CLOSE = /^\s*(`{3,}|~{3,})\s*$/;
185
+ const STRAY_TASK = /\*\*\s*task\s+\d+/i;
186
+ /** A bold `**TASK N:**` inside a task block other than its own task line. */
187
+ const INNER_TASK = /(?<!\S)\*\*task\s+\d+\s*:?\s*\*\*/i;
188
+ const STRAY_FIELD = new RegExp(
189
+ String.raw`\*\*\s*(${[...PLAN_FIELDS, "TEST-ORDER EXCEPTION"].join("|")})\s*(?::\*\*|\*\*\s*:)`,
190
+ "i",
191
+ );
192
+
193
+ /** `text` with inline code spans removed: prose naming a label is not the label. */
194
+ const withoutCode = (text: string) => text.replace(/(`+)[\s\S]*?\1/g, "");
195
+
196
+ const spansOf = (text: string) => [...text.matchAll(/`([^`]+)`/g)].map((m) => m[1]!.trim());
197
+
198
+ /** A CREATE / READ / UPDATE / DELETE value, or null when it is none of the forms. */
199
+ export function parseRef(value: string, line: number): PlanRef | null {
200
+ const v = value.trim();
201
+ const sym = /^`([^`]+)`\s+in\s+`([^`]+)`\s*(?::\s*([\s\S]*))?$/.exec(v);
202
+ if (sym)
203
+ return { symbol: sym[1]!.trim(), file: sym[2]!.trim(), what: (sym[3] ?? "").trim(), line };
204
+ const section = /^(§[^`]*?)\s+in\s+`([^`]+)`\s*(?::\s*([\s\S]*))?$/.exec(v);
205
+ if (section)
206
+ return {
207
+ symbol: section[1]!.trim(),
208
+ file: section[2]!.trim(),
209
+ what: (section[3] ?? "").trim(),
210
+ line,
211
+ };
212
+ const file = /^`([^`]+)`\s*(?::\s*([\s\S]*))?$/.exec(v);
213
+ if (file) return { file: file[1]!.trim(), what: (file[2] ?? "").trim(), line };
214
+ return null;
215
+ }
216
+
217
+ interface FieldEntry {
218
+ label: PlanField;
219
+ line: number;
220
+ value: string;
221
+ indent: number;
222
+ items: Array<{ line: number; text: string }>;
223
+ last: number;
224
+ }
225
+
226
+ const testOf = (text: string, line: number): PlanTest | null => {
227
+ const spans = spansOf(text);
228
+ return spans.length >= 2
229
+ ? { test: path.posix.normalize(spans[0]!), name: spans[1]!, line }
230
+ : null;
231
+ };
232
+
233
+ /** One task's fields from its entries; format errors are pushed to `errors`. */
234
+ function taskFromFields(
235
+ head: { task: number; group: number; title: string; done: boolean; line: number; end: number },
236
+ fields: FieldEntry[],
237
+ text: string,
238
+ errors: PlanBlockError[],
239
+ ): PlanTaskBlock {
240
+ const t: PlanTaskBlock = {
241
+ ...head,
242
+ create: [],
243
+ read: [],
244
+ update: [],
245
+ delete: [],
246
+ dependsOn: [],
247
+ text,
248
+ };
249
+ const refs: Record<string, PlanRef[]> = {
250
+ CREATE: t.create,
251
+ READ: t.read,
252
+ UPDATE: t.update,
253
+ DELETE: t.delete,
254
+ };
255
+ for (const f of fields) {
256
+ const entries = [
257
+ ...(f.value.trim() === "" ? [] : [{ line: f.line, text: f.value }]),
258
+ ...f.items,
259
+ ];
260
+ if (f.label in refs) {
261
+ if (entries.length === 0)
262
+ errors.push({
263
+ line: f.line,
264
+ kind: "format",
265
+ message: `TASK ${t.task}: ${f.label} is empty`,
266
+ });
267
+ for (const e of entries) {
268
+ const ref = parseRef(e.text, e.line);
269
+ if (ref === null)
270
+ errors.push({
271
+ line: e.line,
272
+ kind: "format",
273
+ message: `TASK ${t.task}: ${f.label} is neither \`symbol\` in \`file\`: what nor \`file\`: what`,
274
+ });
275
+ else refs[f.label]!.push(ref);
276
+ }
277
+ } else if (f.label === "DEPENDS ON") {
278
+ // `TASK N` names when the entry has them, else bare numbers as the template writes them;
279
+ // an entry starting `none` names no task, whatever follows it. `GROUP N` numbers name
280
+ // groups, never tasks.
281
+ for (const e of entries) {
282
+ if (/^none\b/i.test(e.text.trim())) continue;
283
+ const text = e.text.replace(/\bGROUPS?\s+\d+(?:\s*(?:,|&|-|–|and)\s*\d+)*/gi, "");
284
+ const named = [...text.matchAll(/TASK\s+(\d+)/gi)];
285
+ const nums = named.length > 0 ? named : [...text.matchAll(/\b(\d+)\b/g)];
286
+ for (const m of nums) t.dependsOn.push(Number(m[1]));
287
+ }
288
+ } else if (f.label === "DONE WHEN") {
289
+ t.doneWhen = entries.map((e) => e.text.trim()).join(" ");
290
+ } else if (f.label === "TEST FIRST") {
291
+ const tf: PlanTestFirst = { line: f.line, tests: [], malformed: [] };
292
+ for (const e of entries) {
293
+ if (/^n\/a\b/i.test(e.text.trim())) {
294
+ tf.na = e.text.trim().replace(/^n\/a\s*[—–-]?\s*/i, "");
295
+ continue;
296
+ }
297
+ const test = testOf(e.text, e.line);
298
+ if (test === null) tf.malformed.push(e.line);
299
+ else tf.tests.push(test);
300
+ }
301
+ t.testFirst = tf;
302
+ } else {
303
+ const tests = entries.map((e) => testOf(e.text, e.line)).filter((x) => x !== null);
304
+ const lines = Array.from({ length: f.last - f.line + 1 }, (_, k) => f.line + k);
305
+ t.testException = { tests, lines, text: entries.map((e) => e.text.trim()).join(" ") };
306
+ }
307
+ }
308
+ return t;
309
+ }
310
+
311
+ /** Groups, tasks and errors of a plan's (or issue's) task section. */
312
+ export function parsePlanBlocks(text: string): PlanBlocks {
313
+ const lines = text.split(/\r?\n/);
314
+ const errors: PlanBlockError[] = [];
315
+ const groups: PlanGroup[] = [];
316
+ const tasks: PlanTaskBlock[] = [];
317
+ const groupSeen = new Map<number, number>();
318
+ const taskSeen = new Map<number, number>();
319
+ const err = (line: number, message: string) => errors.push({ line, kind: "format", message });
320
+ /**
321
+ * Group numbers ascend through the file: the build runs groups in file order. Task numbers
322
+ * ascend within a group, so a Decision Pause task numbered above the plan's highest can join
323
+ * the paused group.
324
+ */
325
+ let lastGroup = 0;
326
+ let lastTask = 0;
327
+ const numbered = (kind: "GROUP" | "TASK", no: number, last: number, n: number) => {
328
+ if (no === 0) err(n, `${kind} 0: ${kind.toLowerCase()} numbers start at 1`);
329
+ else if (no <= last)
330
+ err(
331
+ n,
332
+ `${kind} ${no} comes after ${kind} ${last}; number ${kind.toLowerCase()}s in ascending order`,
333
+ );
334
+ };
335
+
336
+ let fence: string | null = null;
337
+ let fenceLine = 0;
338
+ let group: PlanGroup | null = null;
339
+ let task: {
340
+ task: number;
341
+ line: number;
342
+ head: RegExpExecArray | null;
343
+ fields: FieldEntry[];
344
+ /** The field later lines continue; null after a line that is no field. */
345
+ current: FieldEntry | null;
346
+ counted: boolean;
347
+ } | null = null;
348
+ /** The marker whose next non-blank line must be its heading. */
349
+ let expect: { kind: "group" | "task"; n: number; line: number } | null = null;
350
+
351
+ const closeTask = (end: number) => {
352
+ const open = task!;
353
+ task = null;
354
+ if (!open.counted) return;
355
+ const block = taskFromFields(
356
+ {
357
+ task: open.task,
358
+ group: group?.group ?? 0,
359
+ title: (open.head?.[3] ?? "").trim(),
360
+ done: open.head !== null && open.head[1] !== " ",
361
+ line: open.line,
362
+ end,
363
+ },
364
+ open.fields,
365
+ lines.slice(open.line - 1, end).join("\n"),
366
+ errors,
367
+ );
368
+ tasks.push(block);
369
+ group?.tasks.push(block);
370
+ };
371
+
372
+ for (const [i, raw] of lines.entries()) {
373
+ const n = i + 1;
374
+ const fenceMark = FENCE.exec(raw)?.[1];
375
+ // A closing fence carries no info string: only the fence characters and spaces.
376
+ const closes = fence !== null && fenceMark?.startsWith(fence) === true && FENCE_CLOSE.test(raw);
377
+ if (fenceMark !== undefined && (fence === null || closes)) {
378
+ fence = fence === null ? fenceMark : null;
379
+ fenceLine = n;
380
+ continue;
381
+ }
382
+ if (fence !== null) continue;
383
+
384
+ if (expect !== null && raw.trim() !== "") {
385
+ const want = expect;
386
+ expect = null;
387
+ if (want.kind === "group") {
388
+ const h = GROUP_HEAD.exec(raw);
389
+ if (h === null)
390
+ err(
391
+ n,
392
+ `GROUP ${want.n} (line ${want.line}) is not followed by \`### GROUP ${want.n}: {title}\``,
393
+ );
394
+ else if (Number(h[1]) !== want.n)
395
+ err(
396
+ n,
397
+ `heading says GROUP ${h[1]}, its marker at line ${want.line} says GROUP ${want.n}`,
398
+ );
399
+ else if (group !== null) group.title = h[2]!.trim();
400
+ if (h !== null) continue;
401
+ } else {
402
+ const h = TASK_HEAD.exec(raw);
403
+ if (h === null)
404
+ err(
405
+ n,
406
+ `TASK ${want.n} (line ${want.line}) is not followed by \`- [ ] **TASK ${want.n}:** {title}\``,
407
+ );
408
+ else if (Number(h[2]) !== want.n)
409
+ err(
410
+ n,
411
+ `task line says TASK ${h[2]}, its marker at line ${want.line} says TASK ${want.n}`,
412
+ );
413
+ else if (task !== null) task.head = h;
414
+ if (h !== null) continue;
415
+ }
416
+ }
417
+
418
+ const marker = MARKER.exec(raw);
419
+ if (marker) {
420
+ const kind = marker[1] === "group" ? "group" : "task";
421
+ const edge = marker[3];
422
+ const no = Number(marker[2]);
423
+ if (kind === "group" && edge === "start") {
424
+ if (task !== null) {
425
+ err(n, `GROUP ${no} starts inside TASK ${task.task} (line ${task.line})`);
426
+ closeTask(n - 1);
427
+ }
428
+ if (group !== null)
429
+ err(n, `GROUP ${no} starts inside GROUP ${group.group} (line ${group.line})`);
430
+ if (groupSeen.has(no)) err(n, `GROUP ${no} is already used at line ${groupSeen.get(no)}`);
431
+ else {
432
+ groupSeen.set(no, n);
433
+ numbered("GROUP", no, lastGroup, n);
434
+ lastGroup = no;
435
+ }
436
+ lastTask = 0;
437
+ group = { group: no, title: "", line: n, end: n, tasks: [] };
438
+ groups.push(group);
439
+ expect = { kind, n: no, line: n };
440
+ } else if (kind === "task" && edge === "start") {
441
+ if (task !== null) {
442
+ err(n, `TASK ${no} starts inside TASK ${task.task} (line ${task.line})`);
443
+ closeTask(n - 1);
444
+ }
445
+ if (group === null)
446
+ err(n, `TASK ${no} starts outside a group: wrap it in \`<!-- bkit:group N start -->\``);
447
+ const dup = taskSeen.has(no);
448
+ if (dup)
449
+ err(
450
+ n,
451
+ `TASK ${no} is already used at line ${taskSeen.get(no)}; task numbers are unique in the plan`,
452
+ );
453
+ else {
454
+ taskSeen.set(no, n);
455
+ numbered("TASK", no, lastTask, n);
456
+ lastTask = no;
457
+ }
458
+ task = {
459
+ task: no,
460
+ line: n,
461
+ head: null,
462
+ fields: [],
463
+ current: null,
464
+ counted: group !== null && !dup,
465
+ };
466
+ expect = { kind, n: no, line: n };
467
+ } else if (kind === "task") {
468
+ if (task === null) err(n, `\`<!-- bkit:task ${no} end -->\` has no open TASK ${no}`);
469
+ else {
470
+ if (task.task !== no)
471
+ err(
472
+ n,
473
+ `\`<!-- bkit:task ${no} end -->\` closes TASK ${task.task}, opened at line ${task.line}`,
474
+ );
475
+ closeTask(n);
476
+ }
477
+ } else {
478
+ if (task !== null) {
479
+ err(n, `GROUP ${no} ends inside TASK ${task.task} (line ${task.line})`);
480
+ closeTask(n - 1);
481
+ }
482
+ if (group === null) err(n, `\`<!-- bkit:group ${no} end -->\` has no open GROUP ${no}`);
483
+ else {
484
+ if (group.group !== no)
485
+ err(
486
+ n,
487
+ `\`<!-- bkit:group ${no} end -->\` closes GROUP ${group.group}, opened at line ${group.line}`,
488
+ );
489
+ group.end = n;
490
+ group = null;
491
+ }
492
+ }
493
+ continue;
494
+ }
495
+
496
+ if (task !== null) {
497
+ if (INNER_TASK.test(withoutCode(raw))) {
498
+ err(
499
+ n,
500
+ `TASK ${task.task}: a second task line inside its block; give that task its own \`<!-- bkit:task N start -->\` block`,
501
+ );
502
+ task.current = null;
503
+ continue;
504
+ }
505
+ const label = LABEL_LINE.exec(raw);
506
+ if (label) {
507
+ const name = label[2]!.trim();
508
+ const known = PLAN_FIELDS.find((f) => f === name);
509
+ const first = task.fields.find((f) => f.label === known);
510
+ if (known !== undefined && SINGLE_FIELDS.has(known) && first !== undefined) {
511
+ err(
512
+ n,
513
+ `TASK ${task.task}: **${known}:** is written twice (first at line ${first.line}); write each field once`,
514
+ );
515
+ task.current = null;
516
+ continue;
517
+ }
518
+ if (known !== undefined) {
519
+ task.current = {
520
+ label: known,
521
+ line: n,
522
+ value: label[3]!,
523
+ indent: label[1]!.length,
524
+ items: [],
525
+ last: n,
526
+ };
527
+ task.fields.push(task.current);
528
+ continue;
529
+ }
530
+ if (PLAN_FIELDS.some((f) => f === name.toUpperCase()))
531
+ err(n, `TASK ${task.task}: write the label in uppercase: **${name.toUpperCase()}:**`);
532
+ else if (/^[A-Z][A-Z -]*$/.test(name))
533
+ err(
534
+ n,
535
+ `TASK ${task.task}: unknown label **${name}:** (fields: ${PLAN_FIELDS.join(", ")})`,
536
+ );
537
+ task.current = null;
538
+ continue;
539
+ }
540
+ const field = task.current;
541
+ if (field === null || raw.trim() === "") continue;
542
+ const bullet = SUB_BULLET.exec(raw);
543
+ if (bullet && bullet[1]!.length <= field.indent) task.current = null;
544
+ else {
545
+ if (bullet) field.items.push({ line: n, text: bullet[2]! });
546
+ else if (field.items.length > 0) field.items.at(-1)!.text += ` ${raw.trim()}`;
547
+ else field.value += ` ${raw.trim()}`;
548
+ field.last = n;
549
+ }
550
+ continue;
551
+ }
552
+
553
+ if (raw.trim() === "") continue;
554
+ const prose = withoutCode(raw);
555
+ if (STRAY_TASK.test(prose) || STRAY_FIELD.test(prose))
556
+ errors.push({
557
+ line: n,
558
+ kind: "stray",
559
+ message: `task-like text outside every task block: ${raw.trim()}`,
560
+ });
561
+ }
562
+
563
+ if (fence !== null) err(fenceLine, fenceNeverClosed(fenceLine));
564
+ if (task !== null) {
565
+ const open = task as { task: number; line: number };
566
+ err(open.line, `TASK ${open.task} has no \`<!-- bkit:task ${open.task} end -->\``);
567
+ closeTask(lines.length);
568
+ }
569
+ if (group !== null) {
570
+ const open = group as PlanGroup;
571
+ err(open.line, `GROUP ${open.group} has no \`<!-- bkit:group ${open.group} end -->\``);
572
+ open.end = lines.length;
573
+ }
574
+ if (expect !== null) {
575
+ const want = expect as { kind: string; n: number; line: number };
576
+ err(want.line, `${want.kind.toUpperCase()} ${want.n} has no heading line`);
577
+ }
578
+ errors.sort((a, b) => a.line - b.line);
579
+ return fence === null
580
+ ? { groups, tasks, errors }
581
+ : { groups, tasks, errors, fenceOpen: fenceLine };
582
+ }
@@ -0,0 +1,21 @@
1
+ // Which numbered draft in a run's log folder is the newest: `bkit workflow promote`
2
+ // and the run's `plan` block (`run-facts.ts`) both pick the draft this way.
3
+
4
+ import * as fs from "node:fs";
5
+
6
+ /**
7
+ * The highest `{workflow}-v{N}-{name}.md` draft, or null. `-v{N}-{k}.md` and `-v{N}-split.md`
8
+ * are files an older bkit's plan split wrote; they are never a draft.
9
+ */
10
+ export function highestVersion(dir: string, workflow: string): { file: string; n: number } | null {
11
+ if (!fs.existsSync(dir)) return null;
12
+ const re = new RegExp(`^${workflow}-v([0-9]+)-(.+)\\.md$`);
13
+ let best: { file: string; n: number } | null = null;
14
+ for (const f of fs.readdirSync(dir).sort()) {
15
+ const m = re.exec(f);
16
+ if (!m || /^[0-9]+$/.test(m[2]!) || m[2] === "split") continue;
17
+ const n = Number(m[1]);
18
+ if (best === null || n > best.n) best = { file: f, n };
19
+ }
20
+ return best;
21
+ }