Files
magpie/internal/proc/wsl_test.go
T
yetone e6cdeac9c6 proc: with no WSL distro registered, or after wsl.exe asks to be updated, magpie starts no wsl.exe (TraderX219 on X)
TraderX219 on Windows: while magpie ran, a console window "WSL 必须更新到
最新版本……按任意键安装" came up every 20 to 30 seconds. magpie asked
wsl.exe for its distros (the running list retries every 30s after a
failure), and Windows' inbox wsl.exe answers each run on a computer
without a current WSL package by starting `wsl.exe --update --confirm
--prompt-before-exit` in a console of its own.

proc.Command, CommandContext and ProbeContext are where every wsl.exe
magpie runs is made (agent's wslRun/wslExec, wslrun, claudetui, the
library's rtk and MCP checks, the gateway's TUI helper), so both gates
sit in wslOff beside #1264's ErrWSLOff:

- The registry is read first (HKCU\...\CurrentVersion\Lxss, one subkey
  with a DistributionName per distro). With the key there or gone and no
  distro in it, wsl.exe isn't started: ErrNoWSLDistro. agent's wslList
  and wslrun's running list read that as "no distros", not as a failure
  or a hang, so nothing is retried every 30s. It is read at every start,
  so a distro installed later is seen at once. A read that fails is
  unknown: wsl.exe is asked as before.
- When a run's output (UTF-16LE or UTF-8, stdout and stderr) says WSL
  must be updated or installed, proc.WSLSaid writes settings' wsl-update
  mark (atomic, like wsl-hung) and no magpie process starts wsl.exe for
  6h: ErrWSLNeedsUpdate. The mark holds the WSL package version (HKLM
  Lxss\MSI Version) and the registered distros; a change to either
  clears it. A refused listing is unknown, so wsl.seen, WSLKept values
  and links are kept, never removed.

docs/subsystems/agent-wiring.md describes both.

Run:
- mote: gofmt -l (clean); go vet ./..., GOOS=linux go vet -tags nogui
  ./..., GOOS=windows go vet ./...; go build for darwin, linux (nogui),
  windows: all ok.
- mote: go test -count=1 of internal/proc, agent, wslrun, library,
  sessions, claudetui at base 191f3c0af: all ok.
- mote, full suite through suite.lock (go test -count=1 -timeout 45m
  ./...) at base 191f3c0af: 88 packages ok, none failed.
