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) + } + } +}