@cbartens/bkit 5.2.4 → 5.4.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 (271) hide show
  1. package/.bkit/README.md +27 -11
  2. package/.bkit/core/.agents/context.md +1 -1
  3. package/.bkit/core/.agents/docs.md +1 -1
  4. package/.bkit/core/.agents/plugins/bkit/hooks/bkit-guardrails.cjs +1 -1
  5. package/.bkit/core/.agents/rules.md +18 -1
  6. package/.bkit/core/.bkit/README.md +26 -10
  7. package/.bkit/core/.bkit/cloud.md +40 -39
  8. package/.bkit/core/.bkit/comparison.md +68 -345
  9. package/.bkit/core/.bkit/install.md +16 -9
  10. package/.bkit/core/.bkit/lite.md +73 -0
  11. package/.bkit/core/.bkit/pipeline.md +346 -0
  12. package/.bkit/core/.bkit/prompts.md +90 -74
  13. package/.bkit/core/.bkit/providers.md +42 -23
  14. package/.bkit/core/.bkit/templates.md +138 -19
  15. package/.bkit/core/.bkit/trackers.md +49 -4
  16. package/.bkit/core/.bkit/workflows.md +144 -123
  17. package/.bkit/core/.claude/hooks/bkit-guardrails.cjs +1 -1
  18. package/.bkit/core/.codex/hooks/bkit-guardrails.cjs +7 -4
  19. package/.bkit/core/.cursor/hooks/bkit-guardrails.cjs +8 -5
  20. package/.bkit/core/docs/templates/bkit-audit-accessibility.md +3 -2
  21. package/.bkit/core/docs/templates/bkit-audit-adoption.md +2 -2
  22. package/.bkit/core/docs/templates/bkit-audit-agents.md +1 -1
  23. package/.bkit/core/docs/templates/bkit-audit-aio-seo.md +1 -1
  24. package/.bkit/core/docs/templates/bkit-audit-analytics.md +1 -1
  25. package/.bkit/core/docs/templates/bkit-audit-annotations.md +1 -1
  26. package/.bkit/core/docs/templates/bkit-audit-app-performance.md +1 -1
  27. package/.bkit/core/docs/templates/bkit-audit-app-store.md +1 -1
  28. package/.bkit/core/docs/templates/bkit-audit-architecture.md +1 -1
  29. package/.bkit/core/docs/templates/bkit-audit-ci-performance.md +4 -2
  30. package/.bkit/core/docs/templates/bkit-audit-config.md +3 -2
  31. package/.bkit/core/docs/templates/bkit-audit-cost.md +3 -2
  32. package/.bkit/core/docs/templates/bkit-audit-coverage.md +1 -1
  33. package/.bkit/core/docs/templates/bkit-audit-database.md +3 -2
  34. package/.bkit/core/docs/templates/bkit-audit-dead-code.md +5 -5
  35. package/.bkit/core/docs/templates/bkit-audit-deferrals.md +1 -1
  36. package/.bkit/core/docs/templates/bkit-audit-dependencies.md +26 -21
  37. package/.bkit/core/docs/templates/bkit-audit-documentation.md +1 -1
  38. package/.bkit/core/docs/templates/bkit-audit-duplication.md +2 -2
  39. package/.bkit/core/docs/templates/bkit-audit-failures.md +1 -1
  40. package/.bkit/core/docs/templates/bkit-audit-flakes.md +3 -2
  41. package/.bkit/core/docs/templates/bkit-audit-gates.md +5 -2
  42. package/.bkit/core/docs/templates/bkit-audit-issues.md +3 -2
  43. package/.bkit/core/docs/templates/bkit-audit-llm.md +22 -15
  44. package/.bkit/core/docs/templates/bkit-audit-logs.md +10 -6
  45. package/.bkit/core/docs/templates/bkit-audit-models.md +1 -1
  46. package/.bkit/core/docs/templates/bkit-audit-privacy.md +1 -1
  47. package/.bkit/core/docs/templates/bkit-audit-prompts.md +7 -7
  48. package/.bkit/core/docs/templates/bkit-audit-red-team.md +12 -9
  49. package/.bkit/core/docs/templates/bkit-audit-reliability.md +3 -2
  50. package/.bkit/core/docs/templates/bkit-audit-repo.md +1 -1
  51. package/.bkit/core/docs/templates/bkit-audit-security.md +14 -12
  52. package/.bkit/core/docs/templates/bkit-audit-slop.md +1 -1
  53. package/.bkit/core/docs/templates/bkit-audit-testing.md +10 -8
  54. package/.bkit/core/docs/templates/bkit-audit-usability.md +1 -1
  55. package/.bkit/core/docs/templates/bkit-plan.md +8 -6
  56. package/.bkit/core/engine/checks/after/auth.ts +9 -9
  57. package/.bkit/core/engine/checks/after/ci-yml.ts +14 -12
  58. package/.bkit/core/engine/checks/after/github.ts +29 -120
  59. package/.bkit/core/engine/checks/after/index.ts +2 -2
  60. package/.bkit/core/engine/checks/after/managed.ts +8 -28
  61. package/.bkit/core/engine/checks/after/tools.ts +3 -15
  62. package/.bkit/core/engine/checks/before/context-fetch.ts +1 -21
  63. package/.bkit/core/engine/checks/before/index.ts +7 -43
  64. package/.bkit/core/engine/checks/before/mcp.ts +17 -8
  65. package/.bkit/core/engine/checks/before/repo.ts +9 -12
  66. package/.bkit/core/engine/checks/status-holes.ts +4 -6
  67. package/.bkit/core/engine/checks/tips/catalogue.ts +76 -24
  68. package/.bkit/core/engine/checks/tips/detect.ts +45 -2
  69. package/.bkit/core/engine/checks/tips/index.ts +77 -26
  70. package/.bkit/core/engine/cli.ts +57 -27
  71. package/.bkit/core/engine/commands/analysis.ts +36 -26
  72. package/.bkit/core/engine/commands/comply.ts +42 -32
  73. package/.bkit/core/engine/commands/harvest.ts +12 -12
  74. package/.bkit/core/engine/commands/install/doctor.ts +57 -28
  75. package/.bkit/core/engine/commands/install/install.ts +73 -130
  76. package/.bkit/core/engine/commands/install/uninstall.ts +191 -33
  77. package/.bkit/core/engine/commands/install/upgrade.ts +49 -43
  78. package/.bkit/core/engine/commands/land.ts +156 -0
  79. package/.bkit/core/engine/commands/lib.ts +45 -18
  80. package/.bkit/core/engine/commands/registry.ts +2 -0
  81. package/.bkit/core/engine/commands/tips.ts +12 -5
  82. package/.bkit/core/engine/commands/workflow/branches.ts +386 -286
  83. package/.bkit/core/engine/commands/workflow/counters.ts +21 -12
  84. package/.bkit/core/engine/commands/workflow/disk.ts +556 -0
  85. package/.bkit/core/engine/commands/workflow/fids.ts +46 -0
  86. package/.bkit/core/engine/commands/workflow/issues.ts +73 -28
  87. package/.bkit/core/engine/commands/workflow/ledgers.ts +3 -1
  88. package/.bkit/core/engine/commands/workflow/locks.ts +38 -152
  89. package/.bkit/core/engine/commands/workflow/prs.ts +15 -4
  90. package/.bkit/core/engine/commands/workflow/repo.ts +2 -2
  91. package/.bkit/core/engine/commands/workflow/roadmap.ts +49 -53
  92. package/.bkit/core/engine/commands/workflow/runs.ts +8 -7
  93. package/.bkit/core/engine/commands/workflow/runtime.ts +3 -2
  94. package/.bkit/core/engine/commands/workflow/status.ts +33 -11
  95. package/.bkit/core/engine/commands/workflow/steps.ts +25 -15
  96. package/.bkit/core/engine/commands/workflow/tests.ts +17 -5
  97. package/.bkit/core/engine/commands/workflow/workflows.ts +436 -267
  98. package/.bkit/core/engine/commands/workflow/workstreams.ts +4 -2
  99. package/.bkit/core/engine/commands/workflow/worktrees.ts +24 -6
  100. package/.bkit/core/engine/install/cloud-env.ts +10 -31
  101. package/.bkit/core/engine/install/manifest.ts +14 -7
  102. package/.bkit/core/engine/install/modules/antigravity.ts +15 -26
  103. package/.bkit/core/engine/install/modules/claude.ts +20 -28
  104. package/.bkit/core/engine/install/modules/cloud.ts +5 -2
  105. package/.bkit/core/engine/install/modules/codex.ts +15 -29
  106. package/.bkit/core/engine/install/modules/core.ts +106 -70
  107. package/.bkit/core/engine/install/modules/cursor.ts +6 -20
  108. package/.bkit/core/engine/install/modules/index.ts +7 -1
  109. package/.bkit/core/engine/install/modules/opencode.ts +48 -70
  110. package/.bkit/core/engine/install/network-allowlist.ts +73 -0
  111. package/.bkit/core/engine/install/placeholders.ts +16 -35
  112. package/.bkit/core/engine/install/write.ts +59 -12
  113. package/.bkit/core/engine/ledgers/telemetry.ts +6 -4
  114. package/.bkit/core/engine/render/hosts.ts +47 -67
  115. package/.bkit/core/engine/render/models.ts +29 -11
  116. package/.bkit/core/engine/shared/branches.ts +13 -0
  117. package/.bkit/core/engine/shared/egress.ts +3 -3
  118. package/.bkit/core/engine/shared/json.ts +18 -1
  119. package/.bkit/core/engine/shared/package-json.ts +63 -0
  120. package/.bkit/core/engine/shared/package-roots.ts +0 -10
  121. package/.bkit/core/engine/shared/protected-paths.ts +5 -130
  122. package/.bkit/core/engine/shared/settings.ts +44 -0
  123. package/.bkit/core/engine/shared/timeouts.ts +8 -7
  124. package/.bkit/core/hooks/antigravity-hook.js +1 -0
  125. package/.bkit/core/hooks/guardrails-shell.js +186 -40
  126. package/.bkit/core/hooks/guardrails-tiers.js +5 -78
  127. package/.bkit/core/prompts/bkit-audit-generate.md +103 -7
  128. package/.bkit/core/prompts/bkit-build-challenge-1.md +1 -1
  129. package/.bkit/core/prompts/bkit-build-challenge-2.md +1 -1
  130. package/.bkit/core/prompts/bkit-build-challenge-reconcile.md +1 -1
  131. package/.bkit/core/prompts/bkit-build-challenge-remediate.md +1 -1
  132. package/.bkit/core/prompts/bkit-build-challenge-tests.md +1 -1
  133. package/.bkit/core/prompts/bkit-build-install-skills.md +1 -1
  134. package/.bkit/core/prompts/bkit-build-plan-current.md +1 -1
  135. package/.bkit/core/prompts/bkit-build-plan-execute.md +4 -4
  136. package/.bkit/core/prompts/bkit-build-plan-gaps-remediate.md +1 -1
  137. package/.bkit/core/prompts/bkit-build-plan-verify.md +2 -2
  138. package/.bkit/core/prompts/bkit-debug-abort-handover.md +1 -1
  139. package/.bkit/core/prompts/bkit-debug-failure-diagnose.md +1 -1
  140. package/.bkit/core/prompts/bkit-debug-failure-remediate.md +1 -1
  141. package/.bkit/core/prompts/bkit-debug-failure-reproduce.md +78 -0
  142. package/.bkit/core/prompts/bkit-debug-failure-soak.md +1 -1
  143. package/.bkit/core/prompts/bkit-debug-summary.md +3 -3
  144. package/.bkit/core/prompts/bkit-design-spike.md +1 -1
  145. package/.bkit/core/prompts/bkit-design-summary.md +2 -2
  146. package/.bkit/core/prompts/bkit-design-update.md +1 -1
  147. package/.bkit/core/prompts/bkit-plan-complexity-check.md +1 -1
  148. package/.bkit/core/prompts/bkit-plan-generate.md +1 -1
  149. package/.bkit/core/prompts/bkit-plan-scope-check-1.md +1 -1
  150. package/.bkit/core/prompts/bkit-plan-scope-check-2.md +1 -1
  151. package/.bkit/core/prompts/bkit-plan-summary.md +1 -1
  152. package/.bkit/core/prompts/bkit-plan-update.md +1 -1
  153. package/.bkit/core/prompts/bkit-ship-conflict-resolve.md +1 -1
  154. package/.bkit/core/prompts/bkit-ship-deferred-sweep.md +1 -1
  155. package/.bkit/core/prompts/bkit-ship-update-docs.md +1 -1
  156. package/.bkit/core/prompts/bkit-utility-update-decisions.md +9 -2
  157. package/.bkit/core/prompts/bkit-utility-update-deferrals.md +9 -2
  158. package/.bkit/core/prompts/bkit-utility-update-documentation.md +2 -1
  159. package/.bkit/core/prompts/bkit-utility-update-failures.md +9 -2
  160. package/.bkit/core/prompts/bkit-utility-update-knowledge.md +9 -2
  161. package/.bkit/core/prompts/bkit-utility-update-roadmap.md +11 -4
  162. package/.bkit/core/workflows/bkit-auto.md +2 -2
  163. package/.bkit/core/workflows/bkit-build.md +10 -10
  164. package/.bkit/core/workflows/bkit-debug.md +80 -44
  165. package/.bkit/core/workflows/bkit-design.md +22 -13
  166. package/.bkit/core/workflows/bkit-plan.md +33 -16
  167. package/.bkit/core/workflows/bkit-ship.md +87 -98
  168. package/.bkit/shared/.claude/bkit-cloud.md +58 -25
  169. package/.bkit/shared/.claude/settings.json +19 -0
  170. package/.bkit/shared/.cursor/mcp.json +2 -1
  171. package/.bkit/shared/.opencode/opencode.json +19 -1
  172. package/.bkit/shared/docs/deployments.md +49 -29
  173. package/.bkit/shared/docs/security.md +8 -6
  174. package/.bkit/shared/docs/testing.md +9 -5
  175. package/.bkit/shared/gitignore +1 -2
  176. package/.bkit/templates/.cursor/Dockerfile +30 -16
  177. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-bug.yml +78 -0
  178. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-change.yml +91 -0
  179. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-idea.yml +56 -0
  180. package/.bkit/templates/.github/renovate.json5 +91 -12
  181. package/.bkit/templates/.github/workflows/bkit-ci.yml +73 -55
  182. package/.bkit/templates/.github/workflows/bkit-cloud.yml +3 -1
  183. package/.bkit/templates/.github/workflows/bkit-renovate.yml +8 -1
  184. package/.bkit/templates/.github/workflows/bkit-trivy.yml +67 -0
  185. package/.bkit/templates/.husky/pre-commit +6 -0
  186. package/.bkit/templates/.husky/pre-push +15 -3
  187. package/.bkit/templates/.lintstagedrc.json +5 -1
  188. package/.bkit/templates/.oxlintrc.json +21 -0
  189. package/.bkit/templates/docs/capabilities.md +1 -1
  190. package/.bkit/templates/scripts/ci/check-capstone.mjs +176 -0
  191. package/.bkit/templates/scripts/ci/check-capstone.test.mjs +311 -0
  192. package/.bkit/templates/scripts/cleanup/bkit-disk-prune.machine.mjs +197 -0
  193. package/.bkit/templates/scripts/cleanup/bkit-disk-prune.mjs +164 -0
  194. package/.bkit/templates/scripts/graphify/bkit-graph-eager.py +2 -2
  195. package/.bkit/templates/scripts/graphify/bkit-graph-imports.py +43 -20
  196. package/.bkit/templates/scripts/graphify/bkit-graph-pre-push.sh +33 -11
  197. package/.bkit/templates/scripts/graphify/bkit-graph-repair.py +3 -2
  198. package/.bkit/templates/scripts/graphify/bkit-graphify-rollup.py +5 -5
  199. package/.bkit/templates/scripts/graphify/bkit-graphify.sh +79 -19
  200. package/CHANGELOG.md +98 -3
  201. package/README.md +252 -122
  202. package/dist/checks/after/auth.js +10 -11
  203. package/dist/checks/after/ci-yml.js +11 -14
  204. package/dist/checks/after/github.js +32 -125
  205. package/dist/checks/after/index.js +3 -2
  206. package/dist/checks/after/managed.js +8 -24
  207. package/dist/checks/after/tools.js +3 -16
  208. package/dist/checks/before/context-fetch.js +1 -21
  209. package/dist/checks/before/index.js +6 -42
  210. package/dist/checks/before/mcp.js +14 -9
  211. package/dist/checks/before/repo.js +8 -11
  212. package/dist/checks/status-holes.js +5 -7
  213. package/dist/checks/tips/catalogue.js +65 -18
  214. package/dist/checks/tips/detect.js +41 -2
  215. package/dist/checks/tips/index.js +61 -25
  216. package/dist/cli.js +52 -26
  217. package/dist/commands/analysis.js +21 -5
  218. package/dist/commands/comply.js +40 -31
  219. package/dist/commands/harvest.js +12 -13
  220. package/dist/commands/install/doctor.js +55 -23
  221. package/dist/commands/install/install.js +72 -122
  222. package/dist/commands/install/uninstall.js +165 -31
  223. package/dist/commands/install/upgrade.js +46 -39
  224. package/dist/commands/land.js +129 -0
  225. package/dist/commands/lib.js +41 -16
  226. package/dist/commands/registry.js +2 -0
  227. package/dist/commands/tips.js +10 -3
  228. package/dist/commands/workflow/branches.js +303 -248
  229. package/dist/commands/workflow/counters.js +17 -7
  230. package/dist/commands/workflow/disk.js +452 -0
  231. package/dist/commands/workflow/fids.js +39 -0
  232. package/dist/commands/workflow/issues.js +63 -27
  233. package/dist/commands/workflow/ledgers.js +3 -1
  234. package/dist/commands/workflow/locks.js +35 -130
  235. package/dist/commands/workflow/prs.js +8 -5
  236. package/dist/commands/workflow/repo.js +2 -2
  237. package/dist/commands/workflow/roadmap.js +50 -57
  238. package/dist/commands/workflow/runs.js +5 -5
  239. package/dist/commands/workflow/runtime.js +3 -2
  240. package/dist/commands/workflow/status.js +32 -12
  241. package/dist/commands/workflow/steps.js +19 -11
  242. package/dist/commands/workflow/tests.js +16 -3
  243. package/dist/commands/workflow/workflows.js +390 -249
  244. package/dist/commands/workflow/workstreams.js +4 -2
  245. package/dist/commands/workflow/worktrees.js +15 -6
  246. package/dist/install/cloud-env.js +10 -23
  247. package/dist/install/manifest.js +14 -7
  248. package/dist/install/modules/antigravity.js +15 -26
  249. package/dist/install/modules/claude.js +17 -21
  250. package/dist/install/modules/cloud.js +6 -2
  251. package/dist/install/modules/codex.js +15 -29
  252. package/dist/install/modules/core.js +95 -63
  253. package/dist/install/modules/cursor.js +6 -9
  254. package/dist/install/modules/index.js +6 -1
  255. package/dist/install/modules/opencode.js +41 -72
  256. package/dist/install/network-allowlist.js +62 -0
  257. package/dist/install/placeholders.js +16 -35
  258. package/dist/install/write.js +55 -15
  259. package/dist/ledgers/telemetry.js +5 -4
  260. package/dist/render/hosts.js +30 -55
  261. package/dist/render/models.js +20 -9
  262. package/dist/shared/branches.js +11 -0
  263. package/dist/shared/egress.js +0 -2
  264. package/dist/shared/json.js +14 -0
  265. package/dist/shared/package-json.js +61 -0
  266. package/dist/shared/package-roots.js +0 -8
  267. package/dist/shared/protected-paths.js +5 -122
  268. package/dist/shared/settings.js +23 -1
  269. package/dist/shared/timeouts.js +8 -7
  270. package/package.json +21 -17
  271. package/.bkit/core/.agents/shortcuts.md +0 -73
