@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
@@ -13,6 +13,7 @@ import * as fs from "node:fs";
13
13
  import * as path from "node:path";
14
14
  import { fold, lastRun, statusTimestamp, } from "../../ledgers/status.js";
15
15
  import { CommandError, EXIT, parseJson, repoRoot, statusRelPath, utcMonth, } from "../lib.js";
16
+ import { removeRecord } from "./ledgers.js";
16
17
  import { str } from "./status.js";
17
18
  /** A workflow lock whose holder shows no sign of life for this long is stale. */
18
19
  export const WORKFLOW_LOCK_IDLE_S = 1800;
@@ -112,7 +113,7 @@ function liveReason(root, month, fid, holder, now) {
112
113
  * Fold a status log for the lock cross-check. A missing or malformed log shows no
113
114
  * run: there is no evidence the holder is alive, so the lock is reclaimed.
114
115
  */
115
- function readRunsLenient(file) {
116
+ export function readRunsLenient(file) {
116
117
  if (!fs.existsSync(file))
117
118
  return [];
118
119
  const raw = fs.readFileSync(file, "utf-8").trim();
@@ -127,6 +128,24 @@ function readRunsLenient(file) {
127
128
  }
128
129
  return fold(events);
129
130
  }
131
+ /** Whether `fid` holds a workflow lock whose holder is still stepping (not reclaimable). */
132
+ export function workflowLockLive(root, fid) {
133
+ return monthBuckets(root, fid).some((month) => {
134
+ const holder = readHolder(workflowLockPath(root, month, fid));
135
+ return holder !== undefined && "live" in liveReason(root, month, fid, holder, Date.now());
136
+ });
137
+ }
138
+ /**
139
+ * Whether `fid` has a workflow lock file younger than the idle limit, whatever
140
+ * its run's state: a run that has closed but not yet released its lock (it still
141
+ * pushes and waits for CI) still owns its checkout.
142
+ */
143
+ export function workflowLockFresh(root, fid) {
144
+ return monthBuckets(root, fid).some((month) => {
145
+ const stat = fs.statSync(workflowLockPath(root, month, fid), { throwIfNoEntry: false });
146
+ return stat !== undefined && (Date.now() - stat.mtimeMs) / 1000 < WORKFLOW_LOCK_IDLE_S;
147
+ });
148
+ }
130
149
  export function acquireWorkflowLock(opts) {
131
150
  const root = repoRoot(opts.cwd);
132
151
  const month = resolveMonth(root, opts.fid, opts.month);
@@ -219,7 +238,7 @@ export const locksCommand = {
219
238
  },
220
239
  },
221
240
  release: {
222
- summary: "Remove a lock this caller owns; another owner's lock is left in place.",
241
+ summary: "Remove a lock this caller owns (and the feature's shared-checkout record); another owner's lock is left in place.",
223
242
  flags: [
224
243
  KIND_FLAG,
225
244
  LOCK_WORKFLOW_FLAG,
@@ -253,6 +272,9 @@ export const locksCommand = {
253
272
  const root = repoRoot(ctx.cwd);
254
273
  const file = workflowLockPath(root, resolveMonth(root, who.fid, str(ctx.flags.month)), who.fid);
255
274
  const { removeFailed, ...result } = releaseLock(file, who);
275
+ // The run is over once its lock is released: so is its shared-checkout record.
276
+ if (!removeFailed && result.heldBy === null)
277
+ removeRecord(root, who.fid);
256
278
  ctx.emit({ kind, ...result });
257
279
  if (removeFailed) {
258
280
  ctx.log(`lock removal failed: ${file}`);
@@ -0,0 +1,437 @@
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
+ import * as path from "node:path";
19
+ /** The task fields, written in uppercase as `**LABEL:**`. */
20
+ export const PLAN_FIELDS = [
21
+ "CREATE",
22
+ "READ",
23
+ "UPDATE",
24
+ "DELETE",
25
+ "DEPENDS ON",
26
+ "DONE WHEN",
27
+ "TEST FIRST",
28
+ "TEST EXCEPTION",
29
+ ];
30
+ const tableCells = (row) => row
31
+ .split("|")
32
+ .slice(1, -1)
33
+ .map((c) => c.trim());
34
+ /**
35
+ * The plan's Reuse Inventory: the first table under the first heading naming Reuse
36
+ * Inventory, before the next heading of the same or a higher level. Each row's `File`
37
+ * cell gives its backticked paths, normalised. No heading, table or `File` column → null.
38
+ */
39
+ export function reuseInventory(text) {
40
+ const lines = text.split(/\r?\n/);
41
+ const start = lines.findIndex((l) => /^#{2,4} .*Reuse Inventory/i.test(l));
42
+ if (start < 0)
43
+ return null;
44
+ const level = /^#+/.exec(lines[start])[0].length;
45
+ const ends = (l) => new RegExp(`^#{1,${level}} `).test(l);
46
+ let head = -1;
47
+ for (let i = start + 1; i < lines.length && !ends(lines[i]); i++)
48
+ if (/^\s*\|/.test(lines[i])) {
49
+ head = i;
50
+ break;
51
+ }
52
+ if (head < 0)
53
+ return null;
54
+ const cols = tableCells(lines[head]);
55
+ const file = cols.findIndex((c) => /^file\b/i.test(c));
56
+ const decision = cols.findIndex((c) => /^decision\b/i.test(c));
57
+ if (file < 0)
58
+ return null;
59
+ const rows = [];
60
+ for (let i = head + 2; i < lines.length && /^\s*\|/.test(lines[i]); i++) {
61
+ const cells = tableCells(lines[i]);
62
+ rows.push({
63
+ line: lines[i],
64
+ files: [...(cells[file] ?? "").matchAll(/`([^`]+)`/g)].map((m) => path.posix.normalize(m[1])),
65
+ decision: decision < 0 ? "" : (cells[decision] ?? ""),
66
+ });
67
+ }
68
+ return { head: lines.slice(head, head + 2), rows };
69
+ }
70
+ /** The error for a code fence opened at `line` and never closed. */
71
+ export const fenceNeverClosed = (line) => `the code fence at line ${line} is never closed; nothing after it is read`;
72
+ /** The fields a task writes at most once; CREATE / READ / UPDATE / DELETE repeat, one line per ref. */
73
+ const SINGLE_FIELDS = new Set([
74
+ "DEPENDS ON",
75
+ "DONE WHEN",
76
+ "TEST FIRST",
77
+ "TEST EXCEPTION",
78
+ ]);
79
+ const MARKER = /^\s*<!--\s*bkit:(group|task)\s+(\d+)\s+(start|end)\s*-->\s*$/;
80
+ const GROUP_HEAD = /^###\s+GROUP\s+(\d+):\s*(.*)$/;
81
+ const TASK_HEAD = /^\s*- \[([ xX])\] \*\*TASK (\d+):\*\*\s*(.*)$/;
82
+ const LABEL_LINE = /^(\s*)(?:- )?\*\*([A-Za-z][A-Za-z -]*?):\*\*\s*(.*)$/;
83
+ const SUB_BULLET = /^(\s*)- (.*)$/;
84
+ const FENCE = /^\s*(`{3,}|~{3,})/;
85
+ const FENCE_CLOSE = /^\s*(`{3,}|~{3,})\s*$/;
86
+ const STRAY_TASK = /\*\*\s*task\s+\d+/i;
87
+ /** A bold `**TASK N:**` inside a task block other than its own task line. */
88
+ const INNER_TASK = /(?<!\S)\*\*task\s+\d+\s*:?\s*\*\*/i;
89
+ const STRAY_FIELD = new RegExp(String.raw `\*\*\s*(${[...PLAN_FIELDS, "TEST-ORDER EXCEPTION"].join("|")})\s*(?::\*\*|\*\*\s*:)`, "i");
90
+ /** `text` with inline code spans removed: prose naming a label is not the label. */
91
+ const withoutCode = (text) => text.replace(/(`+)[\s\S]*?\1/g, "");
92
+ const spansOf = (text) => [...text.matchAll(/`([^`]+)`/g)].map((m) => m[1].trim());
93
+ /** A CREATE / READ / UPDATE / DELETE value, or null when it is none of the forms. */
94
+ export function parseRef(value, line) {
95
+ const v = value.trim();
96
+ const sym = /^`([^`]+)`\s+in\s+`([^`]+)`\s*(?::\s*([\s\S]*))?$/.exec(v);
97
+ if (sym)
98
+ return { symbol: sym[1].trim(), file: sym[2].trim(), what: (sym[3] ?? "").trim(), line };
99
+ const section = /^(§[^`]*?)\s+in\s+`([^`]+)`\s*(?::\s*([\s\S]*))?$/.exec(v);
100
+ if (section)
101
+ return {
102
+ symbol: section[1].trim(),
103
+ file: section[2].trim(),
104
+ what: (section[3] ?? "").trim(),
105
+ line,
106
+ };
107
+ const file = /^`([^`]+)`\s*(?::\s*([\s\S]*))?$/.exec(v);
108
+ if (file)
109
+ return { file: file[1].trim(), what: (file[2] ?? "").trim(), line };
110
+ return null;
111
+ }
112
+ const testOf = (text, line) => {
113
+ const spans = spansOf(text);
114
+ return spans.length >= 2
115
+ ? { test: path.posix.normalize(spans[0]), name: spans[1], line }
116
+ : null;
117
+ };
118
+ /** One task's fields from its entries; format errors are pushed to `errors`. */
119
+ function taskFromFields(head, fields, text, errors) {
120
+ const t = {
121
+ ...head,
122
+ create: [],
123
+ read: [],
124
+ update: [],
125
+ delete: [],
126
+ dependsOn: [],
127
+ text,
128
+ };
129
+ const refs = {
130
+ CREATE: t.create,
131
+ READ: t.read,
132
+ UPDATE: t.update,
133
+ DELETE: t.delete,
134
+ };
135
+ for (const f of fields) {
136
+ const entries = [
137
+ ...(f.value.trim() === "" ? [] : [{ line: f.line, text: f.value }]),
138
+ ...f.items,
139
+ ];
140
+ if (f.label in refs) {
141
+ if (entries.length === 0)
142
+ errors.push({
143
+ line: f.line,
144
+ kind: "format",
145
+ message: `TASK ${t.task}: ${f.label} is empty`,
146
+ });
147
+ for (const e of entries) {
148
+ const ref = parseRef(e.text, e.line);
149
+ if (ref === null)
150
+ errors.push({
151
+ line: e.line,
152
+ kind: "format",
153
+ message: `TASK ${t.task}: ${f.label} is neither \`symbol\` in \`file\`: what nor \`file\`: what`,
154
+ });
155
+ else
156
+ refs[f.label].push(ref);
157
+ }
158
+ }
159
+ else if (f.label === "DEPENDS ON") {
160
+ // `TASK N` names when the entry has them, else bare numbers as the template writes them;
161
+ // an entry starting `none` names no task, whatever follows it. `GROUP N` numbers name
162
+ // groups, never tasks.
163
+ for (const e of entries) {
164
+ if (/^none\b/i.test(e.text.trim()))
165
+ continue;
166
+ const text = e.text.replace(/\bGROUPS?\s+\d+(?:\s*(?:,|&|-|–|and)\s*\d+)*/gi, "");
167
+ const named = [...text.matchAll(/TASK\s+(\d+)/gi)];
168
+ const nums = named.length > 0 ? named : [...text.matchAll(/\b(\d+)\b/g)];
169
+ for (const m of nums)
170
+ t.dependsOn.push(Number(m[1]));
171
+ }
172
+ }
173
+ else if (f.label === "DONE WHEN") {
174
+ t.doneWhen = entries.map((e) => e.text.trim()).join(" ");
175
+ }
176
+ else if (f.label === "TEST FIRST") {
177
+ const tf = { line: f.line, tests: [], malformed: [] };
178
+ for (const e of entries) {
179
+ if (/^n\/a\b/i.test(e.text.trim())) {
180
+ tf.na = e.text.trim().replace(/^n\/a\s*[—–-]?\s*/i, "");
181
+ continue;
182
+ }
183
+ const test = testOf(e.text, e.line);
184
+ if (test === null)
185
+ tf.malformed.push(e.line);
186
+ else
187
+ tf.tests.push(test);
188
+ }
189
+ t.testFirst = tf;
190
+ }
191
+ else {
192
+ const tests = entries.map((e) => testOf(e.text, e.line)).filter((x) => x !== null);
193
+ const lines = Array.from({ length: f.last - f.line + 1 }, (_, k) => f.line + k);
194
+ t.testException = { tests, lines, text: entries.map((e) => e.text.trim()).join(" ") };
195
+ }
196
+ }
197
+ return t;
198
+ }
199
+ /** Groups, tasks and errors of a plan's (or issue's) task section. */
200
+ export function parsePlanBlocks(text) {
201
+ const lines = text.split(/\r?\n/);
202
+ const errors = [];
203
+ const groups = [];
204
+ const tasks = [];
205
+ const groupSeen = new Map();
206
+ const taskSeen = new Map();
207
+ const err = (line, message) => errors.push({ line, kind: "format", message });
208
+ /**
209
+ * Group numbers ascend through the file: the build runs groups in file order. Task numbers
210
+ * ascend within a group, so a Decision Pause task numbered above the plan's highest can join
211
+ * the paused group.
212
+ */
213
+ let lastGroup = 0;
214
+ let lastTask = 0;
215
+ const numbered = (kind, no, last, n) => {
216
+ if (no === 0)
217
+ err(n, `${kind} 0: ${kind.toLowerCase()} numbers start at 1`);
218
+ else if (no <= last)
219
+ err(n, `${kind} ${no} comes after ${kind} ${last}; number ${kind.toLowerCase()}s in ascending order`);
220
+ };
221
+ let fence = null;
222
+ let fenceLine = 0;
223
+ let group = null;
224
+ let task = null;
225
+ /** The marker whose next non-blank line must be its heading. */
226
+ let expect = null;
227
+ const closeTask = (end) => {
228
+ const open = task;
229
+ task = null;
230
+ if (!open.counted)
231
+ return;
232
+ const block = taskFromFields({
233
+ task: open.task,
234
+ group: group?.group ?? 0,
235
+ title: (open.head?.[3] ?? "").trim(),
236
+ done: open.head !== null && open.head[1] !== " ",
237
+ line: open.line,
238
+ end,
239
+ }, open.fields, lines.slice(open.line - 1, end).join("\n"), errors);
240
+ tasks.push(block);
241
+ group?.tasks.push(block);
242
+ };
243
+ for (const [i, raw] of lines.entries()) {
244
+ const n = i + 1;
245
+ const fenceMark = FENCE.exec(raw)?.[1];
246
+ // A closing fence carries no info string: only the fence characters and spaces.
247
+ const closes = fence !== null && fenceMark?.startsWith(fence) === true && FENCE_CLOSE.test(raw);
248
+ if (fenceMark !== undefined && (fence === null || closes)) {
249
+ fence = fence === null ? fenceMark : null;
250
+ fenceLine = n;
251
+ continue;
252
+ }
253
+ if (fence !== null)
254
+ continue;
255
+ if (expect !== null && raw.trim() !== "") {
256
+ const want = expect;
257
+ expect = null;
258
+ if (want.kind === "group") {
259
+ const h = GROUP_HEAD.exec(raw);
260
+ if (h === null)
261
+ err(n, `GROUP ${want.n} (line ${want.line}) is not followed by \`### GROUP ${want.n}: {title}\``);
262
+ else if (Number(h[1]) !== want.n)
263
+ err(n, `heading says GROUP ${h[1]}, its marker at line ${want.line} says GROUP ${want.n}`);
264
+ else if (group !== null)
265
+ group.title = h[2].trim();
266
+ if (h !== null)
267
+ continue;
268
+ }
269
+ else {
270
+ const h = TASK_HEAD.exec(raw);
271
+ if (h === null)
272
+ err(n, `TASK ${want.n} (line ${want.line}) is not followed by \`- [ ] **TASK ${want.n}:** {title}\``);
273
+ else if (Number(h[2]) !== want.n)
274
+ err(n, `task line says TASK ${h[2]}, its marker at line ${want.line} says TASK ${want.n}`);
275
+ else if (task !== null)
276
+ task.head = h;
277
+ if (h !== null)
278
+ continue;
279
+ }
280
+ }
281
+ const marker = MARKER.exec(raw);
282
+ if (marker) {
283
+ const kind = marker[1] === "group" ? "group" : "task";
284
+ const edge = marker[3];
285
+ const no = Number(marker[2]);
286
+ if (kind === "group" && edge === "start") {
287
+ if (task !== null) {
288
+ err(n, `GROUP ${no} starts inside TASK ${task.task} (line ${task.line})`);
289
+ closeTask(n - 1);
290
+ }
291
+ if (group !== null)
292
+ err(n, `GROUP ${no} starts inside GROUP ${group.group} (line ${group.line})`);
293
+ if (groupSeen.has(no))
294
+ err(n, `GROUP ${no} is already used at line ${groupSeen.get(no)}`);
295
+ else {
296
+ groupSeen.set(no, n);
297
+ numbered("GROUP", no, lastGroup, n);
298
+ lastGroup = no;
299
+ }
300
+ lastTask = 0;
301
+ group = { group: no, title: "", line: n, end: n, tasks: [] };
302
+ groups.push(group);
303
+ expect = { kind, n: no, line: n };
304
+ }
305
+ else if (kind === "task" && edge === "start") {
306
+ if (task !== null) {
307
+ err(n, `TASK ${no} starts inside TASK ${task.task} (line ${task.line})`);
308
+ closeTask(n - 1);
309
+ }
310
+ if (group === null)
311
+ err(n, `TASK ${no} starts outside a group: wrap it in \`<!-- bkit:group N start -->\``);
312
+ const dup = taskSeen.has(no);
313
+ if (dup)
314
+ err(n, `TASK ${no} is already used at line ${taskSeen.get(no)}; task numbers are unique in the plan`);
315
+ else {
316
+ taskSeen.set(no, n);
317
+ numbered("TASK", no, lastTask, n);
318
+ lastTask = no;
319
+ }
320
+ task = {
321
+ task: no,
322
+ line: n,
323
+ head: null,
324
+ fields: [],
325
+ current: null,
326
+ counted: group !== null && !dup,
327
+ };
328
+ expect = { kind, n: no, line: n };
329
+ }
330
+ else if (kind === "task") {
331
+ if (task === null)
332
+ err(n, `\`<!-- bkit:task ${no} end -->\` has no open TASK ${no}`);
333
+ else {
334
+ if (task.task !== no)
335
+ err(n, `\`<!-- bkit:task ${no} end -->\` closes TASK ${task.task}, opened at line ${task.line}`);
336
+ closeTask(n);
337
+ }
338
+ }
339
+ else {
340
+ if (task !== null) {
341
+ err(n, `GROUP ${no} ends inside TASK ${task.task} (line ${task.line})`);
342
+ closeTask(n - 1);
343
+ }
344
+ if (group === null)
345
+ err(n, `\`<!-- bkit:group ${no} end -->\` has no open GROUP ${no}`);
346
+ else {
347
+ if (group.group !== no)
348
+ err(n, `\`<!-- bkit:group ${no} end -->\` closes GROUP ${group.group}, opened at line ${group.line}`);
349
+ group.end = n;
350
+ group = null;
351
+ }
352
+ }
353
+ continue;
354
+ }
355
+ if (task !== null) {
356
+ if (INNER_TASK.test(withoutCode(raw))) {
357
+ err(n, `TASK ${task.task}: a second task line inside its block; give that task its own \`<!-- bkit:task N start -->\` block`);
358
+ task.current = null;
359
+ continue;
360
+ }
361
+ const label = LABEL_LINE.exec(raw);
362
+ if (label) {
363
+ const name = label[2].trim();
364
+ const known = PLAN_FIELDS.find((f) => f === name);
365
+ const first = task.fields.find((f) => f.label === known);
366
+ if (known !== undefined && SINGLE_FIELDS.has(known) && first !== undefined) {
367
+ err(n, `TASK ${task.task}: **${known}:** is written twice (first at line ${first.line}); write each field once`);
368
+ task.current = null;
369
+ continue;
370
+ }
371
+ if (known !== undefined) {
372
+ task.current = {
373
+ label: known,
374
+ line: n,
375
+ value: label[3],
376
+ indent: label[1].length,
377
+ items: [],
378
+ last: n,
379
+ };
380
+ task.fields.push(task.current);
381
+ continue;
382
+ }
383
+ if (PLAN_FIELDS.some((f) => f === name.toUpperCase()))
384
+ err(n, `TASK ${task.task}: write the label in uppercase: **${name.toUpperCase()}:**`);
385
+ else if (/^[A-Z][A-Z -]*$/.test(name))
386
+ err(n, `TASK ${task.task}: unknown label **${name}:** (fields: ${PLAN_FIELDS.join(", ")})`);
387
+ task.current = null;
388
+ continue;
389
+ }
390
+ const field = task.current;
391
+ if (field === null || raw.trim() === "")
392
+ continue;
393
+ const bullet = SUB_BULLET.exec(raw);
394
+ if (bullet && bullet[1].length <= field.indent)
395
+ task.current = null;
396
+ else {
397
+ if (bullet)
398
+ field.items.push({ line: n, text: bullet[2] });
399
+ else if (field.items.length > 0)
400
+ field.items.at(-1).text += ` ${raw.trim()}`;
401
+ else
402
+ field.value += ` ${raw.trim()}`;
403
+ field.last = n;
404
+ }
405
+ continue;
406
+ }
407
+ if (raw.trim() === "")
408
+ continue;
409
+ const prose = withoutCode(raw);
410
+ if (STRAY_TASK.test(prose) || STRAY_FIELD.test(prose))
411
+ errors.push({
412
+ line: n,
413
+ kind: "stray",
414
+ message: `task-like text outside every task block: ${raw.trim()}`,
415
+ });
416
+ }
417
+ if (fence !== null)
418
+ err(fenceLine, fenceNeverClosed(fenceLine));
419
+ if (task !== null) {
420
+ const open = task;
421
+ err(open.line, `TASK ${open.task} has no \`<!-- bkit:task ${open.task} end -->\``);
422
+ closeTask(lines.length);
423
+ }
424
+ if (group !== null) {
425
+ const open = group;
426
+ err(open.line, `GROUP ${open.group} has no \`<!-- bkit:group ${open.group} end -->\``);
427
+ open.end = lines.length;
428
+ }
429
+ if (expect !== null) {
430
+ const want = expect;
431
+ err(want.line, `${want.kind.toUpperCase()} ${want.n} has no heading line`);
432
+ }
433
+ errors.sort((a, b) => a.line - b.line);
434
+ return fence === null
435
+ ? { groups, tasks, errors }
436
+ : { groups, tasks, errors, fenceOpen: fenceLine };
437
+ }
@@ -0,0 +1,22 @@
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
+ import * as fs from "node:fs";
4
+ /**
5
+ * The highest `{workflow}-v{N}-{name}.md` draft, or null. `-v{N}-{k}.md` and `-v{N}-split.md`
6
+ * are files an older bkit's plan split wrote; they are never a draft.
7
+ */
8
+ export function highestVersion(dir, workflow) {
9
+ if (!fs.existsSync(dir))
10
+ return null;
11
+ const re = new RegExp(`^${workflow}-v([0-9]+)-(.+)\\.md$`);
12
+ let best = null;
13
+ for (const f of fs.readdirSync(dir).sort()) {
14
+ const m = re.exec(f);
15
+ if (!m || /^[0-9]+$/.test(m[2]) || m[2] === "split")
16
+ continue;
17
+ const n = Number(m[1]);
18
+ if (best === null || n > best.n)
19
+ best = { file: f, n };
20
+ }
21
+ return best;
22
+ }