From 924f62e90d13ae3c91268d2744f994b879f85ae4 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 15 Oct 2026 01:16:13 +0800 Subject: [PATCH] agent: opencode and omp installed with bun on Windows, and omp's own binary install, offer their updates (#2192, xiaozhu1337) On Windows the Agents page offered no update for opencode 2.0.24 or omp 18.6.1 while newer ones were out (Claude Code's showed). Two causes: - bun on Windows doesn't link a global's program: it writes an 8 KB .exe shim and, beside it, .bunx naming the real program in UTF-16LE relative to bun's folder (install\global\node_modules\opencode-ai\bin\opencode.exe"). The shim names no package, so fromNodeModules gave up and the CLI read as installed some unknown way. bunxNames now reads the .bunx (<= 64 KB), and a shim whose .bunx names node_modules// is bun's: updated with bun add -g @latest, as on macOS and Linux. - omp's installer (-Binary on Windows, the default in install.sh) puts a standalone omp in %LOCALAPPDATA%\omp or ~/.local/bin, which `omp update` replaces. omp had no self updater; it has one now for those two places. Tried for real on the Windows box (Windows 11 x64), in a sandbox USERPROFILE/APPDATA/LOCALAPPDATA/BUN_INSTALL with bun 1.4.3 and `bun add -g opencode-ai @oh-my-pi/pi-coding-agent`: a probe built from this commit calling agent.Find(id).CLI() reports opencode and omp via "bun" with "bun.exe add -g opencode-ai@latest" / "...pi-coding-agent@latest" and their latest versions; the same probe built from b9c589ebb reports both with no via, no latest and no command, as the reporter saw. TestHowInstalledBunWindowsShim uses the .bunx bytes bun 1.4.3 wrote there; with bunxNames taken out (or the omp self updater) it fails with 4 errors. Ran on b9c589ebb plus these commits: gofmt -l; go vet (darwin, GOOS=linux -tags nogui, GOOS=windows); go test -tags nogui ./... (every package ok but two filed load flakes, TestMaxRPMWaitsForRoomInTheMinute #1791 and TestCopilotSecretToolUsername #1627, at load ~200; each passed -count=3 alone). - rebased onto e98440ae6 (27 more commits on main) and run again there: gofmt -l, go vet ./... (darwin, linux nogui, windows), go test -tags nogui ./internal/gateway ./internal/proc ./internal/agent. - then onto af27b70b4 (9 commits; gateway routing.go/gateway.go and agent codex.go touched, not these lines): gofmt -l, go vet on the three OSes, and the tests named above plus the new remote-magpie and Codex ones. - the version magpie shows after an update: on the box, `bun add -g opencode-ai@1.18.0` then `bun update -g --latest opencode-ai` (and a second `bun add -g`) each rewrite bin\opencode.exe and .bunx (new mtime), so installedVersion's stamp changes and 1.19.0 is read; nothing to change there. Not tried: omp's own -Binary install on a real machine (only the path rule is tested), and an update run from magpie's Agents page. --- internal/agent/cliupdate.go | 39 +++++++++++++++++++++++--- internal/agent/cliupdate_test.go | 48 ++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/internal/agent/cliupdate.go b/internal/agent/cliupdate.go index 45bc98268..3d1018dfd 100644 --- a/internal/agent/cliupdate.go +++ b/internal/agent/cliupdate.go @@ -25,6 +25,7 @@ import ( "strings" "sync" "time" + "unicode/utf16" "github.com/yetone/magpie/internal/edit" "github.com/yetone/magpie/internal/netproxy" @@ -141,8 +142,18 @@ var cliSpecs = map[string]cliSpec{ } return nil }}, - "pi": {npm: []string{"@earendil-works/pi-coding-agent", "@mariozechner/pi-coding-agent"}, brew: []string{"pi-coding-agent"}}, - "omp": {npm: []string{"@oh-my-pi/pi-coding-agent"}}, + "pi": {npm: []string{"@earendil-works/pi-coding-agent", "@mariozechner/pi-coding-agent"}, brew: []string{"pi-coding-agent"}}, + // omp: bun's global, or omp.sh's -Binary install, a binary of its own + // in ~/.local/bin (install, PI_INSTALL_DIR aside) or + // %LOCALAPPDATA%\omp (install.ps1), which omp update replaces with + // the release's, as the reporter of #2192 saw it do + "omp": {npm: []string{"@oh-my-pi/pi-coding-agent"}, + self: func(bin, real string) []string { + if l := strings.ToLower(real); strings.HasSuffix(l, "/.local/bin/omp") || strings.HasSuffix(l, "/appdata/local/omp/omp.exe") { + return []string{bin, "update"} + } + return nil + }}, "copilot": {npm: []string{"@github/copilot"}, brew: []string{"copilot-cli"}}, "crush": {npm: []string{"@charmland/crush"}, brew: []string{"crush"}}, "cline": {npm: []string{"cline"}, brew: []string{"cline"}}, @@ -310,12 +321,12 @@ func fromNodeModules(pkg, bin, real string) *updater { if !isDir(filepath.Join(filepath.FromSlash(modules), filepath.FromSlash(pkg))) { modules = "" // pnpm's shims name a store elsewhere } - } else { + } else if !bunxNames(bin, pkg) { return nil } lower := strings.ToLower(real + "|" + filepath.ToSlash(bin)) switch { - case strings.Contains(lower, "/.bun/"): + case strings.Contains(lower, "/.bun/") || modules == "" && bunxNames(bin, pkg): bun := filepath.Join(filepath.Dir(bin), exe("bun")) if !isFile(bun) { var err error @@ -369,6 +380,26 @@ func fromNodeModules(pkg, bin, real string) *updater { return u } +// bunxNames says whether bin is bun's Windows shim of pkg's program: a +// small opencode.exe that runs what the opencode.bunx beside it names, in +// UTF-16, from bun's folder: install\global\node_modules\opencode-ai\ +// bin\opencode.exe (bun 1.4.3, #2192). The .exe itself names no package. +func bunxNames(bin, pkg string) bool { + if !strings.EqualFold(filepath.Ext(bin), ".exe") { + return false + } + b, err := os.ReadFile(strings.TrimSuffix(bin, filepath.Ext(bin)) + ".bunx") + if err != nil || len(b) > 64<<10 { + return false + } + u := make([]uint16, 0, len(b)/2) + for i := 0; i+1 < len(b); i += 2 { + u = append(u, uint16(b[i])|uint16(b[i+1])<<8) + } + named := strings.ReplaceAll(string(utf16.Decode(u)), `\`, "/") + return strings.Contains(named, "node_modules/"+pkg+"/") +} + // shimNames says whether bin is a small script naming pkg's folder. func shimNames(bin, pkg string) bool { st, err := os.Stat(bin) diff --git a/internal/agent/cliupdate_test.go b/internal/agent/cliupdate_test.go index 54d2a91d1..cf97ef9db 100644 --- a/internal/agent/cliupdate_test.go +++ b/internal/agent/cliupdate_test.go @@ -570,3 +570,51 @@ func TestGrokBrewCask(t *testing.T) { t.Errorf("installer only: %+v", c) } } + +// bunxOf is a .bunx as bun 1.4.3 wrote it beside its Windows shims, on the +// Windows box after bun add -g opencode-ai @oh-my-pi/pi-coding-agent: the +// program's path from bun's folder in UTF-16, a quote, then bun's own +// bytes (omp's names bun as its runner). +func bunxOf(t *testing.T, path string, tail []byte) string { + t.Helper() + var b []byte + for _, r := range path + `"` { + b = append(b, byte(r), 0) + } + return string(append(append(b, 0, 0), tail...)) +} + +// opencode and omp bun installed on Windows are updated with bun: bun's +// shim there is an .exe naming no package, and the .bunx beside it names +// it (#2192, xiaozhu1337: opencode 2.0.24 and omp 18.6.1 with newer out, +// no update offered). omp's own -Binary install updates itself. +func TestHowInstalledBunWindowsShim(t *testing.T) { + bunDir := filepath.Join(t.TempDir(), "bun-home") // a BUN_INSTALL with no .bun in it + bun := file(t, filepath.Join(bunDir, "bin", exe("bun")), "MZ") + file(t, filepath.Join(bunDir, "install/global/node_modules/opencode-ai/bin/opencode.exe"), "MZ") + file(t, filepath.Join(bunDir, "install/global/node_modules/@oh-my-pi/pi-coding-agent/dist/cli.js"), "") + opencode := file(t, filepath.Join(bunDir, "bin/opencode.exe"), "MZ\x90\x00 node_modules") // bun's 8 KB shim + file(t, filepath.Join(bunDir, "bin/opencode.bunx"), bunxOf(t, `install\global\node_modules\opencode-ai\bin\opencode.exe`, []byte{0x30, 0xab})) + omp := file(t, filepath.Join(bunDir, "bin/omp.exe"), "MZ\x90\x00 node_modules") + file(t, filepath.Join(bunDir, "bin/omp.bunx"), bunxOf(t, `install\global\node_modules\@oh-my-pi\pi-coding-agent\dist\cli.js`, + []byte{'b', 0, 'u', 0, 'n', 0, ' ', 0, 0x82, 0, 0, 0, 8, 0, 0, 0, 0x35, 0xab})) + if u := howInstalled(cliSpecs["opencode"], opencode); u == nil || u.via != "bun" || !reflect.DeepEqual(u.cmd, []string{bun, "add", "-g", "opencode-ai@latest"}) { + t.Errorf("opencode's bun shim: %+v", u) + } + if u := howInstalled(cliSpecs["omp"], omp); u == nil || u.via != "bun" || !reflect.DeepEqual(u.cmd, []string{bun, "add", "-g", "@oh-my-pi/pi-coding-agent@latest"}) { + t.Errorf("omp's bun shim: %+v", u) + } + // a .bunx naming another package is not this one's + if u := howInstalled(cliSpecs["omp"], opencode); u != nil { + t.Errorf("opencode's shim taken as omp's: %+v", u) + } + + // omp.sh's binary install: %LOCALAPPDATA%\omp\omp.exe, ~/.local/bin/omp + home := t.TempDir() + for _, p := range []string{"AppData/Local/omp/omp.exe", ".local/bin/omp"} { + bin := file(t, filepath.Join(home, p), "MZ") + if u := howInstalled(cliSpecs["omp"], bin); u == nil || u.via != "self" || !reflect.DeepEqual(u.cmd, []string{bin, "update"}) || u.pkg != "@oh-my-pi/pi-coding-agent" { + t.Errorf("omp's binary at %s: %+v", p, u) + } + } +}