@cbartens/bkit 0.0.0-stage → 5.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 (373) hide show
  1. package/.bkit/README.md +421 -0
  2. package/.bkit/core/.agents/context.md +95 -0
  3. package/.bkit/core/.agents/docs.md +62 -0
  4. package/.bkit/core/.agents/rules.md +147 -0
  5. package/.bkit/core/.agents/skills.md +43 -0
  6. package/.bkit/core/.bkit/LICENSE.md +151 -0
  7. package/.bkit/core/.bkit/README.md +421 -0
  8. package/.bkit/core/.bkit/cloud.md +502 -0
  9. package/.bkit/core/.bkit/comparison.md +318 -0
  10. package/.bkit/core/.bkit/install.md +496 -0
  11. package/.bkit/core/.bkit/jsonl.md +497 -0
  12. package/.bkit/core/.bkit/prompts.md +474 -0
  13. package/.bkit/core/.bkit/providers.md +598 -0
  14. package/.bkit/core/.bkit/templates.md +471 -0
  15. package/.bkit/core/.bkit/trackers.md +95 -0
  16. package/.bkit/core/.bkit/workflows.md +1546 -0
  17. package/.bkit/core/.claude/hooks/bkit-gateguard.cjs +82 -0
  18. package/.bkit/core/.claude/hooks/bkit-guardrails.cjs +93 -0
  19. package/.bkit/core/.claude/hooks/bkit-preflight.sh +74 -0
  20. package/.bkit/core/.codex/hooks/bkit-gateguard.cjs +115 -0
  21. package/.bkit/core/.codex/hooks/bkit-guardrails.cjs +135 -0
  22. package/.bkit/core/.cursor/hooks/bkit-gateguard.cjs +111 -0
  23. package/.bkit/core/.cursor/hooks/bkit-guardrails.cjs +169 -0
  24. package/.bkit/core/.cursor/hooks/bkit-preflight.sh +63 -0
  25. package/.bkit/core/.opencode/plugin/bkit-gateguard.js +225 -0
  26. package/.bkit/core/AGENTS.md +77 -0
  27. package/.bkit/core/LICENSE.md +149 -0
  28. package/.bkit/core/docs/templates/bkit-audit-accessibility.md +272 -0
  29. package/.bkit/core/docs/templates/bkit-audit-adoption.md +223 -0
  30. package/.bkit/core/docs/templates/bkit-audit-agents.md +230 -0
  31. package/.bkit/core/docs/templates/bkit-audit-aio-seo.md +227 -0
  32. package/.bkit/core/docs/templates/bkit-audit-analytics.md +266 -0
  33. package/.bkit/core/docs/templates/bkit-audit-annotations.md +230 -0
  34. package/.bkit/core/docs/templates/bkit-audit-app-performance.md +270 -0
  35. package/.bkit/core/docs/templates/bkit-audit-app-store.md +254 -0
  36. package/.bkit/core/docs/templates/bkit-audit-architecture.md +221 -0
  37. package/.bkit/core/docs/templates/bkit-audit-ci-performance.md +241 -0
  38. package/.bkit/core/docs/templates/bkit-audit-config.md +230 -0
  39. package/.bkit/core/docs/templates/bkit-audit-cost.md +218 -0
  40. package/.bkit/core/docs/templates/bkit-audit-coverage.md +221 -0
  41. package/.bkit/core/docs/templates/bkit-audit-database.md +229 -0
  42. package/.bkit/core/docs/templates/bkit-audit-dead-code.md +251 -0
  43. package/.bkit/core/docs/templates/bkit-audit-deferrals.md +283 -0
  44. package/.bkit/core/docs/templates/bkit-audit-dependencies.md +268 -0
  45. package/.bkit/core/docs/templates/bkit-audit-documentation.md +280 -0
  46. package/.bkit/core/docs/templates/bkit-audit-duplication.md +253 -0
  47. package/.bkit/core/docs/templates/bkit-audit-failures.md +259 -0
  48. package/.bkit/core/docs/templates/bkit-audit-flakes.md +237 -0
  49. package/.bkit/core/docs/templates/bkit-audit-gates.md +250 -0
  50. package/.bkit/core/docs/templates/bkit-audit-issues.md +273 -0
  51. package/.bkit/core/docs/templates/bkit-audit-llm.md +239 -0
  52. package/.bkit/core/docs/templates/bkit-audit-logs.md +266 -0
  53. package/.bkit/core/docs/templates/bkit-audit-models.md +210 -0
  54. package/.bkit/core/docs/templates/bkit-audit-privacy.md +211 -0
  55. package/.bkit/core/docs/templates/bkit-audit-prompts.md +216 -0
  56. package/.bkit/core/docs/templates/bkit-audit-red-team.md +220 -0
  57. package/.bkit/core/docs/templates/bkit-audit-reliability.md +222 -0
  58. package/.bkit/core/docs/templates/bkit-audit-repo.md +224 -0
  59. package/.bkit/core/docs/templates/bkit-audit-security.md +274 -0
  60. package/.bkit/core/docs/templates/bkit-audit-slop.md +200 -0
  61. package/.bkit/core/docs/templates/bkit-audit-testing.md +270 -0
  62. package/.bkit/core/docs/templates/bkit-audit-usability.md +237 -0
  63. package/.bkit/core/docs/templates/bkit-plan.md +476 -0
  64. package/.bkit/core/engine/checks/after/auth.ts +247 -0
  65. package/.bkit/core/engine/checks/after/ci-yml.ts +739 -0
  66. package/.bkit/core/engine/checks/after/github.ts +424 -0
  67. package/.bkit/core/engine/checks/after/index.ts +556 -0
  68. package/.bkit/core/engine/checks/after/managed.ts +217 -0
  69. package/.bkit/core/engine/checks/after/tools.ts +427 -0
  70. package/.bkit/core/engine/checks/after/vercel.ts +101 -0
  71. package/.bkit/core/engine/checks/before/context-fetch.ts +429 -0
  72. package/.bkit/core/engine/checks/before/env.ts +201 -0
  73. package/.bkit/core/engine/checks/before/files.ts +202 -0
  74. package/.bkit/core/engine/checks/before/index.ts +206 -0
  75. package/.bkit/core/engine/checks/before/mcp.ts +127 -0
  76. package/.bkit/core/engine/checks/before/repo.ts +77 -0
  77. package/.bkit/core/engine/checks/before/version.ts +67 -0
  78. package/.bkit/core/engine/checks/status-holes.ts +479 -0
  79. package/.bkit/core/engine/cli.ts +871 -0
  80. package/.bkit/core/engine/commands/analysis.ts +550 -0
  81. package/.bkit/core/engine/commands/ci/ci.ts +483 -0
  82. package/.bkit/core/engine/commands/comply.ts +415 -0
  83. package/.bkit/core/engine/commands/harvest.ts +459 -0
  84. package/.bkit/core/engine/commands/install/check.ts +70 -0
  85. package/.bkit/core/engine/commands/install/doctor.ts +247 -0
  86. package/.bkit/core/engine/commands/install/install.ts +528 -0
  87. package/.bkit/core/engine/commands/install/uninstall.ts +972 -0
  88. package/.bkit/core/engine/commands/install/upgrade.ts +1026 -0
  89. package/.bkit/core/engine/commands/launch.ts +139 -0
  90. package/.bkit/core/engine/commands/lib.ts +494 -0
  91. package/.bkit/core/engine/commands/registry.ts +53 -0
  92. package/.bkit/core/engine/commands/setup/setup.ts +184 -0
  93. package/.bkit/core/engine/commands/telemetry.ts +32 -0
  94. package/.bkit/core/engine/commands/workflow/branches.ts +1514 -0
  95. package/.bkit/core/engine/commands/workflow/context.ts +961 -0
  96. package/.bkit/core/engine/commands/workflow/counters.ts +268 -0
  97. package/.bkit/core/engine/commands/workflow/fids.ts +243 -0
  98. package/.bkit/core/engine/commands/workflow/issues.ts +675 -0
  99. package/.bkit/core/engine/commands/workflow/ledgers.ts +575 -0
  100. package/.bkit/core/engine/commands/workflow/locks.ts +478 -0
  101. package/.bkit/core/engine/commands/workflow/prs.ts +1087 -0
  102. package/.bkit/core/engine/commands/workflow/repo.ts +220 -0
  103. package/.bkit/core/engine/commands/workflow/roadmap.ts +576 -0
  104. package/.bkit/core/engine/commands/workflow/runs.ts +2140 -0
  105. package/.bkit/core/engine/commands/workflow/runtime.ts +300 -0
  106. package/.bkit/core/engine/commands/workflow/skills.ts +118 -0
  107. package/.bkit/core/engine/commands/workflow/status.ts +455 -0
  108. package/.bkit/core/engine/commands/workflow/steps.ts +1300 -0
  109. package/.bkit/core/engine/commands/workflow/tests.ts +161 -0
  110. package/.bkit/core/engine/commands/workflow/workflows.ts +1573 -0
  111. package/.bkit/core/engine/commands/workflow/workstreams.ts +164 -0
  112. package/.bkit/core/engine/commands/workflow/worktrees.ts +534 -0
  113. package/.bkit/core/engine/install/cloud-env.ts +925 -0
  114. package/.bkit/core/engine/install/managed-block.ts +212 -0
  115. package/.bkit/core/engine/install/manifest.ts +448 -0
  116. package/.bkit/core/engine/install/modules/claude.ts +362 -0
  117. package/.bkit/core/engine/install/modules/cloud.ts +174 -0
  118. package/.bkit/core/engine/install/modules/codex.ts +219 -0
  119. package/.bkit/core/engine/install/modules/core.ts +600 -0
  120. package/.bkit/core/engine/install/modules/cursor.ts +231 -0
  121. package/.bkit/core/engine/install/modules/index.ts +242 -0
  122. package/.bkit/core/engine/install/modules/opencode.ts +573 -0
  123. package/.bkit/core/engine/install/placeholders.ts +108 -0
  124. package/.bkit/core/engine/install/template-copies.ts +119 -0
  125. package/.bkit/core/engine/install/version-file.ts +284 -0
  126. package/.bkit/core/engine/install/write.ts +467 -0
  127. package/.bkit/core/engine/ledgers/clamp-log.ts +32 -0
  128. package/.bkit/core/engine/ledgers/status.ts +213 -0
  129. package/.bkit/core/engine/ledgers/telemetry.ts +1143 -0
  130. package/.bkit/core/engine/render/hosts.ts +372 -0
  131. package/.bkit/core/engine/render/models.ts +323 -0
  132. package/.bkit/core/engine/render/parts.ts +38 -0
  133. package/.bkit/core/engine/render/trackers.ts +170 -0
  134. package/.bkit/core/engine/shared/egress.ts +345 -0
  135. package/.bkit/core/engine/shared/json.ts +16 -0
  136. package/.bkit/core/engine/shared/log-prefix.ts +9 -0
  137. package/.bkit/core/engine/shared/package-json.ts +29 -0
  138. package/.bkit/core/engine/shared/package-manager.ts +38 -0
  139. package/.bkit/core/engine/shared/package-roots.ts +163 -0
  140. package/.bkit/core/engine/shared/protected-paths.ts +164 -0
  141. package/.bkit/core/engine/shared/settings.ts +72 -0
  142. package/.bkit/core/engine/shared/timeouts.ts +20 -0
  143. package/.bkit/core/engine/tools/wording.ts +748 -0
  144. package/.bkit/core/hooks/gateguard-edit-payload.js +18 -0
  145. package/.bkit/core/hooks/gateguard-engine.js +193 -0
  146. package/.bkit/core/hooks/guardrails-shell.js +154 -0
  147. package/.bkit/core/hooks/guardrails-tiers.js +68 -0
  148. package/.bkit/core/hooks/preflight-linear.sh +73 -0
  149. package/.bkit/core/prompts/bkit-audit-generate.md +66 -0
  150. package/.bkit/core/prompts/bkit-build-challenge-1.md +79 -0
  151. package/.bkit/core/prompts/bkit-build-challenge-2.md +81 -0
  152. package/.bkit/core/prompts/bkit-build-challenge-reconcile.md +68 -0
  153. package/.bkit/core/prompts/bkit-build-challenge-remediate.md +70 -0
  154. package/.bkit/core/prompts/bkit-build-challenge-tests.md +73 -0
  155. package/.bkit/core/prompts/bkit-build-install-skills.md +67 -0
  156. package/.bkit/core/prompts/bkit-build-plan-current.md +75 -0
  157. package/.bkit/core/prompts/bkit-build-plan-execute.md +72 -0
  158. package/.bkit/core/prompts/bkit-build-plan-gaps-remediate.md +73 -0
  159. package/.bkit/core/prompts/bkit-build-plan-verify.md +88 -0
  160. package/.bkit/core/prompts/bkit-build-summary.md +93 -0
  161. package/.bkit/core/prompts/bkit-debug-abort-handover.md +90 -0
  162. package/.bkit/core/prompts/bkit-debug-failure-diagnose.md +151 -0
  163. package/.bkit/core/prompts/bkit-debug-failure-remediate.md +78 -0
  164. package/.bkit/core/prompts/bkit-debug-failure-soak.md +73 -0
  165. package/.bkit/core/prompts/bkit-debug-summary.md +95 -0
  166. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-1.md +72 -0
  167. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-2.md +72 -0
  168. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-3.md +75 -0
  169. package/.bkit/core/prompts/bkit-design-generate.md +110 -0
  170. package/.bkit/core/prompts/bkit-design-organise.md +85 -0
  171. package/.bkit/core/prompts/bkit-design-review-1.md +121 -0
  172. package/.bkit/core/prompts/bkit-design-review-2.md +111 -0
  173. package/.bkit/core/prompts/bkit-design-spike.md +97 -0
  174. package/.bkit/core/prompts/bkit-design-summary.md +106 -0
  175. package/.bkit/core/prompts/bkit-design-update.md +66 -0
  176. package/.bkit/core/prompts/bkit-plan-complexity-check.md +66 -0
  177. package/.bkit/core/prompts/bkit-plan-complexity-split.md +87 -0
  178. package/.bkit/core/prompts/bkit-plan-generate.md +74 -0
  179. package/.bkit/core/prompts/bkit-plan-review-1.md +90 -0
  180. package/.bkit/core/prompts/bkit-plan-review-2.md +80 -0
  181. package/.bkit/core/prompts/bkit-plan-review-3.md +92 -0
  182. package/.bkit/core/prompts/bkit-plan-scope-check-1.md +84 -0
  183. package/.bkit/core/prompts/bkit-plan-scope-check-2.md +84 -0
  184. package/.bkit/core/prompts/bkit-plan-summary.md +106 -0
  185. package/.bkit/core/prompts/bkit-plan-update.md +68 -0
  186. package/.bkit/core/prompts/bkit-ship-conflict-resolve.md +90 -0
  187. package/.bkit/core/prompts/bkit-ship-deferred-sweep.md +94 -0
  188. package/.bkit/core/prompts/bkit-ship-summary.md +77 -0
  189. package/.bkit/core/prompts/bkit-ship-update-docs.md +71 -0
  190. package/.bkit/core/prompts/bkit-utility-compress-markdown.md +91 -0
  191. package/.bkit/core/prompts/bkit-utility-update-decisions.md +75 -0
  192. package/.bkit/core/prompts/bkit-utility-update-deferrals.md +76 -0
  193. package/.bkit/core/prompts/bkit-utility-update-documentation.md +61 -0
  194. package/.bkit/core/prompts/bkit-utility-update-failures.md +76 -0
  195. package/.bkit/core/prompts/bkit-utility-update-knowledge.md +75 -0
  196. package/.bkit/core/prompts/bkit-utility-update-roadmap.md +82 -0
  197. package/.bkit/core/workflows/bkit-auto.md +121 -0
  198. package/.bkit/core/workflows/bkit-build.md +802 -0
  199. package/.bkit/core/workflows/bkit-debug.md +642 -0
  200. package/.bkit/core/workflows/bkit-design.md +495 -0
  201. package/.bkit/core/workflows/bkit-plan.md +510 -0
  202. package/.bkit/core/workflows/bkit-ship.md +650 -0
  203. package/.bkit/shared/.bkit/.env.example +108 -0
  204. package/.bkit/shared/.claude/bkit-cloud.md +117 -0
  205. package/.bkit/shared/.claude/settings.json +88 -0
  206. package/.bkit/shared/.codex/hooks.json +26 -0
  207. package/.bkit/shared/.cursor/bkit-cloud.md +84 -0
  208. package/.bkit/shared/.cursor/hooks.json +26 -0
  209. package/.bkit/shared/.cursor/mcp.json +15 -0
  210. package/.bkit/shared/.cursor/rules/bkit-rules.mdc +12 -0
  211. package/.bkit/shared/.gitattributes +37 -0
  212. package/.bkit/shared/.mcp.json +16 -0
  213. package/.bkit/shared/.opencode/opencode.json +62 -0
  214. package/.bkit/shared/CLAUDE.md +11 -0
  215. package/.bkit/shared/LICENSE.md +22 -0
  216. package/.bkit/shared/docs/architecture.md +19 -0
  217. package/.bkit/shared/docs/data-schema.md +19 -0
  218. package/.bkit/shared/docs/deployments.md +438 -0
  219. package/.bkit/shared/docs/design.md +39 -0
  220. package/.bkit/shared/docs/noise.md +19 -0
  221. package/.bkit/shared/docs/observability.md +25 -0
  222. package/.bkit/shared/docs/security.md +42 -0
  223. package/.bkit/shared/docs/setup.md +30 -0
  224. package/.bkit/shared/docs/technology.md +18 -0
  225. package/.bkit/shared/docs/testing.md +413 -0
  226. package/.bkit/shared/gitignore +92 -0
  227. package/.bkit/templates/.claude/hooks/bkit-cloud-bootstrap.sh +192 -0
  228. package/.bkit/templates/.cursor/Dockerfile +68 -0
  229. package/.bkit/templates/.cursor/environment.json +6 -0
  230. package/.bkit/templates/.cursor/settings.json +11 -0
  231. package/.bkit/templates/.envrc +7 -0
  232. package/.bkit/templates/.github/dependabot.yml +52 -0
  233. package/.bkit/templates/.github/renovate.json5 +239 -0
  234. package/.bkit/templates/.github/workflows/bkit-ci.yml +1082 -0
  235. package/.bkit/templates/.github/workflows/bkit-cloud.yml +566 -0
  236. package/.bkit/templates/.github/workflows/bkit-renovate.yml +107 -0
  237. package/.bkit/templates/.graphifyignore +108 -0
  238. package/.bkit/templates/.husky/pre-commit +16 -0
  239. package/.bkit/templates/.husky/pre-push +91 -0
  240. package/.bkit/templates/.lintstagedrc.json +3 -0
  241. package/.bkit/templates/.prettierignore +41 -0
  242. package/.bkit/templates/.serena/project.yml +243 -0
  243. package/.bkit/templates/.vale/styles/House/Filler.yml +11 -0
  244. package/.bkit/templates/.vale/styles/House/Hedging.yml +12 -0
  245. package/.bkit/templates/.vale/styles/House/LLMSlop.yml +13 -0
  246. package/.bkit/templates/.vale/styles/House/Verbose.yml +12 -0
  247. package/.bkit/templates/.vale.ini +67 -0
  248. package/.bkit/templates/LICENSE.md +22 -0
  249. package/.bkit/templates/context/bkit-cloudrun.md +84 -0
  250. package/.bkit/templates/context/bkit-expo.md +186 -0
  251. package/.bkit/templates/context/bkit-github.md +79 -0
  252. package/.bkit/templates/context/bkit-inngest.md +45 -0
  253. package/.bkit/templates/context/bkit-maestro.md +76 -0
  254. package/.bkit/templates/context/bkit-playwright.md +62 -0
  255. package/.bkit/templates/context/bkit-resend.md +36 -0
  256. package/.bkit/templates/context/bkit-sentry.md +65 -0
  257. package/.bkit/templates/context/bkit-serena.md +33 -0
  258. package/.bkit/templates/context/bkit-stripe.md +36 -0
  259. package/.bkit/templates/context/bkit-supabase.md +59 -0
  260. package/.bkit/templates/context/bkit-tavily.md +49 -0
  261. package/.bkit/templates/context/bkit-vercel.md +64 -0
  262. package/.bkit/templates/docs/capabilities.md +72 -0
  263. package/.bkit/templates/scripts/ci/accepted-divergences.json +21 -0
  264. package/.bkit/templates/scripts/ci/check-accepted-divergences.mjs +152 -0
  265. package/.bkit/templates/scripts/ci/check-accepted-divergences.test.mjs +130 -0
  266. package/.bkit/templates/scripts/ci/lanes/resolve-push-base.mjs +98 -0
  267. package/.bkit/templates/scripts/ci/lanes/resolve-push-base.test.mjs +86 -0
  268. package/.bkit/templates/scripts/graphify/bkit-graph-drift-rules.json +23 -0
  269. package/.bkit/templates/scripts/graphify/bkit-graph-drift.py +208 -0
  270. package/.bkit/templates/scripts/graphify/bkit-graph-eager-rules.json +35 -0
  271. package/.bkit/templates/scripts/graphify/bkit-graph-eager.py +236 -0
  272. package/.bkit/templates/scripts/graphify/bkit-graph-external-rules.json +17 -0
  273. package/.bkit/templates/scripts/graphify/bkit-graph-imports.py +290 -0
  274. package/.bkit/templates/scripts/graphify/bkit-graph-pre-push.sh +19 -0
  275. package/.bkit/templates/scripts/graphify/bkit-graph-repair.py +120 -0
  276. package/.bkit/templates/scripts/graphify/bkit-graph-surface.py +232 -0
  277. package/.bkit/templates/scripts/graphify/bkit-graphify-graphml.py +36 -0
  278. package/.bkit/templates/scripts/graphify/bkit-graphify-rollup.py +927 -0
  279. package/.bkit/templates/scripts/graphify/bkit-graphify.sh +351 -0
  280. package/.bkit/templates/scripts/launchd/ai.bkit.launchd.plist +26 -0
  281. package/.bkit/templates/scripts/launchd/bkit-launchd-env.sh +53 -0
  282. package/.bkit/templates/scripts/launchd/launchd.env.example +13 -0
  283. package/.bkit/templates/scripts/prettier/bkit-insert-ignore.mjs +275 -0
  284. package/.bkit/templates/scripts/secrets/bkit-secrets.config.mjs +66 -0
  285. package/.bkit/templates/scripts/secrets/bkit-secrets.mjs +785 -0
  286. package/.bkit/templates/scripts/serena/bkit-serena-mcp.sh +30 -0
  287. package/.bkit/templates/scripts/serena/bkit-serena.mcp.json +9 -0
  288. package/.bkit/templates/scripts/slop/bkit-slop-type-suppression.mjs +286 -0
  289. package/.bkit/templates/slop-scan.config.json +120 -0
  290. package/CHANGELOG.md +1486 -0
  291. package/LICENSE.md +161 -0
  292. package/README.md +185 -2
  293. package/dist/checks/after/auth.js +232 -0
  294. package/dist/checks/after/ci-yml.js +718 -0
  295. package/dist/checks/after/github.js +400 -0
  296. package/dist/checks/after/index.js +447 -0
  297. package/dist/checks/after/managed.js +187 -0
  298. package/dist/checks/after/tools.js +351 -0
  299. package/dist/checks/after/vercel.js +102 -0
  300. package/dist/checks/before/context-fetch.js +426 -0
  301. package/dist/checks/before/env.js +173 -0
  302. package/dist/checks/before/files.js +185 -0
  303. package/dist/checks/before/index.js +162 -0
  304. package/dist/checks/before/mcp.js +111 -0
  305. package/dist/checks/before/repo.js +71 -0
  306. package/dist/checks/before/version.js +60 -0
  307. package/dist/checks/status-holes.js +393 -0
  308. package/dist/cli.js +692 -0
  309. package/dist/commands/analysis.js +325 -0
  310. package/dist/commands/ci/ci.js +438 -0
  311. package/dist/commands/comply.js +310 -0
  312. package/dist/commands/harvest.js +354 -0
  313. package/dist/commands/install/check.js +61 -0
  314. package/dist/commands/install/doctor.js +207 -0
  315. package/dist/commands/install/install.js +461 -0
  316. package/dist/commands/install/uninstall.js +815 -0
  317. package/dist/commands/install/upgrade.js +771 -0
  318. package/dist/commands/launch.js +118 -0
  319. package/dist/commands/lib.js +362 -0
  320. package/dist/commands/registry.js +50 -0
  321. package/dist/commands/setup/setup.js +158 -0
  322. package/dist/commands/telemetry.js +29 -0
  323. package/dist/commands/workflow/branches.js +1265 -0
  324. package/dist/commands/workflow/context.js +846 -0
  325. package/dist/commands/workflow/counters.js +227 -0
  326. package/dist/commands/workflow/fids.js +224 -0
  327. package/dist/commands/workflow/issues.js +585 -0
  328. package/dist/commands/workflow/ledgers.js +472 -0
  329. package/dist/commands/workflow/locks.js +364 -0
  330. package/dist/commands/workflow/prs.js +904 -0
  331. package/dist/commands/workflow/repo.js +199 -0
  332. package/dist/commands/workflow/roadmap.js +495 -0
  333. package/dist/commands/workflow/runs.js +1717 -0
  334. package/dist/commands/workflow/runtime.js +257 -0
  335. package/dist/commands/workflow/skills.js +110 -0
  336. package/dist/commands/workflow/status.js +392 -0
  337. package/dist/commands/workflow/steps.js +1093 -0
  338. package/dist/commands/workflow/tests.js +144 -0
  339. package/dist/commands/workflow/workflows.js +1342 -0
  340. package/dist/commands/workflow/workstreams.js +141 -0
  341. package/dist/commands/workflow/worktrees.js +473 -0
  342. package/dist/install/cloud-env.js +819 -0
  343. package/dist/install/managed-block.js +182 -0
  344. package/dist/install/manifest.js +376 -0
  345. package/dist/install/modules/claude.js +288 -0
  346. package/dist/install/modules/cloud.js +152 -0
  347. package/dist/install/modules/codex.js +193 -0
  348. package/dist/install/modules/core.js +511 -0
  349. package/dist/install/modules/cursor.js +186 -0
  350. package/dist/install/modules/index.js +152 -0
  351. package/dist/install/modules/opencode.js +504 -0
  352. package/dist/install/placeholders.js +87 -0
  353. package/dist/install/template-copies.js +106 -0
  354. package/dist/install/version-file.js +204 -0
  355. package/dist/install/write.js +342 -0
  356. package/dist/ledgers/clamp-log.js +27 -0
  357. package/dist/ledgers/status.js +156 -0
  358. package/dist/ledgers/telemetry.js +637 -0
  359. package/dist/render/hosts.js +339 -0
  360. package/dist/render/models.js +288 -0
  361. package/dist/render/parts.js +33 -0
  362. package/dist/render/trackers.js +135 -0
  363. package/dist/shared/egress.js +299 -0
  364. package/dist/shared/json.js +16 -0
  365. package/dist/shared/log-prefix.js +7 -0
  366. package/dist/shared/package-json.js +22 -0
  367. package/dist/shared/package-manager.js +32 -0
  368. package/dist/shared/package-roots.js +145 -0
  369. package/dist/shared/protected-paths.js +153 -0
  370. package/dist/shared/settings.js +44 -0
  371. package/dist/shared/timeouts.js +19 -0
  372. package/dist/tools/wording.js +680 -0
  373. package/package.json +101 -4
