@cbartens/bkit 5.2.4 → 5.3.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 (221) hide show
  1. package/.bkit/README.md +26 -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 +11 -0
  6. package/.bkit/core/.bkit/README.md +25 -10
  7. package/.bkit/core/.bkit/cloud.md +9 -8
  8. package/.bkit/core/.bkit/comparison.md +68 -345
  9. package/.bkit/core/.bkit/install.md +12 -6
  10. package/.bkit/core/.bkit/lite.md +73 -0
  11. package/.bkit/core/.bkit/pipeline.md +345 -0
  12. package/.bkit/core/.bkit/prompts.md +88 -73
  13. package/.bkit/core/.bkit/providers.md +41 -22
  14. package/.bkit/core/.bkit/templates.md +106 -17
  15. package/.bkit/core/.bkit/trackers.md +47 -1
  16. package/.bkit/core/.bkit/workflows.md +44 -43
  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-adoption.md +1 -1
  21. package/.bkit/core/docs/templates/bkit-audit-dependencies.md +14 -10
  22. package/.bkit/core/docs/templates/bkit-audit-llm.md +20 -14
  23. package/.bkit/core/docs/templates/bkit-audit-prompts.md +2 -2
  24. package/.bkit/core/docs/templates/bkit-audit-red-team.md +10 -8
  25. package/.bkit/core/docs/templates/bkit-audit-security.md +3 -3
  26. package/.bkit/core/docs/templates/bkit-audit-testing.md +4 -3
  27. package/.bkit/core/docs/templates/bkit-plan.md +8 -6
  28. package/.bkit/core/engine/checks/after/auth.ts +9 -9
  29. package/.bkit/core/engine/checks/after/ci-yml.ts +14 -12
  30. package/.bkit/core/engine/checks/after/github.ts +29 -120
  31. package/.bkit/core/engine/checks/after/index.ts +2 -2
  32. package/.bkit/core/engine/checks/after/managed.ts +8 -28
  33. package/.bkit/core/engine/checks/after/tools.ts +3 -15
  34. package/.bkit/core/engine/checks/before/context-fetch.ts +1 -21
  35. package/.bkit/core/engine/checks/before/index.ts +7 -43
  36. package/.bkit/core/engine/checks/before/mcp.ts +17 -8
  37. package/.bkit/core/engine/checks/before/repo.ts +9 -12
  38. package/.bkit/core/engine/checks/status-holes.ts +4 -5
  39. package/.bkit/core/engine/checks/tips/catalogue.ts +60 -24
  40. package/.bkit/core/engine/checks/tips/detect.ts +45 -2
  41. package/.bkit/core/engine/checks/tips/index.ts +37 -8
  42. package/.bkit/core/engine/cli.ts +39 -16
  43. package/.bkit/core/engine/commands/analysis.ts +36 -26
  44. package/.bkit/core/engine/commands/comply.ts +17 -16
  45. package/.bkit/core/engine/commands/harvest.ts +12 -12
  46. package/.bkit/core/engine/commands/install/doctor.ts +57 -28
  47. package/.bkit/core/engine/commands/install/install.ts +62 -126
  48. package/.bkit/core/engine/commands/install/uninstall.ts +191 -33
  49. package/.bkit/core/engine/commands/install/upgrade.ts +49 -43
  50. package/.bkit/core/engine/commands/lib.ts +35 -10
  51. package/.bkit/core/engine/commands/tips.ts +8 -5
  52. package/.bkit/core/engine/commands/workflow/branches.ts +4 -4
  53. package/.bkit/core/engine/commands/workflow/counters.ts +21 -12
  54. package/.bkit/core/engine/commands/workflow/fids.ts +46 -0
  55. package/.bkit/core/engine/commands/workflow/ledgers.ts +3 -1
  56. package/.bkit/core/engine/commands/workflow/prs.ts +4 -2
  57. package/.bkit/core/engine/commands/workflow/repo.ts +2 -2
  58. package/.bkit/core/engine/commands/workflow/roadmap.ts +4 -3
  59. package/.bkit/core/engine/commands/workflow/runs.ts +8 -7
  60. package/.bkit/core/engine/commands/workflow/steps.ts +1 -0
  61. package/.bkit/core/engine/commands/workflow/tests.ts +17 -5
  62. package/.bkit/core/engine/commands/workflow/workflows.ts +15 -14
  63. package/.bkit/core/engine/commands/workflow/workstreams.ts +2 -1
  64. package/.bkit/core/engine/commands/workflow/worktrees.ts +13 -5
  65. package/.bkit/core/engine/install/cloud-env.ts +10 -31
  66. package/.bkit/core/engine/install/manifest.ts +14 -7
  67. package/.bkit/core/engine/install/modules/antigravity.ts +15 -26
  68. package/.bkit/core/engine/install/modules/claude.ts +20 -28
  69. package/.bkit/core/engine/install/modules/cloud.ts +5 -2
  70. package/.bkit/core/engine/install/modules/codex.ts +15 -29
  71. package/.bkit/core/engine/install/modules/core.ts +91 -70
  72. package/.bkit/core/engine/install/modules/cursor.ts +6 -20
  73. package/.bkit/core/engine/install/modules/index.ts +7 -1
  74. package/.bkit/core/engine/install/modules/opencode.ts +48 -70
  75. package/.bkit/core/engine/install/network-allowlist.ts +73 -0
  76. package/.bkit/core/engine/install/placeholders.ts +3 -18
  77. package/.bkit/core/engine/install/write.ts +59 -12
  78. package/.bkit/core/engine/ledgers/telemetry.ts +6 -4
  79. package/.bkit/core/engine/render/hosts.ts +47 -67
  80. package/.bkit/core/engine/render/models.ts +29 -11
  81. package/.bkit/core/engine/shared/branches.ts +13 -0
  82. package/.bkit/core/engine/shared/egress.ts +3 -3
  83. package/.bkit/core/engine/shared/json.ts +18 -1
  84. package/.bkit/core/engine/shared/package-json.ts +63 -0
  85. package/.bkit/core/engine/shared/package-roots.ts +0 -10
  86. package/.bkit/core/engine/shared/protected-paths.ts +5 -130
  87. package/.bkit/core/engine/shared/settings.ts +36 -0
  88. package/.bkit/core/hooks/antigravity-hook.js +1 -0
  89. package/.bkit/core/hooks/guardrails-shell.js +186 -40
  90. package/.bkit/core/hooks/guardrails-tiers.js +5 -78
  91. package/.bkit/core/prompts/bkit-audit-generate.md +60 -7
  92. package/.bkit/core/prompts/bkit-build-challenge-1.md +1 -1
  93. package/.bkit/core/prompts/bkit-build-challenge-2.md +1 -1
  94. package/.bkit/core/prompts/bkit-build-challenge-reconcile.md +1 -1
  95. package/.bkit/core/prompts/bkit-build-challenge-remediate.md +1 -1
  96. package/.bkit/core/prompts/bkit-build-challenge-tests.md +1 -1
  97. package/.bkit/core/prompts/bkit-build-install-skills.md +1 -1
  98. package/.bkit/core/prompts/bkit-build-plan-current.md +1 -1
  99. package/.bkit/core/prompts/bkit-build-plan-execute.md +4 -4
  100. package/.bkit/core/prompts/bkit-build-plan-gaps-remediate.md +1 -1
  101. package/.bkit/core/prompts/bkit-build-plan-verify.md +2 -2
  102. package/.bkit/core/prompts/bkit-debug-abort-handover.md +1 -1
  103. package/.bkit/core/prompts/bkit-debug-failure-diagnose.md +1 -1
  104. package/.bkit/core/prompts/bkit-debug-failure-remediate.md +1 -1
  105. package/.bkit/core/prompts/bkit-debug-failure-reproduce.md +78 -0
  106. package/.bkit/core/prompts/bkit-debug-failure-soak.md +1 -1
  107. package/.bkit/core/prompts/bkit-debug-summary.md +3 -3
  108. package/.bkit/core/prompts/bkit-design-spike.md +1 -1
  109. package/.bkit/core/prompts/bkit-design-summary.md +2 -2
  110. package/.bkit/core/prompts/bkit-design-update.md +1 -1
  111. package/.bkit/core/prompts/bkit-plan-complexity-check.md +1 -1
  112. package/.bkit/core/prompts/bkit-plan-generate.md +1 -1
  113. package/.bkit/core/prompts/bkit-plan-scope-check-1.md +1 -1
  114. package/.bkit/core/prompts/bkit-plan-scope-check-2.md +1 -1
  115. package/.bkit/core/prompts/bkit-plan-summary.md +1 -1
  116. package/.bkit/core/prompts/bkit-plan-update.md +1 -1
  117. package/.bkit/core/prompts/bkit-ship-conflict-resolve.md +1 -1
  118. package/.bkit/core/prompts/bkit-ship-deferred-sweep.md +1 -1
  119. package/.bkit/core/prompts/bkit-ship-update-docs.md +1 -1
  120. package/.bkit/core/prompts/bkit-utility-update-decisions.md +9 -2
  121. package/.bkit/core/prompts/bkit-utility-update-deferrals.md +9 -2
  122. package/.bkit/core/prompts/bkit-utility-update-documentation.md +2 -1
  123. package/.bkit/core/prompts/bkit-utility-update-failures.md +9 -2
  124. package/.bkit/core/prompts/bkit-utility-update-knowledge.md +9 -2
  125. package/.bkit/core/prompts/bkit-utility-update-roadmap.md +11 -4
  126. package/.bkit/core/workflows/bkit-build.md +3 -3
  127. package/.bkit/core/workflows/bkit-debug.md +69 -33
  128. package/.bkit/core/workflows/bkit-design.md +2 -2
  129. package/.bkit/core/workflows/bkit-plan.md +11 -4
  130. package/.bkit/core/workflows/bkit-ship.md +5 -5
  131. package/.bkit/shared/.claude/bkit-cloud.md +46 -13
  132. package/.bkit/shared/.claude/settings.json +18 -0
  133. package/.bkit/shared/.cursor/mcp.json +2 -1
  134. package/.bkit/shared/.opencode/opencode.json +19 -1
  135. package/.bkit/shared/docs/deployments.md +20 -4
  136. package/.bkit/shared/docs/security.md +8 -6
  137. package/.bkit/shared/docs/testing.md +9 -5
  138. package/.bkit/templates/.cursor/Dockerfile +30 -16
  139. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-bug.yml +78 -0
  140. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-change.yml +91 -0
  141. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-idea.yml +56 -0
  142. package/.bkit/templates/.github/renovate.json5 +91 -12
  143. package/.bkit/templates/.github/workflows/bkit-ci.yml +66 -53
  144. package/.bkit/templates/.github/workflows/bkit-cloud.yml +3 -1
  145. package/.bkit/templates/.github/workflows/bkit-renovate.yml +8 -1
  146. package/.bkit/templates/.github/workflows/bkit-trivy.yml +67 -0
  147. package/.bkit/templates/.husky/pre-push +8 -3
  148. package/.bkit/templates/.oxlintrc.json +21 -0
  149. package/.bkit/templates/docs/capabilities.md +1 -1
  150. package/.bkit/templates/scripts/ci/check-capstone.mjs +176 -0
  151. package/.bkit/templates/scripts/ci/check-capstone.test.mjs +311 -0
  152. package/.bkit/templates/scripts/graphify/bkit-graph-eager.py +2 -2
  153. package/.bkit/templates/scripts/graphify/bkit-graph-imports.py +43 -20
  154. package/.bkit/templates/scripts/graphify/bkit-graph-pre-push.sh +33 -11
  155. package/.bkit/templates/scripts/graphify/bkit-graph-repair.py +3 -2
  156. package/.bkit/templates/scripts/graphify/bkit-graphify-rollup.py +5 -5
  157. package/.bkit/templates/scripts/graphify/bkit-graphify.sh +79 -19
  158. package/CHANGELOG.md +50 -3
  159. package/README.md +258 -129
  160. package/dist/checks/after/auth.js +10 -11
  161. package/dist/checks/after/ci-yml.js +11 -14
  162. package/dist/checks/after/github.js +32 -125
  163. package/dist/checks/after/index.js +3 -2
  164. package/dist/checks/after/managed.js +8 -24
  165. package/dist/checks/after/tools.js +3 -16
  166. package/dist/checks/before/context-fetch.js +1 -21
  167. package/dist/checks/before/index.js +6 -42
  168. package/dist/checks/before/mcp.js +14 -9
  169. package/dist/checks/before/repo.js +8 -11
  170. package/dist/checks/status-holes.js +5 -6
  171. package/dist/checks/tips/catalogue.js +51 -18
  172. package/dist/checks/tips/detect.js +41 -2
  173. package/dist/checks/tips/index.js +25 -5
  174. package/dist/cli.js +34 -14
  175. package/dist/commands/analysis.js +21 -5
  176. package/dist/commands/comply.js +20 -18
  177. package/dist/commands/harvest.js +12 -13
  178. package/dist/commands/install/doctor.js +55 -23
  179. package/dist/commands/install/install.js +62 -118
  180. package/dist/commands/install/uninstall.js +165 -31
  181. package/dist/commands/install/upgrade.js +46 -39
  182. package/dist/commands/lib.js +32 -10
  183. package/dist/commands/tips.js +6 -3
  184. package/dist/commands/workflow/branches.js +3 -5
  185. package/dist/commands/workflow/counters.js +17 -7
  186. package/dist/commands/workflow/fids.js +39 -0
  187. package/dist/commands/workflow/ledgers.js +3 -1
  188. package/dist/commands/workflow/prs.js +3 -3
  189. package/dist/commands/workflow/repo.js +2 -2
  190. package/dist/commands/workflow/roadmap.js +4 -4
  191. package/dist/commands/workflow/runs.js +5 -5
  192. package/dist/commands/workflow/steps.js +1 -0
  193. package/dist/commands/workflow/tests.js +16 -3
  194. package/dist/commands/workflow/workflows.js +15 -15
  195. package/dist/commands/workflow/workstreams.js +2 -1
  196. package/dist/commands/workflow/worktrees.js +4 -5
  197. package/dist/install/cloud-env.js +10 -23
  198. package/dist/install/manifest.js +14 -7
  199. package/dist/install/modules/antigravity.js +15 -26
  200. package/dist/install/modules/claude.js +17 -21
  201. package/dist/install/modules/cloud.js +6 -2
  202. package/dist/install/modules/codex.js +15 -29
  203. package/dist/install/modules/core.js +82 -63
  204. package/dist/install/modules/cursor.js +6 -9
  205. package/dist/install/modules/index.js +6 -1
  206. package/dist/install/modules/opencode.js +41 -72
  207. package/dist/install/network-allowlist.js +62 -0
  208. package/dist/install/placeholders.js +3 -16
  209. package/dist/install/write.js +55 -15
  210. package/dist/ledgers/telemetry.js +5 -4
  211. package/dist/render/hosts.js +30 -55
  212. package/dist/render/models.js +20 -9
  213. package/dist/shared/branches.js +11 -0
  214. package/dist/shared/egress.js +0 -2
  215. package/dist/shared/json.js +14 -0
  216. package/dist/shared/package-json.js +61 -0
  217. package/dist/shared/package-roots.js +0 -8
  218. package/dist/shared/protected-paths.js +5 -122
  219. package/dist/shared/settings.js +18 -1
  220. package/package.json +21 -17
  221. package/.bkit/core/.agents/shortcuts.md +0 -73
