koro-ai 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (670) hide show
  1. package/AGENTS.md +69 -0
  2. package/BUN_SHELL_MIGRATION_PLAN.md +136 -0
  3. package/Dockerfile +18 -0
  4. package/README.md +15 -0
  5. package/bin/koro +16 -0
  6. package/bin/opencode +179 -0
  7. package/bunfig.toml +7 -0
  8. package/drizzle.config.ts +10 -0
  9. package/git +0 -0
  10. package/migration/20260127222353_familiar_lady_ursula/migration.sql +90 -0
  11. package/migration/20260127222353_familiar_lady_ursula/snapshot.json +796 -0
  12. package/migration/20260211171708_add_project_commands/migration.sql +1 -0
  13. package/migration/20260211171708_add_project_commands/snapshot.json +806 -0
  14. package/migration/20260213144116_wakeful_the_professor/migration.sql +11 -0
  15. package/migration/20260213144116_wakeful_the_professor/snapshot.json +897 -0
  16. package/migration/20260225215848_workspace/migration.sql +7 -0
  17. package/migration/20260225215848_workspace/snapshot.json +959 -0
  18. package/migration/20260227213759_add_session_workspace_id/migration.sql +2 -0
  19. package/migration/20260227213759_add_session_workspace_id/snapshot.json +983 -0
  20. package/migration/20260228203230_blue_harpoon/migration.sql +17 -0
  21. package/migration/20260228203230_blue_harpoon/snapshot.json +1102 -0
  22. package/migration/20260303231226_add_workspace_fields/migration.sql +5 -0
  23. package/migration/20260303231226_add_workspace_fields/snapshot.json +1013 -0
  24. package/migration/20260309230000_move_org_to_state/migration.sql +3 -0
  25. package/migration/20260309230000_move_org_to_state/snapshot.json +1156 -0
  26. package/migration/20260312043431_session_message_cursor/migration.sql +4 -0
  27. package/migration/20260312043431_session_message_cursor/snapshot.json +1168 -0
  28. package/migration/20260323234822_events/migration.sql +13 -0
  29. package/migration/20260323234822_events/snapshot.json +1271 -0
  30. package/package.json +163 -0
  31. package/parsers-config.ts +290 -0
  32. package/script/build-node.ts +56 -0
  33. package/script/build.ts +276 -0
  34. package/script/check-migrations.ts +16 -0
  35. package/script/postinstall.mjs +131 -0
  36. package/script/publish.ts +181 -0
  37. package/script/schema.ts +63 -0
  38. package/script/seed-e2e.ts +60 -0
  39. package/script/upgrade-opentui.ts +64 -0
  40. package/specs/effect-migration.md +294 -0
  41. package/specs/tui-plugins.md +436 -0
  42. package/src/account/account.sql.ts +39 -0
  43. package/src/account/index.ts +424 -0
  44. package/src/account/repo.ts +163 -0
  45. package/src/account/schema.ts +91 -0
  46. package/src/acp/README.md +174 -0
  47. package/src/acp/agent.ts +1763 -0
  48. package/src/acp/session.ts +116 -0
  49. package/src/acp/types.ts +24 -0
  50. package/src/agent/agent.ts +476 -0
  51. package/src/agent/generate.txt +75 -0
  52. package/src/agent/prompt/compaction.txt +15 -0
  53. package/src/agent/prompt/explore.txt +18 -0
  54. package/src/agent/prompt/summary.txt +11 -0
  55. package/src/agent/prompt/title.txt +44 -0
  56. package/src/auth/index.ts +109 -0
  57. package/src/bus/bus-event.ts +40 -0
  58. package/src/bus/global.ts +10 -0
  59. package/src/bus/index.ts +185 -0
  60. package/src/cli/bootstrap.ts +17 -0
  61. package/src/cli/cmd/account.ts +257 -0
  62. package/src/cli/cmd/acp.ts +70 -0
  63. package/src/cli/cmd/agent.ts +245 -0
  64. package/src/cli/cmd/cmd.ts +7 -0
  65. package/src/cli/cmd/db.ts +119 -0
  66. package/src/cli/cmd/debug/agent.ts +167 -0
  67. package/src/cli/cmd/debug/config.ts +16 -0
  68. package/src/cli/cmd/debug/file.ts +97 -0
  69. package/src/cli/cmd/debug/index.ts +48 -0
  70. package/src/cli/cmd/debug/lsp.ts +53 -0
  71. package/src/cli/cmd/debug/ripgrep.ts +87 -0
  72. package/src/cli/cmd/debug/scrap.ts +16 -0
  73. package/src/cli/cmd/debug/skill.ts +16 -0
  74. package/src/cli/cmd/debug/snapshot.ts +52 -0
  75. package/src/cli/cmd/export.ts +89 -0
  76. package/src/cli/cmd/generate.ts +38 -0
  77. package/src/cli/cmd/github.ts +1646 -0
  78. package/src/cli/cmd/import.ts +207 -0
  79. package/src/cli/cmd/mcp.ts +754 -0
  80. package/src/cli/cmd/models.ts +78 -0
  81. package/src/cli/cmd/plug.ts +233 -0
  82. package/src/cli/cmd/pr.ts +127 -0
  83. package/src/cli/cmd/providers.ts +478 -0
  84. package/src/cli/cmd/run.ts +676 -0
  85. package/src/cli/cmd/serve.ts +24 -0
  86. package/src/cli/cmd/session.ts +159 -0
  87. package/src/cli/cmd/stats.ts +410 -0
  88. package/src/cli/cmd/tui/app.tsx +919 -0
  89. package/src/cli/cmd/tui/attach.ts +88 -0
  90. package/src/cli/cmd/tui/component/border.tsx +21 -0
  91. package/src/cli/cmd/tui/component/dialog-agent.tsx +31 -0
  92. package/src/cli/cmd/tui/component/dialog-command.tsx +171 -0
  93. package/src/cli/cmd/tui/component/dialog-import-share.tsx +118 -0
  94. package/src/cli/cmd/tui/component/dialog-mcp.tsx +86 -0
  95. package/src/cli/cmd/tui/component/dialog-model.tsx +179 -0
  96. package/src/cli/cmd/tui/component/dialog-provider.tsx +329 -0
  97. package/src/cli/cmd/tui/component/dialog-session-list.tsx +108 -0
  98. package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
  99. package/src/cli/cmd/tui/component/dialog-skill.tsx +36 -0
  100. package/src/cli/cmd/tui/component/dialog-stash.tsx +87 -0
  101. package/src/cli/cmd/tui/component/dialog-status.tsx +168 -0
  102. package/src/cli/cmd/tui/component/dialog-tag.tsx +44 -0
  103. package/src/cli/cmd/tui/component/dialog-theme-list.tsx +50 -0
  104. package/src/cli/cmd/tui/component/dialog-variant.tsx +39 -0
  105. package/src/cli/cmd/tui/component/dialog-workspace-list.tsx +320 -0
  106. package/src/cli/cmd/tui/component/error-component.tsx +92 -0
  107. package/src/cli/cmd/tui/component/logo.tsx +85 -0
  108. package/src/cli/cmd/tui/component/plugin-route-missing.tsx +14 -0
  109. package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +672 -0
  110. package/src/cli/cmd/tui/component/prompt/frecency.tsx +90 -0
  111. package/src/cli/cmd/tui/component/prompt/history.tsx +108 -0
  112. package/src/cli/cmd/tui/component/prompt/index.tsx +1310 -0
  113. package/src/cli/cmd/tui/component/prompt/part.ts +16 -0
  114. package/src/cli/cmd/tui/component/prompt/stash.tsx +101 -0
  115. package/src/cli/cmd/tui/component/spinner.tsx +24 -0
  116. package/src/cli/cmd/tui/component/startup-loading.tsx +63 -0
  117. package/src/cli/cmd/tui/component/task-panel.tsx +44 -0
  118. package/src/cli/cmd/tui/component/textarea-keybindings.ts +73 -0
  119. package/src/cli/cmd/tui/component/todo-item.tsx +32 -0
  120. package/src/cli/cmd/tui/component/token-bar.tsx +60 -0
  121. package/src/cli/cmd/tui/component/workspace/dialog-session-list.tsx +151 -0
  122. package/src/cli/cmd/tui/context/args.tsx +15 -0
  123. package/src/cli/cmd/tui/context/directory.ts +13 -0
  124. package/src/cli/cmd/tui/context/exit.tsx +60 -0
  125. package/src/cli/cmd/tui/context/helper.tsx +25 -0
  126. package/src/cli/cmd/tui/context/keybind.tsx +105 -0
  127. package/src/cli/cmd/tui/context/kv.tsx +52 -0
  128. package/src/cli/cmd/tui/context/local.tsx +412 -0
  129. package/src/cli/cmd/tui/context/plugin-keybinds.ts +41 -0
  130. package/src/cli/cmd/tui/context/prompt.tsx +18 -0
  131. package/src/cli/cmd/tui/context/route.tsx +52 -0
  132. package/src/cli/cmd/tui/context/sdk.tsx +128 -0
  133. package/src/cli/cmd/tui/context/sync.tsx +504 -0
  134. package/src/cli/cmd/tui/context/theme/aura.json +69 -0
  135. package/src/cli/cmd/tui/context/theme/ayu.json +80 -0
  136. package/src/cli/cmd/tui/context/theme/carbonfox.json +248 -0
  137. package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +233 -0
  138. package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +233 -0
  139. package/src/cli/cmd/tui/context/theme/catppuccin.json +112 -0
  140. package/src/cli/cmd/tui/context/theme/cobalt2.json +228 -0
  141. package/src/cli/cmd/tui/context/theme/cursor.json +249 -0
  142. package/src/cli/cmd/tui/context/theme/dracula.json +219 -0
  143. package/src/cli/cmd/tui/context/theme/everforest.json +241 -0
  144. package/src/cli/cmd/tui/context/theme/flexoki.json +237 -0
  145. package/src/cli/cmd/tui/context/theme/github.json +233 -0
  146. package/src/cli/cmd/tui/context/theme/gruvbox.json +242 -0
  147. package/src/cli/cmd/tui/context/theme/gtr.json +245 -0
  148. package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -0
  149. package/src/cli/cmd/tui/context/theme/koro.json +241 -0
  150. package/src/cli/cmd/tui/context/theme/lucent-orng.json +237 -0
  151. package/src/cli/cmd/tui/context/theme/material.json +235 -0
  152. package/src/cli/cmd/tui/context/theme/matrix.json +77 -0
  153. package/src/cli/cmd/tui/context/theme/mercury.json +252 -0
  154. package/src/cli/cmd/tui/context/theme/monokai.json +221 -0
  155. package/src/cli/cmd/tui/context/theme/nightowl.json +221 -0
  156. package/src/cli/cmd/tui/context/theme/nord.json +223 -0
  157. package/src/cli/cmd/tui/context/theme/one-dark.json +84 -0
  158. package/src/cli/cmd/tui/context/theme/opencode.json +245 -0
  159. package/src/cli/cmd/tui/context/theme/orng.json +249 -0
  160. package/src/cli/cmd/tui/context/theme/osaka-jade.json +93 -0
  161. package/src/cli/cmd/tui/context/theme/palenight.json +222 -0
  162. package/src/cli/cmd/tui/context/theme/rosepine.json +234 -0
  163. package/src/cli/cmd/tui/context/theme/solarized.json +223 -0
  164. package/src/cli/cmd/tui/context/theme/synthwave84.json +226 -0
  165. package/src/cli/cmd/tui/context/theme/tokyonight.json +243 -0
  166. package/src/cli/cmd/tui/context/theme/vercel.json +245 -0
  167. package/src/cli/cmd/tui/context/theme/vesper.json +218 -0
  168. package/src/cli/cmd/tui/context/theme/zenburn.json +223 -0
  169. package/src/cli/cmd/tui/context/theme.tsx +1240 -0
  170. package/src/cli/cmd/tui/context/tui-config.tsx +9 -0
  171. package/src/cli/cmd/tui/event.ts +49 -0
  172. package/src/cli/cmd/tui/feature-plugins/home/footer.tsx +93 -0
  173. package/src/cli/cmd/tui/feature-plugins/home/tips-view.tsx +152 -0
  174. package/src/cli/cmd/tui/feature-plugins/home/tips.tsx +50 -0
  175. package/src/cli/cmd/tui/feature-plugins/sidebar/context.tsx +63 -0
  176. package/src/cli/cmd/tui/feature-plugins/sidebar/files.tsx +62 -0
  177. package/src/cli/cmd/tui/feature-plugins/sidebar/footer.tsx +93 -0
  178. package/src/cli/cmd/tui/feature-plugins/sidebar/lsp.tsx +66 -0
  179. package/src/cli/cmd/tui/feature-plugins/sidebar/mcp.tsx +96 -0
  180. package/src/cli/cmd/tui/feature-plugins/sidebar/todo.tsx +48 -0
  181. package/src/cli/cmd/tui/feature-plugins/system/plugins.tsx +270 -0
  182. package/src/cli/cmd/tui/plugin/api.tsx +430 -0
  183. package/src/cli/cmd/tui/plugin/index.ts +3 -0
  184. package/src/cli/cmd/tui/plugin/internal.ts +27 -0
  185. package/src/cli/cmd/tui/plugin/runtime.ts +1033 -0
  186. package/src/cli/cmd/tui/plugin/slots.tsx +60 -0
  187. package/src/cli/cmd/tui/routes/home.tsx +84 -0
  188. package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +65 -0
  189. package/src/cli/cmd/tui/routes/session/dialog-message.tsx +110 -0
  190. package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +26 -0
  191. package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +47 -0
  192. package/src/cli/cmd/tui/routes/session/footer.tsx +93 -0
  193. package/src/cli/cmd/tui/routes/session/index.tsx +2270 -0
  194. package/src/cli/cmd/tui/routes/session/permission.tsx +691 -0
  195. package/src/cli/cmd/tui/routes/session/question.tsx +468 -0
  196. package/src/cli/cmd/tui/routes/session/sidebar.tsx +74 -0
  197. package/src/cli/cmd/tui/routes/session/subagent-footer.tsx +131 -0
  198. package/src/cli/cmd/tui/thread.ts +232 -0
  199. package/src/cli/cmd/tui/ui/dialog-alert.tsx +59 -0
  200. package/src/cli/cmd/tui/ui/dialog-confirm.tsx +89 -0
  201. package/src/cli/cmd/tui/ui/dialog-export-options.tsx +211 -0
  202. package/src/cli/cmd/tui/ui/dialog-help.tsx +40 -0
  203. package/src/cli/cmd/tui/ui/dialog-prompt.tsx +130 -0
  204. package/src/cli/cmd/tui/ui/dialog-select.tsx +409 -0
  205. package/src/cli/cmd/tui/ui/dialog.tsx +192 -0
  206. package/src/cli/cmd/tui/ui/link.tsx +28 -0
  207. package/src/cli/cmd/tui/ui/spinner.ts +368 -0
  208. package/src/cli/cmd/tui/ui/toast.tsx +100 -0
  209. package/src/cli/cmd/tui/util/clipboard.ts +192 -0
  210. package/src/cli/cmd/tui/util/editor.ts +37 -0
  211. package/src/cli/cmd/tui/util/model.ts +23 -0
  212. package/src/cli/cmd/tui/util/scroll.ts +23 -0
  213. package/src/cli/cmd/tui/util/selection.ts +25 -0
  214. package/src/cli/cmd/tui/util/signal.ts +7 -0
  215. package/src/cli/cmd/tui/util/terminal.ts +114 -0
  216. package/src/cli/cmd/tui/util/transcript.ts +112 -0
  217. package/src/cli/cmd/tui/win32.ts +129 -0
  218. package/src/cli/cmd/tui/worker.ts +175 -0
  219. package/src/cli/cmd/uninstall.ts +353 -0
  220. package/src/cli/cmd/upgrade.ts +73 -0
  221. package/src/cli/cmd/web.ts +81 -0
  222. package/src/cli/effect/prompt.ts +25 -0
  223. package/src/cli/error.ts +46 -0
  224. package/src/cli/heap.ts +59 -0
  225. package/src/cli/logo.ts +14 -0
  226. package/src/cli/network.ts +60 -0
  227. package/src/cli/ui.ts +133 -0
  228. package/src/cli/upgrade.ts +31 -0
  229. package/src/command/index.ts +195 -0
  230. package/src/command/template/initialize.txt +66 -0
  231. package/src/command/template/review.txt +101 -0
  232. package/src/config/config.ts +1591 -0
  233. package/src/config/markdown.ts +99 -0
  234. package/src/config/paths.ts +181 -0
  235. package/src/config/tui-migrate.ts +155 -0
  236. package/src/config/tui-schema.ts +36 -0
  237. package/src/config/tui.ts +171 -0
  238. package/src/control-plane/adaptors/index.ts +20 -0
  239. package/src/control-plane/adaptors/worktree.ts +38 -0
  240. package/src/control-plane/schema.ts +17 -0
  241. package/src/control-plane/sse.ts +66 -0
  242. package/src/control-plane/types.ts +21 -0
  243. package/src/control-plane/workspace.sql.ts +17 -0
  244. package/src/control-plane/workspace.ts +154 -0
  245. package/src/effect/cross-spawn-spawner.ts +502 -0
  246. package/src/effect/instance-ref.ts +6 -0
  247. package/src/effect/instance-registry.ts +12 -0
  248. package/src/effect/instance-state.ts +82 -0
  249. package/src/effect/run-service.ts +33 -0
  250. package/src/effect/runner.ts +216 -0
  251. package/src/env/index.ts +28 -0
  252. package/src/file/ignore.ts +82 -0
  253. package/src/file/index.ts +686 -0
  254. package/src/file/protected.ts +59 -0
  255. package/src/file/ripgrep.ts +376 -0
  256. package/src/file/time.ts +133 -0
  257. package/src/file/watcher.ts +171 -0
  258. package/src/filesystem/index.ts +226 -0
  259. package/src/flag/flag.ts +155 -0
  260. package/src/format/formatter.ts +413 -0
  261. package/src/format/index.ts +203 -0
  262. package/src/git/index.ts +303 -0
  263. package/src/global/index.ts +161 -0
  264. package/src/id/id.ts +85 -0
  265. package/src/ide/index.ts +74 -0
  266. package/src/index.ts +240 -0
  267. package/src/installation/index.ts +355 -0
  268. package/src/installation/meta.ts +7 -0
  269. package/src/lsp/client.ts +252 -0
  270. package/src/lsp/index.ts +558 -0
  271. package/src/lsp/language.ts +120 -0
  272. package/src/lsp/launch.ts +21 -0
  273. package/src/lsp/server.ts +1958 -0
  274. package/src/mcp/auth.ts +173 -0
  275. package/src/mcp/index.ts +921 -0
  276. package/src/mcp/oauth-callback.ts +215 -0
  277. package/src/mcp/oauth-provider.ts +185 -0
  278. package/src/memory/index.ts +117 -0
  279. package/src/node.ts +1 -0
  280. package/src/npm/index.ts +180 -0
  281. package/src/orchestrator/agent-registry.ts +38 -0
  282. package/src/orchestrator/conflict.ts +25 -0
  283. package/src/orchestrator/context-manager.ts +22 -0
  284. package/src/orchestrator/index.ts +9 -0
  285. package/src/orchestrator/scheduler.ts +30 -0
  286. package/src/orchestrator/state-tracker.ts +71 -0
  287. package/src/orchestrator/task-manager.ts +69 -0
  288. package/src/patch/index.ts +680 -0
  289. package/src/permission/arity.ts +163 -0
  290. package/src/permission/evaluate.ts +15 -0
  291. package/src/permission/index.ts +325 -0
  292. package/src/permission/schema.ts +17 -0
  293. package/src/plugin/codex.ts +596 -0
  294. package/src/plugin/github-copilot/copilot.ts +353 -0
  295. package/src/plugin/github-copilot/models.ts +144 -0
  296. package/src/plugin/index.ts +281 -0
  297. package/src/plugin/install.ts +439 -0
  298. package/src/plugin/loader.ts +174 -0
  299. package/src/plugin/meta.ts +188 -0
  300. package/src/plugin/shared.ts +307 -0
  301. package/src/project/bootstrap.ts +31 -0
  302. package/src/project/instance.ts +175 -0
  303. package/src/project/project.sql.ts +16 -0
  304. package/src/project/project.ts +519 -0
  305. package/src/project/schema.ts +16 -0
  306. package/src/project/state.ts +70 -0
  307. package/src/project/vcs.ts +240 -0
  308. package/src/provider/auth.ts +253 -0
  309. package/src/provider/error.ts +197 -0
  310. package/src/provider/models-snapshot.ts +60410 -0
  311. package/src/provider/models.ts +162 -0
  312. package/src/provider/provider.ts +1677 -0
  313. package/src/provider/schema.ts +38 -0
  314. package/src/provider/sdk/copilot/README.md +5 -0
  315. package/src/provider/sdk/copilot/chat/convert-to-openai-compatible-chat-messages.ts +170 -0
  316. package/src/provider/sdk/copilot/chat/get-response-metadata.ts +15 -0
  317. package/src/provider/sdk/copilot/chat/map-openai-compatible-finish-reason.ts +19 -0
  318. package/src/provider/sdk/copilot/chat/openai-compatible-api-types.ts +64 -0
  319. package/src/provider/sdk/copilot/chat/openai-compatible-chat-language-model.ts +815 -0
  320. package/src/provider/sdk/copilot/chat/openai-compatible-chat-options.ts +28 -0
  321. package/src/provider/sdk/copilot/chat/openai-compatible-metadata-extractor.ts +44 -0
  322. package/src/provider/sdk/copilot/chat/openai-compatible-prepare-tools.ts +83 -0
  323. package/src/provider/sdk/copilot/copilot-provider.ts +100 -0
  324. package/src/provider/sdk/copilot/index.ts +2 -0
  325. package/src/provider/sdk/copilot/openai-compatible-error.ts +27 -0
  326. package/src/provider/sdk/copilot/responses/convert-to-openai-responses-input.ts +335 -0
  327. package/src/provider/sdk/copilot/responses/map-openai-responses-finish-reason.ts +22 -0
  328. package/src/provider/sdk/copilot/responses/openai-config.ts +18 -0
  329. package/src/provider/sdk/copilot/responses/openai-error.ts +22 -0
  330. package/src/provider/sdk/copilot/responses/openai-responses-api-types.ts +214 -0
  331. package/src/provider/sdk/copilot/responses/openai-responses-language-model.ts +1769 -0
  332. package/src/provider/sdk/copilot/responses/openai-responses-prepare-tools.ts +173 -0
  333. package/src/provider/sdk/copilot/responses/openai-responses-settings.ts +1 -0
  334. package/src/provider/sdk/copilot/responses/tool/code-interpreter.ts +87 -0
  335. package/src/provider/sdk/copilot/responses/tool/file-search.ts +127 -0
  336. package/src/provider/sdk/copilot/responses/tool/image-generation.ts +114 -0
  337. package/src/provider/sdk/copilot/responses/tool/local-shell.ts +64 -0
  338. package/src/provider/sdk/copilot/responses/tool/web-search-preview.ts +103 -0
  339. package/src/provider/sdk/copilot/responses/tool/web-search.ts +102 -0
  340. package/src/provider/transform.ts +1046 -0
  341. package/src/pty/index.ts +401 -0
  342. package/src/pty/schema.ts +17 -0
  343. package/src/question/index.ts +224 -0
  344. package/src/question/schema.ts +17 -0
  345. package/src/server/error.ts +36 -0
  346. package/src/server/event.ts +7 -0
  347. package/src/server/instance.ts +314 -0
  348. package/src/server/mdns.ts +60 -0
  349. package/src/server/middleware.ts +33 -0
  350. package/src/server/projectors.ts +28 -0
  351. package/src/server/router.ts +99 -0
  352. package/src/server/routes/config.ts +92 -0
  353. package/src/server/routes/event.ts +83 -0
  354. package/src/server/routes/experimental.ts +271 -0
  355. package/src/server/routes/file.ts +197 -0
  356. package/src/server/routes/global.ts +312 -0
  357. package/src/server/routes/mcp.ts +225 -0
  358. package/src/server/routes/permission.ts +69 -0
  359. package/src/server/routes/project.ts +118 -0
  360. package/src/server/routes/provider.ts +171 -0
  361. package/src/server/routes/pty.ts +211 -0
  362. package/src/server/routes/question.ts +99 -0
  363. package/src/server/routes/session.ts +1031 -0
  364. package/src/server/routes/tui.ts +379 -0
  365. package/src/server/routes/workspace.ts +94 -0
  366. package/src/server/server.ts +312 -0
  367. package/src/session/compaction.ts +428 -0
  368. package/src/session/index.ts +887 -0
  369. package/src/session/instruction.ts +258 -0
  370. package/src/session/llm.ts +370 -0
  371. package/src/session/message-v2.ts +1031 -0
  372. package/src/session/message.ts +191 -0
  373. package/src/session/overflow.ts +22 -0
  374. package/src/session/processor.ts +523 -0
  375. package/src/session/projectors.ts +135 -0
  376. package/src/session/prompt/anthropic.txt +105 -0
  377. package/src/session/prompt/beast.txt +147 -0
  378. package/src/session/prompt/build-switch.txt +5 -0
  379. package/src/session/prompt/codex.txt +79 -0
  380. package/src/session/prompt/copilot-gpt-5.txt +143 -0
  381. package/src/session/prompt/default.txt +105 -0
  382. package/src/session/prompt/gemini.txt +155 -0
  383. package/src/session/prompt/gpt.txt +107 -0
  384. package/src/session/prompt/kimi.txt +114 -0
  385. package/src/session/prompt/max-steps.txt +16 -0
  386. package/src/session/prompt/plan-reminder-anthropic.txt +67 -0
  387. package/src/session/prompt/plan.txt +26 -0
  388. package/src/session/prompt/trinity.txt +97 -0
  389. package/src/session/prompt.ts +1908 -0
  390. package/src/session/retry.ts +106 -0
  391. package/src/session/revert.ts +176 -0
  392. package/src/session/schema.ts +38 -0
  393. package/src/session/session.sql.ts +103 -0
  394. package/src/session/status.ts +102 -0
  395. package/src/session/summary.ts +177 -0
  396. package/src/session/system.ts +76 -0
  397. package/src/session/todo.ts +95 -0
  398. package/src/share/share-next.ts +369 -0
  399. package/src/share/share.sql.ts +13 -0
  400. package/src/shell/shell.ts +110 -0
  401. package/src/skill/discovery.ts +116 -0
  402. package/src/skill/index.ts +277 -0
  403. package/src/snapshot/index.ts +571 -0
  404. package/src/sql.d.ts +4 -0
  405. package/src/storage/db.bun.ts +8 -0
  406. package/src/storage/db.node.ts +8 -0
  407. package/src/storage/db.ts +174 -0
  408. package/src/storage/json-migration.ts +425 -0
  409. package/src/storage/schema.sql.ts +10 -0
  410. package/src/storage/schema.ts +5 -0
  411. package/src/storage/storage.ts +353 -0
  412. package/src/sync/README.md +179 -0
  413. package/src/sync/event.sql.ts +16 -0
  414. package/src/sync/index.ts +263 -0
  415. package/src/sync/schema.ts +14 -0
  416. package/src/token/index.ts +77 -0
  417. package/src/tool/apply_patch.ts +281 -0
  418. package/src/tool/apply_patch.txt +33 -0
  419. package/src/tool/bash.ts +496 -0
  420. package/src/tool/bash.txt +117 -0
  421. package/src/tool/batch.ts +183 -0
  422. package/src/tool/batch.txt +24 -0
  423. package/src/tool/codesearch.ts +132 -0
  424. package/src/tool/codesearch.txt +12 -0
  425. package/src/tool/edit.ts +667 -0
  426. package/src/tool/edit.txt +10 -0
  427. package/src/tool/external-directory.ts +37 -0
  428. package/src/tool/glob.ts +78 -0
  429. package/src/tool/glob.txt +6 -0
  430. package/src/tool/grep.ts +156 -0
  431. package/src/tool/grep.txt +8 -0
  432. package/src/tool/invalid.ts +17 -0
  433. package/src/tool/ls.ts +121 -0
  434. package/src/tool/ls.txt +1 -0
  435. package/src/tool/lsp.ts +97 -0
  436. package/src/tool/lsp.txt +19 -0
  437. package/src/tool/multiedit.ts +46 -0
  438. package/src/tool/multiedit.txt +41 -0
  439. package/src/tool/plan-enter.txt +14 -0
  440. package/src/tool/plan-exit.txt +13 -0
  441. package/src/tool/plan.ts +131 -0
  442. package/src/tool/question.ts +46 -0
  443. package/src/tool/question.txt +10 -0
  444. package/src/tool/read.ts +296 -0
  445. package/src/tool/read.txt +14 -0
  446. package/src/tool/registry.ts +248 -0
  447. package/src/tool/schema.ts +17 -0
  448. package/src/tool/skill.ts +105 -0
  449. package/src/tool/task.ts +166 -0
  450. package/src/tool/task.txt +60 -0
  451. package/src/tool/todo.ts +48 -0
  452. package/src/tool/todowrite.txt +167 -0
  453. package/src/tool/tool.ts +112 -0
  454. package/src/tool/truncate.ts +144 -0
  455. package/src/tool/truncation-dir.ts +4 -0
  456. package/src/tool/webfetch.ts +206 -0
  457. package/src/tool/webfetch.txt +13 -0
  458. package/src/tool/websearch.ts +150 -0
  459. package/src/tool/websearch.txt +14 -0
  460. package/src/tool/write.ts +84 -0
  461. package/src/tool/write.txt +8 -0
  462. package/src/util/abort.ts +35 -0
  463. package/src/util/archive.ts +17 -0
  464. package/src/util/color.ts +19 -0
  465. package/src/util/context.ts +25 -0
  466. package/src/util/data-url.ts +9 -0
  467. package/src/util/defer.ts +12 -0
  468. package/src/util/effect-http-client.ts +11 -0
  469. package/src/util/effect-zod.ts +98 -0
  470. package/src/util/error.ts +77 -0
  471. package/src/util/filesystem.ts +245 -0
  472. package/src/util/flock.ts +333 -0
  473. package/src/util/fn.ts +21 -0
  474. package/src/util/format.ts +20 -0
  475. package/src/util/glob.ts +34 -0
  476. package/src/util/hash.ts +7 -0
  477. package/src/util/iife.ts +3 -0
  478. package/src/util/keybind.ts +103 -0
  479. package/src/util/lazy.ts +23 -0
  480. package/src/util/locale.ts +81 -0
  481. package/src/util/lock.ts +98 -0
  482. package/src/util/log.ts +182 -0
  483. package/src/util/network.ts +9 -0
  484. package/src/util/process.ts +176 -0
  485. package/src/util/queue.ts +32 -0
  486. package/src/util/record.ts +3 -0
  487. package/src/util/rpc.ts +66 -0
  488. package/src/util/schema.ts +53 -0
  489. package/src/util/scrap.ts +10 -0
  490. package/src/util/signal.ts +12 -0
  491. package/src/util/timeout.ts +14 -0
  492. package/src/util/token.ts +7 -0
  493. package/src/util/update-schema.ts +13 -0
  494. package/src/util/which.ts +14 -0
  495. package/src/util/wildcard.ts +59 -0
  496. package/src/worktree/index.ts +612 -0
  497. package/sst-env.d.ts +10 -0
  498. package/test/AGENTS.md +81 -0
  499. package/test/account/repo.test.ts +326 -0
  500. package/test/account/service.test.ts +393 -0
  501. package/test/acp/agent-interface.test.ts +51 -0
  502. package/test/acp/event-subscription.test.ts +685 -0
  503. package/test/agent/agent.test.ts +717 -0
  504. package/test/auth/auth.test.ts +58 -0
  505. package/test/bus/bus-effect.test.ts +164 -0
  506. package/test/bus/bus-integration.test.ts +87 -0
  507. package/test/bus/bus.test.ts +219 -0
  508. package/test/cli/account.test.ts +26 -0
  509. package/test/cli/cmd/tui/prompt-part.test.ts +47 -0
  510. package/test/cli/github-action.test.ts +198 -0
  511. package/test/cli/github-remote.test.ts +80 -0
  512. package/test/cli/import.test.ts +54 -0
  513. package/test/cli/plugin-auth-picker.test.ts +120 -0
  514. package/test/cli/tui/keybind-plugin.test.ts +90 -0
  515. package/test/cli/tui/plugin-add.test.ts +107 -0
  516. package/test/cli/tui/plugin-install.test.ts +89 -0
  517. package/test/cli/tui/plugin-lifecycle.test.ts +225 -0
  518. package/test/cli/tui/plugin-loader-entrypoint.test.ts +492 -0
  519. package/test/cli/tui/plugin-loader-pure.test.ts +72 -0
  520. package/test/cli/tui/plugin-loader.test.ts +752 -0
  521. package/test/cli/tui/plugin-toggle.test.ts +159 -0
  522. package/test/cli/tui/slot-replace.test.tsx +47 -0
  523. package/test/cli/tui/theme-store.test.ts +51 -0
  524. package/test/cli/tui/thread.test.ts +128 -0
  525. package/test/cli/tui/transcript.test.ts +426 -0
  526. package/test/config/agent-color.test.ts +71 -0
  527. package/test/config/config.test.ts +2348 -0
  528. package/test/config/fixtures/empty-frontmatter.md +4 -0
  529. package/test/config/fixtures/frontmatter.md +28 -0
  530. package/test/config/fixtures/markdown-header.md +11 -0
  531. package/test/config/fixtures/no-frontmatter.md +1 -0
  532. package/test/config/fixtures/weird-model-id.md +13 -0
  533. package/test/config/markdown.test.ts +228 -0
  534. package/test/config/tui.test.ts +752 -0
  535. package/test/control-plane/sse.test.ts +56 -0
  536. package/test/effect/cross-spawn-spawner.test.ts +412 -0
  537. package/test/effect/instance-state.test.ts +482 -0
  538. package/test/effect/run-service.test.ts +46 -0
  539. package/test/effect/runner.test.ts +523 -0
  540. package/test/fake/provider.ts +81 -0
  541. package/test/file/fsmonitor.test.ts +62 -0
  542. package/test/file/ignore.test.ts +10 -0
  543. package/test/file/index.test.ts +946 -0
  544. package/test/file/path-traversal.test.ts +198 -0
  545. package/test/file/ripgrep.test.ts +54 -0
  546. package/test/file/time.test.ts +445 -0
  547. package/test/file/watcher.test.ts +247 -0
  548. package/test/filesystem/filesystem.test.ts +319 -0
  549. package/test/fixture/db.ts +11 -0
  550. package/test/fixture/fixture.test.ts +26 -0
  551. package/test/fixture/fixture.ts +172 -0
  552. package/test/fixture/flock-worker.ts +72 -0
  553. package/test/fixture/lsp/fake-lsp-server.js +77 -0
  554. package/test/fixture/plug-worker.ts +93 -0
  555. package/test/fixture/plugin-meta-worker.ts +26 -0
  556. package/test/fixture/skills/agents-sdk/SKILL.md +152 -0
  557. package/test/fixture/skills/agents-sdk/references/callable.md +92 -0
  558. package/test/fixture/skills/cloudflare/SKILL.md +211 -0
  559. package/test/fixture/skills/index.json +6 -0
  560. package/test/fixture/tui-plugin.ts +337 -0
  561. package/test/fixture/tui-runtime.ts +27 -0
  562. package/test/format/format.test.ts +171 -0
  563. package/test/git/git.test.ts +128 -0
  564. package/test/ide/ide.test.ts +82 -0
  565. package/test/installation/installation.test.ts +151 -0
  566. package/test/keybind.test.ts +421 -0
  567. package/test/lib/effect.ts +53 -0
  568. package/test/lib/filesystem.ts +10 -0
  569. package/test/lib/llm-server.ts +795 -0
  570. package/test/lsp/client.test.ts +95 -0
  571. package/test/lsp/index.test.ts +55 -0
  572. package/test/lsp/launch.test.ts +22 -0
  573. package/test/lsp/lifecycle.test.ts +147 -0
  574. package/test/mcp/headers.test.ts +153 -0
  575. package/test/mcp/lifecycle.test.ts +750 -0
  576. package/test/mcp/oauth-auto-connect.test.ts +199 -0
  577. package/test/mcp/oauth-browser.test.ts +249 -0
  578. package/test/memory/abort-leak.test.ts +137 -0
  579. package/test/patch/patch.test.ts +348 -0
  580. package/test/permission/arity.test.ts +33 -0
  581. package/test/permission/next.test.ts +1148 -0
  582. package/test/permission-task.test.ts +323 -0
  583. package/test/plugin/auth-override.test.ts +74 -0
  584. package/test/plugin/codex.test.ts +123 -0
  585. package/test/plugin/github-copilot-models.test.ts +117 -0
  586. package/test/plugin/install-concurrency.test.ts +140 -0
  587. package/test/plugin/install.test.ts +570 -0
  588. package/test/plugin/loader-shared.test.ts +1136 -0
  589. package/test/plugin/meta.test.ts +137 -0
  590. package/test/plugin/trigger.test.ts +111 -0
  591. package/test/preload.ts +90 -0
  592. package/test/project/migrate-global.test.ts +140 -0
  593. package/test/project/project.test.ts +459 -0
  594. package/test/project/state.test.ts +115 -0
  595. package/test/project/vcs.test.ts +228 -0
  596. package/test/project/worktree-remove.test.ts +96 -0
  597. package/test/project/worktree.test.ts +173 -0
  598. package/test/provider/amazon-bedrock.test.ts +447 -0
  599. package/test/provider/copilot/convert-to-copilot-messages.test.ts +523 -0
  600. package/test/provider/copilot/copilot-chat-model.test.ts +592 -0
  601. package/test/provider/gitlab-duo.test.ts +412 -0
  602. package/test/provider/provider.test.ts +2284 -0
  603. package/test/provider/transform.test.ts +2839 -0
  604. package/test/pty/pty-output-isolation.test.ts +141 -0
  605. package/test/pty/pty-session.test.ts +92 -0
  606. package/test/pty/pty-shell.test.ts +59 -0
  607. package/test/question/question.test.ts +453 -0
  608. package/test/server/global-session-list.test.ts +89 -0
  609. package/test/server/project-init-git.test.ts +121 -0
  610. package/test/server/session-actions.test.ts +83 -0
  611. package/test/server/session-list.test.ts +98 -0
  612. package/test/server/session-messages.test.ts +159 -0
  613. package/test/server/session-select.test.ts +84 -0
  614. package/test/session/compaction.test.ts +1212 -0
  615. package/test/session/instruction.test.ts +286 -0
  616. package/test/session/llm.test.ts +1098 -0
  617. package/test/session/message-v2.test.ts +957 -0
  618. package/test/session/messages-pagination.test.ts +885 -0
  619. package/test/session/processor-effect.test.ts +747 -0
  620. package/test/session/prompt-effect.test.ts +1241 -0
  621. package/test/session/prompt.test.ts +518 -0
  622. package/test/session/retry.test.ts +232 -0
  623. package/test/session/revert-compact.test.ts +621 -0
  624. package/test/session/session.test.ts +142 -0
  625. package/test/session/snapshot-tool-race.test.ts +242 -0
  626. package/test/session/structured-output-integration.test.ts +233 -0
  627. package/test/session/structured-output.test.ts +391 -0
  628. package/test/session/system.test.ts +59 -0
  629. package/test/share/share-next.test.ts +333 -0
  630. package/test/shell/shell.test.ts +73 -0
  631. package/test/skill/discovery.test.ts +116 -0
  632. package/test/skill/skill.test.ts +392 -0
  633. package/test/snapshot/snapshot.test.ts +1312 -0
  634. package/test/storage/db.test.ts +14 -0
  635. package/test/storage/json-migration.test.ts +849 -0
  636. package/test/storage/storage.test.ts +295 -0
  637. package/test/sync/index.test.ts +191 -0
  638. package/test/tool/__snapshots__/tool.test.ts.snap +9 -0
  639. package/test/tool/apply_patch.test.ts +567 -0
  640. package/test/tool/bash.test.ts +1099 -0
  641. package/test/tool/edit.test.ts +681 -0
  642. package/test/tool/external-directory.test.ts +198 -0
  643. package/test/tool/fixtures/large-image.png +0 -0
  644. package/test/tool/fixtures/models-api.json +65179 -0
  645. package/test/tool/grep.test.ts +111 -0
  646. package/test/tool/question.test.ts +126 -0
  647. package/test/tool/read.test.ts +546 -0
  648. package/test/tool/registry.test.ts +126 -0
  649. package/test/tool/skill.test.ts +167 -0
  650. package/test/tool/task.test.ts +49 -0
  651. package/test/tool/tool-define.test.ts +101 -0
  652. package/test/tool/truncation.test.ts +161 -0
  653. package/test/tool/webfetch.test.ts +101 -0
  654. package/test/tool/write.test.ts +353 -0
  655. package/test/util/data-url.test.ts +14 -0
  656. package/test/util/effect-zod.test.ts +61 -0
  657. package/test/util/error.test.ts +38 -0
  658. package/test/util/filesystem.test.ts +656 -0
  659. package/test/util/flock.test.ts +383 -0
  660. package/test/util/format.test.ts +59 -0
  661. package/test/util/glob.test.ts +164 -0
  662. package/test/util/iife.test.ts +36 -0
  663. package/test/util/lazy.test.ts +50 -0
  664. package/test/util/lock.test.ts +72 -0
  665. package/test/util/module.test.ts +59 -0
  666. package/test/util/process.test.ts +128 -0
  667. package/test/util/timeout.test.ts +21 -0
  668. package/test/util/which.test.ts +100 -0
  669. package/test/util/wildcard.test.ts +90 -0
  670. package/tsconfig.json +23 -0