- xiaomi-gpu (Windows 11, win-run): go test -count=1 ./internal/agent/...
  ./internal/proc/... ./internal/wslrun/...: proc and wslrun ok;
  agent has 25 reds, the same 25 by name on untouched origin/main
  191f3c0af. 24 were filed (#4115, #4786, #5068 and others);
  TestCodexOwnModelsFollowPicks wasn't, now #5605.
- xiaomi-gpu, read-only: TestWSLRegistryLive (MAGPIE_WSL_REGISTRY_LIVE=1)
  read the real Lxss key (magpie-fedora, Ubuntu, docker-desktop,
  magpie-3896, magpie-desktop; package 2.6.1.0) and ran `wsl.exe -l -q`
  (docker-desktop, magpie-fedora, Ubuntu, magpie-desktop): every listed
  distro is registered, so the gate never reads none while wsl.exe has
  some. magpie-3896 is registered in State 4 (uninstalling) and not
  listed, which only keeps wsl.exe being asked. No distro was stopped or
  changed.
- At the pushed head (rebased on f13d5f16f, which touches gateway and
  provider only), mote: go test of proc, wslrun, provider ok. agent's
  TestWSLMoreAgentsPick failed with "no space left on device" (mote's
  data volume was at 100%). Rerun: -count=3 -run 'TestWSLMoreAgents|WSL'
  ./internal/agent/ ok, and -run History ./internal/gateway/ ok. abe7b0063 (gateway) and
  e4fdf9ed3 (gui) landed after that run and were rebased onto unrun; they
  touch no file this change does.

Broken by hand, each red:
- registry check off: TestNoWSLDistroStartsNoWSL ("wsl.exe wasn't
  refused") and TestNoWSLDistroRunsNoWSL ("no distro registered read as
  wsl.exe failing").
- WSLSaid left out of agent's wslRun: TestWSLUpdateKeepsDistros ("wsl.exe
  -l -q was started: exit status 1").
- wslList's ErrNoWSLDistro as listed: TestNoWSLDistroRunsNoWSL.
- wslUpdateLately always false: TestWSLUpdateBacksOff ("wsl.exe run again
  after it asked for an update").

Not tried:
- The stub's window itself: xiaomi-gpu has a current WSL and distros, so
  it doesn't pop there. The fix wasn't run on the reporter's machine.
- The registry key missing (ErrNotExist) on a real Windows without WSL.
- The stub's real exit code and stdout: the update text is matched from
  the inbox wsl.exe's .mui strings (en, zh-CN) and the reporter's words.
- A wsl.exe that times out is not taken as asking for an update; the
  existing wsl-hung back-off still covers it.
2026-11-07 17:51:51 +08:00

195 lines
6.7 KiB
Go

package proc
import (
"context"
"encoding/json"
"errors"
"os"
"os/exec"
"testing"
"time"
"unicode/utf16"
"github.com/yetone/magpie/internal/settings"
)
// #4872: with Settings' Detect agents in WSL off, nothing magpie makes
// starts wsl.exe, by any name or path: a request's stand-in asked it past
// agent's gates, and on a computer without WSL each ask put up a window
// offering to install it.
func TestWSLOffStartsNoWSL(t *testing.T) {
t.Cleanup(func() { settings.Save(settings.Settings{}) })
// a distro registered: only the setting refuses it
defer FakeWSLRegistry([]string{"Ubuntu"}, true, "")()
names := []string{"wsl.exe", "WSL.EXE", "wsl", `C:\Windows\System32\wsl.exe`, "/mnt/c/Windows/System32/wsl.exe"}
// each is only started while it is refused: on, wsl.exe isn't run
makers := map[string]func(name string) *exec.Cmd{
"Command": func(n string) *exec.Cmd { return Command(n, "-l", "--running", "-q") },
"CommandContext": func(n string) *exec.Cmd {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel)
return CommandContext(ctx, n, "-l", "-q")
},
"ProbeContext": func(n string) *exec.Cmd {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel)
return ProbeContext(ctx, n, "-l", "-q")
},
}
if err := settings.Save(settings.Settings{NoWSLAgents: true}); err != nil {
t.Fatal(err)
}
for how, mk := range makers {
for _, n := range names {
c := mk(n)
if !errors.Is(c.Err, ErrWSLOff) {
t.Errorf("detection off, %s(%q) wasn't refused: %v", how, n, c.Err)
continue
}
if _, err := c.Output(); !errors.Is(err, ErrWSLOff) || c.Process != nil {
t.Errorf("detection off, %s(%q) started: %v", how, n, err)
}
}
// only wsl.exe itself
for _, n := range []string{"wslconfig.exe", "notwsl.exe", `C:\wsl\git.exe`} {
if c := mk(n); errors.Is(c.Err, ErrWSLOff) {
t.Errorf("%s(%q) refused as wsl.exe", how, n)
}
}
}
if err := settings.Save(settings.Settings{}); err != nil {
t.Fatal(err)
}
for how, mk := range makers {
if c := mk("wsl.exe"); errors.Is(c.Err, ErrWSLOff) {
t.Errorf("detection on, %s(wsl.exe) refused", how)
}
}
}
// TraderX219 on X: with no WSL distro registered, nothing magpie makes
// starts wsl.exe, which on a computer without WSL's package put up a
// console window asking to install it every 20 to 30 seconds. The registry
// that can't be read, or that has a distro, starts it as before, and a
// distro registered later is run at once.
func TestNoWSLDistroStartsNoWSL(t *testing.T) {
defer FakeWSLRegistry(nil, true, "")()
for _, n := range []string{"wsl.exe", "WSL.EXE", `C:\Windows\System32\wsl.exe`} {
c := Command(n, "-l", "--running", "-q")
if !errors.Is(c.Err, ErrNoWSLDistro) {
t.Fatalf("no distro, %q wasn't refused: %v", n, c.Err)
}
if _, err := c.Output(); !errors.Is(err, ErrNoWSLDistro) || c.Process != nil {
t.Fatalf("no distro, %q started: %v", n, err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if c := ProbeContext(ctx, n, "-l", "-q"); !errors.Is(c.Err, ErrNoWSLDistro) {
t.Fatalf("no distro, probe of %q wasn't refused: %v", n, c.Err)
}
cancel()
}
if c := Command("git", "version"); c.Err != nil {
t.Fatalf("git refused: %v", c.Err)
}
defer FakeWSLRegistry(nil, false, "")()
if c := Command("wsl.exe", "-l", "-q"); c.Err != nil {
t.Fatalf("registry unread, wsl.exe refused: %v", c.Err)
}
defer FakeWSLRegistry([]string{"Ubuntu"}, true, "")()
if c := Command("wsl.exe", "-l", "-q"); c.Err != nil {
t.Fatalf("a distro registered, wsl.exe refused: %v", c.Err)
}
}
func utf16le(s string) []byte {
var b []byte
for _, r := range utf16.Encode([]rune(s)) {
b = append(b, byte(r), byte(r>>8))
}
return b
}
// A wsl.exe that says WSL must be updated isn't run again for hours, by
// any route, unless WSL's package or distros change; a failure that says
// something else, or a run that succeeded, holds nothing back.
func TestWSLUpdateBacksOff(t *testing.T) {
t.Cleanup(func() { os.Remove(wslUpdatePath()) })
defer FakeWSLRegistry([]string{"Ubuntu"}, true, "2.6.1.0")()
refused := func() bool {
return errors.Is(Command("wsl.exe", "-l", "--running", "-q").Err, ErrWSLNeedsUpdate)
}
fail := errors.New("exit status 1")
// what doesn't ask for an update
WSLSaid([]byte("run wsl.exe --update yourself\n"), nil)
WSLSaid(utf16le("There is no distribution with the supplied name.\r\nError code: Wsl/Service/WSL_E_DISTRO_NOT_FOUND\r\n"), fail)
WSLSaid(nil, ErrNoWSLDistro)
if refused() {
t.Fatal("refused after runs that didn't ask for an update")
}
says := map[string][]byte{
"utf-16": utf16le("Windows Subsystem for Linux must be updated to the latest version to proceed. You can update by running 'wsl.exe --update'.\r\n"),
// the reporter's words, as the screenshot has them
"chinese": utf16le("\ufeffWSL 必须更新到最新版本……按任意键安装\r\n"),
"prompt": []byte("\r\n按任意键安装适用于 Linux 的 Windows 子系统。\r\n按 CTRL-C 或关闭此窗口以取消。\r\n"),
"stderr": nil,
}
for name, out := range says {
os.Remove(wslUpdatePath())
err := fail
if out == nil {
err = &exec.ExitError{Stderr: []byte("This application requires the Windows Subsystem for Linux Optional Component.\nInstall it by running: wsl.exe --install --no-distribution\n")}
}
WSLSaid(out, err)
if !refused() {
t.Fatalf("%s: wsl.exe run again after it asked for an update", name)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if c := CommandContext(ctx, "wsl.exe", "-d", "Ubuntu", "-e", "true"); !errors.Is(c.Err, ErrWSLNeedsUpdate) {
t.Fatalf("%s: a run in a distro wasn't refused: %v", name, c.Err)
}
cancel()
}
// the package updated, or a distro added: asked again at once
restore := FakeWSLRegistry([]string{"Ubuntu"}, true, "2.6.2.0")
if refused() {
t.Fatal("refused after WSL's package changed")
}
restore()
restore = FakeWSLRegistry([]string{"Ubuntu", "Debian"}, true, "2.6.1.0")
if refused() {
t.Fatal("refused after a distro was added")
}
restore()
if !refused() {
t.Fatal("not refused for WSL as it was")
}
// the registry unread: the mark stands
restore = FakeWSLRegistry(nil, false, "")
if !refused() {
t.Fatal("registry unread, the mark didn't stand")
}
restore()
// its age runs out
b, _ := json.Marshal(wslUpdateMark{At: time.Now().Add(-wslUpdateAge - time.Minute), Sig: wslSig([]string{"Ubuntu"}, true)})
if err := os.WriteFile(wslUpdatePath(), b, 0o600); err != nil {
t.Fatal(err)
}
if refused() {
t.Fatal("refused past wslUpdateAge")
}
// a mark that doesn't parse isn't one
if err := os.WriteFile(wslUpdatePath(), []byte("{"), 0o600); err != nil {
t.Fatal(err)
}
if refused() {
t.Fatal("refused for a mark that doesn't parse")
}
}