opencode-v2 1.1.53

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 (439) hide show
  1. package/AGENTS.md +27 -0
  2. package/Dockerfile +18 -0
  3. package/README.md +15 -0
  4. package/bin/opencode +84 -0
  5. package/bunfig.toml +5 -0
  6. package/package.json +126 -0
  7. package/parsers-config.ts +253 -0
  8. package/script/build.ts +193 -0
  9. package/script/postinstall.mjs +125 -0
  10. package/script/publish.ts +181 -0
  11. package/script/schema.ts +47 -0
  12. package/script/seed-e2e.ts +50 -0
  13. package/src/acp/README.md +164 -0
  14. package/src/acp/agent.ts +1676 -0
  15. package/src/acp/session.ts +117 -0
  16. package/src/acp/types.ts +23 -0
  17. package/src/agent/agent.ts +414 -0
  18. package/src/agent/generate.txt +75 -0
  19. package/src/agent/prompt/compaction.txt +12 -0
  20. package/src/agent/prompt/explore.txt +18 -0
  21. package/src/agent/prompt/summary.txt +11 -0
  22. package/src/agent/prompt/title.txt +44 -0
  23. package/src/auth/index.ts +70 -0
  24. package/src/bun/index.ts +137 -0
  25. package/src/bun/registry.ts +48 -0
  26. package/src/bus/bus-event.ts +43 -0
  27. package/src/bus/global.ts +10 -0
  28. package/src/bus/index.ts +105 -0
  29. package/src/cli/bootstrap.ts +17 -0
  30. package/src/cli/cmd/acp.ts +70 -0
  31. package/src/cli/cmd/agent.ts +257 -0
  32. package/src/cli/cmd/auth.ts +400 -0
  33. package/src/cli/cmd/cmd.ts +7 -0
  34. package/src/cli/cmd/debug/agent.ts +167 -0
  35. package/src/cli/cmd/debug/config.ts +16 -0
  36. package/src/cli/cmd/debug/file.ts +97 -0
  37. package/src/cli/cmd/debug/index.ts +48 -0
  38. package/src/cli/cmd/debug/lsp.ts +52 -0
  39. package/src/cli/cmd/debug/ripgrep.ts +87 -0
  40. package/src/cli/cmd/debug/scrap.ts +16 -0
  41. package/src/cli/cmd/debug/skill.ts +16 -0
  42. package/src/cli/cmd/debug/snapshot.ts +52 -0
  43. package/src/cli/cmd/export.ts +88 -0
  44. package/src/cli/cmd/generate.ts +38 -0
  45. package/src/cli/cmd/github.ts +1540 -0
  46. package/src/cli/cmd/import.ts +147 -0
  47. package/src/cli/cmd/mcp.ts +755 -0
  48. package/src/cli/cmd/models.ts +77 -0
  49. package/src/cli/cmd/pr.ts +112 -0
  50. package/src/cli/cmd/run.ts +617 -0
  51. package/src/cli/cmd/serve.ts +20 -0
  52. package/src/cli/cmd/session.ts +135 -0
  53. package/src/cli/cmd/stats.ts +426 -0
  54. package/src/cli/cmd/tui/app.tsx +801 -0
  55. package/src/cli/cmd/tui/attach.ts +52 -0
  56. package/src/cli/cmd/tui/component/border.tsx +21 -0
  57. package/src/cli/cmd/tui/component/dialog-agent.tsx +31 -0
  58. package/src/cli/cmd/tui/component/dialog-command.tsx +148 -0
  59. package/src/cli/cmd/tui/component/dialog-mcp.tsx +86 -0
  60. package/src/cli/cmd/tui/component/dialog-model.tsx +234 -0
  61. package/src/cli/cmd/tui/component/dialog-provider.tsx +266 -0
  62. package/src/cli/cmd/tui/component/dialog-session-list.tsx +108 -0
  63. package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
  64. package/src/cli/cmd/tui/component/dialog-skill.tsx +36 -0
  65. package/src/cli/cmd/tui/component/dialog-stash.tsx +87 -0
  66. package/src/cli/cmd/tui/component/dialog-status.tsx +177 -0
  67. package/src/cli/cmd/tui/component/dialog-tag.tsx +44 -0
  68. package/src/cli/cmd/tui/component/dialog-theme-list.tsx +50 -0
  69. package/src/cli/cmd/tui/component/logo.tsx +85 -0
  70. package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +666 -0
  71. package/src/cli/cmd/tui/component/prompt/frecency.tsx +89 -0
  72. package/src/cli/cmd/tui/component/prompt/history.tsx +108 -0
  73. package/src/cli/cmd/tui/component/prompt/index.tsx +1132 -0
  74. package/src/cli/cmd/tui/component/prompt/stash.tsx +101 -0
  75. package/src/cli/cmd/tui/component/spinner.tsx +24 -0
  76. package/src/cli/cmd/tui/component/textarea-keybindings.ts +73 -0
  77. package/src/cli/cmd/tui/component/tips.tsx +153 -0
  78. package/src/cli/cmd/tui/component/todo-item.tsx +32 -0
  79. package/src/cli/cmd/tui/context/args.tsx +15 -0
  80. package/src/cli/cmd/tui/context/directory.ts +13 -0
  81. package/src/cli/cmd/tui/context/exit.tsx +52 -0
  82. package/src/cli/cmd/tui/context/helper.tsx +25 -0
  83. package/src/cli/cmd/tui/context/keybind.tsx +100 -0
  84. package/src/cli/cmd/tui/context/kv.tsx +52 -0
  85. package/src/cli/cmd/tui/context/local.tsx +409 -0
  86. package/src/cli/cmd/tui/context/prompt.tsx +18 -0
  87. package/src/cli/cmd/tui/context/route.tsx +46 -0
  88. package/src/cli/cmd/tui/context/sdk.tsx +101 -0
  89. package/src/cli/cmd/tui/context/sync.tsx +470 -0
  90. package/src/cli/cmd/tui/context/theme/aura.json +69 -0
  91. package/src/cli/cmd/tui/context/theme/ayu.json +80 -0
  92. package/src/cli/cmd/tui/context/theme/carbonfox.json +248 -0
  93. package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +233 -0
  94. package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +233 -0
  95. package/src/cli/cmd/tui/context/theme/catppuccin.json +112 -0
  96. package/src/cli/cmd/tui/context/theme/cobalt2.json +228 -0
  97. package/src/cli/cmd/tui/context/theme/cursor.json +249 -0
  98. package/src/cli/cmd/tui/context/theme/dracula.json +219 -0
  99. package/src/cli/cmd/tui/context/theme/everforest.json +241 -0
  100. package/src/cli/cmd/tui/context/theme/flexoki.json +237 -0
  101. package/src/cli/cmd/tui/context/theme/github.json +233 -0
  102. package/src/cli/cmd/tui/context/theme/gruvbox.json +242 -0
  103. package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -0
  104. package/src/cli/cmd/tui/context/theme/lucent-orng.json +237 -0
  105. package/src/cli/cmd/tui/context/theme/material.json +235 -0
  106. package/src/cli/cmd/tui/context/theme/matrix.json +77 -0
  107. package/src/cli/cmd/tui/context/theme/mercury.json +252 -0
  108. package/src/cli/cmd/tui/context/theme/monokai.json +221 -0
  109. package/src/cli/cmd/tui/context/theme/nightowl.json +221 -0
  110. package/src/cli/cmd/tui/context/theme/nord.json +223 -0
  111. package/src/cli/cmd/tui/context/theme/one-dark.json +84 -0
  112. package/src/cli/cmd/tui/context/theme/orng.json +249 -0
  113. package/src/cli/cmd/tui/context/theme/osaka-jade.json +93 -0
  114. package/src/cli/cmd/tui/context/theme/palenight.json +222 -0
  115. package/src/cli/cmd/tui/context/theme/rosepine.json +234 -0
  116. package/src/cli/cmd/tui/context/theme/solarized.json +223 -0
  117. package/src/cli/cmd/tui/context/theme/synthwave84.json +226 -0
  118. package/src/cli/cmd/tui/context/theme/tokyonight.json +243 -0
  119. package/src/cli/cmd/tui/context/theme/vercel.json +245 -0
  120. package/src/cli/cmd/tui/context/theme/vesper.json +218 -0
  121. package/src/cli/cmd/tui/context/theme/zenburn.json +223 -0
  122. package/src/cli/cmd/tui/context/theme.tsx +1152 -0
  123. package/src/cli/cmd/tui/event.ts +48 -0
  124. package/src/cli/cmd/tui/routes/home.tsx +140 -0
  125. package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +64 -0
  126. package/src/cli/cmd/tui/routes/session/dialog-message.tsx +109 -0
  127. package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +26 -0
  128. package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +47 -0
  129. package/src/cli/cmd/tui/routes/session/footer.tsx +91 -0
  130. package/src/cli/cmd/tui/routes/session/header.tsx +142 -0
  131. package/src/cli/cmd/tui/routes/session/index.tsx +2126 -0
  132. package/src/cli/cmd/tui/routes/session/permission.tsx +508 -0
  133. package/src/cli/cmd/tui/routes/session/question.tsx +466 -0
  134. package/src/cli/cmd/tui/routes/session/sidebar.tsx +313 -0
  135. package/src/cli/cmd/tui/thread.ts +175 -0
  136. package/src/cli/cmd/tui/ui/dialog-alert.tsx +68 -0
  137. package/src/cli/cmd/tui/ui/dialog-confirm.tsx +93 -0
  138. package/src/cli/cmd/tui/ui/dialog-export-options.tsx +215 -0
  139. package/src/cli/cmd/tui/ui/dialog-help.tsx +49 -0
  140. package/src/cli/cmd/tui/ui/dialog-prompt.tsx +88 -0
  141. package/src/cli/cmd/tui/ui/dialog-select.tsx +399 -0
  142. package/src/cli/cmd/tui/ui/dialog.tsx +167 -0
  143. package/src/cli/cmd/tui/ui/link.tsx +28 -0
  144. package/src/cli/cmd/tui/ui/spinner.ts +368 -0
  145. package/src/cli/cmd/tui/ui/toast.tsx +100 -0
  146. package/src/cli/cmd/tui/util/clipboard.ts +159 -0
  147. package/src/cli/cmd/tui/util/editor.ts +32 -0
  148. package/src/cli/cmd/tui/util/signal.ts +7 -0
  149. package/src/cli/cmd/tui/util/terminal.ts +114 -0
  150. package/src/cli/cmd/tui/util/transcript.ts +98 -0
  151. package/src/cli/cmd/tui/worker.ts +152 -0
  152. package/src/cli/cmd/uninstall.ts +357 -0
  153. package/src/cli/cmd/upgrade.ts +73 -0
  154. package/src/cli/cmd/web.ts +81 -0
  155. package/src/cli/error.ts +57 -0
  156. package/src/cli/logo.ts +6 -0
  157. package/src/cli/network.ts +60 -0
  158. package/src/cli/ui.ts +113 -0
  159. package/src/cli/upgrade.ts +25 -0
  160. package/src/command/index.ts +150 -0
  161. package/src/command/template/initialize.txt +10 -0
  162. package/src/command/template/review.txt +99 -0
  163. package/src/config/config.ts +1477 -0
  164. package/src/config/markdown.ts +98 -0
  165. package/src/env/index.ts +28 -0
  166. package/src/file/ignore.ts +83 -0
  167. package/src/file/index.ts +583 -0
  168. package/src/file/ripgrep.ts +375 -0
  169. package/src/file/time.ts +69 -0
  170. package/src/file/watcher.ts +127 -0
  171. package/src/flag/flag.ts +97 -0
  172. package/src/format/formatter.ts +366 -0
  173. package/src/format/index.ts +137 -0
  174. package/src/global/index.ts +55 -0
  175. package/src/id/id.ts +83 -0
  176. package/src/ide/index.ts +76 -0
  177. package/src/index.ts +159 -0
  178. package/src/installation/index.ts +246 -0
  179. package/src/lsp/client.ts +252 -0
  180. package/src/lsp/index.ts +485 -0
  181. package/src/lsp/language.ts +119 -0
  182. package/src/lsp/server.ts +2046 -0
  183. package/src/mcp/auth.ts +132 -0
  184. package/src/mcp/index.ts +934 -0
  185. package/src/mcp/oauth-callback.ts +200 -0
  186. package/src/mcp/oauth-provider.ts +154 -0
  187. package/src/patch/index.ts +680 -0
  188. package/src/permission/arity.ts +163 -0
  189. package/src/permission/index.ts +210 -0
  190. package/src/permission/next.ts +280 -0
  191. package/src/plugin/codex.ts +624 -0
  192. package/src/plugin/copilot.ts +327 -0
  193. package/src/plugin/index.ts +138 -0
  194. package/src/project/bootstrap.ts +35 -0
  195. package/src/project/instance.ts +114 -0
  196. package/src/project/project.ts +371 -0
  197. package/src/project/state.ts +70 -0
  198. package/src/project/vcs.ts +76 -0
  199. package/src/provider/auth.ts +147 -0
  200. package/src/provider/models.ts +133 -0
  201. package/src/provider/provider.ts +1262 -0
  202. package/src/provider/sdk/copilot/README.md +5 -0
  203. package/src/provider/sdk/copilot/chat/convert-to-openai-compatible-chat-messages.ts +164 -0
  204. package/src/provider/sdk/copilot/chat/get-response-metadata.ts +15 -0
  205. package/src/provider/sdk/copilot/chat/map-openai-compatible-finish-reason.ts +17 -0
  206. package/src/provider/sdk/copilot/chat/openai-compatible-api-types.ts +64 -0
  207. package/src/provider/sdk/copilot/chat/openai-compatible-chat-language-model.ts +780 -0
  208. package/src/provider/sdk/copilot/chat/openai-compatible-chat-options.ts +28 -0
  209. package/src/provider/sdk/copilot/chat/openai-compatible-metadata-extractor.ts +44 -0
  210. package/src/provider/sdk/copilot/chat/openai-compatible-prepare-tools.ts +87 -0
  211. package/src/provider/sdk/copilot/copilot-provider.ts +100 -0
  212. package/src/provider/sdk/copilot/index.ts +2 -0
  213. package/src/provider/sdk/copilot/openai-compatible-error.ts +27 -0
  214. package/src/provider/sdk/copilot/responses/convert-to-openai-responses-input.ts +303 -0
  215. package/src/provider/sdk/copilot/responses/map-openai-responses-finish-reason.ts +22 -0
  216. package/src/provider/sdk/copilot/responses/openai-config.ts +18 -0
  217. package/src/provider/sdk/copilot/responses/openai-error.ts +22 -0
  218. package/src/provider/sdk/copilot/responses/openai-responses-api-types.ts +207 -0
  219. package/src/provider/sdk/copilot/responses/openai-responses-language-model.ts +1732 -0
  220. package/src/provider/sdk/copilot/responses/openai-responses-prepare-tools.ts +177 -0
  221. package/src/provider/sdk/copilot/responses/openai-responses-settings.ts +1 -0
  222. package/src/provider/sdk/copilot/responses/tool/code-interpreter.ts +88 -0
  223. package/src/provider/sdk/copilot/responses/tool/file-search.ts +128 -0
  224. package/src/provider/sdk/copilot/responses/tool/image-generation.ts +115 -0
  225. package/src/provider/sdk/copilot/responses/tool/local-shell.ts +65 -0
  226. package/src/provider/sdk/copilot/responses/tool/web-search-preview.ts +104 -0
  227. package/src/provider/sdk/copilot/responses/tool/web-search.ts +103 -0
  228. package/src/provider/transform.ts +828 -0
  229. package/src/pty/index.ts +250 -0
  230. package/src/question/index.ts +171 -0
  231. package/src/scheduler/index.ts +61 -0
  232. package/src/server/error.ts +36 -0
  233. package/src/server/event.ts +7 -0
  234. package/src/server/mdns.ts +60 -0
  235. package/src/server/routes/config.ts +92 -0
  236. package/src/server/routes/experimental.ts +208 -0
  237. package/src/server/routes/file.ts +197 -0
  238. package/src/server/routes/global.ts +183 -0
  239. package/src/server/routes/mcp.ts +225 -0
  240. package/src/server/routes/permission.ts +68 -0
  241. package/src/server/routes/project.ts +82 -0
  242. package/src/server/routes/provider.ts +165 -0
  243. package/src/server/routes/pty.ts +169 -0
  244. package/src/server/routes/question.ts +98 -0
  245. package/src/server/routes/session.ts +939 -0
  246. package/src/server/routes/tui.ts +379 -0
  247. package/src/server/server.ts +613 -0
  248. package/src/session/compaction.ts +226 -0
  249. package/src/session/index.ts +524 -0
  250. package/src/session/instruction.ts +197 -0
  251. package/src/session/llm.ts +289 -0
  252. package/src/session/message-v2.ts +802 -0
  253. package/src/session/message.ts +189 -0
  254. package/src/session/processor.ts +407 -0
  255. package/src/session/prompt/agent.txt +43 -0
  256. package/src/session/prompt/anthropic-20250930.txt +166 -0
  257. package/src/session/prompt/anthropic.txt +105 -0
  258. package/src/session/prompt/beast.txt +147 -0
  259. package/src/session/prompt/build-switch.txt +5 -0
  260. package/src/session/prompt/codex_header.txt +79 -0
  261. package/src/session/prompt/copilot-gpt-5.txt +143 -0
  262. package/src/session/prompt/gemini.txt +155 -0
  263. package/src/session/prompt/max-steps.txt +16 -0
  264. package/src/session/prompt/plan-reminder-anthropic.txt +67 -0
  265. package/src/session/prompt/plan.txt +26 -0
  266. package/src/session/prompt/qwen.txt +109 -0
  267. package/src/session/prompt/research.txt +81 -0
  268. package/src/session/prompt/trinity.txt +97 -0
  269. package/src/session/prompt.ts +1952 -0
  270. package/src/session/retry.ts +97 -0
  271. package/src/session/revert.ts +121 -0
  272. package/src/session/status.ts +76 -0
  273. package/src/session/summary.ts +217 -0
  274. package/src/session/system.ts +54 -0
  275. package/src/session/todo.ts +37 -0
  276. package/src/share/share-next.ts +200 -0
  277. package/src/share/share.ts +92 -0
  278. package/src/shell/shell.ts +67 -0
  279. package/src/skill/discovery.ts +97 -0
  280. package/src/skill/index.ts +1 -0
  281. package/src/skill/skill.ts +188 -0
  282. package/src/snapshot/index.ts +255 -0
  283. package/src/storage/storage.ts +227 -0
  284. package/src/tool/agent-enter.txt +1 -0
  285. package/src/tool/agent-exit.txt +1 -0
  286. package/src/tool/agent.ts +237 -0
  287. package/src/tool/apply_patch.ts +281 -0
  288. package/src/tool/apply_patch.txt +33 -0
  289. package/src/tool/bash.ts +269 -0
  290. package/src/tool/bash.txt +115 -0
  291. package/src/tool/batch.ts +175 -0
  292. package/src/tool/batch.txt +24 -0
  293. package/src/tool/chat-enter.txt +15 -0
  294. package/src/tool/chat-exit.txt +7 -0
  295. package/src/tool/chat.ts +217 -0
  296. package/src/tool/codesearch.ts +132 -0
  297. package/src/tool/codesearch.txt +12 -0
  298. package/src/tool/edit.ts +655 -0
  299. package/src/tool/edit.txt +10 -0
  300. package/src/tool/external-directory.ts +32 -0
  301. package/src/tool/glob.ts +78 -0
  302. package/src/tool/glob.txt +6 -0
  303. package/src/tool/grep.ts +147 -0
  304. package/src/tool/grep.txt +8 -0
  305. package/src/tool/invalid.ts +17 -0
  306. package/src/tool/ls.ts +121 -0
  307. package/src/tool/ls.txt +1 -0
  308. package/src/tool/lsp.ts +96 -0
  309. package/src/tool/lsp.txt +19 -0
  310. package/src/tool/multiedit.ts +46 -0
  311. package/src/tool/multiedit.txt +41 -0
  312. package/src/tool/plan-enter.txt +14 -0
  313. package/src/tool/plan-exit.txt +13 -0
  314. package/src/tool/plan.ts +130 -0
  315. package/src/tool/question.ts +33 -0
  316. package/src/tool/question.txt +10 -0
  317. package/src/tool/read.ts +211 -0
  318. package/src/tool/read.txt +12 -0
  319. package/src/tool/registry.ts +167 -0
  320. package/src/tool/research-enter.txt +1 -0
  321. package/src/tool/research-exit.txt +1 -0
  322. package/src/tool/research.ts +134 -0
  323. package/src/tool/skill.ts +123 -0
  324. package/src/tool/task.ts +165 -0
  325. package/src/tool/task.txt +60 -0
  326. package/src/tool/todo.ts +53 -0
  327. package/src/tool/todoread.txt +14 -0
  328. package/src/tool/todowrite.txt +167 -0
  329. package/src/tool/tool.ts +89 -0
  330. package/src/tool/truncation.ts +106 -0
  331. package/src/tool/webfetch.ts +186 -0
  332. package/src/tool/webfetch.txt +13 -0
  333. package/src/tool/websearch.ts +150 -0
  334. package/src/tool/websearch.txt +14 -0
  335. package/src/tool/write.ts +85 -0
  336. package/src/tool/write.txt +8 -0
  337. package/src/util/abort.ts +35 -0
  338. package/src/util/archive.ts +16 -0
  339. package/src/util/color.ts +19 -0
  340. package/src/util/context.ts +25 -0
  341. package/src/util/defer.ts +12 -0
  342. package/src/util/eventloop.ts +20 -0
  343. package/src/util/filesystem.ts +93 -0
  344. package/src/util/fn.ts +11 -0
  345. package/src/util/format.ts +20 -0
  346. package/src/util/iife.ts +3 -0
  347. package/src/util/keybind.ts +103 -0
  348. package/src/util/lazy.ts +18 -0
  349. package/src/util/locale.ts +81 -0
  350. package/src/util/lock.ts +98 -0
  351. package/src/util/log.ts +180 -0
  352. package/src/util/proxied.ts +3 -0
  353. package/src/util/queue.ts +32 -0
  354. package/src/util/rpc.ts +66 -0
  355. package/src/util/scrap.ts +10 -0
  356. package/src/util/signal.ts +12 -0
  357. package/src/util/timeout.ts +14 -0
  358. package/src/util/token.ts +7 -0
  359. package/src/util/wildcard.ts +56 -0
  360. package/src/worktree/index.ts +574 -0
  361. package/sst-env.d.ts +9 -0
  362. package/test/acp/agent-interface.test.ts +51 -0
  363. package/test/acp/event-subscription.test.ts +436 -0
  364. package/test/agent/agent.test.ts +675 -0
  365. package/test/bun.test.ts +53 -0
  366. package/test/cli/github-action.test.ts +161 -0
  367. package/test/cli/github-remote.test.ts +80 -0
  368. package/test/cli/import.test.ts +38 -0
  369. package/test/cli/tui/transcript.test.ts +322 -0
  370. package/test/config/agent-color.test.ts +71 -0
  371. package/test/config/config.test.ts +1802 -0
  372. package/test/config/fixtures/empty-frontmatter.md +4 -0
  373. package/test/config/fixtures/frontmatter.md +28 -0
  374. package/test/config/fixtures/markdown-header.md +11 -0
  375. package/test/config/fixtures/no-frontmatter.md +1 -0
  376. package/test/config/fixtures/weird-model-id.md +13 -0
  377. package/test/config/markdown.test.ts +228 -0
  378. package/test/file/ignore.test.ts +10 -0
  379. package/test/file/path-traversal.test.ts +198 -0
  380. package/test/file/ripgrep.test.ts +39 -0
  381. package/test/fixture/fixture.ts +45 -0
  382. package/test/fixture/lsp/fake-lsp-server.js +77 -0
  383. package/test/ide/ide.test.ts +82 -0
  384. package/test/keybind.test.ts +421 -0
  385. package/test/lsp/client.test.ts +95 -0
  386. package/test/mcp/headers.test.ts +153 -0
  387. package/test/mcp/oauth-browser.test.ts +249 -0
  388. package/test/memory/abort-leak.test.ts +136 -0
  389. package/test/patch/patch.test.ts +348 -0
  390. package/test/permission/arity.test.ts +33 -0
  391. package/test/permission/next.test.ts +690 -0
  392. package/test/permission-task.test.ts +319 -0
  393. package/test/plugin/auth-override.test.ts +44 -0
  394. package/test/plugin/codex.test.ts +123 -0
  395. package/test/preload.ts +63 -0
  396. package/test/project/project.test.ts +120 -0
  397. package/test/provider/amazon-bedrock.test.ts +445 -0
  398. package/test/provider/copilot/convert-to-copilot-messages.test.ts +523 -0
  399. package/test/provider/copilot/copilot-chat-model.test.ts +592 -0
  400. package/test/provider/gitlab-duo.test.ts +262 -0
  401. package/test/provider/provider.test.ts +2129 -0
  402. package/test/provider/transform.test.ts +2022 -0
  403. package/test/question/question.test.ts +300 -0
  404. package/test/scheduler.test.ts +73 -0
  405. package/test/server/session-list.test.ts +39 -0
  406. package/test/server/session-select.test.ts +78 -0
  407. package/test/session/compaction.test.ts +293 -0
  408. package/test/session/instruction.test.ts +170 -0
  409. package/test/session/llm.test.ts +691 -0
  410. package/test/session/message-v2.test.ts +786 -0
  411. package/test/session/prompt-missing-file.test.ts +53 -0
  412. package/test/session/prompt-special-chars.test.ts +56 -0
  413. package/test/session/prompt-variant.test.ts +60 -0
  414. package/test/session/retry.test.ts +179 -0
  415. package/test/session/revert-compact.test.ts +285 -0
  416. package/test/session/session.test.ts +71 -0
  417. package/test/skill/discovery.test.ts +60 -0
  418. package/test/skill/skill.test.ts +388 -0
  419. package/test/snapshot/snapshot.test.ts +1040 -0
  420. package/test/tool/__snapshots__/tool.test.ts.snap +9 -0
  421. package/test/tool/apply_patch.test.ts +559 -0
  422. package/test/tool/bash.test.ts +399 -0
  423. package/test/tool/external-directory.test.ts +127 -0
  424. package/test/tool/fixtures/large-image.png +0 -0
  425. package/test/tool/fixtures/models-api.json +38413 -0
  426. package/test/tool/grep.test.ts +110 -0
  427. package/test/tool/question.test.ts +107 -0
  428. package/test/tool/read.test.ts +358 -0
  429. package/test/tool/registry.test.ts +122 -0
  430. package/test/tool/skill.test.ts +112 -0
  431. package/test/tool/truncation.test.ts +159 -0
  432. package/test/util/filesystem.test.ts +39 -0
  433. package/test/util/format.test.ts +59 -0
  434. package/test/util/iife.test.ts +36 -0
  435. package/test/util/lazy.test.ts +50 -0
  436. package/test/util/lock.test.ts +72 -0
  437. package/test/util/timeout.test.ts +21 -0
  438. package/test/util/wildcard.test.ts +75 -0
  439. package/tsconfig.json +16 -0
