@cbartens/bkit 6.0.0 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (375) hide show
  1. package/.bkit/README.md +154 -151
  2. package/.bkit/core/.agents/context.md +95 -29
  3. package/.bkit/core/.agents/docs.md +17 -15
  4. package/.bkit/core/.agents/plugins/bkit/hooks/bkit-gateguard.cjs +1 -0
  5. package/.bkit/core/.agents/plugins/bkit/hooks/bkit-guardrails.cjs +6 -5
  6. package/.bkit/core/.agents/plugins/bkit/hooks/bkit-readguard.cjs +1 -0
  7. package/.bkit/core/.agents/plugins/bkit/hooks/bkit-rules.cjs +65 -0
  8. package/.bkit/core/.agents/rules.md +108 -107
  9. package/.bkit/core/.agents/skills.md +63 -15
  10. package/.bkit/core/.bkit/README.md +154 -151
  11. package/.bkit/core/.bkit/authoring.md +714 -84
  12. package/.bkit/core/.bkit/cli.md +407 -0
  13. package/.bkit/core/.bkit/cloud.md +281 -193
  14. package/.bkit/core/.bkit/comparison.md +43 -41
  15. package/.bkit/core/.bkit/install.md +154 -84
  16. package/.bkit/core/.bkit/jsonl.md +193 -130
  17. package/.bkit/core/.bkit/pipeline.md +114 -114
  18. package/.bkit/core/.bkit/prompts.md +272 -165
  19. package/.bkit/core/.bkit/providers.md +457 -271
  20. package/.bkit/core/.bkit/router.md +69 -0
  21. package/.bkit/core/.bkit/templates.md +529 -256
  22. package/.bkit/core/.bkit/trackers.md +80 -52
  23. package/.bkit/core/.bkit/workflows.md +589 -504
  24. package/.bkit/core/.claude/hooks/bkit-gateguard.cjs +2 -2
  25. package/.bkit/core/.claude/hooks/bkit-guardrails.cjs +3 -3
  26. package/.bkit/core/.claude/hooks/bkit-preflight.sh +45 -14
  27. package/.bkit/core/.codex/hooks/bkit-guardrails.cjs +3 -3
  28. package/.bkit/core/.codex/hooks/bkit-rules.cjs +57 -0
  29. package/.bkit/core/.cursor/hooks/bkit-guardrails.cjs +7 -7
  30. package/.bkit/core/.cursor/hooks/bkit-preflight.sh +47 -12
  31. package/.bkit/core/.opencode/plugin/bkit-rules.js +89 -0
  32. package/.bkit/core/AGENTS.md +5 -73
  33. package/.bkit/core/docs/templates/bkit-audit-accessibility.md +127 -105
  34. package/.bkit/core/docs/templates/bkit-audit-adoption.md +104 -70
  35. package/.bkit/core/docs/templates/bkit-audit-agents.md +94 -72
  36. package/.bkit/core/docs/templates/bkit-audit-aio-seo.md +99 -78
  37. package/.bkit/core/docs/templates/bkit-audit-analytics.md +140 -82
  38. package/.bkit/core/docs/templates/bkit-audit-annotations.md +98 -72
  39. package/.bkit/core/docs/templates/bkit-audit-app-performance.md +121 -96
  40. package/.bkit/core/docs/templates/bkit-audit-app-store.md +157 -76
  41. package/.bkit/core/docs/templates/bkit-audit-architecture.md +113 -71
  42. package/.bkit/core/docs/templates/bkit-audit-ci-performance.md +75 -72
  43. package/.bkit/core/docs/templates/bkit-audit-config.md +122 -76
  44. package/.bkit/core/docs/templates/bkit-audit-cost.md +85 -74
  45. package/.bkit/core/docs/templates/bkit-audit-coverage.md +75 -68
  46. package/.bkit/core/docs/templates/bkit-audit-database.md +101 -70
  47. package/.bkit/core/docs/templates/bkit-audit-dead-code.md +164 -83
  48. package/.bkit/core/docs/templates/bkit-audit-deferrals.md +115 -86
  49. package/.bkit/core/docs/templates/bkit-audit-dependencies.md +264 -88
  50. package/.bkit/core/docs/templates/bkit-audit-documentation.md +118 -93
  51. package/.bkit/core/docs/templates/bkit-audit-duplication.md +139 -86
  52. package/.bkit/core/docs/templates/bkit-audit-failures.md +110 -81
  53. package/.bkit/core/docs/templates/bkit-audit-flakes.md +99 -74
  54. package/.bkit/core/docs/templates/bkit-audit-gates.md +105 -77
  55. package/.bkit/core/docs/templates/bkit-audit-issues.md +104 -80
  56. package/.bkit/core/docs/templates/bkit-audit-llm.md +131 -58
  57. package/.bkit/core/docs/templates/bkit-audit-logs.md +123 -100
  58. package/.bkit/core/docs/templates/bkit-audit-models.md +98 -53
  59. package/.bkit/core/docs/templates/bkit-audit-privacy.md +56 -52
  60. package/.bkit/core/docs/templates/bkit-audit-prompts.md +63 -54
  61. package/.bkit/core/docs/templates/bkit-audit-red-team.md +124 -68
  62. package/.bkit/core/docs/templates/bkit-audit-reliability.md +95 -70
  63. package/.bkit/core/docs/templates/bkit-audit-repo.md +169 -73
  64. package/.bkit/core/docs/templates/bkit-audit-security.md +206 -96
  65. package/.bkit/core/docs/templates/bkit-audit-slop.md +77 -70
  66. package/.bkit/core/docs/templates/bkit-audit-testing.md +162 -114
  67. package/.bkit/core/docs/templates/bkit-audit-usability.md +113 -71
  68. package/.bkit/core/docs/templates/bkit-plan.md +138 -132
  69. package/.bkit/core/engine/checks/after/auth.ts +40 -37
  70. package/.bkit/core/engine/checks/after/ci-yml.ts +65 -2
  71. package/.bkit/core/engine/checks/after/index.ts +12 -2
  72. package/.bkit/core/engine/checks/after/tools.ts +80 -33
  73. package/.bkit/core/engine/checks/before/context-fetch.ts +227 -33
  74. package/.bkit/core/engine/checks/before/env.ts +1 -1
  75. package/.bkit/core/engine/checks/before/index.ts +7 -0
  76. package/.bkit/core/engine/checks/before/repo.ts +40 -0
  77. package/.bkit/core/engine/checks/status-holes.ts +916 -123
  78. package/.bkit/core/engine/checks/tips/catalogue.ts +68 -17
  79. package/.bkit/core/engine/checks/tips/detect.ts +32 -0
  80. package/.bkit/core/engine/checks/tips/index.ts +47 -6
  81. package/.bkit/core/engine/cli.ts +238 -84
  82. package/.bkit/core/engine/commands/analysis.ts +128 -76
  83. package/.bkit/core/engine/commands/ci/ci.ts +363 -29
  84. package/.bkit/core/engine/commands/ci/pins.ts +172 -0
  85. package/.bkit/core/engine/commands/ci/push-gate.ts +223 -0
  86. package/.bkit/core/engine/commands/install/uninstall.ts +21 -27
  87. package/.bkit/core/engine/commands/install/upgrade.ts +32 -15
  88. package/.bkit/core/engine/commands/land.ts +126 -11
  89. package/.bkit/core/engine/commands/lib.ts +303 -6
  90. package/.bkit/core/engine/commands/registry.ts +6 -2
  91. package/.bkit/core/engine/commands/setup/setup.ts +1 -0
  92. package/.bkit/core/engine/commands/stats.ts +91 -35
  93. package/.bkit/core/engine/commands/workflow/branches.ts +1477 -117
  94. package/.bkit/core/engine/commands/workflow/challenge.ts +114 -39
  95. package/.bkit/core/engine/commands/workflow/completeness.ts +109 -0
  96. package/.bkit/core/engine/commands/workflow/context.ts +433 -55
  97. package/.bkit/core/engine/commands/workflow/counters.ts +244 -39
  98. package/.bkit/core/engine/commands/workflow/decisions.ts +61 -17
  99. package/.bkit/core/engine/commands/workflow/disk.ts +237 -55
  100. package/.bkit/core/engine/commands/workflow/fids.ts +19 -1
  101. package/.bkit/core/engine/commands/workflow/issues.ts +6 -0
  102. package/.bkit/core/engine/commands/workflow/ledgers.ts +458 -18
  103. package/.bkit/core/engine/commands/workflow/locks.ts +2 -0
  104. package/.bkit/core/engine/commands/workflow/plan-blocks.ts +124 -15
  105. package/.bkit/core/engine/commands/workflow/plans.ts +257 -0
  106. package/.bkit/core/engine/commands/workflow/prs.ts +12 -3
  107. package/.bkit/core/engine/commands/workflow/repo.ts +8 -0
  108. package/.bkit/core/engine/commands/workflow/roadmap.ts +56 -15
  109. package/.bkit/core/engine/commands/workflow/run-facts.ts +22 -31
  110. package/.bkit/core/engine/commands/workflow/runs.ts +652 -34
  111. package/.bkit/core/engine/commands/workflow/runtime.ts +3 -0
  112. package/.bkit/core/engine/commands/workflow/skills.ts +1 -0
  113. package/.bkit/core/engine/commands/workflow/status.ts +200 -17
  114. package/.bkit/core/engine/commands/workflow/step-table.ts +75 -33
  115. package/.bkit/core/engine/commands/workflow/steps.ts +635 -24
  116. package/.bkit/core/engine/commands/workflow/summary-findings.ts +177 -0
  117. package/.bkit/core/engine/commands/workflow/summary.ts +192 -46
  118. package/.bkit/core/engine/commands/workflow/tasks.ts +409 -0
  119. package/.bkit/core/engine/commands/workflow/test-blocks.ts +254 -0
  120. package/.bkit/core/engine/commands/workflow/tests-changed.ts +201 -0
  121. package/.bkit/core/engine/commands/workflow/tests.ts +1285 -356
  122. package/.bkit/core/engine/commands/workflow/workflows.ts +228 -43
  123. package/.bkit/core/engine/commands/workflow/worktrees.ts +389 -47
  124. package/.bkit/core/engine/install/cloud-env.ts +251 -121
  125. package/.bkit/core/engine/install/context-ci-workflow.ts +102 -0
  126. package/.bkit/core/engine/install/manifest.ts +60 -12
  127. package/.bkit/core/engine/install/modules/antigravity.ts +3 -2
  128. package/.bkit/core/engine/install/modules/cloud.ts +3 -3
  129. package/.bkit/core/engine/install/modules/codex.ts +15 -2
  130. package/.bkit/core/engine/install/modules/core.ts +956 -52
  131. package/.bkit/core/engine/install/modules/cursor.ts +6 -6
  132. package/.bkit/core/engine/install/modules/opencode.ts +16 -4
  133. package/.bkit/core/engine/install/template-notes.ts +130 -0
  134. package/.bkit/core/engine/install/version-file.ts +3 -2
  135. package/.bkit/core/engine/install/write.ts +5 -0
  136. package/.bkit/core/engine/ledgers/compact.ts +124 -0
  137. package/.bkit/core/engine/ledgers/journal.ts +291 -0
  138. package/.bkit/core/engine/ledgers/status.ts +160 -9
  139. package/.bkit/core/engine/ledgers/telemetry.ts +158 -21
  140. package/.bkit/core/engine/render/hosts.ts +114 -14
  141. package/.bkit/core/engine/shared/bkit-latest.ts +26 -4
  142. package/.bkit/core/engine/shared/dep-updater.ts +60 -0
  143. package/.bkit/core/engine/shared/egress.ts +1 -1
  144. package/.bkit/core/engine/shared/package-manager.ts +9 -0
  145. package/.bkit/core/engine/shared/settings.ts +92 -17
  146. package/.bkit/core/engine/shared/timeouts.ts +2 -0
  147. package/.bkit/core/engine/tools/cli-doc.ts +111 -0
  148. package/.bkit/core/engine/tools/grammar-density.ts +1 -1
  149. package/.bkit/core/engine/tools/instructions-sources.ts +15 -17
  150. package/.bkit/core/engine/tools/instructions.ts +94 -43
  151. package/.bkit/core/hooks/antigravity-hook.js +25 -10
  152. package/.bkit/core/hooks/gateguard-engine.js +53 -29
  153. package/.bkit/core/hooks/guardrails-shell.js +4 -4
  154. package/.bkit/core/hooks/guardrails-tiers.js +3 -3
  155. package/.bkit/core/hooks/preflight-linear.sh +9 -9
  156. package/.bkit/core/hooks/preflight-rules.sh +8 -0
  157. package/.bkit/core/hooks/readguard-engine.js +29 -10
  158. package/.bkit/core/hooks/session-rules-advisory.js +61 -0
  159. package/.bkit/core/prompts/bkit-audit-generate.md +187 -116
  160. package/.bkit/core/prompts/bkit-build-challenge-1.md +56 -37
  161. package/.bkit/core/prompts/bkit-build-challenge-2.md +65 -45
  162. package/.bkit/core/prompts/bkit-build-install-skills.md +38 -29
  163. package/.bkit/core/prompts/bkit-build-plan-convert.md +43 -32
  164. package/.bkit/core/prompts/bkit-build-plan-current.md +49 -39
  165. package/.bkit/core/prompts/bkit-build-plan-gaps-remediate.md +79 -55
  166. package/.bkit/core/prompts/bkit-build-plan-tasks.md +149 -0
  167. package/.bkit/core/prompts/bkit-build-plan-tests.md +120 -0
  168. package/.bkit/core/prompts/bkit-build-plan-verify.md +87 -50
  169. package/.bkit/core/prompts/bkit-debug-abort-handover.md +36 -35
  170. package/.bkit/core/prompts/bkit-debug-failure-diagnose.md +96 -64
  171. package/.bkit/core/prompts/bkit-debug-failure-remediate.md +54 -45
  172. package/.bkit/core/prompts/bkit-debug-failure-reproduce.md +42 -33
  173. package/.bkit/core/prompts/bkit-debug-failure-soak.md +53 -37
  174. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-1.md +35 -30
  175. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-2.md +34 -29
  176. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-3.md +32 -28
  177. package/.bkit/core/prompts/bkit-design-generate.md +68 -52
  178. package/.bkit/core/prompts/bkit-design-organise.md +73 -35
  179. package/.bkit/core/prompts/bkit-design-review-1.md +58 -54
  180. package/.bkit/core/prompts/bkit-design-review-2.md +54 -50
  181. package/.bkit/core/prompts/bkit-design-spike.md +54 -47
  182. package/.bkit/core/prompts/bkit-design-update.md +42 -31
  183. package/.bkit/core/prompts/bkit-plan-generate.md +76 -58
  184. package/.bkit/core/prompts/bkit-plan-review-1.md +63 -49
  185. package/.bkit/core/prompts/bkit-plan-review-2.md +60 -48
  186. package/.bkit/core/prompts/bkit-plan-scope-check.md +53 -46
  187. package/.bkit/core/prompts/bkit-plan-update.md +70 -52
  188. package/.bkit/core/prompts/bkit-ship-conflict-resolve.md +82 -50
  189. package/.bkit/core/prompts/bkit-ship-deferred-sweep.md +80 -45
  190. package/.bkit/core/prompts/bkit-ship-update-docs.md +35 -29
  191. package/.bkit/core/prompts/bkit-utility-compress-markdown.md +55 -49
  192. package/.bkit/core/prompts/bkit-utility-update-decisions.md +76 -49
  193. package/.bkit/core/prompts/bkit-utility-update-deferrals.md +70 -41
  194. package/.bkit/core/prompts/bkit-utility-update-documentation.md +33 -30
  195. package/.bkit/core/prompts/bkit-utility-update-failures.md +73 -43
  196. package/.bkit/core/prompts/bkit-utility-update-knowledge.md +77 -50
  197. package/.bkit/core/prompts/bkit-utility-update-roadmap.md +77 -45
  198. package/.bkit/core/prompts/bkit-utility-update-tracker.md +144 -68
  199. package/.bkit/core/workflows/bkit-auto.md +53 -30
  200. package/.bkit/core/workflows/bkit-build.md +635 -304
  201. package/.bkit/core/workflows/bkit-debug.md +301 -148
  202. package/.bkit/core/workflows/bkit-design.md +167 -95
  203. package/.bkit/core/workflows/bkit-plan.md +172 -84
  204. package/.bkit/core/workflows/bkit-ship.md +299 -152
  205. package/.bkit/shared/.agents/plugins/bkit/hooks.json +10 -0
  206. package/.bkit/shared/.claude/bkit-cloud.md +12 -10
  207. package/.bkit/shared/.claude/settings.json +3 -0
  208. package/.bkit/shared/.codex/bkit-cloud.md +98 -0
  209. package/.bkit/shared/.codex/hooks.json +20 -0
  210. package/.bkit/shared/.cursor/bkit-cloud.md +3 -3
  211. package/.bkit/shared/.opencode/opencode.json +1 -1
  212. package/.bkit/shared/CLAUDE.md +5 -1
  213. package/.bkit/shared/GEMINI.md +4 -1
  214. package/.bkit/shared/docs/architecture.md +4 -7
  215. package/.bkit/shared/docs/data-schema.md +4 -7
  216. package/.bkit/shared/docs/deployments.md +239 -166
  217. package/.bkit/shared/docs/design.md +11 -12
  218. package/.bkit/shared/docs/noise.md +8 -7
  219. package/.bkit/shared/docs/observability.md +5 -8
  220. package/.bkit/shared/docs/security.md +35 -17
  221. package/.bkit/shared/docs/setup.md +29 -18
  222. package/.bkit/shared/docs/technology.md +3 -6
  223. package/.bkit/shared/docs/testing.md +282 -176
  224. package/.bkit/shared/gitignore +6 -2
  225. package/.bkit/templates/.claude/hooks/bkit-cloud-bootstrap.sh +15 -16
  226. package/.bkit/templates/.cursor/Dockerfile +12 -8
  227. package/.bkit/templates/.cursor/environment.json +1 -1
  228. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-bug.yml +10 -8
  229. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-change.yml +24 -12
  230. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-idea.yml +3 -3
  231. package/.bkit/templates/.github/renovate.json5 +34 -6
  232. package/.bkit/templates/.github/workflows/bkit-ci.yml +53 -50
  233. package/.bkit/templates/.github/workflows/bkit-cloud.yml +13 -14
  234. package/.bkit/templates/.github/workflows/bkit-health.yml +97 -0
  235. package/.bkit/templates/.github/workflows/bkit-renovate.yml +5 -4
  236. package/.bkit/templates/.github/workflows/bkit-trivy.yml +5 -4
  237. package/.bkit/templates/.husky/pre-commit +2 -2
  238. package/.bkit/templates/.husky/pre-push +95 -35
  239. package/.bkit/templates/.prettierignore +6 -7
  240. package/.bkit/templates/.vale.ini +27 -27
  241. package/.bkit/templates/context/bkit-cloudrun.md +23 -15
  242. package/.bkit/templates/context/bkit-expo.md +70 -52
  243. package/.bkit/templates/context/bkit-github.md +93 -56
  244. package/.bkit/templates/context/bkit-inngest.md +21 -15
  245. package/.bkit/templates/context/bkit-maestro.md +22 -16
  246. package/.bkit/templates/context/bkit-playwright.md +23 -17
  247. package/.bkit/templates/context/bkit-resend.md +12 -10
  248. package/.bkit/templates/context/bkit-sentry.md +30 -20
  249. package/.bkit/templates/context/bkit-serena.md +8 -7
  250. package/.bkit/templates/context/bkit-stripe.md +14 -13
  251. package/.bkit/templates/context/bkit-supabase.md +16 -13
  252. package/.bkit/templates/context/bkit-tavily.md +14 -11
  253. package/.bkit/templates/context/bkit-vercel.md +27 -18
  254. package/.bkit/templates/docs/capabilities.md +15 -14
  255. package/.bkit/templates/scripts/ci/{check-accepted-divergences.mjs → bkit-check-accepted-divergences.mjs} +6 -3
  256. package/.bkit/templates/scripts/ci/{check-accepted-divergences.test.mjs → bkit-check-accepted-divergences.test.mjs} +3 -3
  257. package/.bkit/templates/scripts/ci/{check-capstone.mjs → bkit-check-capstone.mjs} +2 -2
  258. package/.bkit/templates/scripts/ci/{check-capstone.test.mjs → bkit-check-capstone.test.mjs} +4 -4
  259. package/.bkit/templates/scripts/ci/{check-dependency-audit.mjs → bkit-check-dependency-audit.mjs} +103 -18
  260. package/.bkit/templates/scripts/ci/{check-dependency-audit.test.mjs → bkit-check-dependency-audit.test.mjs} +31 -3
  261. package/.bkit/templates/scripts/ci/bkit-machine-lock.sh +387 -0
  262. package/.bkit/templates/scripts/ci/bkit-test-awake.mjs +75 -0
  263. package/.bkit/templates/scripts/ci/bkit-test-awake.test.mjs +160 -0
  264. package/.bkit/templates/scripts/ci/lanes/{resolve-push-base.mjs → bkit-resolve-push-base.mjs} +3 -3
  265. package/.bkit/templates/scripts/ci/lanes/{resolve-push-base.test.mjs → bkit-resolve-push-base.test.mjs} +4 -4
  266. package/.bkit/templates/scripts/cleanup/bkit-disk-prune.mjs +9 -1
  267. package/.bkit/templates/scripts/graphify/bkit-graph-drift.py +1 -1
  268. package/.bkit/templates/scripts/graphify/bkit-graph-eager.py +3 -3
  269. package/.bkit/templates/scripts/graphify/bkit-graph-embed.py +63 -0
  270. package/.bkit/templates/scripts/graphify/bkit-graph-imports.py +2 -2
  271. package/.bkit/templates/scripts/graphify/bkit-graph-pre-push.sh +69 -18
  272. package/.bkit/templates/scripts/graphify/bkit-graph-repair.py +3 -3
  273. package/.bkit/templates/scripts/graphify/bkit-graph-surface.py +1 -1
  274. package/.bkit/templates/scripts/graphify/bkit-graphify-graphml.py +1 -1
  275. package/.bkit/templates/scripts/graphify/bkit-graphify-rollup.py +197 -126
  276. package/.bkit/templates/scripts/graphify/bkit-graphify.sh +64 -41
  277. package/.bkit/templates/scripts/slop/bkit-slop-type-suppression.mjs +23 -7
  278. package/.bkit/templates/slop-scan.config.json +9 -2
  279. package/CHANGELOG.md +119 -0
  280. package/README.md +250 -76
  281. package/dist/checks/after/auth.js +36 -33
  282. package/dist/checks/after/ci-yml.js +52 -2
  283. package/dist/checks/after/index.js +12 -4
  284. package/dist/checks/after/tools.js +73 -33
  285. package/dist/checks/before/context-fetch.js +218 -33
  286. package/dist/checks/before/env.js +1 -1
  287. package/dist/checks/before/index.js +6 -2
  288. package/dist/checks/before/repo.js +35 -1
  289. package/dist/checks/status-holes.js +739 -114
  290. package/dist/checks/tips/catalogue.js +59 -17
  291. package/dist/checks/tips/detect.js +30 -0
  292. package/dist/checks/tips/index.js +48 -6
  293. package/dist/cli.js +222 -84
  294. package/dist/commands/analysis.js +77 -53
  295. package/dist/commands/ci/ci.js +302 -25
  296. package/dist/commands/ci/pins.js +142 -0
  297. package/dist/commands/ci/push-gate.js +194 -0
  298. package/dist/commands/install/uninstall.js +23 -27
  299. package/dist/commands/install/upgrade.js +21 -11
  300. package/dist/commands/land.js +126 -10
  301. package/dist/commands/lib.js +244 -5
  302. package/dist/commands/registry.js +6 -2
  303. package/dist/commands/setup/setup.js +1 -0
  304. package/dist/commands/stats.js +69 -30
  305. package/dist/commands/workflow/branches.js +1232 -111
  306. package/dist/commands/workflow/challenge.js +90 -37
  307. package/dist/commands/workflow/completeness.js +97 -0
  308. package/dist/commands/workflow/context.js +371 -51
  309. package/dist/commands/workflow/counters.js +206 -40
  310. package/dist/commands/workflow/decisions.js +60 -17
  311. package/dist/commands/workflow/disk.js +213 -55
  312. package/dist/commands/workflow/fids.js +18 -1
  313. package/dist/commands/workflow/issues.js +6 -0
  314. package/dist/commands/workflow/ledgers.js +372 -21
  315. package/dist/commands/workflow/locks.js +2 -0
  316. package/dist/commands/workflow/plan-blocks.js +78 -14
  317. package/dist/commands/workflow/plans.js +205 -0
  318. package/dist/commands/workflow/prs.js +12 -3
  319. package/dist/commands/workflow/repo.js +6 -0
  320. package/dist/commands/workflow/roadmap.js +50 -17
  321. package/dist/commands/workflow/run-facts.js +23 -28
  322. package/dist/commands/workflow/runs.js +530 -33
  323. package/dist/commands/workflow/runtime.js +3 -0
  324. package/dist/commands/workflow/skills.js +1 -0
  325. package/dist/commands/workflow/status.js +166 -19
  326. package/dist/commands/workflow/step-table.js +61 -29
  327. package/dist/commands/workflow/steps.js +542 -26
  328. package/dist/commands/workflow/summary-findings.js +117 -0
  329. package/dist/commands/workflow/summary.js +154 -41
  330. package/dist/commands/workflow/tasks.js +378 -0
  331. package/dist/commands/workflow/test-blocks.js +231 -0
  332. package/dist/commands/workflow/tests-changed.js +172 -0
  333. package/dist/commands/workflow/tests.js +1013 -321
  334. package/dist/commands/workflow/workflows.js +191 -38
  335. package/dist/commands/workflow/worktrees.js +310 -42
  336. package/dist/install/cloud-env.js +233 -99
  337. package/dist/install/context-ci-workflow.js +85 -0
  338. package/dist/install/manifest.js +57 -12
  339. package/dist/install/modules/antigravity.js +3 -2
  340. package/dist/install/modules/cloud.js +3 -3
  341. package/dist/install/modules/codex.js +12 -2
  342. package/dist/install/modules/core.js +851 -53
  343. package/dist/install/modules/cursor.js +6 -6
  344. package/dist/install/modules/opencode.js +11 -5
  345. package/dist/install/template-notes.js +116 -0
  346. package/dist/install/version-file.js +3 -2
  347. package/dist/ledgers/compact.js +87 -0
  348. package/dist/ledgers/journal.js +233 -0
  349. package/dist/ledgers/status.js +115 -5
  350. package/dist/ledgers/telemetry.js +121 -17
  351. package/dist/render/hosts.js +100 -14
  352. package/dist/shared/bkit-latest.js +26 -4
  353. package/dist/shared/dep-updater.js +49 -0
  354. package/dist/shared/egress.js +1 -1
  355. package/dist/shared/package-manager.js +6 -0
  356. package/dist/shared/settings.js +59 -10
  357. package/dist/shared/timeouts.js +2 -0
  358. package/dist/tools/cli-doc.js +97 -0
  359. package/dist/tools/grammar-density.js +1 -1
  360. package/dist/tools/instructions-sources.js +15 -16
  361. package/dist/tools/instructions.js +83 -39
  362. package/package.json +58 -42
  363. package/.bkit/core/.bkit/lite.md +0 -73
  364. package/.bkit/core/engine/commands/workflow/workstreams.ts +0 -172
  365. package/.bkit/core/prompts/bkit-build-plan-execute.md +0 -124
  366. package/.bkit/templates/scripts/ci/bkit-local-stack-lock.sh +0 -194
  367. package/dist/commands/workflow/workstreams.js +0 -149
  368. /package/.bkit/templates/.vale/styles/{House → Bkit}/Filler.yml +0 -0
  369. /package/.bkit/templates/.vale/styles/{House → Bkit}/Hedging.yml +0 -0
  370. /package/.bkit/templates/.vale/styles/{House → Bkit}/LLMSlop.yml +0 -0
  371. /package/.bkit/templates/.vale/styles/{House → Bkit}/Verbose.yml +0 -0
  372. /package/.bkit/templates/scripts/ci/{accepted-advisories.json → bkit-accepted-advisories.json} +0 -0
  373. /package/.bkit/templates/scripts/ci/{accepted-divergences.json → bkit-accepted-divergences.json} +0 -0
  374. /package/.bkit/templates/scripts/launchd/{launchd.env.example → bkit-launchd.env.example} +0 -0
  375. /package/.bkit/templates/scripts/launchd/{ai.bkit.launchd.plist → bkit-launchd.plist} +0 -0
