@cybedefend/vibedefend 1.8.3 → 1.9.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 (320) hide show
  1. package/README.md +135 -16
  2. package/dist/clients/antigravity.js +30 -6
  3. package/dist/clients/antigravity.js.map +1 -1
  4. package/dist/clients/claude-code.js +209 -17
  5. package/dist/clients/claude-code.js.map +1 -1
  6. package/dist/clients/codex.js +171 -11
  7. package/dist/clients/codex.js.map +1 -1
  8. package/dist/clients/cursor.js +149 -9
  9. package/dist/clients/cursor.js.map +1 -1
  10. package/dist/clients/dependency-check-wiring.js +24 -0
  11. package/dist/clients/dependency-check-wiring.js.map +1 -0
  12. package/dist/clients/opencode-plugin.js +905 -0
  13. package/dist/clients/opencode-plugin.js.map +1 -0
  14. package/dist/clients/opencode.js +653 -0
  15. package/dist/clients/opencode.js.map +1 -0
  16. package/dist/clients/registry.js +2 -0
  17. package/dist/clients/registry.js.map +1 -1
  18. package/dist/clients/vscode.js +106 -12
  19. package/dist/clients/vscode.js.map +1 -1
  20. package/dist/clients/windsurf.js +9 -1
  21. package/dist/clients/windsurf.js.map +1 -1
  22. package/dist/commands/mcp/index.js +11 -0
  23. package/dist/commands/mcp/index.js.map +1 -1
  24. package/dist/config.js +3 -0
  25. package/dist/config.js.map +1 -1
  26. package/dist/diagnostics.js +22 -3
  27. package/dist/diagnostics.js.map +1 -1
  28. package/dist/doctor.js +26 -1
  29. package/dist/doctor.js.map +1 -1
  30. package/dist/guards-evaluator/command-analysis.js +347 -86
  31. package/dist/guards-evaluator/command-analysis.js.map +1 -1
  32. package/dist/guards-evaluator/command-view.js +116 -16
  33. package/dist/guards-evaluator/command-view.js.map +1 -1
  34. package/dist/guards-evaluator/evaluate.js +185 -41
  35. package/dist/guards-evaluator/evaluate.js.map +1 -1
  36. package/dist/guards-evaluator/file-reads.js +202 -0
  37. package/dist/guards-evaluator/file-reads.js.map +1 -0
  38. package/dist/guards-evaluator/index.js +1 -1
  39. package/dist/guards-evaluator/index.js.map +1 -1
  40. package/dist/guards-evaluator/inline-danger.js +299 -0
  41. package/dist/guards-evaluator/inline-danger.js.map +1 -0
  42. package/dist/guards-evaluator/normalize.js +1186 -55
  43. package/dist/guards-evaluator/normalize.js.map +1 -1
  44. package/dist/guards-evaluator/regex.js +91 -4
  45. package/dist/guards-evaluator/regex.js.map +1 -1
  46. package/dist/guards-evaluator/shell-files.js +587 -0
  47. package/dist/guards-evaluator/shell-files.js.map +1 -0
  48. package/dist/guards-evaluator/triage.js +32 -2
  49. package/dist/guards-evaluator/triage.js.map +1 -1
  50. package/dist/hook-runner.js +25598 -5432
  51. package/dist/hooks/install.js +8 -3
  52. package/dist/hooks/install.js.map +1 -1
  53. package/dist/hooks/runtime/antigravity-session.js +109 -50
  54. package/dist/hooks/runtime/antigravity-session.js.map +1 -1
  55. package/dist/hooks/runtime/apply-patch.js +92 -0
  56. package/dist/hooks/runtime/apply-patch.js.map +1 -0
  57. package/dist/hooks/runtime/caller-gate.js +27 -0
  58. package/dist/hooks/runtime/caller-gate.js.map +1 -0
  59. package/dist/hooks/runtime/code-mode.js +259 -0
  60. package/dist/hooks/runtime/code-mode.js.map +1 -0
  61. package/dist/hooks/runtime/dependency-check/api.js +179 -0
  62. package/dist/hooks/runtime/dependency-check/api.js.map +1 -0
  63. package/dist/hooks/runtime/dependency-check/capabilities.js +99 -0
  64. package/dist/hooks/runtime/dependency-check/capabilities.js.map +1 -0
  65. package/dist/hooks/runtime/dependency-check/checked-packages.js +48 -0
  66. package/dist/hooks/runtime/dependency-check/checked-packages.js.map +1 -0
  67. package/dist/hooks/runtime/dependency-check/commands/cwd.js +154 -0
  68. package/dist/hooks/runtime/dependency-check/commands/cwd.js.map +1 -0
  69. package/dist/hooks/runtime/dependency-check/commands/env.js +99 -0
  70. package/dist/hooks/runtime/dependency-check/commands/env.js.map +1 -0
  71. package/dist/hooks/runtime/dependency-check/commands/flags.js +164 -0
  72. package/dist/hooks/runtime/dependency-check/commands/flags.js.map +1 -0
  73. package/dist/hooks/runtime/dependency-check/commands/managers.js +418 -0
  74. package/dist/hooks/runtime/dependency-check/commands/managers.js.map +1 -0
  75. package/dist/hooks/runtime/dependency-check/commands/peel.js +111 -0
  76. package/dist/hooks/runtime/dependency-check/commands/peel.js.map +1 -0
  77. package/dist/hooks/runtime/dependency-check/commands/shell.js +217 -0
  78. package/dist/hooks/runtime/dependency-check/commands/shell.js.map +1 -0
  79. package/dist/hooks/runtime/dependency-check/commands/types.js +4 -0
  80. package/dist/hooks/runtime/dependency-check/commands/types.js.map +1 -0
  81. package/dist/hooks/runtime/dependency-check/commands.js +34 -0
  82. package/dist/hooks/runtime/dependency-check/commands.js.map +1 -0
  83. package/dist/hooks/runtime/dependency-check/deadlines.js +69 -0
  84. package/dist/hooks/runtime/dependency-check/deadlines.js.map +1 -0
  85. package/dist/hooks/runtime/dependency-check/dispatch.js +344 -0
  86. package/dist/hooks/runtime/dependency-check/dispatch.js.map +1 -0
  87. package/dist/hooks/runtime/dependency-check/entering.js +63 -0
  88. package/dist/hooks/runtime/dependency-check/entering.js.map +1 -0
  89. package/dist/hooks/runtime/dependency-check/exec-cache.js +36 -0
  90. package/dist/hooks/runtime/dependency-check/exec-cache.js.map +1 -0
  91. package/dist/hooks/runtime/dependency-check/git-blob.js +116 -0
  92. package/dist/hooks/runtime/dependency-check/git-blob.js.map +1 -0
  93. package/dist/hooks/runtime/dependency-check/hook.js +418 -0
  94. package/dist/hooks/runtime/dependency-check/hook.js.map +1 -0
  95. package/dist/hooks/runtime/dependency-check/local-store.js +74 -0
  96. package/dist/hooks/runtime/dependency-check/local-store.js.map +1 -0
  97. package/dist/hooks/runtime/dependency-check/lockfile-walk.js +89 -0
  98. package/dist/hooks/runtime/dependency-check/lockfile-walk.js.map +1 -0
  99. package/dist/hooks/runtime/dependency-check/lockfiles/bun.js +153 -0
  100. package/dist/hooks/runtime/dependency-check/lockfiles/bun.js.map +1 -0
  101. package/dist/hooks/runtime/dependency-check/lockfiles/by-name.js +46 -0
  102. package/dist/hooks/runtime/dependency-check/lockfiles/by-name.js.map +1 -0
  103. package/dist/hooks/runtime/dependency-check/lockfiles/cargo.js +91 -0
  104. package/dist/hooks/runtime/dependency-check/lockfiles/cargo.js.map +1 -0
  105. package/dist/hooks/runtime/dependency-check/lockfiles/composer.js +98 -0
  106. package/dist/hooks/runtime/dependency-check/lockfiles/composer.js.map +1 -0
  107. package/dist/hooks/runtime/dependency-check/lockfiles/gemfile.js +88 -0
  108. package/dist/hooks/runtime/dependency-check/lockfiles/gemfile.js.map +1 -0
  109. package/dist/hooks/runtime/dependency-check/lockfiles/go-mod.js +154 -0
  110. package/dist/hooks/runtime/dependency-check/lockfiles/go-mod.js.map +1 -0
  111. package/dist/hooks/runtime/dependency-check/lockfiles/gradle-lockfile.js +27 -0
  112. package/dist/hooks/runtime/dependency-check/lockfiles/gradle-lockfile.js.map +1 -0
  113. package/dist/hooks/runtime/dependency-check/lockfiles/graph.js +131 -0
  114. package/dist/hooks/runtime/dependency-check/lockfiles/graph.js.map +1 -0
  115. package/dist/hooks/runtime/dependency-check/lockfiles/npm.js +107 -0
  116. package/dist/hooks/runtime/dependency-check/lockfiles/npm.js.map +1 -0
  117. package/dist/hooks/runtime/dependency-check/lockfiles/nuget-packages-lock.js +60 -0
  118. package/dist/hooks/runtime/dependency-check/lockfiles/nuget-packages-lock.js.map +1 -0
  119. package/dist/hooks/runtime/dependency-check/lockfiles/pins.js +105 -0
  120. package/dist/hooks/runtime/dependency-check/lockfiles/pins.js.map +1 -0
  121. package/dist/hooks/runtime/dependency-check/lockfiles/pip-report.js +34 -0
  122. package/dist/hooks/runtime/dependency-check/lockfiles/pip-report.js.map +1 -0
  123. package/dist/hooks/runtime/dependency-check/lockfiles/pnpm.js +109 -0
  124. package/dist/hooks/runtime/dependency-check/lockfiles/pnpm.js.map +1 -0
  125. package/dist/hooks/runtime/dependency-check/lockfiles/poetry.js +105 -0
  126. package/dist/hooks/runtime/dependency-check/lockfiles/poetry.js.map +1 -0
  127. package/dist/hooks/runtime/dependency-check/lockfiles/uv.js +181 -0
  128. package/dist/hooks/runtime/dependency-check/lockfiles/uv.js.map +1 -0
  129. package/dist/hooks/runtime/dependency-check/lockfiles/yaml.js +319 -0
  130. package/dist/hooks/runtime/dependency-check/lockfiles/yaml.js.map +1 -0
  131. package/dist/hooks/runtime/dependency-check/lockfiles/yarn-berry.js +71 -0
  132. package/dist/hooks/runtime/dependency-check/lockfiles/yarn-berry.js.map +1 -0
  133. package/dist/hooks/runtime/dependency-check/lockfiles/yarn1.js +144 -0
  134. package/dist/hooks/runtime/dependency-check/lockfiles/yarn1.js.map +1 -0
  135. package/dist/hooks/runtime/dependency-check/manifests/apply.js +110 -0
  136. package/dist/hooks/runtime/dependency-check/manifests/apply.js.map +1 -0
  137. package/dist/hooks/runtime/dependency-check/manifests/diff.js +127 -0
  138. package/dist/hooks/runtime/dependency-check/manifests/diff.js.map +1 -0
  139. package/dist/hooks/runtime/dependency-check/manifests/edit.js +259 -0
  140. package/dist/hooks/runtime/dependency-check/manifests/edit.js.map +1 -0
  141. package/dist/hooks/runtime/dependency-check/manifests/json.js +109 -0
  142. package/dist/hooks/runtime/dependency-check/manifests/json.js.map +1 -0
  143. package/dist/hooks/runtime/dependency-check/manifests/kind.js +40 -0
  144. package/dist/hooks/runtime/dependency-check/manifests/kind.js.map +1 -0
  145. package/dist/hooks/runtime/dependency-check/manifests/patch.js +179 -0
  146. package/dist/hooks/runtime/dependency-check/manifests/patch.js.map +1 -0
  147. package/dist/hooks/runtime/dependency-check/manifests/text.js +665 -0
  148. package/dist/hooks/runtime/dependency-check/manifests/text.js.map +1 -0
  149. package/dist/hooks/runtime/dependency-check/manifests/toml.js +350 -0
  150. package/dist/hooks/runtime/dependency-check/manifests/toml.js.map +1 -0
  151. package/dist/hooks/runtime/dependency-check/manifests.js +14 -0
  152. package/dist/hooks/runtime/dependency-check/manifests.js.map +1 -0
  153. package/dist/hooks/runtime/dependency-check/messages.js +270 -0
  154. package/dist/hooks/runtime/dependency-check/messages.js.map +1 -0
  155. package/dist/hooks/runtime/dependency-check/net-baseline.js +157 -0
  156. package/dist/hooks/runtime/dependency-check/net-baseline.js.map +1 -0
  157. package/dist/hooks/runtime/dependency-check/net-state.js +70 -0
  158. package/dist/hooks/runtime/dependency-check/net-state.js.map +1 -0
  159. package/dist/hooks/runtime/dependency-check/pending-forces.js +54 -0
  160. package/dist/hooks/runtime/dependency-check/pending-forces.js.map +1 -0
  161. package/dist/hooks/runtime/dependency-check/post-tool-use.js +108 -0
  162. package/dist/hooks/runtime/dependency-check/post-tool-use.js.map +1 -0
  163. package/dist/hooks/runtime/dependency-check/ranges.js +369 -0
  164. package/dist/hooks/runtime/dependency-check/ranges.js.map +1 -0
  165. package/dist/hooks/runtime/dependency-check/requirement-files.js +144 -0
  166. package/dist/hooks/runtime/dependency-check/requirement-files.js.map +1 -0
  167. package/dist/hooks/runtime/dependency-check/resolvers/bun.js +38 -0
  168. package/dist/hooks/runtime/dependency-check/resolvers/bun.js.map +1 -0
  169. package/dist/hooks/runtime/dependency-check/resolvers/bundler.js +218 -0
  170. package/dist/hooks/runtime/dependency-check/resolvers/bundler.js.map +1 -0
  171. package/dist/hooks/runtime/dependency-check/resolvers/cargo.js +303 -0
  172. package/dist/hooks/runtime/dependency-check/resolvers/cargo.js.map +1 -0
  173. package/dist/hooks/runtime/dependency-check/resolvers/composer.js +250 -0
  174. package/dist/hooks/runtime/dependency-check/resolvers/composer.js.map +1 -0
  175. package/dist/hooks/runtime/dependency-check/resolvers/config-files.js +27 -0
  176. package/dist/hooks/runtime/dependency-check/resolvers/config-files.js.map +1 -0
  177. package/dist/hooks/runtime/dependency-check/resolvers/direct-only-registry.js +409 -0
  178. package/dist/hooks/runtime/dependency-check/resolvers/direct-only-registry.js.map +1 -0
  179. package/dist/hooks/runtime/dependency-check/resolvers/direct-only.js +290 -0
  180. package/dist/hooks/runtime/dependency-check/resolvers/direct-only.js.map +1 -0
  181. package/dist/hooks/runtime/dependency-check/resolvers/gem.js +89 -0
  182. package/dist/hooks/runtime/dependency-check/resolvers/gem.js.map +1 -0
  183. package/dist/hooks/runtime/dependency-check/resolvers/go.js +313 -0
  184. package/dist/hooks/runtime/dependency-check/resolvers/go.js.map +1 -0
  185. package/dist/hooks/runtime/dependency-check/resolvers/npm.js +81 -0
  186. package/dist/hooks/runtime/dependency-check/resolvers/npm.js.map +1 -0
  187. package/dist/hooks/runtime/dependency-check/resolvers/pip.js +229 -0
  188. package/dist/hooks/runtime/dependency-check/resolvers/pip.js.map +1 -0
  189. package/dist/hooks/runtime/dependency-check/resolvers/pipx.js +8 -0
  190. package/dist/hooks/runtime/dependency-check/resolvers/pipx.js.map +1 -0
  191. package/dist/hooks/runtime/dependency-check/resolvers/pnpm.js +68 -0
  192. package/dist/hooks/runtime/dependency-check/resolvers/pnpm.js.map +1 -0
  193. package/dist/hooks/runtime/dependency-check/resolvers/poetry.js +70 -0
  194. package/dist/hooks/runtime/dependency-check/resolvers/poetry.js.map +1 -0
  195. package/dist/hooks/runtime/dependency-check/resolvers/project.js +208 -0
  196. package/dist/hooks/runtime/dependency-check/resolvers/project.js.map +1 -0
  197. package/dist/hooks/runtime/dependency-check/resolvers/python-index.js +270 -0
  198. package/dist/hooks/runtime/dependency-check/resolvers/python-index.js.map +1 -0
  199. package/dist/hooks/runtime/dependency-check/resolvers/python.js +229 -0
  200. package/dist/hooks/runtime/dependency-check/resolvers/python.js.map +1 -0
  201. package/dist/hooks/runtime/dependency-check/resolvers/registry.js +221 -0
  202. package/dist/hooks/runtime/dependency-check/resolvers/registry.js.map +1 -0
  203. package/dist/hooks/runtime/dependency-check/resolvers/shared.js +440 -0
  204. package/dist/hooks/runtime/dependency-check/resolvers/shared.js.map +1 -0
  205. package/dist/hooks/runtime/dependency-check/resolvers/tree.js +131 -0
  206. package/dist/hooks/runtime/dependency-check/resolvers/tree.js.map +1 -0
  207. package/dist/hooks/runtime/dependency-check/resolvers/uv.js +433 -0
  208. package/dist/hooks/runtime/dependency-check/resolvers/uv.js.map +1 -0
  209. package/dist/hooks/runtime/dependency-check/resolvers/xml-lite.js +132 -0
  210. package/dist/hooks/runtime/dependency-check/resolvers/xml-lite.js.map +1 -0
  211. package/dist/hooks/runtime/dependency-check/resolvers/yarn-berry.js +106 -0
  212. package/dist/hooks/runtime/dependency-check/resolvers/yarn-berry.js.map +1 -0
  213. package/dist/hooks/runtime/dependency-check/sandbox.js +328 -0
  214. package/dist/hooks/runtime/dependency-check/sandbox.js.map +1 -0
  215. package/dist/hooks/runtime/dependency-check/specs.js +171 -0
  216. package/dist/hooks/runtime/dependency-check/specs.js.map +1 -0
  217. package/dist/hooks/runtime/dependency-check/static-view.js +90 -0
  218. package/dist/hooks/runtime/dependency-check/static-view.js.map +1 -0
  219. package/dist/hooks/runtime/dependency-check/stop-net.js +278 -0
  220. package/dist/hooks/runtime/dependency-check/stop-net.js.map +1 -0
  221. package/dist/hooks/runtime/dependency-check/stop-wiring.js +94 -0
  222. package/dist/hooks/runtime/dependency-check/stop-wiring.js.map +1 -0
  223. package/dist/hooks/runtime/dependency-check/tool-call.js +130 -0
  224. package/dist/hooks/runtime/dependency-check/tool-call.js.map +1 -0
  225. package/dist/hooks/runtime/dependency-check/types.js +96 -0
  226. package/dist/hooks/runtime/dependency-check/types.js.map +1 -0
  227. package/dist/hooks/runtime/emit.js +240 -52
  228. package/dist/hooks/runtime/emit.js.map +1 -1
  229. package/dist/hooks/runtime/exit-deadlock-guard.js +24 -8
  230. package/dist/hooks/runtime/exit-deadlock-guard.js.map +1 -1
  231. package/dist/hooks/runtime/fetch-rules.js +78 -29
  232. package/dist/hooks/runtime/fetch-rules.js.map +1 -1
  233. package/dist/hooks/runtime/grep-include.js +228 -0
  234. package/dist/hooks/runtime/grep-include.js.map +1 -0
  235. package/dist/hooks/runtime/pre-compact.js +1 -1
  236. package/dist/hooks/runtime/pre-compact.js.map +1 -1
  237. package/dist/hooks/runtime/pre-invocation.js +3 -3
  238. package/dist/hooks/runtime/pre-invocation.js.map +1 -1
  239. package/dist/hooks/runtime/repo-registry.js +72 -0
  240. package/dist/hooks/runtime/repo-registry.js.map +1 -0
  241. package/dist/hooks/runtime/repo-scope.js +75 -0
  242. package/dist/hooks/runtime/repo-scope.js.map +1 -0
  243. package/dist/hooks/runtime/resolve.js +11 -2
  244. package/dist/hooks/runtime/resolve.js.map +1 -1
  245. package/dist/hooks/runtime/runner-args.js +17 -2
  246. package/dist/hooks/runtime/runner-args.js.map +1 -1
  247. package/dist/hooks/runtime/script-resolve.js +21 -5
  248. package/dist/hooks/runtime/script-resolve.js.map +1 -1
  249. package/dist/hooks/runtime/session-activation.js +71 -18
  250. package/dist/hooks/runtime/session-activation.js.map +1 -1
  251. package/dist/hooks/runtime/session-review.js +136 -66
  252. package/dist/hooks/runtime/session-review.js.map +1 -1
  253. package/dist/hooks/runtime/session-start.js +68 -13
  254. package/dist/hooks/runtime/session-start.js.map +1 -1
  255. package/dist/hooks/runtime/session-writes.js +11 -5
  256. package/dist/hooks/runtime/session-writes.js.map +1 -1
  257. package/dist/hooks/runtime/shell-writes.js +7 -88
  258. package/dist/hooks/runtime/shell-writes.js.map +1 -1
  259. package/dist/hooks/runtime/sniff.js +227 -2
  260. package/dist/hooks/runtime/sniff.js.map +1 -1
  261. package/dist/hooks/runtime/target-resolve.js +32 -17
  262. package/dist/hooks/runtime/target-resolve.js.map +1 -1
  263. package/dist/hooks/runtime/unmapped-tools.js +77 -1
  264. package/dist/hooks/runtime/unmapped-tools.js.map +1 -1
  265. package/dist/hooks/runtime/user-prompt-submit.js +1 -1
  266. package/dist/hooks/runtime/user-prompt-submit.js.map +1 -1
  267. package/dist/hooks/runtime/work-dir.js +3 -0
  268. package/dist/hooks/runtime/work-dir.js.map +1 -1
  269. package/dist/hooks/shim.js +27 -20
  270. package/dist/hooks/shim.js.map +1 -1
  271. package/dist/index.js +40 -3
  272. package/dist/index.js.map +1 -1
  273. package/dist/install-scope.js +12 -0
  274. package/dist/install-scope.js.map +1 -0
  275. package/dist/install.js +188 -78
  276. package/dist/install.js.map +1 -1
  277. package/dist/link.js +55 -5
  278. package/dist/link.js.map +1 -1
  279. package/dist/mcp/config/hook.js +25 -0
  280. package/dist/mcp/config/hook.js.map +1 -1
  281. package/dist/org.js +3 -1
  282. package/dist/org.js.map +1 -1
  283. package/dist/prompts.js +96 -12
  284. package/dist/prompts.js.map +1 -1
  285. package/dist/refresh-hooks.js +67 -15
  286. package/dist/refresh-hooks.js.map +1 -1
  287. package/dist/repo/exclude-block.js +112 -0
  288. package/dist/repo/exclude-block.js.map +1 -0
  289. package/dist/repo/footer.js +93 -0
  290. package/dist/repo/footer.js.map +1 -0
  291. package/dist/repo/git-repo.js +77 -0
  292. package/dist/repo/git-repo.js.map +1 -0
  293. package/dist/repo/guarded-write.js +45 -0
  294. package/dist/repo/guarded-write.js.map +1 -0
  295. package/dist/repo/install-repo.js +508 -0
  296. package/dist/repo/install-repo.js.map +1 -0
  297. package/dist/repo/machine-wiring.js +122 -0
  298. package/dist/repo/machine-wiring.js.map +1 -0
  299. package/dist/repo/mcp-json.js +57 -0
  300. package/dist/repo/mcp-json.js.map +1 -0
  301. package/dist/repo/registry.js +53 -0
  302. package/dist/repo/registry.js.map +1 -0
  303. package/dist/repo/repo-mode-guard.js +83 -0
  304. package/dist/repo/repo-mode-guard.js.map +1 -0
  305. package/dist/repo/repo-setups.js +123 -0
  306. package/dist/repo/repo-setups.js.map +1 -0
  307. package/dist/repo/repo-status.js +283 -0
  308. package/dist/repo/repo-status.js.map +1 -0
  309. package/dist/repo/uninstall-repo.js +56 -0
  310. package/dist/repo/uninstall-repo.js.map +1 -0
  311. package/dist/repo/unsupported.js +14 -0
  312. package/dist/repo/unsupported.js.map +1 -0
  313. package/dist/shim.js +9 -7
  314. package/dist/status.js +53 -11
  315. package/dist/status.js.map +1 -1
  316. package/dist/uninstall.js +51 -3
  317. package/dist/uninstall.js.map +1 -1
  318. package/dist/update.js +6 -1
  319. package/dist/update.js.map +1 -1
  320. package/package.json +5 -2
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opencode-plugin.js","sourceRoot":"","sources":["../../src/clients/opencode-plugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsJG;AAEH;;;;;GAKG;AACH,MAAM,CAAC,MAAM,sBAAsB,GACjC,iGAAiG,CAAC;AAEpG,6FAA6F;AAC7F,MAAM,CAAC,MAAM,0BAA0B,GAAG,MAAM,CAAC;AAEjD,gFAAgF;AAChF,MAAM,CAAC,MAAM,yBAAyB,GAAG,MAAM,CAAC;AAuBhD;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,IAA2B;IAC9D,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,IAAI,0BAA0B,CAAC;IAC/D,4EAA4E;IAC5E,wEAAwE;IACxE,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,SAAS,IAAI,CAAC,EAAE,CAAC;QAClD,MAAM,IAAI,UAAU,CAAC,4DAA4D,SAAS,EAAE,CAAC,CAAC;IAChG,CAAC;IACD,OAAO;QACL,sBAAsB;QACtB,wFAAwF;QACxF,0EAA0E;QAC1E,6CAA6C;QAC7C,uCAAuC;QACvC,EAAE;QACF,kBAAkB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG;QACpD,qBAAqB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG;QACrD,iCAAiC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,mBAAmB,KAAK,IAAI,CAAC,GAAG;QACrF,sBAAsB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,GAAG;QAC7D,4BAA4B,IAAI,CAAC,SAAS,CACxC,IAAI,CAAC,IAAI,CAAC,CAAC,SAAS,GAAG,yBAAyB,CAAC,GAAG,0BAA0B,CAAC,CAChF,GAAG;QACJ,aAAa;QACb,aAAa;QACb,YAAY;QACZ,mFAAmF;QACnF,EAAE;KACH,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC;AAED;;;;GAIG;AACH,MAAM,aAAa,GAAG,MAAM,CAAC,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsQ/B,CAAC;AAEF,wCAAwC;AACxC,MAAM,aAAa,GAAG,MAAM,CAAC,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8K/B,CAAC;AAEF,qCAAqC;AACrC,MAAM,YAAY,GAAG,MAAM,CAAC,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyQ9B,CAAC"}
@@ -0,0 +1,653 @@
1
+ /**
2
+ * OpenCode adapter (BACK-255).
3
+ *
4
+ * OpenCode runs no hook command. It loads every `*.js` file of the `plugins/`
5
+ * directory of its config dir as an ES module and calls its hooks in-process,
6
+ * so the adapter writes ONE file, `cybedefend-vibedefend.js`, rendered by
7
+ * `renderOpenCodePlugin` (`opencode-plugin.ts`): the plugin only transports,
8
+ * each hook is handed to the hook runner (`--client opencode`).
9
+ *
10
+ * config dir `$XDG_CONFIG_HOME/opencode` when that variable is set and
11
+ * not empty, else `~/.config/opencode` (every platform,
12
+ * Windows included: `%USERPROFILE%\.config\opencode`).
13
+ * plugin `<config dir>/plugins/cybedefend-vibedefend.js`
14
+ * MCP server `mcp.<name> = { type: "remote", url, enabled: true }` in
15
+ * `<config dir>/opencode.jsonc` when it exists, else
16
+ * `opencode.json` (created with OpenCode's `$schema`).
17
+ *
18
+ * A plugin file is OURS when it starts with `OPENCODE_PLUGIN_MARKER` AND
19
+ * references our managed hook dir (`textReferencesHookRunner`). Any other
20
+ * file at that path is never overwritten nor deleted: `writeSettings`
21
+ * refuses, `uninstall` and `inspect` pass it by.
22
+ *
23
+ * OpenCode merges the global config files in the order `config.json`,
24
+ * `opencode.json`, `opencode.jsonc`: the last one wins. We write the last
25
+ * one that exists, and read (`inspect`) and clean (`uninstall`) the two in
26
+ * that precedence. The entry points straight at the region's URL; OpenCode's
27
+ * other MCP servers are not put behind the VibeDefend proxy (as for Codex and
28
+ * Antigravity, BACK-241 Ruling R5).
29
+ *
30
+ * The files are read and written through `json-file.ts`, so the comments, the
31
+ * layout and the other servers of a config people edit by hand stay as they
32
+ * were. Every path is computed per call: `XDG_CONFIG_HOME` / `XDG_DATA_HOME`
33
+ * may change between two calls (and tests move HOME).
34
+ *
35
+ * V1 and V2 share the config dir, and one machine can hold both: the plugin
36
+ * file serves both, and so does the flat MCP entry. What differs is the
37
+ * sign-in. V1 keeps it in `mcp-auth.json`, V2 in its SQLite database, next to
38
+ * the tokens, where only its background service can say whether it is usable:
39
+ * see `openCodeInstalls` and `postRegister`.
40
+ */
41
+ import { execFileSync } from 'node:child_process';
42
+ import { existsSync, readFileSync, readdirSync, rmSync, rmdirSync, writeFileSync } from 'node:fs';
43
+ import { join } from 'node:path';
44
+ import { ensureDirFor, home, log, stripBom, which } from '../utils.js';
45
+ import { parseJsonc, readJson, writeJson } from '../json-file.js';
46
+ import { HOOK_RUNNER_PATH, textReferencesHookRunner } from '../hooks/install.js';
47
+ import { mcpStatus, readJsonServerEntry } from './mcp-url.js';
48
+ import { deleteKeys, editJsonFile } from './unwire.js';
49
+ import { probeBinaryVersion } from './detect.js';
50
+ import { OPENCODE_PLUGIN_MARKER, renderOpenCodePlugin } from './opencode-plugin.js';
51
+ import { isTracked } from '../repo/git-repo.js';
52
+ import { editRepoJson } from '../repo/guarded-write.js';
53
+ import { trackedReason } from '../repo/unsupported.js';
54
+ const OPENCODE_SCHEMA = 'https://opencode.ai/config.json';
55
+ const OPENCODE_DESKTOP_APP = '/Applications/OpenCode.app';
56
+ const PLUGIN_FILE_NAME = 'cybedefend-vibedefend.js';
57
+ /**
58
+ * How long `opencode mcp auth` may run: OpenCode waits 5 minutes for the
59
+ * browser's callback (`oauth-callback.ts`), so this ends a hung run without
60
+ * cutting a slow but live sign-in.
61
+ */
62
+ const OPENCODE_AUTH_TIMEOUT_MS = 330_000;
63
+ /**
64
+ * What V2 leaves in its config dir once it has run. It says "V2 has run",
65
+ * not "`opencode` is V2", and it holds the service's password: test that it
66
+ * exists, never read it.
67
+ */
68
+ const V2_MARKER_FILE = 'service.json';
69
+ function isRecord(value) {
70
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
71
+ }
72
+ /** OpenCode's config dir: `$XDG_CONFIG_HOME/opencode` when set and not empty, else `~/.config/opencode`. */
73
+ export function openCodeConfigDir() {
74
+ const xdg = process.env.XDG_CONFIG_HOME;
75
+ return xdg ? join(xdg, 'opencode') : home('.config', 'opencode');
76
+ }
77
+ /** OpenCode's data dir (its sign-in tokens, its logs): `$XDG_DATA_HOME/opencode`, else `~/.local/share/opencode`. */
78
+ function openCodeDataDir() {
79
+ const xdg = process.env.XDG_DATA_HOME;
80
+ return xdg ? join(xdg, 'opencode') : home('.local', 'share', 'opencode');
81
+ }
82
+ /** Where the plugin vibedefend writes lives. */
83
+ export function openCodePluginPath() {
84
+ return join(openCodeConfigDir(), 'plugins', PLUGIN_FILE_NAME);
85
+ }
86
+ /** The config file `registerMcp` writes: `opencode.jsonc` when it exists, else `opencode.json`. */
87
+ export function openCodeConfigFile() {
88
+ const jsonc = join(openCodeConfigDir(), 'opencode.jsonc');
89
+ return existsSync(jsonc) ? jsonc : join(openCodeConfigDir(), 'opencode.json');
90
+ }
91
+ /** Both global config files, in the order OpenCode lets them win: `opencode.jsonc` first. */
92
+ function openCodeConfigFiles() {
93
+ return [join(openCodeConfigDir(), 'opencode.jsonc'), join(openCodeConfigDir(), 'opencode.json')];
94
+ }
95
+ /**
96
+ * What to do when OpenCode cannot reach the server for want of a sign-in.
97
+ * OpenCode does not open a browser by itself at first connection: it marks the
98
+ * server `needs_auth` and shows a toast naming this command.
99
+ */
100
+ function needsAuthHint(name) {
101
+ return needsAuthHintFor('OpenCode', `\`opencode mcp auth ${name}\``);
102
+ }
103
+ function needsAuthHintFor(label, command) {
104
+ return `If ${label} reports that the server needs authentication, run ${command}.`;
105
+ }
106
+ /**
107
+ * Read what this machine holds. The version is `opencode`'s own: V1 prints
108
+ * `1.19.4`, V2 `opencode v2.2.1`, and `probeBinaryVersion` takes both.
109
+ */
110
+ export function openCodeInstalls() {
111
+ const version = probeBinaryVersion('opencode');
112
+ let cli;
113
+ if (version !== null)
114
+ cli = Number.parseInt(version, 10) >= 2 ? 'v2' : 'v1';
115
+ else
116
+ cli = which('opencode') !== null ? 'unknown' : 'none';
117
+ const opencode2 = which('opencode2') !== null;
118
+ const v2 = cli === 'v2' || opencode2 || existsSync(join(openCodeConfigDir(), V2_MARKER_FILE));
119
+ return { cli, opencode2, v2 };
120
+ }
121
+ /** Whether V2 is here, or may be: an `opencode` that names no version counts. */
122
+ function mayRunV2(installs) {
123
+ return installs.v2 || installs.cli === 'unknown';
124
+ }
125
+ /**
126
+ * Whether V1 is here, or may be: `opencode` is V1 or names no version, or no
127
+ * command is on PATH and nothing says V2 ran (the desktop app alone).
128
+ */
129
+ function mayRunV1(installs) {
130
+ return installs.cli === 'v1' || installs.cli === 'unknown' || (installs.cli === 'none' && !installs.v2);
131
+ }
132
+ /**
133
+ * A command of V2 from here, for a sentence. `opencode2` only when `opencode`
134
+ * does not already reach V2.
135
+ */
136
+ function v2Command(args, installs) {
137
+ if (installs.opencode2 && installs.cli !== 'v2')
138
+ return `\`opencode2 ${args}\``;
139
+ const command = `\`opencode ${args}\``;
140
+ return installs.cli === 'v1'
141
+ ? `${command} with V2's own binary, not the \`opencode\` on your PATH, which is V1`
142
+ : command;
143
+ }
144
+ /**
145
+ * Is OpenCode V1 signed in to the server registered under `region.mcpName` at
146
+ * `region.mcpUrl`? Read defensively from V1's own token file,
147
+ * `<data dir>/mcp-auth.json` — never throws. V2 neither reads nor imports that
148
+ * file, so this says nothing about V2. It mirrors what V1 itself trusts
149
+ * (`mcp/auth.ts`, `mcp/index.ts`):
150
+ * - the entry is keyed by the server name, and only used when its
151
+ * `serverUrl` equals the configured URL (EU and US share the name);
152
+ * - it needs `tokens.accessToken`, and then either an `expiresAt` (seconds)
153
+ * still ahead, or no `expiresAt` at all, or a `refreshToken` — OpenCode
154
+ * refreshes an expired access token when it connects, so only a token
155
+ * that cannot be refreshed needs `opencode mcp auth` again.
156
+ */
157
+ export function openCodeSignedIn(region, nowMs = Date.now()) {
158
+ try {
159
+ const file = join(openCodeDataDir(), 'mcp-auth.json');
160
+ if (!existsSync(file))
161
+ return false;
162
+ const data = JSON.parse(stripBom(readFileSync(file, 'utf8')));
163
+ if (!isRecord(data) || !Object.prototype.hasOwnProperty.call(data, region.mcpName))
164
+ return false;
165
+ const entry = data[region.mcpName];
166
+ if (!isRecord(entry) || entry.serverUrl !== region.mcpUrl)
167
+ return false;
168
+ const tokens = entry.tokens;
169
+ if (!isRecord(tokens) || typeof tokens.accessToken !== 'string' || tokens.accessToken === '') {
170
+ return false;
171
+ }
172
+ const { expiresAt, refreshToken } = tokens;
173
+ // No expiry (or 0) is "valid" to OpenCode; a value that is not a number
174
+ // makes it drop the whole file.
175
+ if (expiresAt === undefined || expiresAt === 0)
176
+ return true;
177
+ if (typeof expiresAt !== 'number')
178
+ return false;
179
+ if (expiresAt >= nowMs / 1000)
180
+ return true;
181
+ return typeof refreshToken === 'string' && refreshToken !== '';
182
+ }
183
+ catch {
184
+ return false;
185
+ }
186
+ }
187
+ /**
188
+ * V2's own server map, `mcp.servers`, or `null`. A flat server that happens to
189
+ * be named `servers` has a `type` and is no map: V2 reads it the same way
190
+ * (`isDirectLegacyMcp`, `config/normalize.ts`).
191
+ */
192
+ function nativeServers(mcp) {
193
+ if (!isRecord(mcp) || !isRecord(mcp.servers))
194
+ return null;
195
+ const { type } = mcp.servers;
196
+ return type === 'local' || type === 'remote' ? null : mcp.servers;
197
+ }
198
+ /** Our plugin: it starts with the marker AND references our managed hook dir. */
199
+ function isOurPlugin(text) {
200
+ return text.startsWith(OPENCODE_PLUGIN_MARKER) && textReferencesHookRunner(text);
201
+ }
202
+ function detect() {
203
+ const version = probeBinaryVersion('opencode') ?? probeBinaryVersion('opencode2') ?? undefined;
204
+ const configDir = openCodeConfigDir();
205
+ const dataDir = openCodeDataDir();
206
+ const installed = version !== undefined ||
207
+ existsSync(configDir) ||
208
+ existsSync(dataDir) ||
209
+ (process.platform === 'darwin' && existsSync(OPENCODE_DESKTOP_APP));
210
+ if (!installed) {
211
+ return {
212
+ installed: false,
213
+ supportsHooks: false,
214
+ reason: `\`opencode\` CLI not on PATH, no ${configDir} or ${dataDir} directory, ` +
215
+ `and no ${OPENCODE_DESKTOP_APP}.`,
216
+ };
217
+ }
218
+ // No version gate: the README says which versions this covers. Any file in
219
+ // the config dir is no proof of a version, and neither is `service.json`
220
+ // (`openCodeInstalls` reads what that one means).
221
+ return { installed: true, version, supportsHooks: true };
222
+ }
223
+ function supports(event) {
224
+ // No PreCompact (`experimental.session.compacting` only feeds the summary)
225
+ // and no UserPromptSubmit: the doctrine reaches OpenCode through the MCP
226
+ // server's `instructions`, as on Codex. Action Guards (the tool hooks) are
227
+ // part of the plugin unconditionally — they aren't a LifecycleEvent.
228
+ return event === 'fetch-rules' || event === 'session-start' || event === 'stop';
229
+ }
230
+ /**
231
+ * Write the plugin at `pluginPath`. Refuses a file there that is not ours,
232
+ * which is never overwritten.
233
+ */
234
+ function writeOpenCodePluginAt(pluginPath, enableSessionReview) {
235
+ if (existsSync(pluginPath) && !isOurPlugin(readFileSync(pluginPath, 'utf8'))) {
236
+ throw new Error(`${pluginPath} exists and was not written by vibedefend; move it away and re-run install`);
237
+ }
238
+ ensureDirFor(pluginPath);
239
+ writeFileSync(pluginPath, renderOpenCodePlugin({
240
+ runnerPath: HOOK_RUNNER_PATH,
241
+ nodePath: process.execPath,
242
+ enableSessionReview,
243
+ }), { mode: 0o644 });
244
+ }
245
+ function writeSettings(opts) {
246
+ const pluginPath = openCodePluginPath();
247
+ writeOpenCodePluginAt(pluginPath, opts.enableSessionReview);
248
+ log.ok(`OpenCode plugin written to ${pluginPath}`);
249
+ const installs = openCodeInstalls();
250
+ log.hint(mayRunV2(installs)
251
+ ? `Restart OpenCode to load it. OpenCode V2's running service does not load it: run ${v2Command('service restart', installs)}.`
252
+ : 'Restart OpenCode to load it.');
253
+ // V2 runs the review in its service once `opencode run` has answered.
254
+ if (mayRunV1(installs)) {
255
+ log.hint("OpenCode V1's `opencode run` ends before the end-of-session review: the review runs in a session that stays open.");
256
+ }
257
+ }
258
+ /** Set `mcp.<name>` of one OpenCode config object, as `registerMcp` does; `isNew`: add the `$schema`. */
259
+ function applyOpenCodeMcpEntry(config, region, isNew, file) {
260
+ if (isNew)
261
+ config.$schema = OPENCODE_SCHEMA;
262
+ config.mcp ??= {};
263
+ if (!isRecord(config.mcp)) {
264
+ throw new Error(`"mcp" in ${file} is not an object; fix it and re-run install`);
265
+ }
266
+ // `opencode mcp add` on V2 writes `mcp.servers.<name>`, and V2 lets it win over
267
+ // a flat entry: re-point that one, or a region switch would change nothing.
268
+ const native = nativeServers(config.mcp);
269
+ const existing = native?.[region.mcpName];
270
+ if (native && isRecord(existing)) {
271
+ native[region.mcpName] = { ...existing, type: 'remote', url: region.mcpUrl };
272
+ }
273
+ else {
274
+ config.mcp[region.mcpName] = { type: 'remote', url: region.mcpUrl, enabled: true };
275
+ }
276
+ }
277
+ /** Write the `cybedefend` entry into `configFile` (created with the `$schema` when absent). */
278
+ function writeOpenCodeMcpEntry(configFile, region) {
279
+ const isNew = !existsSync(configFile);
280
+ const config = readJson(configFile, {});
281
+ if (!isRecord(config)) {
282
+ throw new Error(`${configFile} does not hold a JSON object; fix it and re-run install`);
283
+ }
284
+ applyOpenCodeMcpEntry(config, region, isNew, configFile);
285
+ writeJson(configFile, config);
286
+ }
287
+ /**
288
+ * Register the CybeDefend MCP server as a remote entry of OpenCode's config:
289
+ * `mcp.<name> = { type: "remote", url, enabled: true }`. Every other key and
290
+ * server is kept, and so are the comments of a JSONC file. NOT wrapped by the
291
+ * VibeDefend MCP proxy (like Codex and Antigravity).
292
+ */
293
+ function registerMcp(opts) {
294
+ const { region } = opts;
295
+ log.step(`Registering MCP "${region.mcpName}" in OpenCode`);
296
+ log.hint(`URL: ${region.mcpUrl}`);
297
+ const file = openCodeConfigFile();
298
+ writeOpenCodeMcpEntry(file, region);
299
+ // The sign-in is postRegister's, which install and doctor always run next.
300
+ log.ok(`MCP "${region.mcpName}" registered in ${file}`);
301
+ return true;
302
+ }
303
+ /**
304
+ * OpenCode-specific post-registration step: `opencode mcp auth <name>` signs
305
+ * the server in (OAuth, in the browser) so the first MCP call does not fail.
306
+ *
307
+ * It opens a browser and waits for it (up to 5 minutes), so it runs only when
308
+ * it is needed: the `opencode` CLI is on PATH (OpenCode may be present only as
309
+ * the desktop app) and OpenCode is not already signed in to this server (see
310
+ * `openCodeSignedIn`; a signed-in server would otherwise get a
311
+ * "Re-authenticate?" prompt on every install, update and doctor run). The
312
+ * spawn is bounded, and "complete" is printed only when the sign-in is there
313
+ * afterwards: `opencode mcp auth` exits normally when the user declines or the
314
+ * wait times out.
315
+ *
316
+ * Only a V1 `opencode` is ever run. On V2 the command boots the user's
317
+ * background service, and an answer read from it right after a cold start is
318
+ * not reliable, so V2 gets a hint: nothing here can tell whether it is signed
319
+ * in. A V1 token proves nothing about V2, which does not read that file. An
320
+ * `opencode` that names no version may be V2, and is treated as V2.
321
+ *
322
+ * Never throws — the server is registered either way, so a sign-in that did
323
+ * not happen is a calm hint, not a warning.
324
+ */
325
+ function postRegister(opts) {
326
+ const name = opts.region.mcpName;
327
+ const installs = openCodeInstalls();
328
+ const notKnown = `it is not known whether "${name}" is signed in.`;
329
+ const v2Cannot = `OpenCode V2 keeps its own sign-in, which the installer cannot read: ${notKnown}`;
330
+ if (installs.cli === 'v1' || (installs.cli === 'none' && !installs.v2)) {
331
+ signInV1(opts, installs.v2 ? 'OpenCode V1' : 'OpenCode');
332
+ if (installs.v2) {
333
+ log.hint(v2Cannot);
334
+ log.hint(needsAuthHintFor('OpenCode V2', v2Command(`mcp auth ${name}`, installs)));
335
+ }
336
+ return;
337
+ }
338
+ log.hint(installs.v2
339
+ ? v2Cannot
340
+ : `\`opencode --version\` named no version, so the installer runs no sign-in with it: ${notKnown}`);
341
+ log.hint(needsAuthHintFor('OpenCode', v2Command(`mcp auth ${name}`, installs)));
342
+ }
343
+ /** The V1 half of `postRegister`: `label` names V1 when V2 is on the machine too. */
344
+ function signInV1(opts, label) {
345
+ const name = opts.region.mcpName;
346
+ const signedIn = opts.signedIn ?? (() => openCodeSignedIn(opts.region));
347
+ try {
348
+ if (signedIn()) {
349
+ log.hint(`${label} is already signed in to "${name}".`);
350
+ return;
351
+ }
352
+ }
353
+ catch {
354
+ /* an unreadable sign-in state is "not signed in" */
355
+ }
356
+ const cliOnPath = opts.cliOnPath ?? (() => which('opencode') !== null);
357
+ if (!cliOnPath()) {
358
+ log.hint('OpenCode CLI not on PATH — skipping `opencode mcp auth`.');
359
+ log.hint(needsAuthHint(name));
360
+ return;
361
+ }
362
+ const spawn = opts.spawn ??
363
+ ((cmd, args) => {
364
+ execFileSync(cmd, args, {
365
+ stdio: 'inherit',
366
+ timeout: OPENCODE_AUTH_TIMEOUT_MS,
367
+ // `opencode` may be a `.cmd` shim on Windows — needs a shell to resolve.
368
+ shell: process.platform === 'win32',
369
+ windowsHide: true,
370
+ });
371
+ });
372
+ log.step(`Authenticating ${label} MCP "${name}" (opencode mcp auth)`);
373
+ let done = false;
374
+ try {
375
+ spawn('opencode', ['mcp', 'auth', name]);
376
+ done = signedIn();
377
+ }
378
+ catch {
379
+ /* a failed, cancelled or timed-out sign-in: said below */
380
+ }
381
+ if (done) {
382
+ log.ok(`${label} MCP sign-in complete.`);
383
+ }
384
+ else {
385
+ log.hint(`${label} MCP "${name}" is registered, but its sign-in did not complete.`);
386
+ log.hint(needsAuthHint(name));
387
+ }
388
+ }
389
+ /**
390
+ * What a V2 must be told once the plugin is written (run on 2.2.1): its background
391
+ * service does not load a plugin file added while it runs, and goes on running the
392
+ * old code of one rewritten. `opencode run` and quitting the TUI leave it up. So a
393
+ * first install has no guard at all until it restarts. It comes before the sign-in:
394
+ * `opencode mcp auth` looks the server up in the running service, which answers
395
+ * `MCP server not found` for a few seconds after the entry is written, where a
396
+ * service just restarted has read it (`loadIntegrations` waits for its plugins).
397
+ * The installer runs neither: a command that boots or stops the user's service is
398
+ * theirs to run (V7). Nothing new for a machine with no sign of V2.
399
+ */
400
+ function activationStep() {
401
+ const installs = openCodeInstalls();
402
+ if (!mayRunV2(installs))
403
+ return null;
404
+ return (`OpenCode V2: run ${v2Command('service restart', installs)}. After a first install no guard is active until then, ` +
405
+ "and after an update the previous plugin stays loaded: V2's background service keeps running when you quit " +
406
+ 'OpenCode, and does not load a plugin written while it runs.');
407
+ }
408
+ /**
409
+ * The sign-in `postRegister` left to do, for the install's "Next steps": the
410
+ * `opencode` CLI is not on PATH (OpenCode may be present only as the desktop
411
+ * app), or `opencode mcp auth` ended without a sign-in. `null` once OpenCode is
412
+ * signed in, whether that is from an earlier run or from `postRegister` a
413
+ * moment ago. `openCodeSignedIn` never throws: a state it cannot read is "not
414
+ * signed in".
415
+ *
416
+ * With V2 on the machine the step stays, in words that say the installer cannot
417
+ * tell: nothing here reads V2's sign-in, and V1's token says nothing about it.
418
+ */
419
+ function mcpSignInStep(region) {
420
+ const name = region.mcpName;
421
+ const installs = openCodeInstalls();
422
+ const desktop = `in the desktop app, open the list of MCP servers and authenticate "${name}" there.`;
423
+ const v2Step = (label) => `${label}: if it is not signed in yet (the installer cannot tell), run ${v2Command(`mcp auth ${name}`, installs)}; ${desktop}`;
424
+ if (installs.cli === 'v1' && installs.v2) {
425
+ const v1Done = openCodeSignedIn(region);
426
+ return (v1Done ? '' : `OpenCode V1: run \`opencode mcp auth ${name}\`. `) + v2Step('OpenCode V2');
427
+ }
428
+ if (installs.v2 || installs.cli === 'unknown')
429
+ return v2Step('OpenCode');
430
+ if (openCodeSignedIn(region))
431
+ return null;
432
+ return `OpenCode: run \`opencode mcp auth ${name}\`; ${desktop}`;
433
+ }
434
+ /**
435
+ * Pure introspection — never throws. `hooksWired` is "the plugin file is
436
+ * ours"; `mcpRegistered` / `mcpUrl` come from `mcp.<name>.url` of the first of
437
+ * `opencode.jsonc`, `opencode.json` that carries one (the first is the one
438
+ * OpenCode lets win), `mcp.servers.<name>` first since V2 lets that one win —
439
+ * it must point at this region, not just exist. Both config reads strip a
440
+ * leading BOM first, and read the file as JSONC.
441
+ */
442
+ function inspect(region) {
443
+ let hooksWired = false;
444
+ try {
445
+ const pluginPath = openCodePluginPath();
446
+ if (existsSync(pluginPath))
447
+ hooksWired = isOurPlugin(readFileSync(pluginPath, 'utf8'));
448
+ }
449
+ catch {
450
+ /* missing/unreadable → false */
451
+ }
452
+ let registeredUrl;
453
+ for (const file of openCodeConfigFiles()) {
454
+ const read = () => stripBom(readFileSync(file, 'utf8'));
455
+ const native = nativeServers({ servers: readJsonServerEntry(read, 'mcp', 'servers') })?.[region.mcpName];
456
+ const entry = isRecord(native) ? native : readJsonServerEntry(read, 'mcp', region.mcpName);
457
+ if (typeof entry?.url === 'string' && entry.url.trim() !== '') {
458
+ registeredUrl = entry.url;
459
+ break;
460
+ }
461
+ }
462
+ return { hooksWired, ...mcpStatus(registeredUrl, region) };
463
+ }
464
+ /**
465
+ * Delete our plugin (only when it is ours) and our servers from `mcp` in both
466
+ * config files. Every other file, key and server is left untouched.
467
+ *
468
+ * The three files are independent, so every step always runs even when one
469
+ * throws (a malformed `opencode.jsonc` must not leave the plugin behind) —
470
+ * only once all have been attempted does this re-throw the FIRST error, if
471
+ * any.
472
+ */
473
+ function uninstall(opts) {
474
+ const errors = [];
475
+ const hookFiles = [];
476
+ try {
477
+ const pluginPath = openCodePluginPath();
478
+ if (existsSync(pluginPath) && isOurPlugin(readFileSync(pluginPath, 'utf8'))) {
479
+ rmSync(pluginPath);
480
+ hookFiles.push(pluginPath);
481
+ }
482
+ }
483
+ catch (err) {
484
+ errors.push(err);
485
+ }
486
+ const removed = new Set();
487
+ for (const file of openCodeConfigFiles()) {
488
+ try {
489
+ editJsonFile(file, (config) => {
490
+ const names = [
491
+ ...deleteKeys(nativeServers(config.mcp), opts.mcpNames),
492
+ ...deleteKeys(config.mcp, opts.mcpNames),
493
+ ];
494
+ for (const name of names)
495
+ removed.add(name);
496
+ return names.length > 0;
497
+ });
498
+ }
499
+ catch (err) {
500
+ errors.push(err);
501
+ }
502
+ }
503
+ if (errors.length > 0)
504
+ throw errors[0];
505
+ return { hookFiles, mcpRemoved: [...new Set(opts.mcpNames)].filter((name) => removed.has(name)) };
506
+ }
507
+ const REPO_PLUGIN = '.opencode/plugins/cybedefend-vibedefend.js';
508
+ const REPO_CONFIGS = ['opencode.jsonc', 'opencode.json'];
509
+ const REPO_RESTART_STEP = 'OpenCode V2: run "opencode service restart"';
510
+ const REPO_NEXT_STEP = (name) => `${REPO_RESTART_STEP}; sign in with "opencode mcp auth ${name}" from this folder`;
511
+ /** The repo config to write: `opencode.jsonc` when it exists, else `opencode.json`. */
512
+ function repoConfigRel(root) {
513
+ return existsSync(join(root, REPO_CONFIGS[0])) ? REPO_CONFIGS[0] : REPO_CONFIGS[1];
514
+ }
515
+ function wireRepoHooks(ctx) {
516
+ const root = ctx.tree.root;
517
+ if (ctx.isTracked(root, REPO_PLUGIN)) {
518
+ return { client: 'opencode', piece: 'hooks', state: 'off', files: [], reason: trackedReason(REPO_PLUGIN) };
519
+ }
520
+ try {
521
+ writeOpenCodePluginAt(join(root, REPO_PLUGIN), ctx.hooks.enableSessionReview);
522
+ }
523
+ catch (err) {
524
+ return { client: 'opencode', piece: 'hooks', state: 'off', files: [], reason: err.message };
525
+ }
526
+ const result = { client: 'opencode', piece: 'hooks', state: 'on', files: [{ root, rel: REPO_PLUGIN }] };
527
+ // The MCP piece names the restart OpenCode V2 needs to load the plugin; a
528
+ // tracked config leaves that piece off, so the plugin names it instead.
529
+ if (ctx.isTracked(root, repoConfigRel(root)))
530
+ result.nextStep = REPO_RESTART_STEP;
531
+ return result;
532
+ }
533
+ function wireRepoMcp(ctx) {
534
+ const root = ctx.tree.root;
535
+ const rel = repoConfigRel(root);
536
+ const isNew = !existsSync(join(root, rel));
537
+ let r;
538
+ try {
539
+ r = editRepoJson(root, rel, ctx.isTracked, (config) => {
540
+ applyOpenCodeMcpEntry(config, ctx.region, isNew, rel);
541
+ });
542
+ }
543
+ catch (err) {
544
+ return { client: 'opencode', piece: 'mcp', state: 'off', files: [], reason: err.message };
545
+ }
546
+ if (!r.ok)
547
+ return { client: 'opencode', piece: 'mcp', state: 'off', files: [], reason: r.reason };
548
+ return {
549
+ client: 'opencode',
550
+ piece: 'mcp',
551
+ state: 'on',
552
+ files: [r.file],
553
+ nextStep: REPO_NEXT_STEP(ctx.region.mcpName),
554
+ };
555
+ }
556
+ function readRepoConfig(abs, rel) {
557
+ try {
558
+ const parsed = parseJsonc(stripBom(readFileSync(abs, 'utf8')), rel);
559
+ return isRecord(parsed) ? parsed : null;
560
+ }
561
+ catch {
562
+ return null;
563
+ }
564
+ }
565
+ function unwireRepo(tree, mcpNames, _run, tracked = (root, rel) => isTracked({ root }, rel)) {
566
+ const root = tree.root;
567
+ const files = [];
568
+ const pluginAbs = join(root, REPO_PLUGIN);
569
+ if (existsSync(pluginAbs) && !tracked(root, REPO_PLUGIN)) {
570
+ try {
571
+ if (isOurPlugin(readFileSync(pluginAbs, 'utf8'))) {
572
+ rmSync(pluginAbs, { force: true });
573
+ files.push({ root, rel: REPO_PLUGIN });
574
+ }
575
+ }
576
+ catch {
577
+ /* unreadable: left alone */
578
+ }
579
+ }
580
+ for (const rel of REPO_CONFIGS) {
581
+ const abs = join(root, rel);
582
+ if (!existsSync(abs) || tracked(root, rel))
583
+ continue;
584
+ const config = readRepoConfig(abs, rel);
585
+ if (!config)
586
+ continue;
587
+ const removed = [
588
+ ...deleteKeys(nativeServers(config.mcp), mcpNames),
589
+ ...deleteKeys(config.mcp, mcpNames),
590
+ ];
591
+ if (removed.length === 0)
592
+ continue;
593
+ if (isRecord(config.mcp) && Object.keys(config.mcp).length === 0)
594
+ delete config.mcp;
595
+ if (Object.keys(config).every((k) => k === '$schema'))
596
+ rmSync(abs, { force: true });
597
+ else
598
+ writeJson(abs, config);
599
+ files.push({ root, rel });
600
+ }
601
+ // Remove our folders only when they are now empty (rmdir refuses otherwise).
602
+ for (const dir of ['.opencode/plugins', '.opencode']) {
603
+ try {
604
+ if (readdirSync(join(root, dir)).length === 0)
605
+ rmdirSync(join(root, dir));
606
+ }
607
+ catch {
608
+ /* absent or not empty */
609
+ }
610
+ }
611
+ return files;
612
+ }
613
+ function inspectRepo(tree, region) {
614
+ let hooksWired = false;
615
+ try {
616
+ hooksWired = isOurPlugin(readFileSync(join(tree.root, REPO_PLUGIN), 'utf8'));
617
+ }
618
+ catch {
619
+ /* missing or unreadable */
620
+ }
621
+ let registeredUrl;
622
+ for (const rel of REPO_CONFIGS) {
623
+ const read = () => stripBom(readFileSync(join(tree.root, rel), 'utf8'));
624
+ const native = nativeServers({ servers: readJsonServerEntry(read, 'mcp', 'servers') })?.[region.mcpName];
625
+ const entry = isRecord(native) ? native : readJsonServerEntry(read, 'mcp', region.mcpName);
626
+ if (typeof entry?.url === 'string' && entry.url.trim() !== '') {
627
+ registeredUrl = entry.url;
628
+ break;
629
+ }
630
+ }
631
+ return { hooksWired, mcpRegistered: registeredUrl === region.mcpUrl };
632
+ }
633
+ const repo = {
634
+ wireHooks: wireRepoHooks,
635
+ wireMcp: wireRepoMcp,
636
+ unwire: unwireRepo,
637
+ inspect: inspectRepo,
638
+ };
639
+ export const openCodeAdapter = {
640
+ id: 'opencode',
641
+ label: 'OpenCode',
642
+ detect,
643
+ supports,
644
+ writeSettings,
645
+ registerMcp,
646
+ postRegister,
647
+ activationStep,
648
+ mcpSignInStep,
649
+ inspect,
650
+ uninstall,
651
+ repo,
652
+ };
653
+ //# sourceMappingURL=opencode.js.map