@cbartens/bkit 0.0.0-stage → 5.0.1

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 (375) hide show
  1. package/.bkit/README.md +417 -0
  2. package/.bkit/core/.agents/context.md +95 -0
  3. package/.bkit/core/.agents/docs.md +62 -0
  4. package/.bkit/core/.agents/rules.md +147 -0
  5. package/.bkit/core/.agents/skills.md +43 -0
  6. package/.bkit/core/.bkit/LICENSE.md +151 -0
  7. package/.bkit/core/.bkit/README.md +417 -0
  8. package/.bkit/core/.bkit/cloud.md +493 -0
  9. package/.bkit/core/.bkit/comparison.md +321 -0
  10. package/.bkit/core/.bkit/install.md +401 -0
  11. package/.bkit/core/.bkit/jsonl.md +497 -0
  12. package/.bkit/core/.bkit/prompts.md +474 -0
  13. package/.bkit/core/.bkit/providers.md +598 -0
  14. package/.bkit/core/.bkit/templates.md +470 -0
  15. package/.bkit/core/.bkit/trackers.md +95 -0
  16. package/.bkit/core/.bkit/workflows.md +1550 -0
  17. package/.bkit/core/.claude/hooks/bkit-gateguard.cjs +82 -0
  18. package/.bkit/core/.claude/hooks/bkit-guardrails.cjs +93 -0
  19. package/.bkit/core/.claude/hooks/bkit-preflight.sh +74 -0
  20. package/.bkit/core/.codex/hooks/bkit-gateguard.cjs +115 -0
  21. package/.bkit/core/.codex/hooks/bkit-guardrails.cjs +135 -0
  22. package/.bkit/core/.cursor/hooks/bkit-gateguard.cjs +111 -0
  23. package/.bkit/core/.cursor/hooks/bkit-guardrails.cjs +169 -0
  24. package/.bkit/core/.cursor/hooks/bkit-preflight.sh +63 -0
  25. package/.bkit/core/.opencode/plugin/bkit-gateguard.js +225 -0
  26. package/.bkit/core/AGENTS.md +77 -0
  27. package/.bkit/core/LICENSE.md +149 -0
  28. package/.bkit/core/docs/templates/bkit-audit-accessibility.md +272 -0
  29. package/.bkit/core/docs/templates/bkit-audit-adoption.md +223 -0
  30. package/.bkit/core/docs/templates/bkit-audit-agents.md +230 -0
  31. package/.bkit/core/docs/templates/bkit-audit-aio-seo.md +227 -0
  32. package/.bkit/core/docs/templates/bkit-audit-analytics.md +266 -0
  33. package/.bkit/core/docs/templates/bkit-audit-annotations.md +230 -0
  34. package/.bkit/core/docs/templates/bkit-audit-app-performance.md +270 -0
  35. package/.bkit/core/docs/templates/bkit-audit-app-store.md +254 -0
  36. package/.bkit/core/docs/templates/bkit-audit-architecture.md +221 -0
  37. package/.bkit/core/docs/templates/bkit-audit-ci-performance.md +241 -0
  38. package/.bkit/core/docs/templates/bkit-audit-config.md +230 -0
  39. package/.bkit/core/docs/templates/bkit-audit-cost.md +218 -0
  40. package/.bkit/core/docs/templates/bkit-audit-coverage.md +221 -0
  41. package/.bkit/core/docs/templates/bkit-audit-database.md +229 -0
  42. package/.bkit/core/docs/templates/bkit-audit-dead-code.md +251 -0
  43. package/.bkit/core/docs/templates/bkit-audit-deferrals.md +283 -0
  44. package/.bkit/core/docs/templates/bkit-audit-dependencies.md +268 -0
  45. package/.bkit/core/docs/templates/bkit-audit-documentation.md +280 -0
  46. package/.bkit/core/docs/templates/bkit-audit-duplication.md +253 -0
  47. package/.bkit/core/docs/templates/bkit-audit-failures.md +259 -0
  48. package/.bkit/core/docs/templates/bkit-audit-flakes.md +237 -0
  49. package/.bkit/core/docs/templates/bkit-audit-gates.md +250 -0
  50. package/.bkit/core/docs/templates/bkit-audit-issues.md +273 -0
  51. package/.bkit/core/docs/templates/bkit-audit-llm.md +239 -0
  52. package/.bkit/core/docs/templates/bkit-audit-logs.md +266 -0
  53. package/.bkit/core/docs/templates/bkit-audit-models.md +210 -0
  54. package/.bkit/core/docs/templates/bkit-audit-privacy.md +211 -0
  55. package/.bkit/core/docs/templates/bkit-audit-prompts.md +216 -0
  56. package/.bkit/core/docs/templates/bkit-audit-red-team.md +220 -0
  57. package/.bkit/core/docs/templates/bkit-audit-reliability.md +222 -0
  58. package/.bkit/core/docs/templates/bkit-audit-repo.md +224 -0
  59. package/.bkit/core/docs/templates/bkit-audit-security.md +274 -0
  60. package/.bkit/core/docs/templates/bkit-audit-slop.md +200 -0
  61. package/.bkit/core/docs/templates/bkit-audit-testing.md +270 -0
  62. package/.bkit/core/docs/templates/bkit-audit-usability.md +237 -0
  63. package/.bkit/core/docs/templates/bkit-plan.md +476 -0
  64. package/.bkit/core/engine/checks/after/auth.ts +149 -0
  65. package/.bkit/core/engine/checks/after/ci-yml.ts +701 -0
  66. package/.bkit/core/engine/checks/after/github.ts +424 -0
  67. package/.bkit/core/engine/checks/after/index.ts +530 -0
  68. package/.bkit/core/engine/checks/after/managed.ts +217 -0
  69. package/.bkit/core/engine/checks/after/suppression.ts +53 -0
  70. package/.bkit/core/engine/checks/after/tools.ts +427 -0
  71. package/.bkit/core/engine/checks/before/context-fetch.ts +429 -0
  72. package/.bkit/core/engine/checks/before/env.ts +187 -0
  73. package/.bkit/core/engine/checks/before/files.ts +204 -0
  74. package/.bkit/core/engine/checks/before/index.ts +206 -0
  75. package/.bkit/core/engine/checks/before/mcp.ts +127 -0
  76. package/.bkit/core/engine/checks/before/repo.ts +77 -0
  77. package/.bkit/core/engine/checks/before/version.ts +67 -0
  78. package/.bkit/core/engine/checks/status-holes.ts +479 -0
  79. package/.bkit/core/engine/cli.ts +867 -0
  80. package/.bkit/core/engine/commands/analysis.ts +550 -0
  81. package/.bkit/core/engine/commands/ci/ci.ts +483 -0
  82. package/.bkit/core/engine/commands/comply.ts +415 -0
  83. package/.bkit/core/engine/commands/harvest.ts +459 -0
  84. package/.bkit/core/engine/commands/install/check.ts +70 -0
  85. package/.bkit/core/engine/commands/install/doctor.ts +254 -0
  86. package/.bkit/core/engine/commands/install/install.ts +550 -0
  87. package/.bkit/core/engine/commands/install/uninstall.ts +995 -0
  88. package/.bkit/core/engine/commands/install/upgrade.ts +1037 -0
  89. package/.bkit/core/engine/commands/launch.ts +139 -0
  90. package/.bkit/core/engine/commands/lib.ts +495 -0
  91. package/.bkit/core/engine/commands/registry.ts +53 -0
  92. package/.bkit/core/engine/commands/setup/setup.ts +184 -0
  93. package/.bkit/core/engine/commands/telemetry.ts +32 -0
  94. package/.bkit/core/engine/commands/workflow/branches.ts +1514 -0
  95. package/.bkit/core/engine/commands/workflow/context.ts +961 -0
  96. package/.bkit/core/engine/commands/workflow/counters.ts +268 -0
  97. package/.bkit/core/engine/commands/workflow/fids.ts +243 -0
  98. package/.bkit/core/engine/commands/workflow/issues.ts +675 -0
  99. package/.bkit/core/engine/commands/workflow/ledgers.ts +575 -0
  100. package/.bkit/core/engine/commands/workflow/locks.ts +478 -0
  101. package/.bkit/core/engine/commands/workflow/prs.ts +1087 -0
  102. package/.bkit/core/engine/commands/workflow/repo.ts +220 -0
  103. package/.bkit/core/engine/commands/workflow/roadmap.ts +576 -0
  104. package/.bkit/core/engine/commands/workflow/runs.ts +2140 -0
  105. package/.bkit/core/engine/commands/workflow/runtime.ts +300 -0
  106. package/.bkit/core/engine/commands/workflow/skills.ts +118 -0
  107. package/.bkit/core/engine/commands/workflow/status.ts +455 -0
  108. package/.bkit/core/engine/commands/workflow/steps.ts +1300 -0
  109. package/.bkit/core/engine/commands/workflow/tests.ts +161 -0
  110. package/.bkit/core/engine/commands/workflow/workflows.ts +1573 -0
  111. package/.bkit/core/engine/commands/workflow/workstreams.ts +164 -0
  112. package/.bkit/core/engine/commands/workflow/worktrees.ts +534 -0
  113. package/.bkit/core/engine/install/cloud-env.ts +924 -0
  114. package/.bkit/core/engine/install/managed-block.ts +212 -0
  115. package/.bkit/core/engine/install/manifest.ts +448 -0
  116. package/.bkit/core/engine/install/modules/claude.ts +362 -0
  117. package/.bkit/core/engine/install/modules/cloud.ts +173 -0
  118. package/.bkit/core/engine/install/modules/codex.ts +219 -0
  119. package/.bkit/core/engine/install/modules/core.ts +620 -0
  120. package/.bkit/core/engine/install/modules/cursor.ts +231 -0
  121. package/.bkit/core/engine/install/modules/index.ts +242 -0
  122. package/.bkit/core/engine/install/modules/opencode.ts +573 -0
  123. package/.bkit/core/engine/install/payload-links.ts +150 -0
  124. package/.bkit/core/engine/install/placeholders.ts +108 -0
  125. package/.bkit/core/engine/install/template-copies.ts +119 -0
  126. package/.bkit/core/engine/install/version-file.ts +284 -0
  127. package/.bkit/core/engine/install/write.ts +467 -0
  128. package/.bkit/core/engine/ledgers/clamp-log.ts +32 -0
  129. package/.bkit/core/engine/ledgers/status.ts +213 -0
  130. package/.bkit/core/engine/ledgers/telemetry.ts +1143 -0
  131. package/.bkit/core/engine/render/hosts.ts +372 -0
  132. package/.bkit/core/engine/render/models.ts +323 -0
  133. package/.bkit/core/engine/render/parts.ts +38 -0
  134. package/.bkit/core/engine/render/trackers.ts +170 -0
  135. package/.bkit/core/engine/shared/egress.ts +345 -0
  136. package/.bkit/core/engine/shared/json.ts +16 -0
  137. package/.bkit/core/engine/shared/log-prefix.ts +9 -0
  138. package/.bkit/core/engine/shared/package-json.ts +29 -0
  139. package/.bkit/core/engine/shared/package-manager.ts +38 -0
  140. package/.bkit/core/engine/shared/package-roots.ts +163 -0
  141. package/.bkit/core/engine/shared/protected-paths.ts +164 -0
  142. package/.bkit/core/engine/shared/settings.ts +72 -0
  143. package/.bkit/core/engine/shared/timeouts.ts +20 -0
  144. package/.bkit/core/engine/tools/wording.ts +748 -0
  145. package/.bkit/core/hooks/gateguard-edit-payload.js +18 -0
  146. package/.bkit/core/hooks/gateguard-engine.js +193 -0
  147. package/.bkit/core/hooks/guardrails-shell.js +154 -0
  148. package/.bkit/core/hooks/guardrails-tiers.js +68 -0
  149. package/.bkit/core/hooks/preflight-linear.sh +73 -0
  150. package/.bkit/core/prompts/bkit-audit-generate.md +66 -0
  151. package/.bkit/core/prompts/bkit-build-challenge-1.md +79 -0
  152. package/.bkit/core/prompts/bkit-build-challenge-2.md +81 -0
  153. package/.bkit/core/prompts/bkit-build-challenge-reconcile.md +68 -0
  154. package/.bkit/core/prompts/bkit-build-challenge-remediate.md +70 -0
  155. package/.bkit/core/prompts/bkit-build-challenge-tests.md +73 -0
  156. package/.bkit/core/prompts/bkit-build-install-skills.md +67 -0
  157. package/.bkit/core/prompts/bkit-build-plan-current.md +75 -0
  158. package/.bkit/core/prompts/bkit-build-plan-execute.md +72 -0
  159. package/.bkit/core/prompts/bkit-build-plan-gaps-remediate.md +73 -0
  160. package/.bkit/core/prompts/bkit-build-plan-verify.md +88 -0
  161. package/.bkit/core/prompts/bkit-build-summary.md +93 -0
  162. package/.bkit/core/prompts/bkit-debug-abort-handover.md +90 -0
  163. package/.bkit/core/prompts/bkit-debug-failure-diagnose.md +151 -0
  164. package/.bkit/core/prompts/bkit-debug-failure-remediate.md +78 -0
  165. package/.bkit/core/prompts/bkit-debug-failure-soak.md +73 -0
  166. package/.bkit/core/prompts/bkit-debug-summary.md +95 -0
  167. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-1.md +72 -0
  168. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-2.md +72 -0
  169. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-3.md +75 -0
  170. package/.bkit/core/prompts/bkit-design-generate.md +110 -0
  171. package/.bkit/core/prompts/bkit-design-organise.md +85 -0
  172. package/.bkit/core/prompts/bkit-design-review-1.md +121 -0
  173. package/.bkit/core/prompts/bkit-design-review-2.md +111 -0
  174. package/.bkit/core/prompts/bkit-design-spike.md +97 -0
  175. package/.bkit/core/prompts/bkit-design-summary.md +106 -0
  176. package/.bkit/core/prompts/bkit-design-update.md +66 -0
  177. package/.bkit/core/prompts/bkit-plan-complexity-check.md +66 -0
  178. package/.bkit/core/prompts/bkit-plan-complexity-split.md +87 -0
  179. package/.bkit/core/prompts/bkit-plan-generate.md +74 -0
  180. package/.bkit/core/prompts/bkit-plan-review-1.md +90 -0
  181. package/.bkit/core/prompts/bkit-plan-review-2.md +80 -0
  182. package/.bkit/core/prompts/bkit-plan-review-3.md +92 -0
  183. package/.bkit/core/prompts/bkit-plan-scope-check-1.md +84 -0
  184. package/.bkit/core/prompts/bkit-plan-scope-check-2.md +84 -0
  185. package/.bkit/core/prompts/bkit-plan-summary.md +106 -0
  186. package/.bkit/core/prompts/bkit-plan-update.md +68 -0
  187. package/.bkit/core/prompts/bkit-ship-conflict-resolve.md +90 -0
  188. package/.bkit/core/prompts/bkit-ship-deferred-sweep.md +94 -0
  189. package/.bkit/core/prompts/bkit-ship-summary.md +77 -0
  190. package/.bkit/core/prompts/bkit-ship-update-docs.md +71 -0
  191. package/.bkit/core/prompts/bkit-utility-compress-markdown.md +91 -0
  192. package/.bkit/core/prompts/bkit-utility-update-decisions.md +75 -0
  193. package/.bkit/core/prompts/bkit-utility-update-deferrals.md +76 -0
  194. package/.bkit/core/prompts/bkit-utility-update-documentation.md +61 -0
  195. package/.bkit/core/prompts/bkit-utility-update-failures.md +76 -0
  196. package/.bkit/core/prompts/bkit-utility-update-knowledge.md +75 -0
  197. package/.bkit/core/prompts/bkit-utility-update-roadmap.md +82 -0
  198. package/.bkit/core/workflows/bkit-auto.md +121 -0
  199. package/.bkit/core/workflows/bkit-build.md +802 -0
  200. package/.bkit/core/workflows/bkit-debug.md +649 -0
  201. package/.bkit/core/workflows/bkit-design.md +495 -0
  202. package/.bkit/core/workflows/bkit-plan.md +510 -0
  203. package/.bkit/core/workflows/bkit-ship.md +650 -0
  204. package/.bkit/shared/.bkit/.env.example +86 -0
  205. package/.bkit/shared/.claude/bkit-cloud.md +117 -0
  206. package/.bkit/shared/.claude/settings.json +88 -0
  207. package/.bkit/shared/.codex/hooks.json +26 -0
  208. package/.bkit/shared/.cursor/bkit-cloud.md +84 -0
  209. package/.bkit/shared/.cursor/hooks.json +26 -0
  210. package/.bkit/shared/.cursor/mcp.json +15 -0
  211. package/.bkit/shared/.cursor/rules/bkit-rules.mdc +12 -0
  212. package/.bkit/shared/.gitattributes +37 -0
  213. package/.bkit/shared/.mcp.json +16 -0
  214. package/.bkit/shared/.opencode/opencode.json +62 -0
  215. package/.bkit/shared/CLAUDE.md +11 -0
  216. package/.bkit/shared/LICENSE.md +22 -0
  217. package/.bkit/shared/docs/architecture.md +19 -0
  218. package/.bkit/shared/docs/data-schema.md +19 -0
  219. package/.bkit/shared/docs/deployments.md +433 -0
  220. package/.bkit/shared/docs/design.md +39 -0
  221. package/.bkit/shared/docs/noise.md +19 -0
  222. package/.bkit/shared/docs/observability.md +25 -0
  223. package/.bkit/shared/docs/security.md +42 -0
  224. package/.bkit/shared/docs/setup.md +30 -0
  225. package/.bkit/shared/docs/technology.md +18 -0
  226. package/.bkit/shared/docs/testing.md +413 -0
  227. package/.bkit/shared/gitignore +97 -0
  228. package/.bkit/templates/.claude/hooks/bkit-cloud-bootstrap.sh +187 -0
  229. package/.bkit/templates/.cursor/Dockerfile +68 -0
  230. package/.bkit/templates/.cursor/environment.json +6 -0
  231. package/.bkit/templates/.cursor/settings.json +11 -0
  232. package/.bkit/templates/.envrc +7 -0
  233. package/.bkit/templates/.github/dependabot.yml +52 -0
  234. package/.bkit/templates/.github/renovate.json5 +224 -0
  235. package/.bkit/templates/.github/workflows/bkit-ci.yml +1062 -0
  236. package/.bkit/templates/.github/workflows/bkit-cloud.yml +564 -0
  237. package/.bkit/templates/.github/workflows/bkit-renovate.yml +102 -0
  238. package/.bkit/templates/.graphifyignore +108 -0
  239. package/.bkit/templates/.husky/pre-commit +16 -0
  240. package/.bkit/templates/.husky/pre-push +91 -0
  241. package/.bkit/templates/.lintstagedrc.json +3 -0
  242. package/.bkit/templates/.prettierignore +41 -0
  243. package/.bkit/templates/.serena/project.yml +243 -0
  244. package/.bkit/templates/.vale/styles/House/Filler.yml +11 -0
  245. package/.bkit/templates/.vale/styles/House/Hedging.yml +12 -0
  246. package/.bkit/templates/.vale/styles/House/LLMSlop.yml +13 -0
  247. package/.bkit/templates/.vale/styles/House/Verbose.yml +12 -0
  248. package/.bkit/templates/.vale.ini +67 -0
  249. package/.bkit/templates/LICENSE.md +22 -0
  250. package/.bkit/templates/context/bkit-cloudrun.md +84 -0
  251. package/.bkit/templates/context/bkit-expo.md +186 -0
  252. package/.bkit/templates/context/bkit-github.md +79 -0
  253. package/.bkit/templates/context/bkit-inngest.md +45 -0
  254. package/.bkit/templates/context/bkit-maestro.md +76 -0
  255. package/.bkit/templates/context/bkit-playwright.md +62 -0
  256. package/.bkit/templates/context/bkit-resend.md +36 -0
  257. package/.bkit/templates/context/bkit-sentry.md +65 -0
  258. package/.bkit/templates/context/bkit-serena.md +33 -0
  259. package/.bkit/templates/context/bkit-stripe.md +36 -0
  260. package/.bkit/templates/context/bkit-supabase.md +59 -0
  261. package/.bkit/templates/context/bkit-tavily.md +49 -0
  262. package/.bkit/templates/context/bkit-vercel.md +64 -0
  263. package/.bkit/templates/docs/capabilities.md +72 -0
  264. package/.bkit/templates/scripts/ci/accepted-divergences.json +21 -0
  265. package/.bkit/templates/scripts/ci/check-accepted-divergences.mjs +152 -0
  266. package/.bkit/templates/scripts/ci/check-accepted-divergences.test.mjs +130 -0
  267. package/.bkit/templates/scripts/ci/lanes/resolve-push-base.mjs +98 -0
  268. package/.bkit/templates/scripts/ci/lanes/resolve-push-base.test.mjs +86 -0
  269. package/.bkit/templates/scripts/graphify/bkit-graph-drift-rules.json +23 -0
  270. package/.bkit/templates/scripts/graphify/bkit-graph-drift.py +208 -0
  271. package/.bkit/templates/scripts/graphify/bkit-graph-eager-rules.json +35 -0
  272. package/.bkit/templates/scripts/graphify/bkit-graph-eager.py +236 -0
  273. package/.bkit/templates/scripts/graphify/bkit-graph-external-rules.json +17 -0
  274. package/.bkit/templates/scripts/graphify/bkit-graph-imports.py +290 -0
  275. package/.bkit/templates/scripts/graphify/bkit-graph-pre-push.sh +19 -0
  276. package/.bkit/templates/scripts/graphify/bkit-graph-repair.py +120 -0
  277. package/.bkit/templates/scripts/graphify/bkit-graph-surface.py +232 -0
  278. package/.bkit/templates/scripts/graphify/bkit-graphify-graphml.py +36 -0
  279. package/.bkit/templates/scripts/graphify/bkit-graphify-rollup.py +927 -0
  280. package/.bkit/templates/scripts/graphify/bkit-graphify.sh +351 -0
  281. package/.bkit/templates/scripts/launchd/ai.bkit.launchd.plist +26 -0
  282. package/.bkit/templates/scripts/launchd/bkit-launchd-env.sh +53 -0
  283. package/.bkit/templates/scripts/launchd/launchd.env.example +13 -0
  284. package/.bkit/templates/scripts/prettier/bkit-insert-ignore.mjs +275 -0
  285. package/.bkit/templates/scripts/secrets/bkit-secrets.config.mjs +66 -0
  286. package/.bkit/templates/scripts/secrets/bkit-secrets.mjs +785 -0
  287. package/.bkit/templates/scripts/serena/bkit-serena-mcp.sh +30 -0
  288. package/.bkit/templates/scripts/serena/bkit-serena.mcp.json +9 -0
  289. package/.bkit/templates/scripts/slop/bkit-slop-type-suppression.mjs +286 -0
  290. package/.bkit/templates/slop-scan.config.json +120 -0
  291. package/CHANGELOG.md +1503 -0
  292. package/LICENSE.md +161 -0
  293. package/README.md +184 -2
  294. package/dist/checks/after/auth.js +140 -0
  295. package/dist/checks/after/ci-yml.js +681 -0
  296. package/dist/checks/after/github.js +400 -0
  297. package/dist/checks/after/index.js +431 -0
  298. package/dist/checks/after/managed.js +187 -0
  299. package/dist/checks/after/suppression.js +39 -0
  300. package/dist/checks/after/tools.js +351 -0
  301. package/dist/checks/before/context-fetch.js +426 -0
  302. package/dist/checks/before/env.js +163 -0
  303. package/dist/checks/before/files.js +187 -0
  304. package/dist/checks/before/index.js +162 -0
  305. package/dist/checks/before/mcp.js +111 -0
  306. package/dist/checks/before/repo.js +71 -0
  307. package/dist/checks/before/version.js +60 -0
  308. package/dist/checks/status-holes.js +393 -0
  309. package/dist/cli.js +688 -0
  310. package/dist/commands/analysis.js +325 -0
  311. package/dist/commands/ci/ci.js +438 -0
  312. package/dist/commands/comply.js +310 -0
  313. package/dist/commands/harvest.js +354 -0
  314. package/dist/commands/install/check.js +61 -0
  315. package/dist/commands/install/doctor.js +209 -0
  316. package/dist/commands/install/install.js +475 -0
  317. package/dist/commands/install/uninstall.js +839 -0
  318. package/dist/commands/install/upgrade.js +782 -0
  319. package/dist/commands/launch.js +118 -0
  320. package/dist/commands/lib.js +363 -0
  321. package/dist/commands/registry.js +50 -0
  322. package/dist/commands/setup/setup.js +158 -0
  323. package/dist/commands/telemetry.js +29 -0
  324. package/dist/commands/workflow/branches.js +1265 -0
  325. package/dist/commands/workflow/context.js +846 -0
  326. package/dist/commands/workflow/counters.js +227 -0
  327. package/dist/commands/workflow/fids.js +224 -0
  328. package/dist/commands/workflow/issues.js +585 -0
  329. package/dist/commands/workflow/ledgers.js +472 -0
  330. package/dist/commands/workflow/locks.js +364 -0
  331. package/dist/commands/workflow/prs.js +904 -0
  332. package/dist/commands/workflow/repo.js +199 -0
  333. package/dist/commands/workflow/roadmap.js +495 -0
  334. package/dist/commands/workflow/runs.js +1717 -0
  335. package/dist/commands/workflow/runtime.js +257 -0
  336. package/dist/commands/workflow/skills.js +110 -0
  337. package/dist/commands/workflow/status.js +392 -0
  338. package/dist/commands/workflow/steps.js +1093 -0
  339. package/dist/commands/workflow/tests.js +144 -0
  340. package/dist/commands/workflow/workflows.js +1342 -0
  341. package/dist/commands/workflow/workstreams.js +141 -0
  342. package/dist/commands/workflow/worktrees.js +473 -0
  343. package/dist/install/cloud-env.js +818 -0
  344. package/dist/install/managed-block.js +182 -0
  345. package/dist/install/manifest.js +376 -0
  346. package/dist/install/modules/claude.js +288 -0
  347. package/dist/install/modules/cloud.js +151 -0
  348. package/dist/install/modules/codex.js +193 -0
  349. package/dist/install/modules/core.js +531 -0
  350. package/dist/install/modules/cursor.js +186 -0
  351. package/dist/install/modules/index.js +152 -0
  352. package/dist/install/modules/opencode.js +504 -0
  353. package/dist/install/payload-links.js +119 -0
  354. package/dist/install/placeholders.js +87 -0
  355. package/dist/install/template-copies.js +106 -0
  356. package/dist/install/version-file.js +204 -0
  357. package/dist/install/write.js +342 -0
  358. package/dist/ledgers/clamp-log.js +27 -0
  359. package/dist/ledgers/status.js +156 -0
  360. package/dist/ledgers/telemetry.js +637 -0
  361. package/dist/render/hosts.js +339 -0
  362. package/dist/render/models.js +288 -0
  363. package/dist/render/parts.js +33 -0
  364. package/dist/render/trackers.js +135 -0
  365. package/dist/shared/egress.js +299 -0
  366. package/dist/shared/json.js +16 -0
  367. package/dist/shared/log-prefix.js +7 -0
  368. package/dist/shared/package-json.js +22 -0
  369. package/dist/shared/package-manager.js +32 -0
  370. package/dist/shared/package-roots.js +145 -0
  371. package/dist/shared/protected-paths.js +153 -0
  372. package/dist/shared/settings.js +44 -0
  373. package/dist/shared/timeouts.js +19 -0
  374. package/dist/tools/wording.js +680 -0
  375. package/package.json +101 -4
