From 95ef45fda4b3978e94f12954a27d41e5ef1dca85 Mon Sep 17 00:00:00 2001 From: ggbdpq Date: Sun, 18 Oct 2026 21:10:32 +0800 Subject: [PATCH] agent: a curl-installed opencode's update passes the version its row promised, so it never asks GitHub's API (#2837, ggbdpq) What changed: an opencode 1.19.0 that its own installer put at ~/.opencode/bin/opencode (method curl) asks GitHub's API GET /repos/anomalyco/opencode/releases/latest for the newest release before installing. That request carries no token, so it spends the 60 an hour GitHub answers an address without one. Once those are spent the answer is a 403 and the update stops before its installer runs (ggbdpq: "opencode upgrade: ... 403 GET https://api.github.com/repos/anomalyco/opencode/releases/latest"). cliSpec gains target, for a CLI whose own updater takes the version to install, and opencode sets it. When opencode's own updater is what runs (via "self"), UpdateCLI appends the version it already knows, the row's Latest from npm's opencode-ai, if that is newer than the installed one. The run is then "opencode upgrade 1.19.3". In opencode's source (packages/cli/src/commands/handlers/upgrade.ts) a given target skips latest(), and the curl method passes it to the installer as --version. A stale npm answer appends nothing (Newer), so an update can never become a downgrade. When the lookup fails, the command stays "opencode upgrade". Changed in review: the patch as sent appended the version for every install method. An opencode installed by npm, bun, pnpm or brew then ran `npm install -g --prefix P opencode-ai@latest 1.19.3`, which also asks npm to install a package named "1.19.3", or `brew upgrade opencode 1.19.3`. The append is now limited to via "self". TestUpdateCLIOpenCodeByNpmTakesNoVersion (npm prefix install) guards it. The target comment no longer says 1.19.3 asks npm: opencode's current source asks opencode.ai. Run: - Breaks: without the via guard (the patch as sent), TestUpdateCLIOpenCodeByNpmTakesNoVersion fails with "ran [[.../npm install -g --prefix ... opencode-ai@latest 1.19.3]]". Without the target block, TestUpdateCLIOpenCodeTakesVersion fails at cliupdate_test.go:411 with `ran [[".../bin/opencode" "upgrade"]]`. Both pass restored. - gofmt -l clean; go vet darwin, linux (nogui) and windows ok; go build for all three ok. - Full `go test -tags nogui -timeout 45m ./...` on origin/main d783df46c + this: every package ok except internal/agent and internal/gateway, which hit 45m mid-test under a load average of about 200, and internal/gui's TestSetAnswersWhileDevinAndProvidersHang (10.8s, filed as #1375, output added there). Rerun of agent, gateway and gui with -timeout 120m: all three ok (4061s, 3393s, 564s). After the comment-only edit, `go test ./internal/agent -run TestUpdateCLI` passed again. Not tried: a real opencode 1.19.0 update against GitHub's rate limit. Co-authored-by: yetone --- internal/agent/cliupdate.go | 21 ++++++- internal/agent/cliupdate_test.go | 94 ++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/internal/agent/cliupdate.go b/internal/agent/cliupdate.go index ffbd1ef21..dcebb7e47 100644 --- a/internal/agent/cliupdate.go +++ b/internal/agent/cliupdate.go @@ -82,6 +82,17 @@ type cliSpec struct { // latest is the newest version its own updater would install, for one // not published on npm latest func() string + // target: its own updater takes the version to install (opencode + // upgrade 1.19.3), so magpie gives it the one it already knows, as the + // row promised, when that updater is what runs (via "self"): npm's, + // bun's, pnpm's and brew's commands take no version after them. An opencode 1.19.0 from its own installer (method curl) + // asks GitHub's API for the newest release, which answers an address 60 + // times an hour without a token and 403s once that is spent; opencode's + // current source asks opencode.ai instead, but only an update gets + // there (#2837, ggbdpq: + // "opencode upgrade: ... 403 GET https://api.github.com/repos/ + // anomalyco/opencode/releases/latest") + target bool // only are the systems (GOOS) its vendor builds it for, when not all: // on any other its package installs nothing that runs only []string @@ -121,7 +132,7 @@ var cliSpecs = map[string]cliSpec{ } return nil }}, - "opencode": {npm: []string{"opencode-ai"}, brew: []string{"opencode"}, + "opencode": {npm: []string{"opencode-ai"}, brew: []string{"opencode"}, target: true, self: func(bin, real string) []string { if strings.Contains(real, "/.opencode/bin/") { return []string{bin, "upgrade"} @@ -948,6 +959,14 @@ func (a *Agent) UpdateCLI() (CLI, error) { } defer updating.Delete(a.ID) before := installedVersion(real) + if spec.target && u.via == "self" { + // the version the row promised, so its own updater needn't ask + // GitHub's API for the newest (see cliSpec.target); npm, bun, + // pnpm and brew take no version after their command + if v := latestVersion(u); Newer(v, before) { + u.cmd = append(u.cmd, v) + } + } ctx, cancel := context.WithTimeout(context.Background(), updateTimeout) defer cancel() run := runUpdate diff --git a/internal/agent/cliupdate_test.go b/internal/agent/cliupdate_test.go index cf97ef9db..b29cea6f3 100644 --- a/internal/agent/cliupdate_test.go +++ b/internal/agent/cliupdate_test.go @@ -365,6 +365,100 @@ func TestUpdateCLI(t *testing.T) { } } +// An opencode 1.19.0's upgrade asks GitHub's API for the newest release, +// which answers an address 60 times an hour without a token before a 403 +// (ggbdpq: "opencode upgrade: ... 403 GET https://api.github.com/repos/ +// anomalyco/opencode/releases/latest"). magpie gives it the version the row +// promised, which its installer downloads from the release itself. +func TestUpdateCLIOpenCodeTakesVersion(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("a shell script as the CLI") + } + tmp, _ := filepath.EvalSymlinks(t.TempDir()) + verFile := filepath.Join(tmp, "version") + os.WriteFile(verFile, []byte("1.19.2\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)) + + reg := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/opencode-ai/latest" { + http.NotFound(w, r) + return + } + w.Write([]byte(`{"name":"opencode-ai","version":"1.19.3"}`)) + })) + defer reg.Close() + oldReg, oldRun := npmRegistry, runUpdate + npmRegistry = reg.URL + "/" + versions, latests = memo{}, memo{} + var ran [][]string + runUpdate = func(_ context.Context, u *updater) ([]byte, error) { + ran = append(ran, u.cmd) + os.WriteFile(verFile, []byte("1.19.3\n"), 0o644) + return nil, nil + } + t.Cleanup(func() { npmRegistry, runUpdate = oldReg, oldRun; versions, latests = memo{}, memo{} }) + + a := &Agent{ID: "opencode", Name: "OpenCode", Bin: "opencode"} + if c, ok := a.CLI(); !ok || c.Via != "self" || c.Version != "1.19.2" || c.Latest != "1.19.3" || !c.Update { + t.Fatalf("before: %+v", c) + } + if c, err := a.UpdateCLI(); err != nil || c.Version != "1.19.3" || c.Update { + t.Fatalf("after: %+v, %v", c, err) + } + if len(ran) != 1 || !reflect.DeepEqual(ran[0], []string{bin, "upgrade", "1.19.3"}) { + t.Errorf("ran %q", ran) + } +} + +// An opencode npm installed is updated by npm as it was: the version +// opencode's own upgrade takes is never put after npm's install, where npm +// would read it as one more package to install. +func TestUpdateCLIOpenCodeByNpmTakesNoVersion(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("a shell script as the CLI") + } + tmp, _ := filepath.EvalSymlinks(t.TempDir()) + prefix := filepath.Join(tmp, "node") + verFile := filepath.Join(tmp, "version") + os.WriteFile(verFile, []byte("1.19.2\n"), 0o644) + file(t, filepath.Join(prefix, "lib/node_modules/opencode-ai/bin/opencode"), "#!/bin/sh\n/bin/cat "+verFile+"\n") + file(t, filepath.Join(prefix, "bin/npm"), "#!/bin/sh\nexit 1\n") + link(t, "../lib/node_modules/opencode-ai/bin/opencode", filepath.Join(prefix, "bin/opencode")) + t.Setenv("PATH", filepath.Join(prefix, "bin")) + + reg := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/opencode-ai/latest" { + http.NotFound(w, r) + return + } + w.Write([]byte(`{"name":"opencode-ai","version":"1.19.3"}`)) + })) + defer reg.Close() + oldReg, oldRun := npmRegistry, runUpdate + npmRegistry = reg.URL + "/" + versions, latests = memo{}, memo{} + var ran [][]string + runUpdate = func(_ context.Context, u *updater) ([]byte, error) { + ran = append(ran, u.cmd) + os.WriteFile(verFile, []byte("1.19.3\n"), 0o644) + return nil, nil + } + t.Cleanup(func() { npmRegistry, runUpdate = oldReg, oldRun; versions, latests = memo{}, memo{} }) + + a := &Agent{ID: "opencode", Name: "OpenCode", Bin: "opencode"} + if c, ok := a.CLI(); !ok || c.Via != "npm" || c.Latest != "1.19.3" || !c.Update { + t.Fatalf("before: %+v", c) + } + if _, err := a.UpdateCLI(); err != nil { + t.Fatal(err) + } + if len(ran) != 1 || !strings.HasSuffix(strings.Join(ran[0], " "), "install -g --prefix "+prefix+" opencode-ai@latest") { + t.Errorf("ran %q", ran) + } +} + // An agent's CLI installed with bun is updated by bun add -g, which can't // use a SOCKS5 proxy (#1409): it is given the bridge in front of it. npm, // which can, is given the proxy as it is.