Files
magpie/internal/agent/wsl_codex_stand_test.go
T
ggbdpq 90258cf20a agent: a Codex in a WSL distro stands in for a model it names from its own config.toml (#2198)
StandIn("codex", …) read this machine's ~/.codex/config.toml alone, so a
Codex wired in a WSL distro (model_provider = "magpie", the only shape a
distro under NAT is wired in since #2145) naming a model magpie doesn't
serve (codex -m <unserved>) was answered 404, where this machine's Codex
goes to its config's model and a Claude Code in the same distro goes to
the stand-in its settings name (wslClaudeStandIn, #254). On Windows, when
this machine's Codex names none, StandIn now asks wslCodexStandIn: the
first distro running now whose config.toml has magpie as its provider,
read as codexStandIn reads it. The collecting of running distros the
probe found a kind in is shared with wslClaudeStandIn (wslReady); a
stopped distro is never opened. Off Windows nothing changes.

Reported with a patch by ggbdpq (gua2100 on Discord), independently
re-run by Edward4226. Taken as written except: the distro's config path
comes from the place's codexHome (where magpie writes it) rather than a
spelled-out ~/.codex; wslClaudeClassifier, which landed since with the
same loop, is left as it is (no test covers it); and TestWSLCodexStandIn
now also calls StandIn itself, so the Windows-only wiring is tested.

Run (sandbox HOME, caches pinned):
- go test -tags nogui -run 'TestWSLCodexStandIn|TestWSLClaude|TestCodexStandIn|TestClaudeStandIn' ./internal/agent: ok.
- Broken by hand: wslCodexStandIn returning "" fails TestWSLCodexStandIn
  ("stand-in: \"\""); wslReady without the running filter fails
  TestWSLCodexStandInSkipsStopped ("stand-in from a stopped distro:
  \"stopped/model\""). Restored: ok.
- On the Windows box (agent test binary built GOOS=windows): the same
  five tests pass; built with StandIn's codex branch put back,
  TestWSLCodexStandIn fails "StandIn on windows: \"\", want \"relay/main\"".
- go test -tags nogui -run 'WSL|StandIn|Wsl' ./internal/agent: one red,
  TestWSLColdPlaceOpensNothing (Hermes profiles of a stopped distro),
  red the same at origin/main 7f0f71a0e and filed as #3924.
- gofmt -l clean; go vet darwin, GOOS=windows, GOOS=linux -tags nogui on
  ./internal/agent: clean.

Not tried: a real Codex in a real WSL distro naming an unserved model
(the distros are fakes, on the Windows box too); a Windows Codex wired to
another model still wins over the distro's, as for Claude Code; the full
./... suite (one suite per machine, others running).
2026-10-28 00:47:44 +08:00

98 lines
3.3 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"testing"
)
// codexDistroHome is a distro's / in a temp dir with a user whose ~/.codex
// holds config (none when ""); magpie's own HOME is another temp dir.
func codexDistroHome(t *testing.T, config string) (root, home string) {
t.Helper()
syncHome(t)
root = t.TempDir()
home = filepath.Join(root, "home", "me")
os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
if config != "" {
os.WriteFile(filepath.Join(home, ".codex", "config.toml"), []byte(config), 0o644)
}
return root, home
}
// A Codex in a running distro, routed through magpie, stands in for a model
// it names that magpie doesn't serve, as this machine's does and a Claude
// Code in the distro does (wslClaudeStandIn): the first distro running now
// whose config.toml has magpie as its provider. One whose config isn't
// magpie's has none.
func TestWSLCodexStandIn(t *testing.T) {
root, home := codexDistroHome(t, "")
cfg := filepath.Join(home, ".codex", "config.toml")
fakeWSL(t, "Debian\r\n", "Debian\r\n", map[string]string{
"Debian": "home:/home/me\ndir:.codex\nroute:default via 172.20.0.1 dev eth0\n",
}, map[string]string{"Debian": root})
ds := wslDistros()
if len(ds) != 1 || !ds[0].Running {
t.Fatalf("%+v", ds)
}
os.WriteFile(cfg, []byte("model_provider = \"magpie\"\nmodel = \"relay/main\"\n"), 0o644)
if got := wslCodexStandIn(); got != "relay/main" {
t.Fatalf("stand-in: %q", got)
}
// StandIn asks the distro only on Windows, and only when this
// machine's Codex names none (its HOME here has no config.toml)
want := ""
if runtime.GOOS == "windows" {
want = "relay/main"
}
if got := StandIn("codex", "codex-unserved"); got != want {
t.Fatalf("StandIn on %s: %q, want %q", runtime.GOOS, got, want)
}
os.WriteFile(cfg, []byte("model = \"gpt-5.5\"\n"), 0o644)
if got := wslCodexStandIn(); got != "" {
t.Fatalf("not magpie's provider: %q", got)
}
}
// A distro that has stopped since magpie saw it running is never opened for
// a stand-in, however wired its config on disk is (da5f7a811: a `wsl
// --shutdown` is not undone by background work).
func TestWSLCodexStandInSkipsStopped(t *testing.T) {
root, home := codexDistroHome(t, "model_provider = \"magpie\"\nmodel = \"stopped/model\"\n")
cfg := filepath.Join(home, ".codex", "config.toml")
before, err := os.Stat(cfg)
if err != nil {
t.Fatal(err)
}
seen := map[string]*distro{
"Debian": {Name: "Debian", Home: "/home/me", Root: root, Has: map[string]bool{"dir:.codex": true},
Values: map[string]string{"model": "stopped/model"}},
}
b, err := json.Marshal(seen)
if err != nil {
t.Fatal(err)
}
os.MkdirAll(filepath.Dir(wslStatePath()), 0o755)
os.WriteFile(wslStatePath(), b, 0o600)
other := t.TempDir()
fakeWSL(t, "Debian\r\nOther\r\n", "Other\r\n", map[string]string{
"Other": "home:/home/o\ndir:.codex\n",
}, map[string]string{"Other": other})
ds := wslDistros()
if len(ds) != 2 || ds[0].Name != "Debian" || ds[0].Running || ds[1].Name != "Other" || !ds[1].Running {
t.Fatalf("%+v", ds)
}
if got := wslCodexStandIn(); got != "" {
t.Fatalf("stand-in from a stopped distro: %q", got)
}
after, err := os.Stat(cfg)
if err != nil {
t.Fatal(err)
}
if !after.ModTime().Equal(before.ModTime()) || readFile(cfg) != "model_provider = \"magpie\"\nmodel = \"stopped/model\"\n" {
t.Fatal("a stopped distro's config was touched")
}
}