@cbartens/bkit 5.5.0 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (297) hide show
  1. package/.bkit/README.md +30 -20
  2. package/.bkit/core/.agents/docs.md +1 -0
  3. package/.bkit/core/.agents/rules.md +51 -26
  4. package/.bkit/core/.bkit/README.md +29 -19
  5. package/.bkit/core/.bkit/authoring.md +117 -0
  6. package/.bkit/core/.bkit/cloud.md +11 -11
  7. package/.bkit/core/.bkit/comparison.md +5 -5
  8. package/.bkit/core/.bkit/install.md +20 -20
  9. package/.bkit/core/.bkit/jsonl.md +202 -122
  10. package/.bkit/core/.bkit/lite.md +1 -1
  11. package/.bkit/core/.bkit/pipeline.md +49 -52
  12. package/.bkit/core/.bkit/prompts.md +106 -167
  13. package/.bkit/core/.bkit/providers.md +29 -32
  14. package/.bkit/core/.bkit/templates.md +44 -18
  15. package/.bkit/core/.bkit/trackers.md +60 -28
  16. package/.bkit/core/.bkit/workflows.md +399 -359
  17. package/.bkit/core/AGENTS.md +16 -22
  18. package/.bkit/core/docs/templates/bkit-audit-accessibility.md +14 -38
  19. package/.bkit/core/docs/templates/bkit-audit-adoption.md +16 -40
  20. package/.bkit/core/docs/templates/bkit-audit-agents.md +19 -43
  21. package/.bkit/core/docs/templates/bkit-audit-aio-seo.md +16 -40
  22. package/.bkit/core/docs/templates/bkit-audit-analytics.md +14 -38
  23. package/.bkit/core/docs/templates/bkit-audit-annotations.md +11 -35
  24. package/.bkit/core/docs/templates/bkit-audit-app-performance.md +15 -39
  25. package/.bkit/core/docs/templates/bkit-audit-app-store.md +12 -36
  26. package/.bkit/core/docs/templates/bkit-audit-architecture.md +16 -40
  27. package/.bkit/core/docs/templates/bkit-audit-ci-performance.md +16 -40
  28. package/.bkit/core/docs/templates/bkit-audit-config.md +18 -42
  29. package/.bkit/core/docs/templates/bkit-audit-cost.md +16 -40
  30. package/.bkit/core/docs/templates/bkit-audit-coverage.md +15 -39
  31. package/.bkit/core/docs/templates/bkit-audit-database.md +16 -40
  32. package/.bkit/core/docs/templates/bkit-audit-dead-code.md +15 -39
  33. package/.bkit/core/docs/templates/bkit-audit-deferrals.md +15 -38
  34. package/.bkit/core/docs/templates/bkit-audit-dependencies.md +16 -40
  35. package/.bkit/core/docs/templates/bkit-audit-documentation.md +11 -35
  36. package/.bkit/core/docs/templates/bkit-audit-duplication.md +18 -42
  37. package/.bkit/core/docs/templates/bkit-audit-failures.md +13 -37
  38. package/.bkit/core/docs/templates/bkit-audit-flakes.md +12 -36
  39. package/.bkit/core/docs/templates/bkit-audit-gates.md +14 -38
  40. package/.bkit/core/docs/templates/bkit-audit-issues.md +15 -39
  41. package/.bkit/core/docs/templates/bkit-audit-llm.md +16 -40
  42. package/.bkit/core/docs/templates/bkit-audit-logs.md +14 -38
  43. package/.bkit/core/docs/templates/bkit-audit-models.md +12 -36
  44. package/.bkit/core/docs/templates/bkit-audit-privacy.md +12 -36
  45. package/.bkit/core/docs/templates/bkit-audit-prompts.md +15 -39
  46. package/.bkit/core/docs/templates/bkit-audit-red-team.md +15 -39
  47. package/.bkit/core/docs/templates/bkit-audit-reliability.md +15 -39
  48. package/.bkit/core/docs/templates/bkit-audit-repo.md +12 -36
  49. package/.bkit/core/docs/templates/bkit-audit-security.md +13 -37
  50. package/.bkit/core/docs/templates/bkit-audit-slop.md +14 -38
  51. package/.bkit/core/docs/templates/bkit-audit-testing.md +12 -36
  52. package/.bkit/core/docs/templates/bkit-audit-usability.md +16 -40
  53. package/.bkit/core/docs/templates/bkit-plan.md +97 -226
  54. package/.bkit/core/engine/checks/after/ci-yml.ts +39 -30
  55. package/.bkit/core/engine/checks/after/github.ts +101 -72
  56. package/.bkit/core/engine/checks/after/index.ts +10 -12
  57. package/.bkit/core/engine/checks/after/suppression.ts +8 -5
  58. package/.bkit/core/engine/checks/after/tools.ts +47 -92
  59. package/.bkit/core/engine/checks/before/env.ts +2 -2
  60. package/.bkit/core/engine/checks/before/files.ts +5 -4
  61. package/.bkit/core/engine/checks/before/index.ts +18 -1
  62. package/.bkit/core/engine/checks/before/repo.ts +168 -2
  63. package/.bkit/core/engine/checks/tips/catalogue.ts +16 -5
  64. package/.bkit/core/engine/checks/tips/detect.ts +34 -0
  65. package/.bkit/core/engine/checks/tips/index.ts +10 -7
  66. package/.bkit/core/engine/cli.ts +20 -53
  67. package/.bkit/core/engine/commands/analysis.ts +171 -140
  68. package/.bkit/core/engine/commands/ci/ci.ts +6 -6
  69. package/.bkit/core/engine/commands/comply.ts +77 -103
  70. package/.bkit/core/engine/commands/install/doctor.ts +2 -2
  71. package/.bkit/core/engine/commands/install/install.ts +57 -18
  72. package/.bkit/core/engine/commands/install/uninstall.ts +65 -28
  73. package/.bkit/core/engine/commands/land.ts +18 -12
  74. package/.bkit/core/engine/commands/lib.ts +79 -17
  75. package/.bkit/core/engine/commands/registry.ts +4 -0
  76. package/.bkit/core/engine/commands/setup/setup.ts +7 -7
  77. package/.bkit/core/engine/commands/stats.ts +204 -0
  78. package/.bkit/core/engine/commands/tips.ts +10 -5
  79. package/.bkit/core/engine/commands/workflow/branches.ts +741 -62
  80. package/.bkit/core/engine/commands/workflow/challenge.ts +673 -0
  81. package/.bkit/core/engine/commands/workflow/context.ts +853 -29
  82. package/.bkit/core/engine/commands/workflow/counters.ts +267 -84
  83. package/.bkit/core/engine/commands/workflow/decisions.ts +541 -0
  84. package/.bkit/core/engine/commands/workflow/disk.ts +2 -2
  85. package/.bkit/core/engine/commands/workflow/fids.ts +793 -33
  86. package/.bkit/core/engine/commands/workflow/issues.ts +427 -105
  87. package/.bkit/core/engine/commands/workflow/ledgers.ts +884 -27
  88. package/.bkit/core/engine/commands/workflow/locks.ts +26 -2
  89. package/.bkit/core/engine/commands/workflow/plan-blocks.ts +582 -0
  90. package/.bkit/core/engine/commands/workflow/plan-versions.ts +21 -0
  91. package/.bkit/core/engine/commands/workflow/prs.ts +166 -30
  92. package/.bkit/core/engine/commands/workflow/repo.ts +59 -7
  93. package/.bkit/core/engine/commands/workflow/roadmap.ts +16 -7
  94. package/.bkit/core/engine/commands/workflow/run-facts.ts +501 -0
  95. package/.bkit/core/engine/commands/workflow/runs.ts +179 -49
  96. package/.bkit/core/engine/commands/workflow/runtime.ts +22 -1
  97. package/.bkit/core/engine/commands/workflow/status.ts +142 -29
  98. package/.bkit/core/engine/commands/workflow/step-table.ts +339 -0
  99. package/.bkit/core/engine/commands/workflow/steps.ts +213 -88
  100. package/.bkit/core/engine/commands/workflow/summary.ts +398 -0
  101. package/.bkit/core/engine/commands/workflow/tests.ts +1013 -148
  102. package/.bkit/core/engine/commands/workflow/tracker.ts +1195 -0
  103. package/.bkit/core/engine/commands/workflow/workflows.ts +984 -207
  104. package/.bkit/core/engine/commands/workflow/workstreams.ts +11 -5
  105. package/.bkit/core/engine/commands/workflow/worktrees.ts +276 -22
  106. package/.bkit/core/engine/install/manifest.ts +11 -9
  107. package/.bkit/core/engine/install/modules/cloud.ts +21 -13
  108. package/.bkit/core/engine/install/modules/core.ts +325 -7
  109. package/.bkit/core/engine/install/modules/opencode.ts +2 -2
  110. package/.bkit/core/engine/install/version-file.ts +6 -2
  111. package/.bkit/core/engine/ledgers/migrate.ts +92 -0
  112. package/.bkit/core/engine/ledgers/status.ts +92 -15
  113. package/.bkit/core/engine/ledgers/telemetry.ts +1261 -254
  114. package/.bkit/core/engine/render/hosts.ts +4 -0
  115. package/.bkit/core/engine/render/models.ts +12 -0
  116. package/.bkit/core/engine/shared/branches.ts +4 -1
  117. package/.bkit/core/engine/shared/egress.ts +7 -0
  118. package/.bkit/core/engine/shared/json.ts +25 -7
  119. package/.bkit/core/engine/shared/settings.ts +202 -33
  120. package/.bkit/core/engine/shared/text.ts +361 -0
  121. package/.bkit/core/engine/tools/grammar-density.ts +215 -0
  122. package/.bkit/core/engine/tools/instructions-sources.ts +834 -0
  123. package/.bkit/core/engine/tools/instructions.ts +1751 -0
  124. package/.bkit/core/hooks/gateguard-engine.js +24 -23
  125. package/.bkit/core/hooks/guardrails-shell.js +2 -3
  126. package/.bkit/core/hooks/guardrails-tiers.js +7 -1
  127. package/.bkit/core/hooks/readguard-engine.js +2 -2
  128. package/.bkit/core/prompts/bkit-audit-generate.md +62 -25
  129. package/.bkit/core/prompts/bkit-build-challenge-1.md +40 -17
  130. package/.bkit/core/prompts/bkit-build-challenge-2.md +44 -17
  131. package/.bkit/core/prompts/bkit-build-install-skills.md +26 -13
  132. package/.bkit/core/prompts/bkit-build-plan-convert.md +74 -0
  133. package/.bkit/core/prompts/bkit-build-plan-current.md +26 -13
  134. package/.bkit/core/prompts/bkit-build-plan-execute.md +74 -26
  135. package/.bkit/core/prompts/bkit-build-plan-gaps-remediate.md +64 -23
  136. package/.bkit/core/prompts/bkit-build-plan-verify.md +57 -33
  137. package/.bkit/core/prompts/bkit-debug-abort-handover.md +30 -14
  138. package/.bkit/core/prompts/bkit-debug-failure-diagnose.md +34 -14
  139. package/.bkit/core/prompts/bkit-debug-failure-remediate.md +34 -17
  140. package/.bkit/core/prompts/bkit-debug-failure-reproduce.md +26 -13
  141. package/.bkit/core/prompts/bkit-debug-failure-soak.md +31 -16
  142. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-1.md +21 -11
  143. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-2.md +20 -11
  144. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-3.md +20 -11
  145. package/.bkit/core/prompts/bkit-design-generate.md +33 -13
  146. package/.bkit/core/prompts/bkit-design-organise.md +25 -12
  147. package/.bkit/core/prompts/bkit-design-review-1.md +31 -15
  148. package/.bkit/core/prompts/bkit-design-review-2.md +28 -13
  149. package/.bkit/core/prompts/bkit-design-spike.md +30 -13
  150. package/.bkit/core/prompts/bkit-design-update.md +33 -19
  151. package/.bkit/core/prompts/bkit-plan-generate.md +75 -19
  152. package/.bkit/core/prompts/bkit-plan-review-1.md +50 -27
  153. package/.bkit/core/prompts/bkit-plan-review-2.md +59 -28
  154. package/.bkit/core/prompts/bkit-plan-scope-check.md +109 -0
  155. package/.bkit/core/prompts/bkit-plan-update.md +61 -24
  156. package/.bkit/core/prompts/bkit-ship-conflict-resolve.md +27 -15
  157. package/.bkit/core/prompts/bkit-ship-deferred-sweep.md +31 -16
  158. package/.bkit/core/prompts/bkit-ship-update-docs.md +30 -15
  159. package/.bkit/core/prompts/bkit-utility-compress-markdown.md +24 -14
  160. package/.bkit/core/prompts/bkit-utility-update-decisions.md +32 -14
  161. package/.bkit/core/prompts/bkit-utility-update-deferrals.md +29 -14
  162. package/.bkit/core/prompts/bkit-utility-update-documentation.md +28 -13
  163. package/.bkit/core/prompts/bkit-utility-update-failures.md +31 -14
  164. package/.bkit/core/prompts/bkit-utility-update-knowledge.md +33 -14
  165. package/.bkit/core/prompts/bkit-utility-update-roadmap.md +38 -17
  166. package/.bkit/core/prompts/bkit-utility-update-tracker.md +43 -20
  167. package/.bkit/core/workflows/bkit-auto.md +54 -46
  168. package/.bkit/core/workflows/bkit-build.md +577 -458
  169. package/.bkit/core/workflows/bkit-debug.md +348 -333
  170. package/.bkit/core/workflows/bkit-design.md +201 -237
  171. package/.bkit/core/workflows/bkit-plan.md +210 -321
  172. package/.bkit/core/workflows/bkit-ship.md +328 -278
  173. package/.bkit/shared/.bkit/.env.example +2 -2
  174. package/.bkit/shared/.claude/settings.json +11 -0
  175. package/.bkit/shared/.cursor/mcp.json +1 -1
  176. package/.bkit/shared/.cursor/rules/bkit-rules.mdc +1 -1
  177. package/.bkit/shared/.gitattributes +1 -1
  178. package/.bkit/shared/.mcp.json +1 -1
  179. package/.bkit/shared/.opencode/opencode.json +11 -0
  180. package/.bkit/shared/CLAUDE.md +2 -2
  181. package/.bkit/shared/GEMINI.md +2 -2
  182. package/.bkit/shared/docs/architecture.md +1 -1
  183. package/.bkit/shared/docs/data-schema.md +1 -1
  184. package/.bkit/shared/docs/deployments.md +16 -28
  185. package/.bkit/shared/docs/design.md +1 -1
  186. package/.bkit/shared/docs/noise.md +1 -1
  187. package/.bkit/shared/docs/observability.md +2 -2
  188. package/.bkit/shared/docs/security.md +1 -1
  189. package/.bkit/shared/docs/setup.md +1 -1
  190. package/.bkit/shared/docs/technology.md +1 -1
  191. package/.bkit/shared/docs/testing.md +27 -10
  192. package/.bkit/templates/.claude/hooks/bkit-cloud-bootstrap.sh +1 -1
  193. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-change.yml +26 -5
  194. package/.bkit/templates/.github/workflows/bkit-ci.yml +22 -84
  195. package/.bkit/templates/.github/workflows/bkit-cloud.yml +21 -19
  196. package/.bkit/templates/.husky/pre-push +52 -13
  197. package/.bkit/templates/.prettierignore +7 -0
  198. package/.bkit/templates/scripts/ci/bkit-local-stack-lock.sh +194 -0
  199. package/.bkit/templates/scripts/ci/check-capstone.mjs +2 -4
  200. package/.bkit/templates/scripts/ci/check-capstone.test.mjs +4 -23
  201. package/.bkit/templates/scripts/ci/lanes/resolve-push-base.mjs +104 -23
  202. package/.bkit/templates/scripts/ci/lanes/resolve-push-base.test.mjs +103 -6
  203. package/.bkit/templates/scripts/cleanup/bkit-disk-prune.mjs +1 -1
  204. package/.bkit/templates/scripts/graphify/bkit-graph-pre-push.sh +3 -1
  205. package/.bkit/templates/scripts/prettier/bkit-insert-ignore.mjs +49 -29
  206. package/CHANGELOG.md +142 -0
  207. package/README.md +58 -34
  208. package/dist/checks/after/ci-yml.js +39 -29
  209. package/dist/checks/after/github.js +99 -72
  210. package/dist/checks/after/index.js +11 -13
  211. package/dist/checks/after/suppression.js +8 -5
  212. package/dist/checks/after/tools.js +40 -83
  213. package/dist/checks/before/env.js +2 -2
  214. package/dist/checks/before/files.js +5 -3
  215. package/dist/checks/before/index.js +15 -2
  216. package/dist/checks/before/repo.js +163 -2
  217. package/dist/checks/tips/catalogue.js +14 -5
  218. package/dist/checks/tips/detect.js +32 -0
  219. package/dist/checks/tips/index.js +7 -5
  220. package/dist/cli.js +19 -47
  221. package/dist/commands/analysis.js +109 -100
  222. package/dist/commands/ci/ci.js +6 -6
  223. package/dist/commands/comply.js +54 -91
  224. package/dist/commands/install/doctor.js +2 -2
  225. package/dist/commands/install/install.js +50 -19
  226. package/dist/commands/install/uninstall.js +59 -28
  227. package/dist/commands/land.js +16 -12
  228. package/dist/commands/lib.js +51 -16
  229. package/dist/commands/registry.js +4 -0
  230. package/dist/commands/setup/setup.js +7 -7
  231. package/dist/commands/stats.js +136 -0
  232. package/dist/commands/tips.js +7 -3
  233. package/dist/commands/workflow/branches.js +656 -56
  234. package/dist/commands/workflow/challenge.js +556 -0
  235. package/dist/commands/workflow/context.js +709 -30
  236. package/dist/commands/workflow/counters.js +208 -75
  237. package/dist/commands/workflow/decisions.js +441 -0
  238. package/dist/commands/workflow/disk.js +2 -2
  239. package/dist/commands/workflow/fids.js +689 -33
  240. package/dist/commands/workflow/issues.js +362 -99
  241. package/dist/commands/workflow/ledgers.js +762 -27
  242. package/dist/commands/workflow/locks.js +24 -2
  243. package/dist/commands/workflow/plan-blocks.js +437 -0
  244. package/dist/commands/workflow/plan-versions.js +22 -0
  245. package/dist/commands/workflow/prs.js +142 -32
  246. package/dist/commands/workflow/repo.js +52 -7
  247. package/dist/commands/workflow/roadmap.js +9 -7
  248. package/dist/commands/workflow/run-facts.js +426 -0
  249. package/dist/commands/workflow/runs.js +144 -43
  250. package/dist/commands/workflow/runtime.js +12 -1
  251. package/dist/commands/workflow/status.js +130 -29
  252. package/dist/commands/workflow/step-table.js +276 -0
  253. package/dist/commands/workflow/steps.js +171 -80
  254. package/dist/commands/workflow/summary.js +275 -0
  255. package/dist/commands/workflow/tests.js +834 -136
  256. package/dist/commands/workflow/tracker.js +983 -0
  257. package/dist/commands/workflow/workflows.js +834 -195
  258. package/dist/commands/workflow/workstreams.js +11 -5
  259. package/dist/commands/workflow/worktrees.js +221 -22
  260. package/dist/install/manifest.js +11 -9
  261. package/dist/install/modules/cloud.js +22 -13
  262. package/dist/install/modules/core.js +326 -7
  263. package/dist/install/modules/opencode.js +2 -2
  264. package/dist/install/version-file.js +6 -2
  265. package/dist/ledgers/migrate.js +81 -0
  266. package/dist/ledgers/status.js +61 -8
  267. package/dist/ledgers/telemetry.js +905 -124
  268. package/dist/render/hosts.js +4 -0
  269. package/dist/render/models.js +12 -0
  270. package/dist/shared/branches.js +3 -1
  271. package/dist/shared/egress.js +7 -0
  272. package/dist/shared/json.js +20 -7
  273. package/dist/shared/settings.js +147 -26
  274. package/dist/shared/text.js +333 -0
  275. package/dist/tools/grammar-density.js +183 -0
  276. package/dist/tools/instructions-sources.js +721 -0
  277. package/dist/tools/instructions.js +1579 -0
  278. package/package.json +114 -23
  279. package/.bkit/core/engine/commands/harvest.ts +0 -459
  280. package/.bkit/core/engine/tools/wording.ts +0 -748
  281. package/.bkit/core/prompts/bkit-build-challenge-reconcile.md +0 -69
  282. package/.bkit/core/prompts/bkit-build-challenge-remediate.md +0 -71
  283. package/.bkit/core/prompts/bkit-build-challenge-tests.md +0 -74
  284. package/.bkit/core/prompts/bkit-build-summary.md +0 -94
  285. package/.bkit/core/prompts/bkit-debug-summary.md +0 -96
  286. package/.bkit/core/prompts/bkit-design-summary.md +0 -107
  287. package/.bkit/core/prompts/bkit-plan-complexity-check.md +0 -67
  288. package/.bkit/core/prompts/bkit-plan-complexity-split.md +0 -88
  289. package/.bkit/core/prompts/bkit-plan-review-3.md +0 -93
  290. package/.bkit/core/prompts/bkit-plan-scope-check-1.md +0 -85
  291. package/.bkit/core/prompts/bkit-plan-scope-check-2.md +0 -85
  292. package/.bkit/core/prompts/bkit-plan-summary.md +0 -107
  293. package/.bkit/core/prompts/bkit-ship-summary.md +0 -78
  294. package/.bkit/templates/scripts/ci/lanes/check-tree-cache.mjs +0 -138
  295. package/.bkit/templates/scripts/ci/lanes/check-tree-cache.test.mjs +0 -181
  296. package/dist/commands/harvest.js +0 -353
  297. package/dist/tools/wording.js +0 -680
