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.
215 lines
7.6 KiB
Go
215 lines
7.6 KiB
Go
package library
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// Where magpie can't link a skill into an agent (Windows without the right
|
|
// to make links), it puts a copy there instead. A link shows the library's
|
|
// skill as it is now; the copy has to be made again when the library's
|
|
// changes, or the agent keeps the skill as it was when it was first given.
|
|
func TestSkillCopyFollowsTheLibrary(t *testing.T) {
|
|
h := sandbox(t)
|
|
version := "one"
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write(tarball(t, map[string]string{
|
|
"skills/pdf/SKILL.md": "---\nname: pdf\ndescription: PDFs " + version + "\n---\n",
|
|
"skills/pdf/forms.md": "forms " + version,
|
|
}))
|
|
}))
|
|
defer srv.Close()
|
|
old := tarballURL
|
|
tarballURL = func(repo, ref string) string { return srv.URL + "/" + repo + "/" + ref }
|
|
defer func() { tarballURL = old }()
|
|
|
|
ok(t)(InstallSkills("owner/repo", []string{"skills/pdf"}, []string{"claude"}))
|
|
p := filepath.Join(h, ".claude/skills/pdf")
|
|
// the copy link makes where it can't link, wherever this runs: the
|
|
// library giving copies (a copy left where links are given is made a
|
|
// link again, #896)
|
|
ok(t)(SetSkillHow("", HowCopy))
|
|
if linked(p) {
|
|
t.Fatal("claude's pdf is still a link")
|
|
}
|
|
if !ours(p, "pdf") {
|
|
t.Fatal("the copy isn't magpie's")
|
|
}
|
|
|
|
version = "two"
|
|
ok(t)(UpdateSkill("pdf", false))
|
|
if s := read(t, filepath.Join(p, "SKILL.md")); !strings.Contains(s, "PDFs two") {
|
|
t.Fatalf("claude's copy after the update:\n%s", s)
|
|
}
|
|
if s := read(t, filepath.Join(p, "forms.md")); s != "forms two" {
|
|
t.Fatalf("forms.md after the update: %q", s)
|
|
}
|
|
if !ours(p, "pdf") {
|
|
t.Fatal("the copy made again isn't magpie's")
|
|
}
|
|
|
|
// a skill changed in the library's folder by hand reaches it on a sync
|
|
write(t, filepath.Join(skillDir("pdf"), "forms.md"), "forms three")
|
|
ok(t)(Sync())
|
|
if s := read(t, filepath.Join(p, "forms.md")); s != "forms three" {
|
|
t.Fatalf("forms.md after a sync: %q", s)
|
|
}
|
|
if entries, _ := os.ReadDir(filepath.Dir(p)); len(entries) != 1 {
|
|
var names []string
|
|
for _, e := range entries {
|
|
names = append(names, e.Name())
|
|
}
|
|
t.Fatalf("claude's skills folder: %v", names)
|
|
}
|
|
}
|
|
|
|
// Remaking a copy must never strand it half-removed: when a file in the old
|
|
// copy can't be removed (held open on Windows; a read-only folder here), the
|
|
// agent still gets the whole new copy, magpie still knows it for its own, and
|
|
// what's left of the old one is removed on a later sync.
|
|
func TestSkillCopySwapSurvivesAnUndeletableFile(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("read-only folders don't stop removal on Windows")
|
|
}
|
|
if os.Geteuid() == 0 {
|
|
t.Skip("root can remove files from a read-only folder")
|
|
}
|
|
h := sandbox(t)
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write(tarball(t, map[string]string{
|
|
"skills/pdf/SKILL.md": "---\nname: pdf\ndescription: PDFs one\n---\n",
|
|
}))
|
|
}))
|
|
defer srv.Close()
|
|
old := tarballURL
|
|
tarballURL = func(repo, ref string) string { return srv.URL + "/" + repo + "/" + ref }
|
|
defer func() { tarballURL = old }()
|
|
|
|
ok(t)(InstallSkills("owner/repo", []string{"skills/pdf"}, []string{"claude"}))
|
|
p := filepath.Join(h, ".claude/skills/pdf")
|
|
// a skill with a folder in it, the folder holding the file that will stick
|
|
if err := os.MkdirAll(filepath.Join(skillDir("pdf"), "sub"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
write(t, filepath.Join(skillDir("pdf"), "sub/forms.md"), "forms one")
|
|
// a copy, as where magpie can't link (#896: a copy left where links are
|
|
// given is made a link again)
|
|
ok(t)(SetSkillHow("", HowCopy))
|
|
if linked(p) {
|
|
t.Fatal("claude's pdf is still a link")
|
|
}
|
|
// sub/forms.md in the agent's copy can't be removed
|
|
sub := filepath.Join(p, "sub")
|
|
if err := os.Chmod(sub, 0o555); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
stuck := []string{}
|
|
t.Cleanup(func() {
|
|
for _, d := range append(stuck, sub) {
|
|
os.Chmod(d, 0o755)
|
|
}
|
|
})
|
|
|
|
write(t, filepath.Join(skillDir("pdf"), "SKILL.md"), "---\nname: pdf\ndescription: PDFs two\n---\n")
|
|
write(t, filepath.Join(skillDir("pdf"), "sub/forms.md"), "forms two")
|
|
ok(t)(Sync())
|
|
if !ours(p, "pdf") {
|
|
t.Fatal("the copy isn't magpie's after a swap with an undeletable file")
|
|
}
|
|
if s := read(t, filepath.Join(p, "SKILL.md")); !strings.Contains(s, "PDFs two") {
|
|
t.Fatalf("claude's copy after the update:\n%s", s)
|
|
}
|
|
if s := read(t, filepath.Join(p, "sub/forms.md")); s != "forms two" {
|
|
t.Fatalf("sub/forms.md after the update: %q", s)
|
|
}
|
|
leftover := filepath.Join(filepath.Dir(p), ".pdf.magpie-old")
|
|
if _, err := os.Lstat(leftover); err != nil {
|
|
t.Fatalf("the old copy that couldn't be removed: %v", err)
|
|
}
|
|
stuck = append(stuck, filepath.Join(leftover, "sub"))
|
|
|
|
// a later sync still knows the copy and is not refused
|
|
ok(t)(Sync())
|
|
if !ours(p, "pdf") {
|
|
t.Fatal("the copy isn't magpie's on the next sync")
|
|
}
|
|
|
|
// once the file can be removed, the next sync clears what was left
|
|
os.Chmod(filepath.Join(leftover, "sub"), 0o755)
|
|
ok(t)(Sync())
|
|
if _, err := os.Lstat(leftover); !os.IsNotExist(err) {
|
|
t.Fatalf("the old copy is still there after a sync: %v", err)
|
|
}
|
|
if entries, _ := os.ReadDir(filepath.Dir(p)); len(entries) != 1 {
|
|
var names []string
|
|
for _, e := range entries {
|
|
names = append(names, e.Name())
|
|
}
|
|
t.Fatalf("claude's skills folder: %v", names)
|
|
}
|
|
}
|
|
|
|
// 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.
|
|
func TestWSLSkillCopySwapSurvivesAnUndeletableFile(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("read-only folders don't stop removal on Windows")
|
|
}
|
|
if os.Geteuid() == 0 {
|
|
t.Skip("root can remove files from a read-only folder")
|
|
}
|
|
h := wslSandbox(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" {
|
|
t.Fatal("codex@wsl didn't get magpie's copy")
|
|
}
|
|
sub := filepath.Join(p, "sub")
|
|
if err := os.Chmod(sub, 0o555); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
leftover := filepath.Join(filepath.Dir(p), ".pdf.magpie-old")
|
|
t.Cleanup(func() {
|
|
os.Chmod(sub, 0o755)
|
|
os.Chmod(filepath.Join(leftover, "sub"), 0o755)
|
|
})
|
|
|
|
write(t, filepath.Join(src, "pdf/SKILL.md"), "---\nname: pdf\ndescription: Changed\n---\n")
|
|
write(t, filepath.Join(src, "pdf/sub/forms.md"), "forms two")
|
|
res := ok(t)(Sync())
|
|
if !slices.Contains(res.Changed, wslCodex) {
|
|
t.Errorf("changed: %v", res.Changed)
|
|
}
|
|
if !ours(p, "pdf") {
|
|
t.Fatal("codex@wsl's copy isn't magpie's after a swap with an undeletable file")
|
|
}
|
|
if !strings.Contains(read(t, filepath.Join(p, "SKILL.md")), "Changed") || read(t, filepath.Join(p, "sub/forms.md")) != "forms two" {
|
|
t.Fatal("codex@wsl's copy wasn't made again whole")
|
|
}
|
|
if _, err := os.Lstat(leftover); err != nil {
|
|
t.Fatalf("the old copy that couldn't be removed: %v", err)
|
|
}
|
|
|
|
// once the file can be removed, the next sync clears what was left
|
|
os.Chmod(filepath.Join(leftover, "sub"), 0o755)
|
|
ok(t)(Sync())
|
|
if _, err := os.Lstat(leftover); !os.IsNotExist(err) {
|
|
t.Fatalf("the old copy is still there after a sync: %v", err)
|
|
}
|
|
if entries, _ := os.ReadDir(filepath.Dir(p)); len(entries) != 1 {
|
|
t.Fatalf("codex@wsl's skills folder has %d entries", len(entries))
|
|
}
|
|
}
|