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).
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -2570,7 +2570,8 @@ var claudeManaged = func() string {
|
||||
// StandIn is the model Claude Code is set to use in place of one it named
|
||||
// that magpie doesn't serve: claude-haiku-4-5-… for a title or a small
|
||||
// task goes to its haiku tier's model, and a name of no tier to its main
|
||||
// model. For Codex, the model it is set to (codexStandIn). "" when the
|
||||
// model. For Codex, the model it is set to (codexStandIn), in a WSL
|
||||
// distro the distro's config names (wslCodexStandIn). "" when the
|
||||
// agent isn't routed through magpie or is neither. For gateway.StandIn.
|
||||
func StandIn(agent, model string) string {
|
||||
if agent != "claude" && agent != "codex" {
|
||||
@@ -2581,7 +2582,11 @@ func StandIn(agent, model string) string {
|
||||
return ""
|
||||
}
|
||||
if agent == "codex" {
|
||||
return codexStandIn(filepath.Join(here(home).codexHome(), "config.toml"))
|
||||
if m := codexStandIn(filepath.Join(here(home).codexHome(), "config.toml")); m != "" || runtime.GOOS != "windows" {
|
||||
return m
|
||||
}
|
||||
// a Codex in a WSL distro is known by the same client
|
||||
return wslCodexStandIn()
|
||||
}
|
||||
if m := claudeStandIn(filepath.Join(here(home).claudeDir(), "settings.json"), model); m != "" || runtime.GOOS != "windows" {
|
||||
return m
|
||||
|
||||
+30
-9
@@ -1288,24 +1288,32 @@ func wslLastSeen(name, key string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// wslClaudeStandIn is StandIn for a Claude Code in a WSL distro routed
|
||||
// through magpie: the first, of the distros running now, whose
|
||||
// settings.json has a stand-in. Only magpie's memory of them and which run
|
||||
// (wslRunningLocked) are asked — nothing is probed, and a stopped distro
|
||||
// is never opened.
|
||||
func wslClaudeStandIn(model string) string {
|
||||
// wslReady are the distros running now that the probe found kind in, each
|
||||
// with its mirrored networking settled, ready for a read of its files.
|
||||
// Only magpie's memory of them and which run (wslRunningLocked) are
|
||||
// asked — nothing is probed, and a stopped distro is never opened.
|
||||
func wslReady(kind string) []distro {
|
||||
wsl.Lock()
|
||||
wslRunningLocked()
|
||||
var ds []distro
|
||||
for _, n := range wsl.names {
|
||||
if d := wsl.seen[n]; d != nil && wsl.running[n] && wslKindOf("claude").found(*d) {
|
||||
if d := wsl.seen[n]; d != nil && wsl.running[n] && wslKindOf(kind).found(*d) {
|
||||
ds = append(ds, *d)
|
||||
}
|
||||
}
|
||||
wsl.Unlock()
|
||||
cfg := wslConfig()
|
||||
for _, d := range ds {
|
||||
d.mirror(cfg)
|
||||
for i := range ds {
|
||||
ds[i].mirror(cfg)
|
||||
}
|
||||
return ds
|
||||
}
|
||||
|
||||
// wslClaudeStandIn is StandIn for a Claude Code in a WSL distro routed
|
||||
// through magpie: the first, of the distros running now, whose
|
||||
// settings.json has a stand-in.
|
||||
func wslClaudeStandIn(model string) string {
|
||||
for _, d := range wslReady("claude") {
|
||||
path := filepath.Join(d.local(d.Home), ".claude", "settings.json")
|
||||
if m := claudeStandInAt(path, model, d.base()); m != "" {
|
||||
return m
|
||||
@@ -1336,6 +1344,19 @@ func wslClaudeClassifier(model string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// wslCodexStandIn is StandIn for a Codex in a WSL distro routed through
|
||||
// magpie: the first, of the distros running now, whose config.toml has
|
||||
// magpie as its provider, as this machine's codexStandIn reads it.
|
||||
func wslCodexStandIn() string {
|
||||
for _, d := range wslReady("codex") {
|
||||
path := filepath.Join(d.place("codex@wsl:"+d.Name).codexHome(), "config.toml")
|
||||
if m := codexStandIn(path); m != "" {
|
||||
return m
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// wslList is the names wsl.exe lists with args; ok is false when it
|
||||
// couldn't say (no WSL, or none installed).
|
||||
func wslList(args ...string) (names []string, ok bool) {
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user