Files
magpie/internal/agent/wsl_link_test.go
T
yetone 73a12e8bf6 wsl: a distro's agent folder that is a link is opened where it leads (#4502)
On Windows, adding a skill or switching RTK on for an agent in WSL failed
with `mkdir \\wsl.localhost\<distro>\root\.codex: Cannot create a file
when that file already exists` (linusxiong). In the distro ~/.codex is a
link to /mnt/c/Users/<user>/.codex. Through \\wsl.localhost Windows can't
follow a link the distro has: a mkdir of it says it exists, and nothing
below it can be made, written, read or listed. ggbdpq found and explained
the link (thanks); their patch, which takes that EEXIST as "there", wasn't
used: the writes below the link fail next (its test passed because a
symlink here is followed).

The fix, for every agent folder and every writer through \\wsl.localhost:
- The WSL probe asks `readlink -f` in the distro where each agent folder,
  its skills folder and the folders beside them lead (wslLinkPaths ->
  distro.Links; a $HOME that is itself through a link is left alone), and
  which drive each Windows mount is (win:/mnt/c C: -> distro.Drives).
- distro.open opens a path at or below a link where it leads: on a Windows
  drive, the drive's own path (C:\Users\<user>\.codex); elsewhere in the
  distro, through \\wsl.localhost again. native spells it back through
  the link, so the agent is told its own path.
- A link readlink couldn't resolve is never guessed at: distro.open
  errors, and the agent is unreached (Agent.Unreached: its notice says
  why; every field Set and Sync returns it).
- Writers: the agents' own files (place.join/place.open, every
  filepath.Join(at.home, ...) in internal/agent), the library's WSL
  targets (wslOpen/wslOpened; one "wsl" problem per agent and nothing
  written), RTK (rtkAt, rtkInstall checks agent.WSLOpen before its mkdir),
  the Sessions page's WSL reads (sessions.WSLHome.Open) and the Copilot
  CLI sign-in read (provider.CopilotWSLHomes -> CopilotWSLDirs).
- Not changed: magpie's image MCP server opens arbitrary paths an agent
  in WSL gives it the same way; filed as #4578. Links deeper than the
  probed paths (other than <agent dir>/skills) aren't resolved. A distro
  probed by an older magpie has no Links until probed again (each run).

Run:
- Real Windows box (xiaomi-gpu, distro magpie-desktop, sandbox
  /tmp/x4502/home with .codex -> /mnt/c/Users/yeton/runs/x4502-target/.codex,
  .claude -> /tmp/x4502/data/claude, .pi -> a missing folder), a throwaway
  Windows test running the real probe with that HOME: probe said
  link:.codex /mnt/c/..., link:.claude /tmp/x4502/data/claude, link:.pi,
  win:/mnt/c C:, win:/mnt/d D:. The old way, MkdirAll(\\wsl.localhost\
  magpie-desktop\tmp\x4502\home\.codex\skills\pdf) = "Cannot create a file
  when that file already exists" (the report's error). distro.open wrote
  SKILL.md to C:\Users\yeton\runs\x4502-target\.codex\skills\pdf and to
  \\wsl.localhost\magpie-desktop\tmp\x4502\data\claude\skills\pdf, both
  read back with cat in the distro through the links; .pi said "~/.pi in
  WSL magpie-desktop is a link magpie couldn't follow". Sandbox and target
  removed after.
- New tests with a fake distro whose ~/.codex and ~/.claude are links to
  /mnt/c/... that don't exist here, so, as through \\wsl.localhost,
  nothing below them can be made: TestWSLFolderIsALinkToC,
  TestWSLFolderLinkUnresolved, TestRTKWSLFolderIsALink,
  TestRTKWSLFolderLinkUnresolved (library); TestWSLProbeLinks (runs the
  probe script), TestWSLOpenThroughLinks, TestWSLAgentFolderIsALink
  (config and a Claude session read on C:), TestWSLAgentFolderLinkUnresolved
  (agent).
