agent: OmO's update runs the npm of the node it was installed with, not a system one first on magpie's PATH (#2021, ggbdpq)

OmO updates itself: magpie runs `omo update`, and omo-ai 5.1.32's
updateTarget (bin/lib/package-paths.js) then runs a bare
`npm i -g omo-ai@<v>` (or `bun add -g` for a bun install). The
updater for a self-updating CLI had no path, so that npm was whichever
came first on magpie's PATH. With OmO installed under an fnm/nvm node
and a system npm in /usr/local/bin, the update went to the system
prefix (or failed on its permissions), and the omo the agent runs
stayed old.

howInstalled's self branch now gives the updater the bin dir of the
package manager that holds the CLI (selfPath): for an npm install, the
prefix's bin that fromNodeModules already finds for npm-installed CLIs;
for bun or pnpm, the dir of that manager's binary. That dir goes first
on the update's PATH, as it does for npm updates.

Run: TestOmoUpdateTakesItsOwnNpm (new: a fake system npm that fails and
an fnm prefix's npm that records; on the old code the update ran the
system npm and failed with exit status 1), TestOmo*, TestHowInstalled*,
go test ./internal/agent (ok in 1783s at load average ~100;
a 15m timeout ran out under that load, on no failure),
gofmt -l, GOOS=linux and GOOS=windows go vet -tags nogui ./internal/agent.
This commit is contained in:
yetone
2026-10-14 05:22:47 +08:00
parent 45e77b61f7
commit 27956002c3
2 changed files with 67 additions and 1 deletions
+25 -1
View File
@@ -207,7 +207,9 @@ func howInstalled(spec cliSpec, bin string) *updater {
if len(spec.npm) > 0 {
pkg = spec.npm[0]
}
return &updater{via: "self", pkg: pkg, cmd: c, latest: spec.latest}
u := &updater{via: "self", pkg: pkg, cmd: c, latest: spec.latest}
u.path = selfPath(spec, bin, slash)
return u
}
}
// a formula of the agent's own is Homebrew's, though a Node one keeps
@@ -228,6 +230,28 @@ func howInstalled(spec cliSpec, bin string) *updater {
return nil
}
// selfPath is the folder to put first on PATH for a CLI's own updater, when
// a package manager installed it in a global node_modules: the updater runs
// that manager by its bare name (omo update runs npm i -g), and magpie's
// PATH can find another one first, a system node's in /usr/local/bin
// before a version manager's, whose npm then installs into a folder only
// root may change (#2021, ggbdpq). npm's global folder follows the node
// that runs it, so the folder holds that node too.
func selfPath(spec cliSpec, bin, real string) string {
for _, pkg := range spec.npm {
if u := fromNodeModules(pkg, bin, real); u != nil {
if u.via == "npm" {
return u.path
}
if u.via == "bun" || u.via == "pnpm" {
return filepath.Dir(u.cmd[0])
}
return ""
}
}
return ""
}
// fromBrew is the updater of the CLI at real (forward slashes), which m,
// brewPath's match in it, says Homebrew's formula or cask m[2] installed.
func fromBrew(real string, m []string) *updater {
+42
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
@@ -134,3 +135,44 @@ func TestOmoCLI(t *testing.T) {
t.Fatalf("%+v", u)
}
}
// omo update runs npm by its bare name, so it is given the npm (and node)
// of the prefix OmO was installed in first on PATH: a system node's npm in
// /usr/local/bin, which magpie's PATH holds before a version manager's
// (fnm), would install into a folder only root may change (#2021, ggbdpq).
func TestOmoUpdateTakesItsOwnNpm(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("symlinks and sh scripts")
}
tmp, _ := filepath.EvalSymlinks(t.TempDir())
ran := filepath.Join(tmp, "ran")
// the system's npm, found first on magpie's PATH
sys := filepath.Join(tmp, "usr/local/bin")
file(t, filepath.Join(sys, "npm"), "#!/bin/sh\necho system >> "+ran+"\nexit 1\n")
// fnm's node, where OmO is
prefix := filepath.Join(tmp, "fnm/node-versions/v24.21.0/installation")
file(t, filepath.Join(prefix, "bin/npm"), "#!/bin/sh\necho fnm >> "+ran+"\n")
file(t, filepath.Join(prefix, "bin/node"), "#!/bin/sh\n")
file(t, filepath.Join(prefix, "lib/node_modules/omo-ai/bin/omo.js"), "#!/bin/sh\n[ \"$1\" = update ] && npm i -g omo-ai@5.1.32\n")
omoBin := link(t, "../lib/node_modules/omo-ai/bin/omo.js", filepath.Join(prefix, "bin/omo"))
t.Setenv("PATH", sys+string(os.PathListSeparator)+"/usr/bin:/bin")
u := howInstalled(cliSpecs["omo"], omoBin)
if u == nil || u.via != "self" || u.path != filepath.Join(prefix, "bin") {
t.Fatalf("%+v", u)
}
if out, err := runUpdate(t.Context(), u); err != nil {
t.Fatalf("%v: %s", err, out)
}
if b, _ := os.ReadFile(ran); string(b) != "fnm\n" {
t.Fatalf("omo update ran npm of %q, want fnm's", b)
}
// bun's OmO (bun add -g) keeps bun's folder first the same way
bunHome := filepath.Join(tmp, ".bun")
file(t, filepath.Join(bunHome, "bin/bun"), "#!/bin/sh\n")
file(t, filepath.Join(bunHome, "install/global/node_modules/omo-ai/bin/omo.js"), "")
bunOmo := link(t, "../install/global/node_modules/omo-ai/bin/omo.js", filepath.Join(bunHome, "bin/omo"))
if u := howInstalled(cliSpecs["omo"], bunOmo); u == nil || u.via != "self" || u.path != filepath.Join(bunHome, "bin") {
t.Fatalf("bun: %+v", u)
}
}