Files
magpie/internal/agent/wsl_links.go
T
yetone 22f5cf69e5 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.
2026-11-03 20:03:00 +08:00

210 lines
6.2 KiB
Go

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
}