Files
magpie/internal/agent/cliupdate_wsl_test.go
T
yetone d1b4eff5aa agent: version detection and updates for agents installed in WSL distros (mewcoder on X)
A WSL twin row (<kind>@wsl:<distro>) showed no CLI version and no Update
button; the host path's cliupdate machinery only knew host binaries.

Now a twin in a running distro is read and updated inside the distro, for
every cliSpecs kind that has a command (cliupdate_wsl.go):
- one `wsl.exe -d <distro> -e sh` probe prints the command's real path,
  --version and exit code, with its own folder, npm prefix, ~/.local/bin and
  ~/.bun/bin first on PATH (wsl.exe -e has no login PATH, node CLIs exit 127);
  kept 10 min in wslProbes, dropped by Rescan and after an update.
- wslHowInstalled applies the host rules to the Linux path: own updater
  (spec.self), Homebrew, npm, bun, pnpm; volta, yarn and unwritable prefixes
  are left alone.
- the update runs in the distro through wsl.exe (wslRunUpdate); a Linux
  brew's newest is asked in the distro too.
- a stopped distro is never started: no version, and an update says to
  start it. CLIs() lists twins, so Update all takes them in.
No GUI change: the Agents row is keyed by agent id.

Run: gofmt clean; go vet darwin, linux, windows on internal/agent;
go test ./internal/agent ok on mote. TestWSLAgentCLIVersionAndUpdate (fake
registry + fakeWSL distro with an nvm prefix) goes red with the CLI hook
removed from CLI(). Real: win-run on xiaomi-gpu, magpie-desktop's own codex
read 0.162.1, Update true, updated to 0.167.1.
internal/gui on mote: only TestPanelHiddenByWailsMarksPageOutOfSight failed
(AppKit test killed at 120s, mote load ~80; no GUI file touched).

Not tried: real brew in a distro, proxy env inside the distro, sudo-owned npm
folders, kinds without a bin (Goose, mu), starting a stopped distro. Host's
proxy settings are not carried into the distro.
2026-11-11 11:35:41 +08:00

112 lines
3.4 KiB
Go

package agent
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
// An agent installed inside a WSL distro has its version read and is
// updated in the distro (@mewcoder on X: "可以支持wsl里的agent版本检测和更新吗"),
// the way the host's is: by the npm that owns it, run through wsl.exe in the
// distro. A distro that isn't running is never asked.
func TestWSLAgentCLIVersionAndUpdate(t *testing.T) {
reg := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"version":"0.2.0"}`))
}))
oldReg, oldRun := npmRegistry, wslRunUpdate
npmRegistry = reg.URL + "/"
t.Cleanup(func() {
reg.Close()
npmRegistry, wslRunUpdate = oldReg, oldRun
versions, latests, wslProbes = memo{}, memo{}, memo{}
})
versions, latests, wslProbes = memo{}, memo{}, memo{}
root := t.TempDir()
os.MkdirAll(filepath.Join(root, "home", "me", ".codex"), 0o755)
const prefix = "/home/me/.nvm/versions/node/v22"
const bin = prefix + "/bin/codex"
const real = prefix + "/lib/node_modules/@openai/codex/bin/codex.js"
fakeWSL(t, "Ubuntu\r\nStopped\r\n", "Ubuntu\r\n", map[string]string{
"Ubuntu": "home:/home/me\ndir:.codex\nbin:codex " + bin + "\nroute:default via 172.20.0.1 dev eth0\n",
}, map[string]string{"Ubuntu": root})
var mu sync.Mutex
version, asked := "0.1.0", map[string]int{}
probe := wslRun
wslRun = func(d time.Duration, args ...string) ([]byte, error) {
mu.Lock()
defer mu.Unlock()
if len(args) > 4 && args[0] == "-d" && args[2] == "-e" {
asked[args[1]]++
switch args[3] {
case "test":
return nil, nil // writable, and npm is there
case "sh":
if args[4] == "-c" && strings.Contains(args[5], "readlink") {
return []byte("bin:" + bin + "\nreal:" + real + "\ncode:0\nver:codex-cli " + version + " \n"), nil
}
}
}
return probe(d, args...)
}
var ran []string
wslRunUpdate = func(_ time.Duration, u *updater) ([]byte, error) {
mu.Lock()
defer mu.Unlock()
ran = append([]string{u.wsl, u.path}, u.cmd...)
version = "0.2.0"
return nil, nil
}
ds := wslDistros()
var d distro
for _, x := range ds {
if x.Name == "Ubuntu" {
d = x
}
}
a := wslAgent(wslKindOf("codex"), d)
c, ok := a.CLI()
if !ok || c.Version != "0.1.0" || c.Via != "npm" || c.Latest != "0.2.0" || !c.Update || c.Path != bin {
t.Fatalf("CLI = %+v, %v", c, ok)
}
if !strings.Contains(c.Command, "npm install -g --prefix "+prefix) {
t.Fatalf("command %q", c.Command)
}
if got, _ := CLIs(5 * time.Second); got[a.ID].Version != "0.1.0" {
t.Fatalf("CLIs has no %s: %+v", a.ID, got)
}
c, err := a.UpdateCLI()
if err != nil {
t.Fatal(err)
}
if c.Version != "0.2.0" || c.Update {
t.Fatalf("after the update: %+v", c)
}
want := []string{"Ubuntu", prefix + "/bin", prefix + "/bin/npm", "install", "-g", "--prefix", prefix, "@openai/codex@latest"}
if strings.Join(ran, " ") != strings.Join(want, " ") {
t.Fatalf("ran %q, want %q", ran, want)
}
// a distro that isn't running is never asked
stopped := wslAgent(wslKindOf("codex"), distro{Name: "Stopped", Home: "/home/s", Has: map[string]bool{"dir:.codex": true}})
before := asked["Stopped"]
if _, ok := stopped.CLI(); ok {
t.Fatal("a stopped distro's CLI was read")
}
if _, err := stopped.UpdateCLI(); err == nil {
t.Fatal("a stopped distro was updated")
}
if asked["Stopped"] != before {
t.Fatalf("a stopped distro was asked %d times", asked["Stopped"]-before)
}
}