agents: whether WSL is in mirrored networking is asked of WSL itself (wslinfo --networking-mode), and a UTF-16 .wslconfig is read (lgtm on Discord: told "WSL Ubuntu isn't in mirrored networking … pointed at Windows (http://192.168.16.1:3425)", while their WSL has always been in Mirrored) — magpie decided it by %UserProfile%\.wslconfig alone, read as UTF-8: a .wslconfig Notepad saved as "Unicode" (UTF-16, which WSL reads) said nothing, a "[wsl2] # comment" header ended the section, and mirrored set anywhere magpie didn't read was missed — so a really mirrored distro was pointed at its default route, which under mirrored is the LAN router, not Windows. The probe each running distro already gets now also prints wslinfo --networking-mode (WSL 2.0 on), kept as the distro's Net ("mirrored", "nat", …) and deciding Mirrored; .wslconfig decides only when there is no wslinfo, and is read as UTF-8, UTF-16LE (BOM or not) or UTF-16BE. A distro seen stopped is probed again once it runs, so WSL restarted in another mode is seen without restarting magpie. The notice says what it knows: "WSL Ubuntu is in nat networking, not mirrored, …", or, without wslinfo, that magpie couldn't ask and .wslconfig doesn't set it — never "isn't in mirrored" on .wslconfig's word alone. TestWSLConfigUTF16 (UTF-8, its BOM, UTF-16LE with and without BOM, UTF-16BE; fails without the change for the UTF-16 three), TestParseProbeNet, TestWSLNetworkingFromWslinfo (wslinfo mirrored with no .wslconfig is 127.0.0.1; wslinfo nat beats a .wslconfig saying mirrored; the fallback either way and its notice) and TestWSLReprobedAfterStop are new. The probe run through the real wsl.exe on a Windows 11 box (WSL 2.6.1, a NAT distro) read net:nat. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass.
This commit is contained in:
+77
-25
@@ -29,9 +29,10 @@ import (
|
||||
// stopped), and edits the files through \\wsl.localhost\<distro>. Each is
|
||||
// an agent of its own, <id>@wsl:<distro> (codex@wsl:Ubuntu,
|
||||
// pi@wsl:Ubuntu, claude@wsl:Ubuntu). The gateway it is pointed at is
|
||||
// 127.0.0.1 when WSL shares Windows' network (networkingMode=mirrored in
|
||||
// .wslconfig); under NAT it is Windows as WSL sees it, which reaches the
|
||||
// gateway only while that listens beyond loopback.
|
||||
// 127.0.0.1 when WSL shares Windows' network (mirrored networking, as the
|
||||
// distro's wslinfo says, or .wslconfig where there is no wslinfo); under
|
||||
// NAT it is Windows as WSL sees it, which reaches the gateway only while
|
||||
// that listens beyond loopback.
|
||||
|
||||
// place is where an agent lives: its home as magpie opens it, how a path
|
||||
// there is spelt in the agent's own config, and the gateway as it reaches
|
||||
@@ -113,15 +114,19 @@ func (p place) key(k string) string {
|
||||
|
||||
// distro is one WSL distro, as probed.
|
||||
type distro struct {
|
||||
Name string `json:"name"`
|
||||
Home string `json:"home"` // $HOME inside it, e.g. /home/me
|
||||
Root string `json:"root"` // where magpie opens its / from, e.g. \\wsl.localhost\Ubuntu
|
||||
Has map[string]bool `json:"has"` // "dir:.codex", "bin:pi": what the probe found
|
||||
Probe int `json:"probe,omitempty"` // the wslProbeVersion that found it
|
||||
Gateway string `json:"gateway,omitempty"` // the Windows host as the distro reaches it, when not mirrored
|
||||
Values map[string]string `json:"values,omitempty"` // its agents' fields as last read (wslKind.memo), shown while it is stopped
|
||||
Mirrored bool `json:"-"`
|
||||
Running bool `json:"-"`
|
||||
Name string `json:"name"`
|
||||
Home string `json:"home"` // $HOME inside it, e.g. /home/me
|
||||
Root string `json:"root"` // where magpie opens its / from, e.g. \\wsl.localhost\Ubuntu
|
||||
Has map[string]bool `json:"has"` // "dir:.codex", "bin:pi": what the probe found
|
||||
Probe int `json:"probe,omitempty"` // the wslProbeVersion that found it
|
||||
Gateway string `json:"gateway,omitempty"` // the Windows host as the distro reaches it, when not mirrored
|
||||
Values map[string]string `json:"values,omitempty"` // its agents' fields as last read (wslKind.memo), shown while it is stopped
|
||||
// Net is WSL's networking mode as the distro's wslinfo said at the
|
||||
// last probe ("mirrored", "nat", "virtioproxy", "none"); "" when it
|
||||
// couldn't say (a WSL without wslinfo), and .wslconfig is read instead
|
||||
Net string `json:"net,omitempty"`
|
||||
Mirrored bool `json:"-"`
|
||||
Running bool `json:"-"`
|
||||
}
|
||||
|
||||
// local is a path inside the distro as magpie opens it.
|
||||
@@ -142,6 +147,23 @@ func (d distro) native(local string) string {
|
||||
return rel
|
||||
}
|
||||
|
||||
// mirror settles Mirrored: what wslinfo said at the probe, or cfg
|
||||
// (.wslconfig's say) when it couldn't.
|
||||
func (d *distro) mirror(cfg bool) {
|
||||
d.Mirrored = d.Net == "mirrored" || d.Net == "" && cfg
|
||||
}
|
||||
|
||||
// notMirrored is the notice of an agent in a distro not known to be in
|
||||
// mirrored networking: what WSL said, or that only .wslconfig was read.
|
||||
func (d distro) notMirrored(agent string) string {
|
||||
why := "WSL " + d.Name + " is in " + d.Net + " networking, not mirrored"
|
||||
if d.Net == "" {
|
||||
why = "magpie couldn't ask WSL " + d.Name + " its networking mode (it has no wslinfo) and %UserProfile%\\.wslconfig doesn't set it to mirrored"
|
||||
}
|
||||
return why + ", so its " + agent + " was pointed at Windows (" + d.base() + ") rather than 127.0.0.1; that answers only while the gateway listens beyond loopback and Windows' firewall lets WSL in. " +
|
||||
"For 127.0.0.1, set networkingMode=mirrored under [wsl2] in %UserProfile%\\.wslconfig and run wsl --shutdown, then pick the model again."
|
||||
}
|
||||
|
||||
// base is the gateway's URL from inside the distro.
|
||||
func (d distro) base() string {
|
||||
if d.Mirrored || d.Gateway == "" {
|
||||
@@ -374,9 +396,7 @@ func wslAgent(k wslKind, d distro) *Agent {
|
||||
a.LastUsed = nil
|
||||
a.Notice = func() string {
|
||||
if !d.Mirrored {
|
||||
return "WSL " + d.Name + " isn't in mirrored networking, so its " + k.name + " can't reach magpie on 127.0.0.1 and was pointed at Windows (" + d.base() +
|
||||
"), which answers only while the gateway listens beyond loopback and Windows' firewall lets WSL in. " +
|
||||
"Set networkingMode=mirrored under [wsl2] in %UserProfile%\\.wslconfig and run wsl --shutdown, then pick the model again."
|
||||
return d.notMirrored(k.name)
|
||||
}
|
||||
if k.restart == "" {
|
||||
return ""
|
||||
@@ -560,6 +580,11 @@ func wslDistros() []distro {
|
||||
var out []distro
|
||||
for _, n := range wsl.names {
|
||||
installed[n] = true
|
||||
// a distro that stopped is probed again once it runs: WSL may have
|
||||
// been restarted meanwhile in another networking mode
|
||||
if !wsl.running[n] {
|
||||
delete(wsl.probed, n)
|
||||
}
|
||||
if wsl.running[n] && !wsl.probed[n] {
|
||||
if t, ok := wsl.failed[n]; !ok || time.Since(t) > wslRetryAge {
|
||||
if d := wslProbe(n); d == nil {
|
||||
@@ -583,7 +608,8 @@ func wslDistros() []distro {
|
||||
continue
|
||||
}
|
||||
c := *d
|
||||
c.Running, c.Mirrored = wsl.running[n], mirrored
|
||||
c.Running = wsl.running[n]
|
||||
c.mirror(mirrored)
|
||||
out = append(out, c)
|
||||
}
|
||||
// an unregistered distro is forgotten
|
||||
@@ -673,7 +699,7 @@ func wslClaudeStandIn(model string) string {
|
||||
wsl.Unlock()
|
||||
mirrored := wslMirrored(wslConfig())
|
||||
for _, d := range ds {
|
||||
d.Mirrored = mirrored
|
||||
d.mirror(mirrored)
|
||||
path := filepath.Join(d.local(d.Home), ".claude", "settings.json")
|
||||
if m := claudeStandInAt(path, model, d.base()); m != "" {
|
||||
return m
|
||||
@@ -694,11 +720,14 @@ func wslList(args ...string) (names []string, ok bool) {
|
||||
|
||||
// wslProbeVersion is that of wslProbeScript: 2 says where each command is,
|
||||
// so one from Windows' drives isn't taken for the distro's own.
|
||||
// (wslinfo's networking mode came without a new version: a probe that
|
||||
// didn't ask leaves Net "", read as .wslconfig says, as before.)
|
||||
const wslProbeVersion = 2
|
||||
|
||||
// wslProbeScript prints the distro's home, what of each of wslKinds it
|
||||
// has (each command with where it is), where Windows' drives are mounted,
|
||||
// and its default route (the Windows host under NAT).
|
||||
// its default route (the Windows host under NAT), and WSL's networking
|
||||
// mode as wslinfo (WSL 2.0 on) says it.
|
||||
var wslProbeScript = func() string {
|
||||
s := `echo "home:$HOME"; `
|
||||
for _, k := range wslKinds {
|
||||
@@ -710,7 +739,8 @@ var wslProbeScript = func() string {
|
||||
// a drive's source in /proc/mounts is C:\ (written C:\134), under any automount root
|
||||
s += `awk '$1 ~ /^[A-Za-z]:/ {print "win:" $2}' /proc/mounts 2>/dev/null; `
|
||||
return s + `ip route show default 2>/dev/null | head -n1 | sed 's/^/route:/'; ` +
|
||||
`grep -m1 '^nameserver' /etc/resolv.conf 2>/dev/null | sed 's/^/ns:/'; true`
|
||||
`grep -m1 '^nameserver' /etc/resolv.conf 2>/dev/null | sed 's/^/ns:/'; ` +
|
||||
`command -v wslinfo >/dev/null 2>&1 && echo "net:$(wslinfo --networking-mode 2>/dev/null)"; true`
|
||||
}()
|
||||
|
||||
func wslProbe(name string) *distro {
|
||||
@@ -761,6 +791,11 @@ func parseProbe(name, out string) *distro {
|
||||
if f := strings.Fields(v); len(f) >= 2 && net.ParseIP(f[1]) != nil {
|
||||
ns = f[1]
|
||||
}
|
||||
case "net":
|
||||
// one word; anything else (an old wslinfo's usage) says nothing
|
||||
if m := strings.ToLower(strings.TrimSpace(v)); m != "" && !strings.ContainsAny(m, " \t") {
|
||||
d.Net = m
|
||||
}
|
||||
}
|
||||
}
|
||||
if !strings.HasPrefix(d.Home, "/") {
|
||||
@@ -821,11 +856,25 @@ func decodeWSL(b []byte) string {
|
||||
return string(utf16.Decode(u))
|
||||
}
|
||||
|
||||
// wslConfig is %UserProfile%\.wslconfig, "" if there is none.
|
||||
// wslConfig is %UserProfile%\.wslconfig, "" if there is none: UTF-8, or
|
||||
// UTF-16 as Notepad's "Unicode" saves it, which WSL reads as well.
|
||||
func wslConfig() string {
|
||||
home, _ := os.UserHomeDir()
|
||||
home, _ := os.UserHomeDir() // %UserProfile% on Windows
|
||||
b, _ := os.ReadFile(filepath.Join(home, ".wslconfig"))
|
||||
return string(b)
|
||||
return decodeText(b)
|
||||
}
|
||||
|
||||
// decodeText is a text file's contents: UTF-16 with a BOM (either byte
|
||||
// order), UTF-16LE without one, or else UTF-8.
|
||||
func decodeText(b []byte) string {
|
||||
if len(b) >= 2 && b[0] == 0xfe && b[1] == 0xff {
|
||||
u := make([]uint16, (len(b)-2)/2)
|
||||
for i := range u {
|
||||
u[i] = uint16(b[2+2*i])<<8 | uint16(b[3+2*i])
|
||||
}
|
||||
return string(utf16.Decode(u))
|
||||
}
|
||||
return decodeWSL(b)
|
||||
}
|
||||
|
||||
// wslMirrored reports whether a .wslconfig puts WSL 2 in mirrored
|
||||
@@ -838,9 +887,12 @@ func wslMirrored(cfg string) bool {
|
||||
if l == "" || l[0] == '#' || l[0] == ';' {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(l, "[") && strings.HasSuffix(l, "]") {
|
||||
section = strings.ToLower(strings.TrimSpace(l[1 : len(l)-1]))
|
||||
continue
|
||||
if strings.HasPrefix(l, "[") {
|
||||
// [wsl2], or [wsl2] # a comment
|
||||
if end := strings.Index(l, "]"); end > 0 {
|
||||
section = strings.ToLower(strings.TrimSpace(l[1:end]))
|
||||
continue
|
||||
}
|
||||
}
|
||||
k, v, ok := strings.Cut(l, "=")
|
||||
if !ok || section != "wsl2" || !strings.EqualFold(strings.TrimSpace(k), "networkingMode") {
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
"unicode/utf16"
|
||||
|
||||
"github.com/yetone/magpie/internal/gateway"
|
||||
)
|
||||
|
||||
// A .wslconfig Notepad saved as "Unicode" (UTF-16LE with a BOM), as
|
||||
// "Unicode big endian", or UTF-16LE with no BOM, is read as WSL reads it
|
||||
// (lgtm on Discord: mirrored all along, and told it wasn't).
|
||||
func TestWSLConfigUTF16(t *testing.T) {
|
||||
cfg := "[wsl2] # networking\r\nmemory=8GB\r\nnetworkingMode=mirrored\r\n"
|
||||
be := []byte{0xfe, 0xff}
|
||||
for _, u := range utf16.Encode([]rune(cfg)) {
|
||||
be = append(be, byte(u>>8), byte(u))
|
||||
}
|
||||
for name, b := range map[string][]byte{
|
||||
"utf-8": []byte(cfg),
|
||||
"utf-8 bom": append([]byte{0xef, 0xbb, 0xbf}, cfg...),
|
||||
"utf-16le bom": utf16le(cfg, true),
|
||||
"utf-16le, no bom": utf16le(cfg, false),
|
||||
"utf-16be bom": be,
|
||||
} {
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv("USERPROFILE", home)
|
||||
os.WriteFile(filepath.Join(home, ".wslconfig"), b, 0o644)
|
||||
if !wslMirrored(wslConfig()) {
|
||||
t.Errorf("%s: not mirrored: %q", name, wslConfig())
|
||||
}
|
||||
}
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv("USERPROFILE", home)
|
||||
if wslConfig() != "" || wslMirrored(wslConfig()) {
|
||||
t.Error("no .wslconfig")
|
||||
}
|
||||
}
|
||||
|
||||
// The probe takes WSL's networking mode from wslinfo; an empty answer or
|
||||
// an old wslinfo's usage text says nothing.
|
||||
func TestParseProbeNet(t *testing.T) {
|
||||
for out, want := range map[string]string{
|
||||
"home:/home/me\nnet:mirrored\n": "mirrored",
|
||||
"home:/home/me\nnet:NAT\r\n": "nat",
|
||||
"home:/home/me\nnet:\n": "",
|
||||
"home:/home/me\nnet:Usage: wslinfo [Options...]\n": "",
|
||||
"home:/home/me\n": "",
|
||||
} {
|
||||
if d := parseProbe("U", out); d == nil || d.Net != want {
|
||||
t.Errorf("%q: %+v", out, d)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(wslProbeScript, "wslinfo --networking-mode") {
|
||||
t.Error("probe doesn't ask wslinfo")
|
||||
}
|
||||
}
|
||||
|
||||
// What WSL says wins over .wslconfig: mirrored by wslinfo with no
|
||||
// .wslconfig at all is 127.0.0.1; nat by wslinfo with a .wslconfig saying
|
||||
// mirrored (not yet applied) is Windows, and the notice says nat. Without
|
||||
// wslinfo, .wslconfig decides, and the notice says magpie couldn't ask.
|
||||
func TestWSLNetworkingFromWslinfo(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
net, cfg string
|
||||
mirrored bool
|
||||
notice string
|
||||
}{
|
||||
{"mirrored", "", true, ""},
|
||||
{"nat", "[wsl2]\nnetworkingMode=mirrored\n", false, "is in nat networking"},
|
||||
{"", "[wsl2]\nnetworkingMode=mirrored\n", true, ""},
|
||||
{"", "", false, "couldn't ask WSL Ubuntu"},
|
||||
} {
|
||||
home, _ := codexHome(t, "", "model = \"gpt-5.5\"\n")
|
||||
if c.cfg != "" {
|
||||
os.WriteFile(filepath.Join(home, ".wslconfig"), utf16le(c.cfg, true), 0o644)
|
||||
}
|
||||
probe := "home:/" + filepath.Base(home) + "\ndir:.codex\nroute:default via 192.168.16.1 dev eth0\n"
|
||||
if c.net != "" {
|
||||
probe += "net:" + c.net + "\n"
|
||||
}
|
||||
fakeWSL(t, "Ubuntu\r\n", "Ubuntu\r\n", map[string]string{"Ubuntu": probe},
|
||||
map[string]string{"Ubuntu": filepath.Dir(home)})
|
||||
ds := wslDistros()
|
||||
if len(ds) != 1 || ds[0].Mirrored != c.mirrored || ds[0].Net != c.net {
|
||||
t.Fatalf("%+v: %+v", c, ds)
|
||||
}
|
||||
a := wslCodex(ds[0])
|
||||
n := a.Notice()
|
||||
if c.mirrored {
|
||||
if ds[0].base() != gateway.URL() || strings.Contains(n, "mirrored") {
|
||||
t.Errorf("%+v: %s / %s", c, ds[0].base(), n)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(n, c.notice) || !strings.Contains(n, "192.168.16.1") || strings.Contains(n, "isn't in mirrored") {
|
||||
t.Errorf("%+v: %s", c, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A distro that stopped is probed again once it runs, so WSL restarted in
|
||||
// another networking mode is seen without restarting magpie.
|
||||
func TestWSLReprobedAfterStop(t *testing.T) {
|
||||
home, _ := codexHome(t, "", "model = \"gpt-5.5\"\n")
|
||||
fakeWSL(t, "Ubuntu\r\n", "Ubuntu\r\n", nil, map[string]string{"Ubuntu": filepath.Dir(home)})
|
||||
running, net, probes := true, "nat", 0
|
||||
wslRun = func(_ time.Duration, args ...string) ([]byte, error) {
|
||||
switch strings.Join(args, " ") {
|
||||
case "-l -q":
|
||||
return utf16le("Ubuntu\r\n", true), nil
|
||||
case "-l --running -q":
|
||||
if running {
|
||||
return utf16le("Ubuntu\r\n", true), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
if len(args) > 2 && args[0] == "-d" {
|
||||
probes++
|
||||
return []byte("home:/" + filepath.Base(home) + "\ndir:.codex\nnet:" + net + "\n"), nil
|
||||
}
|
||||
return nil, errors.New("unexpected wsl.exe " + strings.Join(args, " "))
|
||||
}
|
||||
look := func() distro {
|
||||
wsl.Lock()
|
||||
wsl.at = time.Time{}
|
||||
wsl.Unlock()
|
||||
ds := wslDistros()
|
||||
if len(ds) != 1 {
|
||||
t.Fatalf("%+v", ds)
|
||||
}
|
||||
return ds[0]
|
||||
}
|
||||
if d := look(); d.Mirrored || probes != 1 {
|
||||
t.Fatalf("%+v, %d probes", d, probes)
|
||||
}
|
||||
if d := look(); probes != 1 {
|
||||
t.Fatalf("probed again while running: %+v", d)
|
||||
}
|
||||
running = false
|
||||
if d := look(); d.Running || probes != 1 {
|
||||
t.Fatalf("stopped: %+v, %d probes", d, probes)
|
||||
}
|
||||
running, net = true, "mirrored"
|
||||
if d := look(); !d.Mirrored || probes != 2 {
|
||||
t.Fatalf("restarted mirrored: %+v, %d probes", d, probes)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user