- Broken by hand: the library tests on the base (with a no-op
  FakeWSLDrives) fail with "mkdir .../home/me/.codex: file exists", the
  report's error; with ggbdpq's patch applied they still fail (the skill
  mkdir, and RTK's hook not written).
- mote: go build ./...; go vet of agent, library, sessions, provider for
  darwin, GOOS=windows (and linux for agent, library); gofmt -l clean;
  go test -timeout 45m ./internal/agent ./internal/library
  ./internal/sessions ./internal/provider: all ok.
- Windows (win-run): the same four packages. The new tests pass (the
  RTK ones skip: the fake distro runs sh). 32 tests are red, all already
  red at the base c63c2136b run on the same box, and filed (#4115, #2808:
  Hermes profiles, Codex keychain, Agy, desktop entries, ...); the Hermes
  ones fail alike on base and branch with -count=3.

- Rebased on origin/main 3ecc02b78 (it changed internal/agent): at this
  head on mote, go build ./..., go vet of the four packages for darwin,
  windows and linux, gofmt -l, go test ./internal/agent ./internal/library:
  ok.

Not tried: the full ./... suite; the GUI (no GUI change; the unreached
notice uses the existing Notice); a real Codex/Claude Code run against
the moved files in the distro.
2026-10-31 21:29:22 +08:00

202 lines
7.4 KiB
Go

package agent
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/sessions"
)
// The probe asks the distro where each agent folder that is a link leads
// (#4502), with readlink -f there, and where Windows' drives are mounted:
// a folder that isn't a link isn't listed.
func TestWSLProbeLinks(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the probe is a shell script")
}
home := filepath.Join(t.TempDir(), "home", "me")
to := filepath.Join(t.TempDir(), "Users", "me", ".codex")
for _, d := range []string{to, filepath.Join(home, ".claude")} {
if err := os.MkdirAll(d, 0o755); err != nil {
t.Fatal(err)
}
}
if err := os.Symlink(to, filepath.Join(home, ".codex")); err != nil {
t.Fatal(err)
}
cmd := exec.Command("/bin/sh", "-c", wslProbeScript)
cmd.Env = []string{"HOME=" + home, "PATH=/usr/bin:/bin"}
out, _ := cmd.Output()
d := parseProbe("U", string(out))
real, _ := filepath.EvalSymlinks(to)
if d == nil || d.Links[".codex"] != real {
t.Fatalf("the probe said:\n%s\nread as %+v", out, d)
}
if _, ok := d.Links[".claude"]; ok {
t.Fatalf("a folder that isn't a link is listed: %v", d.Links)
}
}
// What the probe says of links and drives, and where magpie opens a path
// below each: a link to Windows' C: on C: itself, one elsewhere in the
// distro through its root, and one it couldn't resolve not at all. The
// agent is told the path through the link, as it knows it.
func TestWSLOpenThroughLinks(t *testing.T) {
d := parseProbe("U", "home:/home/me\ndir:.codex\n"+
"link:.codex /mnt/c/Users/me/.codex\nlink:.claude /data/claude/\nlink:.pi\nlink:.bashrc /etc/bashrc\n"+
"win:/mnt/c C:\nwin:/mnt/wsl/d d:\nwin:/mnt/e\n")
if d == nil {
t.Fatal("no distro")
}
if _, ok := d.Links[".bashrc"]; ok {
t.Errorf("a path the probe doesn't ask is taken: %v", d.Links)
}
if d.Links[".claude"] != "/data/claude" || d.Links[".pi"] != "" || d.Drives["/mnt/c"] != "C:" || d.Drives["/mnt/wsl/d"] != "D:" || len(d.Drives) != 2 {
t.Fatalf("links %q, drives %q", d.Links, d.Drives)
}
sep := string(filepath.Separator)
d.Root = filepath.Join(t.TempDir(), "U")
t.Cleanup(FakeWSLDrives(map[string]string{"C:": filepath.Join(t.TempDir(), "C")}))
c := wslDrive("C:")
h := d.local(d.Home)
for _, tc := range []struct{ in, want, err string }{
{filepath.Join(h, ".codex"), c + filepath.Join("Users", "me", ".codex"), ""},
{filepath.Join(h, ".codex", "skills", "pdf"), c + filepath.Join("Users", "me", ".codex", "skills", "pdf"), ""},
{filepath.Join(h, ".claude", "settings.json"), d.local("/data/claude/settings.json"), ""},
{filepath.Join(h, ".codexx", "a"), filepath.Join(h, ".codexx", "a"), ""},
{filepath.Join(h, ".omo", "agent"), filepath.Join(h, ".omo", "agent"), ""},
{filepath.Join(h, ".pi", "agent", "x"), "", "~/.pi in WSL U is a link magpie couldn't follow"},
} {
got, err := d.open(tc.in)
if tc.err != "" {
if err == nil || !strings.Contains(err.Error(), tc.err) {
t.Errorf("open %s: %q %v", tc.in, got, err)
}
continue
}
if err != nil || got != tc.want {
t.Errorf("open %s: %q %v, want %q", tc.in, got, err, tc.want)
}
if n := d.native(got); n != d.native(tc.in) && !strings.HasPrefix(tc.in, filepath.Join(h, ".codexx")) {
t.Errorf("native %s: %q, want %q", got, n, d.native(tc.in))
}
}
if n := d.native(c + "Users" + sep + "me" + sep + ".codex" + sep + "AGENTS.md"); n != "/home/me/.codex/AGENTS.md" {
t.Errorf("the agent is told %q", n)
}
// a probe before Drives: WSL's own /mnt/<letter>
d.Drives = nil
if got, _ := d.open(filepath.Join(h, ".codex", "x")); got != c+filepath.Join("Users", "me", ".codex", "x") {
t.Errorf("no drives probed: %q", got)
}
}
// linkedDistro is a running distro whose ~/.codex and ~/.claude are links
// to /mnt/c/Users/me/… that, as through \\wsl.localhost, can't be followed
// here: nothing below them can be made or read. C: has the real folders,
// with Claude Code's session.
func linkedDistro(t *testing.T, links string) (home, c string) {
t.Helper()
syncHome(t)
root, drive := t.TempDir(), t.TempDir()
home = filepath.Join(root, "home", "me")
c = filepath.Join(drive, "Users", "me")
if err := os.MkdirAll(home, 0o755); err != nil {
t.Fatal(err)
}
for _, d := range []string{".codex", ".claude"} {
if err := os.MkdirAll(filepath.Join(c, d), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink("/mnt/c/Users/me/"+d, filepath.Join(home, d)); err != nil {
t.Skip("no symlinks here:", err)
}
}
t.Cleanup(FakeWSL(map[string]string{"Ubuntu": "home:/home/me\ndir:.codex\ndir:.claude\n" + links + "win:/mnt/c C:\n"},
map[string]string{"Ubuntu": root}))
t.Cleanup(FakeWSLDrives(map[string]string{"C:": drive}))
return home, c
}
func wslAgentByID(t *testing.T, id string) *Agent {
t.Helper()
for _, a := range wslAgents() {
if a.ID == id {
return a
}
}
t.Fatalf("no %s in %v", id, wslAgents())
return nil
}
// Codex in a distro whose ~/.codex leads to C: (#4502) is set up there:
// its config is read and written on C:, and its sessions are listed from
// it; Claude Code's sessions too.
func TestWSLAgentFolderIsALink(t *testing.T) {
_, c := linkedDistro(t, "link:.codex /mnt/c/Users/me/.codex\nlink:.claude /mnt/c/Users/me/.claude\n")
a := wslAgentByID(t, "codex@wsl:Ubuntu")
if a.Unreached() != nil || a.Path != filepath.Join(c, ".codex", "config.toml") {
t.Fatalf("Codex: path %q, unreached %v", a.Path, a.Unreached())
}
if err := a.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
cfg, err := os.ReadFile(filepath.Join(c, ".codex", "config.toml"))
if err != nil || !strings.Contains(string(cfg), `model = "fake/m1"`) {
t.Fatalf("Codex's config on C: (%v):\n%s", err, cfg)
}
id := "44444444-2222-3333-4444-555555555555"
project := filepath.Join(c, ".claude", "projects", "-home-me-app")
if err := os.MkdirAll(project, 0o755); err != nil {
t.Fatal(err)
}
line := `{"type":"user","message":{"role":"user","content":"Tidy the README"},"uuid":"u1","timestamp":"2026-09-20T10:00:01.000Z","cwd":"/home/me/app","sessionId":"` + id + `"}` + "\n"
if err := os.WriteFile(filepath.Join(project, id+".jsonl"), []byte(line), 0o644); err != nil {
t.Fatal(err)
}
sessions.Reset()
t.Cleanup(sessions.Reset)
found := false
for _, s := range sessions.List(0) {
found = found || s.ID == id && s.WSL == "Ubuntu"
}
if !found {
t.Fatal("the session on C: behind ~/.claude isn't listed")
}
}
// A link the distro couldn't resolve: the agent behind it says why, and
// no change of it is written anywhere.
func TestWSLAgentFolderLinkUnresolved(t *testing.T) {
_, c := linkedDistro(t, "link:.codex\nlink:.claude /mnt/c/Users/me/.claude\n")
a := wslAgentByID(t, "codex@wsl:Ubuntu")
const why = "~/.codex in WSL Ubuntu is a link magpie couldn't follow"
if err := a.Unreached(); err == nil || !strings.Contains(err.Error(), why) || a.Notice == nil || !strings.Contains(a.Notice(), why) {
t.Fatalf("Codex behind a link not resolved: %v", err)
}
if a.Sync == nil {
t.Fatal("no Sync")
}
if err := a.Sync(); err == nil || !strings.Contains(err.Error(), why) {
t.Fatalf("Sync: %v", err)
}
for _, f := range a.Fields {
if f.Set != nil {
if err := f.Set("x"); err == nil || !strings.Contains(err.Error(), why) {
t.Fatalf("%s set: %v", f.Key, err)
}
}
}
if es, _ := os.ReadDir(filepath.Join(c, ".codex")); len(es) != 0 {
t.Fatalf("written where the link was guessed to lead: %v", es)
}
if b := wslAgentByID(t, "claude@wsl:Ubuntu"); b.Unreached() != nil {
t.Fatalf("Claude Code beside it: %v", b.Unreached())
}
}