@cbartens/bkit 5.5.1 → 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 (288) 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 +35 -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 +19 -18
  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 +91 -167
  13. package/.bkit/core/.bkit/providers.md +29 -32
  14. package/.bkit/core/.bkit/templates.md +12 -11
  15. package/.bkit/core/.bkit/trackers.md +51 -28
  16. package/.bkit/core/.bkit/workflows.md +394 -355
  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 -29
  55. package/.bkit/core/engine/checks/after/github.ts +95 -17
  56. package/.bkit/core/engine/checks/after/index.ts +10 -7
  57. package/.bkit/core/engine/checks/after/suppression.ts +8 -5
  58. package/.bkit/core/engine/checks/after/tools.ts +46 -91
  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 +6 -6
  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 +407 -64
  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 +58 -8
  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 +185 -8
  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 +250 -3
  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 +157 -20
  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 +29 -15
  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 +32 -14
  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 +72 -18
  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 +29 -14
  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 +27 -12
  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 +41 -18
  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 +11 -11
  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 +16 -7
  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 +1 -1
  195. package/.bkit/templates/.github/workflows/bkit-cloud.yml +21 -19
  196. package/.bkit/templates/.husky/pre-push +4 -4
  197. package/.bkit/templates/.prettierignore +7 -0
  198. package/.bkit/templates/scripts/cleanup/bkit-disk-prune.mjs +1 -1
  199. package/CHANGELOG.md +112 -0
  200. package/README.md +50 -35
  201. package/dist/checks/after/ci-yml.js +39 -28
  202. package/dist/checks/after/github.js +91 -17
  203. package/dist/checks/after/index.js +11 -8
  204. package/dist/checks/after/suppression.js +8 -5
  205. package/dist/checks/after/tools.js +39 -82
  206. package/dist/checks/before/env.js +2 -2
  207. package/dist/checks/before/files.js +5 -3
  208. package/dist/checks/before/index.js +15 -2
  209. package/dist/checks/before/repo.js +163 -2
  210. package/dist/checks/tips/catalogue.js +14 -5
  211. package/dist/checks/tips/detect.js +32 -0
  212. package/dist/checks/tips/index.js +4 -4
  213. package/dist/cli.js +19 -47
  214. package/dist/commands/analysis.js +109 -100
  215. package/dist/commands/ci/ci.js +6 -6
  216. package/dist/commands/comply.js +54 -91
  217. package/dist/commands/install/doctor.js +2 -2
  218. package/dist/commands/install/install.js +50 -19
  219. package/dist/commands/install/uninstall.js +59 -28
  220. package/dist/commands/land.js +16 -12
  221. package/dist/commands/lib.js +51 -16
  222. package/dist/commands/registry.js +4 -0
  223. package/dist/commands/setup/setup.js +7 -7
  224. package/dist/commands/stats.js +136 -0
  225. package/dist/commands/tips.js +7 -3
  226. package/dist/commands/workflow/branches.js +656 -56
  227. package/dist/commands/workflow/challenge.js +556 -0
  228. package/dist/commands/workflow/context.js +709 -30
  229. package/dist/commands/workflow/counters.js +208 -75
  230. package/dist/commands/workflow/decisions.js +441 -0
  231. package/dist/commands/workflow/disk.js +2 -2
  232. package/dist/commands/workflow/fids.js +689 -33
  233. package/dist/commands/workflow/issues.js +343 -59
  234. package/dist/commands/workflow/ledgers.js +762 -27
  235. package/dist/commands/workflow/locks.js +24 -2
  236. package/dist/commands/workflow/plan-blocks.js +437 -0
  237. package/dist/commands/workflow/plan-versions.js +22 -0
  238. package/dist/commands/workflow/prs.js +142 -32
  239. package/dist/commands/workflow/repo.js +52 -8
  240. package/dist/commands/workflow/roadmap.js +9 -7
  241. package/dist/commands/workflow/run-facts.js +426 -0
  242. package/dist/commands/workflow/runs.js +144 -43
  243. package/dist/commands/workflow/runtime.js +12 -1
  244. package/dist/commands/workflow/status.js +130 -29
  245. package/dist/commands/workflow/step-table.js +276 -0
  246. package/dist/commands/workflow/steps.js +171 -80
  247. package/dist/commands/workflow/summary.js +275 -0
  248. package/dist/commands/workflow/tests.js +834 -136
  249. package/dist/commands/workflow/tracker.js +983 -0
  250. package/dist/commands/workflow/workflows.js +834 -195
  251. package/dist/commands/workflow/workstreams.js +11 -5
  252. package/dist/commands/workflow/worktrees.js +147 -9
  253. package/dist/install/manifest.js +11 -9
  254. package/dist/install/modules/cloud.js +22 -13
  255. package/dist/install/modules/core.js +247 -3
  256. package/dist/install/modules/opencode.js +2 -2
  257. package/dist/install/version-file.js +6 -2
  258. package/dist/ledgers/migrate.js +81 -0
  259. package/dist/ledgers/status.js +61 -8
  260. package/dist/ledgers/telemetry.js +905 -124
  261. package/dist/render/hosts.js +4 -0
  262. package/dist/render/models.js +12 -0
  263. package/dist/shared/branches.js +3 -1
  264. package/dist/shared/egress.js +7 -0
  265. package/dist/shared/json.js +20 -7
  266. package/dist/shared/settings.js +125 -17
  267. package/dist/shared/text.js +333 -0
  268. package/dist/tools/grammar-density.js +183 -0
  269. package/dist/tools/instructions-sources.js +721 -0
  270. package/dist/tools/instructions.js +1579 -0
  271. package/package.json +113 -24
  272. package/.bkit/core/engine/commands/harvest.ts +0 -459
  273. package/.bkit/core/engine/tools/wording.ts +0 -748
  274. package/.bkit/core/prompts/bkit-build-challenge-reconcile.md +0 -69
  275. package/.bkit/core/prompts/bkit-build-challenge-remediate.md +0 -71
  276. package/.bkit/core/prompts/bkit-build-challenge-tests.md +0 -74
  277. package/.bkit/core/prompts/bkit-build-summary.md +0 -94
  278. package/.bkit/core/prompts/bkit-debug-summary.md +0 -96
  279. package/.bkit/core/prompts/bkit-design-summary.md +0 -107
  280. package/.bkit/core/prompts/bkit-plan-complexity-check.md +0 -67
  281. package/.bkit/core/prompts/bkit-plan-complexity-split.md +0 -88
  282. package/.bkit/core/prompts/bkit-plan-review-3.md +0 -93
  283. package/.bkit/core/prompts/bkit-plan-scope-check-1.md +0 -85
  284. package/.bkit/core/prompts/bkit-plan-scope-check-2.md +0 -85
  285. package/.bkit/core/prompts/bkit-plan-summary.md +0 -107
  286. package/.bkit/core/prompts/bkit-ship-summary.md +0 -78
  287. package/dist/commands/harvest.js +0 -353
  288. package/dist/tools/wording.js +0 -680
@@ -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
- }