@@ -0,0 +1,1802 @@
1
+ import { test, expect, describe, mock, afterEach } from "bun:test"
2
+ import { Config } from "../../src/config/config"
3
+ import { Instance } from "../../src/project/instance"
4
+ import { Auth } from "../../src/auth"
5
+ import { tmpdir } from "../fixture/fixture"
6
+ import path from "path"
7
+ import fs from "fs/promises"
8
+ import { pathToFileURL } from "url"
9
+ import { Global } from "../../src/global"
10
+
11
+ // Get managed config directory from environment (set in preload.ts)
12
+ const managedConfigDir = process.env.OPENCODE_TEST_MANAGED_CONFIG_DIR!
13
+
14
+ afterEach(async () => {
15
+ await fs.rm(managedConfigDir, { force: true, recursive: true }).catch(() => {})
16
+ })
17
+
18
+ async function writeManagedSettings(settings: object, filename = "opencode.json") {
19
+ await fs.mkdir(managedConfigDir, { recursive: true })
20
+ await Bun.write(path.join(managedConfigDir, filename), JSON.stringify(settings))
21
+ }
22
+
23
+ async function writeConfig(dir: string, config: object, name = "opencode.json") {
24
+ await Bun.write(path.join(dir, name), JSON.stringify(config))
25
+ }
26
+
27
+ test("loads config with defaults when no files exist", async () => {
28
+ await using tmp = await tmpdir()
29
+ await Instance.provide({
30
+ directory: tmp.path,
31
+ fn: async () => {
32
+ const config = await Config.get()
33
+ expect(config.username).toBeDefined()
34
+ },
35
+ })
36
+ })
37
+
38
+ test("loads JSON config file", async () => {
39
+ await using tmp = await tmpdir({
40
+ init: async (dir) => {
41
+ await writeConfig(dir, {
42
+ $schema: "https://opencode.ai/config.json",
43
+ model: "test/model",
44
+ username: "testuser",
45
+ })
46
+ },
47
+ })
48
+ await Instance.provide({
49
+ directory: tmp.path,
50
+ fn: async () => {
51
+ const config = await Config.get()
52
+ expect(config.model).toBe("test/model")
53
+ expect(config.username).toBe("testuser")
54
+ },
55
+ })
56
+ })
57
+
58
+ test("loads JSONC config file", async () => {
59
+ await using tmp = await tmpdir({
60
+ init: async (dir) => {
61
+ await Bun.write(
62
+ path.join(dir, "opencode.jsonc"),
63
+ `{
64
+ // This is a comment
65
+ "$schema": "https://opencode.ai/config.json",
66
+ "model": "test/model",
67
+ "username": "testuser"
68
+ }`,
69
+ )
70
+ },
71
+ })
72
+ await Instance.provide({
73
+ directory: tmp.path,
74
+ fn: async () => {
75
+ const config = await Config.get()
76
+ expect(config.model).toBe("test/model")
77
+ expect(config.username).toBe("testuser")
78
+ },
79
+ })
80
+ })
81
+
82
+ test("merges multiple config files with correct precedence", async () => {
83
+ await using tmp = await tmpdir({
84
+ init: async (dir) => {
85
+ await writeConfig(
86
+ dir,
87
+ {
88
+ $schema: "https://opencode.ai/config.json",
89
+ model: "base",
90
+ username: "base",
91
+ },
92
+ "opencode.jsonc",
93
+ )
94
+ await writeConfig(dir, {
95
+ $schema: "https://opencode.ai/config.json",
96
+ model: "override",
97
+ })
98
+ },
99
+ })
100
+ await Instance.provide({
101
+ directory: tmp.path,
102
+ fn: async () => {
103
+ const config = await Config.get()
104
+ expect(config.model).toBe("override")
105
+ expect(config.username).toBe("base")
106
+ },
107
+ })
108
+ })
109
+
110
+ test("handles environment variable substitution", async () => {
111
+ const originalEnv = process.env["TEST_VAR"]
112
+ process.env["TEST_VAR"] = "test_theme"
113
+
114
+ try {
115
+ await using tmp = await tmpdir({
116
+ init: async (dir) => {
117
+ await writeConfig(dir, {
118
+ $schema: "https://opencode.ai/config.json",
119
+ theme: "{env:TEST_VAR}",
120
+ })
121
+ },
122
+ })
123
+ await Instance.provide({
124
+ directory: tmp.path,
125
+ fn: async () => {
126
+ const config = await Config.get()
127
+ expect(config.theme).toBe("test_theme")
128
+ },
129
+ })
130
+ } finally {
131
+ if (originalEnv !== undefined) {
132
+ process.env["TEST_VAR"] = originalEnv
133
+ } else {
134
+ delete process.env["TEST_VAR"]
135
+ }
136
+ }
137
+ })
138
+
139
+ test("preserves env variables when adding $schema to config", async () => {
140
+ const originalEnv = process.env["PRESERVE_VAR"]
141
+ process.env["PRESERVE_VAR"] = "secret_value"
142
+
143
+ try {
144
+ await using tmp = await tmpdir({
145
+ init: async (dir) => {
146
+ // Config without $schema - should trigger auto-add
147
+ await Bun.write(
148
+ path.join(dir, "opencode.json"),
149
+ JSON.stringify({
150
+ theme: "{env:PRESERVE_VAR}",
151
+ }),
152
+ )
153
+ },
154
+ })
155
+ await Instance.provide({
156
+ directory: tmp.path,
157
+ fn: async () => {
158
+ const config = await Config.get()
159
+ expect(config.theme).toBe("secret_value")
160
+
161
+ // Read the file to verify the env variable was preserved
162
+ const content = await Bun.file(path.join(tmp.path, "opencode.json")).text()
163
+ expect(content).toContain("{env:PRESERVE_VAR}")
164
+ expect(content).not.toContain("secret_value")
165
+ expect(content).toContain("$schema")
166
+ },
167
+ })
168
+ } finally {
169
+ if (originalEnv !== undefined) {
170
+ process.env["PRESERVE_VAR"] = originalEnv
171
+ } else {
172
+ delete process.env["PRESERVE_VAR"]
173
+ }
174
+ }
175
+ })
176
+
177
+ test("handles file inclusion substitution", async () => {
178
+ await using tmp = await tmpdir({
179
+ init: async (dir) => {
180
+ await Bun.write(path.join(dir, "included.txt"), "test_theme")
181
+ await writeConfig(dir, {
182
+ $schema: "https://opencode.ai/config.json",
183
+ theme: "{file:included.txt}",
184
+ })
185
+ },
186
+ })
187
+ await Instance.provide({
188
+ directory: tmp.path,
189
+ fn: async () => {
190
+ const config = await Config.get()
191
+ expect(config.theme).toBe("test_theme")
192
+ },
193
+ })
194
+ })
195
+
196
+ test("handles file inclusion with replacement tokens", async () => {
197
+ await using tmp = await tmpdir({
198
+ init: async (dir) => {
199
+ await Bun.write(path.join(dir, "included.md"), "const out = await Bun.$`echo hi`")
200
+ await writeConfig(dir, {
201
+ $schema: "https://opencode.ai/config.json",
202
+ theme: "{file:included.md}",
203
+ })
204
+ },
205
+ })
206
+ await Instance.provide({
207
+ directory: tmp.path,
208
+ fn: async () => {
209
+ const config = await Config.get()
210
+ expect(config.theme).toBe("const out = await Bun.$`echo hi`")
211
+ },
212
+ })
213
+ })
214
+
215
+ test("validates config schema and throws on invalid fields", async () => {
216
+ await using tmp = await tmpdir({
217
+ init: async (dir) => {
218
+ await writeConfig(dir, {
219
+ $schema: "https://opencode.ai/config.json",
220
+ invalid_field: "should cause error",
221
+ })
222
+ },
223
+ })
224
+ await Instance.provide({
225
+ directory: tmp.path,
226
+ fn: async () => {
227
+ // Strict schema should throw an error for invalid fields
228
+ await expect(Config.get()).rejects.toThrow()
229
+ },
230
+ })
231
+ })
232
+
233
+ test("throws error for invalid JSON", async () => {
234
+ await using tmp = await tmpdir({
235
+ init: async (dir) => {
236
+ await Bun.write(path.join(dir, "opencode.json"), "{ invalid json }")
237
+ },
238
+ })
239
+ await Instance.provide({
240
+ directory: tmp.path,
241
+ fn: async () => {
242
+ await expect(Config.get()).rejects.toThrow()
243
+ },
244
+ })
245
+ })
246
+
247
+ test("handles agent configuration", async () => {
248
+ await using tmp = await tmpdir({
249
+ init: async (dir) => {
250
+ await writeConfig(dir, {
251
+ $schema: "https://opencode.ai/config.json",
252
+ agent: {
253
+ test_agent: {
254
+ model: "test/model",
255
+ temperature: 0.7,
256
+ description: "test agent",
257
+ },
258
+ },
259
+ })
260
+ },
261
+ })
262
+ await Instance.provide({
263
+ directory: tmp.path,
264
+ fn: async () => {
265
+ const config = await Config.get()
266
+ expect(config.agent?.["test_agent"]).toEqual(
267
+ expect.objectContaining({
268
+ model: "test/model",
269
+ temperature: 0.7,
270
+ description: "test agent",
271
+ }),
272
+ )
273
+ },
274
+ })
275
+ })
276
+
277
+ test("treats agent variant as model-scoped setting (not provider option)", async () => {
278
+ await using tmp = await tmpdir({
279
+ init: async (dir) => {
280
+ await writeConfig(dir, {
281
+ $schema: "https://opencode.ai/config.json",
282
+ agent: {
283
+ test_agent: {
284
+ model: "openai/gpt-5.2",
285
+ variant: "xhigh",
286
+ max_tokens: 123,
287
+ },
288
+ },
289
+ })
290
+ },
291
+ })
292
+
293
+ await Instance.provide({
294
+ directory: tmp.path,
295
+ fn: async () => {
296
+ const config = await Config.get()
297
+ const agent = config.agent?.["test_agent"]
298
+
299
+ expect(agent?.variant).toBe("xhigh")
300
+ expect(agent?.options).toMatchObject({
301
+ max_tokens: 123,
302
+ })
303
+ expect(agent?.options).not.toHaveProperty("variant")
304
+ },
305
+ })
306
+ })
307
+
308
+ test("handles command configuration", async () => {
309
+ await using tmp = await tmpdir({
310
+ init: async (dir) => {
311
+ await writeConfig(dir, {
312
+ $schema: "https://opencode.ai/config.json",
313
+ command: {
314
+ test_command: {
315
+ template: "test template",
316
+ description: "test command",
317
+ agent: "test_agent",
318
+ },
319
+ },
320
+ })
321
+ },
322
+ })
323
+ await Instance.provide({
324
+ directory: tmp.path,
325
+ fn: async () => {
326
+ const config = await Config.get()
327
+ expect(config.command?.["test_command"]).toEqual({
328
+ template: "test template",
329
+ description: "test command",
330
+ agent: "test_agent",
331
+ })
332
+ },
333
+ })
334
+ })
335
+
336
+ test("migrates autoshare to share field", async () => {
337
+ await using tmp = await tmpdir({
338
+ init: async (dir) => {
339
+ await Bun.write(
340
+ path.join(dir, "opencode.json"),
341
+ JSON.stringify({
342
+ $schema: "https://opencode.ai/config.json",
343
+ autoshare: true,
344
+ }),
345
+ )
346
+ },
347
+ })
348
+ await Instance.provide({
349
+ directory: tmp.path,
350
+ fn: async () => {
351
+ const config = await Config.get()
352
+ expect(config.share).toBe("auto")
353
+ expect(config.autoshare).toBe(true)
354
+ },
355
+ })
356
+ })
357
+
358
+ test("migrates mode field to agent field", async () => {
359
+ await using tmp = await tmpdir({
360
+ init: async (dir) => {
361
+ await Bun.write(
362
+ path.join(dir, "opencode.json"),
363
+ JSON.stringify({
364
+ $schema: "https://opencode.ai/config.json",
365
+ mode: {
366
+ test_mode: {
367
+ model: "test/model",
368
+ temperature: 0.5,
369
+ },
370
+ },
371
+ }),
372
+ )
373
+ },
374
+ })
375
+ await Instance.provide({
376
+ directory: tmp.path,
377
+ fn: async () => {
378
+ const config = await Config.get()
379
+ expect(config.agent?.["test_mode"]).toEqual({
380
+ model: "test/model",
381
+ temperature: 0.5,
382
+ mode: "primary",
383
+ options: {},
384
+ permission: {},
385
+ })
386
+ },
387
+ })
388
+ })
389
+
390
+ test("loads config from .opencode directory", async () => {
391
+ await using tmp = await tmpdir({
392
+ init: async (dir) => {
393
+ const opencodeDir = path.join(dir, ".opencode")
394
+ await fs.mkdir(opencodeDir, { recursive: true })
395
+ const agentDir = path.join(opencodeDir, "agent")
396
+ await fs.mkdir(agentDir, { recursive: true })
397
+
398
+ await Bun.write(
399
+ path.join(agentDir, "test.md"),
400
+ `---
401
+ model: test/model
402
+ ---
403
+ Test agent prompt`,
404
+ )
405
+ },
406
+ })
407
+ await Instance.provide({
408
+ directory: tmp.path,
409
+ fn: async () => {
410
+ const config = await Config.get()
411
+ expect(config.agent?.["test"]).toEqual(
412
+ expect.objectContaining({
413
+ name: "test",
414
+ model: "test/model",
415
+ prompt: "Test agent prompt",
416
+ }),
417
+ )
418
+ },
419
+ })
420
+ })
421
+
422
+ test("loads agents from .opencode/agents (plural)", async () => {
423
+ await using tmp = await tmpdir({
424
+ init: async (dir) => {
425
+ const opencodeDir = path.join(dir, ".opencode")
426
+ await fs.mkdir(opencodeDir, { recursive: true })
427
+
428
+ const agentsDir = path.join(opencodeDir, "agents")
429
+ await fs.mkdir(path.join(agentsDir, "nested"), { recursive: true })
430
+
431
+ await Bun.write(
432
+ path.join(agentsDir, "helper.md"),
433
+ `---
434
+ model: test/model
435
+ mode: subagent
436
+ ---
437
+ Helper agent prompt`,
438
+ )
439
+
440
+ await Bun.write(
441
+ path.join(agentsDir, "nested", "child.md"),
442
+ `---
443
+ model: test/model
444
+ mode: subagent
445
+ ---
446
+ Nested agent prompt`,
447
+ )
448
+ },
449
+ })
450
+
451
+ await Instance.provide({
452
+ directory: tmp.path,
453
+ fn: async () => {
454
+ const config = await Config.get()
455
+
456
+ expect(config.agent?.["helper"]).toMatchObject({
457
+ name: "helper",
458
+ model: "test/model",
459
+ mode: "subagent",
460
+ prompt: "Helper agent prompt",
461
+ })
462
+
463
+ expect(config.agent?.["nested/child"]).toMatchObject({
464
+ name: "nested/child",
465
+ model: "test/model",
466
+ mode: "subagent",
467
+ prompt: "Nested agent prompt",
468
+ })
469
+ },
470
+ })
471
+ })
472
+
473
+ test("loads commands from .opencode/command (singular)", async () => {
474
+ await using tmp = await tmpdir({
475
+ init: async (dir) => {
476
+ const opencodeDir = path.join(dir, ".opencode")
477
+ await fs.mkdir(opencodeDir, { recursive: true })
478
+
479
+ const commandDir = path.join(opencodeDir, "command")
480
+ await fs.mkdir(path.join(commandDir, "nested"), { recursive: true })
481
+
482
+ await Bun.write(
483
+ path.join(commandDir, "hello.md"),
484
+ `---
485
+ description: Test command
486
+ ---
487
+ Hello from singular command`,
488
+ )
489
+
490
+ await Bun.write(
491
+ path.join(commandDir, "nested", "child.md"),
492
+ `---
493
+ description: Nested command
494
+ ---
495
+ Nested command template`,
496
+ )
497
+ },
498
+ })
499
+
500
+ await Instance.provide({
501
+ directory: tmp.path,
502
+ fn: async () => {
503
+ const config = await Config.get()
504
+
505
+ expect(config.command?.["hello"]).toEqual({
506
+ description: "Test command",
507
+ template: "Hello from singular command",
508
+ })
509
+
510
+ expect(config.command?.["nested/child"]).toEqual({
511
+ description: "Nested command",
512
+ template: "Nested command template",
513
+ })
514
+ },
515
+ })
516
+ })
517
+
518
+ test("loads commands from .opencode/commands (plural)", async () => {
519
+ await using tmp = await tmpdir({
520
+ init: async (dir) => {
521
+ const opencodeDir = path.join(dir, ".opencode")
522
+ await fs.mkdir(opencodeDir, { recursive: true })
523
+
524
+ const commandsDir = path.join(opencodeDir, "commands")
525
+ await fs.mkdir(path.join(commandsDir, "nested"), { recursive: true })
526
+
527
+ await Bun.write(
528
+ path.join(commandsDir, "hello.md"),
529
+ `---
530
+ description: Test command
531
+ ---
532
+ Hello from plural commands`,
533
+ )
534
+
535
+ await Bun.write(
536
+ path.join(commandsDir, "nested", "child.md"),
537
+ `---
538
+ description: Nested command
539
+ ---
540
+ Nested command template`,
541
+ )
542
+ },
543
+ })
544
+
545
+ await Instance.provide({
546
+ directory: tmp.path,
547
+ fn: async () => {
548
+ const config = await Config.get()
549
+
550
+ expect(config.command?.["hello"]).toEqual({
551
+ description: "Test command",
552
+ template: "Hello from plural commands",
553
+ })
554
+
555
+ expect(config.command?.["nested/child"]).toEqual({
556
+ description: "Nested command",
557
+ template: "Nested command template",
558
+ })
559
+ },
560
+ })
561
+ })
562
+
563
+ test("updates config and writes to file", async () => {
564
+ await using tmp = await tmpdir()
565
+ await Instance.provide({
566
+ directory: tmp.path,
567
+ fn: async () => {
568
+ const newConfig = { model: "updated/model" }
569
+ await Config.update(newConfig as any)
570
+
571
+ const writtenConfig = JSON.parse(await Bun.file(path.join(tmp.path, "config.json")).text())
572
+ expect(writtenConfig.model).toBe("updated/model")
573
+ },
574
+ })
575
+ })
576
+
577
+ test("gets config directories", async () => {
578
+ await using tmp = await tmpdir()
579
+ await Instance.provide({
580
+ directory: tmp.path,
581
+ fn: async () => {
582
+ const dirs = await Config.directories()
583
+ expect(dirs.length).toBeGreaterThanOrEqual(1)
584
+ },
585
+ })
586
+ })
587
+
588
+ test("does not try to install dependencies in read-only OPENCODE_CONFIG_DIR", async () => {
589
+ if (process.platform === "win32") return
590
+
591
+ await using tmp = await tmpdir<string>({
592
+ init: async (dir) => {
593
+ const ro = path.join(dir, "readonly")
594
+ await fs.mkdir(ro, { recursive: true })
595
+ await fs.chmod(ro, 0o555)
596
+ return ro
597
+ },
598
+ dispose: async (dir) => {
599
+ const ro = path.join(dir, "readonly")
600
+ await fs.chmod(ro, 0o755).catch(() => {})
601
+ return ro
602
+ },
603
+ })
604
+
605
+ const prev = process.env.OPENCODE_CONFIG_DIR
606
+ process.env.OPENCODE_CONFIG_DIR = tmp.extra
607
+
608
+ try {
609
+ await Instance.provide({
610
+ directory: tmp.path,
611
+ fn: async () => {
612
+ await Config.get()
613
+ },
614
+ })
615
+ } finally {
616
+ if (prev === undefined) delete process.env.OPENCODE_CONFIG_DIR
617
+ else process.env.OPENCODE_CONFIG_DIR = prev
618
+ }
619
+ })
620
+
621
+ test("installs dependencies in writable OPENCODE_CONFIG_DIR", async () => {
622
+ await using tmp = await tmpdir<string>({
623
+ init: async (dir) => {
624
+ const cfg = path.join(dir, "configdir")
625
+ await fs.mkdir(cfg, { recursive: true })
626
+ return cfg
627
+ },
628
+ })
629
+
630
+ const prev = process.env.OPENCODE_CONFIG_DIR
631
+ process.env.OPENCODE_CONFIG_DIR = tmp.extra
632
+
633
+ try {
634
+ await Instance.provide({
635
+ directory: tmp.path,
636
+ fn: async () => {
637
+ await Config.get()
638
+ await Config.waitForDependencies()
639
+ },
640
+ })
641
+
642
+ expect(await Bun.file(path.join(tmp.extra, "package.json")).exists()).toBe(true)
643
+ expect(await Bun.file(path.join(tmp.extra, ".gitignore")).exists()).toBe(true)
644
+ } finally {
645
+ if (prev === undefined) delete process.env.OPENCODE_CONFIG_DIR
646
+ else process.env.OPENCODE_CONFIG_DIR = prev
647
+ }
648
+ })
649
+
650
+ test("resolves scoped npm plugins in config", async () => {
651
+ await using tmp = await tmpdir({
652
+ init: async (dir) => {
653
+ const pluginDir = path.join(dir, "node_modules", "@scope", "plugin")
654
+ await fs.mkdir(pluginDir, { recursive: true })
655
+
656
+ await Bun.write(
657
+ path.join(dir, "package.json"),
658
+ JSON.stringify({ name: "config-fixture", version: "1.0.0", type: "module" }, null, 2),
659
+ )
660
+
661
+ await Bun.write(
662
+ path.join(pluginDir, "package.json"),
663
+ JSON.stringify(
664
+ {
665
+ name: "@scope/plugin",
666
+ version: "1.0.0",
667
+ type: "module",
668
+ main: "./index.js",
669
+ },
670
+ null,
671
+ 2,
672
+ ),
673
+ )
674
+
675
+ await Bun.write(path.join(pluginDir, "index.js"), "export default {}\n")
676
+
677
+ await Bun.write(
678
+ path.join(dir, "opencode.json"),
679
+ JSON.stringify({ $schema: "https://opencode.ai/config.json", plugin: ["@scope/plugin"] }, null, 2),
680
+ )
681
+ },
682
+ })
683
+
684
+ await Instance.provide({
685
+ directory: tmp.path,
686
+ fn: async () => {
687
+ const config = await Config.get()
688
+ const pluginEntries = config.plugin ?? []
689
+
690
+ const baseUrl = pathToFileURL(path.join(tmp.path, "opencode.json")).href
691
+ const expected = import.meta.resolve("@scope/plugin", baseUrl)
692
+
693
+ expect(pluginEntries.includes(expected)).toBe(true)
694
+
695
+ const scopedEntry = pluginEntries.find((entry) => entry === expected)
696
+ expect(scopedEntry).toBeDefined()
697
+ expect(scopedEntry?.includes("/node_modules/@scope/plugin/")).toBe(true)
698
+ },
699
+ })
700
+ })
701
+
702
+ test("merges plugin arrays from global and local configs", async () => {
703
+ await using tmp = await tmpdir({
704
+ init: async (dir) => {
705
+ // Create a nested project structure with local .opencode config
706
+ const projectDir = path.join(dir, "project")
707
+ const opencodeDir = path.join(projectDir, ".opencode")
708
+ await fs.mkdir(opencodeDir, { recursive: true })
709
+
710
+ // Global config with plugins
711
+ await Bun.write(
712
+ path.join(dir, "opencode.json"),
713
+ JSON.stringify({
714
+ $schema: "https://opencode.ai/config.json",
715
+ plugin: ["global-plugin-1", "global-plugin-2"],
716
+ }),
717
+ )
718
+
719
+ // Local .opencode config with different plugins
720
+ await Bun.write(
721
+ path.join(opencodeDir, "opencode.json"),
722
+ JSON.stringify({
723
+ $schema: "https://opencode.ai/config.json",
724
+ plugin: ["local-plugin-1"],
725
+ }),
726
+ )
727
+ },
728
+ })
729
+
730
+ await Instance.provide({
731
+ directory: path.join(tmp.path, "project"),
732
+ fn: async () => {
733
+ const config = await Config.get()
734
+ const plugins = config.plugin ?? []
735
+
736
+ // Should contain both global and local plugins
737
+ expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
738
+ expect(plugins.some((p) => p.includes("global-plugin-2"))).toBe(true)
739
+ expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
740
+
741
+ // Should have all 3 plugins (not replaced, but merged)
742
+ const pluginNames = plugins.filter((p) => p.includes("global-plugin") || p.includes("local-plugin"))
743
+ expect(pluginNames.length).toBeGreaterThanOrEqual(3)
744
+ },
745
+ })
746
+ })
747
+
748
+ test("does not error when only custom agent is a subagent", async () => {
749
+ await using tmp = await tmpdir({
750
+ init: async (dir) => {
751
+ const opencodeDir = path.join(dir, ".opencode")
752
+ await fs.mkdir(opencodeDir, { recursive: true })
753
+ const agentDir = path.join(opencodeDir, "agent")
754
+ await fs.mkdir(agentDir, { recursive: true })
755
+
756
+ await Bun.write(
757
+ path.join(agentDir, "helper.md"),
758
+ `---
759
+ model: test/model
760
+ mode: subagent
761
+ ---
762
+ Helper subagent prompt`,
763
+ )
764
+ },
765
+ })
766
+ await Instance.provide({
767
+ directory: tmp.path,
768
+ fn: async () => {
769
+ const config = await Config.get()
770
+ expect(config.agent?.["helper"]).toMatchObject({
771
+ name: "helper",
772
+ model: "test/model",
773
+ mode: "subagent",
774
+ prompt: "Helper subagent prompt",
775
+ })
776
+ },
777
+ })
778
+ })
779
+
780
+ test("merges instructions arrays from global and local configs", async () => {
781
+ await using tmp = await tmpdir({
782
+ init: async (dir) => {
783
+ const projectDir = path.join(dir, "project")
784
+ const opencodeDir = path.join(projectDir, ".opencode")
785
+ await fs.mkdir(opencodeDir, { recursive: true })
786
+
787
+ await Bun.write(
788
+ path.join(dir, "opencode.json"),
789
+ JSON.stringify({
790
+ $schema: "https://opencode.ai/config.json",
791
+ instructions: ["global-instructions.md", "shared-rules.md"],
792
+ }),
793
+ )
794
+
795
+ await Bun.write(
796
+ path.join(opencodeDir, "opencode.json"),
797
+ JSON.stringify({
798
+ $schema: "https://opencode.ai/config.json",
799
+ instructions: ["local-instructions.md"],
800
+ }),
801
+ )
802
+ },
803
+ })
804
+
805
+ await Instance.provide({
806
+ directory: path.join(tmp.path, "project"),
807
+ fn: async () => {
808
+ const config = await Config.get()
809
+ const instructions = config.instructions ?? []
810
+
811
+ expect(instructions).toContain("global-instructions.md")
812
+ expect(instructions).toContain("shared-rules.md")
813
+ expect(instructions).toContain("local-instructions.md")
814
+ expect(instructions.length).toBe(3)
815
+ },
816
+ })
817
+ })
818
+
819
+ test("deduplicates duplicate instructions from global and local configs", async () => {
820
+ await using tmp = await tmpdir({
821
+ init: async (dir) => {
822
+ const projectDir = path.join(dir, "project")
823
+ const opencodeDir = path.join(projectDir, ".opencode")
824
+ await fs.mkdir(opencodeDir, { recursive: true })
825
+
826
+ await Bun.write(
827
+ path.join(dir, "opencode.json"),
828
+ JSON.stringify({
829
+ $schema: "https://opencode.ai/config.json",
830
+ instructions: ["duplicate.md", "global-only.md"],
831
+ }),
832
+ )
833
+
834
+ await Bun.write(
835
+ path.join(opencodeDir, "opencode.json"),
836
+ JSON.stringify({
837
+ $schema: "https://opencode.ai/config.json",
838
+ instructions: ["duplicate.md", "local-only.md"],
839
+ }),
840
+ )
841
+ },
842
+ })
843
+
844
+ await Instance.provide({
845
+ directory: path.join(tmp.path, "project"),
846
+ fn: async () => {
847
+ const config = await Config.get()
848
+ const instructions = config.instructions ?? []
849
+
850
+ expect(instructions).toContain("global-only.md")
851
+ expect(instructions).toContain("local-only.md")
852
+ expect(instructions).toContain("duplicate.md")
853
+
854
+ const duplicates = instructions.filter((i) => i === "duplicate.md")
855
+ expect(duplicates.length).toBe(1)
856
+ expect(instructions.length).toBe(3)
857
+ },
858
+ })
859
+ })
860
+
861
+ test("deduplicates duplicate plugins from global and local configs", async () => {
862
+ await using tmp = await tmpdir({
863
+ init: async (dir) => {
864
+ // Create a nested project structure with local .opencode config
865
+ const projectDir = path.join(dir, "project")
866
+ const opencodeDir = path.join(projectDir, ".opencode")
867
+ await fs.mkdir(opencodeDir, { recursive: true })
868
+
869
+ // Global config with plugins
870
+ await Bun.write(
871
+ path.join(dir, "opencode.json"),
872
+ JSON.stringify({
873
+ $schema: "https://opencode.ai/config.json",
874
+ plugin: ["duplicate-plugin", "global-plugin-1"],
875
+ }),
876
+ )
877
+
878
+ // Local .opencode config with some overlapping plugins
879
+ await Bun.write(
880
+ path.join(opencodeDir, "opencode.json"),
881
+ JSON.stringify({
882
+ $schema: "https://opencode.ai/config.json",
883
+ plugin: ["duplicate-plugin", "local-plugin-1"],
884
+ }),
885
+ )
886
+ },
887
+ })
888
+
889
+ await Instance.provide({
890
+ directory: path.join(tmp.path, "project"),
891
+ fn: async () => {
892
+ const config = await Config.get()
893
+ const plugins = config.plugin ?? []
894
+
895
+ // Should contain all unique plugins
896
+ expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
897
+ expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
898
+ expect(plugins.some((p) => p.includes("duplicate-plugin"))).toBe(true)
899
+
900
+ // Should deduplicate the duplicate plugin
901
+ const duplicatePlugins = plugins.filter((p) => p.includes("duplicate-plugin"))
902
+ expect(duplicatePlugins.length).toBe(1)
903
+
904
+ // Should have exactly 3 unique plugins
905
+ const pluginNames = plugins.filter(
906
+ (p) => p.includes("global-plugin") || p.includes("local-plugin") || p.includes("duplicate-plugin"),
907
+ )
908
+ expect(pluginNames.length).toBe(3)
909
+ },
910
+ })
911
+ })
912
+
913
+ // Legacy tools migration tests
914
+
915
+ test("migrates legacy tools config to permissions - allow", async () => {
916
+ await using tmp = await tmpdir({
917
+ init: async (dir) => {
918
+ await Bun.write(
919
+ path.join(dir, "opencode.json"),
920
+ JSON.stringify({
921
+ $schema: "https://opencode.ai/config.json",
922
+ agent: {
923
+ test: {
924
+ tools: {
925
+ bash: true,
926
+ read: true,
927
+ },
928
+ },
929
+ },
930
+ }),
931
+ )
932
+ },
933
+ })
934
+ await Instance.provide({
935
+ directory: tmp.path,
936
+ fn: async () => {
937
+ const config = await Config.get()
938
+ expect(config.agent?.["test"]?.permission).toEqual({
939
+ bash: "allow",
940
+ read: "allow",
941
+ })
942
+ },
943
+ })
944
+ })
945
+
946
+ test("migrates legacy tools config to permissions - deny", async () => {
947
+ await using tmp = await tmpdir({
948
+ init: async (dir) => {
949
+ await Bun.write(
950
+ path.join(dir, "opencode.json"),
951
+ JSON.stringify({
952
+ $schema: "https://opencode.ai/config.json",
953
+ agent: {
954
+ test: {
955
+ tools: {
956
+ bash: false,
957
+ webfetch: false,
958
+ },
959
+ },
960
+ },
961
+ }),
962
+ )
963
+ },
964
+ })
965
+ await Instance.provide({
966
+ directory: tmp.path,
967
+ fn: async () => {
968
+ const config = await Config.get()
969
+ expect(config.agent?.["test"]?.permission).toEqual({
970
+ bash: "deny",
971
+ webfetch: "deny",
972
+ })
973
+ },
974
+ })
975
+ })
976
+
977
+ test("migrates legacy write tool to edit permission", async () => {
978
+ await using tmp = await tmpdir({
979
+ init: async (dir) => {
980
+ await Bun.write(
981
+ path.join(dir, "opencode.json"),
982
+ JSON.stringify({
983
+ $schema: "https://opencode.ai/config.json",
984
+ agent: {
985
+ test: {
986
+ tools: {
987
+ write: true,
988
+ },
989
+ },
990
+ },
991
+ }),
992
+ )
993
+ },
994
+ })
995
+ await Instance.provide({
996
+ directory: tmp.path,
997
+ fn: async () => {
998
+ const config = await Config.get()
999
+ expect(config.agent?.["test"]?.permission).toEqual({
1000
+ edit: "allow",
1001
+ })
1002
+ },
1003
+ })
1004
+ })
1005
+
1006
+ // Managed settings tests
1007
+ // Note: preload.ts sets OPENCODE_TEST_MANAGED_CONFIG which Global.Path.managedConfig uses
1008
+
1009
+ test("managed settings override user settings", async () => {
1010
+ await using tmp = await tmpdir({
1011
+ init: async (dir) => {
1012
+ await writeConfig(dir, {
1013
+ $schema: "https://opencode.ai/config.json",
1014
+ model: "user/model",
1015
+ share: "auto",
1016
+ username: "testuser",
1017
+ })
1018
+ },
1019
+ })
1020
+
1021
+ await writeManagedSettings({
1022
+ $schema: "https://opencode.ai/config.json",
1023
+ model: "managed/model",
1024
+ share: "disabled",
1025
+ })
1026
+
1027
+ await Instance.provide({
1028
+ directory: tmp.path,
1029
+ fn: async () => {
1030
+ const config = await Config.get()
1031
+ expect(config.model).toBe("managed/model")
1032
+ expect(config.share).toBe("disabled")
1033
+ expect(config.username).toBe("testuser")
1034
+ },
1035
+ })
1036
+ })
1037
+
1038
+ test("managed settings override project settings", async () => {
1039
+ await using tmp = await tmpdir({
1040
+ init: async (dir) => {
1041
+ await writeConfig(dir, {
1042
+ $schema: "https://opencode.ai/config.json",
1043
+ autoupdate: true,
1044
+ disabled_providers: [],
1045
+ theme: "dark",
1046
+ })
1047
+ },
1048
+ })
1049
+
1050
+ await writeManagedSettings({
1051
+ $schema: "https://opencode.ai/config.json",
1052
+ autoupdate: false,
1053
+ disabled_providers: ["openai"],
1054
+ })
1055
+
1056
+ await Instance.provide({
1057
+ directory: tmp.path,
1058
+ fn: async () => {
1059
+ const config = await Config.get()
1060
+ expect(config.autoupdate).toBe(false)
1061
+ expect(config.disabled_providers).toEqual(["openai"])
1062
+ expect(config.theme).toBe("dark")
1063
+ },
1064
+ })
1065
+ })
1066
+
1067
+ test("missing managed settings file is not an error", async () => {
1068
+ await using tmp = await tmpdir({
1069
+ init: async (dir) => {
1070
+ await writeConfig(dir, {
1071
+ $schema: "https://opencode.ai/config.json",
1072
+ model: "user/model",
1073
+ })
1074
+ },
1075
+ })
1076
+
1077
+ await Instance.provide({
1078
+ directory: tmp.path,
1079
+ fn: async () => {
1080
+ const config = await Config.get()
1081
+ expect(config.model).toBe("user/model")
1082
+ },
1083
+ })
1084
+ })
1085
+
1086
+ test("migrates legacy edit tool to edit permission", async () => {
1087
+ await using tmp = await tmpdir({
1088
+ init: async (dir) => {
1089
+ await Bun.write(
1090
+ path.join(dir, "opencode.json"),
1091
+ JSON.stringify({
1092
+ $schema: "https://opencode.ai/config.json",
1093
+ agent: {
1094
+ test: {
1095
+ tools: {
1096
+ edit: false,
1097
+ },
1098
+ },
1099
+ },
1100
+ }),
1101
+ )
1102
+ },
1103
+ })
1104
+ await Instance.provide({
1105
+ directory: tmp.path,
1106
+ fn: async () => {
1107
+ const config = await Config.get()
1108
+ expect(config.agent?.["test"]?.permission).toEqual({
1109
+ edit: "deny",
1110
+ })
1111
+ },
1112
+ })
1113
+ })
1114
+
1115
+ test("migrates legacy patch tool to edit permission", async () => {
1116
+ await using tmp = await tmpdir({
1117
+ init: async (dir) => {
1118
+ await Bun.write(
1119
+ path.join(dir, "opencode.json"),
1120
+ JSON.stringify({
1121
+ $schema: "https://opencode.ai/config.json",
1122
+ agent: {
1123
+ test: {
1124
+ tools: {
1125
+ patch: true,
1126
+ },
1127
+ },
1128
+ },
1129
+ }),
1130
+ )
1131
+ },
1132
+ })
1133
+ await Instance.provide({
1134
+ directory: tmp.path,
1135
+ fn: async () => {
1136
+ const config = await Config.get()
1137
+ expect(config.agent?.["test"]?.permission).toEqual({
1138
+ edit: "allow",
1139
+ })
1140
+ },
1141
+ })
1142
+ })
1143
+
1144
+ test("migrates legacy multiedit tool to edit permission", async () => {
1145
+ await using tmp = await tmpdir({
1146
+ init: async (dir) => {
1147
+ await Bun.write(
1148
+ path.join(dir, "opencode.json"),
1149
+ JSON.stringify({
1150
+ $schema: "https://opencode.ai/config.json",
1151
+ agent: {
1152
+ test: {
1153
+ tools: {
1154
+ multiedit: false,
1155
+ },
1156
+ },
1157
+ },
1158
+ }),
1159
+ )
1160
+ },
1161
+ })
1162
+ await Instance.provide({
1163
+ directory: tmp.path,
1164
+ fn: async () => {
1165
+ const config = await Config.get()
1166
+ expect(config.agent?.["test"]?.permission).toEqual({
1167
+ edit: "deny",
1168
+ })
1169
+ },
1170
+ })
1171
+ })
1172
+
1173
+ test("migrates mixed legacy tools config", async () => {
1174
+ await using tmp = await tmpdir({
1175
+ init: async (dir) => {
1176
+ await Bun.write(
1177
+ path.join(dir, "opencode.json"),
1178
+ JSON.stringify({
1179
+ $schema: "https://opencode.ai/config.json",
1180
+ agent: {
1181
+ test: {
1182
+ tools: {
1183
+ bash: true,
1184
+ write: true,
1185
+ read: false,
1186
+ webfetch: true,
1187
+ },
1188
+ },
1189
+ },
1190
+ }),
1191
+ )
1192
+ },
1193
+ })
1194
+ await Instance.provide({
1195
+ directory: tmp.path,
1196
+ fn: async () => {
1197
+ const config = await Config.get()
1198
+ expect(config.agent?.["test"]?.permission).toEqual({
1199
+ bash: "allow",
1200
+ edit: "allow",
1201
+ read: "deny",
1202
+ webfetch: "allow",
1203
+ })
1204
+ },
1205
+ })
1206
+ })
1207
+
1208
+ test("merges legacy tools with existing permission config", async () => {
1209
+ await using tmp = await tmpdir({
1210
+ init: async (dir) => {
1211
+ await Bun.write(
1212
+ path.join(dir, "opencode.json"),
1213
+ JSON.stringify({
1214
+ $schema: "https://opencode.ai/config.json",
1215
+ agent: {
1216
+ test: {
1217
+ permission: {
1218
+ glob: "allow",
1219
+ },
1220
+ tools: {
1221
+ bash: true,
1222
+ },
1223
+ },
1224
+ },
1225
+ }),
1226
+ )
1227
+ },
1228
+ })
1229
+ await Instance.provide({
1230
+ directory: tmp.path,
1231
+ fn: async () => {
1232
+ const config = await Config.get()
1233
+ expect(config.agent?.["test"]?.permission).toEqual({
1234
+ glob: "allow",
1235
+ bash: "allow",
1236
+ })
1237
+ },
1238
+ })
1239
+ })
1240
+
1241
+ test("permission config preserves key order", async () => {
1242
+ await using tmp = await tmpdir({
1243
+ init: async (dir) => {
1244
+ await Bun.write(
1245
+ path.join(dir, "opencode.json"),
1246
+ JSON.stringify({
1247
+ $schema: "https://opencode.ai/config.json",
1248
+ permission: {
1249
+ "*": "deny",
1250
+ edit: "ask",
1251
+ write: "ask",
1252
+ external_directory: "ask",
1253
+ read: "allow",
1254
+ todowrite: "allow",
1255
+ todoread: "allow",
1256
+ "thoughts_*": "allow",
1257
+ "reasoning_model_*": "allow",
1258
+ "tools_*": "allow",
1259
+ "pr_comments_*": "allow",
1260
+ },
1261
+ }),
1262
+ )
1263
+ },
1264
+ })
1265
+ await Instance.provide({
1266
+ directory: tmp.path,
1267
+ fn: async () => {
1268
+ const config = await Config.get()
1269
+ expect(Object.keys(config.permission!)).toEqual([
1270
+ "*",
1271
+ "edit",
1272
+ "write",
1273
+ "external_directory",
1274
+ "read",
1275
+ "todowrite",
1276
+ "todoread",
1277
+ "thoughts_*",
1278
+ "reasoning_model_*",
1279
+ "tools_*",
1280
+ "pr_comments_*",
1281
+ ])
1282
+ },
1283
+ })
1284
+ })
1285
+
1286
+ // MCP config merging tests
1287
+
1288
+ test("project config can override MCP server enabled status", async () => {
1289
+ await using tmp = await tmpdir({
1290
+ init: async (dir) => {
1291
+ // Simulates a base config (like from remote .well-known) with disabled MCP
1292
+ await Bun.write(
1293
+ path.join(dir, "opencode.jsonc"),
1294
+ JSON.stringify({
1295
+ $schema: "https://opencode.ai/config.json",
1296
+ mcp: {
1297
+ jira: {
1298
+ type: "remote",
1299
+ url: "https://jira.example.com/mcp",
1300
+ enabled: false,
1301
+ },
1302
+ wiki: {
1303
+ type: "remote",
1304
+ url: "https://wiki.example.com/mcp",
1305
+ enabled: false,
1306
+ },
1307
+ },
1308
+ }),
1309
+ )
1310
+ // Project config enables just jira
1311
+ await Bun.write(
1312
+ path.join(dir, "opencode.json"),
1313
+ JSON.stringify({
1314
+ $schema: "https://opencode.ai/config.json",
1315
+ mcp: {
1316
+ jira: {
1317
+ type: "remote",
1318
+ url: "https://jira.example.com/mcp",
1319
+ enabled: true,
1320
+ },
1321
+ },
1322
+ }),
1323
+ )
1324
+ },
1325
+ })
1326
+ await Instance.provide({
1327
+ directory: tmp.path,
1328
+ fn: async () => {
1329
+ const config = await Config.get()
1330
+ // jira should be enabled (overridden by project config)
1331
+ expect(config.mcp?.jira).toEqual({
1332
+ type: "remote",
1333
+ url: "https://jira.example.com/mcp",
1334
+ enabled: true,
1335
+ })
1336
+ // wiki should still be disabled (not overridden)
1337
+ expect(config.mcp?.wiki).toEqual({
1338
+ type: "remote",
1339
+ url: "https://wiki.example.com/mcp",
1340
+ enabled: false,
1341
+ })
1342
+ },
1343
+ })
1344
+ })
1345
+
1346
+ test("MCP config deep merges preserving base config properties", async () => {
1347
+ await using tmp = await tmpdir({
1348
+ init: async (dir) => {
1349
+ // Base config with full MCP definition
1350
+ await Bun.write(
1351
+ path.join(dir, "opencode.jsonc"),
1352
+ JSON.stringify({
1353
+ $schema: "https://opencode.ai/config.json",
1354
+ mcp: {
1355
+ myserver: {
1356
+ type: "remote",
1357
+ url: "https://myserver.example.com/mcp",
1358
+ enabled: false,
1359
+ headers: {
1360
+ "X-Custom-Header": "value",
1361
+ },
1362
+ },
1363
+ },
1364
+ }),
1365
+ )
1366
+ // Override just enables it, should preserve other properties
1367
+ await Bun.write(
1368
+ path.join(dir, "opencode.json"),
1369
+ JSON.stringify({
1370
+ $schema: "https://opencode.ai/config.json",
1371
+ mcp: {
1372
+ myserver: {
1373
+ type: "remote",
1374
+ url: "https://myserver.example.com/mcp",
1375
+ enabled: true,
1376
+ },
1377
+ },
1378
+ }),
1379
+ )
1380
+ },
1381
+ })
1382
+ await Instance.provide({
1383
+ directory: tmp.path,
1384
+ fn: async () => {
1385
+ const config = await Config.get()
1386
+ expect(config.mcp?.myserver).toEqual({
1387
+ type: "remote",
1388
+ url: "https://myserver.example.com/mcp",
1389
+ enabled: true,
1390
+ headers: {
1391
+ "X-Custom-Header": "value",
1392
+ },
1393
+ })
1394
+ },
1395
+ })
1396
+ })
1397
+
1398
+ test("local .opencode config can override MCP from project config", async () => {
1399
+ await using tmp = await tmpdir({
1400
+ init: async (dir) => {
1401
+ // Project config with disabled MCP
1402
+ await Bun.write(
1403
+ path.join(dir, "opencode.json"),
1404
+ JSON.stringify({
1405
+ $schema: "https://opencode.ai/config.json",
1406
+ mcp: {
1407
+ docs: {
1408
+ type: "remote",
1409
+ url: "https://docs.example.com/mcp",
1410
+ enabled: false,
1411
+ },
1412
+ },
1413
+ }),
1414
+ )
1415
+ // Local .opencode directory config enables it
1416
+ const opencodeDir = path.join(dir, ".opencode")
1417
+ await fs.mkdir(opencodeDir, { recursive: true })
1418
+ await Bun.write(
1419
+ path.join(opencodeDir, "opencode.json"),
1420
+ JSON.stringify({
1421
+ $schema: "https://opencode.ai/config.json",
1422
+ mcp: {
1423
+ docs: {
1424
+ type: "remote",
1425
+ url: "https://docs.example.com/mcp",
1426
+ enabled: true,
1427
+ },
1428
+ },
1429
+ }),
1430
+ )
1431
+ },
1432
+ })
1433
+ await Instance.provide({
1434
+ directory: tmp.path,
1435
+ fn: async () => {
1436
+ const config = await Config.get()
1437
+ expect(config.mcp?.docs?.enabled).toBe(true)
1438
+ },
1439
+ })
1440
+ })
1441
+
1442
+ test("project config overrides remote well-known config", async () => {
1443
+ const originalFetch = globalThis.fetch
1444
+ let fetchedUrl: string | undefined
1445
+ const mockFetch = mock((url: string | URL | Request) => {
1446
+ const urlStr = url.toString()
1447
+ if (urlStr.includes(".well-known/opencode")) {
1448
+ fetchedUrl = urlStr
1449
+ return Promise.resolve(
1450
+ new Response(
1451
+ JSON.stringify({
1452
+ config: {
1453
+ mcp: {
1454
+ jira: {
1455
+ type: "remote",
1456
+ url: "https://jira.example.com/mcp",
1457
+ enabled: false,
1458
+ },
1459
+ },
1460
+ },
1461
+ }),
1462
+ { status: 200 },
1463
+ ),
1464
+ )
1465
+ }
1466
+ return originalFetch(url)
1467
+ })
1468
+ globalThis.fetch = mockFetch as unknown as typeof fetch
1469
+
1470
+ const originalAuthAll = Auth.all
1471
+ Auth.all = mock(() =>
1472
+ Promise.resolve({
1473
+ "https://example.com": {
1474
+ type: "wellknown" as const,
1475
+ key: "TEST_TOKEN",
1476
+ token: "test-token",
1477
+ },
1478
+ }),
1479
+ )
1480
+
1481
+ try {
1482
+ await using tmp = await tmpdir({
1483
+ git: true,
1484
+ init: async (dir) => {
1485
+ // Project config enables jira (overriding remote default)
1486
+ await Bun.write(
1487
+ path.join(dir, "opencode.json"),
1488
+ JSON.stringify({
1489
+ $schema: "https://opencode.ai/config.json",
1490
+ mcp: {
1491
+ jira: {
1492
+ type: "remote",
1493
+ url: "https://jira.example.com/mcp",
1494
+ enabled: true,
1495
+ },
1496
+ },
1497
+ }),
1498
+ )
1499
+ },
1500
+ })
1501
+ await Instance.provide({
1502
+ directory: tmp.path,
1503
+ fn: async () => {
1504
+ const config = await Config.get()
1505
+ // Verify fetch was called for wellknown config
1506
+ expect(fetchedUrl).toBe("https://example.com/.well-known/opencode")
1507
+ // Project config (enabled: true) should override remote (enabled: false)
1508
+ expect(config.mcp?.jira?.enabled).toBe(true)
1509
+ },
1510
+ })
1511
+ } finally {
1512
+ globalThis.fetch = originalFetch
1513
+ Auth.all = originalAuthAll
1514
+ }
1515
+ })
1516
+
1517
+ describe("getPluginName", () => {
1518
+ test("extracts name from file:// URL", () => {
1519
+ expect(Config.getPluginName("file:///path/to/plugin/foo.js")).toBe("foo")
1520
+ expect(Config.getPluginName("file:///path/to/plugin/bar.ts")).toBe("bar")
1521
+ expect(Config.getPluginName("file:///some/path/my-plugin.js")).toBe("my-plugin")
1522
+ })
1523
+
1524
+ test("extracts name from npm package with version", () => {
1525
+ expect(Config.getPluginName("oh-my-opencode@2.4.3")).toBe("oh-my-opencode")
1526
+ expect(Config.getPluginName("some-plugin@1.0.0")).toBe("some-plugin")
1527
+ expect(Config.getPluginName("plugin@latest")).toBe("plugin")
1528
+ })
1529
+
1530
+ test("extracts name from scoped npm package", () => {
1531
+ expect(Config.getPluginName("@scope/pkg@1.0.0")).toBe("@scope/pkg")
1532
+ expect(Config.getPluginName("@opencode/plugin@2.0.0")).toBe("@opencode/plugin")
1533
+ })
1534
+
1535
+ test("returns full string for package without version", () => {
1536
+ expect(Config.getPluginName("some-plugin")).toBe("some-plugin")
1537
+ expect(Config.getPluginName("@scope/pkg")).toBe("@scope/pkg")
1538
+ })
1539
+ })
1540
+
1541
+ describe("deduplicatePlugins", () => {
1542
+ test("removes duplicates keeping higher priority (later entries)", () => {
1543
+ const plugins = ["global-plugin@1.0.0", "shared-plugin@1.0.0", "local-plugin@2.0.0", "shared-plugin@2.0.0"]
1544
+
1545
+ const result = Config.deduplicatePlugins(plugins)
1546
+
1547
+ expect(result).toContain("global-plugin@1.0.0")
1548
+ expect(result).toContain("local-plugin@2.0.0")
1549
+ expect(result).toContain("shared-plugin@2.0.0")
1550
+ expect(result).not.toContain("shared-plugin@1.0.0")
1551
+ expect(result.length).toBe(3)
1552
+ })
1553
+
1554
+ test("prefers local file over npm package with same name", () => {
1555
+ const plugins = ["oh-my-opencode@2.4.3", "file:///project/.opencode/plugin/oh-my-opencode.js"]
1556
+
1557
+ const result = Config.deduplicatePlugins(plugins)
1558
+
1559
+ expect(result.length).toBe(1)
1560
+ expect(result[0]).toBe("file:///project/.opencode/plugin/oh-my-opencode.js")
1561
+ })
1562
+
1563
+ test("preserves order of remaining plugins", () => {
1564
+ const plugins = ["a-plugin@1.0.0", "b-plugin@1.0.0", "c-plugin@1.0.0"]
1565
+
1566
+ const result = Config.deduplicatePlugins(plugins)
1567
+
1568
+ expect(result).toEqual(["a-plugin@1.0.0", "b-plugin@1.0.0", "c-plugin@1.0.0"])
1569
+ })
1570
+
1571
+ test("local plugin directory overrides global opencode.json plugin", async () => {
1572
+ await using tmp = await tmpdir({
1573
+ init: async (dir) => {
1574
+ const projectDir = path.join(dir, "project")
1575
+ const opencodeDir = path.join(projectDir, ".opencode")
1576
+ const pluginDir = path.join(opencodeDir, "plugin")
1577
+ await fs.mkdir(pluginDir, { recursive: true })
1578
+
1579
+ await Bun.write(
1580
+ path.join(dir, "opencode.json"),
1581
+ JSON.stringify({
1582
+ $schema: "https://opencode.ai/config.json",
1583
+ plugin: ["my-plugin@1.0.0"],
1584
+ }),
1585
+ )
1586
+
1587
+ await Bun.write(path.join(pluginDir, "my-plugin.js"), "export default {}")
1588
+ },
1589
+ })
1590
+
1591
+ await Instance.provide({
1592
+ directory: path.join(tmp.path, "project"),
1593
+ fn: async () => {
1594
+ const config = await Config.get()
1595
+ const plugins = config.plugin ?? []
1596
+
1597
+ const myPlugins = plugins.filter((p) => Config.getPluginName(p) === "my-plugin")
1598
+ expect(myPlugins.length).toBe(1)
1599
+ expect(myPlugins[0].startsWith("file://")).toBe(true)
1600
+ },
1601
+ })
1602
+ })
1603
+ })
1604
+
1605
+ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
1606
+ test("skips project config files when flag is set", async () => {
1607
+ const originalEnv = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1608
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
1609
+
1610
+ try {
1611
+ await using tmp = await tmpdir({
1612
+ init: async (dir) => {
1613
+ // Create a project config that would normally be loaded
1614
+ await Bun.write(
1615
+ path.join(dir, "opencode.json"),
1616
+ JSON.stringify({
1617
+ $schema: "https://opencode.ai/config.json",
1618
+ model: "project/model",
1619
+ username: "project-user",
1620
+ }),
1621
+ )
1622
+ },
1623
+ })
1624
+ await Instance.provide({
1625
+ directory: tmp.path,
1626
+ fn: async () => {
1627
+ const config = await Config.get()
1628
+ // Project config should NOT be loaded - model should be default, not "project/model"
1629
+ expect(config.model).not.toBe("project/model")
1630
+ expect(config.username).not.toBe("project-user")
1631
+ },
1632
+ })
1633
+ } finally {
1634
+ if (originalEnv === undefined) {
1635
+ delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1636
+ } else {
1637
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalEnv
1638
+ }
1639
+ }
1640
+ })
1641
+
1642
+ test("skips project .opencode/ directories when flag is set", async () => {
1643
+ const originalEnv = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1644
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
1645
+
1646
+ try {
1647
+ await using tmp = await tmpdir({
1648
+ init: async (dir) => {
1649
+ // Create a .opencode directory with a command
1650
+ const opencodeDir = path.join(dir, ".opencode", "command")
1651
+ await fs.mkdir(opencodeDir, { recursive: true })
1652
+ await Bun.write(path.join(opencodeDir, "test-cmd.md"), "# Test Command\nThis is a test command.")
1653
+ },
1654
+ })
1655
+ await Instance.provide({
1656
+ directory: tmp.path,
1657
+ fn: async () => {
1658
+ const directories = await Config.directories()
1659
+ // Project .opencode should NOT be in directories list
1660
+ const hasProjectOpencode = directories.some((d) => d.startsWith(tmp.path))
1661
+ expect(hasProjectOpencode).toBe(false)
1662
+ },
1663
+ })
1664
+ } finally {
1665
+ if (originalEnv === undefined) {
1666
+ delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1667
+ } else {
1668
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalEnv
1669
+ }
1670
+ }
1671
+ })
1672
+
1673
+ test("still loads global config when flag is set", async () => {
1674
+ const originalEnv = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1675
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
1676
+
1677
+ try {
1678
+ await using tmp = await tmpdir()
1679
+ await Instance.provide({
1680
+ directory: tmp.path,
1681
+ fn: async () => {
1682
+ // Should still get default config (from global or defaults)
1683
+ const config = await Config.get()
1684
+ expect(config).toBeDefined()
1685
+ expect(config.username).toBeDefined()
1686
+ },
1687
+ })
1688
+ } finally {
1689
+ if (originalEnv === undefined) {
1690
+ delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1691
+ } else {
1692
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalEnv
1693
+ }
1694
+ }
1695
+ })
1696
+
1697
+ test("skips relative instructions with warning when flag is set but no config dir", async () => {
1698
+ const originalDisable = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1699
+ const originalConfigDir = process.env["OPENCODE_CONFIG_DIR"]
1700
+
1701
+ try {
1702
+ // Ensure no config dir is set
1703
+ delete process.env["OPENCODE_CONFIG_DIR"]
1704
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
1705
+
1706
+ await using tmp = await tmpdir({
1707
+ init: async (dir) => {
1708
+ // Create a config with relative instruction path
1709
+ await Bun.write(
1710
+ path.join(dir, "opencode.json"),
1711
+ JSON.stringify({
1712
+ $schema: "https://opencode.ai/config.json",
1713
+ instructions: ["./CUSTOM.md"],
1714
+ }),
1715
+ )
1716
+ // Create the instruction file (should be skipped)
1717
+ await Bun.write(path.join(dir, "CUSTOM.md"), "# Custom Instructions")
1718
+ },
1719
+ })
1720
+
1721
+ await Instance.provide({
1722
+ directory: tmp.path,
1723
+ fn: async () => {
1724
+ // The relative instruction should be skipped without error
1725
+ // We're mainly verifying this doesn't throw and the config loads
1726
+ const config = await Config.get()
1727
+ expect(config).toBeDefined()
1728
+ // The instruction should have been skipped (warning logged)
1729
+ // We can't easily test the warning was logged, but we verify
1730
+ // the relative path didn't cause an error
1731
+ },
1732
+ })
1733
+ } finally {
1734
+ if (originalDisable === undefined) {
1735
+ delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1736
+ } else {
1737
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalDisable
1738
+ }
1739
+ if (originalConfigDir === undefined) {
1740
+ delete process.env["OPENCODE_CONFIG_DIR"]
1741
+ } else {
1742
+ process.env["OPENCODE_CONFIG_DIR"] = originalConfigDir
1743
+ }
1744
+ }
1745
+ })
1746
+
1747
+ test("OPENCODE_CONFIG_DIR still works when flag is set", async () => {
1748
+ const originalDisable = process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1749
+ const originalConfigDir = process.env["OPENCODE_CONFIG_DIR"]
1750
+
1751
+ try {
1752
+ await using configDirTmp = await tmpdir({
1753
+ init: async (dir) => {
1754
+ // Create config in the custom config dir
1755
+ await Bun.write(
1756
+ path.join(dir, "opencode.json"),
1757
+ JSON.stringify({
1758
+ $schema: "https://opencode.ai/config.json",
1759
+ model: "configdir/model",
1760
+ }),
1761
+ )
1762
+ },
1763
+ })
1764
+
1765
+ await using projectTmp = await tmpdir({
1766
+ init: async (dir) => {
1767
+ // Create config in project (should be ignored)
1768
+ await Bun.write(
1769
+ path.join(dir, "opencode.json"),
1770
+ JSON.stringify({
1771
+ $schema: "https://opencode.ai/config.json",
1772
+ model: "project/model",
1773
+ }),
1774
+ )
1775
+ },
1776
+ })
1777
+
1778
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = "true"
1779
+ process.env["OPENCODE_CONFIG_DIR"] = configDirTmp.path
1780
+
1781
+ await Instance.provide({
1782
+ directory: projectTmp.path,
1783
+ fn: async () => {
1784
+ const config = await Config.get()
1785
+ // Should load from OPENCODE_CONFIG_DIR, not project
1786
+ expect(config.model).toBe("configdir/model")
1787
+ },
1788
+ })
1789
+ } finally {
1790
+ if (originalDisable === undefined) {
1791
+ delete process.env["OPENCODE_DISABLE_PROJECT_CONFIG"]
1792
+ } else {
1793
+ process.env["OPENCODE_DISABLE_PROJECT_CONFIG"] = originalDisable
1794
+ }
1795
+ if (originalConfigDir === undefined) {
1796
+ delete process.env["OPENCODE_CONFIG_DIR"]
1797
+ } else {
1798
+ process.env["OPENCODE_CONFIG_DIR"] = originalConfigDir
1799
+ }
1800
+ }
1801
+ })
1802
+ })