Files
magpie/internal/library/wsl_mcp_test.go
T
yetone 22f5cf69e5 library: an agent in WSL links to the distro's one copy of a skill (cutemmuu on X)
Cause: an agent in a WSL distro could only get copies (Target.Copy).
A distro can't follow a link to the library's folder on Windows. A
link Windows makes through \\wsl.localhost (os.Symlink) is an empty
folder inside the distro. Windows can't use a link the distro has: it
can Lstat one (irregular), but Readlink fails ("Incorrect function"),
and Stat, ReadDir and Remove all say it isn't there. Probed on
xiaomi-gpu with magpie-desktop. So every agent there got a copy of
its own of every skill.

Now each distro keeps one copy of each skill its agents are given,
magpie's and marked, in ~/.local/share/magpie/skills (Target.Store,
storeSkill). Each agent there gets a link to it, which wsl.exe makes,
reads and removes inside the distro (agent.WSLSymlink, WSLReadlink,
WSLRemoveLink). The scripts refuse to put a link over a folder or file.
- An edit made through the link is in that copy, and takeEdits reads
  it like any copy of magpie's.
- When no agent there links to a copy or is given its skill any more,
  sweepWSLStores removes it. An edited copy goes to the backups.
- A folder in the store that isn't magpie's is never touched. Each
  agent it blocks reports a problem and keeps what it had.