@@ -1,138 +0,0 @@
1
- #!/usr/bin/env node
2
- // Example. Copy into scripts/ci/lanes/ and own it. bkit install does not write this file
3
- // and does not overwrite a copy.
4
- /**
5
- * check-tree-cache.mjs — whether a pull request lane may skip its suite.
6
- *
7
- * A pull request lane skips when the files it would test are exactly the files a push run
8
- * already tested green. "The files" is the git tree of the checked-out commit (for a pull
9
- * request, GitHub's merge preview). A push run tests its head commit's tree, so a green push
10
- * run whose head has the same tree proved this lane — provided that run's job of the same name
11
- * concluded `success` (a `[skip ci]` or `run_level=none` push concludes green with every lane
12
- * skipped, which proves nothing).
13
- *
14
- * Push and dispatch runs never skip: the run on a push to dev or the default branch is the
15
- * record of what that branch holds, so it always runs the suite. `[force build]` /
16
- * `[force cache]` in the pull request title skip nothing either.
17
- *
18
- * Usage: node scripts/ci/lanes/check-tree-cache.mjs >> "$GITHUB_OUTPUT"
19
- * (env: GH_TOKEN, REPO, EVENT_NAME, JOB, PR_TITLE; optional WORKFLOW_FILE)
20
- * Output: `skip=true` or `skip=false` on stdout; the reason on stderr. Exit 0 always — every
21
- * failure (gh, git, unparseable output) resolves to `skip=false`, which runs the suite.
22
- */
23
-
24
- import { spawnSync } from "node:child_process";
25
- import { pathToFileURL } from "node:url";
26
-
27
- /** The workflow whose runs count. Set WORKFLOW_FILE if your CI file has another name. */
28
- export const WORKFLOW_FILE = process.env.WORKFLOW_FILE || "bkit-ci.yml";
29
-
30
- /** How far back the run lookup reaches. */
31
- export const LOOKBACK_DAYS = 7;
32
-
33
- /** At most this many tree-matching runs have their jobs read before giving up. */
34
- export const MAX_JOB_LOOKUPS = 3;
35
-
36
- const FORCE_MARKERS = ["[force build]", "[force cache]"];
37
-
38
- /**
39
- * Pure decision. `runs` are green push runs (`{ id, run_number, tree }`); `jobConclusion(id)`
40
- * returns the conclusion of this lane's job in run `id` ("" when unknown).
41
- */
42
- export function decideTreeSkip({ eventName, title, headTree, runs, jobConclusion }) {
43
- if (eventName !== "pull_request") {
44
- return { skip: false, reason: `${eventName || "unknown"} event — only a pull request skips` };
45
- }
46
- const marker = FORCE_MARKERS.find((m) => (title ?? "").includes(m));
47
- if (marker) return { skip: false, reason: `${marker} in the title` };
48
- if (!headTree) return { skip: false, reason: "no tree for HEAD" };
49
- const matches = (runs ?? [])
50
- .filter((r) => r?.tree === headTree && Number.isFinite(r?.id))
51
- .sort((a, b) => (b.run_number ?? 0) - (a.run_number ?? 0))
52
- .slice(0, MAX_JOB_LOOKUPS);
53
- for (const run of matches) {
54
- const conclusion = jobConclusion(run.id);
55
- if (conclusion === "success") {
56
- return { skip: true, reason: `tree ${headTree} passed in push run ${run.id}` };
57
- }
58
- }
59
- return {
60
- skip: false,
61
- reason: matches.length
62
- ? `tree ${headTree} matched ${matches.length} push run(s), none with this job green`
63
- : `no green push run in ${LOOKBACK_DAYS} days has tree ${headTree}`,
64
- };
65
- }
66
-
67
- /** Runs `gh`; never throws. `ok` false carries the reason (spawn error or first stderr line). */
68
- function gh(args) {
69
- const { status, stdout, stderr, error } = spawnSync("gh", args, {
70
- encoding: "utf8",
71
- stdio: ["ignore", "pipe", "pipe"],
72
- });
73
- if (error || status !== 0) {
74
- return { ok: false, out: "", reason: error ? error.message : String(stderr).split("\n")[0] };
75
- }
76
- return { ok: true, out: stdout, reason: "" };
77
- }
78
-
79
- function greenPushRuns(repo, now = new Date()) {
80
- const since = new Date(now.getTime() - LOOKBACK_DAYS * 86_400_000).toISOString().slice(0, 10);
81
- const path =
82
- `repos/${repo}/actions/workflows/${WORKFLOW_FILE}/runs` +
83
- `?event=push&status=success&created=${encodeURIComponent(`>=${since}`)}&per_page=100`;
84
- const res = gh([
85
- "api",
86
- path,
87
- "--jq",
88
- "[.workflow_runs[] | {id, run_number, tree: .head_commit.tree_id}]",
89
- ]);
90
- // No run list is not fatal: main() turns this into skip=false, which runs the suite.
91
- if (!res.ok) throw new Error(`could not list ${WORKFLOW_FILE} runs (${res.reason})`);
92
- return JSON.parse(res.out || "[]");
93
- }
94
-
95
- function jobConclusionIn(repo, job) {
96
- return (runId) => {
97
- const res = gh([
98
- "api",
99
- `repos/${repo}/actions/runs/${runId}/jobs?per_page=100`,
100
- "--jq",
101
- `[.jobs[] | select(.name == ${JSON.stringify(job)}) | .conclusion] | first // ""`,
102
- ]);
103
- // An unreadable job list proves nothing about this lane, so the run does not count.
104
- if (!res.ok) {
105
- console.error(`[check-tree-cache] could not read jobs of run ${runId} (${res.reason})`);
106
- return "";
107
- }
108
- return res.out.trim();
109
- };
110
- }
111
-
112
- function headTree() {
113
- const { status, stdout } = spawnSync("git", ["rev-parse", "HEAD^{tree}"], {
114
- encoding: "utf8",
115
- stdio: ["ignore", "pipe", "ignore"],
116
- });
117
- return status === 0 ? stdout.trim() : "";
118
- }
119
-
120
- export function main(env) {
121
- let result;
122
- try {
123
- const isPr = env.EVENT_NAME === "pull_request";
124
- result = decideTreeSkip({
125
- eventName: env.EVENT_NAME,
126
- title: env.PR_TITLE,
127
- headTree: isPr ? headTree() : "",
128
- runs: isPr ? greenPushRuns(env.REPO) : [],
129
- jobConclusion: jobConclusionIn(env.REPO, env.JOB),
130
- });
131
- } catch (err) {
132
- result = { skip: false, reason: `lookup failed (${err.message}) — running the suite` };
133
- }
134
- console.error(`[check-tree-cache] skip=${result.skip} — ${result.reason}`);
135
- process.stdout.write(`skip=${result.skip}\n`);
136
- }
137
-
138
- if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) main(process.env);
@@ -1,181 +0,0 @@
1
- #!/usr/bin/env node
2
- // Example. Copy into scripts/ci/lanes/ and own it. bkit install does not write this file
3
- // and does not overwrite a copy.
4
- /**
5
- * check-tree-cache.test.mjs — unit tests for the pull request tree cache.
6
- *
7
- * Pure Node, no runner: `node scripts/ci/lanes/check-tree-cache.test.mjs`.
8
- * Exit: 0 = all tests passed, 1 = at least one test failed.
9
- */
10
-
11
- import { spawnSync } from "node:child_process";
12
- import fs from "node:fs";
13
- import os from "node:os";
14
- import path from "node:path";
15
- import { fileURLToPath } from "node:url";
16
- import { decideTreeSkip } from "./check-tree-cache.mjs";
17
-
18
- const SCRIPT = fileURLToPath(new URL("./check-tree-cache.mjs", import.meta.url));
19
-
20
- /** Run the script with fake `gh` / `git` binaries first on PATH. */
21
- function runWithFakes({ gh, git, eventName = "pull_request", title = "feat: x" }) {
22
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), "tree-cache-"));
23
- try {
24
- fs.writeFileSync(path.join(dir, "gh"), `#!/bin/sh\n${gh}\n`, { mode: 0o755 });
25
- fs.writeFileSync(path.join(dir, "git"), `#!/bin/sh\n${git}\n`, { mode: 0o755 });
26
- const r = spawnSync(process.execPath, [SCRIPT], {
27
- encoding: "utf8",
28
- env: {
29
- ...process.env,
30
- PATH: `${dir}${path.delimiter}${process.env.PATH}`,
31
- REPO: "o/r",
32
- EVENT_NAME: eventName,
33
- JOB: "bkit-tests-unit",
34
- PR_TITLE: title,
35
- },
36
- });
37
- return { status: r.status, stdout: r.stdout.trim(), stderr: r.stderr };
38
- } finally {
39
- fs.rmSync(dir, { recursive: true, force: true });
40
- }
41
- }
42
-
43
- let passed = 0;
44
- let failed = 0;
45
-
46
- function test(name, fn) {
47
- try {
48
- fn();
49
- passed++;
50
- console.log(` ok ${name}`);
51
- } catch (err) {
52
- failed++;
53
- console.log(` FAIL ${name} — ${err.message}`);
54
- }
55
- }
56
-
57
- function assert(cond, message) {
58
- if (!cond) throw new Error(message);
59
- }
60
-
61
- const TREE = "t".repeat(40);
62
- const OTHER = "o".repeat(40);
63
- const green = () => "success";
64
- const base = { eventName: "pull_request", title: "feat: x", headTree: TREE };
65
-
66
- test("a pull request whose files a green push run tested skips", () => {
67
- const r = decideTreeSkip({
68
- ...base,
69
- runs: [{ id: 7, run_number: 3, tree: TREE }],
70
- jobConclusion: green,
71
- });
72
- assert(r.skip === true, r.reason);
73
- });
74
-
75
- test("a push run never skips, even with the same files green", () => {
76
- for (const eventName of ["push", "workflow_dispatch"]) {
77
- const r = decideTreeSkip({
78
- ...base,
79
- eventName,
80
- runs: [{ id: 7, run_number: 3, tree: TREE }],
81
- jobConclusion: green,
82
- });
83
- assert(r.skip === false, `${eventName}: ${r.reason}`);
84
- }
85
- });
86
-
87
- test("[force build] / [force cache] in the title run the suite", () => {
88
- for (const title of ["feat: x [force build]", "[force cache] feat: x"]) {
89
- const r = decideTreeSkip({
90
- ...base,
91
- title,
92
- runs: [{ id: 7, run_number: 3, tree: TREE }],
93
- jobConclusion: green,
94
- });
95
- assert(r.skip === false, `${title}: ${r.reason}`);
96
- }
97
- });
98
-
99
- test("other files → no skip", () => {
100
- const r = decideTreeSkip({
101
- ...base,
102
- runs: [{ id: 7, run_number: 3, tree: OTHER }],
103
- jobConclusion: green,
104
- });
105
- assert(r.skip === false, r.reason);
106
- });
107
-
108
- test("same files, but that run skipped this lane ([skip ci] / run_level=none) → no skip", () => {
109
- const r = decideTreeSkip({
110
- ...base,
111
- runs: [{ id: 7, run_number: 3, tree: TREE }],
112
- jobConclusion: () => "skipped",
113
- });
114
- assert(r.skip === false, r.reason);
115
- });
116
-
117
- test("a later matching run with the lane green is found past one that skipped it", () => {
118
- const seen = [];
119
- const r = decideTreeSkip({
120
- ...base,
121
- runs: [
122
- { id: 1, run_number: 1, tree: TREE },
123
- { id: 2, run_number: 2, tree: TREE },
124
- ],
125
- jobConclusion: (id) => {
126
- seen.push(id);
127
- return id === 1 ? "success" : "skipped";
128
- },
129
- });
130
- assert(r.skip === true, r.reason);
131
- assert(seen.join(",") === "2,1", `newest first, got ${seen}`);
132
- });
133
-
134
- test("no tree for HEAD → no skip", () => {
135
- const r = decideTreeSkip({
136
- ...base,
137
- headTree: "",
138
- runs: [{ id: 7, run_number: 3, tree: "" }],
139
- jobConclusion: green,
140
- });
141
- assert(r.skip === false, r.reason);
142
- });
143
-
144
- const RUNS = `echo '[{"id":7,"run_number":3,"tree":"${TREE}"}]'`;
145
- const GH_OK = `case "$2" in *jobs*) echo success ;; *) ${RUNS} ;; esac`;
146
-
147
- test("end to end: matching green push run prints skip=true", () => {
148
- const r = runWithFakes({ gh: GH_OK, git: `echo ${TREE}` });
149
- assert(r.status === 0 && r.stdout === "skip=true", JSON.stringify(r));
150
- });
151
-
152
- test("end to end: a push event prints skip=false without calling gh", () => {
153
- const r = runWithFakes({ gh: "exit 9", git: `echo ${TREE}`, eventName: "push" });
154
- assert(r.status === 0 && r.stdout === "skip=false", JSON.stringify(r));
155
- });
156
-
157
- test("a gh failure exits 0 with skip=false", () => {
158
- const r = runWithFakes({ gh: "exit 4", git: `echo ${TREE}` });
159
- assert(r.status === 0 && r.stdout === "skip=false", JSON.stringify(r));
160
- });
161
-
162
- test("unparseable gh output exits 0 with skip=false", () => {
163
- const r = runWithFakes({ gh: "echo 'not json'", git: `echo ${TREE}` });
164
- assert(r.status === 0 && r.stdout === "skip=false", JSON.stringify(r));
165
- });
166
-
167
- test("a failed job lookup does not count the run: skip=false", () => {
168
- const r = runWithFakes({
169
- gh: `case "$2" in *jobs*) echo boom >&2; exit 1 ;; *) ${RUNS} ;; esac`,
170
- git: `echo ${TREE}`,
171
- });
172
- assert(r.status === 0 && r.stdout === "skip=false", JSON.stringify(r));
173
- });
174
-
175
- test("a git failure exits 0 with skip=false", () => {
176
- const r = runWithFakes({ gh: GH_OK, git: "exit 128" });
177
- assert(r.status === 0 && r.stdout === "skip=false", JSON.stringify(r));
178
- });
179
-
180
- console.log(`\n${passed} passed, ${failed} failed`);
181
- process.exit(failed > 0 ? 1 : 0);
@@ -1,353 +0,0 @@
1
- // commands/harvest.ts — Cross-repo harvest CLI.
2
- //
3
- // MOVES consumer `.bkit/friction.jsonl` and COPIES `.bkit/stats.jsonl` into bkit's
4
- // central store at `harvest/`. COUNT-ONLY aggregates `docs/{deferrals,failures}.jsonl`
5
- // (dimensional counts from envelope metadata; prose NOT copied). Deduplicates by
6
- // row content; entity counts fold by per-kind composite identity.
7
- //
8
- // INVARIANTS (PII guard + move/copy contract):
9
- // - Touches ONLY the named .jsonl ledgers — never any .env/secret files.
10
- // - NEVER commits in any repo (bkit's or a consumer's) — the caller commits.
11
- // - stats.jsonl is COPY-ONLY: consumer mtime + content unchanged.
12
- // - friction.jsonl is MOVE: harvested rows are pruned from the consumer, but ONLY
13
- // after that exact row is durably fsync'd + read back from the central store.
14
- // - The prune gate is byte-for-byte CONTENT, never identity.
15
- // - The central store ACCUMULATES across runs (never cleared) and holds every
16
- // DISTINCT row, deduped by content only. It is never rewritten from the fold. It is
17
- // gitignored (private consumer content), so it is the only record of pruned consumer
18
- // friction; readers fold it.
19
- // - Malformed consumer lines are NEVER pruned (not ours to delete).
20
- // - Grep-able [bkit-harvest] log lines.
21
- // - Relies on the tolerant `parse` for half-written tails.
22
- //
23
- // Friction is moved, not copied: consumers start each cycle clean. Ordering is
24
- // copy -> fsync -> verify -> prune; a crash at any point re-copies idempotently on the
25
- // next run.
26
- //
27
- // Consumers are listed in `harvest/consumers.md` (one absolute path per line).
28
- // See DE-34 for the shared-entrypoint + crash-isolation contract.
29
- import * as fs from "node:fs";
30
- import * as path from "node:path";
31
- import { fold, parse, resolveRepoSlug, } from "../ledgers/telemetry.js";
32
- import { parseJson } from "../shared/json.js";
33
- // ---------------------------------------------------------------------------
34
- // Constants
35
- // ---------------------------------------------------------------------------
36
- /** Files to COPY from each consumer's `.bkit/` directory (content + dedup). */
37
- export const HARVEST_COPY_FILES = ["stats.jsonl", "friction.jsonl"];
38
- /**
39
- * Files PRUNED from the consumer after their rows are durably in the central store.
40
- * Must be a subset of HARVEST_COPY_FILES. `stats.jsonl` is absent: it is copied, never
41
- * moved.
42
- */
43
- export const HARVEST_PRUNE_FILES = ["friction.jsonl"];
44
- /** Files to COUNT-ONLY from each consumer's `docs/` directory (metadata only). */
45
- export const HARVEST_COUNT_FILES = ["deferrals.jsonl", "failures.jsonl"];
46
- /** Path (relative to bkit repo root) for the consumers list. */
47
- export const CONSUMERS_FILE = "harvest/consumers.md";
48
- /**
49
- * Destination directory in bkit for harvested data. Repo root, NOT under `.bkit/`
50
- * (bkit's managed/installed surface).
51
- */
52
- export const HARVEST_DEST_DIR = "harvest";
53
- // ---------------------------------------------------------------------------
54
- // Read consumer list from `harvest/consumers.md`
55
- // ---------------------------------------------------------------------------
56
- /**
57
- * Read the list of consumer repo root paths from `harvest/consumers.md`.
58
- * Returns an array of absolute paths (non-empty lines, comments stripped).
59
- */
60
- export function readConsumerPaths(bkitRoot) {
61
- const consumersFile = path.join(bkitRoot, CONSUMERS_FILE);
62
- if (!fs.existsSync(consumersFile)) {
63
- console.error(`[bkit-harvest] consumers file not found: ${consumersFile}`);
64
- return [];
65
- }
66
- const raw = fs.readFileSync(consumersFile, "utf-8");
67
- return raw
68
- .split("\n")
69
- .map((line) => line.trim())
70
- .filter((line) => line.length > 0 && !line.startsWith("#"));
71
- }
72
- // ---------------------------------------------------------------------------
73
- // Harvest a single consumer
74
- // ---------------------------------------------------------------------------
75
- /**
76
- * Harvest one consumer's `.bkit/{stats,friction}.jsonl` (COPY) and
77
- * `docs/{deferrals,failures}.jsonl` (COUNT-ONLY).
78
- * NEVER writes to the consumer repo.
79
- */
80
- export function harvestConsumer(consumerPath, bkitRoot) {
81
- const result = {
82
- consumerPath,
83
- copiedRows: 0,
84
- countOnlyRows: {},
85
- countOnlyRawRows: {},
86
- malformedLineCount: 0,
87
- skipped: false,
88
- prunedRows: {},
89
- };
90
- if (!fs.existsSync(consumerPath)) {
91
- result.skipped = true;
92
- result.skipReason = `consumer path not found: ${consumerPath}`;
93
- console.error(`[bkit-harvest] SKIP ${consumerPath} — path not found`);
94
- return result;
95
- }
96
- const destDir = path.join(bkitRoot, HARVEST_DEST_DIR);
97
- if (!fs.existsSync(destDir)) {
98
- fs.mkdirSync(destDir, { recursive: true });
99
- }
100
- // COPY: .bkit/{stats,friction}.jsonl
101
- for (const filename of HARVEST_COPY_FILES) {
102
- const srcPath = path.join(consumerPath, ".bkit", filename);
103
- if (!fs.existsSync(srcPath)) {
104
- console.error(`[bkit-harvest] ${consumerPath}/.bkit/${filename} — not found, skipping`);
105
- continue;
106
- }
107
- // Record mtime BEFORE reading (copy-only invariant verification support)
108
- const statBefore = fs.statSync(srcPath);
109
- const raw = fs.readFileSync(srcPath, "utf-8");
110
- const statAfter = fs.statSync(srcPath);
111
- // Verify consumer file was not modified by our read (should always hold)
112
- if (statBefore.mtimeMs !== statAfter.mtimeMs) {
113
- console.error(`[bkit-harvest] WARNING: ${srcPath} mtime changed during read — harvest continues but data may be inconsistent`);
114
- }
115
- const parsed = parse(raw);
116
- result.copiedRows += parsed.events.length;
117
- result.malformedLineCount += parsed.malformedLineCount;
118
- // Stamp repo on each event if missing
119
- const consumerSlug = consumerPathToSlug(consumerPath);
120
- const stamped = parsed.events.map((ev) => stampRepo(ev, consumerSlug));
121
- // Write to harvest dest (append)
122
- const destPath = path.join(destDir, filename);
123
- for (const ev of stamped) {
124
- fs.appendFileSync(destPath, JSON.stringify(ev) + "\n", "utf-8");
125
- }
126
- console.error(`[bkit-harvest] ${consumerPath}/.bkit/${filename} — copied ${parsed.events.length} rows (malformed: ${parsed.malformedLineCount})`);
127
- }
128
- // COUNT-ONLY: docs/{deferrals,failures}.jsonl
129
- for (const filename of HARVEST_COUNT_FILES) {
130
- const srcPath = path.join(consumerPath, "docs", filename);
131
- if (!fs.existsSync(srcPath)) {
132
- continue;
133
- }
134
- const raw = fs.readFileSync(srcPath, "utf-8");
135
- const parsed = parse(raw);
136
- // Count ENTITIES, not lines: updating a deferral appends a new same-key row, and
137
- // `fold` collapses same-key rows to the current state. See .bkit/jsonl.md § Updating
138
- // a Row.
139
- const folded = fold(parsed.events);
140
- result.countOnlyRows[filename] = folded.size;
141
- result.countOnlyRawRows[filename] = parsed.events.length;
142
- result.malformedLineCount += parsed.malformedLineCount;
143
- console.error(`[bkit-harvest] ${consumerPath}/docs/${filename} — count-only: ${folded.size} entities (${parsed.events.length} rows)`);
144
- }
145
- return result;
146
- }
147
- // ---------------------------------------------------------------------------
148
- // Prune — remove durably-stored rows from a consumer ledger
149
- // ---------------------------------------------------------------------------
150
- /**
151
- * Stamp `repo` on an event when the consumer wrote the row without one.
152
- * MUST be applied on both the store side and the prune side, or the two contents
153
- * diverge and a stored row is never recognised as prunable.
154
- */
155
- export function stampRepo(event, consumerSlug) {
156
- return {
157
- ...event,
158
- repo: event.repo && event.repo.length > 0 ? event.repo : consumerSlug,
159
- };
160
- }
161
- /**
162
- * Remove from `srcPath` exactly those rows whose CONTENT is durably in the store.
163
- *
164
- * Gated on byte-for-byte content, NOT on identity.
165
- *
166
- * Line-level, NOT event-level: a line that does not parse is kept verbatim.
167
- *
168
- * Re-reads the file at prune time, so rows appended by the consumer DURING the harvest
169
- * survive.
170
- *
171
- * Writes via temp-file + rename; an interrupted prune never leaves a torn ledger.
172
- */
173
- export function pruneConsumerFile(srcPath, consumerSlug, durable) {
174
- const raw = fs.readFileSync(srcPath, "utf-8");
175
- const kept = [];
176
- let removed = 0;
177
- for (const line of raw.split("\n")) {
178
- if (line.trim() === "")
179
- continue;
180
- const parsed = parseJson(line.trim());
181
- if (!parsed.ok) {
182
- kept.push(line); // malformed — never ours to delete
183
- continue;
184
- }
185
- const event = parsed.value;
186
- if (durable.rows.has(canonicalRow(stampRepo(event, consumerSlug)))) {
187
- removed++;
188
- continue;
189
- }
190
- kept.push(line);
191
- }
192
- if (removed === 0)
193
- return { removed: 0, kept: kept.length };
194
- const next = kept.length > 0 ? kept.join("\n") + "\n" : "";
195
- const tmpPath = `${srcPath}.bkit-harvest.tmp`;
196
- fs.writeFileSync(tmpPath, next, "utf-8");
197
- fs.renameSync(tmpPath, srcPath);
198
- return { removed, kept: kept.length };
199
- }
200
- /**
201
- * Canonical serialisation used to compare a consumer row against a stored row.
202
- * Safe as a plain `JSON.stringify`: the store was written from exactly this function's
203
- * output, and a JSON.parse -> JSON.stringify round-trip preserves key order, so the
204
- * store's own rewrite is byte-stable.
205
- */
206
- function canonicalRow(event) {
207
- return JSON.stringify(event);
208
- }
209
- /** fsync a file to disk, so a durability check is not just a page-cache read. */
210
- function fsyncFile(filePath) {
211
- const fd = fs.openSync(filePath, "r+");
212
- try {
213
- fs.fsyncSync(fd);
214
- }
215
- finally {
216
- fs.closeSync(fd);
217
- }
218
- }
219
- // ---------------------------------------------------------------------------
220
- // Run harvest across all consumers
221
- // ---------------------------------------------------------------------------
222
- /**
223
- * Run harvest across all consumers listed in `harvest/consumers.md`.
224
- * Deduplicates the store by row content after collecting all rows.
225
- */
226
- export function runHarvest(bkitRoot) {
227
- const consumerPaths = readConsumerPaths(bkitRoot);
228
- console.error(`[bkit-harvest] Found ${consumerPaths.length} consumer(s) in ${CONSUMERS_FILE}`);
229
- const consumerResults = [];
230
- let totalCopied = 0;
231
- const totalCountOnly = {};
232
- // The central store ACCUMULATES — it is NEVER cleared between runs. Re-copied rows
233
- // collapse via the content dedup below.
234
- const destDir = path.join(bkitRoot, HARVEST_DEST_DIR);
235
- for (const consumerPath of consumerPaths) {
236
- console.error(`[bkit-harvest] Harvesting: ${consumerPath}`);
237
- const res = harvestConsumer(consumerPath, bkitRoot);
238
- consumerResults.push(res);
239
- if (!res.skipped) {
240
- totalCopied += res.copiedRows;
241
- for (const [k, v] of Object.entries(res.countOnlyRows)) {
242
- totalCountOnly[k] = (totalCountOnly[k] ?? 0) + v;
243
- }
244
- }
245
- }
246
- // Deduplicate by content: drop exact re-copies, keep every distinct row in first-seen
247
- // order. Entity counts come from the fold, which is a read — never what is written.
248
- let mergedState = new Map();
249
- let dedupedRows = 0;
250
- /** Per-file durable content read BACK OFF DISK — the prune gate. */
251
- const durableByFile = {};
252
- for (const filename of HARVEST_COPY_FILES) {
253
- const destPath = path.join(destDir, filename);
254
- if (!fs.existsSync(destPath))
255
- continue;
256
- const raw = fs.readFileSync(destPath, "utf-8");
257
- const parsed = parse(raw);
258
- const folded = fold(parsed.events);
259
- mergedState = new Map([...mergedState, ...folded]);
260
- dedupedRows += folded.size;
261
- const distinctLines = [...new Set(parsed.events.map((ev) => canonicalRow(ev)))].join("\n");
262
- fs.writeFileSync(destPath, distinctLines + (distinctLines.length > 0 ? "\n" : ""), "utf-8");
263
- // Durability gate: fsync, then read the rows BACK OFF DISK; prune against those.
264
- fsyncFile(destPath);
265
- const verified = parse(fs.readFileSync(destPath, "utf-8"));
266
- durableByFile[filename] = {
267
- rows: new Set(verified.events.map((ev) => canonicalRow(ev))),
268
- };
269
- }
270
- // Prune ONLY now — every row below is confirmed byte-for-byte in the store on disk.
271
- let totalPruned = 0;
272
- let totalKept = 0;
273
- for (const res of consumerResults) {
274
- if (res.skipped)
275
- continue;
276
- const consumerSlug = consumerPathToSlug(res.consumerPath);
277
- for (const filename of HARVEST_PRUNE_FILES) {
278
- const srcPath = path.join(res.consumerPath, ".bkit", filename);
279
- if (!fs.existsSync(srcPath))
280
- continue;
281
- const durable = durableByFile[filename];
282
- if (!durable || durable.rows.size === 0)
283
- continue;
284
- const { removed, kept } = pruneConsumerFile(srcPath, consumerSlug, durable);
285
- if (removed > 0) {
286
- res.prunedRows[filename] = removed;
287
- totalPruned += removed;
288
- console.error(`[bkit-harvest] ${res.consumerPath}/.bkit/${filename} — pruned ${removed} harvested row(s)`);
289
- }
290
- if (kept > 0) {
291
- totalKept += kept;
292
- console.error(`[bkit-harvest] ${res.consumerPath}/.bkit/${filename} — kept ${kept} line(s) not in the store (malformed, or appended after the copy)`);
293
- }
294
- }
295
- }
296
- console.error(`[bkit-harvest] Done — total rows collected: ${totalCopied}, entities after fold: ${dedupedRows}, pruned from consumers: ${totalPruned}, kept in consumers: ${totalKept}`);
297
- if (totalPruned > 0) {
298
- console.error(`[bkit-harvest] ${totalPruned} row(s) now exist ONLY in ${HARVEST_DEST_DIR}/ (gitignored) — back it up to make them durable.`);
299
- }
300
- return {
301
- consumers: consumerResults,
302
- totalCopiedRows: totalCopied,
303
- totalCountOnly,
304
- dedupedRows,
305
- totalPrunedRows: totalPruned,
306
- mergedState,
307
- };
308
- }
309
- // ---------------------------------------------------------------------------
310
- // Helpers
311
- // ---------------------------------------------------------------------------
312
- /** Convert an absolute consumer path to a repo slug for stamping. */
313
- export function consumerPathToSlug(consumerPath) {
314
- // The consumer's OWN git remote — the only authoritative source.
315
- const slug = resolveRepoSlug(consumerPath);
316
- if (slug)
317
- return slug;
318
- // Last resort, and loud: a remote-less consumer still harvests.
319
- const parts = consumerPath
320
- .replace(/\/+$/, "")
321
- .split("/")
322
- .filter((p) => p.length > 0);
323
- const fallback = parts[parts.length - 1] ?? consumerPath;
324
- console.error(`[bkit-harvest] no git remote at ${consumerPath} — falling back to path-derived slug "${fallback}". ` +
325
- `This is NOT an owner/repo slug and will not join rows from the same repo harvested elsewhere.`);
326
- return fallback;
327
- }
328
- // harvest — cross-repo harvest CLI.
329
- // Copies consumer .bkit/stats.jsonl and MOVES .bkit/friction.jsonl into harvest/;
330
- // COUNT-ONLY for docs/{deferrals,failures}.jsonl. Friction rows are pruned from the
331
- // consumer only after they are fsync'd + read back from the store. Fail-open: exits 0.
332
- // See DE-34 for the shared-entrypoint + crash-isolation contract.
333
- export function runHarvestCommand() {
334
- try {
335
- const cwd = process.cwd();
336
- console.log("[bkit-harvest] Starting cross-repo harvest...");
337
- const result = runHarvest(cwd);
338
- console.log(`[bkit-harvest] Harvest complete — consumers: ${result.consumers.length}, rows copied: ${result.totalCopiedRows}, entities after fold: ${result.dedupedRows}, pruned from consumers: ${result.totalPrunedRows}`);
339
- const countOnlyEntries = Object.entries(result.totalCountOnly);
340
- if (countOnlyEntries.length > 0) {
341
- for (const [file, count] of countOnlyEntries) {
342
- console.log(`[bkit-harvest] count-only ${file}: ${count} rows (not copied)`);
343
- }
344
- }
345
- if (result.totalPrunedRows > 0) {
346
- console.log(`[bkit-harvest] ${result.totalPrunedRows} friction row(s) were REMOVED from consumers and now exist only in ${HARVEST_DEST_DIR}/ — commit it.`);
347
- }
348
- }
349
- catch (err) {
350
- const msg = err instanceof Error ? err.message : String(err);
351
- console.error(`[bkit-harvest] harvest error (fail-open): ${msg}`);
352
- }
353
- }