Files
magpie/internal/agent/wsl_fake.go
T
yetone da5f7a8115 wsl: background work never touches a WSL distro that has stopped since it was last seen running — it booted it again (TJHHHH on Discord: while they were repairing WSL, magpie kept starting it). Detection already listed only running distros, but the running set was kept for a minute (wslListAge), agents built while a distro ran kept its home and read its files from their Get/Sync/Check/Reached/Routed closures later, the sessions listing kept a distro's Running for 30s+ and read its \\wsl.localhost files, and wslrun.Find reused a tool it had found in a distro for a minute and ran wsl.exe -d on it. Any wsl.exe -d or \\wsl.localhost read boots a stopped distro, so a wsl --shutdown was undone within a minute, and the distro, running again, kept it going. Now wsl.exe -l --running -q (it starts nothing; UTF-16 with or without BOM, or UTF-8 under WSL_UTF8) is asked again after 3s, and a distro not in it is not touched: a WSL agent's Get gives the last values seen, Sync/Check/Reached do nothing and Routed/Joined/Beside/Added say no; sessions aren't listed or read from it (wslCold says cold); wslrun.Find (Claude subscription sign-in and usage) doesn't reuse or run a tool there; the Library gives skills to no target in it and its MCP probe says it isn't running. An explicit action still starts it: setting a WSL agent's field, a Set on a running agent, resuming a WSL session. TestWSLStoppedSinceNotOpened (agent, sessions), TestWSLStoppedSinceNoTarget (library) and TestFindStoppedSince (wslrun) fail without the change and pass; TestUp covers the -l --running parse. On the Windows box (10.0.0.132) a test binary that had seen magpie-fedora running ran Values/Check/Reached/wslDistros/wslAgents/sessions.List/wslrun.Find against it for 4 rounds while it was stopped: it stayed Stopped and wsl -l -v was unchanged; wslrun and agent WSL tests pass there. No distro was terminated for the check (magpie-desktop is in use, the stopped ones aren't ours). go test ./... (nogui), -race on the WSL tests, go vet, windows and linux (nogui) builds pass.
2026-10-06 14:25:41 +08:00

74 lines
1.9 KiB
Go

package agent
import (
"errors"
"strings"
"time"
)
// FakeWSL, for other packages' tests, stands in for wsl.exe on any
// system: each distro named in probes is installed and running, answers
// the probe with what probes has for it (wslProbeScript's output: "home:/
// home/me\ndir:.codex\n…") and is opened at roots' folder for it. What
// magpie remembers of WSL is forgotten, before and after; the returned
// func puts everything back.
func FakeWSL(probes, roots map[string]string) func() {
on, run, root := wslOn, wslRun, wslRoot
reset := func() {
wsl.Lock()
wsl.seen, wsl.at, wsl.runAt, wsl.names, wsl.running, wsl.probed, wsl.failed, wsl.dirty = nil, time.Time{}, time.Time{}, nil, nil, nil, nil, false
wsl.Unlock()
}
reset()
var names []string
for n := range probes {
names = append(names, n)
}
list := []byte(strings.Join(names, "\n"))
stopped := map[string]bool{}
fakeStop = func(name string) {
wsl.Lock()
stopped[name] = true
wsl.runAt = time.Time{}
wsl.Unlock()
}
wslOn = true
wslRun = func(_ time.Duration, args ...string) ([]byte, error) {
switch strings.Join(args, " ") {
case "-l -q":
return list, nil
case "-l --running -q":
var run []string
for _, n := range names {
if !stopped[n] {
run = append(run, n)
}
}
return []byte(strings.Join(run, "\n")), nil
}
if len(args) > 2 && args[0] == "-d" {
if p, ok := probes[args[1]]; ok {
return []byte(p), nil
}
}
return nil, errors.New("unexpected wsl.exe " + strings.Join(args, " "))
}
wslRoot = func(name string) string { return roots[name] }
return func() {
wslOn, wslRun, wslRoot = on, run, root
fakeStop = nil
reset()
}
}
var fakeStop func(string)
// StopFakeWSL, for other packages' tests, has FakeWSL's distro name stop,
// as the user stopping it (wsl --shutdown) does: wsl.exe lists it as not
// running from now on.
func StopFakeWSL(name string) {
if fakeStop != nil {
fakeStop(name)
}
}