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.
194 lines
6.0 KiB
Go
194 lines
6.0 KiB
Go
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)
|
|
}
|
|
}
|
|
}
|
|
}
|