@@ -1,41 +1,22 @@
1
1
  import * as fs from "node:fs";
2
2
  import * as path from "node:path";
3
- import { execSync } from "node:child_process";
4
- import { getPkgRoot } from "../shared/package-roots.js";
3
+ import { spawnSync } from "node:child_process";
4
+ import { readInstallingVersion } from "../shared/package-json.js";
5
5
  import { managedVersionMarker } from "./version-file.js";
6
6
  import { gitEnv, resolveRepoSlug } from "../ledgers/telemetry.js";
7
- // Read the INSTALLING bkit version from the package root (NOT the consumer's
8
- // package.json — that is a different version). Mirrors readInstallingVersion
9
- // (install/modules/index.ts) / readPkgVersion (checks/after/index.ts); kept local to avoid a
10
- // tokens → modules import cycle. "0.0.0" on any read failure.
11
- function readInstallingBkitVersion() {
12
- try {
13
- const pkg = JSON.parse(fs.readFileSync(path.join(getPkgRoot(), "package.json"), "utf-8"));
14
- return pkg.version ?? "0.0.0";
15
- }
16
- catch {
17
- return "0.0.0";
18
- }
19
- }
20
- // Resolve DEFAULT_BRANCH: git symbolic-ref refs/remotes/origin/HEAD → "main"
7
+ // Resolve DEFAULT_BRANCH, the production branch: refs/remotes/origin/HEAD, else "main".
8
+ // `dev` is bkit's integration branch, never production, so a remote whose default is `dev`
9
+ // also gets "main" — and renders the same bytes as a checkout with no origin/HEAD.
21
10
  function resolveDefaultBranch(cwd) {
22
- try {
23
- const raw = execSync("git symbolic-ref refs/remotes/origin/HEAD", {
24
- cwd,
25
- encoding: "utf-8",
26
- stdio: ["ignore", "pipe", "ignore"],
27
- env: gitEnv(),
28
- }).trim();
29
- // raw is like "refs/remotes/origin/main"
30
- const parts = raw.split("/");
31
- const branch = parts[parts.length - 1];
32
- if (branch)
33
- return branch;
34
- }
35
- catch {
36
- // not a git repo or no remote HEAD configured — fall back
37
- }
38
- return "main";
11
+ const res = spawnSync("git", ["symbolic-ref", "refs/remotes/origin/HEAD"], {
12
+ cwd,
13
+ encoding: "utf-8",
14
+ stdio: ["ignore", "pipe", "ignore"],
15
+ env: gitEnv(),
16
+ });
17
+ // stdout is like "refs/remotes/origin/main"; empty when not a git repo or no remote HEAD.
18
+ const branch = res.status === 0 ? res.stdout.trim().replace(/^refs\/remotes\/origin\//, "") : "";
19
+ return branch === "" || branch === "dev" ? "main" : branch;
39
20
  }
40
21
  // Read and parse package.json from cwd. Throws if absent.
41
22
  function readPackageJson(cwd) {
@@ -58,7 +39,7 @@ export async function resolveTokens(cwd) {
58
39
  DEFAULT_BRANCH: resolveDefaultBranch(cwd),
59
40
  PROJECT_NAME: name.trim(),
60
41
  REPO_SLUG: resolveRepoSlug(cwd),
61
- BKIT_VERSION: readInstallingBkitVersion(),
42
+ BKIT_VERSION: readInstallingVersion(),
62
43
  };
63
44
  }
64
45
  // Substitute {{TOKEN}} placeholders in content string.
@@ -78,7 +59,7 @@ export function applyTokens(content, tokens) {
78
59
  // so `# {{BKIT_MANAGED_MARKER}}` yields the hash form. The version is read from the
79
60
  // package only when the token is present and the TokenMap omits BKIT_VERSION.
80
61
  if (out.includes("{{BKIT_MANAGED_MARKER}}")) {
81
- const version = tokens.BKIT_VERSION ?? readInstallingBkitVersion();
62
+ const version = tokens.BKIT_VERSION ?? readInstallingVersion();
82
63
  out = out
83
64
  .replace(/^\/\/ \{\{BKIT_MANAGED_MARKER\}\}/gm, () => managedVersionMarker(version, "slash"))
84
65
  .replace(/\{\{BKIT_MANAGED_MARKER\}\}/g, () => managedVersionMarker(version, "html"));
@@ -2,6 +2,7 @@ import * as fs from "node:fs";
2
2
  import * as path from "node:path";
3
3
  import { updateManagedBlock, extractFullManagedBlock, extractManagedBlock, findLineAnchoredMarkers, findUnmarkedCopy, selectMarkers, } from "./managed-block.js";
4
4
  import { readTemplate } from "../render/parts.js";
5
+ import { parseJson } from "../shared/json.js";
5
6
  import { applyTokens } from "./placeholders.js";
6
7
  import { sha256 } from "./version-file.js";
7
8
  export function deepContains(template, consumer, pathPrefix = "") {
@@ -135,6 +136,51 @@ export function deepMergeJson(base, override) {
135
136
  return result;
136
137
  }
137
138
  // ---------------------------------------------------------------------------
139
+ // MCP tracker entry — bkit owns the template's parts of the entry, not the whole entry (#613)
140
+ //
141
+ // - every key the template sets is reset to the template's value
142
+ // - `headers["X-MCP-Toolsets"]` keeps the consumer's list and appends any template toolset
143
+ // it lacks, so a consumer can enable extra toolsets and a toolset bkit adds still lands
144
+ // - every other consumer key, header and toolset is kept
145
+ // ---------------------------------------------------------------------------
146
+ export const MCP_TOOLSETS_HEADER = "X-MCP-Toolsets";
147
+ /** Split an `X-MCP-Toolsets` value into toolset names (trimmed, empties dropped). */
148
+ export function parseToolsets(value) {
149
+ return value
150
+ .split(",")
151
+ .map((s) => s.trim())
152
+ .filter((s) => s !== "");
153
+ }
154
+ /** True for a JSON object (not null, not an array). */
155
+ export function isJsonObject(v) {
156
+ return v !== null && typeof v === "object" && !Array.isArray(v);
157
+ }
158
+ /** Merge bkit's tracker entry `owned` into the consumer's `existing` entry (rules above). */
159
+ export function mergeTrackerEntry(existing, owned) {
160
+ if (!isJsonObject(existing) || !isJsonObject(owned))
161
+ return owned;
162
+ const merged = deepMergeJson(existing, owned);
163
+ const ownedHeaders = owned.headers;
164
+ const existingHeaders = existing.headers;
165
+ if (isJsonObject(ownedHeaders) && isJsonObject(existingHeaders)) {
166
+ const ownedSets = ownedHeaders[MCP_TOOLSETS_HEADER];
167
+ const existingSets = existingHeaders[MCP_TOOLSETS_HEADER];
168
+ if (typeof ownedSets === "string" && typeof existingSets === "string") {
169
+ const have = parseToolsets(existingSets);
170
+ const missing = parseToolsets(ownedSets).filter((t) => !have.includes(t));
171
+ merged.headers[MCP_TOOLSETS_HEADER] =
172
+ missing.length === 0 ? existingSets : [...have, ...missing].join(",");
173
+ }
174
+ }
175
+ return merged;
176
+ }
177
+ /** True when `existing` already holds everything `mergeTrackerEntry` would write. */
178
+ export function trackerEntryInSync(existing, owned) {
179
+ if (existing === undefined)
180
+ return false;
181
+ return deepContains(mergeTrackerEntry(existing, owned), existing).ok;
182
+ }
183
+ // ---------------------------------------------------------------------------
138
184
  // writeTier2JsonContained — deep-containment JSON helper (BAR-167 Phase 1)
139
185
  //
140
186
  // Contract:
@@ -147,13 +193,11 @@ export function deepMergeJson(base, override) {
147
193
  // ---------------------------------------------------------------------------
148
194
  export function writeTier2JsonContained(srcPath, dstPath, dryRun, ws) {
149
195
  const srcRaw = fs.readFileSync(srcPath, "utf-8");
150
- let template;
151
- try {
152
- template = JSON.parse(srcRaw);
153
- }
154
- catch {
196
+ const templateRead = parseJson(srcRaw);
197
+ if (!templateRead.ok) {
155
198
  throw new Error(`writeTier2JsonContained: template at ${srcPath} is not valid JSON`);
156
199
  }
200
+ const template = templateRead.value;
157
201
  if (!fs.existsSync(dstPath)) {
158
202
  // First install — write template as-is
159
203
  ensureDir(path.dirname(dstPath));
@@ -167,14 +211,12 @@ export function writeTier2JsonContained(srcPath, dstPath, dryRun, ws) {
167
211
  }
168
212
  // dest exists — read and check containment
169
213
  const dstRaw = fs.readFileSync(dstPath, "utf-8");
170
- let existing;
171
- try {
172
- existing = JSON.parse(dstRaw);
173
- }
174
- catch {
214
+ const existingRead = parseJson(dstRaw);
215
+ if (!existingRead.ok) {
175
216
  // Malformed destination — refuse to overwrite; require manual fix (matches malformed-package-json blocker discipline)
176
217
  throw new Error(`[bkit] ${dstPath} is malformed JSON — fix manually before running bkit install`);
177
218
  }
219
+ const existing = existingRead.value;
178
220
  // Check if existing already contains the template
179
221
  const containsResult = deepContains(template, existing);
180
222
  if (containsResult.ok) {
@@ -364,13 +406,11 @@ export function ensurePackageJsonScripts(scripts, dryRun, ws) {
364
406
  return { added: [], preserved: [], missingPkg: true, malformed: false };
365
407
  }
366
408
  const raw = fs.readFileSync(pkgPath, "utf-8");
367
- let pkg;
368
- try {
369
- pkg = JSON.parse(raw);
370
- }
371
- catch {
409
+ const parsed = parseJson(raw);
410
+ if (!parsed.ok) {
372
411
  return { added: [], preserved: [], missingPkg: false, malformed: true };
373
412
  }
413
+ const pkg = parsed.value;
374
414
  let pkgScripts = pkg.scripts ?? undefined;
375
415
  const added = [];
376
416
  const preserved = [];
@@ -18,6 +18,7 @@
18
18
  // so an unexpected version surfaces for triage.
19
19
  import { spawnSync } from "node:child_process";
20
20
  import * as fs from "node:fs";
21
+ import { parseJson } from "../shared/json.js";
21
22
  // ---------------------------------------------------------------------------
22
23
  // Constants
23
24
  // ---------------------------------------------------------------------------
@@ -397,11 +398,11 @@ export function parse(raw) {
397
398
  const trimmed = line.trim();
398
399
  if (trimmed === "")
399
400
  continue;
400
- try {
401
- const obj = JSON.parse(trimmed);
402
- result.events.push(obj);
401
+ const obj = parseJson(trimmed);
402
+ if (obj.ok) {
403
+ result.events.push(obj.value);
403
404
  }
404
- catch {
405
+ else {
405
406
  result.malformedLineCount++;
406
407
  }
407
408
  }
@@ -103,22 +103,10 @@ export function antigravityToolsBlock(toolsBlock) {
103
103
  * lookup missing for name.
104
104
  */
105
105
  export function applyOpencodeAgent(content, opencodeModelLookup, effortMap, provider) {
106
- const fmMatch = content.match(/^(---\r?\n)([\s\S]*?)(\r?\n---\r?\n?)/);
107
- if (!fmMatch)
106
+ const subagent = requireSubagentModel(content, opencodeModelLookup, "opencode");
107
+ if (!subagent)
108
108
  return content;
109
- const fmBlock = fmMatch[2];
110
- const afterFm = content.slice(fmMatch[0].length);
111
- const parsed = parseFrontmatter(fmBlock);
112
- const name = parsed.get("name") ?? "";
113
- if (!name) {
114
- console.error(`${LOG_PREFIX} Subagent frontmatter missing 'name' field`);
115
- process.exit(1);
116
- }
117
- const opencodeModel = opencodeModelLookup.get(name);
118
- if (!opencodeModel) {
119
- console.error(`${LOG_PREFIX} Missing opencode model lookup for ${name}`);
120
- process.exit(1);
121
- }
109
+ const { fmBlock, afterFm, parsed, name, model: opencodeModel } = subagent;
122
110
  const description = parsed.get("description") ?? "";
123
111
  const effort = parsed.get("effort");
124
112
  const toolsBlock = extractFrontmatterField(fmBlock, "tools");
@@ -201,22 +189,11 @@ function tomlString(key, value) {
201
189
  * host (documented in providers.md).
202
190
  */
203
191
  export function applyCodexAgent(content, codexModelLookup, effortMap) {
204
- const fmMatch = content.match(/^(---\r?\n)([\s\S]*?)(\r?\n---\r?\n?)/);
205
- if (!fmMatch)
192
+ const subagent = requireSubagentModel(content, codexModelLookup, "codex");
193
+ if (!subagent)
206
194
  return content;
207
- const fmBlock = fmMatch[2];
208
- const body = content.slice(fmMatch[0].length).trim();
209
- const parsed = parseFrontmatter(fmBlock);
210
- const name = parsed.get("name") ?? "";
211
- if (!name) {
212
- console.error(`${LOG_PREFIX} Subagent frontmatter missing 'name' field`);
213
- process.exit(1);
214
- }
215
- const model = codexModelLookup.get(name);
216
- if (!model) {
217
- console.error(`${LOG_PREFIX} Missing codex model lookup for ${name}`);
218
- process.exit(1);
219
- }
195
+ const { afterFm, parsed, name, model } = subagent;
196
+ const body = afterFm.trim();
220
197
  const effort = parsed.get("effort");
221
198
  let out = "";
222
199
  out += tomlString("name", name);
@@ -347,27 +324,37 @@ function extractFrontmatterField(fmBlock, field) {
347
324
  const m = fmBlock.match(re);
348
325
  return m ? m[1] : null;
349
326
  }
350
- // T3: Cursor subagent frontmatter rewrite.
351
- // Emits: name, description, model, tools (passthrough — Cursor 2.4+ accepts identical tool names per BAR-113),
352
- // effort (from prompts.md Cursor column). Drops: permissionMode, maxTurns, readonly, is_background.
353
- // Hard-fails with non-zero exit if model lookup missing for name.
354
- export function applyT3(content, modelLookup, effortMap) {
327
+ // Shared opening of the subagent renderers: split off the frontmatter, require `name`, look
328
+ // the model up. Returns null when `content` has no frontmatter (the renderer passes it
329
+ // through); exits 1 when `name` or the model is missing. `host` qualifies the model-lookup
330
+ // message ("" for the unqualified Cursor message).
331
+ function requireSubagentModel(content, lookup, host) {
355
332
  const fmMatch = content.match(/^(---\r?\n)([\s\S]*?)(\r?\n---\r?\n?)/);
356
333
  if (!fmMatch)
357
- return content;
334
+ return null;
358
335
  const fmBlock = fmMatch[2];
359
- const afterFm = content.slice(fmMatch[0].length);
360
336
  const parsed = parseFrontmatter(fmBlock);
361
337
  const name = parsed.get("name") ?? "";
362
338
  if (!name) {
363
339
  console.error(`${LOG_PREFIX} Subagent frontmatter missing 'name' field`);
364
340
  process.exit(1);
365
341
  }
366
- const cursorModel = modelLookup.get(name);
367
- if (!cursorModel) {
368
- console.error(`${LOG_PREFIX} Missing model lookup for ${name}`);
342
+ const model = lookup.get(name);
343
+ if (!model) {
344
+ console.error(`${LOG_PREFIX} Missing ${host ? `${host} ` : ""}model lookup for ${name}`);
369
345
  process.exit(1);
370
346
  }
347
+ return { fmBlock, afterFm: content.slice(fmMatch[0].length), parsed, name, model };
348
+ }
349
+ // T3: Cursor subagent frontmatter rewrite.
350
+ // Emits: name, description, model, tools (passthrough — Cursor 2.4+ accepts identical tool names per BAR-113),
351
+ // effort (from prompts.md Cursor column). Drops: permissionMode, maxTurns, readonly, is_background.
352
+ // Hard-fails with non-zero exit if model lookup missing for name.
353
+ export function applyT3(content, modelLookup, effortMap) {
354
+ const subagent = requireSubagentModel(content, modelLookup, "");
355
+ if (!subagent)
356
+ return content;
357
+ const { fmBlock, afterFm, parsed, name, model: cursorModel } = subagent;
371
358
  const description = parsed.get("description") ?? "";
372
359
  const effort = parsed.get("effort");
373
360
  const toolsBlock = extractFrontmatterField(fmBlock, "tools");
@@ -427,22 +414,10 @@ export function applyAntigravityToolRewrites(content, agentsDir = ANTIGRAVITY_AG
427
414
  * readonly) are dropped. Hard-fails with non-zero exit if the model lookup is missing.
428
415
  */
429
416
  export function applyAntigravityPrompt(content, modelLookup, effortMap) {
430
- const fmMatch = content.match(/^(---\r?\n)([\s\S]*?)(\r?\n---\r?\n?)/);
431
- if (!fmMatch)
417
+ const subagent = requireSubagentModel(content, modelLookup, "antigravity");
418
+ if (!subagent)
432
419
  return content;
433
- const fmBlock = fmMatch[2];
434
- const afterFm = content.slice(fmMatch[0].length);
435
- const parsed = parseFrontmatter(fmBlock);
436
- const name = parsed.get("name") ?? "";
437
- if (!name) {
438
- console.error(`${LOG_PREFIX} Subagent frontmatter missing 'name' field`);
439
- process.exit(1);
440
- }
441
- const model = modelLookup.get(name);
442
- if (!model) {
443
- console.error(`${LOG_PREFIX} Missing antigravity model lookup for ${name}`);
444
- process.exit(1);
445
- }
420
+ const { fmBlock, afterFm, parsed, name, model } = subagent;
446
421
  const description = parsed.get("description") ?? "";
447
422
  const effort = parsed.get("effort");
448
423
  const toolsBlock = extractFrontmatterField(fmBlock, "tools");
@@ -3,7 +3,7 @@
3
3
  import * as fs from "node:fs";
4
4
  import { findLineAnchoredMarkers, selectMarkers } from "../install/managed-block.js";
5
5
  import { LOG_PREFIX } from "../shared/log-prefix.js";
6
- import { OPENCODE_PROVIDER_DEFAULTS } from "../shared/egress.js";
6
+ import { OPENCODE_PROVIDERS, OPENCODE_PROVIDER_DEFAULTS, } from "../shared/egress.js";
7
7
  /**
8
8
  * Parse .bkit/prompts.md § Models table → Map<name, columnValue>.
9
9
  * columnHeader is the column header label to extract (e.g. "Cursor model", "opencode model").
@@ -83,6 +83,9 @@ export function parseModelLookup(subagentsMdPath) {
83
83
  *
84
84
  * Cells hold the BARE, gateway-scoped model id. Any prefix a program's config format needs
85
85
  * is added at render time by formatModelPin — never stored in the table.
86
+ *
87
+ * Every allowlisted opencode provider MUST have an entry; the `satisfies Record<OpencodeProvider | …>`
88
+ * check fails tsc when one is missing.
86
89
  */
87
90
  const GATEWAY_COLUMN = {
88
91
  anthropic: "Anthropic",
@@ -94,6 +97,9 @@ const GATEWAY_COLUMN = {
94
97
  openai: "OpenAI",
95
98
  google: "Google",
96
99
  };
100
+ function isGatewayKey(value) {
101
+ return Object.hasOwn(GATEWAY_COLUMN, value);
102
+ }
97
103
  /**
98
104
  * Spell a model pin the way a program's config file expects it.
99
105
  *
@@ -110,17 +116,16 @@ export function formatModelPin(program, gateway, bareModel) {
110
116
  return bareModel;
111
117
  }
112
118
  /**
113
- * Build the per-agent opencode model lookup for the selected provider.
114
- * Source of truth = prompts.md § Models, which carries one curated column per
115
- * provider ("OpenRouter", "Ollama"). Header-addressed. Every allowlisted provider
116
- * ships a column; an absent one throws.
119
+ * Build the per-agent model lookup for one gateway: an opencode provider, or the Codex
120
+ * "openai" column. Source of truth = prompts.md § Models, which carries one curated column per
121
+ * gateway ("OpenRouter", "Ollama", "OpenAI"). Header-addressed. Every key of GATEWAY_COLUMN
122
+ * ships a column; any other key throws.
117
123
  */
118
124
  export function buildOpencodeModelLookup(subagentsMdPath, provider) {
119
- const header = GATEWAY_COLUMN[provider];
120
- if (!header) {
125
+ if (!isGatewayKey(provider)) {
121
126
  throw new Error(`buildOpencodeModelLookup: no prompts.md § Models column for provider ${JSON.stringify(provider)}`);
122
127
  }
123
- return parseModelLookupForColumn(subagentsMdPath, header);
128
+ return parseModelLookupForColumn(subagentsMdPath, GATEWAY_COLUMN[provider]);
124
129
  }
125
130
  /**
126
131
  * Build the per-agent Antigravity model lookup for Google models.
@@ -293,5 +298,11 @@ export function lookupEffort(effortMap, program, sourceEffort) {
293
298
  * keys pass through unchanged.
294
299
  */
295
300
  export function opencodeProviderId(providerKey) {
296
- return OPENCODE_PROVIDER_DEFAULTS[providerKey]?.providerId ?? providerKey;
301
+ if (!isOpencodeProvider(providerKey))
302
+ return providerKey;
303
+ return OPENCODE_PROVIDER_DEFAULTS[providerKey].providerId ?? providerKey;
304
+ }
305
+ /** Narrow a raw string to the opencode provider allowlist. */
306
+ export function isOpencodeProvider(value) {
307
+ return OPENCODE_PROVIDERS.some((provider) => provider === value);
297
308
  }
@@ -0,0 +1,11 @@
1
+ // shared/branches.ts — which branches are shared, so nobody works on them directly.
2
+ //
3
+ // `dev` is the integration branch; `dev` and `main` are the shared branches. The workflow
4
+ // commands (`commands/lib.ts` re-exports these) and the managed-version check both read
5
+ // this one rule.
6
+ /** The integration branch feature work lands on. */
7
+ export const DEV = "dev";
8
+ /** True for `dev` and `main`: no feature work happens on them, no force-push touches them. */
9
+ export function isSharedBranch(branch) {
10
+ return branch === DEV || branch === "main";
11
+ }
@@ -3,8 +3,6 @@
3
3
  /**
4
4
  * Providers bkit supports for the opencode host. `--opencode=<provider>` accepts
5
5
  * ONLY these — anything else is rejected (no generic/arbitrary-endpoint fallback).
6
- * Keep in sync with OPENCODE_PROVIDER_DEFAULTS (below) and
7
- * GATEWAY_COLUMN (render/models.ts § Models lookup).
8
6
  */
9
7
  export const OPENCODE_PROVIDERS = [
10
8
  // OpenAI-compatible npm driver — endpoint + key per provider.
@@ -4,6 +4,8 @@
4
4
  // .bkit/version) must fail open into a result the caller reports in its own terms,
5
5
  // never abort install, upgrade or `bkit check`. One shape, one catch — so the
6
6
  // defensive-slop gate reads a single documented fallback instead of one per caller.
7
+ // `audits/repo/json-parse-owner.test.ts` fails on a `JSON.parse` inside a `try` anywhere else.
8
+ import * as fs from "node:fs";
7
9
  /** `JSON.parse(raw)` as a result: `{ ok: true, value }`, or `{ ok: false }` when not JSON. */
8
10
  export function parseJson(raw) {
9
11
  try {
@@ -14,3 +16,15 @@ export function parseJson(raw) {
14
16
  return { ok: false };
15
17
  }
16
18
  }
19
+ /** `parseJson` over the file at `file`; `{ ok: false }` also when the file cannot be read. */
20
+ export function readJsonFile(file) {
21
+ let raw;
22
+ try {
23
+ raw = fs.readFileSync(file, "utf-8");
24
+ }
25
+ catch {
26
+ // Missing, unreadable or a directory: the result says so, like malformed JSON.
27
+ return { ok: false };
28
+ }
29
+ return parseJson(raw);
30
+ }
@@ -3,9 +3,12 @@
3
3
  // Commands that look up a script or a `bkit` setting need to tell "no file" and
4
4
  // "file is not JSON" apart from "key absent", so a broken manifest is named in the
5
5
  // message instead of reading as an empty one.
6
+ //
7
+ // `readInstallingVersion` reads the same file for the bkit package doing the install.
6
8
  import * as fs from "node:fs";
7
9
  import * as path from "node:path";
8
10
  import { parseJson } from "./json.js";
11
+ import { getPkgRoot } from "./package-roots.js";
9
12
  /** `dir/package.json` as an object, or why it could not be read as one. */
10
13
  export function readPackageJson(dir) {
11
14
  const file = path.join(dir, "package.json");
@@ -20,3 +23,61 @@ export function readPackageJson(dir) {
20
23
  }
21
24
  return { ok: true, pkg: parsed.value };
22
25
  }
26
+ /** Every dependency name across the four `package.json` dependency fields; `[]` when unreadable. */
27
+ export function readDependencyNames(cwd) {
28
+ const read = readPackageJson(cwd);
29
+ if (!read.ok)
30
+ return [];
31
+ const names = [];
32
+ for (const field of [
33
+ "dependencies",
34
+ "devDependencies",
35
+ "optionalDependencies",
36
+ "peerDependencies",
37
+ ]) {
38
+ const deps = read.pkg[field];
39
+ if (deps && typeof deps === "object") {
40
+ names.push(...Object.keys(deps));
41
+ }
42
+ }
43
+ return names;
44
+ }
45
+ /**
46
+ * The spec `package.json` declares for dependency `name` (`"^5.2.0"`, `"file:../x.tgz"`, …),
47
+ * from `dependencies`, `devDependencies` or `optionalDependencies`; `null` when undeclared or
48
+ * the file is unreadable.
49
+ */
50
+ export function readDependencySpec(cwd, name) {
51
+ const read = readPackageJson(cwd);
52
+ if (!read.ok)
53
+ return null;
54
+ for (const field of ["dependencies", "devDependencies", "optionalDependencies"]) {
55
+ const deps = read.pkg[field];
56
+ if (deps && typeof deps === "object") {
57
+ const spec = deps[name];
58
+ if (typeof spec === "string")
59
+ return spec;
60
+ }
61
+ }
62
+ return null;
63
+ }
64
+ /**
65
+ * The version of the bkit package doing the install, read from that package's own
66
+ * `package.json` (`root` defaults to the package root; never the consumer's file).
67
+ * Throws when the file is missing, is not a JSON object, or has no non-empty string
68
+ * `version`: the value is stamped into every managed file and the `.bkit/version` pin,
69
+ * so a damaged package stops the command instead of writing a guessed version.
70
+ */
71
+ export function readInstallingVersion(root = getPkgRoot()) {
72
+ const file = path.join(root, "package.json");
73
+ const read = readPackageJson(root);
74
+ if (!read.ok) {
75
+ const state = read.reason === "missing" ? "is missing" : "is not a JSON object";
76
+ throw new Error(`bkit's own package.json ${state} (${file}); reinstall @cbartens/bkit.`);
77
+ }
78
+ const version = read.pkg["version"];
79
+ if (typeof version !== "string" || version === "") {
80
+ throw new Error(`bkit's own package.json has no string "version" (${file}); reinstall @cbartens/bkit.`);
81
+ }
82
+ return version;
83
+ }
@@ -132,11 +132,3 @@ export function getHooksRoot() {
132
132
  export function getBkitDocsRoot() {
133
133
  return path.join(getCoreRoot(), ".bkit");
134
134
  }
135
- // ---------------------------------------------------------------------------
136
- // isDogfood — true when bkit is running inside its own repo (in-repo install).
137
- // In this case the source tree IS the consumer's .agents/ directory, so any
138
- // payload copy would overwrite the source with itself.
139
- // ---------------------------------------------------------------------------
140
- export function isDogfood() {
141
- return getPkgRoot() === process.cwd();
142
- }