@@ -0,0 +1,680 @@
1
+ // tools/wording.ts — check and sync the shared sentences in `.bkit/wording.md`.
2
+ //
3
+ // Check mode runs in the test suite (`tools/wording.test.ts`); write mode is
4
+ // `pnpm wording:sync`. Same model as prettier: CI checks, the author writes.
5
+ //
6
+ // The unit is the sentence. A clause is matched as a substring of a line, so a
7
+ // list marker, step number, or trailing local text around it never matters.
8
+ // Clause instances carry no markers in the prompts: sync finds them by
9
+ // near-copy similarity and by the clause's `retired` wordings, which is how a
10
+ // wording change reaches every file — edit the clause, move the old text to
11
+ // `retired`, run the sync.
12
+ import * as fs from "node:fs";
13
+ import * as path from "node:path";
14
+ import { fileURLToPath } from "node:url";
15
+ import { findLineAnchoredMarkers, selectMarkers } from "../install/managed-block.js";
16
+ /** Near-copy threshold: a sentence this similar to a clause but not equal to it is drift. */
17
+ export const NEAR_COPY = 0.8;
18
+ /** Cut-off per surface: a sentence shared by this many files of one surface belongs in the wording file. */
19
+ export const SHARED_MIN = { prompt: 5, workflow: 3, audit: 5 };
20
+ const SURFACES = ["prompt", "workflow", "rules", "audit"];
21
+ /** Surfaces whose files open with the CAUTION header that positioned clauses order. */
22
+ const HEADED = ["prompt", "workflow"];
23
+ /** Surfaces checked inside the `bkit:start` / `bkit:end` block only. */
24
+ const MANAGED = ["rules", "audit"];
25
+ const NOUN = {
26
+ prompt: "prompts",
27
+ workflow: "workflows",
28
+ audit: "audit templates",
29
+ };
30
+ const MODES = [
31
+ "required",
32
+ "where-used",
33
+ "repeatable",
34
+ "forbidden",
35
+ "if-verdict",
36
+ "if-bash",
37
+ ];
38
+ const POSITIONS = ["first", "header", "last"];
39
+ const VERDICT_END = "<!-- verdict:end -->";
40
+ const SLOT = /\{\{([A-Za-z][\w-]*)(\?)?\}\}/g;
41
+ const LABEL = /^\*\*[^*]+:\*\* /;
42
+ /** Structure, not sentences: headings, HTML comments, table rows. Never counted toward the cut-off. */
43
+ const STRUCTURE = /^(?:#{1,6} |<!--|\|)/;
44
+ // ---------------------------------------------------------------------------
45
+ // Wording file
46
+ // ---------------------------------------------------------------------------
47
+ /** Parse `.bkit/wording.md`: one `### name` section per clause under `## Clauses`. */
48
+ export function parseWording(md) {
49
+ const start = md.indexOf("\n## Clauses\n");
50
+ if (start === -1)
51
+ throw new Error("[bkit-wording] wording file has no `## Clauses` section");
52
+ const clauses = [];
53
+ for (const section of md.slice(start).split(/^### /m).slice(1)) {
54
+ const name = section.slice(0, section.indexOf("\n")).trim();
55
+ const clause = {
56
+ name,
57
+ texts: [],
58
+ retired: [],
59
+ modes: {},
60
+ except: new Set(),
61
+ anchored: false,
62
+ };
63
+ for (const m of section.matchAll(/^- ([a-z]+): (.+)$/gm)) {
64
+ const [key, value] = [m[1], m[2].trim()];
65
+ if (SURFACES.includes(key)) {
66
+ if (!MODES.includes(value)) {
67
+ throw new Error(`[bkit-wording] ${name}: unknown mode "${value}" for ${key}`);
68
+ }
69
+ clause.modes[key] = value;
70
+ }
71
+ else if (key === "except") {
72
+ for (const f of value.split(","))
73
+ clause.except.add(f.trim());
74
+ }
75
+ else if (key === "position") {
76
+ if (!POSITIONS.includes(value)) {
77
+ throw new Error(`[bkit-wording] ${name}: position must be first, header, or last`);
78
+ }
79
+ clause.position = value;
80
+ }
81
+ else if (key === "anchored") {
82
+ clause.anchored = value === "yes";
83
+ }
84
+ else {
85
+ throw new Error(`[bkit-wording] ${name}: unknown key "${key}"`);
86
+ }
87
+ }
88
+ for (const m of section.matchAll(/^```(text|retired)\n([\s\S]*?)^```$/gm)) {
89
+ const lines = m[2].split("\n").filter((l) => l.trim() !== "");
90
+ if (m[1] === "text")
91
+ clause.texts.push(...lines);
92
+ else
93
+ clause.retired.push(...lines);
94
+ }
95
+ if (clause.texts.length === 0)
96
+ throw new Error(`[bkit-wording] ${name}: no text block`);
97
+ if (Object.keys(clause.modes).length === 0) {
98
+ throw new Error(`[bkit-wording] ${name}: applies to no surface`);
99
+ }
100
+ clauses.push(clause);
101
+ }
102
+ return clauses;
103
+ }
104
+ // ---------------------------------------------------------------------------
105
+ // Corpus
106
+ // ---------------------------------------------------------------------------
107
+ const RULES_FILES = [".bkit/core/.agents/rules.md", ".agents/rules.md"];
108
+ /** Audit templates, then the repo's adopted copies (same basenames, same managed block). */
109
+ const AUDIT_DIRS = [".bkit/core/docs/templates", "docs/templates"];
110
+ const AUDIT_FILE = /^bkit-audit-.+\.md$/;
111
+ /** Every file the wording file governs, read from `root`. */
112
+ export function loadCorpus(root) {
113
+ const files = [];
114
+ for (const [surface, dir] of [
115
+ ["prompt", path.join(".bkit", "core", "prompts")],
116
+ ["workflow", path.join(".bkit", "core", "workflows")],
117
+ ]) {
118
+ for (const f of fs
119
+ .readdirSync(path.join(root, dir))
120
+ .filter((n) => n.endsWith(".md"))
121
+ .sort()) {
122
+ const id = path.join(dir, f);
123
+ files.push({
124
+ surface,
125
+ id,
126
+ name: f.slice(0, -3),
127
+ content: fs.readFileSync(path.join(root, id), "utf-8"),
128
+ });
129
+ }
130
+ }
131
+ for (const dir of AUDIT_DIRS) {
132
+ if (!fs.existsSync(path.join(root, dir)))
133
+ continue;
134
+ for (const f of fs
135
+ .readdirSync(path.join(root, dir))
136
+ .filter((n) => AUDIT_FILE.test(n))
137
+ .sort()) {
138
+ const id = path.join(dir, f);
139
+ files.push({
140
+ surface: "audit",
141
+ id,
142
+ name: f.slice(0, -3),
143
+ content: fs.readFileSync(path.join(root, id), "utf-8"),
144
+ });
145
+ }
146
+ }
147
+ for (const id of RULES_FILES) {
148
+ if (!fs.existsSync(path.join(root, id)))
149
+ continue;
150
+ files.push({
151
+ surface: "rules",
152
+ id,
153
+ name: "rules",
154
+ content: fs.readFileSync(path.join(root, id), "utf-8"),
155
+ });
156
+ }
157
+ return files;
158
+ }
159
+ /**
160
+ * Indexes of the lines that are prose the clauses govern. Prompts and workflows:
161
+ * everything but front matter and fenced blocks (a fenced example is
162
+ * documentation, not instruction). Rules and audit templates: only the
163
+ * bkit-managed block — the consumer-owned part below `bkit:end` is never bkit's
164
+ * to check or rewrite.
165
+ */
166
+ export function proseLineIndexes(file) {
167
+ const lines = file.content.split("\n");
168
+ let from = 0;
169
+ let to = lines.length;
170
+ if (MANAGED.includes(file.surface)) {
171
+ const [startMarker, endMarker] = selectMarkers("html-comment");
172
+ const starts = findLineAnchoredMarkers(file.content, startMarker);
173
+ const ends = findLineAnchoredMarkers(file.content, endMarker);
174
+ if (starts.length !== 1 || ends.length !== 1)
175
+ return [];
176
+ from = file.content.slice(0, starts[0]).split("\n").length;
177
+ to = file.content.slice(0, ends[0]).split("\n").length - 1;
178
+ }
179
+ else if (lines[0] === "---") {
180
+ const close = lines.indexOf("---", 1);
181
+ if (close !== -1)
182
+ from = close + 1;
183
+ }
184
+ const out = [];
185
+ let fence = null;
186
+ for (let i = from; i < to; i++) {
187
+ const line = lines[i];
188
+ if (fence !== null) {
189
+ if (new RegExp(`^[ \\t]*${fence}[ \\t]*$`).test(line))
190
+ fence = null;
191
+ continue;
192
+ }
193
+ const open = /^[ \t]*(`{3,})[\w-]*[ \t]*$/.exec(line);
194
+ if (open) {
195
+ fence = open[1];
196
+ continue;
197
+ }
198
+ out.push(i);
199
+ }
200
+ return out;
201
+ }
202
+ /** Tool names in the front matter `tools:` list, inline (`[a, b]`) or block (`- a`) form. */
203
+ export function toolsOf(content) {
204
+ const lines = content.split("\n");
205
+ if (lines[0] !== "---")
206
+ return [];
207
+ const close = lines.indexOf("---", 1);
208
+ const front = lines.slice(1, close === -1 ? 1 : close);
209
+ const at = front.findIndex((l) => /^tools:/.test(l));
210
+ if (at === -1)
211
+ return [];
212
+ const inline = /^tools:[ \t]*\[(.*)\][ \t]*$/.exec(front[at]);
213
+ if (inline) {
214
+ return inline[1]
215
+ .split(",")
216
+ .map((t) => t.trim())
217
+ .filter(Boolean);
218
+ }
219
+ const tools = [];
220
+ for (const line of front.slice(at + 1)) {
221
+ if (/^\S/.test(line))
222
+ break;
223
+ const item = /^[ \t]+-[ \t]+(\S+)/.exec(line);
224
+ if (item)
225
+ tools.push(item[1]);
226
+ }
227
+ return tools;
228
+ }
229
+ // ---------------------------------------------------------------------------
230
+ // Matching
231
+ // ---------------------------------------------------------------------------
232
+ const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
233
+ const compiledCache = new Map();
234
+ export function compileText(text) {
235
+ const hit = compiledCache.get(text);
236
+ if (hit)
237
+ return hit;
238
+ const slots = [];
239
+ let source = "";
240
+ let last = 0;
241
+ for (const m of text.matchAll(SLOT)) {
242
+ source += escapeRe(text.slice(last, m.index));
243
+ source += m[2] ? "(.*?)" : "(.+?)";
244
+ slots.push(m[1]);
245
+ last = m.index + m[0].length;
246
+ }
247
+ const tail = text.slice(last);
248
+ // A closing `.` also matches `;`: the line continues the sentence with local text.
249
+ source += tail.endsWith(".") ? `${escapeRe(tail.slice(0, -1))}[.;]` : escapeRe(tail);
250
+ const first = text.search(SLOT);
251
+ const compiled = {
252
+ re: new RegExp(source, "g"),
253
+ slots,
254
+ prefix: first === -1 ? text : text.slice(0, first),
255
+ slotted: slots.length > 0,
256
+ };
257
+ compiledCache.set(text, compiled);
258
+ return compiled;
259
+ }
260
+ const countIn = (line, c) => [...line.matchAll(c.re)].length;
261
+ /** Drop the blockquote, list marker, step number, or checkbox in front of a line's text. */
262
+ export const stripLinePrefix = (line) => line.replace(/^[ \t]*(?:>[ \t]?)?(?:[ \t]*(?:[-*]|\d+\.)[ \t]+)?(?:\[[ xX]\][ \t]+)?/, "");
263
+ /** Split after `.` or `;` (bold close allowed between), never after `e.g.`; the punctuation stays on the sentence. */
264
+ export const splitSentences = (s) => s
265
+ .split(/(?<!\be\.g\.)(?<=[.;]\**)\s+(?=\S)/)
266
+ .map((x) => x.trim())
267
+ .filter(Boolean);
268
+ /** Word-level Levenshtein similarity in [0, 1]. */
269
+ export function similarity(a, b) {
270
+ const x = a.split(/\s+/);
271
+ const y = b.split(/\s+/);
272
+ let prev = Array.from({ length: y.length + 1 }, (_, j) => j);
273
+ for (let i = 1; i <= x.length; i++) {
274
+ const cur = [i];
275
+ for (let j = 1; j <= y.length; j++) {
276
+ cur[j] = Math.min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (x[i - 1] === y[j - 1] ? 0 : 1));
277
+ }
278
+ prev = cur;
279
+ }
280
+ return 1 - prev[y.length] / Math.max(x.length, y.length);
281
+ }
282
+ function modeFor(clause, file) {
283
+ if (clause.except.has(file.name))
284
+ return undefined;
285
+ const mode = clause.modes[file.surface];
286
+ if (mode === "if-verdict")
287
+ return file.content.includes(VERDICT_END) ? "required" : "forbidden";
288
+ if (mode === "if-bash")
289
+ return toolsOf(file.content).includes("Bash") ? "required" : "forbidden";
290
+ return mode;
291
+ }
292
+ const siblingCache = new WeakMap();
293
+ const lineCache = new Map();
294
+ const wordCounts = (words) => {
295
+ const counts = new Map();
296
+ for (const w of words)
297
+ counts.set(w, (counts.get(w) ?? 0) + 1);
298
+ return counts;
299
+ };
300
+ const canonicalCache = new Map();
301
+ /** A clause wording's sentence count, words, and word counts; memoised like `lineParts`. */
302
+ function canonicalParts(canonical) {
303
+ let hit = canonicalCache.get(canonical);
304
+ if (!hit) {
305
+ const words = canonical.split(/\s+/);
306
+ hit = { sentences: splitSentences(canonical).length, words, counts: wordCounts(words) };
307
+ canonicalCache.set(canonical, hit);
308
+ }
309
+ return hit;
310
+ }
311
+ /** A line's sentences and word counts, prefix dropped; memoised, since every clause scans every line. */
312
+ function lineParts(line) {
313
+ let hit = lineCache.get(line);
314
+ if (!hit) {
315
+ if (lineCache.size > 50_000)
316
+ lineCache.clear();
317
+ const rest = stripLinePrefix(line);
318
+ hit = { sentences: splitSentences(rest), words: wordCounts(rest.split(/\s+/)) };
319
+ lineCache.set(line, hit);
320
+ }
321
+ return hit;
322
+ }
323
+ /** Other unslotted clause wordings near enough to `canonical` to read as its near-copy. */
324
+ function siblingsOf(canonical, allTexts) {
325
+ let byText = siblingCache.get(allTexts);
326
+ if (!byText) {
327
+ byText = new Map();
328
+ siblingCache.set(allTexts, byText);
329
+ }
330
+ let hit = byText.get(canonical);
331
+ if (!hit) {
332
+ hit = [...allTexts].filter((t) => t !== canonical && !compileText(t).slotted && similarity(t, canonical) >= NEAR_COPY);
333
+ byText.set(canonical, hit);
334
+ }
335
+ return hit;
336
+ }
337
+ /** Sentence windows in a line that are near, but not equal to, an unslotted clause wording. */
338
+ function nearCopies(clause, lineIdx, line, allTexts) {
339
+ if (clause.texts.some((t) => countIn(line, compileText(t)) > 0))
340
+ return [];
341
+ const { sentences, words: lineWords } = lineParts(line);
342
+ const out = [];
343
+ for (const canonical of clause.texts) {
344
+ if (compileText(canonical).slotted)
345
+ continue;
346
+ // A line carrying a sibling clause verbatim (`Gate 2` beside `Gate 3`) is that clause, not drift of this one.
347
+ if (siblingsOf(canonical, allTexts).some((t) => countIn(line, compileText(t)) > 0))
348
+ continue;
349
+ const { sentences: k, words: canonicalWords, counts } = canonicalParts(canonical);
350
+ const cw = canonicalWords.length;
351
+ // Every window is drawn from the line's words: too few of the clause's words anywhere → no near-copy.
352
+ let shared = 0;
353
+ for (const [w, n] of counts)
354
+ shared += Math.min(n, lineWords.get(w) ?? 0);
355
+ if (shared < Math.ceil(NEAR_COPY * cw))
356
+ continue;
357
+ // A drift can add or drop a sentence break (`;` turned into `,`): try one sentence fewer and more.
358
+ for (let i = 0; i < sentences.length;) {
359
+ let best;
360
+ for (let size = Math.max(1, k - 1); size <= k + 1 && i + size <= sentences.length; size++) {
361
+ let window = sentences.slice(i, i + size).join(" ");
362
+ // An unlabelled clause can sit behind any surface's own label
363
+ // (`**Tools:**` in a prompt, `**Verify tools:**` in rules): compare the text only.
364
+ if (!canonical.startsWith("**"))
365
+ window = window.replace(LABEL, "");
366
+ // A clause can close a sentence that opens with local text (`**If** {cond} → {clause}`): also compare the tail.
367
+ const words = window.split(/\s+/);
368
+ const candidates = [window];
369
+ for (const n of [cw - 1, cw, cw + 1]) {
370
+ if (n > 0 && n < words.length)
371
+ candidates.push(words.slice(-n).join(" "));
372
+ }
373
+ for (const found of candidates) {
374
+ if (allTexts.has(found) || !nearCandidate(found.split(/\s+/), canonicalWords))
375
+ continue;
376
+ const score = similarity(found, canonical);
377
+ if (score >= NEAR_COPY && (!best || score > best.score))
378
+ best = { found, size, score };
379
+ }
380
+ }
381
+ if (!best) {
382
+ i++;
383
+ continue;
384
+ }
385
+ out.push({ line: lineIdx, found: best.found, canonical });
386
+ i += best.size;
387
+ }
388
+ }
389
+ return out;
390
+ }
391
+ /** Header line indexes in file order: the CAUTION block through to the body. */
392
+ export function headerLines(content) {
393
+ const lines = content.split("\n");
394
+ const start = lines.findIndex((l) => l.startsWith("> [!CAUTION]"));
395
+ if (start === -1)
396
+ return [];
397
+ const out = [];
398
+ for (let i = start; i < lines.length; i++) {
399
+ const line = lines[i];
400
+ if (line === "---" || /^\*\*/.test(line))
401
+ break;
402
+ out.push(i);
403
+ }
404
+ return out;
405
+ }
406
+ /** Labelled header lines in file order, EXECUTION PROTOCOL excluded. */
407
+ export function headerLabels(content) {
408
+ const lines = content.split("\n");
409
+ return headerLines(content)
410
+ .map((i) => lines[i].match(/^> (?:- )?\*\*([^*]+):\*\*/)?.[1])
411
+ .filter((l) => l !== undefined && l !== "EXECUTION PROTOCOL");
412
+ }
413
+ // ---------------------------------------------------------------------------
414
+ // Check
415
+ // ---------------------------------------------------------------------------
416
+ export function checkWording(clauses, files) {
417
+ const findings = [];
418
+ const allTexts = new Set(clauses.flatMap((c) => c.texts));
419
+ const compiled = new Map(clauses.map((c) => [c, c.texts.map(compileText)]));
420
+ for (const file of files) {
421
+ const lines = file.content.split("\n");
422
+ const prose = proseLineIndexes(file);
423
+ const headed = HEADED.includes(file.surface);
424
+ const labels = headed ? headerLabels(file.content) : [];
425
+ const header = new Set(headed ? headerLines(file.content) : []);
426
+ const positioned = [];
427
+ const at = (i) => (i === undefined ? undefined : i + 1);
428
+ for (const clause of clauses) {
429
+ const mode = modeFor(clause, file);
430
+ if (mode === undefined)
431
+ continue;
432
+ const forms = compiled.get(clause);
433
+ // Forms can overlap (a slotted form may also fit another's line): a line counts once per instance.
434
+ const perLine = (i) => Math.max(...forms.map((c) => countIn(lines[i], c)));
435
+ const hits = prose.filter((i) => perLine(i) > 0);
436
+ const count = hits.reduce((n, i) => n + perLine(i), 0);
437
+ if (mode === "forbidden" && count > 0) {
438
+ findings.push({
439
+ file: file.id,
440
+ line: at(hits[0]),
441
+ clause: clause.name,
442
+ problem: "present where it must not be",
443
+ });
444
+ }
445
+ if (mode === "required" && count === 0) {
446
+ findings.push({
447
+ file: file.id,
448
+ clause: clause.name,
449
+ problem: `missing; add: ${clause.texts[0]}`,
450
+ });
451
+ }
452
+ if ((mode === "required" || mode === "where-used") && count > 1) {
453
+ findings.push({
454
+ file: file.id,
455
+ line: at(hits[1]),
456
+ clause: clause.name,
457
+ problem: `appears ${count} times; expected once`,
458
+ });
459
+ }
460
+ if (mode === "forbidden")
461
+ continue;
462
+ for (const i of prose) {
463
+ const line = lines[i];
464
+ for (const near of nearCopies(clause, i, line, allTexts)) {
465
+ findings.push({
466
+ file: file.id,
467
+ line: near.line + 1,
468
+ clause: clause.name,
469
+ problem: `near-copy (run pnpm wording:sync): "${near.found}"`,
470
+ });
471
+ }
472
+ for (const old of clause.retired) {
473
+ if (countIn(line, compileText(old)) > 0) {
474
+ findings.push({
475
+ file: file.id,
476
+ line: i + 1,
477
+ clause: clause.name,
478
+ problem: "retired wording (run pnpm wording:sync)",
479
+ });
480
+ }
481
+ }
482
+ if (clause.anchored &&
483
+ forms.some((c) => c.slotted && line.includes(c.prefix)) &&
484
+ perLine(i) === 0) {
485
+ const templates = clause.texts.filter((t) => compileText(t).slotted);
486
+ findings.push({
487
+ file: file.id,
488
+ line: i + 1,
489
+ clause: clause.name,
490
+ problem: `does not match the template${templates.length > 1 ? "s" : ""}: ${templates.join(" | ")}`,
491
+ });
492
+ }
493
+ }
494
+ if (clause.position && count > 0 && headed) {
495
+ positioned.push({ clause, line: hits[0] });
496
+ const label = clause.texts[0].match(/^\*\*([^*]+):\*\*/)?.[1];
497
+ const edge = clause.position === "first" ? labels[0] : labels[labels.length - 1];
498
+ if (clause.position !== "header" && label && edge !== label) {
499
+ findings.push({
500
+ file: file.id,
501
+ clause: clause.name,
502
+ problem: `must be the ${clause.position} labelled header line; found "${edge ?? "none"}"`,
503
+ });
504
+ }
505
+ }
506
+ }
507
+ // Positioned clauses keep wording-file order inside the header.
508
+ let prev;
509
+ for (const p of positioned) {
510
+ if (!header.has(p.line)) {
511
+ findings.push({
512
+ file: file.id,
513
+ line: at(p.line),
514
+ clause: p.clause.name,
515
+ problem: "must be a header line",
516
+ });
517
+ continue;
518
+ }
519
+ if (prev && p.line < prev.line) {
520
+ findings.push({
521
+ file: file.id,
522
+ line: at(p.line),
523
+ clause: p.clause.name,
524
+ problem: `header order: must come after ${prev.clause.name}`,
525
+ });
526
+ }
527
+ prev = p;
528
+ }
529
+ }
530
+ return findings.concat(unlistedShared(clauses, files));
531
+ }
532
+ /** Could two sentences be NEAR_COPY-similar? Cheap word-count and shared-word bounds before `similarity`. */
533
+ function nearCandidate(a, b) {
534
+ const longer = Math.max(a.length, b.length);
535
+ const need = Math.ceil(NEAR_COPY * longer);
536
+ if (Math.min(a.length, b.length) < need)
537
+ return false;
538
+ const pool = new Map();
539
+ for (const w of b)
540
+ pool.set(w, (pool.get(w) ?? 0) + 1);
541
+ let shared = 0;
542
+ for (const w of a) {
543
+ const n = pool.get(w) ?? 0;
544
+ if (n > 0) {
545
+ shared++;
546
+ pool.set(w, n - 1);
547
+ }
548
+ }
549
+ return shared >= need;
550
+ }
551
+ /**
552
+ * Sentences shared by a surface's SHARED_MIN or more files that no clause covers.
553
+ * The cut-off is the rule for what belongs in the wording file. Files count by
554
+ * basename, so an adopted copy never counts twice. Headings, HTML comments, and
555
+ * table rows are structure and never count. A sentence ending its line and
556
+ * the same sentence continued with `;` count as one. Near-copies count toward the
557
+ * cut-off, so one drifted copy cannot hide a sentence the other files share.
558
+ */
559
+ export function unlistedShared(clauses, files) {
560
+ const forms = clauses.flatMap((c) => c.texts.map(compileText));
561
+ const findings = [];
562
+ for (const surface of SURFACES) {
563
+ const min = SHARED_MIN[surface];
564
+ if (min === undefined)
565
+ continue;
566
+ const seen = new Map();
567
+ for (const file of files.filter((f) => f.surface === surface)) {
568
+ const lines = file.content.split("\n");
569
+ for (const i of proseLineIndexes(file)) {
570
+ let rest = stripLinePrefix(lines[i]);
571
+ if (STRUCTURE.test(rest))
572
+ continue;
573
+ for (const c of forms)
574
+ rest = rest.replace(c.re, "\u0000");
575
+ for (const sentence of splitSentences(rest)) {
576
+ const s = sentence.replace(/[.;]$/, "");
577
+ if (s.includes("\u0000") || s.split(/\s+/).length < 3)
578
+ continue;
579
+ if (!seen.has(s))
580
+ seen.set(s, new Map());
581
+ if (!seen.get(s).has(file.name))
582
+ seen.get(s).set(file.name, file.id);
583
+ }
584
+ }
585
+ }
586
+ findings.push(...sharedGroups(seen, min, NOUN[surface]));
587
+ }
588
+ return findings;
589
+ }
590
+ /** Group each sentence with its near-copies; report every group reaching `min` files. */
591
+ function sharedGroups(seen, min, noun) {
592
+ const words = new Map([...seen.keys()].map((s) => [s, s.split(/\s+/)]));
593
+ const reported = new Set();
594
+ const findings = [];
595
+ // Seeds: sentences in 2+ files, widest first, so a group is reported once under its most-copied wording.
596
+ const seeds = [...seen].filter(([, ids]) => ids.size >= 2).sort((a, b) => b[1].size - a[1].size);
597
+ for (const [seed, ids] of seeds) {
598
+ if (reported.has(seed))
599
+ continue;
600
+ const near = [...seen.keys()].filter((s) => s !== seed &&
601
+ !reported.has(s) &&
602
+ nearCandidate(words.get(seed), words.get(s)) &&
603
+ similarity(seed, s) >= NEAR_COPY);
604
+ const group = new Set([...ids.keys(), ...near.flatMap((s) => [...seen.get(s).keys()])]);
605
+ if (group.size < min)
606
+ continue;
607
+ for (const s of [seed, ...near])
608
+ reported.add(s);
609
+ const drifted = near.length > 0 ? `, ${group.size - ids.size} as near-copies` : "";
610
+ findings.push({
611
+ file: [...ids.values()][0],
612
+ clause: "(none)",
613
+ problem: `shared by ${group.size} ${noun}${drifted} but not in .bkit/wording.md: "${seed}"`,
614
+ });
615
+ }
616
+ return findings;
617
+ }
618
+ // ---------------------------------------------------------------------------
619
+ // Sync
620
+ // ---------------------------------------------------------------------------
621
+ /** Rewrite near-copies and retired wordings in place. Returns only the files that changed. */
622
+ export function syncWording(clauses, files) {
623
+ const changed = new Map();
624
+ const allTexts = new Set(clauses.flatMap((c) => c.texts));
625
+ for (const file of files) {
626
+ const lines = file.content.split("\n");
627
+ for (const i of proseLineIndexes(file)) {
628
+ for (const clause of clauses) {
629
+ if (modeFor(clause, file) === undefined || modeFor(clause, file) === "forbidden")
630
+ continue;
631
+ const target = compileText(clause.texts[0]);
632
+ for (const old of clause.retired) {
633
+ const from = compileText(old);
634
+ lines[i] = lines[i].replace(from.re, (...groups) => {
635
+ const bySlot = new Map(from.slots.map((s, n) => [s, String(groups[n + 1])]));
636
+ const out = clause.texts[0].replace(SLOT, (_, slot) => bySlot.get(slot) ?? "");
637
+ return String(groups[0]).endsWith(";") && out.endsWith(".")
638
+ ? `${out.slice(0, -1)};`
639
+ : out;
640
+ });
641
+ }
642
+ if (target.slotted)
643
+ continue;
644
+ for (const near of nearCopies(clause, i, lines[i], allTexts)) {
645
+ lines[i] = lines[i].replace(near.found, near.canonical);
646
+ }
647
+ }
648
+ }
649
+ const content = lines.join("\n");
650
+ if (content !== file.content)
651
+ changed.set(file.id, content);
652
+ }
653
+ return changed;
654
+ }
655
+ // ---------------------------------------------------------------------------
656
+ // CLI: `pnpm wording:sync` (write) / `tsx .bkit/core/engine/tools/wording.ts` (check)
657
+ // ---------------------------------------------------------------------------
658
+ function main(argv) {
659
+ const root = process.cwd();
660
+ const clauses = parseWording(fs.readFileSync(path.join(root, ".bkit/wording.md"), "utf-8"));
661
+ let files = loadCorpus(root);
662
+ if (argv.includes("--write")) {
663
+ const changed = syncWording(clauses, files);
664
+ for (const [id, content] of changed) {
665
+ fs.writeFileSync(path.join(root, id), content, "utf-8");
666
+ console.error(`[bkit-wording] rewrote ${id}`);
667
+ }
668
+ files = loadCorpus(root);
669
+ }
670
+ const findings = checkWording(clauses, files);
671
+ for (const f of findings) {
672
+ console.error(`[bkit-wording] ${f.file}${f.line ? `:${f.line}` : ""} ${f.clause}: ${f.problem}`);
673
+ }
674
+ return findings.length === 0 ? 0 : 1;
675
+ }
676
+ if (process.argv[1] && fs.existsSync(process.argv[1])) {
677
+ if (fs.realpathSync(fileURLToPath(import.meta.url)) === fs.realpathSync(process.argv[1])) {
678
+ process.exitCode = main(process.argv.slice(2));
679
+ }
680
+ }