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.
This commit is contained in:
yetone
2026-11-03 20:03:00 +08:00
parent 6c201095d8
commit 22f5cf69e5
17 changed files with 952 additions and 33 deletions
File diff suppressed because one or more lines are too long
+77 -2
View File
@@ -2,6 +2,8 @@ package agent
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
@@ -15,7 +17,7 @@ import (
// 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
on, run, root, exec := wslOn, wslRun, wslRoot, wslExec
reset := func() {
wsl.Lock()
wsl.seen, wsl.at, wsl.runAt, wsl.names, wsl.running, wsl.probed, wsl.failed, wsl.dirty = nil, time.Time{}, time.Time{}, nil, nil, nil, nil, false
@@ -62,8 +64,20 @@ func FakeWSL(probes, roots map[string]string) func() {
return nil, errors.New("unexpected wsl.exe " + strings.Join(args, " "))
}
wslRoot = func(name string) string { return roots[name] }
wslExec = func(name string, argv ...string) ([]byte, error) {
fakeCalls.Lock()
fakeCalls.in[name]++
fakeCalls.Unlock()
return fakeLinks(roots[name], argv)
}
links.Lock()
links.m = nil
links.Unlock()
return func() {
wslOn, wslRun, wslRoot = on, run, root
wslOn, wslRun, wslRoot, wslExec = on, run, root, exec
links.Lock()
links.m = nil
links.Unlock()
fakeStop = nil
reset()
}
@@ -116,3 +130,64 @@ func FakeWSLDrives(roots map[string]string) func() {
}
return func() { wslDrive = was }
}
// fakeLinks does what wsl_links.go's scripts do in a distro opened at
// root, on this system's own links: a link the distro would lead to
// /home/me/x leads to root's home/me/x here, which this system follows.
func fakeLinks(root string, argv []string) ([]byte, error) {
if len(argv) < 5 || argv[0] != "sh" || argv[1] != "-c" {
return nil, errors.New("unexpected command in WSL: " + strings.Join(argv, " "))
}
at := func(linux string) string { return filepath.Join(root, filepath.FromSlash(linux)) }
args := argv[4:]
switch argv[2] {
case wslLinkScript:
to, link := at(args[0]), at(args[1])
if fi, err := os.Lstat(link); err == nil && fi.Mode()&os.ModeSymlink == 0 {
return []byte(args[1] + " is not a link"), errors.New("exit status 3")
}
if err := os.MkdirAll(filepath.Dir(link), 0o755); err != nil {
return nil, err
}
os.Remove(link)
return nil, os.Symlink(to, link)
case wslUnlinkScript:
p := at(args[0])
fi, err := os.Lstat(p)
if err != nil {
return nil, nil
}
if fi.Mode()&os.ModeSymlink == 0 {
return []byte(args[0] + " is not a link"), errors.New("exit status 3")
}
return nil, os.Remove(p)
case wslLinksScript:
es, _ := os.ReadDir(at(args[0]))
var b strings.Builder
for _, e := range es {
if to, err := os.Readlink(filepath.Join(at(args[0]), e.Name())); err == nil {
// spelt as the distro spells it
if rel, ok := strings.CutPrefix(to, root); ok {
to = filepath.ToSlash(rel)
}
fmt.Fprintf(&b, "%s\t%s\n", e.Name(), to)
}
}
return []byte(b.String()), nil
}
return nil, errors.New("unexpected script in WSL: " + argv[2])
}
// realWSLRun is wsl.exe itself, for FakeWSLRealLinks.
var realWSLRun = wslRun
// FakeWSLRealLinks, for a test on Windows with a real distro behind
// FakeWSL's root (\\wsl.localhost\<distro>), has the links in it made,
// read and removed by the real wsl.exe, in that distro alone.
func FakeWSLRealLinks() (restore func()) {
exec := wslExec
wslExec = func(distro string, argv ...string) ([]byte, error) {
return realWSLRun(30*time.Second, append([]string{"-d", distro, "--exec"}, argv...)...)
}
return func() { wslExec = exec }
}
+209
View File
@@ -0,0 +1,209 @@
package agent
// Links in a WSL distro, made, read and taken away from inside it.
//
// Through \\wsl.localhost Windows sees a link the distro has as an entry
// it can't follow: Lstat says it is there (Go reports it irregular), but
// Readlink fails ("Incorrect function"), and opening, listing or removing
// it says it isn't there. A link Windows makes there (os.Symlink) is an
// empty folder in the distro, not a link at all. So magpie makes, reads
// and removes a distro's links with wsl.exe, in the distro, where they are
// read (tried on Windows 11 with WSL 2 and Ubuntu 24.04).
import (
"errors"
"path"
"strings"
"sync"
"time"
)
// wslExec runs argv in the distro; a var for tests.
var wslExec = func(distro string, argv ...string) ([]byte, error) {
return wslRun(30*time.Second, append([]string{"-d", distro, "--exec"}, argv...)...)
}
// distroAt is the running distro local, a path as magpie opens it, is in:
// under its root, or under where one of its Links leads (distro.native).
func distroAt(local string) (distro, bool) {
wsl.Lock()
defer wsl.Unlock()
// a command run in a stopped distro would start it (wslUp)
wslRunningLocked()
for name, d := range wsl.seen {
if d == nil || d.Root == "" || !wsl.running[name] {
continue
}
if _, ok := below(local, d.Root); ok {
return *d, true
}
for _, to := range d.Links {
if to == "" {
continue
}
if _, ok := below(local, d.where(to)); ok {
return *d, true
}
}
}
return distro{}, false
}
// agentDistro is the distro a's files are in, as last probed.
func agentDistro(a *Agent) (distro, error) {
if a == nil || a.WSL == "" {
return distro{}, errors.New("not an agent in WSL")
}
wsl.Lock()
d := wsl.seen[a.WSL]
wsl.Unlock()
if d == nil || d.Root == "" {
return distro{}, errors.New("WSL " + a.WSL + " hasn't been looked in")
}
return *d, nil
}
// wslLinkScript makes $2 a link to $1: in place of a link there, never of
// a folder or file, which it refuses with 3 (said on stdout, which is
// what wslRun reads).
const wslLinkScript = `if [ -e "$2" ] && [ ! -L "$2" ]; then echo "$2 is not a link"; exit 3; fi
mkdir -p -- "$(dirname -- "$2")" && ln -sfn -- "$1" "$2"`
// wslUnlinkScript removes $1 when it is a link, and nothing else.
const wslUnlinkScript = `if [ -L "$1" ]; then rm -f -- "$1"; elif [ -e "$1" ]; then echo "$1 is not a link"; exit 3; fi`
// wslLinksScript lists the links in the folder $1, one name, a tab and
// where it leads to a line.
const wslLinksScript = `for f in "$1"/* "$1"/.[!.]*; do [ -L "$f" ] && printf '%s\t%s\n' "${f##*/}" "$(readlink -- "$f")"; done; true`
// WSLSymlink makes link a link to target, both in a's distro as magpie
// opens them, from inside the distro: a link the agent there follows. A
// link at link already is replaced; a folder or file there is refused.
func WSLSymlink(a *Agent, target, link string) error {
d, err := agentDistro(a)
if err != nil {
return err
}
out, err := wslExec(d.Name, "sh", "-c", wslLinkScript, "sh", d.native(target), d.native(link))
forgetLinks(link)
if err != nil {
return wslFailed("link "+d.native(link), out, err)
}
return nil
}
// WSLRemoveLink takes the link at local, a path in a running distro as
// magpie opens it, away from inside the distro; what it leads to stays.
// Anything there but a link is refused.
func WSLRemoveLink(local string) error {
d, ok := distroAt(local)
if !ok {
return errors.New(local + " isn't in a running WSL distro")
}
out, err := wslExec(d.Name, "sh", "-c", wslUnlinkScript, "sh", d.native(local))
forgetLinks(local)
if err != nil {
return wslFailed("remove "+d.native(local), out, err)
}
return nil
}
func wslFailed(what string, out []byte, err error) error {
if s := strings.TrimSpace(string(out)); s != "" {
return errors.New(what + " in WSL: " + s)
}
return errors.New(what + " in WSL: " + err.Error())
}
// links keeps each folder's links as the distro listed them, for
// linksAge: a page read looks at every agent's skills, each a wsl.exe
// otherwise.
var links struct {
sync.Mutex
m map[string]linkList
}
type linkList struct {
at time.Time
to map[string]string // by name, as the distro spells where it leads
}
const linksAge = 3 * time.Second
// forgetLinks drops what was listed of the folder local is in.
func forgetLinks(local string) {
dir, _ := splitLocal(local)
links.Lock()
delete(links.m, strings.ToLower(dir))
links.Unlock()
}
// splitLocal is local's folder and name, cut at its last separator.
func splitLocal(local string) (string, string) {
i := strings.LastIndexAny(local, `\/`)
if i < 0 {
return "", local
}
return local[:i], local[i+1:]
}
// WSLReadlink is where the link at local, a path in a running distro as
// magpie opens it, leads, as magpie opens that: false when local is in no
// running distro or isn't a link there. It is asked where Windows can't
// read the link itself (Readlink fails through \\wsl.localhost).
func WSLReadlink(local string) (string, bool) {
d, ok := distroAt(local)
if !ok {
return "", false
}
dir, name := splitLocal(local)
key := strings.ToLower(dir)
links.Lock()
l, ok := links.m[key]
links.Unlock()
if !ok || time.Since(l.at) > linksAge {
out, err := wslExec(d.Name, "sh", "-c", wslLinksScript, "sh", d.native(dir))
if err != nil {
return "", false
}
l = linkList{at: time.Now(), to: parseLinks(string(out))}
links.Lock()
if links.m == nil {
links.m = map[string]linkList{}
}
links.m[key] = l
links.Unlock()
}
to, ok := l.to[name]
if !ok || to == "" {
return "", false
}
if !strings.HasPrefix(to, "/") {
to = path.Join(d.native(dir), to)
}
return d.where(path.Clean(to)), true
}
// parseLinks reads wslLinksScript's lines.
func parseLinks(out string) map[string]string {
m := map[string]string{}
for _, line := range strings.Split(strings.ReplaceAll(out, "\r\n", "\n"), "\n") {
if name, to, ok := strings.Cut(line, "\t"); ok && name != "" {
m[name] = to
}
}
return m
}
// WSLInDistro is whether local, a path of a's as magpie opens it, is in
// the distro's own files (under \\wsl.localhost\<distro>), not on a
// Windows drive a link there leads to: where the distro can make a link
// magpie reads back.
func WSLInDistro(a *Agent, local string) bool {
d, err := agentDistro(a)
if err != nil || local == "" {
return false
}
_, ok := below(local, d.Root)
return ok
}
+139
View File
@@ -0,0 +1,139 @@
package agent
import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
)
// A link made in a distro (WSLSymlink) is read back where Windows can't
// read it (WSLReadlink), made again over itself, never over a folder, and
// taken away alone (WSLRemoveLink), what it leads to left as it is.
func TestWSLLinks(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the fake distro's links are this system's")
}
root := t.TempDir()
t.Cleanup(FakeWSL(map[string]string{"U": "home:/home/me\ndir:.codex\n"}, map[string]string{"U": root}))
wslDistros()
a := &Agent{ID: "codex@wsl:U", WSL: "U"}
home := filepath.Join(root, "home", "me")
store := filepath.Join(home, ".local", "share", "magpie", "skills", "pdf")
if err := os.MkdirAll(store, 0o755); err != nil {
t.Fatal(err)
}
link := filepath.Join(home, ".codex", "skills", "pdf")
for range 2 {
if err := WSLSymlink(a, store, link); err != nil {
t.Fatal(err)
}
}
if got, ok := WSLReadlink(link); !ok || got != store {
t.Fatalf("read back: %q %v", got, ok)
}
if _, ok := WSLReadlink(filepath.Join(home, ".codex", "skills", "none")); ok {
t.Error("a name that isn't there read as a link")
}
if _, ok := WSLReadlink(filepath.Join(t.TempDir(), "x")); ok {
t.Error("a path in no distro read as a link")
}
if err := WSLSymlink(a, store, filepath.Dir(store)); err == nil || !strings.Contains(err.Error(), "is not a link") {
t.Errorf("a link made over a folder: %v", err)
}
if err := WSLRemoveLink(store); err == nil {
t.Error("a folder removed as a link")
}
if err := WSLRemoveLink(link); err != nil {
t.Fatal(err)
}
if _, err := os.Lstat(link); !os.IsNotExist(err) {
t.Errorf("the link is still there: %v", err)
}
if _, ok := WSLReadlink(link); ok {
t.Error("a link taken away still reads as one")
}
if _, err := os.Stat(store); err != nil {
t.Errorf("what it led to went with it: %v", err)
}
}
// On a real WSL: MAGPIE_WSL_TEST_DISTRO names the distro, and the test
// works only in a folder of its own it makes in /tmp there. What Windows
// can't do through \\wsl.localhost (read, follow or remove a link the
// distro has) is done in the distro, and the distro follows the link.
func TestWSLLinksLive(t *testing.T) {
name := os.Getenv("MAGPIE_WSL_TEST_DISTRO")
if name == "" || runtime.GOOS != "windows" {
t.Skip("MAGPIE_WSL_TEST_DISTRO not set")
}
t.Setenv("PATH", filepath.Join(os.Getenv("SystemRoot"), "System32")+string(os.PathListSeparator)+os.Getenv("PATH"))
base := "/tmp/magpie-wsl-links-" + strconv.FormatInt(time.Now().UnixNano(), 36)
sh := func(script string, args ...string) string {
t.Helper()
out, err := realWSLRun(30*time.Second, append([]string{"-d", name, "--exec", "sh", "-c", script, "sh"}, args...)...)
if err != nil {
t.Fatalf("%s: %v %s", script, err, out)
}
return strings.TrimSpace(string(out))
}
sh(`mkdir -p -- "$1/store/pdf" && printf 'Read PDFs' > "$1/store/pdf/SKILL.md"`, base)
t.Cleanup(func() { sh(`rm -rf -- "$1"`, base) })
wsl.Lock()
seen, running, runAt, runOK := wsl.seen, wsl.running, wsl.runAt, wsl.runOK
wsl.seen = map[string]*distro{name: {Name: name, Home: base, Root: wslRoot(name)}}
wsl.running, wsl.runAt, wsl.runOK = map[string]bool{name: true}, time.Now(), true
wsl.Unlock()
t.Cleanup(func() {
wsl.Lock()
wsl.seen, wsl.running, wsl.runAt, wsl.runOK = seen, running, runAt, runOK
wsl.Unlock()
})
d := wsl.seen[name]
a := &Agent{ID: "codex@wsl:" + name, WSL: name}
store, link := d.where(base+"/store/pdf"), d.where(base+"/agent/skills/pdf")
if b, err := os.ReadFile(filepath.Join(store, "SKILL.md")); err != nil || string(b) != "Read PDFs" {
t.Fatalf("the distro's folder through %s: %q %v", d.Root, b, err)
}
for range 2 {
if err := WSLSymlink(a, store, link); err != nil {
t.Fatal(err)
}
}
if got := sh(`readlink -- "$1"`, base+"/agent/skills/pdf"); got != base+"/store/pdf" {
t.Fatalf("the distro's link leads to %q", got)
}
if got := sh(`cat -- "$1/SKILL.md"`, base+"/agent/skills/pdf"); got != "Read PDFs" {
t.Fatalf("the distro reads through the link: %q", got)
}
// what Windows makes of it, as the library meets it
fi, err := os.Lstat(link)
t.Logf("Windows' Lstat of the link: %v %v", fi, err)
if err != nil {
t.Fatalf("Windows doesn't see the link: %v", err)
}
if _, err := os.Readlink(link); err == nil {
t.Log("Windows reads the link itself")
}
if got, ok := WSLReadlink(link); !ok || !strings.EqualFold(filepath.Clean(got), filepath.Clean(store)) {
t.Fatalf("read back %q %v, want %q", got, ok, store)
}
if err := WSLSymlink(a, store, filepath.Dir(store)); err == nil || !strings.Contains(err.Error(), "is not a link") {
t.Errorf("a link made over a folder: %v", err)
}
if err := WSLRemoveLink(store); err == nil {
t.Error("a folder removed as a link")
}
if err := WSLRemoveLink(link); err != nil {
t.Fatal(err)
}
if got := sh(`[ -e "$1" ] || [ -L "$1" ] && echo there || echo gone`, base+"/agent/skills/pdf"); got != "gone" {
t.Errorf("the link: %s", got)
}
if got := sh(`cat -- "$1/SKILL.md"`, base+"/store/pdf"); got != "Read PDFs" {
t.Errorf("what it led to: %q", got)
}
}
+3
View File
@@ -80,6 +80,9 @@ type Library struct {
// blockedSkills are copies takeEdits couldn't scan, kept in place for
// this sync rather than replaced or removed. Keys are physical paths.
blockedSkills map[string]bool
// stored are the skills this sync made sure of in a WSL distro's
// store, by path (storeSkill), with what went wrong
stored map[string]error
}
// Instructions are one shared text, and for each agent whether it gets it
+11
View File
@@ -18,6 +18,8 @@ import (
"os"
"sync"
"sync/atomic"
"github.com/yetone/magpie/internal/agent"
)
// part is what one entry on the way to a path is: there or not, and
@@ -67,6 +69,9 @@ func lookPart(p string) part {
return e
}
// wslReadlink is agent.WSLReadlink; a var for tests.
var wslReadlink = agent.WSLReadlink
// lookAt is statPart; a test counts the looks
var lookAt = statPart
@@ -83,6 +88,12 @@ func statPart(p string) (e part, sure bool) {
}
to, err := os.Readlink(p)
if err != nil || to == "" {
// a link a WSL distro has, which Windows can't read through
// \\wsl.localhost: the distro says where it leads
// (agent.WSLReadlink)
if w, ok := wslReadlink(p); ok {
return part{there: true, link: true, to: w}, true
}
// a link that can't be read is taken as an entry of its own, as
// before; but it isn't kept
return part{there: true}, false
+5
View File
@@ -48,6 +48,11 @@ func (l *Library) takeEdits(targets []*Target, res *Result) {
continue
}
p := filepath.Join(t.Skills, s.Name)
// an agent in WSL linked to magpie's copy there: an edit made
// through the link is in that copy (wsl_skills.go)
if t.Store != "" && ours(p, s.Name) && linked(p) {
p = filepath.Join(t.Store, s.Name)
}
if !ours(p, s.Name) || linked(p) || seen[realDir(p)] {
continue
}
+38 -8
View File
@@ -22,6 +22,7 @@ import (
"time"
"unicode"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/source"
"gopkg.in/yaml.v3"
)
@@ -203,13 +204,14 @@ const marker = ".magpie-library"
// ours reports whether the entry at p is magpie's link (or copy) of the
// library's skill by that name.
func ours(p, name string) bool {
fi, err := os.Lstat(p)
if err != nil {
return false
// a link in a WSL distro is read there (linkTarget), Windows can't;
// one to magpie's copy in the distro is magpie's (wsl_skills.go)
if to, ok := linkTarget(p); ok {
return filepath.Clean(to) == filepath.Clean(skillDir(name)) || inWSLStore(to, name)
}
if linkEntry(fi) {
to, err := os.Readlink(p)
return err == nil && filepath.Clean(to) == filepath.Clean(skillDir(name))
fi, err := os.Lstat(p)
if err != nil || linkEntry(fi) {
return false
}
if fi.IsDir() {
_, err := os.Stat(filepath.Join(p, marker))
@@ -404,7 +406,15 @@ func unlink(p string) error {
return nil
}
if linkEntry(fi) {
return os.Remove(p)
err := os.Remove(p)
// a link a WSL distro has can't be removed from Windows: the
// distro removes it
if err != nil {
if _, ok := wslReadlink(p); ok {
return agent.WSLRemoveLink(p)
}
}
return err
}
return os.RemoveAll(p)
}
@@ -628,6 +638,20 @@ func (l *Library) syncSkills(t *Target, res *Result, all []*Target) {
}
continue
}
// in a WSL distro, magpie's one copy there is what the agent's
// link leads to (wsl_skills.go): made, or made again, first
if t.Store != "" && !cp {
if made, err := l.storeSkill(t, s.Name); err != nil {
res.fail(id, "skill:"+s.Name, err)
if ours(p, s.Name) && !slices.Contains(mine, s.Name) {
mine = append(mine, s.Name)
}
continue
} else if made && ours(p, s.Name) && linked(p) {
// what its link leads to is new
res.changed(id)
}
}
// kept in ~/.agents/skills under another name (sharedEntry): the
// agent whose folder that is has it there already, and a link by
// this name too would be the skill twice for every reader of it
@@ -660,7 +684,11 @@ func (l *Library) syncSkills(t *Target, res *Result, all []*Target) {
// (#896), unless no link can be made here (Windows without the
// right to), where the copy stays
if ours(p, s.Name) && !cp && !linked(p) {
if ok, err := relink(id, p, s.Name); err != nil {
rl := relink
if t.Store != "" {
rl = l.wslRelink(t)
}
if ok, err := rl(id, p, s.Name); err != nil {
res.fail(id, "skill:"+s.Name, err)
continue
} else if ok {
@@ -707,6 +735,8 @@ func (l *Library) syncSkills(t *Target, res *Result, all []*Target) {
put := link
if cp {
put = copyIn
} else if t.Store != "" {
put = func(p, name string) error { return agent.WSLSymlink(t.Agent, filepath.Join(t.Store, name), p) }
}
if err := put(p, s.Name); err != nil {
res.fail(id, "skill:"+s.Name, err)
+2 -1
View File
@@ -155,7 +155,7 @@ func TestSkillCopySwapSurvivesAnUndeletableFile(t *testing.T) {
}
}
// An agent in WSL (a t.Copy target) gets its copy made again through the
// An agent in WSL set to take copies gets its copy made again through the
// same swap: with a file in its old copy that can't be removed, it still
// gets the whole new copy, magpie still knows it for its own, and the
// leftover is cleared by a later sync.
@@ -170,6 +170,7 @@ func TestWSLSkillCopySwapSurvivesAnUndeletableFile(t *testing.T) {
src := filepath.Join(home(), "src", "skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
write(t, filepath.Join(src, "pdf/sub/forms.md"), "forms one")
ok(t)(SetSkillHow(wslCodex, HowCopy))
ok(t)(InstallSkills(src, []string{"pdf"}, []string{wslCodex}))
p := filepath.Join(h, ".codex/skills/pdf")
if linked(p) || !ours(p, "pdf") || read(t, filepath.Join(p, "sub/forms.md")) != "forms one" {
+2
View File
@@ -133,6 +133,8 @@ func syncSteps(all, gone []*Target, b *backups, res *Result) []func(*Library) {
}
}
}
// magpie's copies in a WSL distro no agent there is linked to any more
steps = append(steps, func(l *Library) { l.sweepWSLStores(append(slices.Clone(all), gone...), res) })
return append(steps, func(l *Library) { l.syncProjects(res) })
}
+14 -5
View File
@@ -38,9 +38,15 @@ type Target struct {
// 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.
// the library's changes, rather than a link: one in a WSL distro whose
// skills folder is on a Windows drive (wslStored).
Copy bool
// Store, for an agent in a WSL distro, is magpie's folder there
// (wslStoreRel) holding one copy of each skill given to the distro's
// agents, as magpie opens it: each agent gets a link the distro makes
// to that copy, as it can't follow one to a Windows folder
// (wsl_skills.go).
Store string
// Desktop are Claude Desktop's skills-plugin folders, one for each
// account (desktop_skills.go): each gets a copy of the skills and their
// entries in its manifest.json. Skills is the first one's skills.
@@ -635,13 +641,14 @@ func pluginTarget(a *agent.Agent) *Target {
// distro is stopped, which opening them would start (asked again here: the
// agent may have been made before the user stopped it). 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.
// Windows, and each skill is a link the distro makes to magpie's one copy
// of it there (wsl_skills.go).
func wslTargetOf(a *agent.Agent) *Target {
h := a.Home
if h == "" || !agent.WSLRunning(a.WSL) {
return nil
}
t := &Target{Agent: a, Copy: true}
t := &Target{Agent: a}
// a folder there that is a link is opened where it leads (#4502)
j := func(elem ...string) string { return wslOpen(t, filepath.Join(elem...)) }
id, _, _ := strings.Cut(a.ID, "@")
@@ -721,6 +728,7 @@ func wslTargetOf(a *agent.Agent) *Target {
return wslOwnFolder(a)
}
wslOpened(t)
wslStored(t, h)
if t.MCP != nil {
// a Windows program isn't given to it (mcpFile.side)
t.MCP.WSL, t.MCP.Distro, t.MCP.Home = true, a.WSL, linuxHome(h)
@@ -789,8 +797,9 @@ func wslOwnFolder(a *agent.Agent) *Target {
if t == nil {
return nil
}
t.Agent, t.Copy = a, true
t.Agent, t.Copy = a, false
wslOpened(t)
wslStored(t, a.Home)
if t.MCP != nil {
t.MCP.WSL, t.MCP.Distro, t.MCP.Home = true, a.WSL, linuxHome(a.Home)
}
+2 -2
View File
@@ -115,9 +115,9 @@ func (l *Library) asleepView(a *agent.Agent) (AgentView, bool) {
av.MCP, av.Skills, av.SkillsAlso, av.MCPVia = sh.MCP, sh.Skills, sh.SkillsAlso, sh.MCPVia
av.NoSSE, av.NoRemote, av.NoEnvRefs, av.ToolsOff, av.Startup = sh.NoSSE, sh.NoRemote, sh.NoEnvRefs, sh.ToolsOff, sh.Startup
if sh.Skills != "" {
// a WSL agent takes copies only (Target.Copy)
// as a running one's: links to magpie's copy there, or copies
_, av.HowOwn = l.SkillHow[a.ID]
av.How, av.MustCopy = HowCopy, true
av.How = l.howOf(a.ID)
}
return av, true
}
+1 -1
View File
@@ -127,7 +127,7 @@ func TestWSLMCPSiblings(t *testing.T) {
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 {
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
+3 -3
View File
@@ -39,7 +39,7 @@ func TestWSLOwnFolderTargets(t *testing.T) {
t.Errorf("%s isn't a target: %v", id, ids(Targets()))
continue
}
if tg.MCP == nil || tg.MCP.Path != filepath.Join(h, w[0]) || !tg.MCP.WSL || tg.MCP.Distro != "Ubuntu" || tg.Skills != filepath.Join(h, w[1]) || !tg.Copy || tg.Agent.ID != id {
if tg.MCP == nil || tg.MCP.Path != filepath.Join(h, w[0]) || !tg.MCP.WSL || tg.MCP.Distro != "Ubuntu" || tg.Skills != filepath.Join(h, w[1]) || tg.Copy || tg.Store != filepath.Join(h, ".local/share/magpie/skills") || tg.Agent.ID != id {
t.Errorf("%s: %+v (mcp %+v)", id, tg, tg.MCP)
}
}
@@ -52,8 +52,8 @@ func TestWSLOwnFolderTargets(t *testing.T) {
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
ok(t)(InstallSkills(src, []string{"pdf"}, []string{hermes}))
p := filepath.Join(h, ".hermes", "skills", "pdf")
if fi, err := os.Lstat(p); err != nil || !fi.IsDir() || !ours(p, "pdf") {
t.Fatalf("hermes@wsl has no copy of the skill: %v", err)
if to, err := os.Readlink(p); err != nil || to != filepath.Join(h, ".local/share/magpie/skills/pdf") || !ours(p, "pdf") {
t.Fatalf("hermes@wsl has no link to the distro's copy of the skill: %q %v", to, err)
}
if _, err := os.Lstat(filepath.Join(home(), ".hermes", "skills", "pdf")); !os.IsNotExist(err) {
t.Errorf("this machine's Hermes got it: %v", err)
+193
View File
@@ -0,0 +1,193 @@
package library
// Skills in a WSL distro (cutemmuu on X). An agent there can't follow a
// link to the library's folder on Windows, and Windows can't make a link
// there that the distro follows (one os.Symlink makes through
// \\wsl.localhost is an empty folder in the distro), so each agent in a
// distro used to get a copy of its own of every skill. Now the distro
// holds one copy of each skill its agents are given, magpie's, in
// ~/.local/share/magpie/skills (Target.Store); each agent there gets a
// link to it, made in the distro by wsl.exe (agent.WSLSymlink), as an
// agent on Windows gets one to the library's folder. An edit made in an
// agent through its link is in that copy, which takeEdits looks at as it
// does at any copy of magpie's. An agent set to take copies (SkillHow)
// still gets a copy of its own.
//
// The copy isn't put in the distro's ~/.agents/skills: every agent there
// that reads that folder would find each skill in it, ticked for it or
// not, and the ticks are per agent.
import (
"errors"
"os"
"path/filepath"
"strings"
"github.com/yetone/magpie/internal/agent"
)
// wslStoreRel is magpie's folder for skills in a distro, below its home.
var wslStoreRel = []string{".local", "share", "magpie", "skills"}
// wslStored gives t, an agent's target in a WSL distro whose home magpie
// opens at home, the distro's store of skills, unless it has no skills.
// An agent whose skills folder is on a Windows drive (its folder a link
// to /mnt/c/…, #4502), or a store that is, keeps copies of its own: a
// link the distro makes there is one Windows can neither read nor follow.
func wslStored(t *Target, home string) {
if t.Skills == "" || home == "" {
return
}
store := wslOpen(t, filepath.Join(append([]string{home}, wslStoreRel...)...))
if !agent.WSLInDistro(t.Agent, t.Skills) || !agent.WSLInDistro(t.Agent, store) {
t.Copy = true
return
}
t.Store = store
}
// inWSLStore is whether to, a path as magpie opens it, is a skill by that
// name in a distro's store: what magpie's link in an agent there leads to.
func inWSLStore(to, name string) bool {
parts := strings.FieldsFunc(filepath.ToSlash(to), func(r rune) bool { return r == '/' || r == '\\' })
n := len(parts)
if n < len(wslStoreRel)+1 || parts[n-1] != name {
return false
}
for i, p := range wslStoreRel {
if !strings.EqualFold(parts[n-1-len(wslStoreRel)+i], p) {
return false
}
}
return true
}
// storeSkill makes the distro's copy of the skill (t.Store), or makes it
// again when the library's has changed since; looked at once a sync step
// for each store, and changed says this call made it. A folder there
// that isn't magpie's copy is never touched.
func (l *Library) storeSkill(t *Target, name string) (changed bool, err error) {
p := filepath.Join(t.Store, name)
key := strings.ToLower(p)
if err, ok := l.stored[key]; ok {
return false, err
}
changed, err = func() (bool, error) {
fi, err := os.Lstat(p)
if err != nil {
return true, copyIn(p, name)
}
if !fi.IsDir() || !ours(p, name) {
return false, errors.New(p + " is in the way of magpie's copy of " + name + " in WSL")
}
dropOld(p, name)
h := l.libHash(name)
if h == "" {
return false, nil
}
current, err := copyIsFresh(p, h, l.untouched[p])
if err != nil || current {
return false, err
}
return true, copyIn(p, name)
}()
if l.stored == nil {
l.stored = map[string]error{}
}
l.stored[key] = err
return changed && err == nil, err
}
// wslRelink is relink for t, an agent in a WSL distro: its copy of the
// skill made a link the distro makes to the distro's copy (storeSkill).
// The copy is moved aside first, since the distro won't put a link in a
// folder's place, and put back when no link can be made. A copy that
// differs from the library's skill (edited, and its edit lost) is kept
// aside with the backups rather than lost.
func (l *Library) wslRelink(t *Target) func(id, p, name string) (bool, error) {
return func(id, p, name string) (bool, error) {
current, err := copyIsFresh(p, l.libHash(name), l.untouched[p])
if err != nil {
return false, err
}
old := oldPath(p, name)
if err := os.Rename(p, old); err != nil {
return false, err
}
if err := agent.WSLSymlink(t.Agent, filepath.Join(t.Store, name), p); err != nil {
if back := os.Rename(old, p); back != nil {
return false, errors.Join(err, back)
}
return false, err
}
if current {
// best effort: what's left is tried again on the next sync
os.RemoveAll(old)
} else if _, err := setAside(id, old); err != nil {
return true, err
}
return true, nil
}
}
// sweepWSLStores takes out of each distro's store the copies no agent
// there has a link to, or is given, any more. A copy edited since magpie
// made it is kept aside with the backups. A store with an agent that
// couldn't be reached is left as it is: that agent may still link to any.
func (l *Library) sweepWSLStores(targets []*Target, res *Result) {
type store struct {
path string
owner string
keep map[string]bool
frozen bool
}
stores := map[string]*store{}
for _, t := range targets {
if t.Store == "" {
continue
}
key := strings.ToLower(t.Store)
s := stores[key]
if s == nil {
s = &store{path: t.Store, owner: t.Agent.ID, keep: map[string]bool{}}
stores[key] = s
}
if t.Unreached != nil {
s.frozen = true
}
for _, sk := range l.Skills {
if sk.gives(t.Agent.ID) {
s.keep[sk.Name] = true
}
}
for _, name := range l.applied(t.Agent.ID).Skills {
s.keep[name] = true
}
}
for _, s := range stores {
if s.frozen {
continue
}
es, err := os.ReadDir(s.path)
if err != nil {
continue
}
for _, e := range es {
name := e.Name()
p := filepath.Join(s.path, name)
if strings.HasPrefix(name, ".") || s.keep[name] || !ours(p, name) || linked(p) {
continue
}
dropOld(p, name)
h, _ := markedHash(p)
if h != "" && hashDir(p) == h {
err = os.RemoveAll(p)
} else {
_, err = setAside(s.owner, p)
}
if err != nil {
res.fail(s.owner, "skill:"+name, err)
}
}
}
}
+229
View File
@@ -0,0 +1,229 @@
package library
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/agent"
)
// The copies each agent in WSL got of its own before (cutemmuu on X) are
// links to the distro's one copy after the next sync: one edited in an
// agent, its edit the newest, goes to the library and every agent first;
// one whose edit lost is kept with the backups; a skill folder of the
// user's own beside them, and one they put where magpie's copy goes, are
// never touched.
func TestWSLOldCopiesBecomeLinks(t *testing.T) {
h := wslSandbox(t)
src := filepath.Join(home(), "src", "skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
skill(t, filepath.Join(src, "csv"), "csv", "Read CSVs")
// what magpie gave them before: a marked copy in each
for _, id := range []string{wslCodex, wslClaude, wslPi} {
ok(t)(SetSkillHow(id, HowCopy))
}
ok(t)(InstallSkills(src, []string{"pdf", "csv"}, []string{"claude", wslCodex, wslClaude, wslPi}))
cx, cl, pi := filepath.Join(h, ".codex/skills"), filepath.Join(h, ".claude/skills"), filepath.Join(h, ".pi/agent/skills")
for _, d := range []string{cx, cl, pi} {
if isLink(t, filepath.Join(d, "pdf")) || !ours(filepath.Join(d, "pdf"), "pdf") {
t.Fatalf("%s: no copy of the old kind to start from", d)
}
}
// Codex's pdf edited in the distro; Claude Code's csv edited, then the
// library's after it, so Claude Code's loses
time.Sleep(20 * time.Millisecond)
write(t, filepath.Join(cx, "pdf/SKILL.md"), "---\nname: pdf\ndescription: Edited in WSL Codex\n---\n")
write(t, filepath.Join(cl, "csv/SKILL.md"), "---\nname: csv\ndescription: Edited in WSL Claude\n---\n")
time.Sleep(20 * time.Millisecond)
write(t, filepath.Join(src, "csv/SKILL.md"), "---\nname: csv\ndescription: Edited by hand\n---\n")
// the user's own skill beside them, and a folder of theirs where
// magpie's copy of pdf would go
write(t, filepath.Join(cl, "mine/SKILL.md"), "---\nname: mine\ndescription: Mine\n---\n")
store := filepath.Join(h, ".local/share/magpie/skills")
write(t, filepath.Join(store, "csv/SKILL.md"), "the user's own csv")
// the update: no way of their own set any more, as a user who never
// picked one has
var problems []Problem
for _, id := range []string{wslCodex, wslClaude, wslPi} {
res, err := SetSkillHow(id, "")
if err != nil {
t.Fatal(err)
}
problems = append(problems, res.Problems...)
}
// the user's csv in magpie's place is said, for each agent given csv
for _, p := range problems {
if p.What != "skill:csv" || !strings.Contains(p.Error, "in the way of magpie's copy of csv") {
t.Errorf("problem: %+v", p)
}
}
t.Logf("problems: %+v", problems)
for _, d := range []string{cx, cl, pi} {
p := filepath.Join(d, "pdf")
if to, err := os.Readlink(p); err != nil || to != filepath.Join(store, "pdf") {
t.Errorf("%s: not a link to the distro's copy: %q %v", p, to, err)
}
if s := read(t, filepath.Join(p, "SKILL.md")); !strings.Contains(s, "Edited in WSL Codex") {
t.Errorf("%s lost Codex's edit:\n%s", p, s)
}
}
if s := read(t, filepath.Join(src, "pdf/SKILL.md")); !strings.Contains(s, "Edited in WSL Codex") {
t.Errorf("the library didn't take Codex's edit:\n%s", s)
}
if !backedUp(t, "Edited in WSL Claude") {
t.Error("Claude Code's edit, which lost, wasn't kept")
}
if s := read(t, filepath.Join(h, ".claude/skills/mine/SKILL.md")); !strings.Contains(s, "Mine") {
t.Errorf("the user's own skill: %q", s)
}
// csv's place in the store is the user's: nothing is put there, and
// the agents keep what they had of it
if s := read(t, filepath.Join(store, "csv/SKILL.md")); s != "the user's own csv" {
t.Errorf("the user's folder in magpie's place was written over: %q", s)
}
for _, d := range []string{cx, cl, pi} {
if p := filepath.Join(d, "csv"); isLink(t, p) || !ours(p, "csv") {
t.Errorf("%s: what it had of csv is gone", p)
}
}
if s := read(t, filepath.Join(home(), ".claude/skills/pdf/SKILL.md")); !strings.Contains(s, "Edited in WSL Codex") {
t.Errorf("this machine's Claude Code:\n%s", s)
}
}
// "User edits it, then sync": an edit made in an agent in WSL, through its
// link, is in the distro's copy, and the sync takes it into the library
// rather than making the copy again over it; every agent has it after.
func TestWSLEditThroughLinkReachesTheLibrary(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}))
time.Sleep(20 * time.Millisecond)
write(t, filepath.Join(h, ".codex/skills/pdf/SKILL.md"), "---\nname: pdf\ndescription: Edited through the link\n---\n")
for range 2 { // a sync, and the one of the next start
ok(t)(Sync())
for _, p := range []string{filepath.Join(src, "pdf"), filepath.Join(home(), ".claude/skills/pdf"), filepath.Join(h, ".claude/skills/pdf")} {
if s := read(t, filepath.Join(p, "SKILL.md")); !strings.Contains(s, "Edited through the link") {
t.Fatalf("%s after a sync:\n%s", p, s)
}
}
}
if !isLink(t, filepath.Join(h, ".codex/skills/pdf")) {
t.Error("Codex's link was made a copy")
}
}
// A skill an agent in WSL had of its own, the very same files, gives way
// to the link, kept aside; and a skill no agent there is given any more
// that was edited in the distro's copy is kept, not deleted.
func TestWSLStoreSweepKeepsEdits(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{wslCodex}))
store := filepath.Join(h, ".local/share/magpie/skills/pdf")
// edited in the distro's copy and unticked in one change: the edit is
// taken first, then the copy goes
time.Sleep(20 * time.Millisecond)
write(t, filepath.Join(store, "SKILL.md"), "---\nname: pdf\ndescription: Edited in the store\n---\n")
ok(t)(SkillAgents("pdf", []string{}))
if _, err := os.Lstat(store); !os.IsNotExist(err) {
t.Errorf("the distro's copy is still there: %v", err)
}
if s := read(t, filepath.Join(src, "pdf/SKILL.md")); !strings.Contains(s, "Edited in the store") && !backedUp(t, "Edited in the store") {
t.Error("the edit in the distro's copy was lost")
}
}
// On a real WSL (MAGPIE_WSL_TEST_DISTRO, on Windows): the agents of a
// distro whose home is a folder of the test's own in /tmp there get links
// the distro makes and follows, which magpie reads back through wsl.exe
// (Windows can't), so a sync with nothing new changes nothing; an edit
// made in the distro's copy reaches the library; a way of copies takes
// the link away for a copy and back; unticked, the links and the copy go.
func TestWSLSkillsLinkedLive(t *testing.T) {
name := os.Getenv("MAGPIE_WSL_TEST_DISTRO")
if name == "" || runtime.GOOS != "windows" {
t.Skip("MAGPIE_WSL_TEST_DISTRO not set")
}
t.Setenv("PATH", filepath.Join(os.Getenv("SystemRoot"), "System32")+string(os.PathListSeparator)+os.Getenv("PATH"))
sh := func(script string, args ...string) string {
t.Helper()
cmd := exec.Command("wsl.exe", append([]string{"-d", name, "--exec", "sh", "-c", script, "sh"}, args...)...)
cmd.Env = append(os.Environ(), "WSL_UTF8=1")
out, err := cmd.Output()
if err != nil {
t.Fatalf("%s: %v %s", script, err, out)
}
return strings.TrimSpace(string(out))
}
base := "/tmp/magpie-wsl-skills-" + strconv.FormatInt(time.Now().UnixNano(), 36)
sh(`mkdir -p -- "$1/.codex" "$1/.claude" && printf 'model = "m"\n' > "$1/.codex/config.toml" && printf '{}\n' > "$1/.claude/settings.json"`, base)
t.Cleanup(func() { sh(`rm -rf -- "$1"`, base) })
sandbox(t)
// the real wsl.exe, after the sandbox's PATH
t.Setenv("PATH", filepath.Join(os.Getenv("SystemRoot"), "System32")+string(os.PathListSeparator)+os.Getenv("PATH"))
root := `\\wsl.localhost\` + name
if _, err := os.Stat(root + `\`); err != nil {
root = `\\wsl$\` + name
}
t.Cleanup(agent.FakeWSL(map[string]string{name: "home:" + base + "\ndir:.codex\ndir:.claude\n"}, map[string]string{name: root}))
t.Cleanup(agent.FakeWSLRealLinks())
codex, claude := "codex@wsl:"+name, "claude@wsl:"+name
h := filepath.Join(root, filepath.FromSlash(base))
src := filepath.Join(home(), "src", "skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
ok(t)(InstallSkills(src, []string{"pdf"}, []string{"claude", codex, claude}))
for _, d := range []string{"/.codex/skills/pdf", "/.claude/skills/pdf"} {
if got := sh(`readlink -- "$1"`, base+d); got != base+"/.local/share/magpie/skills/pdf" {
t.Fatalf("%s leads to %q", d, got)
}
if got := sh(`cat -- "$1/SKILL.md"`, base+d); !strings.Contains(got, "Read PDFs") {
t.Fatalf("the distro reads through %s: %q", d, got)
}
if p := filepath.Join(h, filepath.FromSlash(d)); !ours(p, "pdf") || !linked(p) {
t.Fatalf("%s isn't magpie's link, as Windows reads it", p)
}
}
if res := ok(t)(Sync()); len(res.Changed) != 0 {
t.Errorf("a sync with nothing new wrote %v", res.Changed)
}
if v := viewOf(t, codex); v == nil || v.How != HowLink || v.MustCopy {
t.Errorf("Codex's row: %+v", v)
}
// "user edits it, then sync", in the distro, through the link
time.Sleep(1100 * time.Millisecond)
sh(`printf -- '---\nname: pdf\ndescription: Edited in WSL\n---\n' > "$1/SKILL.md"`, base+"/.codex/skills/pdf")
ok(t)(Sync())
for _, p := range []string{filepath.Join(src, "pdf"), filepath.Join(home(), ".claude/skills/pdf")} {
if s := read(t, filepath.Join(p, "SKILL.md")); !strings.Contains(s, "Edited in WSL") {
t.Errorf("%s after the edit in WSL:\n%s", p, s)
}
}
// copies for Codex: its link goes for a copy, and back
ok(t)(SetSkillHow(codex, HowCopy))
if got := sh(`[ -L "$1" ] && echo link || { [ -f "$1/.magpie-library" ] && echo copy; }`, base+"/.codex/skills/pdf"); got != "copy" {
t.Fatalf("Codex given copies has %q", got)
}
ok(t)(SetSkillHow(codex, ""))
if got := sh(`readlink -- "$1"`, base+"/.codex/skills/pdf"); got != base+"/.local/share/magpie/skills/pdf" {
t.Fatalf("Codex's copy made a link again leads to %q", got)
}
ok(t)(SkillAgents("pdf", []string{"claude"}))
if got := sh(`for p in "$@"; do { [ -e "$p" ] || [ -L "$p" ]; } && echo "$p"; done; true`,
base+"/.codex/skills/pdf", base+"/.claude/skills/pdf", base+"/.local/share/magpie/skills/pdf"); got != "" {
t.Errorf("still there once unticked: %s", got)
}
}
+23 -10
View File
@@ -61,7 +61,7 @@ func TestWSLTargets(t *testing.T) {
}
return filepath.Join(h, rel)
}
if tg.Instructions != at(w[0]) || mcp != at(w[1]) || tg.Skills != at(w[2]) || !tg.Copy {
if tg.Instructions != at(w[0]) || mcp != at(w[1]) || tg.Skills != at(w[2]) || tg.Copy || tg.Store != at(".local/share/magpie/skills") {
t.Errorf("%s: %+v (mcp %s)", id, tg, mcp)
}
}
@@ -76,19 +76,25 @@ func TestWSLTargets(t *testing.T) {
}
}
// 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) {
// A skill reaches the agents in WSL as one copy in the distro, magpie's
// (~/.local/share/magpie/skills) — a link to a Windows folder is nothing
// they can follow — and each agent there a link the distro makes to it
// (cutemmuu on X). The copy is made again when the library's changes,
// and taken away once no agent there has it.
func TestWSLSkillsLinked(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}))
store := filepath.Join(h, ".local/share/magpie/skills/pdf")
if fi, err := os.Lstat(store); err != nil || !fi.IsDir() || !ours(store, "pdf") || !strings.Contains(read(t, filepath.Join(store, "SKILL.md")), "Read PDFs") {
t.Fatalf("no copy of magpie's in the distro: %v %v", fi, err)
}
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)
to, err := os.Readlink(p)
if err != nil || to != store {
t.Fatalf("%s: not a link to the distro's copy: %q %v", d, to, 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)
@@ -123,9 +129,16 @@ func TestWSLSkillsCopied(t *testing.T) {
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") {
if !ours(filepath.Join(h, ".claude/skills/pdf"), "pdf") || !ours(store, "pdf") {
t.Error("claude@wsl lost it")
}
// no agent in the distro given it: its copy there goes too
ok(t)(SkillAgents("pdf", []string{"claude"}))
for _, p := range []string{filepath.Join(h, ".claude/skills/pdf"), store} {
if _, err := os.Lstat(p); !os.IsNotExist(err) {
t.Errorf("%s is still there: %v", p, err)
}
}
}
// MCP servers are written into the distro's files in each agent's own
@@ -341,7 +354,7 @@ func TestWSLStoppedListedAndQueued(t *testing.T) {
if cx == nil {
t.Fatal("a stopped distro's Codex isn't listed")
}
if cx.Asleep != distro || cx.Unseen || cx.MCP != filepath.Join(h, ".codex", "config.toml") || cx.Skills != filepath.Join(h, ".codex", "skills") || !cx.NoSSE || !cx.MustCopy || cx.How != HowCopy {
if cx.Asleep != distro || cx.Unseen || cx.MCP != filepath.Join(h, ".codex", "config.toml") || cx.Skills != filepath.Join(h, ".codex", "skills") || !cx.NoSSE || cx.MustCopy || cx.How != HowLink {
t.Errorf("Codex while stopped: %+v", *cx)
}
if cl := viewOf(t, wslClaude); cl == nil || cl.Asleep != distro || cl.MCP != filepath.Join(h, ".claude.json") || cl.NoSSE {