Files
magpie/internal/library/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

143 lines
5.6 KiB
Go

package library
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/agent"
)
// wslLinked is sandbox with a running WSL distro, Ubuntu-24.04, whose
// ~/.codex and ~/.claude are links to /mnt/c/Users/me/… (#4502), as
// \\wsl.localhost shows them to Windows: a link nothing can be followed
// through. In the fake distro's root each is a link to /mnt/c/…, which
// isn't there, so — as through \\wsl.localhost — nothing below it can be
// made, written or read, and a mkdir of it says it exists. Windows' C: is
// a folder of its own, with the agents' real folders. probe is what the
// distro answers of the links; it gives back the home as magpie opens it
// and C:\Users\me.
func wslLinked(t *testing.T, links string) (home, c string) {
t.Helper()
sandbox(t)
root, drive := t.TempDir(), t.TempDir()
home = filepath.Join(root, "home", "me")
c = filepath.Join(drive, "Users", "me")
write(t, filepath.Join(c, ".codex", "config.toml"), "model = \"gpt-5.5\"\n")
write(t, filepath.Join(c, ".claude", "settings.json"), "{}\n")
if err := os.MkdirAll(home, 0o755); err != nil {
t.Fatal(err)
}
for _, d := range []string{".codex", ".claude"} {
if err := os.Symlink("/mnt/c/Users/me/"+d, filepath.Join(home, d)); err != nil {
t.Skip("no symlinks here:", err)
}
}
t.Cleanup(agent.FakeWSL(map[string]string{
"Ubuntu-24.04": "home:/home/me\ndir:.codex\ndir:.claude\n" + links + "win:/mnt/c C:\nwin:/mnt/d D:\n",
}, map[string]string{"Ubuntu-24.04": root}))
t.Cleanup(agent.FakeWSLDrives(map[string]string{"C:": drive}))
return home, c
}
// A skill added to Codex and Claude Code in a distro whose ~/.codex and
// ~/.claude lead to Windows' C: lands where they lead (#4502:
// linusxiong's mkdir \\wsl.localhost\…\.codex "Cannot create a file when
// that file already exists"), and so do their instructions; what the
// agents are told is the distro's own path, through the link.
func TestWSLFolderIsALinkToC(t *testing.T) {
h, c := wslLinked(t, "link:.codex /mnt/c/Users/me/.codex\nlink:.claude /mnt/c/Users/me/.claude/\n")
src := filepath.Join(home(), "src", "skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
ok(t)(InstallSkills(src, []string{"pdf"}, []string{wslCodex, wslClaude}))
for _, d := range []string{".codex", ".claude"} {
if _, err := os.Stat(filepath.Join(c, d, "skills", "pdf", "SKILL.md")); err != nil {
t.Errorf("the skill isn't in C:'s %s, where the distro's link leads: %v", d, err)
}
}
if tg := targetByID(wslCodex); tg == nil || tg.Instructions != filepath.Join(c, ".codex", "AGENTS.md") || tg.Skills != filepath.Join(c, ".codex", "skills") {
t.Fatalf("Codex's target: %+v", tg)
}
if fi, err := os.Lstat(filepath.Join(h, ".codex")); err != nil || fi.Mode()&os.ModeSymlink == 0 {
t.Fatalf("the distro's link was replaced: %v %v", fi, err)
}
}
// A link the distro couldn't resolve is written through by nothing: each
// agent behind one says why, once, and the others are written as ever.
func TestWSLFolderLinkUnresolved(t *testing.T) {
_, c := wslLinked(t, "link:.codex\nlink:.claude /mnt/c/Users/me/.claude\n")
src := filepath.Join(home(), "src", "skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
r, err := InstallSkills(src, []string{"pdf"}, []string{wslCodex, wslClaude})
if err != nil {
t.Fatal(err)
}
var got []Problem
for _, p := range r.Problems {
if p.Agent == wslCodex {
got = append(got, p)
}
}
if len(got) != 1 || got[0].What != "wsl" || !strings.Contains(got[0].Error, "~/.codex in WSL Ubuntu-24.04 is a link magpie couldn't follow") {
t.Fatalf("Codex behind a link not resolved: %+v", r.Problems)
}
if _, err := os.Stat(filepath.Join(c, ".claude", "skills", "pdf", "SKILL.md")); err != nil {
t.Errorf("Claude Code's skill, beside it: %v", err)
}
if _, err := os.Stat(filepath.Join(c, ".codex", "skills")); err == nil {
t.Error("written to where the link was guessed to lead")
}
}
// RTK for Claude Code in a distro whose ~/.claude leads to C: (#4502's
// second report): switched on, its folder is made where the link leads,
// and the hook rtk writes there (in the distro, where the link is
// followed) is read back from C:. A link not resolved fails with why.
func TestRTKWSLFolderIsALink(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the fake distro runs sh")
}
_, c := wslLinked(t, "link:.claude /mnt/c/Users/me/.claude\n")
// in the distro the link is followed: rtk runs in a home whose
// ~/.claude is C:'s
inside := filepath.Join(t.TempDir(), "home", "me")
if err := os.MkdirAll(inside, 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(filepath.Join(c, ".claude"), filepath.Join(inside, ".claude")); err != nil {
t.Fatal(err)
}
fakeDistroRTK(t, inside)
if _, err := InstallRTKIn("Ubuntu-24.04"); err != nil {
t.Fatal(err)
}
if _, err := SetRTK(wslClaude, true); err != nil {
t.Fatalf("RTK for Claude Code behind a link: %v", err)
}
b, _ := os.ReadFile(filepath.Join(c, ".claude", "settings.json"))
if !strings.Contains(string(b), "rtk hook claude") {
t.Fatalf("no hook in C:'s .claude/settings.json: %s", b)
}
if a := rtkRow(t, ReadRTK(), wslClaude); !a.On {
t.Fatalf("switched on, yet the row is off: %+v", a)
}
}
func TestRTKWSLFolderLinkUnresolved(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the fake distro runs sh")
}
wslLinked(t, "link:.claude\n")
fakeDistroRTK(t, t.TempDir())
if _, err := InstallRTKIn("Ubuntu-24.04"); err != nil {
t.Fatal(err)
}
_, err := SetRTK(wslClaude, true)
if err == nil || !strings.Contains(err.Error(), "~/.claude in WSL Ubuntu-24.04 is a link magpie couldn't follow") {
t.Fatalf("RTK behind a link not resolved: %v", err)
}
}