Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

agent: a hung wsl.exe no longer holds every Windows command 20s

yetone6d24d4f4ea97
diff --git a/internal/agent/agents.go b/internal/agent/agents.go
index 3685a3cc1..03f2558c4 100644
--- a/internal/agent/agents.go
+++ b/internal/agent/agents.go
@@ -132,6 +132,21 @@ func homes() (home, cfg string) {
// builtins are the agents magpie has wiring of its own for.
func builtins(home, cfg string) []*Agent {
+ return append(onMachine(home, cfg), wslAgents()...)
+}
+
+// local is All() without the agents in WSL distros, which wsl.exe is asked
+// for: what start-up moves look in, on every command, `magpie version`
+// too, and the moves came before WSL agents. A wsl.exe that hangs held each
+// command 20s, its two listings' timeouts.
+func local() []*Agent {
+ home, cfg := homes()
+ own := onMachine(home, cfg)
+ return append(own, pluggedAgents(home, own, nil)...)
+}
+
+// onMachine are builtins but those in WSL.
+func onMachine(home, cfg string) []*Agent {
return append([]*Agent{
claude(home),
claudeDesktop(home),
@@ -201,7 +216,7 @@ func builtins(home, cfg string) []*Agent {
cindy(),
aionui(),
lody(home),
- }, append(ompProfiles(home), wslAgents()...)...)
+ }, ompProfiles(home)...)
}
// ---- accessors -------------------------------------------------------------
diff --git a/internal/agent/legacy.go b/internal/agent/legacy.go
index c93e5c92e..d6b1b8866 100644
--- a/internal/agent/legacy.go
+++ b/internal/agent/legacy.go
@@ -21,7 +21,7 @@ const legacyID = "dial"
// swallowed on purpose — a file magpie cannot rewrite is one the user can
// still fix from the picker.
func RenameLegacy() {
- for _, a := range All() {
+ for _, a := range local() {
if _, err := os.Stat(a.Path); err != nil {
continue
}
@@ -78,7 +78,7 @@ func moveEfforts(pid string, base func(string) (string, string, bool)) {
offers := func(opts []Option, v string) bool {
return slices.ContainsFunc(opts, func(o Option) bool { return o.Value == v })
}
- for _, a := range All() {
+ for _, a := range local() {
if _, err := os.Stat(a.Path); err != nil {
continue
}
diff --git a/internal/agent/wsl.go b/internal/agent/wsl.go
index e79c13833..fb1b1ccc4 100644
--- a/internal/agent/wsl.go
+++ b/internal/agent/wsl.go
@@ -1060,6 +1060,30 @@ var wslRoot = func(name string) string {
return `\\wsl$\` + name // before Windows 11 / WSL 0.50
}
+// wslHungPath, while recent, says a wsl.exe listing timed out: each
+// command a hung WSL held for its 10s timeout (20s with the running list),
+// `magpie providers` for Copilot's sign-in in a distro, `magpie version`
+// for start-up's moves.
+func wslHungPath() string { return filepath.Join(filepath.Dir(provider.Path()), "wsl-hung") }
+
+// wslHungAfter: a failure this slow is a hang, not no WSL; a var for tests.
+var wslHungAfter = 5 * time.Second
+
+const wslHungAge = 10 * time.Minute // how long another process takes it as hung
+
+func wslHungLately() bool {
+ fi, err := os.Stat(wslHungPath())
+ return err == nil && time.Since(fi.ModTime()) < wslHungAge
+}
+
+func wslHung(hung bool) {
+ if hung {
+ _ = os.WriteFile(wslHungPath(), []byte(time.Now().UTC().Format(time.RFC3339)+"\n"), 0o600)
+ } else {
+ _ = os.Remove(wslHungPath())
+ }
+}
+
func wslStatePath() string { return filepath.Join(filepath.Dir(provider.Path()), "wsl.json") }
func wslDistros() []distro {
@@ -1073,8 +1097,21 @@ func wslDistros() []distro {
wslForgetOldBins()
}
if time.Since(wsl.at) > wslListAge {
- wsl.names, wsl.listed = wslList("-l", "-q")
+ if wsl.at.IsZero() && wslHungLately() {
+ // another magpie waited out a hung wsl.exe moments ago: a
+ // command doesn't wait 10s more (this one lists again in a minute)
+ wsl.names, wsl.listed = nil, false
+ } else {
+ start := time.Now()
+ wsl.names, wsl.listed = wslList("-l", "-q")
+ wslHung(!wsl.listed && time.Since(start) >= wslHungAfter)
+ }
wsl.at = time.Now()
+ if !wsl.listed || len(wsl.names) == 0 {
+ // none to run: a wsl.exe that couldn't list them isn't asked
+ // again which run, another timeout under the lock the GUI waits on
+ wsl.running, wsl.runOK, wsl.runAt = map[string]bool{}, wsl.listed, time.Now()
+ }
}
wslRunningLocked()
installed := map[string]bool{}
diff --git a/internal/agent/wsl_startup_test.go b/internal/agent/wsl_startup_test.go
new file mode 100644
index 000000000..f3938b62b
--- /dev/null
+++ b/internal/agent/wsl_startup_test.go
@@ -0,0 +1,108 @@
+package agent
+
+import (
+ "errors"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+ "time"
+)
+
+// Every command ran start-up's moves over All(), which lists WSL's
+// distros: on a Windows whose wsl.exe hangs, `magpie version` took 20s,
+// two listings' timeouts. The moves look at this machine's agents only.
+func TestStartUpMovesAskNoWSL(t *testing.T) {
+ syncHome(t)
+ t.Cleanup(FakeWSL(map[string]string{"Ubuntu": "home:/home/me\ndir:.codex\n"}, map[string]string{"Ubuntu": t.TempDir()}))
+ var asked []string
+ run := wslRun
+ wslRun = func(d time.Duration, args ...string) ([]byte, error) {
+ asked = append(asked, strings.Join(args, " "))
+ return run(d, args...)
+ }
+ RenameLegacy()
+ MoveCursorEfforts()
+ MoveAntigravityEfforts()
+ if len(asked) != 0 {
+ t.Fatalf("start-up asked wsl.exe %q", asked)
+ }
+ // All() still has the distro's agents
+ n := 0
+ for _, a := range All() {
+ if a.WSL != "" {
+ n++
+ }
+ }
+ if n == 0 || len(asked) == 0 {
+ t.Fatalf("All() found %d WSL agents, asked %q", n, asked)
+ }
+}
+
+// A wsl.exe that can't list the distros isn't asked which of them run:
+// that is a second timeout, under the lock the GUI waits on.
+func TestWSLListFailsAsksNoRunning(t *testing.T) {
+ fakeWSL(t, "", "", nil, nil)
+ var asked []string
+ wslRun = func(_ time.Duration, args ...string) ([]byte, error) {
+ asked = append(asked, strings.Join(args, " "))
+ return nil, errors.New("timed out")
+ }
+ wslDistros()
+ if len(asked) != 1 || asked[0] != "-l -q" {
+ t.Fatalf("asked %q", asked)
+ }
+ // and not again on the next look within the failed age
+ wslUp("Ubuntu")
+ if len(asked) != 1 {
+ t.Fatalf("asked %q", asked)
+ }
+}
+
+// A wsl.exe that hung to its timeout is remembered beside magpie's
+// settings: the next command (a new process) doesn't wait on it again,
+// and once a listing answers, it is forgotten.
+func TestWSLHangRememberedAcrossProcesses(t *testing.T) {
+ fakeWSL(t, "", "", nil, nil)
+ after := wslHungAfter
+ wslHungAfter = 0
+ t.Cleanup(func() { wslHungAfter = after; os.Remove(wslHungPath()) })
+ os.MkdirAll(filepath.Dir(wslHungPath()), 0o755)
+ asks := 0
+ hung := true
+ wslRun = func(_ time.Duration, args ...string) ([]byte, error) {
+ asks++
+ if hung {
+ return nil, errors.New("timed out")
+ }
+ return []byte("Ubuntu\n"), nil
+ }
+ newProcess := func() {
+ wsl.Lock()
+ wsl.at, wsl.runAt, wsl.names, wsl.running = time.Time{}, time.Time{}, nil, nil
+ wsl.Unlock()
+ }
+ wslDistros()
+ if asks != 1 || !wslHungLately() {
+ t.Fatalf("asks %d, remembered %v", asks, wslHungLately())
+ }
+ newProcess()
+ wslDistros()
+ if asks != 1 {
+ t.Fatalf("a new process asked a hung wsl.exe again: %d", asks)
+ }
+ // it is asked again once the mark is old, and an answer clears it
+ old := time.Now().Add(-wslHungAge - time.Minute)
+ if err := os.Chtimes(wslHungPath(), old, old); err != nil {
+ t.Fatal(err)
+ }
+ hung = false
+ newProcess()
+ wslDistros()
+ if asks < 2 || wslHungLately() {
+ t.Fatalf("asks %d, remembered %v", asks, wslHungLately())
+ }
+ if _, err := os.Stat(wslHungPath()); !os.IsNotExist(err) {
+ t.Fatalf("mark kept: %v", err)
+ }
+}