Files
magpie/internal/agent/wsl_fake.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

194 lines
5.6 KiB
Go

package agent
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
// FakeWSL, for other packages' tests, stands in for wsl.exe on any
// system: each distro named in probes is installed and running, answers
// the probe with what probes has for it (wslProbeScript's output: "home:/
// home/me\ndir:.codex\n…") and is opened at roots' folder for it. What
// magpie remembers of WSL is forgotten, before and after; the returned
// func puts everything back.
func FakeWSL(probes, roots map[string]string) func() {
on, run, root, exec := wslOn, wslRun, wslRoot, wslExec
reset := func() {
wsl.Lock()
wsl.seen, wsl.at, wsl.runAt, wsl.names, wsl.running, wsl.probed, wsl.failed, wsl.dirty = nil, time.Time{}, time.Time{}, nil, nil, nil, nil, false
wsl.Unlock()
}
reset()
var names []string
for n := range probes {
names = append(names, n)
}
list := []byte(strings.Join(names, "\n"))
stopped := map[string]bool{}
fakeStop = func(name string, stop bool) {
wsl.Lock()
stopped[name] = stop
wsl.runAt = time.Time{}
wsl.Unlock()
}
fakeCalls.Lock()
fakeCalls.in = map[string]int{}
fakeCalls.Unlock()
wslOn = true
wslRun = func(_ time.Duration, args ...string) ([]byte, error) {
switch strings.Join(args, " ") {
case "-l -q":
return list, nil
case "-l --running -q":
var run []string
for _, n := range names {
if !stopped[n] {
run = append(run, n)
}
}
return []byte(strings.Join(run, "\n")), nil
}
if len(args) > 2 && args[0] == "-d" {
fakeCalls.Lock()
fakeCalls.in[args[1]]++
fakeCalls.Unlock()
if p, ok := probes[args[1]]; ok {
return []byte(p), nil
}
}
return nil, errors.New("unexpected wsl.exe " + strings.Join(args, " "))
}
wslRoot = func(name string) string { return roots[name] }
wslExec = func(name string, argv ...string) ([]byte, error) {
fakeCalls.Lock()
fakeCalls.in[name]++
fakeCalls.Unlock()
return fakeLinks(roots[name], argv)
}
links.Lock()
links.m = nil
links.Unlock()
return func() {
wslOn, wslRun, wslRoot, wslExec = on, run, root, exec
links.Lock()
links.m = nil
links.Unlock()
fakeStop = nil
reset()
}
}
var fakeStop func(string, bool)
// fakeCalls counts, for each of FakeWSL's distros, the commands run in it
// (wsl.exe -d name …): each one would start a stopped distro.
var fakeCalls struct {
sync.Mutex
in map[string]int
}
// StopFakeWSL, for other packages' tests, has FakeWSL's distro name stop,
// as the user stopping it (wsl --shutdown) does: wsl.exe lists it as not
// running from now on.
func StopFakeWSL(name string) {
if fakeStop != nil {
fakeStop(name, true)
}
}
// StartFakeWSL has a distro StopFakeWSL stopped run again, as the user
// opening a WSL shell does.
func StartFakeWSL(name string) {
if fakeStop != nil {
fakeStop(name, false)
}
}
// FakeWSLRuns is how many commands magpie ran in FakeWSL's distro name
// (wsl.exe -d name …), each of which starts a stopped distro.
func FakeWSLRuns(name string) int {
fakeCalls.Lock()
defer fakeCalls.Unlock()
return fakeCalls.in[name]
}
// FakeWSLDrives, for other packages' tests, has Windows' drives ("C:")
// opened at roots' folder for each, where a link in a FakeWSL distro to
// /mnt/c leads (distro.open); the returned func puts it back.
func FakeWSLDrives(roots map[string]string) func() {
was := wslDrive
wslDrive = func(drive string) string {
if r, ok := roots[drive]; ok {
return r + string(filepath.Separator)
}
return was(drive)
}
return func() { wslDrive = was }
}
// fakeLinks does what wsl_links.go's scripts do in a distro opened at
// root, on this system's own links: a link the distro would lead to
// /home/me/x leads to root's home/me/x here, which this system follows.
func fakeLinks(root string, argv []string) ([]byte, error) {
if len(argv) < 5 || argv[0] != "sh" || argv[1] != "-c" {
return nil, errors.New("unexpected command in WSL: " + strings.Join(argv, " "))
}
at := func(linux string) string { return filepath.Join(root, filepath.FromSlash(linux)) }
args := argv[4:]
switch argv[2] {
case wslLinkScript:
to, link := at(args[0]), at(args[1])
if fi, err := os.Lstat(link); err == nil && fi.Mode()&os.ModeSymlink == 0 {
return []byte(args[1] + " is not a link"), errors.New("exit status 3")
}
if err := os.MkdirAll(filepath.Dir(link), 0o755); err != nil {
return nil, err
}
os.Remove(link)
return nil, os.Symlink(to, link)
case wslUnlinkScript:
p := at(args[0])
fi, err := os.Lstat(p)
if err != nil {
return nil, nil
}
if fi.Mode()&os.ModeSymlink == 0 {
return []byte(args[0] + " is not a link"), errors.New("exit status 3")
}
return nil, os.Remove(p)
case wslLinksScript:
es, _ := os.ReadDir(at(args[0]))
var b strings.Builder
for _, e := range es {
if to, err := os.Readlink(filepath.Join(at(args[0]), e.Name())); err == nil {
// spelt as the distro spells it
if rel, ok := strings.CutPrefix(to, root); ok {
to = filepath.ToSlash(rel)
}
fmt.Fprintf(&b, "%s\t%s\n", e.Name(), to)
}
}
return []byte(b.String()), nil
}
return nil, errors.New("unexpected script in WSL: " + argv[2])
}
// realWSLRun is wsl.exe itself, for FakeWSLRealLinks.
var realWSLRun = wslRun
// FakeWSLRealLinks, for a test on Windows with a real distro behind
// FakeWSL's root (\\wsl.localhost\<distro>), has the links in it made,
// read and removed by the real wsl.exe, in that distro alone.
func FakeWSLRealLinks() (restore func()) {
exec := wslExec
wslExec = func(distro string, argv ...string) ([]byte, error) {
return realWSLRun(30*time.Second, append([]string{"-d", distro, "--exec"}, argv...)...)
}
return func() { wslExec = exec }
}