- An agent set to copies (SkillHow) still gets copies of its own.
- So does one whose skills folder or the store is on a Windows drive
  (#4502): a link the distro makes there can't be read from Windows.
- The copy isn't put in the distro's ~/.agents/skills. Every agent
  there that reads that folder would get each skill, ticked or not.
- A stopped distro's agents are now shown as linked (HowLink), not as
  forced to copies.

Agents that read ~/.agents/skills on their own (readsShared, checked
against each one's source or docs when the list was made): Codex,
Gemini CLI, OpenCode, DevEco Code, Crush, dsh, Command Code, Devin,
Droid, Cline, Grok Build, Grok App, fx, Alma, Kimi Code and OmO, plus
Pi from 0.54.0 and omp from 11.0.0. Claude Code doesn't. Ticks are per
agent, so the store keeps per-agent choices working in WSL.

Existing users: nothing to do. On the next sync, each per-agent copy
magpie made in a distro is replaced by a link:
- its edit is taken into the library first (newest edit wins);
- a copy whose edit lost is set aside with the backups (wslRelink);
- unmarked folders (the user's own skills) are left alone.

Run:
- mote, at 3f1f1882a plus this change:
  - go test ./internal/library ./internal/agent -timeout 45m -count=1:
    both ok (493 and 1210 Test funcs; library 184s, agent 312s).
  - gofmt -l .: empty.
  - GOOS=windows, linux and darwin go vet of both packages: ok.
- xiaomi-gpu, Windows 11, WSL 2, distro magpie-desktop only:
  - TestWSLLinksLive passes. A link made through wsl.exe is followed by
    the distro, read back from Windows, refused over a folder and
    removed alone.
  - TestWSLSkillsLinkedLive passes. Working in a /tmp home inside the
    distro, it checks:
    - links for Codex and Claude Code that the distro follows;
    - a second sync changes nothing;
    - an edit made in WSL through the link reaches the library and
      Windows' Claude Code;
    - copies, then links again;
    - an untick removes the links and the store copy.
  - go test ./internal/library ./internal/agent -run 'WSL|Wsl|Skill'
    with MAGPIE_WSL_TEST_DISTRO set: library ok. In agent, only the
    older TestWSLCodexLive is red. It calls wslList() without -l -q, so
    it lists no distros. Filed as #5077; this change doesn't touch it.
New tests:
- TestWSLLinks (fake);
- TestWSLOldCopiesBecomeLinks, which edits a copy and then syncs;
- TestWSLEditThroughLinkReachesTheLibrary;
- TestWSLStoreSweepKeepsEdits;
- the two live tests above.
TestWSLSkillsCopied is now TestWSLSkillsLinked.

Broken by hand (logs in wsl-skills-scratch):
- WSL targets given copies again: 7 red, including TestWSLTargets,
  TestWSLSkillsLinked and TestWSLOldCopiesBecomeLinks.
- takeEdits not following the link to the store: 1 red,
  TestWSLEditThroughLinkReachesTheLibrary.
- ours not counting a link to the store: 3 red.
- wslLinkScript's folder refusal dropped: TestWSLLinksLive red on the
  real distro.

Not tried:
- the magpie GUI on Windows against a real distro, and agents running
  in a real WSL reading a skill through the link (the distro reads the
  link's files, checked with cat);
- WSL 1;
- a distro whose home is on a Windows drive (covered by the fake
  #4502 tests only);
- the GUI suite, since no GUI file changed.
2026-11-03 20:03:00 +08:00

163 lines
6.6 KiB
Go

package library
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/agent"
)
// A Pi in a WSL distro is offered MCP servers, written where the Pi there
// reads them: as on this machine, by the version the distro's pi gave its
// probe and the extensions its settings.json and the distro's npm folders
// have (leokon on Discord: only the Windows Pi was listed under MCP).
func TestWSLPiMCP(t *testing.T) {
sandbox(t)
root := map[string]string{"Ubuntu": t.TempDir(), "Debian": t.TempDir(), "Arch": t.TempDir()}
homeOf := func(d string) string { return filepath.Join(root[d], "home", "me") }
// Ubuntu: Pi 1.2.0, MCP of its own; Debian: Pi 0.98, before it, with
// no extension; Arch: Pi 0.98 with pi-mcp-adapter 2, from the
// distro's npm global folder (settings.json doesn't pin it)
write(t, filepath.Join(homeOf("Ubuntu"), ".pi", "agent", "settings.json"), "{}\n")
write(t, filepath.Join(homeOf("Debian"), ".pi", "agent", "settings.json"), "{}\n")
write(t, filepath.Join(homeOf("Arch"), ".pi", "agent", "settings.json"), `{"packages": ["npm:pi-mcp-adapter"]}`+"\n")
write(t, filepath.Join(homeOf("Arch"), ".npm-global", "lib", "node_modules", "pi-mcp-adapter", "package.json"), `{"name": "pi-mcp-adapter", "version": "2.4.0"}`)
t.Cleanup(agent.FakeWSL(map[string]string{
"Ubuntu": "home:/home/me\ndir:.pi\nbin:pi /usr/local/bin/pi\nver:pi 1.2.0\n",
"Debian": "home:/home/me\ndir:.pi\nbin:pi /usr/local/bin/pi\nver:pi 0.98.0\n",
"Arch": "home:/home/me\ndir:.pi\nbin:pi /usr/local/bin/pi\nver:pi 0.98.0\n",
}, root))
want := map[string]struct {
file string
format mcpFormat
via string
}{
"Ubuntu": {".pi/agent/mcp.json", fmtPiNative, ""},
"Debian": {".pi/agent/mcp-adapter.json", fmtPi, "pi-mcp-adapter"},
"Arch": {".pi/agent/mcp.json", fmtPi, "pi-mcp-adapter"},
}
var all []string
for d, w := range want {
id := "pi@wsl:" + d
all = append(all, id)
tg := targetByID(id)
if tg == nil {
t.Fatalf("%s isn't a target: %v", id, ids(Targets()))
}
if tg.MCP == nil || tg.MCP.Path != filepath.Join(homeOf(d), w.file) || tg.MCP.Format != w.format || tg.MCPVia != w.via ||
!tg.MCP.WSL || tg.MCP.Distro != d {
t.Errorf("%s: mcp %+v via %q, want %+v", id, tg.MCP, tg.MCPVia, w)
}
}
// the page lists it under MCP: its view names the file
view, err := Read(nil)
if err != nil {
t.Fatal(err)
}
for _, v := range view.Agents {
if strings.HasPrefix(v.ID, "pi@wsl:") && v.MCP == "" {
t.Errorf("%s has no MCP file in the page's view", v.ID)
}
}
ok(t)(SaveServer("", Server{Name: "fetch", Transport: "stdio", Command: "uvx", Args: []string{"mcp-server-fetch"}, Agents: all}))
for d, w := range want {
if s := read(t, filepath.Join(homeOf(d), w.file)); !strings.Contains(s, `"fetch"`) || !strings.Contains(s, `"uvx"`) {
t.Errorf("pi@wsl:%s %s:\n%s", d, w.file, s)
}
}
if b, _ := os.ReadFile(filepath.Join(home(), ".pi", "agent", "mcp.json")); strings.Contains(string(b), "fetch") {
t.Error("this machine's Pi got the server")
}
// a Windows program is nothing it can start
res, err := SaveServer("", Server{Name: "win", Transport: "stdio", Command: `C:\Program Files\nodejs\npx.cmd`, Args: []string{"x"}, Agents: []string{"pi@wsl:Ubuntu"}})
if err != nil {
t.Fatal(err)
}
if len(res.Problems) != 1 || res.Problems[0].Agent != "pi@wsl:Ubuntu" {
t.Errorf("problems: %+v", res.Problems)
}
if strings.Contains(read(t, filepath.Join(homeOf("Ubuntu"), ".pi/agent/mcp.json")), `"win"`) {
t.Error("the Windows program was given to pi@wsl")
}
}
// Every other agent this machine's MCP servers reach that magpie finds in a
// WSL distro is given them there too, in its own file under the distro's
// home and never a Windows program: Gemini CLI, Antigravity CLI, Crush,
// Cline, CodeBuddy Code, Kimi Code and Goose (each was set up in WSL but
// left out of the library).
func TestWSLMCPSiblings(t *testing.T) {
sandbox(t)
root := t.TempDir()
h := filepath.Join(root, "home", "me")
for _, f := range []string{
".gemini/settings.json", ".gemini/antigravity-cli/settings.json", ".config/crush/crush.json",
".cline/data/settings/providers.json", ".codebuddy/models.json", ".kimi-code/config.toml", ".config/goose/config.yaml",
} {
content := "{}\n"
if strings.HasSuffix(f, ".toml") || strings.HasSuffix(f, ".yaml") {
content = ""
}
write(t, filepath.Join(h, f), content)
}
t.Cleanup(agent.FakeWSL(map[string]string{
"Ubuntu": "home:/home/me\nbin:gemini /usr/bin/gemini\ndir:.gemini/antigravity-cli\nbin:agy /usr/bin/agy\n" +
"dir:.config/crush\nbin:crush /usr/bin/crush\ndir:.cline\nbin:cline /usr/bin/cline\n" +
"dir:.codebuddy\nbin:codebuddy /usr/bin/codebuddy\ndir:.kimi-code\nbin:kimi /usr/bin/kimi\ndir:.config/goose\n",
}, map[string]string{"Ubuntu": root}))
want := map[string]string{
"gemini@wsl:Ubuntu": ".gemini/settings.json",
"agy@wsl:Ubuntu": ".gemini/config/mcp_config.json",
"crush@wsl:Ubuntu": ".config/crush/crush.json",
"cline@wsl:Ubuntu": ".cline/data/settings/cline_mcp_settings.json",
"codebuddy@wsl:Ubuntu": ".codebuddy/.mcp.json",
"kimi@wsl:Ubuntu": ".kimi-code/mcp.json",
"goose@wsl:Ubuntu": ".config/goose/config.yaml",
}
var all []string
for id, f := range want {
all = append(all, id)
tg := targetByID(id)
if tg == nil {
t.Errorf("%s isn't a target: %v", id, ids(Targets()))
continue
}
if tg.MCP == nil || tg.MCP.Path != filepath.Join(h, f) || !tg.MCP.WSL || tg.MCP.Distro != "Ubuntu" || tg.Copy || tg.Skills != "" && tg.Store == "" {
t.Errorf("%s: mcp %+v", id, tg.MCP)
}
// nothing it is given is this machine's
for _, p := range []string{tg.Instructions, tg.Skills} {
if p != "" && !strings.HasPrefix(p, h) {
t.Errorf("%s: %s isn't in the distro", id, p)
}
}
}
if tg := targetByID("crush@wsl:Ubuntu"); tg != nil && (len(tg.SkillsAlso) != 1 || tg.SkillsAlso[0] != "claude@wsl:Ubuntu") {
t.Errorf("crush@wsl reads skills of %v", tg.SkillsAlso)
}
ok(t)(SaveServer("", Server{Name: "fetch", Transport: "stdio", Command: "uvx", Args: []string{"mcp-server-fetch"}, Agents: all}))
for id, f := range want {
if s := read(t, filepath.Join(h, f)); !strings.Contains(s, "fetch") || !strings.Contains(s, "uvx") {
t.Errorf("%s %s:\n%s", id, f, s)
}
}
res, err := SaveServer("", Server{Name: "win", Transport: "stdio", Command: `C:\Program Files\nodejs\npx.cmd`, Args: []string{"x"}, Agents: all})
if err != nil {
t.Fatal(err)
}
if len(res.Problems) != len(all) {
t.Errorf("problems: %+v", res.Problems)
}
for id, f := range want {
if strings.Contains(read(t, filepath.Join(h, f)), "npx.cmd") {
t.Errorf("the Windows program was given to %s", id)
}
}
}