From b03967bb79810eb73b24bb6fe73fb389f430f8d5 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 30 Sep 2026 22:17:23 +0800 Subject: [PATCH] =?UTF-8?q?library:=20agents=20in=20a=20running=20WSL=20di?= =?UTF-8?q?stro=20take=20skills,=20MCP=20servers=20and=20instructions=20(#?= =?UTF-8?q?331)=20=E2=80=94=20the=20Library=20knew=20only=20this=20machine?= =?UTF-8?q?'s=20agent=20ids,=20so=20codex@wsl:Ubuntu=20and=20the=20like=20?= =?UTF-8?q?were=20never=20offered,=20and=20the=20id's=20colon=20was=20refu?= =?UTF-8?q?sed=20as=20an=20agent's=20name=20and=20can't=20be=20in=20a=20Wi?= =?UTF-8?q?ndows=20file=20name.=20Claude=20Code,=20Codex,=20Pi=20and=20OmO?= =?UTF-8?q?=20in=20a=20distro=20now=20get=20them=20at=20their=20default=20?= =?UTF-8?q?places=20under=20the=20distro's=20$HOME=20through=20\\wsl.local?= =?UTF-8?q?host,=20only=20while=20it=20runs=20(opening=20a=20stopped=20one?= =?UTF-8?q?'s=20files=20starts=20it;=20its=20state=20is=20kept=20and=20ask?= =?UTF-8?q?ing=20for=20it=20says=20to=20start=20it).=20Skills=20go=20in=20?= =?UTF-8?q?as=20copies,=20made=20beside=20and=20renamed=20into=20place=20a?= =?UTF-8?q?nd=20made=20again=20when=20the=20library's=20changes,=20since?= =?UTF-8?q?=20a=20Linux=20agent=20can't=20follow=20a=20link=20to=20a=20Win?= =?UTF-8?q?dows=20folder=20(project=20skills=20share=20the=20same=20copy?= =?UTF-8?q?=20code);=20an=20MCP=20server=20that=20runs=20a=20Windows=20pro?= =?UTF-8?q?gram=20(C:\=E2=80=A6,=20*.exe/.cmd/.bat/.ps1)=20is=20refused=20?= =?UTF-8?q?for=20them=20with=20a=20message;=20Pi=20there=20gets=20no=20MCP?= =?UTF-8?q?=20servers,=20where=20its=20version=20decides=20the=20file.=20T?= =?UTF-8?q?he=20id's=20colon=20is=20a=20dot=20in=20file=20and=20folder=20n?= =?UTF-8?q?ames,=20and=20setup=20export/import=20turns=20it=20back.=20Test?= =?UTF-8?q?WSLTargets,=20TestWSLSkillsCopied,=20TestWSLServers=20and=20Tes?= =?UTF-8?q?tWSLInstructions=20(a=20fake=20distro)=20fail=20without=20the?= =?UTF-8?q?=20change.=20go=20vet=20(and=20GOOS=3Dwindows=20for=20agent,=20?= =?UTF-8?q?library),=20go=20build=20-tags=20nogui=20(darwin,=20GOOS=3Dlinu?= =?UTF-8?q?x),=20GOOS=3Dwindows=20go=20build,=20go=20test=20-tags=20nogui?= =?UTF-8?q?=20./...=20pass.=20Not=20run=20against=20a=20real=20distro.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/agent/agent.go | 5 + internal/agent/wsl.go | 8 +- internal/agent/wsl_fake.go | 46 ++++++++ internal/library/backup.go | 4 +- internal/library/carry.go | 2 +- internal/library/instructions.go | 4 +- internal/library/library.go | 19 +++ internal/library/mcp.go | 11 ++ internal/library/projects.go | 17 +-- internal/library/setup.go | 5 +- internal/library/skills.go | 44 ++++++- internal/library/targets.go | 52 +++++++++ internal/library/wsl_test.go | 195 +++++++++++++++++++++++++++++++ 13 files changed, 386 insertions(+), 26 deletions(-) create mode 100644 internal/agent/wsl_fake.go create mode 100644 internal/library/wsl_test.go diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 9412f582e..e7d6302a6 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -87,6 +87,11 @@ type Agent struct { // WSL is the distro an agent inside WSL lives in, "" for this // machine's own (see wsl.go). WSL string + // Home is a WSL agent's $HOME in its distro as magpie opens it + // (\\wsl.localhost\\home\me), where its other files are; "" + // for this machine's, and while the distro is stopped: opening it + // would start it. + Home string // Import, for an app that takes magpie only through an import link of // its own, which the user confirms there (Cindy), is that link; the app // has no fields magpie sets. Added says whether it has magpie already. diff --git a/internal/agent/wsl.go b/internal/agent/wsl.go index c20550707..fd49622c9 100644 --- a/internal/agent/wsl.go +++ b/internal/agent/wsl.go @@ -247,6 +247,9 @@ func wslAgent(k wslKind, d distro) *Agent { id := k.id + "@wsl:" + d.Name a := k.in(d.place(id)) a.ID, a.Name, a.Aliases, a.Bin, a.UA, a.WSL = id, k.name+" · WSL "+d.Name, nil, "", nil, d.Name + if d.Running { + a.Home = d.local(d.Home) + } a.detect = func() bool { return k.found(d) } // its requests carry the agent's User-Agent and are counted as its // Windows twin's, so a prompt with none of "its" own seen isn't a bypass @@ -341,7 +344,7 @@ func asleep(live *Agent, k wslKind, d distro) *Agent { // wslAgents are the agents in this machine's WSL distros; none off Windows. func wslAgents() []*Agent { - if runtime.GOOS != "windows" { + if !wslOn { return nil } return wslAgentsOf(wslDistros()) @@ -382,6 +385,9 @@ const ( wslRetryAge = 10 * time.Minute ) +// wslOn is whether there is WSL to look in: on Windows, or in tests. +var wslOn = runtime.GOOS == "windows" + // wslRun runs wsl.exe; a var for tests. var wslRun = func(timeout time.Duration, args ...string) ([]byte, error) { ctx, cancel := context.WithTimeout(context.Background(), timeout) diff --git a/internal/agent/wsl_fake.go b/internal/agent/wsl_fake.go new file mode 100644 index 000000000..6b912f8c2 --- /dev/null +++ b/internal/agent/wsl_fake.go @@ -0,0 +1,46 @@ +package agent + +import ( + "errors" + "strings" + "time" +) + +// FakeWSL, for other packages' tests, stands in for wsl.exe on any +// system: each distro named in probes is installed and running, answers +// the probe with what probes has for it (wslProbeScript's output: "home:/ +// home/me\ndir:.codex\n…") and is opened at roots' folder for it. What +// magpie remembers of WSL is forgotten, before and after; the returned +// func puts everything back. +func FakeWSL(probes, roots map[string]string) func() { + on, run, root := wslOn, wslRun, wslRoot + reset := func() { + wsl.Lock() + wsl.seen, wsl.at, wsl.names, wsl.running, wsl.probed, wsl.failed, wsl.dirty = nil, time.Time{}, nil, nil, nil, nil, false + wsl.Unlock() + } + reset() + var names []string + for n := range probes { + names = append(names, n) + } + list := []byte(strings.Join(names, "\n")) + wslOn = true + wslRun = func(_ time.Duration, args ...string) ([]byte, error) { + switch strings.Join(args, " ") { + case "-l -q", "-l --running -q": + return list, nil + } + if len(args) > 2 && args[0] == "-d" { + if p, ok := probes[args[1]]; ok { + return []byte(p), nil + } + } + return nil, errors.New("unexpected wsl.exe " + strings.Join(args, " ")) + } + wslRoot = func(name string) string { return roots[name] } + return func() { + wslOn, wslRun, wslRoot = on, run, root + reset() + } +} diff --git a/internal/library/backup.go b/internal/library/backup.go index 2b82e8f5e..2cb6e937c 100644 --- a/internal/library/backup.go +++ b/internal/library/backup.go @@ -45,11 +45,11 @@ func (b *backups) keep(agent, path string) error { if b.dir == "" { b.dir = filepath.Join(BackupDir(), time.Now().Format("2006-01-02_15-04-05.000")) } - dst := filepath.Join(b.dir, agent, filepath.Base(path)) + dst := filepath.Join(b.dir, fileName(agent), filepath.Base(path)) if _, err := os.Stat(dst); err == nil { // another file by that name (each dsh profile's cordis.patch.yml): // kept under its folder's name - dst = filepath.Join(b.dir, agent, filepath.Base(filepath.Dir(path)), filepath.Base(path)) + dst = filepath.Join(b.dir, fileName(agent), filepath.Base(filepath.Dir(path)), filepath.Base(path)) } if err := os.MkdirAll(filepath.Dir(dst), 0o700); err != nil { return err diff --git a/internal/library/carry.go b/internal/library/carry.go index feb61155e..136a1a92b 100644 --- a/internal/library/carry.go +++ b/internal/library/carry.go @@ -154,7 +154,7 @@ func (b *Bundle) check() error { } } for id := range b.Extra { - if err := checkName("agent", id); err != nil { + if err := checkAgent(id); err != nil { return err } } diff --git a/internal/library/instructions.go b/internal/library/instructions.go index eae06150e..163af0a4e 100644 --- a/internal/library/instructions.go +++ b/internal/library/instructions.go @@ -68,7 +68,7 @@ func (l *Library) active() string { func (l *Library) sharedPath() string { return setPath(l.active()) } func extraPath(agent string) string { - return filepath.Join(Dir(), "instructions", agent+".md") + return filepath.Join(Dir(), "instructions", fileName(agent)+".md") } func readText(path string) string { @@ -298,7 +298,7 @@ func SaveInstructions(c InstructionsChange) (*Result, error) { if x == nil { continue } - if err := checkName("agent", id); err != nil { + if err := checkAgent(id); err != nil { return err } if err := writeText(extraPath(id), *x); err != nil { diff --git a/internal/library/library.go b/internal/library/library.go index 80debec86..630a42d69 100644 --- a/internal/library/library.go +++ b/internal/library/library.go @@ -12,6 +12,7 @@ import ( "regexp" "slices" "sort" + "strings" "sync" "github.com/yetone/magpie/internal/edit" @@ -138,6 +139,24 @@ func checkName(kind, name string) error { return nil } +// agentRe is an agent's id: a name, or one in a WSL distro's +// (codex@wsl:Ubuntu-24.04). +var agentRe = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_-]{0,63}(@wsl:[A-Za-z0-9][A-Za-z0-9._-]{0,63})?$`) + +func checkAgent(id string) error { + if !agentRe.MatchString(id) { + return fmt.Errorf("agent %q: not an agent's id", id) + } + return nil +} + +// fileName is an agent's id as the name of a file or folder of its: +// Windows has no colon in one, so a WSL agent's is codex@wsl.Ubuntu. +func fileName(id string) string { return strings.Replace(id, "@wsl:", "@wsl.", 1) } + +// agentOfFile is the agent's id a fileName is of. +func agentOfFile(name string) string { return strings.Replace(name, "@wsl.", "@wsl:", 1) } + // set turns id on or off in a list of agent ids, which stays sorted. func set(list []string, id string, on bool) []string { i := slices.Index(list, id) diff --git a/internal/library/mcp.go b/internal/library/mcp.go index 05ee4e8d9..64fd52091 100644 --- a/internal/library/mcp.go +++ b/internal/library/mcp.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "maps" + "regexp" "slices" "sort" "strings" @@ -148,6 +149,9 @@ type mcpFile struct { // library, but not written: Pi's mcp.json that pi-mcp-adapter no longer // reads, with what couldn't be moved from it. Extra []string + // WSL: the agent runs in a WSL distro, where a Windows program isn't + // one it can start + WSL bool } // files are every file the servers are written into. @@ -179,9 +183,16 @@ func (f *mcpFile) supports(s *Server) error { if f.Format == fmtDsh && s.Name != "" && !dshServerName.MatchString(s.Name) { return errDshName } + if f.WSL && !s.Remote() && windowsPath.MatchString(s.Command) { + return fmt.Errorf("it runs a Windows program (%s), which an agent in WSL can't start: give it a command WSL has", s.Command) + } return nil } +// windowsPath is a program named as Windows names one: C:/…, a path with a +// backslash in it, or …/npx.cmd. +var windowsPath = regexp.MustCompile(`^[A-Za-z]:[\\/]|\\|(?i)\.(?:exe|cmd|bat|ps1)$`) + // errNoRemote is what the page says of an app that reaches only a server // it runs itself (Claude Desktop, whose remote ones are its Connectors). var errNoRemote = errors.New("no-remote") diff --git a/internal/library/projects.go b/internal/library/projects.go index f058e0149..13e8fd93f 100644 --- a/internal/library/projects.go +++ b/internal/library/projects.go @@ -170,22 +170,7 @@ func (p *Project) place(e, name string) error { } return link(abs, name) } - // the new copy is made beside and put in the old one's place - next := filepath.Join(filepath.Dir(abs), "."+name+".magpie-next") - os.RemoveAll(next) - if err := copyDir(realDir(lib), next); err != nil { - os.RemoveAll(next) - return err - } - if err := os.WriteFile(filepath.Join(next, marker), []byte("copied from "+lib+" by magpie, and copied again when it changes\n"), 0o644); err != nil { - os.RemoveAll(next) - return err - } - if err := unlink(abs); err != nil { - os.RemoveAll(next) - return err - } - return os.Rename(next, abs) + return copyIn(abs, name) } // mkdirs makes dir and the folders above it the project hasn't, noting diff --git a/internal/library/setup.go b/internal/library/setup.go index d957c74d9..1d70cad30 100644 --- a/internal/library/setup.go +++ b/internal/library/setup.go @@ -101,7 +101,8 @@ func extras() map[string]string { es, _ := os.ReadDir(filepath.Dir(extraPath("x"))) for _, e := range es { id, ok := strings.CutSuffix(e.Name(), ".md") - if !ok || e.IsDir() || checkName("agent", id) != nil { + id = agentOfFile(id) + if !ok || e.IsDir() || checkAgent(id) != nil { continue } if text := readText(extraPath(id)); text != "" { @@ -162,7 +163,7 @@ func Restore(s *Setup) (*Result, error) { texts[extraPath(id)] = "" } for id, text := range s.Instructions.Extra { - if err := checkName("agent", id); err != nil { + if err := checkAgent(id); err != nil { return err } texts[extraPath(id)] = text diff --git a/internal/library/skills.go b/internal/library/skills.go index 1caf430d3..3b5229b27 100644 --- a/internal/library/skills.go +++ b/internal/library/skills.go @@ -155,6 +155,34 @@ func link(p, name string) error { return os.WriteFile(filepath.Join(p, marker), []byte("copied from "+skillDir(name)+"\n"), 0o644) } +// copyIn puts a copy of the library's skill at p, marked as magpie's, in +// place of a link: made beside it and put in the place of what is there, +// so the agent never finds half of one. +func copyIn(p, name string) error { + lib := skillDir(name) + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + return err + } + next := filepath.Join(filepath.Dir(p), "."+name+".magpie-next") + os.RemoveAll(next) + if err := copyDir(realDir(lib), next); err != nil { + os.RemoveAll(next) + return err + } + if err := os.WriteFile(filepath.Join(next, marker), []byte("copied from "+lib+" by magpie, and copied again when it changes\n"), 0o644); err != nil { + os.RemoveAll(next) + return err + } + if err := unlink(p); err != nil { + os.RemoveAll(next) + return err + } + return os.Rename(next, p) +} + +// fresh is whether the copy at p holds what the library's skill does. +func fresh(p, name string) bool { return hashDir(p) == hashDir(realDir(skillDir(name))) } + func unlink(p string) error { fi, err := os.Lstat(p) if err != nil { @@ -304,6 +332,14 @@ func (l *Library) syncSkills(t *Target, res *Result, all []*Target) { // the folder the library's skill links to is there already: one // brought in from ~/.agents/skills, which stays where it is if ours(p, s.Name) || realDir(p) == realDir(skillDir(s.Name)) { + // a copy is made again once the library's skill has changed + if t.Copy && ours(p, s.Name) && (linked(p) || !fresh(p, s.Name)) { + if err := copyIn(p, s.Name); err != nil { + res.fail(id, "skill:"+s.Name, err) + } else { + res.changed(id) + } + } mine = append(mine, s.Name) continue } @@ -323,7 +359,11 @@ func (l *Library) syncSkills(t *Target, res *Result, all []*Target) { continue } } - if err := link(p, s.Name); err != nil { + put := link + if t.Copy { + put = copyIn + } + if err := put(p, s.Name); err != nil { res.fail(id, "skill:"+s.Name, err) continue } @@ -1223,7 +1263,7 @@ func (e *leftBehind) Unwrap() error { return e.err } // setAside moves an agent's own skill (a folder, or a link) out of its // way, into a backup of its own: nothing of the user's is just deleted. func setAside(agent, p string) (string, error) { - dst := filepath.Join(BackupDir(), time.Now().Format("2006-01-02_15-04-05.000"), agent, "skills", filepath.Base(p)) + dst := filepath.Join(BackupDir(), time.Now().Format("2006-01-02_15-04-05.000"), fileName(agent), "skills", filepath.Base(p)) for i := 2; ; i++ { if _, err := os.Lstat(dst); errors.Is(err, fs.ErrNotExist) { break diff --git a/internal/library/targets.go b/internal/library/targets.go index 74c12bcfc..014ccb797 100644 --- a/internal/library/targets.go +++ b/internal/library/targets.go @@ -28,6 +28,10 @@ type Target struct { MCPVia string // Note is what the page says of the agent's instructions file. Note string + // Copy is an agent that gets a copy of each skill, made again when + // the library's changes, rather than a link: one in a WSL distro, + // which can't follow a link to a Windows folder. + Copy bool } func home() string { h, _ := os.UserHomeDir(); return h } @@ -58,6 +62,9 @@ func codexDir() string { // targetOf is where a known agent keeps them, or nil for one magpie can't // give any of them to. func targetOf(a *agent.Agent) *Target { + if a.WSL != "" { + return wslTargetOf(a) + } h := home() t := &Target{Agent: a} switch a.ID { @@ -261,6 +268,48 @@ func targetOf(a *agent.Agent) *Target { return t } +// wslTargetOf is where an agent in a WSL distro keeps them: its files at +// their defaults under the distro's $HOME (the distro's variables that +// move them aren't read), opened through \\wsl.localhost; nil while the +// distro is stopped, which opening them would start. What is written is +// the same as for this machine's agent: nothing in it names a place on +// Windows, and each skill is a copy. +func wslTargetOf(a *agent.Agent) *Target { + h := a.Home + if h == "" { + return nil + } + t := &Target{Agent: a, Copy: true} + id, _, _ := strings.Cut(a.ID, "@") + switch id { + case "claude": + d := filepath.Join(h, ".claude") + t.Instructions = filepath.Join(d, "CLAUDE.md") + t.MCP = &mcpFile{Path: filepath.Join(h, ".claude.json"), Format: fmtClaude, WSL: true} + t.Skills = filepath.Join(d, "skills") + case "codex": + d := filepath.Join(h, ".codex") + t.Instructions = filepath.Join(d, "AGENTS.md") + t.Override = filepath.Join(d, "AGENTS.override.md") + t.MCP = &mcpFile{Path: filepath.Join(d, "config.toml"), Format: fmtCodex, WSL: true} + t.Skills = filepath.Join(d, "skills") + case "pi": + // its MCP servers go where the Pi installed there reads them, + // which its version decides (piMCP), and that isn't known here + d := filepath.Join(h, ".pi", "agent") + t.Instructions = filepath.Join(d, "AGENTS.md") + t.Skills = filepath.Join(d, "skills") + case "omo": + d := filepath.Join(h, ".omo", "agent") + t.Instructions = filepath.Join(d, "AGENTS.md") + t.MCP = &mcpFile{Path: filepath.Join(d, "mcp.json"), Format: fmtPiNative, WSL: true} + t.Skills = filepath.Join(d, "skills") + default: + return nil + } + return t +} + // apps are what the library can give MCP servers to that aren't agents // magpie sets up: known by the folder they keep their settings in. func apps() []*agent.Agent { @@ -384,6 +433,9 @@ func Takes(q, kind string) (string, error) { t := targetOf(a) var has bool what := map[string]string{"instructions": "instructions", "mcp": "MCP servers", "skills": "skills"}[kind] + if t == nil && a.WSL != "" && a.Home == "" { + return "", fmt.Errorf("WSL %s isn't running: start it, and %s can be given %s", a.WSL, a.Name, what) + } if t != nil { switch kind { case "instructions": diff --git a/internal/library/wsl_test.go b/internal/library/wsl_test.go new file mode 100644 index 000000000..5e5ca9bb6 --- /dev/null +++ b/internal/library/wsl_test.go @@ -0,0 +1,195 @@ +package library + +import ( + "os" + "path/filepath" + "slices" + "strings" + "testing" + + "github.com/yetone/magpie/internal/agent" +) + +// wslSandbox is sandbox with a running WSL distro, Ubuntu-24.04, whose / +// is a temp folder and whose user /home/me has Codex, Claude Code, Pi and +// OmO; it gives back that user's home as magpie opens it. +func wslSandbox(t *testing.T) string { + t.Helper() + sandbox(t) + root := t.TempDir() + home := filepath.Join(root, "home", "me") + write(t, filepath.Join(home, ".codex", "config.toml"), "model = \"gpt-5.5\"\n") + write(t, filepath.Join(home, ".claude", "settings.json"), "{}\n") + write(t, filepath.Join(home, ".pi", "agent", "settings.json"), "{}\n") + write(t, filepath.Join(home, ".omo", "agent", "settings.json"), "{}\n") + t.Cleanup(agent.FakeWSL(map[string]string{ + "Ubuntu-24.04": "home:/home/me\ndir:.codex\ndir:.claude\ndir:.pi\ndir:.omo\n", + }, map[string]string{"Ubuntu-24.04": root})) + return home +} + +const ( + wslCodex = "codex@wsl:Ubuntu-24.04" + wslClaude = "claude@wsl:Ubuntu-24.04" + wslPi = "pi@wsl:Ubuntu-24.04" + wslOmO = "omo@wsl:Ubuntu-24.04" +) + +// The agents magpie found in a WSL distro are the library's to give to +// (#331), each at its own files in the distro's home, not Windows'. +func TestWSLTargets(t *testing.T) { + h := wslSandbox(t) + want := map[string][3]string{ // instructions, MCP, skills + wslCodex: {".codex/AGENTS.md", ".codex/config.toml", ".codex/skills"}, + wslClaude: {".claude/CLAUDE.md", ".claude.json", ".claude/skills"}, + wslPi: {".pi/agent/AGENTS.md", "", ".pi/agent/skills"}, + wslOmO: {".omo/agent/AGENTS.md", ".omo/agent/mcp.json", ".omo/agent/skills"}, + } + for id, w := range want { + tg := targetByID(id) + if tg == nil { + t.Fatalf("%s isn't a target: %v", id, ids(Targets())) + } + mcp := "" + if tg.MCP != nil { + mcp = tg.MCP.Path + } + at := func(rel string) string { + if rel == "" { + return "" + } + return filepath.Join(h, rel) + } + if tg.Instructions != at(w[0]) || mcp != at(w[1]) || tg.Skills != at(w[2]) || !tg.Copy { + t.Errorf("%s: %+v (mcp %s)", id, tg, mcp) + } + } + for _, kind := range []string{"instructions", "mcp", "skills"} { + if id, err := Takes("Codex · WSL Ubuntu-24.04", kind); err != nil || id != wslCodex { + t.Errorf("Takes %s: %s %v", kind, id, err) + } + } + // a stopped distro's is none: opening its files would start it + if tg := targetOf(&agent.Agent{ID: "codex@wsl:Stopped", WSL: "Stopped"}); tg != nil { + t.Errorf("stopped distro: %+v", tg) + } +} + +// A skill reaches an agent in WSL as a copy — a link to a Windows folder +// is nothing it can follow — which is copied again when the library's +// changes, and taken away with the rest. +func TestWSLSkillsCopied(t *testing.T) { + h := wslSandbox(t) + src := filepath.Join(home(), "src", "skills") + skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs") + ok(t)(InstallSkills(src, []string{"pdf"}, []string{"claude", wslCodex, wslClaude, wslPi})) + for _, d := range []string{".codex/skills/pdf", ".claude/skills/pdf", ".pi/agent/skills/pdf"} { + p := filepath.Join(h, d) + fi, err := os.Lstat(p) + if err != nil || !fi.IsDir() { + t.Fatalf("%s: not a folder: %v", d, err) + } + if !strings.Contains(read(t, filepath.Join(p, "SKILL.md")), "Read PDFs") || read(t, filepath.Join(p, "scripts/run.sh")) != "echo hi\n" || !ours(p, "pdf") { + t.Errorf("%s isn't the library's skill", d) + } + } + if !linked(filepath.Join(home(), ".claude/skills/pdf")) { + t.Error("this machine's Claude Code got a copy, not a link") + } + // the skill is its folder: an edit there reaches the copies at the next sync + write(t, filepath.Join(src, "pdf/SKILL.md"), "---\nname: pdf\ndescription: Changed\n---\n") + res := ok(t)(Sync()) + if !slices.Contains(res.Changed, wslCodex) { + t.Errorf("changed: %v", res.Changed) + } + if !strings.Contains(read(t, filepath.Join(h, ".codex/skills/pdf/SKILL.md")), "Changed") { + t.Error("the copy wasn't made again") + } + if res := ok(t)(Sync()); len(res.Changed) != 0 { + t.Errorf("a sync with nothing new wrote %v", res.Changed) + } + ok(t)(SkillAgents("pdf", []string{"claude", wslClaude})) + if _, err := os.Lstat(filepath.Join(h, ".codex/skills/pdf")); !os.IsNotExist(err) { + t.Errorf("codex@wsl still has it: %v", err) + } + if !ours(filepath.Join(h, ".claude/skills/pdf"), "pdf") { + t.Error("claude@wsl lost it") + } +} + +// MCP servers are written into the distro's files in each agent's own +// format; one that runs a Windows program isn't given to them. +func TestWSLServers(t *testing.T) { + h := wslSandbox(t) + ok(t)(SaveServer("", Server{Name: "fetch", Transport: "stdio", Command: "uvx", Args: []string{"mcp-server-fetch"}, + Agents: []string{"codex", wslCodex, wslClaude, wslOmO}})) + if s := read(t, filepath.Join(h, ".codex/config.toml")); !strings.Contains(s, "[mcp_servers.fetch]") || !strings.Contains(s, `command = "uvx"`) || !strings.Contains(s, `model = "gpt-5.5"`) { + t.Errorf("codex@wsl config.toml:\n%s", s) + } + if s := read(t, filepath.Join(h, ".claude.json")); !strings.Contains(s, `"fetch"`) || !strings.Contains(s, `"uvx"`) { + t.Errorf("claude@wsl .claude.json:\n%s", s) + } + if s := read(t, filepath.Join(h, ".omo/agent/mcp.json")); !strings.Contains(s, `"fetch"`) { + t.Errorf("omo@wsl mcp.json:\n%s", s) + } + if s := read(t, filepath.Join(home(), ".codex/config.toml")); !strings.Contains(s, "[mcp_servers.fetch]") { + t.Errorf("this machine's codex:\n%s", s) + } + + res, err := SaveServer("", Server{Name: "win", Transport: "stdio", Command: `C:\Program Files\nodejs\npx.cmd`, Args: []string{"x"}, + Agents: []string{"codex", wslCodex}}) + if err != nil { + t.Fatal(err) + } + if len(res.Problems) != 1 || res.Problems[0].Agent != wslCodex || res.Problems[0].What != "mcp:win" { + t.Errorf("problems: %+v", res.Problems) + } + if strings.Contains(read(t, filepath.Join(h, ".codex/config.toml")), "mcp_servers.win") { + t.Error("the Windows program was given to codex@wsl") + } + if !strings.Contains(read(t, filepath.Join(home(), ".codex/config.toml")), "mcp_servers.win") { + t.Error("this machine's codex didn't get it") + } +} + +// The shared instructions, and what one WSL agent gets besides, go into +// its file in the distro; the files magpie keeps of it are named without +// the colon Windows can't have in a name. +func TestWSLInstructions(t *testing.T) { + h := wslSandbox(t) + write(t, filepath.Join(h, ".codex/AGENTS.md"), "Mine.\n") + shared, extra := "Be brief.", "Use apt." + res := ok(t)(SaveInstructions(InstructionsChange{Shared: &shared, Agents: []string{wslCodex, wslClaude}, + Extra: map[string]*string{wslCodex: &extra}})) + s := read(t, filepath.Join(h, ".codex/AGENTS.md")) + if !strings.HasPrefix(s, "Mine.\n") || !strings.Contains(s, "Be brief.\n\nUse apt.") { + t.Errorf("codex@wsl AGENTS.md:\n%s", s) + } + if s := read(t, filepath.Join(h, ".claude/CLAUDE.md")); !strings.Contains(s, "Be brief.") || strings.Contains(s, "apt") { + t.Errorf("claude@wsl CLAUDE.md:\n%s", s) + } + // what it gets besides, and its file kept before the change + for _, dir := range []string{filepath.Dir(extraPath(wslCodex)), res.Backup} { + es, _ := os.ReadDir(dir) + var names []string + for _, e := range es { + names = append(names, e.Name()) + } + if !slices.ContainsFunc(names, func(n string) bool { return strings.HasPrefix(n, "codex@wsl") && !strings.Contains(n, ":") }) || + slices.ContainsFunc(names, func(n string) bool { return strings.Contains(n, ":") }) { + t.Errorf("%s: %v", dir, names) + } + } + // a setup carried elsewhere keeps what it gets besides + if x := extras(); x[wslCodex] != extra { + t.Errorf("extras: %v", x) + } + iv, err := ReadInstructions() + if err != nil { + t.Fatal(err) + } + i := slices.IndexFunc(iv.Agents, func(a AgentInstructions) bool { return a.Agent == wslCodex }) + if i < 0 || !iv.Agents[i].On || iv.Agents[i].Extra != extra || iv.Agents[i].Own != 1 { + t.Errorf("instructions page: %+v", iv.Agents) + } +}