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.
103 lines
3.0 KiB
Go
103 lines
3.0 KiB
Go
package library
|
|
|
|
// Reading the Library page follows the links on the way to a few hundred
|
|
// paths: each agent's instructions, MCP file and skills folder, each skill
|
|
// and where it came from, CC Switch's folder, the shared skills folder,
|
|
// most of them many times over (once for each skill, or each pair of
|
|
// agents). resolve looks at a path a part at a time, so one read of the
|
|
// page looked at the same few hundred folders thousands of times, two or
|
|
// three system calls each; on Windows that alone was over a second on
|
|
// every visit (oxyuan, #1771). While a read holds the library's lock,
|
|
// what each part was found to be is kept for the rest of that read, so
|
|
// each folder is looked at once. Nothing is kept past the read: the next
|
|
// visit looks again, so a link made or changed on disk in between shows.
|
|
|
|
import (
|
|
"errors"
|
|
"io/fs"
|
|
"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
|
|
// where it points when it is a link (a symlink, or on Windows a junction).
|
|
type part struct {
|
|
there, link bool
|
|
to string
|
|
}
|
|
|
|
type partMemo struct {
|
|
sync.Mutex
|
|
m map[string]part
|
|
}
|
|
|
|
// reading is the memo of the read in progress, nil when there is none.
|
|
var reading atomic.Pointer[partMemo]
|
|
|
|
// readParts keeps what lookPart finds until the returned func is called.
|
|
// Only a read of the page that holds mu calls it: no change can make or
|
|
// take away a link while it does, since every change holds mu too. A
|
|
// caller without mu meanwhile (a probe of a folder to add) is given what
|
|
// the read found, at most as old as the read.
|
|
func readParts() (done func()) {
|
|
reading.Store(&partMemo{m: map[string]part{}})
|
|
return func() { reading.Store(nil) }
|
|
}
|
|
|
|
// lookPart is what the entry at p is, as found once in this read.
|
|
func lookPart(p string) part {
|
|
pm := reading.Load()
|
|
if pm == nil {
|
|
e, _ := lookAt(p)
|
|
return e
|
|
}
|
|
pm.Lock()
|
|
e, ok := pm.m[p]
|
|
pm.Unlock()
|
|
if ok {
|
|
return e
|
|
}
|
|
e, sure := lookAt(p)
|
|
if sure {
|
|
pm.Lock()
|
|
pm.m[p] = e
|
|
pm.Unlock()
|
|
}
|
|
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
|
|
|
|
// statPart looks at the entry at p: one Lstat, and a Readlink for a link.
|
|
// sure is false when it couldn't be looked at for another reason than
|
|
// its not being there: unknown, looked at again next time.
|
|
func statPart(p string) (e part, sure bool) {
|
|
fi, err := os.Lstat(p)
|
|
if err != nil {
|
|
return part{}, errors.Is(err, fs.ErrNotExist)
|
|
}
|
|
if !linkEntry(fi) {
|
|
return part{there: true}, true
|
|
}
|
|
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
|
|
}
|
|
return part{there: true, link: true, to: to}, true
|
|
}
|