Files
magpie/internal/agent/wsl_rekey_test.go
T
yetone f8767d1980 agent, gui: turning LAN sharing on gives a WSL agent under NAT connected before it the sharing key, so its requests are no longer 401
An agent in a WSL distro under NAT reaches the gateway at Windows'
address, where only a named key is taken; magpie writes it the sharing
key while sharing is on (keyAt). One connected while nothing was shared
was written gateway.Token, and turning sharing on afterwards left it
there: tried on the Windows box with magpie-desktop (NAT), Codex · WSL
connected, then Settings → LAN sharing on, `codex exec` in WSL was 401
"not an enabled magpie gateway key" on every try. #1488 (91581fca2) did
this for the library's relayed MCP servers only.

The LAN toggle now takes agent.OnGateway() before ConfigureLAN and
agent.Rekey()s the ones that reach the gateway beyond loopback after it:
on, off and a new key alike. Rekey shares setAgain with Rewire (the port
move), so what the agent had before magpie stays stashed. An agent that
can't be set again is said on the Settings page (settingsJSON.rekeyed).

Run: TestRekeyGivesWSLAgentTheSharingKey (red with Rekey made a no-op:
key "magpie"), TestRekeyLeavesLoopback; go test ./internal/agent and
./internal/gateway alone green (the full ./... run timed both out at
10m under load average ~200, each test then 1-5s in); gui green in the
full run; gofmt, go vet (darwin, windows; linux for internal/agent).
Real use on the Windows box (this commit cross-built, sandbox HOME,
port 3436): sharing off then on rewrote the WSL Codex token to the
sharing key, and `codex exec "Reply with exactly: hi from wsl"` in
magpie-desktop answered through the gateway (Gateway page: codex
deepseek/deepseek-flash responses 200).
2026-10-11 01:21:21 +08:00

116 lines
3.1 KiB
Go

package agent
import (
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
)
// Codex in a WSL distro under NAT connected while the gateway wasn't shared
// was written gateway.Token, which the gateway turns away from beyond
// loopback; turning sharing on afterwards left it there, and every request
// was 401 "not an enabled magpie gateway key" (tried on Windows with
// magpie-desktop). Rekey, run as sharing changes, gives it the sharing key,
// and gateway.Token again once sharing is off.
func TestRekeyGivesWSLAgentTheSharingKey(t *testing.T) {
t.Setenv("MAGPIE_ADDR", "")
home, _ := codexHome(t, "", "model = \"gpt-5.5\"\n")
probe := "home:/" + filepath.Base(home) + "\ndir:.codex\nroute:default via 172.23.80.1 dev eth0 proto kernel\nnet:nat\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 {
t.Fatalf("%+v", ds)
}
a := wslCodex(ds[0])
if !strings.HasPrefix(a.Gateway(), "http://172.23.80.1:") {
t.Fatalf("pointed at %s", a.Gateway())
}
if err := a.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
cfg := filepath.Join(home, ".codex", "config.toml")
token := func() string {
t.Helper()
tb, err := edit.GetTOMLTable(cfg, "model_providers."+magpieID)
if err != nil {
t.Fatal(err)
}
return tb["experimental_bearer_token"]
}
if k := token(); k != gateway.Token {
t.Fatalf("connected unshared: key %q", k)
}
on := []*Agent{a}
if err := access.ConfigureLAN(true, false); err != nil {
t.Fatal(err)
}
lan := access.LANSecret()
if lan == "" {
t.Fatal("no sharing key")
}
moved, err := Rekey(on)
if err != nil {
t.Fatal(err)
}
if k := token(); k != lan {
t.Fatalf("sharing on: key %q, want the sharing key (moved %v)", k, moved)
}
if !a.Wired() {
t.Error("no longer connected")
}
if d := a.Drift(); d != nil {
t.Errorf("drift %+v", d)
}
if err := access.ConfigureLAN(true, true); err != nil {
t.Fatal(err)
}
if _, err := Rekey(on); err != nil {
t.Fatal(err)
}
if k := token(); k == lan || k != access.LANSecret() {
t.Fatalf("key made anew: config has %q", k)
}
if err := access.ConfigureLAN(false, false); err != nil {
t.Fatal(err)
}
if _, err := Rekey(on); err != nil {
t.Fatal(err)
}
if k := token(); k != gateway.Token {
t.Fatalf("sharing off: key %q", k)
}
}
// Rekey leaves an agent on this machine's loopback as it is: its key
// doesn't follow sharing.
func TestRekeyLeavesLoopback(t *testing.T) {
t.Setenv("MAGPIE_ADDR", "")
_, read := codexHome(t, "", "model = \"gpt-5.5\"\n")
var as []*Agent
for _, a := range All() {
if a.ID == "codex" {
as = append(as, a)
}
}
if len(as) != 1 {
t.Fatalf("codex: %d", len(as))
}
if err := access.ConfigureLAN(true, false); err != nil {
t.Fatal(err)
}
if moved, err := Rekey(as); err != nil || len(moved) != 0 {
t.Fatalf("moved %v, %v", moved, err)
}
if cfg := read(); strings.Contains(cfg, "magpie") {
t.Fatalf("written:\n%s", cfg)
}
}