@@ -0,0 +1,409 @@
1
+ // commands/workflow/tasks.ts — the plan's task boxes as a command.
2
+ //
3
+ // The tick a verified group hands over: `tick` addresses each task by the heading line
4
+ // inside the block `parsePlanBlocks` returns (`line`..`end`), rewrites only that line's
5
+ // box `[ ]` -> `[x]` and leaves every other byte of the plan as it was. Idempotent: a
6
+ // task already ticked is not rewritten and never repeated in `ticked`. `--green` reads a
7
+ // `bkit tests green` report and drops every task it gaps, so a group's gap-free tasks
8
+ // tick without the caller re-reading the report.
9
+ //
10
+ // `open` reads the execute log for the group's `DONE` / `BLOCKED` lines; `currency`
11
+ // matches each task's `CREATE` / `READ` / `UPDATE` / `DELETE` paths and symbols against
12
+ // `{base}...{against}` and prints the matched paths, needles and diff hunks — the
13
+ // mechanical half of the plan-currency judgement, which stays with the agent.
14
+
15
+ import * as fs from "node:fs";
16
+ import * as path from "node:path";
17
+
18
+ import { parseJson } from "../../shared/json.js";
19
+ import { CommandError, EXIT, type CommandSpec, git, runGit } from "../lib.js";
20
+ import { EXIT_DIFF_FAILED, fetchRemoteRef } from "./branches.js";
21
+ import { DIFF_BASE } from "./context.js";
22
+ import {
23
+ NO_MARKERS_REASON,
24
+ parsePlanBlocks,
25
+ type PlanGroup,
26
+ type PlanRef,
27
+ type PlanTaskBlock,
28
+ } from "./plan-blocks.js";
29
+ import { positiveInt, str } from "./status.js";
30
+ import { doneTasks } from "./tests.js";
31
+
32
+ /** A task's heading line: the box and the number the tick addresses. */
33
+ const TASK_BOX = /^(\s*)- \[([ xX])\] \*\*TASK (\d+):\*\*/;
34
+
35
+ /** A `tests green` report's gap line: `- Task {N} \`{kind}\` — …`. */
36
+ const REPORT_GAP = /^-\s+Task\s+(\d+)\s+`/gm;
37
+
38
+ interface Plan {
39
+ file: string;
40
+ /** The plan as written: every line the tick does not rewrite stays byte-identical. */
41
+ text: string;
42
+ groups: PlanGroup[];
43
+ tasks: PlanTaskBlock[];
44
+ }
45
+
46
+ /** The plan at `arg`, its blocks parsed: exit 2 for no such file or a block that does not parse, 1 for no task blocks. */
47
+ function readPlan(cwd: string, arg: string): Plan {
48
+ const file = path.resolve(cwd, arg);
49
+ if (fs.statSync(file, { throwIfNoEntry: false })?.isFile() !== true)
50
+ throw new CommandError(EXIT.USAGE, `${arg} does not exist`);
51
+ const text = fs.readFileSync(file, "utf-8");
52
+ const blocks = parsePlanBlocks(text);
53
+ const error = blocks.errors[0];
54
+ if (error !== undefined)
55
+ throw new CommandError(
56
+ EXIT.USAGE,
57
+ `${arg}:${error.line}: ${error.message} — run \`bkit tests plan-check\``,
58
+ );
59
+ if (blocks.tasks.length === 0) throw new CommandError(EXIT.NO, `${arg}: ${NO_MARKERS_REASON}`);
60
+ return { file, text, groups: blocks.groups, tasks: blocks.tasks };
61
+ }
62
+
63
+ /** The tasks of `group`: exit 2 when the plan has no such group. */
64
+ function groupTasks(plan: Plan, arg: string, group: number): PlanTaskBlock[] {
65
+ if (!plan.groups.some((g) => g.group === group))
66
+ throw new CommandError(EXIT.USAGE, `${arg} has no GROUP ${group}`);
67
+ return plan.tasks.filter((t) => t.group === group);
68
+ }
69
+
70
+ /**
71
+ * The task numbers a repeated value names: comma- or space-separated, each a positive
72
+ * integer. The token `none` names nothing to tick, so the verify header's empty form
73
+ * (`Tasks: none`) passes straight through as `ticked: []`.
74
+ */
75
+ function taskNumbers(values: readonly string[], flag: string): number[] {
76
+ const numbers: number[] = [];
77
+ let none = false;
78
+ for (const value of values)
79
+ for (const token of value.split(/[\s,]+/).filter((t) => t !== "")) {
80
+ if (token.toLowerCase() === "none") {
81
+ none = true;
82
+ continue;
83
+ }
84
+ if (!/^[1-9][0-9]*$/.test(token))
85
+ throw new CommandError(
86
+ EXIT.USAGE,
87
+ `--${flag} must name positive task numbers, got "${token}"`,
88
+ );
89
+ const n = Number(token);
90
+ if (!numbers.includes(n)) numbers.push(n);
91
+ }
92
+ if (numbers.length === 0 && !none) throw new CommandError(EXIT.USAGE, `--${flag} names no task`);
93
+ return numbers;
94
+ }
95
+
96
+ /**
97
+ * The task numbers a `bkit tests green` report gaps: the `- Task {N} \`{kind}\`` lines of
98
+ * its `## Gaps` section, or the `gaps` array of its `--json` payload.
99
+ */
100
+ export function gappedTasks(text: string): number[] {
101
+ const json = parseJson(text);
102
+ if (json.ok && typeof json.value === "object" && json.value !== null) {
103
+ const gaps = (json.value as { gaps?: unknown }).gaps;
104
+ if (Array.isArray(gaps))
105
+ return gaps
106
+ .map((g) => (typeof g === "object" && g !== null ? (g as { task?: unknown }).task : null))
107
+ .filter((t): t is number => typeof t === "number");
108
+ }
109
+ return [...text.matchAll(REPORT_GAP)].map((m) => Number(m[1]));
110
+ }
111
+
112
+ /** `task`'s heading line inside its block: its box and number, or null when the block holds none. */
113
+ function headBox(lines: string[], task: PlanTaskBlock): { index: number; ticked: boolean } | null {
114
+ for (let i = task.line - 1; i < task.end; i++) {
115
+ const box = TASK_BOX.exec(lines[i]!);
116
+ if (box !== null && Number(box[3]) === task.task) return { index: i, ticked: box[2] !== " " };
117
+ }
118
+ return null;
119
+ }
120
+
121
+ /** The refs a task names: its four path-and-symbol fields. */
122
+ const taskRefs = (task: PlanTaskBlock): PlanRef[] => [
123
+ ...task.create,
124
+ ...task.read,
125
+ ...task.update,
126
+ ...task.delete,
127
+ ];
128
+
129
+ /**
130
+ * A plan symbol as a `git diff -S` needle: a Markdown section's heading text, a flag,
131
+ * or the leading identifier of an applied symbol (`doneTasks(log, verbs)` -> `doneTasks`).
132
+ */
133
+ export function needleOf(symbol: string): string | undefined {
134
+ const s = symbol.trim();
135
+ if (s === "") return undefined;
136
+ const heading = /^§\s*(.+)$/.exec(s);
137
+ if (heading) return heading[1]!.trim();
138
+ const token = /^(--?[\w-]+|[A-Za-z_$][\w$]*)/.exec(s)?.[0];
139
+ if (token === undefined) return s;
140
+ return /^[A-Za-z_$][\w$]*\s*[(\[]/.test(s) || token === s ? token : s;
141
+ }
142
+
143
+ /** `paths` as git printed them, split on NUL; empty output is no path. */
144
+ const splitPaths = (stdout: string): string[] => stdout.split("\0").filter(Boolean);
145
+
146
+ /** A workflow run log: another feature's record, never code a plan task reads or changes. */
147
+ const RUN_LOG = /^docs\/plans\/[^/]+\/logs\//;
148
+
149
+ export const tasksCommand: CommandSpec = {
150
+ noun: "tasks",
151
+ summary: "Tick a plan's task boxes, from the tasks a verify judged satisfied.",
152
+ verbs: {
153
+ tick: {
154
+ summary:
155
+ "Tick the named tasks of one task group, less those a `tests green` report gaps; exit 0 ticked (including none), 1 a plan with no task blocks, 2 a bad flag value or plan.",
156
+ use: "workflow",
157
+ flags: [
158
+ {
159
+ name: "plan",
160
+ type: "string",
161
+ required: true,
162
+ help: "plan file holding the task blocks",
163
+ },
164
+ {
165
+ name: "group",
166
+ type: "string",
167
+ required: true,
168
+ help: "the task group whose tasks are candidates (positive integer)",
169
+ },
170
+ {
171
+ name: "tasks",
172
+ type: "string",
173
+ repeatable: true,
174
+ help: "the task numbers to tick, comma-separated or once per number; `none` ticks nothing (one of --tasks / --green)",
175
+ },
176
+ {
177
+ name: "green",
178
+ type: "string",
179
+ help: "a `bkit tests green` report: every task it gaps stays unticked (one of --tasks / --green)",
180
+ },
181
+ ],
182
+ output: {
183
+ plan: "the plan file ticked, as given",
184
+ group: "the group whose tasks were candidates",
185
+ ticked: "[task numbers] ticked by this call; a task already ticked is not repeated",
186
+ },
187
+ run: (ctx) => {
188
+ const planArg = String(ctx.flags.plan);
189
+ const group = positiveInt(ctx.flags.group, "group");
190
+ if (group === undefined)
191
+ throw new CommandError(EXIT.USAGE, "missing required flag --group");
192
+ const tasksFlag = ctx.flags.tasks as string[] | undefined;
193
+ const greenArg = str(ctx.flags.green);
194
+ if (greenArg === "") throw new CommandError(EXIT.USAGE, "--green must be non-empty");
195
+ if (tasksFlag === undefined && greenArg === undefined)
196
+ throw new CommandError(EXIT.USAGE, "give one of --tasks and --green");
197
+ const plan = readPlan(ctx.cwd, planArg);
198
+ const candidates = groupTasks(plan, planArg, group);
199
+ const named = tasksFlag === undefined ? undefined : taskNumbers(tasksFlag, "tasks");
200
+ for (const n of named ?? []) {
201
+ const task = plan.tasks.find((t) => t.task === n);
202
+ if (task === undefined) throw new CommandError(EXIT.USAGE, `${planArg} has no TASK ${n}`);
203
+ if (task.group !== group)
204
+ throw new CommandError(
205
+ EXIT.USAGE,
206
+ `TASK ${n} is in group ${task.group}, not group ${group}`,
207
+ );
208
+ }
209
+ const gapped = new Set<number>();
210
+ if (greenArg !== undefined) {
211
+ const greenFile = path.resolve(ctx.cwd, greenArg);
212
+ if (fs.statSync(greenFile, { throwIfNoEntry: false })?.isFile() !== true)
213
+ throw new CommandError(EXIT.USAGE, `--green ${greenArg} does not exist`);
214
+ for (const n of gappedTasks(fs.readFileSync(greenFile, "utf-8"))) gapped.add(n);
215
+ }
216
+ const wanted = named ?? candidates.map((t) => t.task);
217
+ const lines = plan.text.split(/\r?\n/);
218
+ const eol = plan.text.includes("\r\n") ? "\r\n" : "\n";
219
+ const ticked: number[] = [];
220
+ for (const n of wanted) {
221
+ if (gapped.has(n)) continue;
222
+ const task = candidates.find((t) => t.task === n);
223
+ if (task === undefined) continue;
224
+ const head = headBox(lines, task);
225
+ if (head === null || head.ticked) continue;
226
+ lines[head.index] = lines[head.index]!.replace(/^(\s*)- \[ \] /, "$1- [x] ");
227
+ ticked.push(n);
228
+ }
229
+ if (ticked.length > 0) fs.writeFileSync(plan.file, lines.join(eol));
230
+ for (const n of ticked) ctx.log(`ticked TASK ${n}`);
231
+ ctx.emit({
232
+ plan: planArg,
233
+ group,
234
+ ticked,
235
+ });
236
+ return EXIT.OK;
237
+ },
238
+ },
239
+ open: {
240
+ summary:
241
+ "Print the group's tasks holding neither a `DONE` nor a `BLOCKED` line in the plan-execute log, and whether any is open; exit 0 (including none), 1 a plan with no task blocks, 2 a missing file or group.",
242
+ use: "by-hand",
243
+ flags: [
244
+ {
245
+ name: "plan",
246
+ type: "string",
247
+ required: true,
248
+ help: "plan file holding the task blocks",
249
+ },
250
+ {
251
+ name: "group",
252
+ type: "string",
253
+ required: true,
254
+ help: "the task group whose tasks are read (positive integer)",
255
+ },
256
+ {
257
+ name: "log",
258
+ type: "string",
259
+ required: true,
260
+ help: "the plan-execute log holding its `DONE` / `BLOCKED — TASK {N}` lines",
261
+ },
262
+ ],
263
+ output: {
264
+ plan: "the plan file read, as given",
265
+ group: "the group whose tasks were read",
266
+ tasks: "[task numbers] the group's tasks with neither a `DONE` nor a `BLOCKED` line",
267
+ handover: "true when at least one task is open, else false",
268
+ },
269
+ run: (ctx) => {
270
+ const planArg = String(ctx.flags.plan);
271
+ const group = positiveInt(ctx.flags.group, "group");
272
+ if (group === undefined)
273
+ throw new CommandError(EXIT.USAGE, "missing required flag --group");
274
+ const logArg = String(ctx.flags.log);
275
+ const logFile = path.resolve(ctx.cwd, logArg);
276
+ if (fs.statSync(logFile, { throwIfNoEntry: false })?.isFile() !== true)
277
+ throw new CommandError(EXIT.USAGE, `--log ${logArg} does not exist`);
278
+ const plan = readPlan(ctx.cwd, planArg);
279
+ const candidates = groupTasks(plan, planArg, group);
280
+ const closed = new Set(doneTasks(fs.readFileSync(logFile, "utf-8"), ["DONE", "BLOCKED"]));
281
+ // The log decides, not the box: a ticked task with no log line is still open.
282
+ const open = candidates.filter((t) => !closed.has(t.task)).map((t) => t.task);
283
+ for (const n of open) ctx.log(`TASK ${n} is open`);
284
+ ctx.emit({ plan: planArg, group, tasks: open, handover: open.length > 0 });
285
+ return EXIT.OK;
286
+ },
287
+ },
288
+ currency: {
289
+ summary:
290
+ "Print each task whose CREATE / READ / UPDATE / DELETE paths or symbols moved between --base and --against, with its matched paths, needles and diff hunks (run logs under docs/plans/*/logs/ are left out); exit 0 (including no match), 1 a plan with no task blocks, 2 a bad plan.",
291
+ use: "by-hand",
292
+ flags: [
293
+ {
294
+ name: "plan",
295
+ type: "string",
296
+ required: true,
297
+ help: "plan file holding the task blocks",
298
+ },
299
+ {
300
+ name: "base",
301
+ type: "string",
302
+ help: "the commit the plan was written against (default origin/dev)",
303
+ },
304
+ {
305
+ name: "against",
306
+ type: "string",
307
+ help: "the commit landed changes are judged on (default origin/dev)",
308
+ },
309
+ ],
310
+ output: {
311
+ plan: "the plan file read, as given",
312
+ base: "the commit the plan was written against",
313
+ against: "the commit landed changes were judged on",
314
+ tasks:
315
+ "each matched task ({task, group, paths, needles, hunks}): the changed paths and the needles that moved, and the diff over those paths only",
316
+ },
317
+ exitCodes: {
318
+ [EXIT_DIFF_FAILED]: "--base or --against does not resolve, or git diff failed",
319
+ },
320
+ run: (ctx) => {
321
+ const planArg = String(ctx.flags.plan);
322
+ const base = str(ctx.flags.base) ?? DIFF_BASE;
323
+ const against = str(ctx.flags.against) ?? DIFF_BASE;
324
+ if (base === "") throw new CommandError(EXIT.USAGE, "--base must be non-empty");
325
+ if (against === "") throw new CommandError(EXIT.USAGE, "--against must be non-empty");
326
+ const plan = readPlan(ctx.cwd, planArg);
327
+ const top = git(ctx.cwd, ["rev-parse", "--show-toplevel"]);
328
+ if (top === null)
329
+ throw new CommandError(EXIT.USAGE, `not inside a git work tree: ${ctx.cwd}`);
330
+ for (const ref of [base, against]) {
331
+ const fetched = fetchRemoteRef(top, ref);
332
+ if (fetched !== null && !fetched.ok)
333
+ ctx.log(
334
+ `git fetch for ${ref} failed — judging against the last fetch: ${fetched.stderr}`,
335
+ );
336
+ if (git(top, ["rev-parse", "-q", "--verify", `${ref}^{commit}`]) === null)
337
+ throw new CommandError(EXIT_DIFF_FAILED, `${ref} does not resolve to a commit`);
338
+ }
339
+ const range = `${base}...${against}`;
340
+ const changed = runGit(top, ["diff", "--name-only", "-z", "--no-renames", range]);
341
+ if (!changed.ok)
342
+ throw new CommandError(EXIT_DIFF_FAILED, `git diff ${range} failed: ${changed.stderr}`);
343
+ const changedPaths = splitPaths(changed.stdout).filter((p) => !RUN_LOG.test(p));
344
+ const needleCache = new Map<string, string[]>();
345
+ /** The paths `needle` moved in, read once per needle. */
346
+ const needleMoves = (needle: string): string[] => {
347
+ const cached = needleCache.get(needle);
348
+ if (cached !== undefined) return cached;
349
+ const diff = runGit(top, [
350
+ "diff",
351
+ "--no-renames",
352
+ "-S",
353
+ needle,
354
+ "--name-only",
355
+ "-z",
356
+ range,
357
+ ]);
358
+ if (!diff.ok)
359
+ throw new CommandError(
360
+ EXIT_DIFF_FAILED,
361
+ `git diff ${range} -S${needle} failed: ${diff.stderr}`,
362
+ );
363
+ const paths = splitPaths(diff.stdout).filter((p) => !RUN_LOG.test(p));
364
+ needleCache.set(needle, paths);
365
+ return paths;
366
+ };
367
+ const tasks: Array<{
368
+ task: number;
369
+ group: number;
370
+ paths: string[];
371
+ needles: string[];
372
+ hunks: string[];
373
+ }> = [];
374
+ for (const task of plan.tasks) {
375
+ const refs = taskRefs(task);
376
+ const files = [...new Set(refs.map((r) => r.file.trim()).filter((f) => f !== ""))];
377
+ const paths = changedPaths.filter((p) =>
378
+ files.some((f) => p === f || p.startsWith(`${f}/`)),
379
+ );
380
+ const needles: string[] = [];
381
+ for (const ref of refs) {
382
+ if (ref.symbol === undefined) continue;
383
+ const needle = needleOf(ref.symbol);
384
+ if (needle === undefined || needles.includes(needle)) continue;
385
+ if (needleMoves(needle).length > 0) needles.push(needle);
386
+ }
387
+ const matched =
388
+ paths.length > 0
389
+ ? paths
390
+ : [...new Set(needles.flatMap((needle) => needleMoves(needle)))].sort();
391
+ if (matched.length === 0) continue;
392
+ const diff = runGit(top, ["diff", "--no-renames", range, "--", ...matched]);
393
+ if (!diff.ok)
394
+ throw new CommandError(EXIT_DIFF_FAILED, `git diff ${range} failed: ${diff.stderr}`);
395
+ tasks.push({
396
+ task: task.task,
397
+ group: task.group,
398
+ paths: matched,
399
+ needles,
400
+ hunks: diff.stdout === "" ? [] : diff.stdout.split("\n"),
401
+ });
402
+ }
403
+ for (const t of tasks) ctx.log(`TASK ${t.task}: ${t.paths.join(", ")}`);
404
+ ctx.emit({ plan: planArg, base, against, tasks });
405
+ return EXIT.OK;
406
+ },
407
+ },
408
+ },
409
+ };
@@ -0,0 +1,254 @@
1
+ // Where one named test sits in its file, and which of the file's other lines it runs —
2
+ // read from indentation alone, with no parser for any language, so `tests green` can
3
+ // tell an edit to a planned test from an edit elsewhere in the same file.
4
+ //
5
+ // A block is a line plus every following line indented deeper, plus a closing line at
6
+ // its own indent (`}`, `)`, `]`, `end`), and the deeper lines after that line when it
7
+ // opens again (`} else {`, `): T {`). A test's block starts at the line declaring it
8
+ // (`declaredName`). What a test runs: its own block, the lines of the blocks around it,
9
+ // and each item in those scopes that is not a test — a hook or bare statement always, a
10
+ // declaration only when the test, or code it runs, names it. Pure text in, no fs.
11
+
12
+ /** A test declaration's name on `line`, or null when the line declares no test. */
13
+ export function declaredName(line: string): string | null {
14
+ const call = /\b(?:it|test|suite|describe|context|bench)(?:\.[a-z]+)*\s*\(\s*(["'`])(.+?)\1/.exec(
15
+ line,
16
+ );
17
+ if (call !== null) return call[2]!;
18
+ const go = /\bfunc\s+(Test\w+)\s*\(/.exec(line);
19
+ if (go !== null) return go[1]!;
20
+ const py = /\bdef\s+(test_\w+)\s*\(/.exec(line);
21
+ if (py !== null) return py[1]!;
22
+ if (/@Test\b/.test(line)) {
23
+ const java = /\b(?:void|fun)\s+(\w+)\s*\(/.exec(line);
24
+ return java?.[1] ?? line.trim();
25
+ }
26
+ return null;
27
+ }
28
+
29
+ /** A test call whose name is on the next line: `it(` alone, as a formatter wraps a long name. */
30
+ const WRAPPED_CALL = /\b(?:it|test|suite|describe|context|bench)(?:\.[a-z]+)*\s*\(\s*$/;
31
+ const CLOSER = /^\s*(?:[}\])]|end\b)/;
32
+ const REOPENS = /(?:[{([]|\bdo)\s*$/;
33
+ const QUOTED = /"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|`(?:[^`\\]|\\.)*`/g;
34
+ const WORD = /[A-Za-z_$][\w$]*/g;
35
+ const IMPORT = /^\s*(?:import|from|use|using|require|include)\b|\brequire\s*\(/;
36
+ const IMPORT_WORDS = new Set([
37
+ "import",
38
+ "from",
39
+ "as",
40
+ "type",
41
+ "use",
42
+ "using",
43
+ "require",
44
+ "include",
45
+ ]);
46
+ const DECLARED =
47
+ /\b(?:function\*?|def|fn|func|class|interface|type|enum|struct|trait|module|const|let|var|val)\s+(?:\([^)]*\)\s*)?([A-Za-z_$][\w$]*)/;
48
+ const ASSIGNED = /^\s*(?:export\s+)?([A-Za-z_$][\w$]*)\s*(?::[^=]+)?=(?![=>])/;
49
+
50
+ const blank = (line: string) => line.trim() === "";
51
+ const indent = (line: string) => /^\s*/.exec(line)![0].length;
52
+
53
+ /** The test name the block starting at `i` declares, its wrapped name included. */
54
+ function openerName(lines: string[], i: number): string | null {
55
+ const own = declaredName(lines[i]!);
56
+ if (own !== null || !WRAPPED_CALL.test(lines[i]!)) return own;
57
+ const next = lines.slice(i + 1).find((l) => !blank(l));
58
+ return next === undefined ? null : declaredName(`${lines[i]!.trimEnd()}${next.trim()}`);
59
+ }
60
+
61
+ /** 0-based last line of the block starting at `start`. */
62
+ function blockEnd(lines: string[], start: number): number {
63
+ const own = indent(lines[start]!);
64
+ let end = start;
65
+ let i = start + 1;
66
+ for (;;) {
67
+ while (i < lines.length && (blank(lines[i]!) || indent(lines[i]!) > own)) {
68
+ if (!blank(lines[i]!)) end = i;
69
+ i++;
70
+ }
71
+ if (i >= lines.length || indent(lines[i]!) !== own || !CLOSER.test(lines[i]!)) return end;
72
+ end = i++;
73
+ if (!REOPENS.test(lines[end]!)) return end;
74
+ }
75
+ }
76
+
77
+ /** The blocks enclosing line `t`, innermost first, as their start lines. */
78
+ function ancestors(lines: string[], t: number): number[] {
79
+ const out: number[] = [];
80
+ let cur = t;
81
+ for (let i = t - 1; i >= 0; i--) {
82
+ if (blank(lines[i]!) || indent(lines[i]!) >= indent(lines[cur]!)) continue;
83
+ if (blockEnd(lines, i) < t) continue;
84
+ out.push(i);
85
+ cur = i;
86
+ }
87
+ return out;
88
+ }
89
+
90
+ /** The start lines of the items directly inside the block at `scope` (-1: the whole file). */
91
+ function items(lines: string[], scope: number): number[] {
92
+ const own = scope === -1 ? -1 : indent(lines[scope]!);
93
+ const last = scope === -1 ? lines.length - 1 : blockEnd(lines, scope);
94
+ const out: number[] = [];
95
+ for (let i = scope + 1; i <= last; i++) {
96
+ if (blank(lines[i]!) || indent(lines[i]!) <= own) continue;
97
+ out.push(i);
98
+ i = blockEnd(lines, i);
99
+ }
100
+ return out;
101
+ }
102
+
103
+ /** The names a declaration or import line binds; empty for a hook, call or bare statement. */
104
+ function declaredNames(line: string): string[] {
105
+ if (IMPORT.test(line))
106
+ return (line.replace(QUOTED, "").match(WORD) ?? []).filter((w) => !IMPORT_WORDS.has(w));
107
+ const name = DECLARED.exec(line)?.[1] ?? ASSIGNED.exec(line)?.[1];
108
+ return name === undefined ? [] : [name];
109
+ }
110
+
111
+ /** The 0-based start of the one block declaring `want`, or null when none or several do. */
112
+ function findTest(lines: string[], want: string): number | null {
113
+ const hits: Array<{ at: number; name: string }> = [];
114
+ for (let i = 0; i < lines.length; i++) {
115
+ const name = openerName(lines, i);
116
+ if (name !== null && (name === want || want.endsWith(` ${name}`))) hits.push({ at: i, name });
117
+ }
118
+ const exact = hits.filter((h) => h.name === want);
119
+ let pick = exact.length > 0 ? exact : hits;
120
+ if (pick.length > 1)
121
+ pick = pick.filter((h) =>
122
+ ancestors(lines, h.at).every((a) => {
123
+ const outer = openerName(lines, a);
124
+ return outer === null || want.includes(outer);
125
+ }),
126
+ );
127
+ return pick.length === 1 ? pick[0]!.at : null;
128
+ }
129
+
130
+ /** One side of a comparison: where the test sits, and the items around it. */
131
+ interface Side {
132
+ lines: string[];
133
+ start: number;
134
+ end: number;
135
+ /** The test's ancestors' start lines, innermost first. */
136
+ scopes: number[];
137
+ /** Each item in those scopes and the file's top level, keyed by its start line. */
138
+ around: Map<number, { end: number; runs: boolean }>;
139
+ }
140
+
141
+ function sideOf(text: string, want: string): Side | null {
142
+ const lines = text.split(/\r?\n/);
143
+ const start = findTest(lines, want);
144
+ if (start === null) return null;
145
+ const end = blockEnd(lines, start);
146
+ const scopes = ancestors(lines, start);
147
+ const words = (from: number, to: number) =>
148
+ lines
149
+ .slice(from, to + 1)
150
+ .join("\n")
151
+ .match(WORD) ?? [];
152
+ const reachable = new Set(words(start, end));
153
+ const around = new Map(
154
+ [...scopes, -1]
155
+ .flatMap((s) => items(lines, s))
156
+ .filter((i) => i !== start && !scopes.includes(i))
157
+ .map((i) => [
158
+ i,
159
+ {
160
+ end: blockEnd(lines, i),
161
+ runs: false,
162
+ test: openerName(lines, i) !== null,
163
+ names: declaredNames(lines[i]!),
164
+ },
165
+ ]),
166
+ );
167
+ // A hook or bare statement runs; a declaration runs once the test or code it runs names it.
168
+ for (let grew = true; grew;) {
169
+ grew = false;
170
+ for (const [i, item] of around) {
171
+ if (item.runs || item.test) continue;
172
+ if (item.names.length > 0 && !item.names.some((n) => reachable.has(n))) continue;
173
+ item.runs = true;
174
+ grew = true;
175
+ for (const w of words(i, item.end)) reachable.add(w);
176
+ }
177
+ }
178
+ return { lines, start, end, scopes, around };
179
+ }
180
+
181
+ /** A uniquely named test's source, enclosing declarations and reachable setup, or null if ambiguous. */
182
+ export function testSource(text: string, want: string): string[] | null {
183
+ const side = sideOf(text, want);
184
+ if (side === null) return null;
185
+ return [
186
+ ...side.scopes.map((at) => side.lines[at]!),
187
+ ...[...side.around]
188
+ .filter(([, item]) => item.runs)
189
+ .flatMap(([at, item]) => side.lines.slice(at, item.end + 1)),
190
+ ...side.lines.slice(side.start, side.end + 1),
191
+ ];
192
+ }
193
+
194
+ /** The 1-based lines a `git diff -U0` removes from its old file and adds to its new one. */
195
+ export function diffLines(diff: string): { removed: number[]; added: number[] } {
196
+ const removed: number[] = [];
197
+ const added: number[] = [];
198
+ for (const m of diff.matchAll(/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/gm)) {
199
+ const span = (at: string, n: string | undefined) =>
200
+ Array.from({ length: n === undefined ? 1 : Number(n) }, (_, k) => Number(at) + k);
201
+ removed.push(...span(m[1]!, m[2]));
202
+ added.push(...span(m[3]!, m[4]));
203
+ }
204
+ return { removed, added };
205
+ }
206
+
207
+ /** How the test named `want` changed between two copies of its file. */
208
+ export interface TestChange {
209
+ /** Its own block changed, indentation, trailing spaces and blank lines aside. */
210
+ body: boolean;
211
+ /** The first line of each changed line's item outside its block that it runs, or of a block around it. */
212
+ runs: string[];
213
+ }
214
+
215
+ /**
216
+ * How the test `want` changed from `before` to `after`, given the 1-based lines a diff
217
+ * removes from `before` and adds to `after`. Null when either copy does not declare the
218
+ * test in exactly one block: the caller then judges the whole file.
219
+ */
220
+ export function testChange(
221
+ before: string,
222
+ after: string,
223
+ want: string,
224
+ changed: { removed: number[]; added: number[] },
225
+ ): TestChange | null {
226
+ const old = sideOf(before, want);
227
+ const now = sideOf(after, want);
228
+ if (old === null || now === null) return null;
229
+ const body = (s: Side) => {
230
+ const base = indent(s.lines[s.start]!);
231
+ return s.lines
232
+ .slice(s.start, s.end + 1)
233
+ .filter((l) => !blank(l))
234
+ .map((l) => l.slice(Math.min(base, indent(l))).trimEnd())
235
+ .join("\n");
236
+ };
237
+ const runs = new Set<string>();
238
+ const judge = (s: Side, line: number) => {
239
+ const at = line - 1;
240
+ if (at < 0 || at >= s.lines.length || blank(s.lines[at]!)) return;
241
+ if (at >= s.start && at <= s.end) return;
242
+ for (const [i, item] of s.around)
243
+ if (at >= i && at <= item.end) {
244
+ if (item.runs) runs.add(s.lines[i]!.trim());
245
+ return;
246
+ }
247
+ // In no item: a line of a block around the test itself (its opener, a closer).
248
+ const scope = s.scopes.find((a) => a <= at && at <= blockEnd(s.lines, a));
249
+ if (scope !== undefined) runs.add(s.lines[scope]!.trim());
250
+ };
251
+ for (const line of changed.removed) judge(old, line);
252
+ for (const line of changed.added) judge(now, line);
253
+ return { body: body(old) !== body(now), runs: [...runs] };
254
+ }