@@ -0,0 +1,573 @@
1
+ // install/modules/opencode.ts — the opencode host module: `.opencode/` agents and commands, and
2
+ // the provider-templated `opencode.json`.
3
+
4
+ import * as fs from "node:fs";
5
+ import * as path from "node:path";
6
+ import { writeTier2, deepMergeJson, ensureDir, type WriteSet, type JsonValue } from "../write.js";
7
+ import { stampManagedVersion } from "../version-file.js";
8
+ import { applyOpencodeAgent, applyOpencodeCommand } from "../../render/hosts.js";
9
+ import type { PreflightFinding } from "../../checks/before/index.js";
10
+ import {
11
+ getCoreRoot,
12
+ getSharedRoot,
13
+ getWorkflowsRoot,
14
+ getPromptsRoot,
15
+ } from "../../shared/package-roots.js";
16
+ import { type IssueTracker, DEFAULT_ISSUE_TRACKER } from "../manifest.js";
17
+ import { substituteWorkflow } from "../../render/trackers.js";
18
+ import { resolveNetworkAllowlist, extractMcpServers, networkAllowlistBases } from "../cloud-env.js";
19
+ import { OPENCODE_PROVIDER_DEFAULTS } from "../../shared/egress.js";
20
+ import type { ModuleContext } from "./index.js";
21
+ import {
22
+ BKIT_PREFIX,
23
+ installFile,
24
+ listMdFiles,
25
+ readInstallingVersion,
26
+ trackMirrorWrite,
27
+ } from "./core.js";
28
+ import { opencodeProviderId } from "../../render/models.js";
29
+
30
+ export function renderOpencodeAgentsCommands(ctx: ModuleContext): string[] {
31
+ const { dryRun, opencodeModelLookup, effortMap, opencodeProvider, logPrefix } = ctx;
32
+ const tag = dryRun ? "[dry-run] " : "";
33
+ const issueTracker = ctx.issueTracker ?? DEFAULT_ISSUE_TRACKER;
34
+ const version = ctx.version ?? readInstallingVersion();
35
+ const written: string[] = [];
36
+ // The opencode host cannot render without a provider — `--opencode=<provider>` is
37
+ // unary at the CLI; an absent value here throws.
38
+ if (!opencodeProvider) {
39
+ throw new Error("renderOpencodeAgentsCommands: ctx.opencodeProvider is required");
40
+ }
41
+ const provider = opencodeProvider;
42
+
43
+ // --- Agents: tracker substitution → applyOpencodeAgent → managed-version stamp (last) ---
44
+ {
45
+ const src = getPromptsRoot();
46
+ const dest = ".opencode/agents";
47
+ // dryRun-gated so `bkit check` stays read-only — installFile/removeOrphans below are
48
+ // already dryRun-guarded, so the dir is only needed for a real write.
49
+ if (!dryRun) ensureDir(dest);
50
+ const files = listMdFiles(src, BKIT_PREFIX);
51
+ const sourceSet = new Set(files);
52
+
53
+ for (const f of files) {
54
+ const transform = (c: string) =>
55
+ stampManagedVersion(
56
+ applyOpencodeAgent(
57
+ substituteWorkflow(c, issueTracker).output,
58
+ opencodeModelLookup,
59
+ effortMap,
60
+ provider,
61
+ ),
62
+ version,
63
+ );
64
+ if (installFile(path.join(src, f), path.join(dest, f), transform, dryRun)) {
65
+ const rel = path.join(dest, f);
66
+ console.log(`${logPrefix} ${tag}write: ${rel}`);
67
+ trackMirrorWrite(ctx, rel, written);
68
+ }
69
+ }
70
+
71
+ // Remove orphan agent files (bkit-prefix only, no allowlist gate needed — .opencode/agents
72
+ // is a dynamic mirror dir like .claude/agents and .cursor/agents). dryRun still reports
73
+ // would-deletes into ws.writes so `bkit check` fails on stale mirrors.
74
+ if (fs.existsSync(dest)) {
75
+ for (const f of fs.readdirSync(dest)) {
76
+ if (!f.endsWith(".md")) continue;
77
+ if (!f.startsWith(BKIT_PREFIX)) continue;
78
+ if (!sourceSet.has(f)) {
79
+ const rel = path.join(dest, f);
80
+ if (!dryRun) fs.rmSync(path.join(dest, f));
81
+ console.log(`${logPrefix} ${tag}delete orphan: ${rel}`);
82
+ trackMirrorWrite(ctx, rel, written);
83
+ }
84
+ }
85
+ }
86
+ }
87
+
88
+ // --- Commands: tracker substitution + applyOpencodeCommand ---
89
+ {
90
+ const src = getWorkflowsRoot();
91
+ const dest = ".opencode/commands";
92
+ // dryRun-gated (see .opencode/agents above) — `bkit check` stays read-only.
93
+ if (!dryRun) ensureDir(dest);
94
+ const files = listMdFiles(src, BKIT_PREFIX);
95
+ const sourceSet = new Set(files);
96
+
97
+ for (const f of files) {
98
+ const transform = (c: string) =>
99
+ stampManagedVersion(
100
+ applyOpencodeCommand(substituteWorkflow(c, issueTracker).output),
101
+ version,
102
+ );
103
+ if (installFile(path.join(src, f), path.join(dest, f), transform, dryRun)) {
104
+ const rel = path.join(dest, f);
105
+ console.log(`${logPrefix} ${tag}write: ${rel}`);
106
+ trackMirrorWrite(ctx, rel, written);
107
+ }
108
+ }
109
+
110
+ if (fs.existsSync(dest)) {
111
+ for (const f of fs.readdirSync(dest)) {
112
+ if (!f.endsWith(".md")) continue;
113
+ if (!f.startsWith(BKIT_PREFIX)) continue;
114
+ if (!sourceSet.has(f)) {
115
+ const rel = path.join(dest, f);
116
+ if (!dryRun) fs.rmSync(path.join(dest, f));
117
+ console.log(`${logPrefix} ${tag}delete orphan: ${rel}`);
118
+ trackMirrorWrite(ctx, rel, written);
119
+ }
120
+ }
121
+ }
122
+ }
123
+ return written;
124
+ }
125
+
126
+ // ---------------------------------------------------------------------------
127
+ // runOpencodeModule — opencode provider-agnostic host (opt-in via --opencode=<provider>)
128
+ //
129
+ // Additive alongside always-on claude/cursor: .opencode/agents/*.md (via applyOpencodeAgent),
130
+ // .opencode/commands/*.md (via applyOpencodeCommand + tracker substitution),
131
+ // .opencode/opencode.json (Tier-2 deep-contained), .opencode/plugin/bkit-gateguard.js (Tier-1).
132
+ //
133
+ // Dir names: .opencode/agents/ and .opencode/commands/ (plural) per opencode.ai/docs.
134
+ // Spike initially observed singular; official docs are plural — plural is correct.
135
+ // .opencode/plugin/ (singular, not plugins/)
136
+ //
137
+ // Provider config is seeded into opencode.json; consumers tune model IDs and provider
138
+ // keys after install. opencode.json is Tier-2 deep-contained (ownership-mixed) so bkit
139
+ // reconverges its keys on upgrade without clobbering consumer additions (KN-17).
140
+ // ---------------------------------------------------------------------------
141
+
142
+ /**
143
+ * The provider secret env var name for a bkit provider key (`BKIT_OPENROUTER_API_KEY`,
144
+ * `BKIT_OPENCODE_API_KEY`, ...). Empty string for an unknown key. Mirrors
145
+ * {@link opencodeProviderId}; used to compare a self-installed opencode.yml handler's `env:`
146
+ * secret against the installed provider.
147
+ */
148
+ export function opencodeProviderEnvVar(providerKey: string): string {
149
+ return OPENCODE_PROVIDER_DEFAULTS[providerKey]?.envVar ?? "";
150
+ }
151
+
152
+ /**
153
+ * Detect drift between the self-installed `.github/workflows/opencode.yml` reactive `/oc` handler
154
+ * and the installed opencode provider.
155
+ *
156
+ * opencode.yml is NOT bkit-managed: it is self-installed via `opencode github install` (the `/oc`
157
+ * | `/opencode` comment handler — the opencode analogue of the App-generated claude.yml). It
158
+ * HARDCODES two provider-specific values: a `model: <providerId>/<model>` line and a provider
159
+ * secret `env:` var. `bkit install --opencode=<provider>` rewrites neither; this advisory names
160
+ * the hand-edit.
161
+ *
162
+ * bkit-cloud.yml runs `opencode run` against the committed `.opencode/` config and fans one
163
+ * `OPENCODE_PROVIDER_KEY` secret out to every provider env var, so it is not checked.
164
+ *
165
+ * No-op when opencode.yml is absent. No equivalent check for claude.yml (provider-fixed to
166
+ * Claude). Severity "advisory": never gates `bkit check`; surfaces on both check and postinstall.
167
+ */
168
+ export function detectOpencodeYmlProviderDrift(cwd: string): PreflightFinding | null {
169
+ const ymlPath = path.join(cwd, ".github", "workflows", "opencode.yml");
170
+ if (!fs.existsSync(ymlPath)) return null;
171
+
172
+ // The installed provider — package.json `bkit.opencodeProvider` is the authoritative record
173
+ // written on install. Absent/broken → the null-guard below skips silently.
174
+ let provider: string | null = null;
175
+ const pkgPath = path.join(cwd, "package.json");
176
+ if (fs.existsSync(pkgPath)) {
177
+ try {
178
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8")) as {
179
+ bkit?: { opencodeProvider?: unknown };
180
+ };
181
+ if (typeof pkg.bkit?.opencodeProvider === "string") provider = pkg.bkit.opencodeProvider;
182
+ } catch {
183
+ // Malformed package.json — leave provider null; a JSON-drift advisory covers that separately.
184
+ }
185
+ }
186
+ if (!provider) return null;
187
+
188
+ const expectedPrefix = opencodeProviderId(provider); // model id form is `<providerId>/<model>`
189
+ const expectedEnvVar = opencodeProviderEnvVar(provider);
190
+ if (!expectedEnvVar) return null; // unknown provider key — no registry entry to compare against.
191
+
192
+ const content = fs.readFileSync(ymlPath, "utf-8");
193
+
194
+ // The handler's hardcoded `model: <providerId>/<model>` line (opencode `run --model`).
195
+ const ymlModel = content.match(/^\s*model:\s*(\S+)/m)?.[1] ?? null;
196
+ const ymlPrefix = ymlModel ? ymlModel.split("/")[0] : null;
197
+ // The handler's hardcoded provider secret — an `env:` key like BKIT_OPENROUTER_API_KEY / BKIT_OPENCODE_API_KEY.
198
+ const ymlEnvVar = content.match(/^\s*([A-Z][A-Z0-9_]*(?:_API_KEY|_HOST)):/m)?.[1] ?? null;
199
+
200
+ // Two independent drift signals. The model prefix is the precise one (it distinguishes the two
201
+ // opencode gateways that share BKIT_OPENCODE_API_KEY); the env var catches a wrong/absent secret.
202
+ const modelDrift = ymlPrefix !== null && ymlPrefix !== expectedPrefix;
203
+ const envDrift = !new RegExp(`\\b${expectedEnvVar}\\b`).test(content);
204
+ if (!modelDrift && !envDrift) return null;
205
+
206
+ const foundModel = ymlModel ?? "(none)";
207
+ const foundEnvVar = ymlEnvVar ?? "(none)";
208
+
209
+ return {
210
+ id: "opencode-yml-provider-drift",
211
+ severity: "advisory",
212
+ detect:
213
+ ".github/workflows/opencode.yml (the self-installed /oc handler, NOT bkit-managed) is pinned to model `" +
214
+ foundModel +
215
+ "` with secret `" +
216
+ foundEnvVar +
217
+ "`, but the installed opencode provider is `" +
218
+ provider +
219
+ "` (model ids `" +
220
+ expectedPrefix +
221
+ "/<model>`, secret `" +
222
+ expectedEnvVar +
223
+ "`). The reactive handler will call the wrong provider's model and/or authenticate with the wrong (likely-unset) secret. bkit install does not rewrite this file.",
224
+ action:
225
+ "opencode.yml is self-installed via `opencode github install` and is NOT bkit-managed, so `bkit install --opencode=<provider>` will not fix it. Hand-edit the two hardcoded lines to match the installed provider:\n\n" +
226
+ " env:\n" +
227
+ " " +
228
+ expectedEnvVar +
229
+ ": ${{ secrets." +
230
+ expectedEnvVar +
231
+ " }}\n" +
232
+ " with:\n" +
233
+ " model: " +
234
+ expectedPrefix +
235
+ "/<model> # a model your provider serves (see .opencode/opencode.json)\n\n" +
236
+ "Keep opencode.yml in sync with .opencode/opencode.json on every provider switch. (bkit-cloud.yml needs no such edit: it reads .opencode/ and fans OPENCODE_PROVIDER_KEY out to every provider env var.)",
237
+ };
238
+ }
239
+
240
+ // Sentinel values used in the opencode.json template for tracker-filtered mcp injection.
241
+ const OPENCODE_TRACKER_MCP_SENTINEL = '"@@OPENCODE_TRACKER_MCP@@": {}';
242
+
243
+ const OPENCODE_PROVIDER_KEY_SENTINEL = "@@OPENCODE_PROVIDER_KEY@@";
244
+
245
+ const OPENCODE_PROVIDER_ENV_VAR_SENTINEL = "@@OPENCODE_PROVIDER_ENV_VAR@@";
246
+
247
+ const OPENCODE_PROVIDER_BASE_URL_SENTINEL = "@@OPENCODE_PROVIDER_BASE_URL@@";
248
+
249
+ /**
250
+ * Render the opencode.json template with tracker-filtered mcp + provider config.
251
+ * Resolves the @@OPENCODE_TRACKER_MCP@@ sentinel into the correct mcpServers entry
252
+ * and fills in provider-specific fields.
253
+ *
254
+ * Also injects `curl *{domain}*` / `wget *{domain}*` **allow** rules (after the
255
+ * template's `curl *` / `wget *` ask) for {@link resolveNetworkAllowlist} so
256
+ * context-fetch / provider hosts do not prompt (OpenCode bash is last-match-wins), and
257
+ * `<server>_*` allows for the consumer's `mcp-servers` block.
258
+ */
259
+ export function renderOpencodeJson(
260
+ templatePath: string,
261
+ tracker: IssueTracker,
262
+ provider: string,
263
+ cwd: string = process.cwd(),
264
+ ): string {
265
+ const raw = fs.readFileSync(templatePath, "utf-8");
266
+ // Strict allowlist: provider is gated to OPENCODE_PROVIDERS at the CLI, so it is always
267
+ // a known key here. An unknown id throws; no substitute provider.
268
+ const providerDefaults = OPENCODE_PROVIDER_DEFAULTS[provider];
269
+ if (!providerDefaults) {
270
+ throw new Error(`renderOpencodeJson: unknown opencode provider ${JSON.stringify(provider)}`);
271
+ }
272
+
273
+ // Resolve tracker MCP entry
274
+ // opencode's MCP schema differs from Claude/Cursor: remote servers use type "remote"
275
+ // (NOT "http" — not in opencode's enum, which makes the whole config invalid) and read
276
+ // secrets via {env:VAR} (NOT shell ${VAR}). See https://opencode.ai/config.json.
277
+ let mcpEntry: string;
278
+ if (tracker === "github") {
279
+ mcpEntry =
280
+ '"github": { "type": "remote", "url": "https://api.githubcopilot.com/mcp/", "headers": { "Authorization": "Bearer {env:BKIT_GH_TOKEN}", "X-MCP-Toolsets": "context,issues,pull_requests,repos,users,projects" } }';
281
+ } else if (tracker === "linear") {
282
+ mcpEntry = '"linear": { "type": "remote", "url": "https://mcp.linear.app/mcp" }';
283
+ } else {
284
+ mcpEntry = "";
285
+ }
286
+
287
+ let rendered = raw.replace(OPENCODE_TRACKER_MCP_SENTINEL, mcpEntry);
288
+ rendered = rendered.replace(OPENCODE_PROVIDER_KEY_SENTINEL, provider);
289
+ rendered = rendered.replace(OPENCODE_PROVIDER_ENV_VAR_SENTINEL, providerDefaults.envVar);
290
+ rendered = rendered.replace(OPENCODE_PROVIDER_BASE_URL_SENTINEL, providerDefaults.baseURL);
291
+
292
+ // Parse + re-serialize to normalise JSON (removes empty mcp entry if tracker=none)
293
+ try {
294
+ const parsed = JSON.parse(rendered) as Record<string, unknown>;
295
+ // Clean up empty mcp object when tracker=none
296
+ const mcp = parsed.mcp as Record<string, unknown> | undefined;
297
+ if (mcp && Object.keys(mcp).length === 0) {
298
+ delete parsed.mcp;
299
+ }
300
+ // Built-in opencode gateways (Zen/Go): opencode resolves the provider from models.dev,
301
+ // so there is no npm driver and no baseURL — emit a minimal block keyed by the
302
+ // opencode-side provider id carrying only the apiKey ({env:BKIT_OPENCODE_API_KEY}).
303
+ if (providerDefaults.builtin) {
304
+ const pid = providerDefaults.providerId ?? provider;
305
+ const options: Record<string, unknown> = { apiKey: providerDefaults.apiPlaceholder };
306
+ (parsed.provider as Record<string, unknown>) = { [pid]: { options } };
307
+ injectOpencodeDerivedAllows(parsed, cwd);
308
+ return JSON.stringify(parsed, null, 2) + "\n";
309
+ }
310
+ // For npm-based custom providers (e.g. openrouter), replace the api-openai provider
311
+ // block with the npm-driver format: { npm, name, options: { baseURL, apiKey } }
312
+ if (providerDefaults.npmPackage) {
313
+ // Every custom provider carries an apiKey as {env:VAR}. opencode rejects an unknown
314
+ // "api" field, so the block is rebuilt to the npm-driver shape only.
315
+ const options: Record<string, unknown> = {
316
+ baseURL: providerDefaults.baseURL,
317
+ apiKey: providerDefaults.apiPlaceholder,
318
+ };
319
+ (parsed.provider as Record<string, unknown>) = {
320
+ [provider]: {
321
+ npm: providerDefaults.npmPackage,
322
+ name: providerDefaults.displayName ?? provider,
323
+ options,
324
+ },
325
+ };
326
+ }
327
+ injectOpencodeDerivedAllows(parsed, cwd);
328
+ return JSON.stringify(parsed, null, 2) + "\n";
329
+ } catch {
330
+ return rendered;
331
+ }
332
+ }
333
+
334
+ /**
335
+ * Append curl/wget domain allows after template ask rules (OpenCode last-match-wins),
336
+ * and one `<server>_*` allow per declared MCP server — opencode names MCP tools
337
+ * `<server>_<tool>` and matches permission keys as globs against the tool name.
338
+ */
339
+ function injectOpencodeDerivedAllows(parsed: Record<string, unknown>, cwd: string): void {
340
+ const permission = parsed.permission as Record<string, unknown> | undefined;
341
+ if (!permission) return;
342
+ for (const server of extractMcpServers(cwd)) {
343
+ permission[`${server}_*`] = "allow";
344
+ }
345
+ if (typeof permission.bash !== "object" || permission.bash === null) return;
346
+ const bash = permission.bash as Record<string, string>;
347
+ for (const base of networkAllowlistBases(resolveNetworkAllowlist(cwd))) {
348
+ bash[`curl *${base}*`] = "allow";
349
+ bash[`wget *${base}*`] = "allow";
350
+ }
351
+ }
352
+
353
+ /**
354
+ * Reconcile a consumer's `.opencode/opencode.json` against the freshly-rendered template.
355
+ * Install is additive across provider keys: existing blocks for other providers
356
+ * (bkit-seeded or consumer-added) are preserved. For each provider key present in the
357
+ * template, the block is REPLACED (not deep-merged), so a hijacked npm/baseURL does not
358
+ * survive reinstall.
359
+ *
360
+ * To remove a stale bkit provider block (e.g. after switching providers), use the surgical
361
+ * `removeOpencodeProviderBlock` below — called by the uninstall path, not install.
362
+ *
363
+ * Mirrors writeTier2JsonContained's contract (first-install write, idempotent no-op via
364
+ * final equality, else merge), so dry-run and real runs compute the identical writeset.
365
+ */
366
+ export function reconcileOpencodeJson(
367
+ renderedTemplate: string,
368
+ relDst: string,
369
+ dryRun: boolean,
370
+ ws: WriteSet,
371
+ ): void {
372
+ const template = JSON.parse(renderedTemplate) as Record<string, JsonValue>;
373
+
374
+ if (!fs.existsSync(relDst)) {
375
+ // First install — write template as-is.
376
+ if (!dryRun) {
377
+ ensureDir(path.dirname(relDst));
378
+ fs.writeFileSync(relDst, JSON.stringify(template, null, 2) + "\n", "utf-8");
379
+ }
380
+ ws.writes.push({ path: relDst, action: "write" });
381
+ return;
382
+ }
383
+
384
+ // Parse into a sentinel and validate AFTER the catch — keeping the throw out of the catch
385
+ // body (matches the codebase's parse-or-sentinel idiom; a throw inside catch obscures the
386
+ // original error). Malformed consumer JSON is a hard stop: refuse to merge, require a fix.
387
+ let existing: Record<string, JsonValue> | null = null;
388
+ try {
389
+ existing = JSON.parse(fs.readFileSync(relDst, "utf-8")) as Record<string, JsonValue>;
390
+ } catch {
391
+ existing = null;
392
+ }
393
+ if (existing === null) {
394
+ throw new Error(
395
+ `[bkit] ${relDst} is malformed JSON — fix manually before running bkit install`,
396
+ );
397
+ }
398
+
399
+ const merged = deepMergeJson(existing as JsonValue, template as JsonValue) as Record<
400
+ string,
401
+ JsonValue
402
+ >;
403
+ // Replace (do not deep-merge) each provider key the template seeds — heals hijacked
404
+ // npm/baseURL/name while keeping other provider keys additive.
405
+ //
406
+ // Preserve the EXISTING file's provider key order (deepMergeJson orders template keys
407
+ // first, which is right for last-match-wins maps like permission.bash): rebuild from the
408
+ // existing order — replacing template-seeded blocks in place — then append new template
409
+ // providers in template order. Install and upgrade render the same order.
410
+ const tmplProv = template.provider;
411
+ if (tmplProv !== null && typeof tmplProv === "object" && !Array.isArray(tmplProv)) {
412
+ const tmplProvObj = tmplProv as Record<string, JsonValue>;
413
+ const existingProvObj =
414
+ existing.provider !== null &&
415
+ typeof existing.provider === "object" &&
416
+ !Array.isArray(existing.provider)
417
+ ? (existing.provider as Record<string, JsonValue>)
418
+ : ({} as Record<string, JsonValue>);
419
+ const orderedProv: Record<string, JsonValue> = {};
420
+ for (const k of Object.keys(existingProvObj)) {
421
+ orderedProv[k] =
422
+ k in tmplProvObj
423
+ ? (JSON.parse(JSON.stringify(tmplProvObj[k])) as JsonValue)
424
+ : existingProvObj[k];
425
+ }
426
+ for (const [k, v] of Object.entries(tmplProvObj)) {
427
+ if (!(k in orderedProv)) orderedProv[k] = JSON.parse(JSON.stringify(v)) as JsonValue;
428
+ }
429
+ merged.provider = orderedProv;
430
+ }
431
+
432
+ const mergedStr = JSON.stringify(merged, null, 2) + "\n";
433
+ if (mergedStr === fs.readFileSync(relDst, "utf-8")) return; // idempotent guard
434
+ if (!dryRun) {
435
+ fs.writeFileSync(relDst, mergedStr, "utf-8");
436
+ }
437
+ ws.writes.push({ path: relDst, action: "update" });
438
+ }
439
+
440
+ /**
441
+ * Remove exactly one bkit-seeded provider block from the consumer's opencode.json.
442
+ * `dst` may be an absolute or relative path (fs.* accepts both).
443
+ * Used by the surgical uninstall path — this is NOT called during install (install is additive).
444
+ * Compares on opencodeProviderId(providerKey) to resolve the opencode-side block key.
445
+ * The caller is responsible for passing only bkit-seeded provider keys; this function does not
446
+ * validate providerKey against OPENCODE_PROVIDER_DEFAULTS (shared/egress.ts).
447
+ */
448
+ export function removeOpencodeProviderBlock(
449
+ dst: string,
450
+ providerKey: string,
451
+ dryRun: boolean,
452
+ ws: WriteSet,
453
+ ): void {
454
+ if (!fs.existsSync(dst)) return;
455
+ // Parse-or-sentinel idiom: keep the early-return outside the catch body.
456
+ let existing: Record<string, JsonValue> | null = null;
457
+ try {
458
+ existing = JSON.parse(fs.readFileSync(dst, "utf-8")) as Record<string, JsonValue>;
459
+ } catch {
460
+ existing = null;
461
+ }
462
+ if (existing === null) {
463
+ console.warn(`[bkit] ${dst} is malformed — leaving as-is`);
464
+ return;
465
+ }
466
+ const blockKey = opencodeProviderId(providerKey);
467
+ const providerObj = existing.provider;
468
+ if (providerObj === null || typeof providerObj !== "object" || Array.isArray(providerObj)) return;
469
+ const pObj = providerObj as Record<string, JsonValue>;
470
+ if (!(blockKey in pObj)) return; // already absent — idempotent
471
+ delete pObj[blockKey];
472
+ if (Object.keys(pObj).length === 0) delete existing.provider;
473
+ const updatedStr = JSON.stringify(existing, null, 2) + "\n";
474
+ if (!dryRun) {
475
+ fs.writeFileSync(dst, updatedStr, "utf-8");
476
+ }
477
+ ws.writes.push({ path: dst, action: "update" });
478
+ }
479
+
480
+ export function runOpencodeModule(ctx: ModuleContext): void {
481
+ const { dryRun, advisories, ws, logPrefix } = ctx;
482
+ const tag = dryRun ? "[dry-run] " : "";
483
+
484
+ const issueTracker = ctx.issueTracker ?? DEFAULT_ISSUE_TRACKER;
485
+ // See renderOpencodeAgentsCommands — the opencode host is unary; no provider throws.
486
+ if (!ctx.opencodeProvider) {
487
+ throw new Error("runOpencodeModule: ctx.opencodeProvider is required");
488
+ }
489
+ const provider = ctx.opencodeProvider;
490
+
491
+ // Source-rendered agents/commands (.opencode/agents + .opencode/commands). Extracted to
492
+ // renderOpencodeAgentsCommands so `bkit upgrade` re-renders them identically —
493
+ // bkit-owned output, force-overwritten.
494
+ renderOpencodeAgentsCommands(ctx);
495
+
496
+ // --- opencode.json — Tier-2 deep-contained (reconverged on upgrade) ---
497
+ {
498
+ const opencodeShared = path.join(getSharedRoot(), ".opencode");
499
+ const jsonSrc = path.join(opencodeShared, "opencode.json");
500
+ const jsonDst = ".opencode/opencode.json";
501
+ if (fs.existsSync(jsonSrc)) {
502
+ // Render the template with tracker-filtered mcp + provider config, then reconcile it into
503
+ // the consumer file. Install is additive: existing provider blocks are preserved.
504
+ // reconcileOpencodeJson computes an identical writeset for dry-run vs real runs — no
505
+ // `.bkit-staging` scratch file.
506
+ const rendered = renderOpencodeJson(jsonSrc, issueTracker, provider, process.cwd());
507
+ const prevLen = ws.writes.length;
508
+ reconcileOpencodeJson(rendered, jsonDst, dryRun, ws);
509
+ if (ws.writes.length > prevLen) {
510
+ console.log(`${logPrefix} ${tag}opencode: ${jsonDst}`);
511
+ }
512
+ }
513
+ }
514
+
515
+ // --- bkit-gateguard.js — Tier-2 markered-text (slash-comment); see install/manifest.ts ---
516
+ {
517
+ const opencodeCore = path.join(getCoreRoot(), ".opencode", "plugin");
518
+ const pluginSrc = path.join(opencodeCore, "bkit-gateguard.js");
519
+ const pluginDst = ".opencode/plugin/bkit-gateguard.js";
520
+ if (fs.existsSync(pluginSrc)) {
521
+ // dryRun-gated — writeTier2 honours dryRun, so the dir is only needed for a real
522
+ // write; `bkit check` stays read-only when gateguard's parent dir is absent.
523
+ if (!dryRun) ensureDir(path.dirname(pluginDst));
524
+ const prevLen = ws.writes.length;
525
+ writeTier2(pluginSrc, pluginDst, ctx.tokens, "slash-comment", dryRun, ws);
526
+ if (ws.writes.length > prevLen) {
527
+ console.log(`${logPrefix} ${tag}opencode: ${pluginDst}`);
528
+ }
529
+ }
530
+ }
531
+
532
+ // opencode post-install advisory — provider key + launch + model-ID lookup pointer.
533
+ advisories.push({
534
+ id: "opencode-setup",
535
+ severity: "advisory",
536
+ detect: `opencode host installed (provider: ${provider}). The opencode CLI and your provider credentials are required before running agents.`,
537
+ action:
538
+ `opencode setup for provider "${provider}":\n\n` +
539
+ (() => {
540
+ const pd = OPENCODE_PROVIDER_DEFAULTS[provider];
541
+ if (!pd) {
542
+ return (
543
+ ` 1. Set ${provider.toUpperCase()}_API_KEY in your environment / .bkit/.env.\n` +
544
+ ` 2. Update .opencode/opencode.json provider.${provider}.options.baseURL for your endpoint.\n`
545
+ );
546
+ }
547
+ // opencode-native gateways (opencode-zen / opencode-go) → BKIT_OPENCODE_API_KEY only.
548
+ // Built-in: no baseURL to seed; models use the opencode-side provider id.
549
+ if (pd.builtin || pd.envVar === "BKIT_OPENCODE_API_KEY") {
550
+ const modelPrefix = opencodeProviderId(provider);
551
+ return (
552
+ ` 1. opencode gateway "${provider}". Create an API key at https://opencode.ai/auth (one BKIT_OPENCODE_API_KEY covers both opencode-zen and opencode-go).\n` +
553
+ ` 2. Set ${pd.envVar} in your environment / .bkit/.env:\n` +
554
+ ` ${pd.envVar}=<your-api-key>\n` +
555
+ ` 3. Built-in provider (no npm/baseURL). Models are referenced as ${modelPrefix}/<id>.\n`
556
+ );
557
+ }
558
+ return (
559
+ ` 1. Set ${pd.envVar} in your environment / .bkit/.env:\n` +
560
+ ` ${pd.envVar}=<your-api-key>\n` +
561
+ ` 2. Provider base URL is pre-seeded: ${pd.baseURL}.\n`
562
+ );
563
+ })() +
564
+ ` 3. Launch opencode: \`opencode\` (CLI, reads .opencode/opencode.json automatically), or\n` +
565
+ ` \`bkit opencode\` to launch the desktop app with .bkit/.env merged into its environment.\n` +
566
+ ` macOS GUI apps launched from the Dock/Finder do not inherit the login-shell / direnv\n` +
567
+ ` env — {env:KEY} placeholders in opencode.json resolve empty without this. Quit the\n` +
568
+ ` running app first (a running instance ignores the new env).\n` +
569
+ ` 4. Model IDs: all four providers (openrouter, ollama, opencode-zen, opencode-go) install ready-to-run curated per-agent pins. ` +
570
+ `Re-pin any agent via .bkit/prompts.md § Model Overrides or by probing the provider's live /v1/models — see .bkit/providers.md § Models. NOTE: ollama opus/convergent pins (kimi-k2.7-code, deepseek-v4-pro) need a paid Ollama plan; its sonnet/haiku pins run on the free tier. opencode-go pins (minimax-m3, qwen3.7-plus, deepseek-v4-pro/-flash) — prefer pins that stay within the Go 5h usage window; opencode-zen pins are existence-validated via models.dev (the /zen/v1/models REST probe cannot see Go-only models — models.dev is the source of truth).\n` +
571
+ ` 5. Verify with: \`bkit doctor\` (credentials + tools) and \`bkit status\` (installed hosts + providers).`,
572
+ });
573
+ }