agent: OpenCode installs as OpenCode 2 (@opencode/cli, opencode.ai/v2/install) (math_milian)

math_milian on Discord: magpie's npm command for OpenCode installs
OpenCode 1, and OpenCode 2 isn't installed that way. On 2026-11-05
opencode-ai's latest is 1.19.5 and it has no 2.x; OpenCode 2 is
@opencode/cli (2.2.5, anomalyco/opencode's README), and
opencode.ai/install refuses a 2.x ("not an OpenCode 1 release") and
points to opencode.ai/v2/install.

- Install: opencode.ai/v2/install on a Mac or Linux, then
  npm install -g --allow-scripts=@opencode/cli @opencode/cli. Air's
  "OpenCode isn't installed" notice says the same, in every language.
- Update: an install from either package is updated within it (npm,
  bun, pnpm by where it sits; brew's opencode or anomalyco/tap's
  opencode-v2). An installer copy in ~/.opencode/bin is offered its
  own line's newest: OpenCode 2 @opencode/cli's, OpenCode 1
  opencode-ai's, since OpenCode 1's opencode upgrade runs the v1-only
  installer. An unreadable version promises none.
- An existing opencode-ai install isn't moved to OpenCode 2: its
  config differs, and npm needs --force over the other package's bin.

Run (mote): gofmt -l, go vet ./internal/agent, GOOS=linux/windows go
vet, go test ./internal/agent -run 'Install|UpdateCLI|HowInstalled|Mend|
Npm|Bun|Pnpm|Shim|Scripts|CLI|OpenCode|Air' ok; node --test i18n-keys,
i18n-en-no-cjk, agent-air-own, agent-notice-lang, agent-cli-missing: 82
pass. Real, sandbox HOME on mote: curl -fsSL opencode.ai/v2/install |
bash gave ~/.opencode/bin/opencode and opencode2, "opencode v2.2.5";
`opencode upgrade 2.2.4` from there went to v2.2.4 (magpie's target
form); npm 10.9.4 and 11.21.2 with magpie's exact command gave v2.2.5,
opencode2 too; `opencode acp` is in 2.2.5 for Air.
Broken by hand: opencode-ai first in npm turns TestInstallCommands red;
without the line hook TestUpdateCLIOpenCodeKeepsItsLine/1.19.4 is
offered 2.2.5 and TestUpdateCLIOpenCodeTakesVersion goes red.
Not tried: Windows npm install for real, Homebrew's opencode-v2 tap,
moving a real opencode-ai user to OpenCode 2.
This commit is contained in:
yetone
2026-11-05 11:50:13 +08:00
parent 06808ad308
commit 4e85cac01d
7 changed files with 166 additions and 10 deletions
+1 -1
View File
@@ -316,7 +316,7 @@ func airAt(home string, a *Agent, path string, done notice) *Agent {
// what air says after a change (notice.go)
var (
noticeAir1 = newNotice("Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g opencode-ai), then pick Magpie in a new Air task.")
noticeAir1 = newNotice("Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g --allow-scripts=@opencode/cli @opencode/cli), then pick Magpie in a new Air task.")
noticeAir2 = newNotice("In Air, start a new task and pick Magpie in its agent menu: magpie's models and routing groups are in its model menu.")
noticeAirIDE = newNotice("In your JetBrains IDE (2026.3, or 2026.2 with the Air plugin), pick Magpie in Air's agent menu — restart the IDE if it isn't listed yet: magpie's models and routing groups are in its model menu.")
)
+25 -1
View File
@@ -114,6 +114,13 @@ type cliSpec struct {
// -g --trust, pnpm add -g --allow-build=<pkg>. npm 10 and 11 run it
// anyway, and take --allow-scripts as a config they don't know.
scripts bool
// line is the npm package of the line version is in, for a CLI whose
// own updater (self) stays on its line: OpenCode 1's opencode upgrade
// runs opencode.ai/install, which installs OpenCode 1 only and refuses
// a 2.x version, so an OpenCode 1 from that installer is told
// opencode-ai's newest, an OpenCode 2 @opencode/cli's. "" when the
// version isn't known: no version is promised.
line func(version string) string
}
var cliSpecs = map[string]cliSpec{
@@ -150,7 +157,24 @@ var cliSpecs = map[string]cliSpec{
}
return nil
}},
"opencode": {npm: []string{"opencode-ai"}, brew: []string{"opencode"}, target: true, scripts: true,
// OpenCode 2 is @opencode/cli on npm (opencode-ai has OpenCode 1
// only, its latest 1.19.5 while @opencode/cli's is 2.2.5, 2026-11-05;
// math_milian on Discord), its installer opencode.ai/v2/install, which
// puts it in ~/.opencode/bin as OpenCode 1's did, and Homebrew's
// opencode formula (2.2.0) or anomalyco/tap's opencode-v2. An install
// of either package is updated on its own: npm, bun and pnpm keep it
// to its package, and line keeps opencode upgrade to its version's.
"opencode": {npm: []string{"@opencode/cli", "opencode-ai"}, brew: []string{"opencode", "opencode-v2"}, target: true, scripts: true,
line: func(v string) string {
major, _, _ := strings.Cut(strings.TrimPrefix(v, "v"), ".")
switch n, err := strconv.Atoi(major); {
case err != nil:
return ""
case n < 2:
return "opencode-ai"
}
return "@opencode/cli"
},
self: func(bin, real string) []string {
if strings.Contains(real, "/.opencode/bin/") {
return []string{bin, "upgrade"}
+127
View File
@@ -0,0 +1,127 @@
package agent
import (
"context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"runtime"
"testing"
)
// opencodeRegistry serves the newest of npm's two OpenCode packages as
// npm did on 2026-11-05: @opencode/cli is OpenCode 2, opencode-ai
// OpenCode 1 only.
func opencodeRegistry(t *testing.T) {
t.Helper()
reg := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/@opencode/cli/latest":
w.Write([]byte(`{"name":"@opencode/cli","version":"2.2.5"}`))
case "/opencode-ai/latest":
w.Write([]byte(`{"name":"opencode-ai","version":"1.19.5"}`))
default:
http.NotFound(w, r)
}
}))
oldReg := npmRegistry
npmRegistry = reg.URL + "/"
versions, latests = memo{}, memo{}
t.Cleanup(func() { reg.Close(); npmRegistry = oldReg; versions, latests = memo{}, memo{} })
}
// An OpenCode from its installer, in ~/.opencode/bin (OpenCode 2's
// opencode.ai/v2/install puts it there as OpenCode 1's did), is offered
// the newest of its own line and upgraded to it: OpenCode 2 to
// @opencode/cli's 2.2.5, OpenCode 1 to opencode-ai's 1.19.5, never to a
// 2.x, which OpenCode 1's installer refuses ("is not an OpenCode 1
// release, so this installer can't install it").
func TestUpdateCLIOpenCodeKeepsItsLine(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("a shell script as the CLI")
}
for _, c := range []struct{ has, latest string }{
{"2.2.4", "2.2.5"},
{"1.19.4", "1.19.5"},
} {
t.Run(c.has, func(t *testing.T) {
tmp, _ := filepath.EvalSymlinks(t.TempDir())
verFile := filepath.Join(tmp, "version")
os.WriteFile(verFile, []byte("opencode v"+c.has+"\n"), 0o644)
oc := file(t, filepath.Join(tmp, "home/.opencode/bin/opencode"), "#!/bin/sh\n/bin/cat "+verFile+"\n")
bin := link(t, oc, filepath.Join(tmp, "bin/opencode"))
t.Setenv("PATH", filepath.Dir(bin))
opencodeRegistry(t)
oldRun := runUpdate
var ran [][]string
runUpdate = func(_ context.Context, u *updater) ([]byte, error) {
ran = append(ran, u.cmd)
os.WriteFile(verFile, []byte("opencode v"+c.latest+"\n"), 0o644)
return nil, nil
}
t.Cleanup(func() { runUpdate = oldRun })
a := &Agent{ID: "opencode", Name: "OpenCode", Bin: "opencode"}
if got, ok := a.CLI(); !ok || got.Via != "self" || got.Version != c.has || got.Latest != c.latest || !got.Update {
t.Fatalf("before: %+v", got)
}
if got, err := a.UpdateCLI(); err != nil || got.Version != c.latest || got.Update {
t.Fatalf("after: %+v, %v", got, err)
}
if len(ran) != 1 || !reflect.DeepEqual(ran[0], []string{bin, "upgrade", c.latest}) {
t.Errorf("ran %q", ran)
}
})
}
}
// OpenCode 2 from npm (@opencode/cli), from bun and from anomalyco/tap's
// opencode-v2 formula is updated the way it was installed, its
// postinstall let run; an OpenCode 1 from npm stays opencode-ai's.
func TestHowInstalledOpenCode2(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("symlinks")
}
tmp, _ := filepath.EvalSymlinks(t.TempDir())
t.Setenv("PATH", t.TempDir())
spec := cliSpecs["opencode"]
prefix := filepath.Join(tmp, "node")
npm := file(t, filepath.Join(prefix, "bin/npm"), "#!/bin/sh\n")
file(t, filepath.Join(prefix, "lib/node_modules/@opencode/cli/bin/opencode.exe"), "#!/bin/sh\n")
oc := link(t, "../lib/node_modules/@opencode/cli/bin/opencode.exe", filepath.Join(prefix, "bin/opencode"))
if u := howInstalled(spec, oc); u == nil || u.via != "npm" || !reflect.DeepEqual(u.cmd, []string{npm, "install", "-g", "--prefix", prefix, "--allow-scripts=@opencode/cli", "@opencode/cli@latest"}) {
t.Errorf("npm's @opencode/cli: %+v", u)
}
// its second command, opencode2, is the same program
oc2 := link(t, "../lib/node_modules/@opencode/cli/bin/opencode.exe", filepath.Join(prefix, "bin/opencode2"))
if u := howInstalled(spec, oc2); u == nil || u.pkg != "@opencode/cli" {
t.Errorf("opencode2: %+v", u)
}
old := filepath.Join(tmp, "old")
oldNpm := file(t, filepath.Join(old, "bin/npm"), "#!/bin/sh\n")
file(t, filepath.Join(old, "lib/node_modules/opencode-ai/bin/opencode.exe"), "#!/bin/sh\n")
oc1 := link(t, "../lib/node_modules/opencode-ai/bin/opencode.exe", filepath.Join(old, "bin/opencode"))
if u := howInstalled(spec, oc1); u == nil || !reflect.DeepEqual(u.cmd, []string{oldNpm, "install", "-g", "--prefix", old, "--allow-scripts=opencode-ai", "opencode-ai@latest"}) {
t.Errorf("npm's opencode-ai: %+v", u)
}
home := filepath.Join(tmp, "home")
bun := file(t, filepath.Join(home, ".bun/bin/bun"), "")
file(t, filepath.Join(home, ".bun/install/global/node_modules/@opencode/cli/bin/opencode.exe"), "")
ocBun := link(t, "../install/global/node_modules/@opencode/cli/bin/opencode.exe", filepath.Join(home, ".bun/bin/opencode"))
if u := howInstalled(spec, ocBun); u == nil || !reflect.DeepEqual(u.cmd, []string{bun, "add", "-g", "--trust", "@opencode/cli@latest"}) {
t.Errorf("bun's @opencode/cli: %+v", u)
}
brewDir := filepath.Join(tmp, "homebrew")
brew := file(t, filepath.Join(brewDir, "bin/brew"), "#!/bin/sh\n")
file(t, filepath.Join(brewDir, "Cellar/opencode-v2/2.2.5/bin/opencode"), "")
ocTap := link(t, "../Cellar/opencode-v2/2.2.5/bin/opencode", filepath.Join(brewDir, "bin/opencode"))
if u := howInstalled(spec, ocTap); u == nil || !reflect.DeepEqual(u.cmd, []string{brew, "upgrade", "opencode-v2"}) {
t.Errorf("anomalyco/tap's opencode-v2: %+v", u)
}
}
+3
View File
@@ -195,6 +195,9 @@ func runnable(p string) string {
func installedAt(spec cliSpec, bin string) (string, *updater) {
real, s := throughShim(bin)
u := howInstalled(spec, real)
if u != nil && u.via == "self" && spec.line != nil {
u.pkg = spec.line(installedVersion(real))
}
if u != nil && s != nil && s.kind == "mise" {
u.reshim = []string{s.mgr, "reshim"}
}
+3 -1
View File
@@ -69,7 +69,9 @@ var vendorInstall = map[string]func(goos string) []InstallCmd{
if goos == "windows" {
return nil
}
return []InstallCmd{{Via: "script", Command: "curl -fsSL https://opencode.ai/install | bash"}}
// OpenCode 2's (its README, opencode.ai/v2/docs); opencode.ai/install
// installs OpenCode 1 only, and refuses a 2.x version
return []InstallCmd{{Via: "script", Command: "curl -fsSL https://opencode.ai/v2/install | bash"}}
},
"codex": func(goos string) []InstallCmd {
switch goos {
+2 -2
View File
@@ -33,8 +33,8 @@ func TestInstallCommands(t *testing.T) {
{"codex", "linux", []string{"script: curl -fsSL https://chatgpt.com/codex/install.sh | sh", "npm: npm install -g @openai/codex"}},
{"gemini", "linux", []string{"brew: brew install gemini-cli", "npm: npm install -g @google/gemini-cli"}},
{"gemini", "windows", []string{"npm: npm install -g @google/gemini-cli"}},
{"opencode", "darwin", []string{"script: curl -fsSL https://opencode.ai/install | bash", "npm: npm install -g --allow-scripts=opencode-ai opencode-ai"}},
{"opencode", "windows", []string{"npm: npm install -g --allow-scripts=opencode-ai opencode-ai"}},
{"opencode", "darwin", []string{"script: curl -fsSL https://opencode.ai/v2/install | bash", "npm: npm install -g --allow-scripts=@opencode/cli @opencode/cli"}},
{"opencode", "windows", []string{"npm: npm install -g --allow-scripts=@opencode/cli @opencode/cli"}},
{"deveco", "darwin", []string{"npm: npm install -g --allow-scripts=@deveco/deveco-code @deveco/deveco-code"}},
{"deveco", "windows", []string{"npm: npm install -g --allow-scripts=@deveco/deveco-code @deveco/deveco-code"}},
{"deveco", "linux", nil}, // no Linux build in its package
+5 -5
View File
@@ -2146,7 +2146,7 @@ const I18N = {
"Restart Zed if it still asks for an API key: magpie has configured its gateway credential in the system credential store.": "若 Zed 仍要求输入 API 密钥,请重启 Zed:网关凭据已写入系统凭据存储。",
"In Air, start a new task and pick Magpie in its agent menu: magpie's models and routing groups are in its model menu.": "在 Air 中新建任务并在智能体菜单选 Magpie:模型菜单中即有 magpie 的模型和路由组。",
"In your JetBrains IDE (2026.3, or 2026.2 with the Air plugin), pick Magpie in Air's agent menu — restart the IDE if it isn't listed yet: magpie's models and routing groups are in its model menu.": "在 JetBrains IDE(2026.3,或装了 Air 插件的 2026.2)中,在 Air 的智能体菜单选 Magpie;若还没列出,请重启 IDE。模型菜单中即有 magpie 的模型和路由组。",
"Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g opencode-ai), then pick Magpie in a new Air task.": "Air 经 OpenCode 的 ACP 服务使用 magpie 的模型,本机未安装 OpenCode:请先安装(npm i -g opencode-ai),再在 Air 新任务中选 Magpie。",
"Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g --allow-scripts=@opencode/cli @opencode/cli), then pick Magpie in a new Air task.": "Air 经 OpenCode 的 ACP 服务使用 magpie 的模型,本机未安装 OpenCode:请先安装(npm i -g --allow-scripts=@opencode/cli @opencode/cli),再在 Air 新任务中选 Magpie。",
"magpie's models are in VS Code's Chat model picker, under magpie (VS Code 1.122 or later). If they don't show, run Developer: Reload Window in VS Code.": "magpie 的模型位于 VS Code 聊天模型选择器的 magpie 分组下(需 VS Code 1.122 或更高)。若未出现,请在 VS Code 中运行 Developer: Reload Window。",
"Later": "稍后",
"Restart": "重启",
@@ -6748,7 +6748,7 @@ const I18N = {
"Restart Zed if it still asks for an API key: magpie has configured its gateway credential in the system credential store.": "若 Zed 仍要求輸入 API 金鑰,請重啟 Zed:閘道憑據已寫入系統憑據儲存。",
"In Air, start a new task and pick Magpie in its agent menu: magpie's models and routing groups are in its model menu.": "在 Air 中新增任務並在智慧體選單選 Magpie:模型選單中即有 magpie 的模型和路由組。",
"In your JetBrains IDE (2026.3, or 2026.2 with the Air plugin), pick Magpie in Air's agent menu — restart the IDE if it isn't listed yet: magpie's models and routing groups are in its model menu.": "在 JetBrains IDE(2026.3,或裝了 Air 外掛的 2026.2)中,在 Air 的智慧體選單選 Magpie;若還沒列出,請重新啟動 IDE。模型選單中即有 magpie 的模型和路由組。",
"Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g opencode-ai), then pick Magpie in a new Air task.": "Air 經 OpenCode 的 ACP 服務使用 magpie 的模型,本機未安裝 OpenCode:請先安裝(npm i -g opencode-ai),再在 Air 新任務中選 Magpie。",
"Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g --allow-scripts=@opencode/cli @opencode/cli), then pick Magpie in a new Air task.": "Air 經 OpenCode 的 ACP 服務使用 magpie 的模型,本機未安裝 OpenCode:請先安裝(npm i -g --allow-scripts=@opencode/cli @opencode/cli),再在 Air 新任務中選 Magpie。",
"magpie's models are in VS Code's Chat model picker, under magpie (VS Code 1.122 or later). If they don't show, run Developer: Reload Window in VS Code.": "magpie 的模型位於 VS Code 聊天模型選擇器的 magpie 分組下(需 VS Code 1.122 或更高)。若未出現,請在 VS Code 中執行 Developer: Reload Window。",
"Later": "稍後",
"Restart": "重啟",
@@ -11326,7 +11326,7 @@ const I18N = {
"Restart Zed if it still asks for an API key: magpie has configured its gateway credential in the system credential store.": "Zed がまだ APIキーを求める場合は Zed を再起動してください。magpie はシステムの資格情報ストアにゲートウェイの資格情報を設定済みです。",
"In Air, start a new task and pick Magpie in its agent menu: magpie's models and routing groups are in its model menu.": "Air で新しいタスクを開始し、エージェントメニューで Magpie を選んでください。magpie のモデルとルーティンググループはモデルメニューにあります。",
"In your JetBrains IDE (2026.3, or 2026.2 with the Air plugin), pick Magpie in Air's agent menu — restart the IDE if it isn't listed yet: magpie's models and routing groups are in its model menu.": "JetBrains IDE(2026.3、または Air プラグインを入れた 2026.2)で、Air のエージェントメニューから Magpie を選んでください。まだ表示されない場合は IDE を再起動してください。magpie のモデルとルーティンググループはモデルメニューにあります。",
"Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g opencode-ai), then pick Magpie in a new Air task.": "Air は OpenCode の ACP サーバーを通じて magpie のモデルを使いますが、OpenCode がインストールされていません。OpenCode をインストール(npm i -g opencode-ai)してから、Air の新しいタスクで Magpie を選んでください。",
"Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g --allow-scripts=@opencode/cli @opencode/cli), then pick Magpie in a new Air task.": "Air は OpenCode の ACP サーバーを通じて magpie のモデルを使いますが、OpenCode がインストールされていません。OpenCode をインストール(npm i -g --allow-scripts=@opencode/cli @opencode/cli)してから、Air の新しいタスクで Magpie を選んでください。",
"magpie's models are in VS Code's Chat model picker, under magpie (VS Code 1.122 or later). If they don't show, run Developer: Reload Window in VS Code.": "magpie のモデルは VS Code のチャットのモデルピッカーの magpie の下にあります(VS Code 1.122 以降)。表示されない場合は VS Code で Developer: Reload Window を実行してください。",
"Later": "後で",
"Restart": "再起動",
@@ -15935,7 +15935,7 @@ const I18N = {
"Restart Zed if it still asks for an API key: magpie has configured its gateway credential in the system credential store.": "Starten Sie Zed neu, falls es weiterhin nach einem API-Schlüssel fragt: magpie hat die Anmeldedaten des Zugangspunkts im Anmeldeinformationsspeicher des Systems hinterlegt.",
"In Air, start a new task and pick Magpie in its agent menu: magpie's models and routing groups are in its model menu.": "Starten Sie in Air eine neue Aufgabe und wählen Sie im Agentenmenü Magpie: Die Modelle und Weiterleitungsgruppen von magpie stehen im Modellmenü.",
"In your JetBrains IDE (2026.3, or 2026.2 with the Air plugin), pick Magpie in Air's agent menu — restart the IDE if it isn't listed yet: magpie's models and routing groups are in its model menu.": "Wählen Sie in Ihrer JetBrains-IDE (2026.3 oder 2026.2 mit dem Air-Plugin) im Agentenmenü von Air Magpie – starten Sie die IDE neu, falls es noch nicht aufgeführt ist: Die Modelle und Weiterleitungsgruppen von magpie stehen im Modellmenü.",
"Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g opencode-ai), then pick Magpie in a new Air task.": "Air nutzt die Modelle von magpie über den ACP-Server von OpenCode, und OpenCode ist hier nicht installiert: Installieren Sie OpenCode (npm i -g opencode-ai) und wählen Sie dann in einer neuen Air-Aufgabe Magpie.",
"Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g --allow-scripts=@opencode/cli @opencode/cli), then pick Magpie in a new Air task.": "Air nutzt die Modelle von magpie über den ACP-Server von OpenCode, und OpenCode ist hier nicht installiert: Installieren Sie OpenCode (npm i -g --allow-scripts=@opencode/cli @opencode/cli) und wählen Sie dann in einer neuen Air-Aufgabe Magpie.",
"magpie's models are in VS Code's Chat model picker, under magpie (VS Code 1.122 or later). If they don't show, run Developer: Reload Window in VS Code.": "Die Modelle von magpie stehen in der Modellauswahl des VS-Code-Chats unter magpie (VS Code 1.122 oder neuer). Erscheinen sie nicht, führen Sie in VS Code Developer: Reload Window aus.",
"Later": "Später",
"Restart": "Neu starten",
@@ -18416,7 +18416,7 @@ const I18N = {
// internal/agent/notice.go), as tNotice matches them; each has its
// translation above. TestEveryNoticeIsTranslated keeps the two the same.
const NOTICES = [
"Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g opencode-ai), then pick Magpie in a new Air task.",
"Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g --allow-scripts=@opencode/cli @opencode/cli), then pick Magpie in a new Air task.",
"Ante reads its catalog at start-up — restart open ante sessions to use this.",
"AstrBot reads cmd_config.json at start-up and writes it back whole when its WebUI saves settings — restart AstrBot to use this, before saving anything in its WebUI.",
"QwenPaw reads its providers at start-up — restart QwenPaw once after connecting it, and after magpie's models change; a model picked for an agent after that is used from its next message.",