@@ -0,0 +1,2348 @@
1
+ import { test, expect, describe, mock, afterEach, beforeEach, spyOn } from "bun:test"
2
+ import { Effect, Layer, Option } from "effect"
3
+ import { NodeFileSystem, NodePath } from "@effect/platform-node"
4
+ import { Config } from "../../src/config/config"
5
+ import { Instance } from "../../src/project/instance"
6
+ import { Auth } from "../../src/auth"
7
+ import { AccessToken, Account, AccountID, OrgID } from "../../src/account"
8
+ import { AppFileSystem } from "../../src/filesystem"
9
+ import { provideTmpdirInstance } from "../fixture/fixture"
10
+ import { tmpdir } from "../fixture/fixture"
11
+ import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner"
12
+
13
+ /** Infra layer that provides FileSystem, Path, ChildProcessSpawner for test fixtures */
14
+ const infra = CrossSpawnSpawner.defaultLayer.pipe(
15
+ Layer.provideMerge(Layer.mergeAll(NodeFileSystem.layer, NodePath.layer)),
16
+ )
17
+ import path from "path"
18
+ import fs from "fs/promises"
19
+ import { pathToFileURL } from "url"
20
+ import { Global } from "../../src/global"
21
+ import { ProjectID } from "../../src/project/schema"
22
+ import { Filesystem } from "../../src/util/filesystem"
23
+ import * as Network from "../../src/util/network"
24
+ import { Npm } from "../../src/npm"
25
+
26
+ const emptyAccount = Layer.mock(Account.Service)({
27
+ active: () => Effect.succeed(Option.none()),
28
+ })
29
+
30
+ const emptyAuth = Layer.mock(Auth.Service)({
31
+ all: () => Effect.succeed({}),
32
+ })
33
+
34
+ // Get managed config directory from environment (set in preload.ts)
35
+ const managedConfigDir = process.env.OPENCODE_TEST_MANAGED_CONFIG_DIR!
36
+
37
+ beforeEach(async () => {
38
+ await Config.invalidate(true)
39
+ })
40
+
41
+ afterEach(async () => {
42
+ await fs.rm(managedConfigDir, { force: true, recursive: true }).catch(() => {})
43
+ await Config.invalidate(true)
44
+ })
45
+
46
+ async function writeManagedSettings(settings: object, filename = "opencode.json") {
47
+ await fs.mkdir(managedConfigDir, { recursive: true })
48
+ await Filesystem.write(path.join(managedConfigDir, filename), JSON.stringify(settings))
49
+ }
50
+
51
+ async function writeConfig(dir: string, config: object, name = "opencode.json") {
52
+ await Filesystem.write(path.join(dir, name), JSON.stringify(config))
53
+ }
54
+
55
+ async function check(map: (dir: string) => string) {
56
+ if (process.platform !== "win32") return
57
+ await using globalTmp = await tmpdir()
58
+ await using tmp = await tmpdir({ git: true, config: { snapshot: true } })
59
+ const prev = Global.Path.config
60
+ ;(Global.Path as { config: string }).config = globalTmp.path
61
+ await Config.invalidate()
62
+ try {
63
+ await writeConfig(globalTmp.path, {
64
+ $schema: "https://opencode.ai/config.json",
65
+ snapshot: false,
66
+ })
67
+ await Instance.provide({
68
+ directory: map(tmp.path),
69
+ fn: async () => {
70
+ const cfg = await Config.get()
71
+ expect(cfg.snapshot).toBe(true)
72
+ expect(Instance.directory).toBe(Filesystem.resolve(tmp.path))
73
+ expect(Instance.project.id).not.toBe(ProjectID.global)
74
+ },
75
+ })
76
+ } finally {
77
+ await Instance.disposeAll()
78
+ ;(Global.Path as { config: string }).config = prev
79
+ await Config.invalidate()
80
+ }
81
+ }
82
+
83
+ test("loads config with defaults when no files exist", async () => {
84
+ await using tmp = await tmpdir()
85
+ await Instance.provide({
86
+ directory: tmp.path,
87
+ fn: async () => {
88
+ const config = await Config.get()
89
+ expect(config.username).toBeDefined()
90
+ },
91
+ })
92
+ })
93
+
94
+ test("loads JSON config file", async () => {
95
+ await using tmp = await tmpdir({
96
+ init: async (dir) => {
97
+ await writeConfig(dir, {
98
+ $schema: "https://opencode.ai/config.json",
99
+ model: "test/model",
100
+ username: "testuser",
101
+ })
102
+ },
103
+ })
104
+ await Instance.provide({
105
+ directory: tmp.path,
106
+ fn: async () => {
107
+ const config = await Config.get()
108
+ expect(config.model).toBe("test/model")
109
+ expect(config.username).toBe("testuser")
110
+ },
111
+ })
112
+ })
113
+
114
+ test("loads project config from Git Bash and MSYS2 paths on Windows", async () => {
115
+ // Git Bash and MSYS2 both use /<drive>/... paths on Windows.
116
+ await check((dir) => {
117
+ const drive = dir[0].toLowerCase()
118
+ const rest = dir.slice(2).replaceAll("\\", "/")
119
+ return `/${drive}${rest}`
120
+ })
121
+ })
122
+
123
+ test("loads project config from Cygwin paths on Windows", async () => {
124
+ await check((dir) => {
125
+ const drive = dir[0].toLowerCase()
126
+ const rest = dir.slice(2).replaceAll("\\", "/")
127
+ return `/cygdrive/${drive}${rest}`
128
+ })
129
+ })
130
+
131
+ test("ignores legacy tui keys in opencode config", async () => {
132
+ await using tmp = await tmpdir({
133
+ init: async (dir) => {
134
+ await writeConfig(dir, {
135
+ $schema: "https://opencode.ai/config.json",
136
+ model: "test/model",
137
+ theme: "legacy",
138
+ tui: { scroll_speed: 4 },
139
+ })
140
+ },
141
+ })
142
+ await Instance.provide({
143
+ directory: tmp.path,
144
+ fn: async () => {
145
+ const config = await Config.get()
146
+ expect(config.model).toBe("test/model")
147
+ expect((config as Record<string, unknown>).theme).toBeUndefined()
148
+ expect((config as Record<string, unknown>).tui).toBeUndefined()
149
+ },
150
+ })
151
+ })
152
+
153
+ test("loads JSONC config file", async () => {
154
+ await using tmp = await tmpdir({
155
+ init: async (dir) => {
156
+ await Filesystem.write(
157
+ path.join(dir, "opencode.jsonc"),
158
+ `{
159
+ // This is a comment
160
+ "$schema": "https://opencode.ai/config.json",
161
+ "model": "test/model",
162
+ "username": "testuser"
163
+ }`,
164
+ )
165
+ },
166
+ })
167
+ await Instance.provide({
168
+ directory: tmp.path,
169
+ fn: async () => {
170
+ const config = await Config.get()
171
+ expect(config.model).toBe("test/model")
172
+ expect(config.username).toBe("testuser")
173
+ },
174
+ })
175
+ })
176
+
177
+ test("jsonc overrides json in the same directory", async () => {
178
+ await using tmp = await tmpdir({
179
+ init: async (dir) => {
180
+ await writeConfig(
181
+ dir,
182
+ {
183
+ $schema: "https://opencode.ai/config.json",
184
+ model: "base",
185
+ username: "base",
186
+ },
187
+ "opencode.jsonc",
188
+ )
189
+ await writeConfig(dir, {
190
+ $schema: "https://opencode.ai/config.json",
191
+ model: "override",
192
+ })
193
+ },
194
+ })
195
+ await Instance.provide({
196
+ directory: tmp.path,
197
+ fn: async () => {
198
+ const config = await Config.get()
199
+ expect(config.model).toBe("base")
200
+ expect(config.username).toBe("base")
201
+ },
202
+ })
203
+ })
204
+
205
+ test("handles environment variable substitution", async () => {
206
+ const originalEnv = process.env["TEST_VAR"]
207
+ process.env["TEST_VAR"] = "test-user"
208
+
209
+ try {
210
+ await using tmp = await tmpdir({
211
+ init: async (dir) => {
212
+ await writeConfig(dir, {
213
+ $schema: "https://opencode.ai/config.json",
214
+ username: "{env:TEST_VAR}",
215
+ })
216
+ },
217
+ })
218
+ await Instance.provide({
219
+ directory: tmp.path,
220
+ fn: async () => {
221
+ const config = await Config.get()
222
+ expect(config.username).toBe("test-user")
223
+ },
224
+ })
225
+ } finally {
226
+ if (originalEnv !== undefined) {
227
+ process.env["TEST_VAR"] = originalEnv
228
+ } else {
229
+ delete process.env["TEST_VAR"]
230
+ }
231
+ }
232
+ })
233
+
234
+ test("preserves env variables when adding $schema to config", async () => {
235
+ const originalEnv = process.env["PRESERVE_VAR"]
236
+ process.env["PRESERVE_VAR"] = "secret_value"
237
+
238
+ try {
239
+ await using tmp = await tmpdir({
240
+ init: async (dir) => {
241
+ // Config without $schema - should trigger auto-add
242
+ await Filesystem.write(
243
+ path.join(dir, "opencode.json"),
244
+ JSON.stringify({
245
+ username: "{env:PRESERVE_VAR}",
246
+ }),
247
+ )
248
+ },
249
+ })
250
+ await Instance.provide({
251
+ directory: tmp.path,
252
+ fn: async () => {
253
+ const config = await Config.get()
254
+ expect(config.username).toBe("secret_value")
255
+
256
+ // Read the file to verify the env variable was preserved
257
+ const content = await Filesystem.readText(path.join(tmp.path, "opencode.json"))
258
+ expect(content).toContain("{env:PRESERVE_VAR}")
259
+ expect(content).not.toContain("secret_value")
260
+ expect(content).toContain("$schema")
261
+ },
262
+ })
263
+ } finally {
264
+ if (originalEnv !== undefined) {
265
+ process.env["PRESERVE_VAR"] = originalEnv
266
+ } else {
267
+ delete process.env["PRESERVE_VAR"]
268
+ }
269
+ }
270
+ })
271
+
272
+ test("resolves env templates in account config with account token", async () => {
273
+ const originalControlToken = process.env["OPENCODE_CONSOLE_TOKEN"]
274
+
275
+ const fakeAccount = Layer.mock(Account.Service)({
276
+ active: () =>
277
+ Effect.succeed(
278
+ Option.some({
279
+ id: AccountID.make("account-1"),
280
+ email: "user@example.com",
281
+ url: "https://control.example.com",
282
+ active_org_id: OrgID.make("org-1"),
283
+ }),
284
+ ),
285
+ config: () =>
286
+ Effect.succeed(
287
+ Option.some({
288
+ provider: { opencode: { options: { apiKey: "{env:OPENCODE_CONSOLE_TOKEN}" } } },
289
+ }),
290
+ ),
291
+ token: () => Effect.succeed(Option.some(AccessToken.make("st_test_token"))),
292
+ })
293
+
294
+ const layer = Config.layer.pipe(
295
+ Layer.provide(AppFileSystem.defaultLayer),
296
+ Layer.provide(emptyAuth),
297
+ Layer.provide(fakeAccount),
298
+ Layer.provideMerge(infra),
299
+ )
300
+
301
+ try {
302
+ await provideTmpdirInstance(() =>
303
+ Config.Service.use((svc) =>
304
+ Effect.gen(function* () {
305
+ const config = yield* svc.get()
306
+ expect(config.provider?.["opencode"]?.options?.apiKey).toBe("st_test_token")
307
+ }),
308
+ ),
309
+ ).pipe(Effect.scoped, Effect.provide(layer), Effect.runPromise)
310
+ } finally {
311
+ if (originalControlToken !== undefined) {
312
+ process.env["OPENCODE_CONSOLE_TOKEN"] = originalControlToken
313
+ } else {
314
+ delete process.env["OPENCODE_CONSOLE_TOKEN"]
315
+ }
316
+ }
317
+ })
318
+
319
+ test("handles file inclusion substitution", async () => {
320
+ await using tmp = await tmpdir({
321
+ init: async (dir) => {
322
+ await Filesystem.write(path.join(dir, "included.txt"), "test-user")
323
+ await writeConfig(dir, {
324
+ $schema: "https://opencode.ai/config.json",
325
+ username: "{file:included.txt}",
326
+ })
327
+ },
328
+ })
329
+ await Instance.provide({
330
+ directory: tmp.path,
331
+ fn: async () => {
332
+ const config = await Config.get()
333
+ expect(config.username).toBe("test-user")
334
+ },
335
+ })
336
+ })
337
+
338
+ test("handles file inclusion with replacement tokens", async () => {
339
+ await using tmp = await tmpdir({
340
+ init: async (dir) => {
341
+ await Filesystem.write(path.join(dir, "included.md"), "const out = await Bun.$`echo hi`")
342
+ await writeConfig(dir, {
343
+ $schema: "https://opencode.ai/config.json",
344
+ username: "{file:included.md}",
345
+ })
346
+ },
347
+ })
348
+ await Instance.provide({
349
+ directory: tmp.path,
350
+ fn: async () => {
351
+ const config = await Config.get()
352
+ expect(config.username).toBe("const out = await Bun.$`echo hi`")
353
+ },
354
+ })
355
+ })
356
+
357
+ test("validates config schema and throws on invalid fields", async () => {
358
+ await using tmp = await tmpdir({
359
+ init: async (dir) => {
360
+ await writeConfig(dir, {
361
+ $schema: "https://opencode.ai/config.json",
362
+ invalid_field: "should cause error",
363
+ })
364
+ },
365
+ })
366
+ await Instance.provide({
367
+ directory: tmp.path,
368
+ fn: async () => {
369
+ // Strict schema should throw an error for invalid fields
370
+ await expect(Config.get()).rejects.toThrow()
371
+ },
372
+ })
373
+ })
374
+
375
+ test("throws error for invalid JSON", async () => {
376
+ await using tmp = await tmpdir({
377
+ init: async (dir) => {
378
+ await Filesystem.write(path.join(dir, "opencode.json"), "{ invalid json }")
379
+ },
380
+ })
381
+ await Instance.provide({
382
+ directory: tmp.path,
383
+ fn: async () => {
384
+ await expect(Config.get()).rejects.toThrow()
385
+ },
386
+ })
387
+ })
388
+
389
+ test("handles agent configuration", async () => {
390
+ await using tmp = await tmpdir({
391
+ init: async (dir) => {
392
+ await writeConfig(dir, {
393
+ $schema: "https://opencode.ai/config.json",
394
+ agent: {
395
+ test_agent: {
396
+ model: "test/model",
397
+ temperature: 0.7,
398
+ description: "test agent",
399
+ },
400
+ },
401
+ })
402
+ },
403
+ })
404
+ await Instance.provide({
405
+ directory: tmp.path,
406
+ fn: async () => {
407
+ const config = await Config.get()
408
+ expect(config.agent?.["test_agent"]).toEqual(
409
+ expect.objectContaining({
410
+ model: "test/model",
411
+ temperature: 0.7,
412
+ description: "test agent",
413
+ }),
414
+ )
415
+ },
416
+ })
417
+ })
418
+
419
+ test("treats agent variant as model-scoped setting (not provider option)", async () => {
420
+ await using tmp = await tmpdir({
421
+ init: async (dir) => {
422
+ await writeConfig(dir, {
423
+ $schema: "https://opencode.ai/config.json",
424
+ agent: {
425
+ test_agent: {
426
+ model: "openai/gpt-5.2",
427
+ variant: "xhigh",
428
+ max_tokens: 123,
429
+ },
430
+ },
431
+ })
432
+ },
433
+ })
434
+
435
+ await Instance.provide({
436
+ directory: tmp.path,
437
+ fn: async () => {
438
+ const config = await Config.get()
439
+ const agent = config.agent?.["test_agent"]
440
+
441
+ expect(agent?.variant).toBe("xhigh")
442
+ expect(agent?.options).toMatchObject({
443
+ max_tokens: 123,
444
+ })
445
+ expect(agent?.options).not.toHaveProperty("variant")
446
+ },
447
+ })
448
+ })
449
+
450
+ test("handles command configuration", async () => {
451
+ await using tmp = await tmpdir({
452
+ init: async (dir) => {
453
+ await writeConfig(dir, {
454
+ $schema: "https://opencode.ai/config.json",
455
+ command: {
456
+ test_command: {
457
+ template: "test template",
458
+ description: "test command",
459
+ agent: "test_agent",
460
+ },
461
+ },
462
+ })
463
+ },
464
+ })
465
+ await Instance.provide({
466
+ directory: tmp.path,
467
+ fn: async () => {
468
+ const config = await Config.get()
469
+ expect(config.command?.["test_command"]).toEqual({
470
+ template: "test template",
471
+ description: "test command",
472
+ agent: "test_agent",
473
+ })
474
+ },
475
+ })
476
+ })
477
+
478
+ test("migrates autoshare to share field", async () => {
479
+ await using tmp = await tmpdir({
480
+ init: async (dir) => {
481
+ await Filesystem.write(
482
+ path.join(dir, "opencode.json"),
483
+ JSON.stringify({
484
+ $schema: "https://opencode.ai/config.json",
485
+ autoshare: true,
486
+ }),
487
+ )
488
+ },
489
+ })
490
+ await Instance.provide({
491
+ directory: tmp.path,
492
+ fn: async () => {
493
+ const config = await Config.get()
494
+ expect(config.share).toBe("auto")
495
+ expect(config.autoshare).toBe(true)
496
+ },
497
+ })
498
+ })
499
+
500
+ test("migrates mode field to agent field", async () => {
501
+ await using tmp = await tmpdir({
502
+ init: async (dir) => {
503
+ await Filesystem.write(
504
+ path.join(dir, "opencode.json"),
505
+ JSON.stringify({
506
+ $schema: "https://opencode.ai/config.json",
507
+ mode: {
508
+ test_mode: {
509
+ model: "test/model",
510
+ temperature: 0.5,
511
+ },
512
+ },
513
+ }),
514
+ )
515
+ },
516
+ })
517
+ await Instance.provide({
518
+ directory: tmp.path,
519
+ fn: async () => {
520
+ const config = await Config.get()
521
+ expect(config.agent?.["test_mode"]).toEqual({
522
+ model: "test/model",
523
+ temperature: 0.5,
524
+ mode: "primary",
525
+ options: {},
526
+ permission: {},
527
+ })
528
+ },
529
+ })
530
+ })
531
+
532
+ test("loads config from .opencode directory", async () => {
533
+ await using tmp = await tmpdir({
534
+ init: async (dir) => {
535
+ const opencodeDir = path.join(dir, ".opencode")
536
+ await fs.mkdir(opencodeDir, { recursive: true })
537
+ const agentDir = path.join(opencodeDir, "agent")
538
+ await fs.mkdir(agentDir, { recursive: true })
539
+
540
+ await Filesystem.write(
541
+ path.join(agentDir, "test.md"),
542
+ `---
543
+ model: test/model
544
+ ---
545
+ Test agent prompt`,
546
+ )
547
+ },
548
+ })
549
+ await Instance.provide({
550
+ directory: tmp.path,
551
+ fn: async () => {
552
+ const config = await Config.get()
553
+ expect(config.agent?.["test"]).toEqual(
554
+ expect.objectContaining({
555
+ name: "test",
556
+ model: "test/model",
557
+ prompt: "Test agent prompt",
558
+ }),
559
+ )
560
+ },
561
+ })
562
+ })
563
+
564
+ test("loads agents from .opencode/agents (plural)", async () => {
565
+ await using tmp = await tmpdir({
566
+ init: async (dir) => {
567
+ const opencodeDir = path.join(dir, ".opencode")
568
+ await fs.mkdir(opencodeDir, { recursive: true })
569
+
570
+ const agentsDir = path.join(opencodeDir, "agents")
571
+ await fs.mkdir(path.join(agentsDir, "nested"), { recursive: true })
572
+
573
+ await Filesystem.write(
574
+ path.join(agentsDir, "helper.md"),
575
+ `---
576
+ model: test/model
577
+ mode: subagent
578
+ ---
579
+ Helper agent prompt`,
580
+ )
581
+
582
+ await Filesystem.write(
583
+ path.join(agentsDir, "nested", "child.md"),
584
+ `---
585
+ model: test/model
586
+ mode: subagent
587
+ ---
588
+ Nested agent prompt`,
589
+ )
590
+ },
591
+ })
592
+
593
+ await Instance.provide({
594
+ directory: tmp.path,
595
+ fn: async () => {
596
+ const config = await Config.get()
597
+
598
+ expect(config.agent?.["helper"]).toMatchObject({
599
+ name: "helper",
600
+ model: "test/model",
601
+ mode: "subagent",
602
+ prompt: "Helper agent prompt",
603
+ })
604
+
605
+ expect(config.agent?.["nested/child"]).toMatchObject({
606
+ name: "nested/child",
607
+ model: "test/model",
608
+ mode: "subagent",
609
+ prompt: "Nested agent prompt",
610
+ })
611
+ },
612
+ })
613
+ })
614
+
615
+ test("loads commands from .opencode/command (singular)", async () => {
616
+ await using tmp = await tmpdir({
617
+ init: async (dir) => {
618
+ const opencodeDir = path.join(dir, ".opencode")
619
+ await fs.mkdir(opencodeDir, { recursive: true })
620
+
621
+ const commandDir = path.join(opencodeDir, "command")
622
+ await fs.mkdir(path.join(commandDir, "nested"), { recursive: true })
623
+
624
+ await Filesystem.write(
625
+ path.join(commandDir, "hello.md"),
626
+ `---
627
+ description: Test command
628
+ ---
629
+ Hello from singular command`,
630
+ )
631
+
632
+ await Filesystem.write(
633
+ path.join(commandDir, "nested", "child.md"),
634
+ `---
635
+ description: Nested command
636
+ ---
637
+ Nested command template`,
638
+ )
639
+ },
640
+ })
641
+
642
+ await Instance.provide({
643
+ directory: tmp.path,
644
+ fn: async () => {
645
+ const config = await Config.get()
646
+
647
+ expect(config.command?.["hello"]).toEqual({
648
+ description: "Test command",
649
+ template: "Hello from singular command",
650
+ })
651
+
652
+ expect(config.command?.["nested/child"]).toEqual({
653
+ description: "Nested command",
654
+ template: "Nested command template",
655
+ })
656
+ },
657
+ })
658
+ })
659
+
660
+ test("loads commands from .opencode/commands (plural)", async () => {
661
+ await using tmp = await tmpdir({
662
+ init: async (dir) => {
663
+ const opencodeDir = path.join(dir, ".opencode")
664
+ await fs.mkdir(opencodeDir, { recursive: true })
665
+
666
+ const commandsDir = path.join(opencodeDir, "commands")
667
+ await fs.mkdir(path.join(commandsDir, "nested"), { recursive: true })
668
+
669
+ await Filesystem.write(
670
+ path.join(commandsDir, "hello.md"),
671
+ `---
672
+ description: Test command
673
+ ---
674
+ Hello from plural commands`,
675
+ )
676
+
677
+ await Filesystem.write(
678
+ path.join(commandsDir, "nested", "child.md"),
679
+ `---
680
+ description: Nested command
681
+ ---
682
+ Nested command template`,
683
+ )
684
+ },
685
+ })
686
+
687
+ await Instance.provide({
688
+ directory: tmp.path,
689
+ fn: async () => {
690
+ const config = await Config.get()
691
+
692
+ expect(config.command?.["hello"]).toEqual({
693
+ description: "Test command",
694
+ template: "Hello from plural commands",
695
+ })
696
+
697
+ expect(config.command?.["nested/child"]).toEqual({
698
+ description: "Nested command",
699
+ template: "Nested command template",
700
+ })
701
+ },
702
+ })
703
+ })
704
+
705
+ test("updates config and writes to file", async () => {
706
+ await using tmp = await tmpdir()
707
+ await Instance.provide({
708
+ directory: tmp.path,
709
+ fn: async () => {
710
+ const newConfig = { model: "updated/model" }
711
+ await Config.update(newConfig as any)
712
+
713
+ const writtenConfig = await Filesystem.readJson(path.join(tmp.path, "config.json"))
714
+ expect(writtenConfig.model).toBe("updated/model")
715
+ },
716
+ })
717
+ })
718
+
719
+ test("gets config directories", async () => {
720
+ await using tmp = await tmpdir()
721
+ await Instance.provide({
722
+ directory: tmp.path,
723
+ fn: async () => {
724
+ const dirs = await Config.directories()
725
+ expect(dirs.length).toBeGreaterThanOrEqual(1)
726
+ },
727
+ })
728
+ })
729
+
730
+ test("does not try to install dependencies in read-only OPENCODE_CONFIG_DIR", async () => {
731
+ if (process.platform === "win32") return
732
+
733
+ await using tmp = await tmpdir<string>({
734
+ init: async (dir) => {
735
+ const ro = path.join(dir, "readonly")
736
+ await fs.mkdir(ro, { recursive: true })
737
+ await fs.chmod(ro, 0o555)
738
+ return ro
739
+ },
740
+ dispose: async (dir) => {
741
+ const ro = path.join(dir, "readonly")
742
+ await fs.chmod(ro, 0o755).catch(() => {})
743
+ return ro
744
+ },
745
+ })
746
+
747
+ const prev = process.env.OPENCODE_CONFIG_DIR
748
+ process.env.OPENCODE_CONFIG_DIR = tmp.extra
749
+
750
+ try {
751
+ await Instance.provide({
752
+ directory: tmp.path,
753
+ fn: async () => {
754
+ await Config.get()
755
+ },
756
+ })
757
+ } finally {
758
+ if (prev === undefined) delete process.env.OPENCODE_CONFIG_DIR
759
+ else process.env.OPENCODE_CONFIG_DIR = prev
760
+ }
761
+ })
762
+
763
+ test("installs dependencies in writable OPENCODE_CONFIG_DIR", async () => {
764
+ await using tmp = await tmpdir<string>({
765
+ init: async (dir) => {
766
+ const cfg = path.join(dir, "configdir")
767
+ await fs.mkdir(cfg, { recursive: true })
768
+ return cfg
769
+ },
770
+ })
771
+
772
+ const prev = process.env.OPENCODE_CONFIG_DIR
773
+ process.env.OPENCODE_CONFIG_DIR = tmp.extra
774
+ const online = spyOn(Network, "online").mockReturnValue(false)
775
+ const install = spyOn(Npm, "install").mockImplementation(async (dir: string) => {
776
+ const mod = path.join(dir, "node_modules", "@opencode-ai", "plugin")
777
+ await fs.mkdir(mod, { recursive: true })
778
+ await Filesystem.write(
779
+ path.join(mod, "package.json"),
780
+ JSON.stringify({ name: "@opencode-ai/plugin", version: "1.0.0" }),
781
+ )
782
+ })
783
+
784
+ try {
785
+ await Instance.provide({
786
+ directory: tmp.path,
787
+ fn: async () => {
788
+ await Config.get()
789
+ await Config.waitForDependencies()
790
+ },
791
+ })
792
+
793
+ expect(await Filesystem.exists(path.join(tmp.extra, "package.json"))).toBe(true)
794
+ expect(await Filesystem.exists(path.join(tmp.extra, ".gitignore"))).toBe(true)
795
+ expect(await Filesystem.readText(path.join(tmp.extra, ".gitignore"))).toContain("package-lock.json")
796
+ } finally {
797
+ online.mockRestore()
798
+ install.mockRestore()
799
+ if (prev === undefined) delete process.env.OPENCODE_CONFIG_DIR
800
+ else process.env.OPENCODE_CONFIG_DIR = prev
801
+ }
802
+ })
803
+
804
+ test("dedupes concurrent config dependency installs for the same dir", async () => {
805
+ await using tmp = await tmpdir()
806
+ const dir = path.join(tmp.path, "a")
807
+ await fs.mkdir(dir, { recursive: true })
808
+
809
+ const ticks: number[] = []
810
+ let calls = 0
811
+ let start = () => {}
812
+ let done = () => {}
813
+ let blocked = () => {}
814
+ const ready = new Promise<void>((resolve) => {
815
+ start = resolve
816
+ })
817
+ const gate = new Promise<void>((resolve) => {
818
+ done = resolve
819
+ })
820
+ const waiting = new Promise<void>((resolve) => {
821
+ blocked = resolve
822
+ })
823
+ const online = spyOn(Network, "online").mockReturnValue(false)
824
+ const targetDir = dir
825
+ const run = spyOn(Npm, "install").mockImplementation(async (d: string) => {
826
+ const hit = path.normalize(d) === path.normalize(targetDir)
827
+ if (hit) {
828
+ calls += 1
829
+ start()
830
+ await gate
831
+ }
832
+ const mod = path.join(d, "node_modules", "@opencode-ai", "plugin")
833
+ await fs.mkdir(mod, { recursive: true })
834
+ await Filesystem.write(
835
+ path.join(mod, "package.json"),
836
+ JSON.stringify({ name: "@opencode-ai/plugin", version: "1.0.0" }),
837
+ )
838
+ if (hit) {
839
+ start()
840
+ await gate
841
+ }
842
+ })
843
+
844
+ try {
845
+ const first = Config.installDependencies(dir)
846
+ await ready
847
+ const second = Config.installDependencies(dir, {
848
+ waitTick: (tick) => {
849
+ ticks.push(tick.attempt)
850
+ blocked()
851
+ blocked = () => {}
852
+ },
853
+ })
854
+ await waiting
855
+ done()
856
+ await Promise.all([first, second])
857
+ } finally {
858
+ online.mockRestore()
859
+ run.mockRestore()
860
+ }
861
+
862
+ expect(calls).toBe(2)
863
+ expect(ticks.length).toBeGreaterThan(0)
864
+ expect(await Filesystem.exists(path.join(dir, "package.json"))).toBe(true)
865
+ })
866
+
867
+ test("serializes config dependency installs across dirs", async () => {
868
+ if (process.platform !== "win32") return
869
+
870
+ await using tmp = await tmpdir()
871
+ const a = path.join(tmp.path, "a")
872
+ const b = path.join(tmp.path, "b")
873
+ await fs.mkdir(a, { recursive: true })
874
+ await fs.mkdir(b, { recursive: true })
875
+
876
+ let calls = 0
877
+ let open = 0
878
+ let peak = 0
879
+ let start = () => {}
880
+ let done = () => {}
881
+ const ready = new Promise<void>((resolve) => {
882
+ start = resolve
883
+ })
884
+ const gate = new Promise<void>((resolve) => {
885
+ done = resolve
886
+ })
887
+
888
+ const online = spyOn(Network, "online").mockReturnValue(false)
889
+ const run = spyOn(Npm, "install").mockImplementation(async (dir: string) => {
890
+ const cwd = path.normalize(dir)
891
+ const hit = cwd === path.normalize(a) || cwd === path.normalize(b)
892
+ if (hit) {
893
+ calls += 1
894
+ open += 1
895
+ peak = Math.max(peak, open)
896
+ if (calls === 1) {
897
+ start()
898
+ await gate
899
+ }
900
+ }
901
+ const mod = path.join(cwd, "node_modules", "@opencode-ai", "plugin")
902
+ await fs.mkdir(mod, { recursive: true })
903
+ await Filesystem.write(
904
+ path.join(mod, "package.json"),
905
+ JSON.stringify({ name: "@opencode-ai/plugin", version: "1.0.0" }),
906
+ )
907
+ if (hit) {
908
+ open -= 1
909
+ }
910
+ })
911
+
912
+ try {
913
+ const first = Config.installDependencies(a)
914
+ await ready
915
+ const second = Config.installDependencies(b)
916
+ done()
917
+ await Promise.all([first, second])
918
+ } finally {
919
+ online.mockRestore()
920
+ run.mockRestore()
921
+ }
922
+
923
+ expect(calls).toBe(2)
924
+ expect(peak).toBe(1)
925
+ })
926
+
927
+ test("resolves scoped npm plugins in config", async () => {
928
+ await using tmp = await tmpdir({
929
+ init: async (dir) => {
930
+ const pluginDir = path.join(dir, "node_modules", "@scope", "plugin")
931
+ await fs.mkdir(pluginDir, { recursive: true })
932
+
933
+ await Filesystem.write(
934
+ path.join(dir, "package.json"),
935
+ JSON.stringify({ name: "config-fixture", version: "1.0.0", type: "module" }, null, 2),
936
+ )
937
+
938
+ await Filesystem.write(
939
+ path.join(pluginDir, "package.json"),
940
+ JSON.stringify(
941
+ {
942
+ name: "@scope/plugin",
943
+ version: "1.0.0",
944
+ type: "module",
945
+ main: "./index.js",
946
+ },
947
+ null,
948
+ 2,
949
+ ),
950
+ )
951
+
952
+ await Filesystem.write(path.join(pluginDir, "index.js"), "export default {}\n")
953
+
954
+ await Filesystem.write(
955
+ path.join(dir, "opencode.json"),
956
+ JSON.stringify({ $schema: "https://opencode.ai/config.json", plugin: ["@scope/plugin"] }, null, 2),
957
+ )
958
+ },
959
+ })
960
+
961
+ await Instance.provide({
962
+ directory: tmp.path,
963
+ fn: async () => {
964
+ const config = await Config.get()
965
+ const pluginEntries = config.plugin ?? []
966
+ expect(pluginEntries).toContain("@scope/plugin")
967
+ },
968
+ })
969
+ })
970
+
971
+ test("merges plugin arrays from global and local configs", async () => {
972
+ await using tmp = await tmpdir({
973
+ init: async (dir) => {
974
+ // Create a nested project structure with local .opencode config
975
+ const projectDir = path.join(dir, "project")
976
+ const opencodeDir = path.join(projectDir, ".opencode")
977
+ await fs.mkdir(opencodeDir, { recursive: true })
978
+
979
+ // Global config with plugins
980
+ await Filesystem.write(
981
+ path.join(dir, "opencode.json"),
982
+ JSON.stringify({
983
+ $schema: "https://opencode.ai/config.json",
984
+ plugin: ["global-plugin-1", "global-plugin-2"],
985
+ }),
986
+ )
987
+
988
+ // Local .opencode config with different plugins
989
+ await Filesystem.write(
990
+ path.join(opencodeDir, "opencode.json"),
991
+ JSON.stringify({
992
+ $schema: "https://opencode.ai/config.json",
993
+ plugin: ["local-plugin-1"],
994
+ }),
995
+ )
996
+ },
997
+ })
998
+
999
+ await Instance.provide({
1000
+ directory: path.join(tmp.path, "project"),
1001
+ fn: async () => {
1002
+ const config = await Config.get()
1003
+ const plugins = config.plugin ?? []
1004
+
1005
+ // Should contain both global and local plugins
1006
+ expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
1007
+ expect(plugins.some((p) => p.includes("global-plugin-2"))).toBe(true)
1008
+ expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
1009
+
1010
+ // Should have all 3 plugins (not replaced, but merged)
1011
+ const pluginNames = plugins.filter((p) => p.includes("global-plugin") || p.includes("local-plugin"))
1012
+ expect(pluginNames.length).toBeGreaterThanOrEqual(3)
1013
+ },
1014
+ })
1015
+ })
1016
+
1017
+ test("does not error when only custom agent is a subagent", async () => {
1018
+ await using tmp = await tmpdir({
1019
+ init: async (dir) => {
1020
+ const opencodeDir = path.join(dir, ".opencode")
1021
+ await fs.mkdir(opencodeDir, { recursive: true })
1022
+ const agentDir = path.join(opencodeDir, "agent")
1023
+ await fs.mkdir(agentDir, { recursive: true })
1024
+
1025
+ await Filesystem.write(
1026
+ path.join(agentDir, "helper.md"),
1027
+ `---
1028
+ model: test/model
1029
+ mode: subagent
1030
+ ---
1031
+ Helper subagent prompt`,
1032
+ )
1033
+ },
1034
+ })
1035
+ await Instance.provide({
1036
+ directory: tmp.path,
1037
+ fn: async () => {
1038
+ const config = await Config.get()
1039
+ expect(config.agent?.["helper"]).toMatchObject({
1040
+ name: "helper",
1041
+ model: "test/model",
1042
+ mode: "subagent",
1043
+ prompt: "Helper subagent prompt",
1044
+ })
1045
+ },
1046
+ })
1047
+ })
1048
+
1049
+ test("merges instructions arrays from global and local configs", async () => {
1050
+ await using tmp = await tmpdir({
1051
+ init: async (dir) => {
1052
+ const projectDir = path.join(dir, "project")
1053
+ const opencodeDir = path.join(projectDir, ".opencode")
1054
+ await fs.mkdir(opencodeDir, { recursive: true })
1055
+
1056
+ await Filesystem.write(
1057
+ path.join(dir, "opencode.json"),
1058
+ JSON.stringify({
1059
+ $schema: "https://opencode.ai/config.json",
1060
+ instructions: ["global-instructions.md", "shared-rules.md"],
1061
+ }),
1062
+ )
1063
+
1064
+ await Filesystem.write(
1065
+ path.join(opencodeDir, "opencode.json"),
1066
+ JSON.stringify({
1067
+ $schema: "https://opencode.ai/config.json",
1068
+ instructions: ["local-instructions.md"],
1069
+ }),
1070
+ )
1071
+ },
1072
+ })
1073
+
1074
+ await Instance.provide({
1075
+ directory: path.join(tmp.path, "project"),
1076
+ fn: async () => {
1077
+ const config = await Config.get()
1078
+ const instructions = config.instructions ?? []
1079
+
1080
+ expect(instructions).toContain("global-instructions.md")
1081
+ expect(instructions).toContain("shared-rules.md")
1082
+ expect(instructions).toContain("local-instructions.md")
1083
+ expect(instructions.length).toBe(3)
1084
+ },
1085
+ })
1086
+ })
1087
+
1088
+ test("deduplicates duplicate instructions from global and local configs", async () => {
1089
+ await using tmp = await tmpdir({
1090
+ init: async (dir) => {
1091
+ const projectDir = path.join(dir, "project")
1092
+ const opencodeDir = path.join(projectDir, ".opencode")
1093
+ await fs.mkdir(opencodeDir, { recursive: true })
1094
+
1095
+ await Filesystem.write(
1096
+ path.join(dir, "opencode.json"),
1097
+ JSON.stringify({
1098
+ $schema: "https://opencode.ai/config.json",
1099
+ instructions: ["duplicate.md", "global-only.md"],
1100
+ }),
1101
+ )
1102
+
1103
+ await Filesystem.write(
1104
+ path.join(opencodeDir, "opencode.json"),
1105
+ JSON.stringify({
1106
+ $schema: "https://opencode.ai/config.json",
1107
+ instructions: ["duplicate.md", "local-only.md"],
1108
+ }),
1109
+ )
1110
+ },
1111
+ })
1112
+
1113
+ await Instance.provide({
1114
+ directory: path.join(tmp.path, "project"),
1115
+ fn: async () => {
1116
+ const config = await Config.get()
1117
+ const instructions = config.instructions ?? []
1118
+
1119
+ expect(instructions).toContain("global-only.md")
1120
+ expect(instructions).toContain("local-only.md")
1121
+ expect(instructions).toContain("duplicate.md")
1122
+
1123
+ const duplicates = instructions.filter((i) => i === "duplicate.md")
1124
+ expect(duplicates.length).toBe(1)
1125
+ expect(instructions.length).toBe(3)
1126
+ },
1127
+ })
1128
+ })
1129
+
1130
+ test("deduplicates duplicate plugins from global and local configs", async () => {
1131
+ await using tmp = await tmpdir({
1132
+ init: async (dir) => {
1133
+ // Create a nested project structure with local .opencode config
1134
+ const projectDir = path.join(dir, "project")
1135
+ const opencodeDir = path.join(projectDir, ".opencode")
1136
+ await fs.mkdir(opencodeDir, { recursive: true })
1137
+
1138
+ // Global config with plugins
1139
+ await Filesystem.write(
1140
+ path.join(dir, "opencode.json"),
1141
+ JSON.stringify({
1142
+ $schema: "https://opencode.ai/config.json",
1143
+ plugin: ["duplicate-plugin", "global-plugin-1"],
1144
+ }),
1145
+ )
1146
+
1147
+ // Local .opencode config with some overlapping plugins
1148
+ await Filesystem.write(
1149
+ path.join(opencodeDir, "opencode.json"),
1150
+ JSON.stringify({
1151
+ $schema: "https://opencode.ai/config.json",
1152
+ plugin: ["duplicate-plugin", "local-plugin-1"],
1153
+ }),
1154
+ )
1155
+ },
1156
+ })
1157
+
1158
+ await Instance.provide({
1159
+ directory: path.join(tmp.path, "project"),
1160
+ fn: async () => {
1161
+ const config = await Config.get()
1162
+ const plugins = config.plugin ?? []
1163
+
1164
+ // Should contain all unique plugins
1165
+ expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
1166
+ expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
1167
+ expect(plugins.some((p) => p.includes("duplicate-plugin"))).toBe(true)
1168
+
1169
+ // Should deduplicate the duplicate plugin
1170
+ const duplicatePlugins = plugins.filter((p) => p.includes("duplicate-plugin"))
1171
+ expect(duplicatePlugins.length).toBe(1)
1172
+
1173
+ // Should have exactly 3 unique plugins
1174
+ const pluginNames = plugins.filter(
1175
+ (p) => p.includes("global-plugin") || p.includes("local-plugin") || p.includes("duplicate-plugin"),
1176
+ )
1177
+ expect(pluginNames.length).toBe(3)
1178
+ },
1179
+ })
1180
+ })
1181
+
1182
+ test("keeps plugin origins aligned with merged plugin list", async () => {
1183
+ await using tmp = await tmpdir({
1184
+ init: async (dir) => {
1185
+ const project = path.join(dir, "project")
1186
+ const local = path.join(project, ".opencode")
1187
+ await fs.mkdir(local, { recursive: true })
1188
+
1189
+ await Filesystem.write(
1190
+ path.join(dir, "opencode.json"),
1191
+ JSON.stringify({
1192
+ $schema: "https://opencode.ai/config.json",
1193
+ plugin: [["shared-plugin@1.0.0", { source: "global" }], "global-only@1.0.0"],
1194
+ }),
1195
+ )
1196
+
1197
+ await Filesystem.write(
1198
+ path.join(local, "opencode.json"),
1199
+ JSON.stringify({
1200
+ $schema: "https://opencode.ai/config.json",
1201
+ plugin: [["shared-plugin@2.0.0", { source: "local" }], "local-only@1.0.0"],
1202
+ }),
1203
+ )
1204
+ },
1205
+ })
1206
+
1207
+ await Instance.provide({
1208
+ directory: path.join(tmp.path, "project"),
1209
+ fn: async () => {
1210
+ const cfg = await Config.get()
1211
+ const plugins = cfg.plugin ?? []
1212
+ const origins = cfg.plugin_origins ?? []
1213
+ const names = plugins.map((item) => Config.pluginSpecifier(item))
1214
+
1215
+ expect(names).toContain("shared-plugin@2.0.0")
1216
+ expect(names).not.toContain("shared-plugin@1.0.0")
1217
+ expect(names).toContain("global-only@1.0.0")
1218
+ expect(names).toContain("local-only@1.0.0")
1219
+
1220
+ expect(origins.map((item) => item.spec)).toEqual(plugins)
1221
+ const hit = origins.find((item) => Config.pluginSpecifier(item.spec) === "shared-plugin@2.0.0")
1222
+ expect(hit?.scope).toBe("local")
1223
+ },
1224
+ })
1225
+ })
1226
+
1227
+ // Legacy tools migration tests
1228
+
1229
+ test("migrates legacy tools config to permissions - allow", async () => {
1230
+ await using tmp = await tmpdir({
1231
+ init: async (dir) => {
1232
+ await Filesystem.write(
1233
+ path.join(dir, "opencode.json"),
1234
+ JSON.stringify({
1235
+ $schema: "https://opencode.ai/config.json",
1236
+ agent: {
1237
+ test: {
1238
+ tools: {
1239
+ bash: true,
1240
+ read: true,
1241
+ },
1242
+ },
1243
+ },
1244
+ }),
1245
+ )
1246
+ },
1247
+ })
1248
+ await Instance.provide({
1249
+ directory: tmp.path,
1250
+ fn: async () => {
1251
+ const config = await Config.get()
1252
+ expect(config.agent?.["test"]?.permission).toEqual({
1253
+ bash: "allow",
1254
+ read: "allow",
1255
+ })
1256
+ },
1257
+ })
1258
+ })
1259
+
1260
+ test("migrates legacy tools config to permissions - deny", async () => {
1261
+ await using tmp = await tmpdir({
1262
+ init: async (dir) => {
1263
+ await Filesystem.write(
1264
+ path.join(dir, "opencode.json"),
1265
+ JSON.stringify({
1266
+ $schema: "https://opencode.ai/config.json",
1267
+ agent: {
1268
+ test: {
1269
+ tools: {
1270
+ bash: false,
1271
+ webfetch: false,
1272
+ },
1273
+ },
1274
+ },
1275
+ }),
1276
+ )
1277
+ },
1278
+ })
1279
+ await Instance.provide({
1280
+ directory: tmp.path,
1281
+ fn: async () => {
1282
+ const config = await Config.get()
1283
+ expect(config.agent?.["test"]?.permission).toEqual({
1284
+ bash: "deny",
1285
+ webfetch: "deny",
1286
+ })
1287
+ },
1288
+ })
1289
+ })
1290
+
1291
+ test("migrates legacy write tool to edit permission", async () => {
1292
+ await using tmp = await tmpdir({
1293
+ init: async (dir) => {
1294
+ await Filesystem.write(
1295
+ path.join(dir, "opencode.json"),
1296
+ JSON.stringify({
1297
+ $schema: "https://opencode.ai/config.json",
1298
+ agent: {
1299
+ test: {
1300
+ tools: {
1301
+ write: true,
1302
+ },
1303
+ },
1304
+ },
1305
+ }),
1306
+ )
1307
+ },
1308
+ })
1309
+ await Instance.provide({
1310
+ directory: tmp.path,
1311
+ fn: async () => {
1312
+ const config = await Config.get()
1313
+ expect(config.agent?.["test"]?.permission).toEqual({
1314
+ edit: "allow",
1315
+ })
1316
+ },
1317
+ })
1318
+ })
1319
+
1320
+ // Managed settings tests
1321
+ // Note: preload.ts sets OPENCODE_TEST_MANAGED_CONFIG which Global.Path.managedConfig uses
1322
+
1323
+ test("managed settings override user settings", async () => {
1324
+ await using tmp = await tmpdir({
1325
+ init: async (dir) => {
1326
+ await writeConfig(dir, {
1327
+ $schema: "https://opencode.ai/config.json",
1328
+ model: "user/model",
1329
+ share: "auto",
1330
+ username: "testuser",
1331
+ })
1332
+ },
1333
+ })
1334
+
1335
+ await writeManagedSettings({
1336
+ $schema: "https://opencode.ai/config.json",
1337
+ model: "managed/model",
1338
+ share: "disabled",
1339
+ })
1340
+
1341
+ await Instance.provide({
1342
+ directory: tmp.path,
1343
+ fn: async () => {
1344
+ const config = await Config.get()
1345
+ expect(config.model).toBe("managed/model")
1346
+ expect(config.share).toBe("disabled")
1347
+ expect(config.username).toBe("testuser")
1348
+ },
1349
+ })
1350
+ })
1351
+
1352
+ test("managed settings override project settings", async () => {
1353
+ await using tmp = await tmpdir({
1354
+ init: async (dir) => {
1355
+ await writeConfig(dir, {
1356
+ $schema: "https://opencode.ai/config.json",
1357
+ autoupdate: true,
1358
+ disabled_providers: [],
1359
+ })
1360
+ },
1361
+ })
1362
+
1363
+ await writeManagedSettings({
1364
+ $schema: "https://opencode.ai/config.json",
1365
+ autoupdate: false,
1366
+ disabled_providers: ["openai"],
1367
+ })
1368
+
1369
+ await Instance.provide({
1370
+ directory: tmp.path,
1371
+ fn: async () => {
1372
+ const config = await Config.get()
1373
+ expect(config.autoupdate).toBe(false)
1374
+ expect(config.disabled_providers).toEqual(["openai"])
1375
+ },
1376
+ })
1377
+ })
1378
+
1379
+ test("missing managed settings file is not an error", async () => {
1380
+ await using tmp = await tmpdir({
1381
+ init: async (dir) => {
1382
+ await writeConfig(dir, {
1383
+ $schema: "https://opencode.ai/config.json",
1384
+ model: "user/model",
1385
+ })
1386
+ },
1387
+ })
1388
+
1389
+ await Instance.provide({
1390
+ directory: tmp.path,
1391
+ fn: async () => {
1392
+ const config = await Config.get()
1393
+ expect(config.model).toBe("user/model")
1394
+ },
1395
+ })
1396
+ })
1397
+
1398
+ test("migrates legacy edit tool to edit permission", async () => {
1399
+ await using tmp = await tmpdir({
1400
+ init: async (dir) => {
1401
+ await Filesystem.write(
1402
+ path.join(dir, "opencode.json"),
1403
+ JSON.stringify({
1404
+ $schema: "https://opencode.ai/config.json",
1405
+ agent: {
1406
+ test: {
1407
+ tools: {
1408
+ edit: false,
1409
+ },
1410
+ },
1411
+ },
1412
+ }),
1413
+ )
1414
+ },
1415
+ })
1416
+ await Instance.provide({
1417
+ directory: tmp.path,
1418
+ fn: async () => {
1419
+ const config = await Config.get()
1420
+ expect(config.agent?.["test"]?.permission).toEqual({
1421
+ edit: "deny",
1422
+ })
1423
+ },
1424
+ })
1425
+ })
1426
+
1427
+ test("migrates legacy patch tool to edit permission", async () => {
1428
+ await using tmp = await tmpdir({
1429
+ init: async (dir) => {
1430
+ await Filesystem.write(
1431
+ path.join(dir, "opencode.json"),
1432
+ JSON.stringify({
1433
+ $schema: "https://opencode.ai/config.json",
1434
+ agent: {
1435
+ test: {
1436
+ tools: {
1437
+ patch: true,
1438
+ },
1439
+ },
1440
+ },
1441
+ }),
1442
+ )
1443
+ },
1444
+ })
1445
+ await Instance.provide({
1446
+ directory: tmp.path,
1447
+ fn: async () => {
1448
+ const config = await Config.get()
1449
+ expect(config.agent?.["test"]?.permission).toEqual({
1450
+ edit: "allow",
1451
+ })
1452
+ },
1453
+ })
1454
+ })
1455
+
1456
+ test("migrates legacy multiedit tool to edit permission", async () => {
1457
+ await using tmp = await tmpdir({
1458
+ init: async (dir) => {
1459
+ await Filesystem.write(
1460
+ path.join(dir, "opencode.json"),
1461
+ JSON.stringify({
1462
+ $schema: "https://opencode.ai/config.json",
1463
+ agent: {
1464
+ test: {
1465
+ tools: {
1466
+ multiedit: false,
1467
+ },
1468
+ },
1469
+ },
1470
+ }),
1471
+ )
1472
+ },
1473
+ })
1474
+ await Instance.provide({
1475
+ directory: tmp.path,
1476
+ fn: async () => {
1477
+ const config = await Config.get()
1478
+ expect(config.agent?.["test"]?.permission).toEqual({
1479
+ edit: "deny",
1480
+ })
1481
+ },
1482
+ })
1483
+ })
1484
+
1485
+ test("migrates mixed legacy tools config", async () => {
1486
+ await using tmp = await tmpdir({
1487
+ init: async (dir) => {
1488
+ await Filesystem.write(
1489
+ path.join(dir, "opencode.json"),
1490
+ JSON.stringify({
1491
+ $schema: "https://opencode.ai/config.json",
1492
+ agent: {
1493
+ test: {
1494
+ tools: {
1495
+ bash: true,
1496
+ write: true,
1497
+ read: false,
1498
+ webfetch: true,
1499
+ },
1500
+ },
1501
+ },
1502
+ }),
1503
+ )
1504
+ },
1505
+ })
1506
+ await Instance.provide({
1507
+ directory: tmp.path,
1508
+ fn: async () => {
1509
+ const config = await Config.get()
1510
+ expect(config.agent?.["test"]?.permission).toEqual({
1511
+ bash: "allow",
1512
+ edit: "allow",
1513
+ read: "deny",
1514
+ webfetch: "allow",
1515
+ })
1516
+ },
1517
+ })
1518
+ })
1519
+
1520
+ test("merges legacy tools with existing permission config", async () => {
1521
+ await using tmp = await tmpdir({
1522
+ init: async (dir) => {
1523
+ await Filesystem.write(
1524
+ path.join(dir, "opencode.json"),
1525
+ JSON.stringify({
1526
+ $schema: "https://opencode.ai/config.json",
1527
+ agent: {
1528
+ test: {
1529
+ permission: {
1530
+ glob: "allow",
1531
+ },
1532
+ tools: {
1533
+ bash: true,
1534
+ },
1535
+ },
1536
+ },
1537
+ }),
1538
+ )
1539
+ },
1540
+ })
1541
+ await Instance.provide({
1542
+ directory: tmp.path,
1543
+ fn: async () => {
1544
+ const config = await Config.get()
1545
+ expect(config.agent?.["test"]?.permission).toEqual({
1546
+ glob: "allow",
1547
+ bash: "allow",
1548
+ })
1549
+ },
1550
+ })
1551
+ })
1552
+
1553
+ test("permission config preserves key order", async () => {
1554
+ await using tmp = await tmpdir({
1555
+ init: async (dir) => {
1556
+ await Filesystem.write(
1557
+ path.join(dir, "opencode.json"),
1558
+ JSON.stringify({
1559
+ $schema: "https://opencode.ai/config.json",
1560
+ permission: {
1561
+ "*": "deny",
1562
+ edit: "ask",
1563
+ write: "ask",
1564
+ external_directory: "ask",
1565
+ read: "allow",
1566
+ todowrite: "allow",
1567
+ "thoughts_*": "allow",
1568
+ "reasoning_model_*": "allow",
1569
+ "tools_*": "allow",
1570
+ "pr_comments_*": "allow",
1571
+ },
1572
+ }),
1573
+ )
1574
+ },
1575
+ })
1576
+ await Instance.provide({
1577
+ directory: tmp.path,
1578
+ fn: async () => {
1579
+ const config = await Config.get()
1580
+ expect(Object.keys(config.permission!)).toEqual([
1581
+ "*",
1582
+ "edit",
1583
+ "write",
1584
+ "external_directory",
1585
+ "read",
1586
+ "todowrite",
1587
+ "thoughts_*",
1588
+ "reasoning_model_*",
1589
+ "tools_*",
1590
+ "pr_comments_*",
1591
+ ])
1592
+ },
1593
+ })
1594
+ })
1595
+
1596
+ // MCP config merging tests
1597
+
1598
+ test("project config can override MCP server enabled status", async () => {
1599
+ await using tmp = await tmpdir({
1600
+ init: async (dir) => {
1601
+ // Simulates a base config (like from remote .well-known) with disabled MCP
1602
+ await Filesystem.write(
1603
+ path.join(dir, "opencode.json"),
1604
+ JSON.stringify({
1605
+ $schema: "https://opencode.ai/config.json",
1606
+ mcp: {
1607
+ jira: {
1608
+ type: "remote",
1609
+ url: "https://jira.example.com/mcp",
1610
+ enabled: false,
1611
+ },
1612
+ wiki: {
1613
+ type: "remote",
1614
+ url: "https://wiki.example.com/mcp",
1615
+ enabled: false,
1616
+ },
1617
+ },
1618
+ }),
1619
+ )
1620
+ // Project config enables just jira
1621
+ await Filesystem.write(
1622
+ path.join(dir, "opencode.jsonc"),
1623
+ JSON.stringify({
1624
+ $schema: "https://opencode.ai/config.json",
1625
+ mcp: {
1626
+ jira: {
1627
+ type: "remote",
1628
+ url: "https://jira.example.com/mcp",
1629
+ enabled: true,
1630
+ },
1631
+ },
1632
+ }),
1633
+ )
1634
+ },
1635
+ })
1636
+ await Instance.provide({
1637
+ directory: tmp.path,
1638
+ fn: async () => {
1639
+ const config = await Config.get()
1640
+ // jira should be enabled (overridden by project config)
1641
+ expect(config.mcp?.jira).toEqual({
1642
+ type: "remote",
1643
+ url: "https://jira.example.com/mcp",
1644
+ enabled: true,
1645
+ })
1646
+ // wiki should still be disabled (not overridden)
1647
+ expect(config.mcp?.wiki).toEqual({
1648
+ type: "remote",
1649
+ url: "https://wiki.example.com/mcp",
1650
+ enabled: false,
1651
+ })
1652
+ },
1653
+ })
1654
+ })
1655
+
1656
+ test("MCP config deep merges preserving base config properties", async () => {
1657
+ await using tmp = await tmpdir({
1658
+ init: async (dir) => {
1659
+ // Base config with full MCP definition
1660
+ await Filesystem.write(
1661
+ path.join(dir, "opencode.json"),
1662
+ JSON.stringify({
1663
+ $schema: "https://opencode.ai/config.json",
1664
+ mcp: {
1665
+ myserver: {
1666
+ type: "remote",
1667
+ url: "https://myserver.example.com/mcp",
1668
+ enabled: false,
1669
+ headers: {
1670
+ "X-Custom-Header": "value",
1671
+ },
1672
+ },
1673
+ },
1674
+ }),
1675
+ )
1676
+ // Override just enables it, should preserve other properties
1677
+ await Filesystem.write(
1678
+ path.join(dir, "opencode.jsonc"),
1679
+ JSON.stringify({
1680
+ $schema: "https://opencode.ai/config.json",
1681
+ mcp: {
1682
+ myserver: {
1683
+ type: "remote",
1684
+ url: "https://myserver.example.com/mcp",
1685
+ enabled: true,
1686
+ },
1687
+ },
1688
+ }),
1689
+ )
1690
+ },
1691
+ })
1692
+ await Instance.provide({
1693
+ directory: tmp.path,
1694
+ fn: async () => {
1695
+ const config = await Config.get()
1696
+ expect(config.mcp?.myserver).toEqual({
1697
+ type: "remote",
1698
+ url: "https://myserver.example.com/mcp",
1699
+ enabled: true,
1700
+ headers: {
1701
+ "X-Custom-Header": "value",
1702
+ },
1703
+ })
1704
+ },
1705
+ })
1706
+ })
1707
+
1708
+ test("local .opencode config can override MCP from project config", async () => {
1709
+ await using tmp = await tmpdir({
1710
+ init: async (dir) => {
1711
+ // Project config with disabled MCP
1712
+ await Filesystem.write(
1713
+ path.join(dir, "opencode.json"),
1714
+ JSON.stringify({
1715
+ $schema: "https://opencode.ai/config.json",
1716
+ mcp: {
1717
+ docs: {
1718
+ type: "remote",
1719
+ url: "https://docs.example.com/mcp",
1720
+ enabled: false,
1721
+ },
1722
+ },
1723
+ }),
1724
+ )
1725
+ // Local .opencode directory config enables it
1726
+ const opencodeDir = path.join(dir, ".opencode")
1727
+ await fs.mkdir(opencodeDir, { recursive: true })
1728
+ await Filesystem.write(
1729
+ path.join(opencodeDir, "opencode.json"),
1730
+ JSON.stringify({
1731
+ $schema: "https://opencode.ai/config.json",
1732
+ mcp: {
1733
+ docs: {
1734
+ type: "remote",
1735
+ url: "https://docs.example.com/mcp",
1736
+ enabled: true,
1737
+ },
1738
+ },
1739
+ }),
1740
+ )
1741
+ },
1742
+ })
1743
+ await Instance.provide({
1744
+ directory: tmp.path,
1745
+ fn: async () => {
1746
+ const config = await Config.get()
1747
+ expect(config.mcp?.docs?.enabled).toBe(true)
1748
+ },
1749
+ })
1750
+ })
1751
+
1752
+ test("project config overrides remote well-known config", async () => {
1753
+ const originalFetch = globalThis.fetch
1754
+ let fetchedUrl: string | undefined
1755
+ globalThis.fetch = mock((url: string | URL | Request) => {
1756
+ const urlStr = url.toString()
1757
+ if (urlStr.includes(".well-known/opencode")) {
1758
+ fetchedUrl = urlStr
1759
+ return Promise.resolve(
1760
+ new Response(
1761
+ JSON.stringify({
1762
+ config: {
1763
+ mcp: { jira: { type: "remote", url: "https://jira.example.com/mcp", enabled: false } },
1764
+ },
1765
+ }),
1766
+ { status: 200 },
1767
+ ),
1768
+ )
1769
+ }
1770
+ return originalFetch(url)
1771
+ }) as unknown as typeof fetch
1772
+
1773
+ const fakeAuth = Layer.mock(Auth.Service)({
1774
+ all: () =>
1775
+ Effect.succeed({
1776
+ "https://example.com": new Auth.WellKnown({ type: "wellknown", key: "TEST_TOKEN", token: "test-token" }),
1777
+ }),
1778
+ })
1779
+
1780
+ const layer = Config.layer.pipe(
1781
+ Layer.provide(AppFileSystem.defaultLayer),
1782
+ Layer.provide(fakeAuth),
1783
+ Layer.provide(emptyAccount),
1784
+ Layer.provideMerge(infra),
1785
+ )
1786
+
1787
+ try {
1788
+ await provideTmpdirInstance(
1789
+ () =>
1790
+ Config.Service.use((svc) =>
1791
+ Effect.gen(function* () {
1792
+ const config = yield* svc.get()
1793
+ expect(fetchedUrl).toBe("https://example.com/.well-known/opencode")
1794
+ expect(config.mcp?.jira?.enabled).toBe(true)
1795
+ }),
1796
+ ),
1797
+ {
1798
+ git: true,
1799
+ config: { mcp: { jira: { type: "remote", url: "https://jira.example.com/mcp", enabled: true } } },
1800
+ },
1801
+ ).pipe(Effect.scoped, Effect.provide(layer), Effect.runPromise)
1802
+ } finally {
1803
+ globalThis.fetch = originalFetch
1804
+ }
1805
+ })
1806
+
1807
+ test("wellknown URL with trailing slash is normalized", async () => {
1808
+ const originalFetch = globalThis.fetch
1809
+ let fetchedUrl: string | undefined
1810
+ globalThis.fetch = mock((url: string | URL | Request) => {
1811
+ const urlStr = url.toString()
1812
+ if (urlStr.includes(".well-known/opencode")) {
1813
+ fetchedUrl = urlStr
1814
+ return Promise.resolve(
1815
+ new Response(
1816
+ JSON.stringify({
1817
+ config: {
1818
+ mcp: { slack: { type: "remote", url: "https://slack.example.com/mcp", enabled: true } },
1819
+ },
1820
+ }),
1821
+ { status: 200 },
1822
+ ),
1823
+ )
1824
+ }
1825
+ return originalFetch(url)
1826
+ }) as unknown as typeof fetch
1827
+
1828
+ const fakeAuth = Layer.mock(Auth.Service)({
1829
+ all: () =>
1830
+ Effect.succeed({
1831
+ "https://example.com/": new Auth.WellKnown({ type: "wellknown", key: "TEST_TOKEN", token: "test-token" }),
1832
+ }),
1833
+ })
1834
+
1835
+ const layer = Config.layer.pipe(
1836
+ Layer.provide(AppFileSystem.defaultLayer),
1837
+ Layer.provide(fakeAuth),
1838
+ Layer.provide(emptyAccount),
1839
+ Layer.provideMerge(infra),
1840
+ )
1841
+
1842
+ try {
1843
+ await provideTmpdirInstance(
1844
+ () =>
1845
+ Config.Service.use((svc) =>
1846
+ Effect.gen(function* () {
1847
+ yield* svc.get()
1848
+ expect(fetchedUrl).toBe("https://example.com/.well-known/opencode")
1849
+ }),
1850
+ ),
1851
+ { git: true },
1852
+ ).pipe(Effect.scoped, Effect.provide(layer), Effect.runPromise)
1853
+ } finally {
1854
+ globalThis.fetch = originalFetch
1855
+ }
1856
+ })
1857
+
1858
+ describe("resolvePluginSpec", () => {
1859
+ test("keeps package specs unchanged", async () => {
1860
+ await using tmp = await tmpdir()
1861
+ const file = path.join(tmp.path, "opencode.json")
1862
+ expect(await Config.resolvePluginSpec("oh-my-opencode@2.4.3", file)).toBe("oh-my-opencode@2.4.3")
1863
+ expect(await Config.resolvePluginSpec("@scope/pkg", file)).toBe("@scope/pkg")
1864
+ })
1865
+
1866
+ test("resolves windows-style relative plugin directory specs", async () => {
1867
+ if (process.platform !== "win32") return
1868
+
1869
+ await using tmp = await tmpdir({
1870
+ init: async (dir) => {
1871
+ const plugin = path.join(dir, "plugin")
1872
+ await fs.mkdir(plugin, { recursive: true })
1873
+ await Filesystem.write(path.join(plugin, "index.ts"), "export default {}")
1874
+ },
1875
+ })
1876
+
1877
+ const file = path.join(tmp.path, "opencode.json")
1878
+ const hit = await Config.resolvePluginSpec(".\\plugin", file)
1879
+ expect(Config.pluginSpecifier(hit)).toBe(pathToFileURL(path.join(tmp.path, "plugin", "index.ts")).href)
1880
+ })
1881
+
1882
+ test("resolves relative file plugin paths to file urls", async () => {
1883
+ await using tmp = await tmpdir({
1884
+ init: async (dir) => {
1885
+ await Filesystem.write(path.join(dir, "plugin.ts"), "export default {}")
1886
+ },
1887
+ })
1888
+
1889
+ const file = path.join(tmp.path, "opencode.json")
1890
+ const hit = await Config.resolvePluginSpec("./plugin.ts", file)
1891
+ expect(Config.pluginSpecifier(hit)).toBe(pathToFileURL(path.join(tmp.path, "plugin.ts")).href)
1892
+ })
1893
+
1894
+ test("resolves plugin directory paths to directory urls", async () => {
1895
+ await using tmp = await tmpdir({
1896
+ init: async (dir) => {
1897
+ const plugin = path.join(dir, "plugin")
1898
+ await fs.mkdir(plugin, { recursive: true })
1899
+ await Filesystem.writeJson(path.join(plugin, "package.json"), {
1900
+ name: "demo-plugin",
1901
+ type: "module",
1902
+ main: "./index.ts",
1903
+ })
1904
+ await Filesystem.write(path.join(plugin, "index.ts"), "export default {}")
1905
+ },
1906
+ })
1907
+
1908
+ const file = path.join(tmp.path, "opencode.json")
1909
+ const hit = await Config.resolvePluginSpec("./plugin", file)
1910
+ expect(Config.pluginSpecifier(hit)).toBe(pathToFileURL(path.join(tmp.path, "plugin")).href)
1911
+ })
1912
+
1913
+ test("resolves plugin directories without package.json to index.ts", async () => {
1914
+ await using tmp = await tmpdir({
1915
+ init: async (dir) => {
1916
+ const plugin = path.join(dir, "plugin")
1917
+ await fs.mkdir(plugin, { recursive: true })
1918
+ await Filesystem.write(path.join(plugin, "index.ts"), "export default {}")
1919
+ },
1920
+ })
1921
+
1922
+ const file = path.join(tmp.path, "opencode.json")
1923
+ const hit = await Config.resolvePluginSpec("./plugin", file)
1924
+ expect(Config.pluginSpecifier(hit)).toBe(pathToFileURL(path.join(tmp.path, "plugin", "index.ts")).href)
1925
+ })
1926
+ })
1927
+
1928
+ describe("deduplicatePluginOrigins", () => {
1929
+ const dedupe = (plugins: Config.PluginSpec[]) =>
1930
+ Config.deduplicatePluginOrigins(
1931
+ plugins.map((spec) => ({
1932
+ spec,
1933
+ source: "",
1934
+ scope: "global" as const,
1935
+ })),
1936
+ ).map((item) => item.spec)
1937
+
1938
+ test("removes duplicates keeping higher priority (later entries)", () => {
1939
+ const plugins = ["global-plugin@1.0.0", "shared-plugin@1.0.0", "local-plugin@2.0.0", "shared-plugin@2.0.0"]
1940
+
1941
+ const result = dedupe(plugins)
1942
+
1943
+ expect(result).toContain("global-plugin@1.0.0")
1944
+ expect(result).toContain("local-plugin@2.0.0")
1945
+ expect(result).toContain("shared-plugin@2.0.0")
1946
+ expect(result).not.toContain("shared-plugin@1.0.0")
1947
+ expect(result.length).toBe(3)
1948
+ })
1949
+
1950
+ test("keeps path plugins separate from package plugins", () => {
1951
+ const plugins = ["oh-my-opencode@2.4.3", "file:///project/.opencode/plugin/oh-my-opencode.js"]
1952
+
1953
+ const result = dedupe(plugins)
1954
+
1955
+ expect(result).toEqual(plugins)
1956
+ })
1957
+
1958
+ test("deduplicates direct path plugins by exact spec", () => {
1959
+ const plugins = ["file:///project/.opencode/plugin/demo.ts", "file:///project/.opencode/plugin/demo.ts"]
1960
+
1961
+ const result = dedupe(plugins)
1962
+
1963
+ expect(result).toEqual(["file:///project/.opencode/plugin/demo.ts"])
1964
+ })
1965
+
1966
+ test("preserves order of remaining plugins", () => {
1967
+ const plugins = ["a-plugin@1.0.0", "b-plugin@1.0.0", "c-plugin@1.0.0"]
1968
+
1969
+ const result = dedupe(plugins)
1970
+
1971
+ expect(result).toEqual(["a-plugin@1.0.0", "b-plugin@1.0.0", "c-plugin@1.0.0"])
1972
+ })
1973
+
1974
+ test("loads auto-discovered local plugins as file urls", async () => {
1975
+ await using tmp = await tmpdir({
1976
+ init: async (dir) => {
1977
+ const projectDir = path.join(dir, "project")
1978
+ const opencodeDir = path.join(projectDir, ".opencode")
1979
+ const pluginDir = path.join(opencodeDir, "plugin")
1980
+ await fs.mkdir(pluginDir, { recursive: true })
1981
+
1982
+ await Filesystem.write(
1983
+ path.join(dir, "opencode.json"),
1984
+ JSON.stringify({
1985
+ $schema: "https://opencode.ai/config.json",
1986
+ plugin: ["my-plugin@1.0.0"],
1987
+ }),
1988
+ )
1989
+
1990
+ await Filesystem.write(path.join(pluginDir, "my-plugin.js"), "export default {}")
1991
+ },
1992
+ })
1993
+
1994
+ await Instance.provide({
1995
+ directory: path.join(tmp.path, "project"),
1996
+ fn: async () => {
1997
+ const config = await Config.get()
1998
+ const plugins = config.plugin ?? []
1999
+
2000
+ expect(plugins.some((p) => Config.pluginSpecifier(p) === "my-plugin@1.0.0")).toBe(true)
2001
+ expect(plugins.some((p) => Config.pluginSpecifier(p).startsWith("file://"))).toBe(true)
2002
+ },
2003
+ })
2004
+ })
2005
+ })
2006
+
2007
+ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
2008
+ test("skips project config files when flag is set", async () => {
2009
+ const originalEnv = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
2010
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
2011
+
2012
+ try {
2013
+ await using tmp = await tmpdir({
2014
+ init: async (dir) => {
2015
+ // Create a project config that would normally be loaded
2016
+ await Filesystem.write(
2017
+ path.join(dir, "opencode.json"),
2018
+ JSON.stringify({
2019
+ $schema: "https://opencode.ai/config.json",
2020
+ model: "project/model",
2021
+ username: "project-user",
2022
+ }),
2023
+ )
2024
+ },
2025
+ })
2026
+ await Instance.provide({
2027
+ directory: tmp.path,
2028
+ fn: async () => {
2029
+ const config = await Config.get()
2030
+ // Project config should NOT be loaded - model should be default, not "project/model"
2031
+ expect(config.model).not.toBe("project/model")
2032
+ expect(config.username).not.toBe("project-user")
2033
+ },
2034
+ })
2035
+ } finally {
2036
+ if (originalEnv === undefined) {
2037
+ delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
2038
+ } else {
2039
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalEnv
2040
+ }
2041
+ }
2042
+ })
2043
+
2044
+ test("skips project .opencode/ directories when flag is set", async () => {
2045
+ const originalEnv = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
2046
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
2047
+
2048
+ try {
2049
+ await using tmp = await tmpdir({
2050
+ init: async (dir) => {
2051
+ // Create a .opencode directory with a command
2052
+ const opencodeDir = path.join(dir, ".opencode", "command")
2053
+ await fs.mkdir(opencodeDir, { recursive: true })
2054
+ await Filesystem.write(path.join(opencodeDir, "test-cmd.md"), "# Test Command\nThis is a test command.")
2055
+ },
2056
+ })
2057
+ await Instance.provide({
2058
+ directory: tmp.path,
2059
+ fn: async () => {
2060
+ const directories = await Config.directories()
2061
+ // Project .opencode should NOT be in directories list
2062
+ const hasProjectOpencode = directories.some((d) => d.startsWith(tmp.path))
2063
+ expect(hasProjectOpencode).toBe(false)
2064
+ },
2065
+ })
2066
+ } finally {
2067
+ if (originalEnv === undefined) {
2068
+ delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
2069
+ } else {
2070
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalEnv
2071
+ }
2072
+ }
2073
+ })
2074
+
2075
+ test("still loads global config when flag is set", async () => {
2076
+ const originalEnv = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
2077
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
2078
+
2079
+ try {
2080
+ await using tmp = await tmpdir()
2081
+ await Instance.provide({
2082
+ directory: tmp.path,
2083
+ fn: async () => {
2084
+ // Should still get default config (from global or defaults)
2085
+ const config = await Config.get()
2086
+ expect(config).toBeDefined()
2087
+ expect(config.username).toBeDefined()
2088
+ },
2089
+ })
2090
+ } finally {
2091
+ if (originalEnv === undefined) {
2092
+ delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
2093
+ } else {
2094
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalEnv
2095
+ }
2096
+ }
2097
+ })
2098
+
2099
+ test("skips relative instructions with warning when flag is set but no config dir", async () => {
2100
+ const originalDisable = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
2101
+ const originalConfigDir = process.env["OPENCODE_CONFIG_DIR"]
2102
+
2103
+ try {
2104
+ // Ensure no config dir is set
2105
+ delete process.env["OPENCODE_CONFIG_DIR"]
2106
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
2107
+
2108
+ await using tmp = await tmpdir({
2109
+ init: async (dir) => {
2110
+ // Create a config with relative instruction path
2111
+ await Filesystem.write(
2112
+ path.join(dir, "opencode.json"),
2113
+ JSON.stringify({
2114
+ $schema: "https://opencode.ai/config.json",
2115
+ instructions: ["./CUSTOM.md"],
2116
+ }),
2117
+ )
2118
+ // Create the instruction file (should be skipped)
2119
+ await Filesystem.write(path.join(dir, "CUSTOM.md"), "# Custom Instructions")
2120
+ },
2121
+ })
2122
+
2123
+ await Instance.provide({
2124
+ directory: tmp.path,
2125
+ fn: async () => {
2126
+ // The relative instruction should be skipped without error
2127
+ // We're mainly verifying this doesn't throw and the config loads
2128
+ const config = await Config.get()
2129
+ expect(config).toBeDefined()
2130
+ // The instruction should have been skipped (warning logged)
2131
+ // We can't easily test the warning was logged, but we verify
2132
+ // the relative path didn't cause an error
2133
+ },
2134
+ })
2135
+ } finally {
2136
+ if (originalDisable === undefined) {
2137
+ delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
2138
+ } else {
2139
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalDisable
2140
+ }
2141
+ if (originalConfigDir === undefined) {
2142
+ delete process.env["OPENCODE_CONFIG_DIR"]
2143
+ } else {
2144
+ process.env["OPENCODE_CONFIG_DIR"] = originalConfigDir
2145
+ }
2146
+ }
2147
+ })
2148
+
2149
+ test("OPENCODE_CONFIG_DIR still works when flag is set", async () => {
2150
+ const originalDisable = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
2151
+ const originalConfigDir = process.env["OPENCODE_CONFIG_DIR"]
2152
+
2153
+ try {
2154
+ await using configDirTmp = await tmpdir({
2155
+ init: async (dir) => {
2156
+ // Create config in the custom config dir
2157
+ await Filesystem.write(
2158
+ path.join(dir, "opencode.json"),
2159
+ JSON.stringify({
2160
+ $schema: "https://opencode.ai/config.json",
2161
+ model: "configdir/model",
2162
+ }),
2163
+ )
2164
+ },
2165
+ })
2166
+
2167
+ await using projectTmp = await tmpdir({
2168
+ init: async (dir) => {
2169
+ // Create config in project (should be ignored)
2170
+ await Filesystem.write(
2171
+ path.join(dir, "opencode.json"),
2172
+ JSON.stringify({
2173
+ $schema: "https://opencode.ai/config.json",
2174
+ model: "project/model",
2175
+ }),
2176
+ )
2177
+ },
2178
+ })
2179
+
2180
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
2181
+ process.env["OPENCODE_CONFIG_DIR"] = configDirTmp.path
2182
+
2183
+ await Instance.provide({
2184
+ directory: projectTmp.path,
2185
+ fn: async () => {
2186
+ const config = await Config.get()
2187
+ // Should load from OPENCODE_CONFIG_DIR, not project
2188
+ expect(config.model).toBe("configdir/model")
2189
+ },
2190
+ })
2191
+ } finally {
2192
+ if (originalDisable === undefined) {
2193
+ delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
2194
+ } else {
2195
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalDisable
2196
+ }
2197
+ if (originalConfigDir === undefined) {
2198
+ delete process.env["OPENCODE_CONFIG_DIR"]
2199
+ } else {
2200
+ process.env["OPENCODE_CONFIG_DIR"] = originalConfigDir
2201
+ }
2202
+ }
2203
+ })
2204
+ })
2205
+
2206
+ describe("OPENCODE_CONFIG_CONTENT token substitution", () => {
2207
+ test("substitutes {env:} tokens in OPENCODE_CONFIG_CONTENT", async () => {
2208
+ const originalEnv = process.env["OPENCODE_CONFIG_CONTENT"]
2209
+ const originalTestVar = process.env["TEST_CONFIG_VAR"]
2210
+ process.env["TEST_CONFIG_VAR"] = "test_api_key_12345"
2211
+ process.env["OPENCODE_CONFIG_CONTENT"] = JSON.stringify({
2212
+ $schema: "https://opencode.ai/config.json",
2213
+ username: "{env:TEST_CONFIG_VAR}",
2214
+ })
2215
+
2216
+ try {
2217
+ await using tmp = await tmpdir()
2218
+ await Instance.provide({
2219
+ directory: tmp.path,
2220
+ fn: async () => {
2221
+ const config = await Config.get()
2222
+ expect(config.username).toBe("test_api_key_12345")
2223
+ },
2224
+ })
2225
+ } finally {
2226
+ if (originalEnv !== undefined) {
2227
+ process.env["OPENCODE_CONFIG_CONTENT"] = originalEnv
2228
+ } else {
2229
+ delete process.env["OPENCODE_CONFIG_CONTENT"]
2230
+ }
2231
+ if (originalTestVar !== undefined) {
2232
+ process.env["TEST_CONFIG_VAR"] = originalTestVar
2233
+ } else {
2234
+ delete process.env["TEST_CONFIG_VAR"]
2235
+ }
2236
+ }
2237
+ })
2238
+
2239
+ test("substitutes {file:} tokens in OPENCODE_CONFIG_CONTENT", async () => {
2240
+ const originalEnv = process.env["OPENCODE_CONFIG_CONTENT"]
2241
+
2242
+ try {
2243
+ await using tmp = await tmpdir({
2244
+ init: async (dir) => {
2245
+ await Filesystem.write(path.join(dir, "api_key.txt"), "secret_key_from_file")
2246
+ process.env["OPENCODE_CONFIG_CONTENT"] = JSON.stringify({
2247
+ $schema: "https://opencode.ai/config.json",
2248
+ username: "{file:./api_key.txt}",
2249
+ })
2250
+ },
2251
+ })
2252
+ await Instance.provide({
2253
+ directory: tmp.path,
2254
+ fn: async () => {
2255
+ const config = await Config.get()
2256
+ expect(config.username).toBe("secret_key_from_file")
2257
+ },
2258
+ })
2259
+ } finally {
2260
+ if (originalEnv !== undefined) {
2261
+ process.env["OPENCODE_CONFIG_CONTENT"] = originalEnv
2262
+ } else {
2263
+ delete process.env["OPENCODE_CONFIG_CONTENT"]
2264
+ }
2265
+ }
2266
+ })
2267
+ })
2268
+
2269
+ // parseManagedPlist unit tests — pure function, no OS interaction
2270
+
2271
+ test("parseManagedPlist strips MDM metadata keys", async () => {
2272
+ const config = await Config.parseManagedPlist(
2273
+ JSON.stringify({
2274
+ PayloadDisplayName: "OpenCode Managed",
2275
+ PayloadIdentifier: "ai.opencode.managed.test",
2276
+ PayloadType: "ai.opencode.managed",
2277
+ PayloadUUID: "AAAA-BBBB-CCCC",
2278
+ PayloadVersion: 1,
2279
+ _manualProfile: true,
2280
+ share: "disabled",
2281
+ model: "mdm/model",
2282
+ }),
2283
+ "test:mobileconfig",
2284
+ )
2285
+ expect(config.share).toBe("disabled")
2286
+ expect(config.model).toBe("mdm/model")
2287
+ // MDM keys must not leak into the parsed config
2288
+ expect((config as any).PayloadUUID).toBeUndefined()
2289
+ expect((config as any).PayloadType).toBeUndefined()
2290
+ expect((config as any)._manualProfile).toBeUndefined()
2291
+ })
2292
+
2293
+ test("parseManagedPlist parses server settings", async () => {
2294
+ const config = await Config.parseManagedPlist(
2295
+ JSON.stringify({
2296
+ $schema: "https://opencode.ai/config.json",
2297
+ server: { hostname: "127.0.0.1", mdns: false },
2298
+ autoupdate: true,
2299
+ }),
2300
+ "test:mobileconfig",
2301
+ )
2302
+ expect(config.server?.hostname).toBe("127.0.0.1")
2303
+ expect(config.server?.mdns).toBe(false)
2304
+ expect(config.autoupdate).toBe(true)
2305
+ })
2306
+
2307
+ test("parseManagedPlist parses permission rules", async () => {
2308
+ const config = await Config.parseManagedPlist(
2309
+ JSON.stringify({
2310
+ $schema: "https://opencode.ai/config.json",
2311
+ permission: {
2312
+ "*": "ask",
2313
+ bash: { "*": "ask", "rm -rf *": "deny", "curl *": "deny" },
2314
+ grep: "allow",
2315
+ glob: "allow",
2316
+ webfetch: "ask",
2317
+ "~/.ssh/*": "deny",
2318
+ },
2319
+ }),
2320
+ "test:mobileconfig",
2321
+ )
2322
+ expect(config.permission?.["*"]).toBe("ask")
2323
+ expect(config.permission?.grep).toBe("allow")
2324
+ expect(config.permission?.webfetch).toBe("ask")
2325
+ expect(config.permission?.["~/.ssh/*"]).toBe("deny")
2326
+ const bash = config.permission?.bash as Record<string, string>
2327
+ expect(bash?.["rm -rf *"]).toBe("deny")
2328
+ expect(bash?.["curl *"]).toBe("deny")
2329
+ })
2330
+
2331
+ test("parseManagedPlist parses enabled_providers", async () => {
2332
+ const config = await Config.parseManagedPlist(
2333
+ JSON.stringify({
2334
+ $schema: "https://opencode.ai/config.json",
2335
+ enabled_providers: ["anthropic", "google"],
2336
+ }),
2337
+ "test:mobileconfig",
2338
+ )
2339
+ expect(config.enabled_providers).toEqual(["anthropic", "google"])
2340
+ })
2341
+
2342
+ test("parseManagedPlist handles empty config", async () => {
2343
+ const config = await Config.parseManagedPlist(
2344
+ JSON.stringify({ $schema: "https://opencode.ai/config.json" }),
2345
+ "test:mobileconfig",
2346
+ )
2347
+ expect(config.$schema).toBe("https://opencode.ai/config.json")
2348
+ })