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.
140 lines
5.0 KiB
Go
140 lines
5.0 KiB
Go
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)
|
|
}
|
|
}
|