@@ -17,9 +17,11 @@
17
17
  import { execFileSync } from "node:child_process";
18
18
  import * as fs from "node:fs";
19
19
  import * as path from "node:path";
20
+ import { readJsonFile } from "../shared/json.js";
20
21
  import { getPkgRoot } from "../shared/package-roots.js";
21
22
  import { isMirrorPath } from "../shared/protected-paths.js";
22
23
  import {
24
+ gitEnv,
23
25
  resolveGitSha,
24
26
  resolveRepoSlug,
25
27
  type TelemetryEnvelope,
@@ -72,6 +74,7 @@ export const realGitAdapter: GitAdapter = {
72
74
  try {
73
75
  const output = execFileSync("git", ["diff", "--name-only", `${parentSha}..HEAD`], {
74
76
  cwd,
77
+ env: gitEnv(),
75
78
  encoding: "utf8",
76
79
  stdio: ["ignore", "pipe", "ignore"],
77
80
  });
@@ -89,6 +92,7 @@ export const realGitAdapter: GitAdapter = {
89
92
  try {
90
93
  return execFileSync("git", ["log", "-1", "--format=%s"], {
91
94
  cwd,
95
+ env: gitEnv(),
92
96
  encoding: "utf8",
93
97
  stdio: ["ignore", "pipe", "ignore"],
94
98
  }).trim();
@@ -342,23 +346,20 @@ export function formatComplianceSummary(violations: Violation[]): string {
342
346
  * running bkit package's root). Returns "0.0.0" on any read/parse error (fail-open).
343
347
  */
344
348
  export function readBkitVersion(searchFrom?: string): string {
345
- try {
346
- // Walk up looking for a package.json with a `version` field; stop at the filesystem root.
347
- let dir = path.resolve(searchFrom ?? getPkgRoot());
348
- for (let i = 0; i < 8; i++) {
349
- const candidate = path.join(dir, "package.json");
350
- if (fs.existsSync(candidate)) {
351
- const pkg = JSON.parse(fs.readFileSync(candidate, "utf8")) as {
352
- version?: string;
353
- };
354
- if (typeof pkg.version === "string") return pkg.version;
355
- }
356
- const parent = path.dirname(dir);
357
- if (parent === dir) break;
358
- dir = parent;
349
+ // Walk up looking for a package.json with a `version` field; stop at the filesystem root.
350
+ let dir = path.resolve(searchFrom ?? getPkgRoot());
351
+ for (let i = 0; i < 8; i++) {
352
+ const candidate = path.join(dir, "package.json");
353
+ if (fs.existsSync(candidate)) {
354
+ const read = readJsonFile(candidate);
355
+ // Fail-open: a package.json that cannot be read ends the search with the default.
356
+ if (!read.ok || read.value === null) return "0.0.0";
357
+ const pkg = read.value as { version?: string };
358
+ if (typeof pkg.version === "string") return pkg.version;
359
359
  }
360
- } catch (_e) {
361
- void _e; // fail-open: version read errors return default
360
+ const parent = path.dirname(dir);
361
+ if (parent === dir) break;
362
+ dir = parent;
362
363
  }
363
364
  return "0.0.0";
364
365
  }
@@ -14,8 +14,8 @@
14
14
  // - The prune gate is byte-for-byte CONTENT, never identity.
15
15
  // - The central store ACCUMULATES across runs (never cleared) and holds every
16
16
  // DISTINCT row, deduped by content only. It is never rewritten from the fold. It is
17
- // the only record of pruned consumer friction until the caller commits it; readers
18
- // fold it.
17
+ // gitignored (private consumer content), so it is the only record of pruned consumer
18
+ // friction; readers fold it.
19
19
  // - Malformed consumer lines are NEVER pruned (not ours to delete).
20
20
  // - Grep-able [bkit-harvest] log lines.
21
21
  // - Relies on the tolerant `parse` for half-written tails.
@@ -24,7 +24,7 @@
24
24
  // copy -> fsync -> verify -> prune; a crash at any point re-copies idempotently on the
25
25
  // next run.
26
26
  //
27
- // Consumers are listed in `docs/consumers.md` (one absolute path per line).
27
+ // Consumers are listed in `harvest/consumers.md` (one absolute path per line).
28
28
  // See DE-34 for the shared-entrypoint + crash-isolation contract.
29
29
 
30
30
  import * as fs from "node:fs";
@@ -36,6 +36,7 @@ import {
36
36
  type FoldedState,
37
37
  type TelemetryEvent,
38
38
  } from "../ledgers/telemetry.js";
39
+ import { parseJson } from "../shared/json.js";
39
40
 
40
41
  // ---------------------------------------------------------------------------
41
42
  // Constants
@@ -52,7 +53,7 @@ export const HARVEST_PRUNE_FILES = ["friction.jsonl"] as const;
52
53
  /** Files to COUNT-ONLY from each consumer's `docs/` directory (metadata only). */
53
54
  export const HARVEST_COUNT_FILES = ["deferrals.jsonl", "failures.jsonl"] as const;
54
55
  /** Path (relative to bkit repo root) for the consumers list. */
55
- export const CONSUMERS_FILE = "docs/consumers.md";
56
+ export const CONSUMERS_FILE = "harvest/consumers.md";
56
57
  /**
57
58
  * Destination directory in bkit for harvested data. Repo root, NOT under `.bkit/`
58
59
  * (bkit's managed/installed surface).
@@ -91,11 +92,11 @@ export interface HarvestResult {
91
92
  }
92
93
 
93
94
  // ---------------------------------------------------------------------------
94
- // Read consumer list from `docs/consumers.md`
95
+ // Read consumer list from `harvest/consumers.md`
95
96
  // ---------------------------------------------------------------------------
96
97
 
97
98
  /**
98
- * Read the list of consumer repo root paths from `docs/consumers.md`.
99
+ * Read the list of consumer repo root paths from `harvest/consumers.md`.
99
100
  * Returns an array of absolute paths (non-empty lines, comments stripped).
100
101
  */
101
102
  export function readConsumerPaths(bkitRoot: string): string[] {
@@ -256,13 +257,12 @@ export function pruneConsumerFile(
256
257
 
257
258
  for (const line of raw.split("\n")) {
258
259
  if (line.trim() === "") continue;
259
- let event: TelemetryEvent;
260
- try {
261
- event = JSON.parse(line.trim()) as TelemetryEvent;
262
- } catch {
260
+ const parsed = parseJson(line.trim());
261
+ if (!parsed.ok) {
263
262
  kept.push(line); // malformed — never ours to delete
264
263
  continue;
265
264
  }
265
+ const event = parsed.value as TelemetryEvent;
266
266
  if (durable.rows.has(canonicalRow(stampRepo(event, consumerSlug)))) {
267
267
  removed++;
268
268
  continue;
@@ -304,7 +304,7 @@ function fsyncFile(filePath: string): void {
304
304
  // ---------------------------------------------------------------------------
305
305
 
306
306
  /**
307
- * Run harvest across all consumers listed in `docs/consumers.md`.
307
+ * Run harvest across all consumers listed in `harvest/consumers.md`.
308
308
  * Deduplicates the store by row content after collecting all rows.
309
309
  */
310
310
  export function runHarvest(bkitRoot: string): HarvestResult {
@@ -391,7 +391,7 @@ export function runHarvest(bkitRoot: string): HarvestResult {
391
391
  );
392
392
  if (totalPruned > 0) {
393
393
  console.error(
394
- `[bkit-harvest] ${totalPruned} row(s) now exist ONLY in ${HARVEST_DEST_DIR}/ — commit it to make them durable.`,
394
+ `[bkit-harvest] ${totalPruned} row(s) now exist ONLY in ${HARVEST_DEST_DIR}/ (gitignored) — back it up to make them durable.`,
395
395
  );
396
396
  }
397
397
 
@@ -3,14 +3,12 @@
3
3
 
4
4
  import * as fs from "node:fs";
5
5
  import * as path from "node:path";
6
- import {
7
- detectOpencodeProvider,
8
- detectOpencodeProviders,
9
- getInstallingVersion,
10
- } from "./install.js";
6
+ import { detectOpencodeProvider, detectOpencodeProviders } from "./install.js";
11
7
  import { type ModuleFlag } from "../../cli.js";
12
8
  import { OPENCODE_PROVIDERS } from "../../shared/egress.js";
9
+ import { readJsonFile } from "../../shared/json.js";
13
10
  import { LOG_PREFIX, setLogPrefix } from "../../shared/log-prefix.js";
11
+ import { readInstallingVersion } from "../../shared/package-json.js";
14
12
  import { getCoreRoot, getSharedRoot, getTemplatesRoot } from "../../shared/package-roots.js";
15
13
  import { runPreflight } from "../../checks/before/index.js";
16
14
  import {
@@ -22,11 +20,47 @@ import type { SuppressionNote } from "../../checks/after/suppression.js";
22
20
  import { collectTips } from "../../checks/tips/index.js";
23
21
  import { parseDotenv } from "../../install/cloud-env.js";
24
22
  import { detectInstalledHostModules } from "../../install/hosts.js";
23
+ import { parseFrontmatter } from "../../render/hosts.js";
24
+
25
+ /** One distinct opencode model pin and how many agent files carry it. */
26
+ export interface OpencodeModelPin {
27
+ model: string;
28
+ agents: number;
29
+ }
30
+
31
+ /**
32
+ * The `model:` pins in the frontmatter of `.opencode/agents/*.md` — where bkit renders the
33
+ * opencode per-agent pins — grouped by model, most-used first (ties by model id). `[]` when
34
+ * the directory is absent or no agent carries a pin.
35
+ */
36
+ export function readOpencodeModelPins(cwd: string): OpencodeModelPin[] {
37
+ const agentsDir = path.join(cwd, ".opencode", "agents");
38
+ if (!fs.existsSync(agentsDir)) return [];
39
+ const counts = new Map<string, number>();
40
+ for (const name of fs.readdirSync(agentsDir)) {
41
+ if (!name.endsWith(".md")) continue;
42
+ const content = fs.readFileSync(path.join(agentsDir, name), "utf-8");
43
+ const fm = content.match(/^---\r?\n([\s\S]*?)\r?\n---/);
44
+ const model = fm ? parseFrontmatter(fm[1]).get("model")?.trim() : undefined;
45
+ if (model) counts.set(model, (counts.get(model) ?? 0) + 1);
46
+ }
47
+ return [...counts]
48
+ .map(([model, agents]) => ({ model, agents }))
49
+ .sort((a, b) => b.agents - a.agents || a.model.localeCompare(b.model));
50
+ }
51
+
52
+ /** `model: <id>` for one distinct pin, else `models: <id> (N agents), …`. */
53
+ export function formatOpencodeModelPins(pins: readonly OpencodeModelPin[]): string {
54
+ if (pins.length === 0) return "model: none pinned in .opencode/agents/";
55
+ if (pins.length === 1) return `model: ${pins[0].model}`;
56
+ const list = pins.map((p) => `${p.model} (${p.agents} agent${p.agents === 1 ? "" : "s"})`);
57
+ return `models: ${list.join(", ")}`;
58
+ }
25
59
 
26
60
  /**
27
61
  * `bkit status` — read-only host/provider status.
28
- * Reports current host+provider sampled from .opencode/opencode.json,
29
- * .claude/, and .cursor/ presence. Writes nothing.
62
+ * Reports current host+provider sampled from .claude/, .cursor/, package.json and the
63
+ * opencode agent model pins in .opencode/agents/. Writes nothing.
30
64
  */
31
65
  export function runStatus(cwd: string, logPrefix: string): void {
32
66
  console.log(`${logPrefix} Host/provider status (read-only — no writes)`);
@@ -38,13 +72,11 @@ export function runStatus(cwd: string, logPrefix: string): void {
38
72
  const claudeSettingsPath = path.join(claudeDir, "settings.json");
39
73
  let claudeModel = "claude (default)";
40
74
  if (claudePresent && fs.existsSync(claudeSettingsPath)) {
41
- try {
42
- const settings = JSON.parse(fs.readFileSync(claudeSettingsPath, "utf-8")) as {
43
- model?: string;
44
- };
75
+ // Best-effort: an unreadable settings file keeps the default label.
76
+ const read = readJsonFile(claudeSettingsPath);
77
+ if (read.ok && typeof read.value === "object" && read.value !== null) {
78
+ const settings = read.value as { model?: string };
45
79
  if (settings.model) claudeModel = settings.model;
46
- } catch {
47
- // best-effort
48
80
  }
49
81
  }
50
82
  console.log(
@@ -63,22 +95,19 @@ export function runStatus(cwd: string, logPrefix: string): void {
63
95
  const activeOpencodeProvider = detectOpencodeProvider(cwd);
64
96
  // All installed providers.
65
97
  const allOpencodeProviders = detectOpencodeProviders(cwd);
66
- let opencodeModel = "unknown";
98
+ // bkit pins models per agent in .opencode/agents/*.md frontmatter; opencode.json carries
99
+ // only the consumer's optional top-level default `model`.
100
+ let opencodeModel = formatOpencodeModelPins(readOpencodeModelPins(cwd));
67
101
  const opencodeJsonPath = path.join(opencodeDir, "opencode.json");
68
102
  if (opencodePresent && fs.existsSync(opencodeJsonPath)) {
69
- try {
70
- const cfg = JSON.parse(fs.readFileSync(opencodeJsonPath, "utf-8")) as {
71
- model?: string;
72
- models?: Record<string, unknown>;
73
- };
74
- if (cfg.model) opencodeModel = cfg.model;
75
- // Fallback: look for non-empty models map
76
- if (opencodeModel === "unknown" && cfg.models) {
77
- const modelKeys = Object.keys(cfg.models);
78
- if (modelKeys.length > 0) opencodeModel = `${modelKeys.length} model(s) configured`;
103
+ const read = readJsonFile(opencodeJsonPath);
104
+ if (!read.ok || read.value === null || typeof read.value !== "object") {
105
+ opencodeModel += "; opencode.json: parse-error";
106
+ } else {
107
+ const cfg = read.value as { model?: unknown };
108
+ if (typeof cfg.model === "string" && cfg.model) {
109
+ opencodeModel += `; default model: ${cfg.model}`;
79
110
  }
80
- } catch {
81
- opencodeModel = "parse-error";
82
111
  }
83
112
  }
84
113
  let opencodeProviderDisplay: string;
@@ -103,7 +132,7 @@ export function runStatus(cwd: string, logPrefix: string): void {
103
132
  const antigravityPresent = detectInstalledHostModules(cwd).has("antigravity");
104
133
 
105
134
  const opencodeStatus = opencodePresent
106
- ? `INSTALLED (${providerLabel}: ${opencodeProviderDisplay} / model: ${opencodeModel})`
135
+ ? `INSTALLED (${providerLabel}: ${opencodeProviderDisplay} / ${opencodeModel})`
107
136
  : "absent (run: bkit install --opencode=<provider>)";
108
137
  console.log(`${logPrefix} opencode: ${opencodeStatus}`);
109
138
  console.log(
@@ -168,7 +197,7 @@ export async function runDoctor(
168
197
  writeAdvisoriesArtifact(allFindings, {
169
198
  cwd: process.cwd(),
170
199
  mode: "doctor",
171
- version: getInstallingVersion(),
200
+ version: readInstallingVersion(),
172
201
  logPrefix: LOG_PREFIX,
173
202
  suppressed,
174
203
  tips,
@@ -5,14 +5,15 @@ import * as fs from "node:fs";
5
5
  import * as path from "node:path";
6
6
  import { OPENCODE_PROVIDERS } from "../../shared/egress.js";
7
7
  import { type ModuleFlag, type CloudVariant } from "../../cli.js";
8
- import { getPkgRoot } from "../../shared/package-roots.js";
8
+ import { readInstallingVersion } from "../../shared/package-json.js";
9
9
  import { type ModuleContext, dispatchModules } from "../../install/modules/index.js";
10
10
  import {
11
11
  type IssueTracker,
12
12
  DEFAULT_ISSUE_TRACKER,
13
- parseIssueTracker,
14
13
  type ModuleName,
15
14
  } from "../../install/manifest.js";
15
+ import { parseJson, readJsonFile } from "../../shared/json.js";
16
+ import { readBkitConfig } from "../../shared/settings.js";
16
17
  import { LOG_PREFIX, setLogPrefix } from "../../shared/log-prefix.js";
17
18
  import type { ParsedArgs } from "../../cli.js";
18
19
  import { detectInstalledModules, reportCheckDrift } from "./check.js";
@@ -37,7 +38,7 @@ import {
37
38
  } from "../../render/models.js";
38
39
  import { getBkitDocsRoot } from "../../shared/package-roots.js";
39
40
  import type { WriteSet } from "../../install/write.js";
40
- import { type UninstallContext, runUninstall } from "./uninstall.js";
41
+ import { type UninstallContext, removeUnusedTrackerMcpEntries, runUninstall } from "./uninstall.js";
41
42
 
42
43
  // ---------------------------------------------------------------------------
43
44
  // Staging scratch area
@@ -86,24 +87,6 @@ export function requireOpencodeProvider(cwd: string, context: string): string {
86
87
  return detected;
87
88
  }
88
89
 
89
- /**
90
- * Read the installing bkit version from the package root's package.json.
91
- * Returns "0.0.0" when unreadable (e.g. dev tree without a package.json at the
92
- * resolved root) — keeps the advisories artifact informational, not fatal.
93
- */
94
- export function getInstallingVersion(): string {
95
- try {
96
- const pkgJsonPath = path.join(getPkgRoot(), "package.json");
97
- if (fs.existsSync(pkgJsonPath)) {
98
- const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8")) as { version?: string };
99
- return pkg.version ?? "0.0.0";
100
- }
101
- } catch {
102
- return "0.0.0"; // best-effort: unreadable/garbled package.json
103
- }
104
- return "0.0.0";
105
- }
106
-
107
90
  /**
108
91
  * Render a resolved module set back into valid CLI flags for
109
92
  * recovery-hint text. Skips core (implicit) and spells opt-ins with their value flags.
@@ -163,37 +146,19 @@ export function validateCloudTrackerMatch(
163
146
  * smuggle an unsupported provider into the render path.
164
147
  */
165
148
  export function detectOpencodeProvider(cwd: string): string | null {
166
- const pkgPath = path.join(cwd, "package.json");
167
- if (fs.existsSync(pkgPath)) {
168
- try {
169
- const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8")) as {
170
- bkit?: { opencodeProvider?: unknown };
171
- };
172
- const stored = pkg.bkit?.opencodeProvider;
173
- if (
174
- typeof stored === "string" &&
175
- (OPENCODE_PROVIDERS as readonly string[]).includes(stored)
176
- ) {
177
- return stored;
178
- }
179
- } catch {
180
- // unparseable package.json — fall through to opencode.json detection
181
- }
182
- }
149
+ const stored = readBkitConfig(cwd).opencodeProvider;
150
+ if (stored !== null) return stored;
183
151
  const jsonPath = path.join(cwd, ".opencode", "opencode.json");
184
152
  if (!fs.existsSync(jsonPath)) return null;
185
- try {
186
- const cfg = JSON.parse(fs.readFileSync(jsonPath, "utf-8")) as {
187
- provider?: Record<string, unknown>;
188
- };
189
- if (cfg.provider && typeof cfg.provider === "object") {
190
- const first = Object.keys(cfg.provider)[0];
191
- if (!first) return null;
192
- // Zen's opencode.json block is keyed by the built-in id `opencode`, not the CLI key.
193
- return first === "opencode" ? "opencode-zen" : first;
194
- }
195
- } catch {
196
- return null; // unparseable opencode.json — fall back to the default provider
153
+ // Unparseable opencode.json — fall back to the default provider.
154
+ const read = readJsonFile(jsonPath);
155
+ if (!read.ok || typeof read.value !== "object" || read.value === null) return null;
156
+ const cfg = read.value as { provider?: Record<string, unknown> };
157
+ if (cfg.provider && typeof cfg.provider === "object") {
158
+ const first = Object.keys(cfg.provider)[0];
159
+ if (!first) return null;
160
+ // Zen's opencode.json block is keyed by the built-in id `opencode`, not the CLI key.
161
+ return first === "opencode" ? "opencode-zen" : first;
197
162
  }
198
163
  return null;
199
164
  }
@@ -204,23 +169,7 @@ export function detectOpencodeProvider(cwd: string): string | null {
204
169
  * the field is absent.
205
170
  */
206
171
  export function detectOpencodeProviders(cwd: string): string[] {
207
- const pkgPath = path.join(cwd, "package.json");
208
- if (!fs.existsSync(pkgPath)) return [];
209
- try {
210
- const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8")) as {
211
- bkit?: { opencodeProviders?: unknown };
212
- };
213
- const arr = pkg.bkit?.opencodeProviders;
214
- if (Array.isArray(arr)) {
215
- return arr.filter(
216
- (v): v is string =>
217
- typeof v === "string" && (OPENCODE_PROVIDERS as readonly string[]).includes(v),
218
- );
219
- }
220
- } catch {
221
- // unparseable — return empty
222
- }
223
- return [];
172
+ return readBkitConfig(cwd).opencodeProviders;
224
173
  }
225
174
 
226
175
  /**
@@ -297,24 +246,8 @@ export async function runInstall(
297
246
  // already-recorded tracker.
298
247
  // Resolved BEFORE preflight: preflight's tracker-dependent detectors grade the tracker
299
248
  // this run INSTALLS, not the one on disk from the previous run. Read-only — no writes here.
300
- let issueTracker: IssueTracker = DEFAULT_ISSUE_TRACKER;
301
- {
302
- const consumerPkgPath = path.join(process.cwd(), "package.json");
303
- if (fs.existsSync(consumerPkgPath)) {
304
- try {
305
- const pkg = JSON.parse(fs.readFileSync(consumerPkgPath, "utf-8")) as Record<
306
- string,
307
- unknown
308
- >;
309
- const bkitField = pkg.bkit as Record<string, unknown> | undefined;
310
- const stored = parseIssueTracker(bkitField?.issueTracker);
311
- if (stored !== null) issueTracker = stored;
312
- } catch {
313
- // non-fatal — keep default
314
- }
315
- }
316
- if (tracker !== null) issueTracker = tracker;
317
- }
249
+ const recordedTracker: IssueTracker = readBkitConfig(process.cwd()).issueTracker;
250
+ const issueTracker: IssueTracker = tracker ?? recordedTracker;
318
251
 
319
252
  // --- Phase 1: Preflight (read-only, before any writes) ---
320
253
  // Must run BEFORE resolveTokens to short-circuit cleanly on blockers.
@@ -332,7 +265,7 @@ export async function runInstall(
332
265
  writeAdvisoriesArtifact(blockers, {
333
266
  cwd: process.cwd(),
334
267
  mode: "upgrade",
335
- version: getInstallingVersion(),
268
+ version: readInstallingVersion(),
336
269
  logPrefix: LOG_PREFIX,
337
270
  });
338
271
  return;
@@ -447,36 +380,40 @@ export async function runInstall(
447
380
  if (fs.existsSync(consumerPkgPath2)) {
448
381
  try {
449
382
  const raw = fs.readFileSync(consumerPkgPath2, "utf-8");
450
- const pkg = JSON.parse(raw) as Record<string, unknown>;
451
- const bkit = (pkg.bkit ?? {}) as Record<string, unknown>;
452
- if (persistTracker) bkit.issueTracker = tracker;
453
- if (persistProvider) {
454
- bkit.opencodeProvider = opencodeProvider; // active (drives agent frontmatter)
455
- // Update providers array (deduped).
456
- const existingArr: string[] = Array.isArray(bkit.opencodeProviders)
457
- ? [...(bkit.opencodeProviders as string[])]
458
- : [];
459
- if (!existingArr.includes(opencodeProvider!)) {
460
- existingArr.push(opencodeProvider!);
383
+ // A package.json that is not JSON is left as-is (the preflight blocker reports it).
384
+ const parsed = parseJson(raw);
385
+ if (parsed.ok) {
386
+ const pkg = parsed.value as Record<string, unknown>;
387
+ const bkit = (pkg.bkit ?? {}) as Record<string, unknown>;
388
+ if (persistTracker) bkit.issueTracker = tracker;
389
+ if (persistProvider) {
390
+ bkit.opencodeProvider = opencodeProvider; // active (drives agent frontmatter)
391
+ // Update providers array (deduped).
392
+ const existingArr: string[] = Array.isArray(bkit.opencodeProviders)
393
+ ? [...(bkit.opencodeProviders as string[])]
394
+ : [];
395
+ if (!existingArr.includes(opencodeProvider!)) {
396
+ existingArr.push(opencodeProvider!);
397
+ }
398
+ bkit.opencodeProviders = existingArr;
461
399
  }
462
- bkit.opencodeProviders = existingArr;
463
- }
464
- pkg.bkit = bkit;
465
- const indentMatch = raw.match(/\n([ \t]+)"/);
466
- const indent = indentMatch ? indentMatch[1] : " ";
467
- const trailingNewline = raw.endsWith("\n") ? "\n" : "";
468
- fs.writeFileSync(
469
- consumerPkgPath2,
470
- JSON.stringify(pkg, null, indent) + trailingNewline,
471
- "utf-8",
472
- );
473
- if (persistTracker) {
474
- console.log(`${LOG_PREFIX} Set bkit.issueTracker = "${tracker}" in package.json`);
475
- }
476
- if (persistProvider) {
477
- console.log(
478
- `${LOG_PREFIX} Set bkit.opencodeProvider = "${opencodeProvider}" and updated bkit.opencodeProviders in package.json`,
400
+ pkg.bkit = bkit;
401
+ const indentMatch = raw.match(/\n([ \t]+)"/);
402
+ const indent = indentMatch ? indentMatch[1] : " ";
403
+ const trailingNewline = raw.endsWith("\n") ? "\n" : "";
404
+ fs.writeFileSync(
405
+ consumerPkgPath2,
406
+ JSON.stringify(pkg, null, indent) + trailingNewline,
407
+ "utf-8",
479
408
  );
409
+ if (persistTracker) {
410
+ console.log(`${LOG_PREFIX} Set bkit.issueTracker = "${tracker}" in package.json`);
411
+ }
412
+ if (persistProvider) {
413
+ console.log(
414
+ `${LOG_PREFIX} Set bkit.opencodeProvider = "${opencodeProvider}" and updated bkit.opencodeProviders in package.json`,
415
+ );
416
+ }
480
417
  }
481
418
  } catch {
482
419
  // non-fatal
@@ -484,6 +421,13 @@ export async function runInstall(
484
421
  }
485
422
  }
486
423
 
424
+ // Tracker switch: drop the bkit MCP entries the new tracker does not use, while each still
425
+ // equals bkit's template (an edited one stays, with a warning). Runs before dispatch so the
426
+ // `.bkit/version` hashes core records cover the final file content.
427
+ if (!dryRun && tracker !== null && tracker !== recordedTracker) {
428
+ removeUnusedTrackerMcpEntries(process.cwd(), tracker, dryRun, ws, LOG_PREFIX);
429
+ }
430
+
487
431
  const ctx: ModuleContext = {
488
432
  dryRun,
489
433
  tokens,
@@ -500,7 +444,7 @@ export async function runInstall(
500
444
  issueTracker,
501
445
  cloudVariant,
502
446
  // Stamped into every rendered agent + command as the managed-version marker.
503
- version: getInstallingVersion(),
447
+ version: readInstallingVersion(),
504
448
  // Full host set for this run: dispatched ∪ already-on-disk. Read by the Antigravity
505
449
  // module to warn when Codex is also installed (both hosts' `bkit-*` workflow skills
506
450
  // then surface in Antigravity's skill selector).
@@ -559,16 +503,8 @@ export async function runInstall(
559
503
  // Runs for both install AND check — detectors are read-only, so check produces
560
504
  // the same advisory set as install would emit against the same filesystem state.
561
505
  // Changelog version-delta header (A7): surface what changed in the installed version.
562
- try {
563
- const pkgJsonPath = path.join(getPkgRoot(), "package.json");
564
- if (fs.existsSync(pkgJsonPath)) {
565
- const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8")) as { version?: string };
566
- const deltaHeader = readChangelogVersionDelta(pkg.version ?? "");
567
- if (deltaHeader) console.log(deltaHeader);
568
- }
569
- } catch (err) {
570
- void err; // Non-fatal — changelog header is informational
571
- }
506
+ const deltaHeader = readChangelogVersionDelta(readInstallingVersion());
507
+ if (deltaHeader) console.log(deltaHeader);
572
508
  const suppressed: SuppressionNote[] = [];
573
509
  const postFindings = runPostInstallCheck(ctx.advisories, process.cwd(), suppressed);
574
510
  if (postWriteBlockers.length > 0) {
@@ -589,7 +525,7 @@ export async function runInstall(
589
525
  writeAdvisoriesArtifact(postFindings, {
590
526
  cwd: process.cwd(),
591
527
  mode: upgradeReport ? "upgrade" : dryRun ? "check" : "install",
592
- version: getInstallingVersion(),
528
+ version: readInstallingVersion(),
593
529
  logPrefix: LOG_PREFIX,
594
530
  suppressed,
595
531
  tips,