Files
magpie/internal/library/rtk_wsl.go
T
yetone 73a12e8bf6 wsl: a distro's agent folder that is a link is opened where it leads (#4502)
On Windows, adding a skill or switching RTK on for an agent in WSL failed
with `mkdir \\wsl.localhost\<distro>\root\.codex: Cannot create a file
when that file already exists` (linusxiong). In the distro ~/.codex is a
link to /mnt/c/Users/<user>/.codex. Through \\wsl.localhost Windows can't
follow a link the distro has: a mkdir of it says it exists, and nothing
below it can be made, written, read or listed. ggbdpq found and explained
the link (thanks); their patch, which takes that EEXIST as "there", wasn't
used: the writes below the link fail next (its test passed because a
symlink here is followed).

The fix, for every agent folder and every writer through \\wsl.localhost:
- The WSL probe asks `readlink -f` in the distro where each agent folder,
  its skills folder and the folders beside them lead (wslLinkPaths ->
  distro.Links; a $HOME that is itself through a link is left alone), and
  which drive each Windows mount is (win:/mnt/c C: -> distro.Drives).
- distro.open opens a path at or below a link where it leads: on a Windows
  drive, the drive's own path (C:\Users\<user>\.codex); elsewhere in the
  distro, through \\wsl.localhost again. native spells it back through
  the link, so the agent is told its own path.
- A link readlink couldn't resolve is never guessed at: distro.open
  errors, and the agent is unreached (Agent.Unreached: its notice says
  why; every field Set and Sync returns it).
- Writers: the agents' own files (place.join/place.open, every
  filepath.Join(at.home, ...) in internal/agent), the library's WSL
  targets (wslOpen/wslOpened; one "wsl" problem per agent and nothing
  written), RTK (rtkAt, rtkInstall checks agent.WSLOpen before its mkdir),
  the Sessions page's WSL reads (sessions.WSLHome.Open) and the Copilot
  CLI sign-in read (provider.CopilotWSLHomes -> CopilotWSLDirs).
- Not changed: magpie's image MCP server opens arbitrary paths an agent
  in WSL gives it the same way; filed as #4578. Links deeper than the
  probed paths (other than <agent dir>/skills) aren't resolved. A distro
  probed by an older magpie has no Links until probed again (each run).

Run:
- Real Windows box (xiaomi-gpu, distro magpie-desktop, sandbox
  /tmp/x4502/home with .codex -> /mnt/c/Users/yeton/runs/x4502-target/.codex,
  .claude -> /tmp/x4502/data/claude, .pi -> a missing folder), a throwaway
  Windows test running the real probe with that HOME: probe said
  link:.codex /mnt/c/..., link:.claude /tmp/x4502/data/claude, link:.pi,
  win:/mnt/c C:, win:/mnt/d D:. The old way, MkdirAll(\\wsl.localhost\
  magpie-desktop\tmp\x4502\home\.codex\skills\pdf) = "Cannot create a file
  when that file already exists" (the report's error). distro.open wrote
  SKILL.md to C:\Users\yeton\runs\x4502-target\.codex\skills\pdf and to
  \\wsl.localhost\magpie-desktop\tmp\x4502\data\claude\skills\pdf, both
  read back with cat in the distro through the links; .pi said "~/.pi in
  WSL magpie-desktop is a link magpie couldn't follow". Sandbox and target
  removed after.
- New tests with a fake distro whose ~/.codex and ~/.claude are links to
  /mnt/c/... that don't exist here, so, as through \\wsl.localhost,
  nothing below them can be made: TestWSLFolderIsALinkToC,
  TestWSLFolderLinkUnresolved, TestRTKWSLFolderIsALink,
  TestRTKWSLFolderLinkUnresolved (library); TestWSLProbeLinks (runs the
  probe script), TestWSLOpenThroughLinks, TestWSLAgentFolderIsALink
  (config and a Claude session read on C:), TestWSLAgentFolderLinkUnresolved
  (agent).
- Broken by hand: the library tests on the base (with a no-op
  FakeWSLDrives) fail with "mkdir .../home/me/.codex: file exists", the
  report's error; with ggbdpq's patch applied they still fail (the skill
  mkdir, and RTK's hook not written).
- mote: go build ./...; go vet of agent, library, sessions, provider for
  darwin, GOOS=windows (and linux for agent, library); gofmt -l clean;
  go test -timeout 45m ./internal/agent ./internal/library
  ./internal/sessions ./internal/provider: all ok.
- Windows (win-run): the same four packages. The new tests pass (the
  RTK ones skip: the fake distro runs sh). 32 tests are red, all already
  red at the base c63c2136b run on the same box, and filed (#4115, #2808:
  Hermes profiles, Codex keychain, Agy, desktop entries, ...); the Hermes
  ones fail alike on base and branch with -count=3.

- Rebased on origin/main 3ecc02b78 (it changed internal/agent): at this
  head on mote, go build ./..., go vet of the four packages for darwin,
  windows and linux, gofmt -l, go test ./internal/agent ./internal/library:
  ok.

Not tried: the full ./... suite; the GUI (no GUI change; the unreached
notice uses the existing Notice); a real Codex/Claude Code run against
the moved files in the distro.
2026-10-31 21:29:22 +08:00

296 lines
9.8 KiB
Go

package library
import (
"errors"
"fmt"
"os/exec"
"path/filepath"
"slices"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/wslrun"
)
// RTK for an agent in a WSL distro (lex on Discord: Claude Code in WSL
// couldn't have the RTK of a magpie on Windows). Its hook runs in the
// distro, where a Windows rtk.exe is no command, so it is the distro's own
// rtk that is installed (rtk's own Linux script, run there), found, and
// run for rtk init, with its files under the distro's home as magpie
// opens it (\\wsl.localhost\<distro>\home\<user>). A distro that isn't
// running is never started for it: its agents are listed asleep.
// RTKDistro is the rtk of one WSL distro an agent listed lives in.
type RTKDistro struct {
Name string `json:"name"`
// Path is rtk's inside the distro, "" when it isn't installed there
Path string `json:"path,omitempty"`
Version string `json:"version,omitempty"`
// OffPath: found where its installer puts it, but not on the PATH of
// the distro's shells, which its hooks run rtk by
OffPath bool `json:"offPath,omitempty"`
// Install is the command Install RTK runs in the distro
Install string `json:"install,omitempty"`
// Asleep: the distro isn't running, and isn't looked in
Asleep bool `json:"asleep,omitempty"`
}
// rtkKind is the rtkSpecs (and rtkNoHook) key of agent id: the id itself,
// or one in WSL's (claude@wsl:Ubuntu) before its @wsl:.
func rtkKind(id string) string {
k, _, _ := strings.Cut(id, "@wsl:")
return k
}
// rtkHome is the home rtk's installer writes under for a: the distro's for
// one in WSL, as magpie opens it; this machine's otherwise.
func rtkHome(a *agent.Agent) string {
if a != nil && a.WSL != "" {
return a.Home
}
return home()
}
// rtkAt is a path under rtkHome(a): in a distro, opened where a link
// there leads (agent.WSLOpen, #4502), as nothing below one can be opened
// through \\wsl.localhost; as it is when the link couldn't be resolved,
// which rtkInstall says.
func rtkAt(a *agent.Agent, elem ...string) string {
p := filepath.Join(append([]string{rtkHome(a)}, elem...)...)
if a == nil || a.WSL == "" {
return p
}
if o, err := agent.WSLOpen(a, p); err == nil {
return o
}
return p
}
// rtkClaudeDir is Claude Code's folder for a: ~/.claude in a distro, whose
// CLAUDE_CONFIG_DIR magpie can't read (as wslTargetOf).
func rtkClaudeDir(a *agent.Agent) string {
if a != nil && a.WSL != "" {
return rtkAt(a, ".claude")
}
return claudeDir()
}
// rtkCodexDir is Codex's folder for a: ~/.codex in a distro.
func rtkCodexDir(a *agent.Agent) string {
if a != nil && a.WSL != "" {
return rtkAt(a, ".codex")
}
return codexDir()
}
// rtkVersionOf is the version of the rtk a's hook runs: its distro's for
// one in WSL.
func rtkVersionOf(a *agent.Agent) string {
if a != nil && a.WSL != "" {
return rtkDistroOf(a.WSL, false).Version
}
return rtkVersion()
}
// openCodeVersionOf is the version of a's OpenCode: for one in WSL what the
// distro's probe found, "" when it didn't ask.
func openCodeVersionOf(a *agent.Agent) string {
if a != nil && a.WSL != "" {
return a.InstalledVersion()
}
return openCodeVersion()
}
// rtkWSLLive is whether a's distro runs now and its files can be opened
// without starting it.
func rtkWSLLive(a *agent.Agent) bool {
return a.Home != "" && agent.WSLRunning(a.WSL)
}
// rtkWSLRun runs wsl.exe with args; a var for tests.
var rtkWSLRun = wslrun.Run
// rtkWSLProbe prints where rtk is in the distro: on the PATH of a login
// shell, or an interactive one's (where .bashrc puts it), "on:1"; else
// where its installers put it, "on:0". A Windows rtk.exe seen through
// /mnt isn't the distro's.
const rtkWSLProbe = `p=$(command -v rtk 2>/dev/null); ` +
`case "$p" in /*) ;; *) p=$(bash -ic 'command -v rtk' 2>/dev/null </dev/null | tail -n1);; esac; ` +
`on=1; case "$p" in /mnt/*|"") p=""; on=0; ` +
`for c in "$HOME/.local/bin/rtk" "$HOME/.cargo/bin/rtk"; do [ -x "$c" ] && { p=$c; break; }; done;; /*) ;; *) p=""; on=0;; esac; ` +
`[ -n "$p" ] || exit 0; echo "bin:$p"; echo "on:$on"; ` +
`echo "version:$("$p" --version 2>/dev/null | head -n1)"`
// rtkWSLScript is rtk's own Linux installer, run in the distro: it puts rtk
// in ~/.local/bin. curl, or wget where a distro has no curl.
const rtkWSLScript = `set -e; u=` + rtkScript + `; ` +
`if command -v curl >/dev/null 2>&1; then s=$(curl -fsSL "$u"); ` +
`elif command -v wget >/dev/null 2>&1; then s=$(wget -qO- "$u"); ` +
`else echo "neither curl nor wget is installed in this distro" >&2; exit 1; fi; ` +
`printf '%s\n' "$s" | sh`
// rtkWSLShown is how Install RTK names what it runs in a distro.
const rtkWSLShown = "curl -fsSL " + rtkScript + " | sh"
var rtkDistros_ struct {
sync.Mutex
at map[string]time.Time
got map[string]RTKDistro
}
// rtkDistroAge is how long a distro's answer is kept: an rtk installed in
// it by hand is seen within it.
const rtkDistroAge = 15 * time.Second
// rtkDistroOf is the rtk of distro, which is asked (never started) unless
// it answered within rtkDistroAge; fresh asks it again.
func rtkDistroOf(distro string, fresh bool) RTKDistro {
d := RTKDistro{Name: distro}
if !agent.WSLRunning(distro) {
d.Asleep = true
return d
}
rtkDistros_.Lock()
at, ok := rtkDistros_.at[distro]
kept := rtkDistros_.got[distro]
rtkDistros_.Unlock()
if ok && !fresh && time.Since(at) < rtkDistroAge {
return kept
}
// asked with no lock held: a distro slow to answer holds no other read
out, err := rtkWSLRun(30*time.Second, "-d", distro, "-e", "sh", "-lc", rtkWSLProbe)
if err != nil {
// not known: not kept, and not offered an install either
return d
}
for _, l := range strings.Split(string(out), "\n") {
k, val, _ := strings.Cut(strings.TrimSpace(l), ":")
switch val = strings.TrimSpace(val); k {
case "bin":
d.Path = val
case "on":
d.OffPath = val == "0"
case "version":
if m := semver.FindStringSubmatch(val); m != nil {
d.Version = m[1]
}
}
}
if !strings.HasPrefix(d.Path, "/") {
d.Path, d.OffPath, d.Version = "", false, ""
}
if d.Path == "" {
d.Install = rtkWSLShown
}
rtkDistros_.Lock()
defer rtkDistros_.Unlock()
if rtkDistros_.at == nil {
rtkDistros_.at, rtkDistros_.got = map[string]time.Time{}, map[string]RTKDistro{}
}
rtkDistros_.at[distro], rtkDistros_.got[distro] = time.Now(), d
return d
}
// rtkDistros is the rtk of each distro an agent of list lives in.
func rtkDistros(list []RTKAgent) []RTKDistro {
var names []string
for _, a := range list {
if a.WSL != "" && !a.NoHook && !slices.Contains(names, a.WSL) {
names = append(names, a.WSL)
}
}
var out []RTKDistro
for _, n := range names {
out = append(out, rtkDistroOf(n, false))
}
return out
}
// shq is s quoted for sh.
func shq(s string) string { return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" }
// rtkInstallWSL runs rtk's installer in a's distro, by the distro's rtk
// (bin, its path there). It runs in a login shell, with what that gives the
// distro's agents; HOME is the distro's, so it writes under the home magpie
// reads a's files at. Claude Code is pointed at a folder of its own made
// and taken away there, as on this machine. An agent's folder variable
// (HERMES_HOME) isn't set: the distro's own, if any, is what its agent
// reads too.
func rtkInstallWSL(bin string, a *agent.Agent, sp rtkSpec) error {
run, args := rtkInitWSL(bin, sp)
out, err := rtkWSLRun(time.Minute, "-d", a.WSL, "-e", "sh", "-lc", run)
if err != nil {
said := installerSaid(string(out)+"\n"+exitSaid(err), 3)
return fmt.Errorf("rtk %s in WSL %s: %s", strings.Join(args, " "), a.WSL, said)
}
if !sp.has(a) {
return fmt.Errorf("rtk's installer didn't add its hook to %s: %s", a.Name, installerSaid(string(out), 3))
}
return nil
}
// rtkInitWSL is the sh command that runs the distro's rtk (bin) for sp's
// agent, and rtk init's arguments.
func rtkInitWSL(bin string, sp rtkSpec) (string, []string) {
args := append([]string{"init", "-g"}, sp.flags...)
if sp.patch {
args = append(args, "--auto-patch")
}
q := make([]string, len(args))
for i, s := range args {
q[i] = shq(s)
}
run := "PATH=" + shq(bin[:strings.LastIndex(bin, "/")]) + `:"$PATH" ` + shq(bin) + " " + strings.Join(q, " ") + " </dev/null 2>&1"
if sp.withClaude {
run = `t=$(mktemp -d) || exit 1; CLAUDE_CONFIG_DIR="$t" ` + run + `; s=$?; rm -rf "$t"; exit $s`
}
return run, args
}
// exitSaid is what a failed wsl.exe wrote to stderr, or the error.
func exitSaid(err error) string {
var ee *exec.ExitError
if errors.As(err, &ee) && len(ee.Stderr) > 0 {
return string(ee.Stderr)
}
return err.Error()
}
// InstallRTKIn installs rtk in WSL distro with rtk's own Linux installer,
// run there, when the user asks for it. A distro that isn't running isn't
// started for it.
func InstallRTKIn(distro string) (*RTKView, error) {
rtkMu.Lock()
known := false
for _, a := range rtkAgents(detected()) {
known = known || strings.EqualFold(a.WSL, distro)
if strings.EqualFold(a.WSL, distro) {
distro = a.WSL
}
}
if !known {
rtkMu.Unlock()
return nil, fmt.Errorf("no agent RTK has a hook for is in a WSL distro named %q", distro)
}
if !agent.WSLRunning(distro) {
rtkMu.Unlock()
return nil, fmt.Errorf("WSL %s isn't running, and magpie doesn't start it to install RTK: start the distro (open its terminal), then install again", distro)
}
if d := rtkDistroOf(distro, true); d.Path != "" {
rtkMu.Unlock()
return ReadRTK(), nil
}
out, err := rtkWSLRun(5*time.Minute, "-d", distro, "-e", "sh", "-lc", rtkWSLScript+" 2>&1")
d := rtkDistroOf(distro, true)
rtkMu.Unlock()
if err != nil {
return nil, fmt.Errorf("RTK's installer failed in WSL %s: %s", distro, installerSaid(string(out)+"\n"+exitSaid(err), 3))
}
if d.Path == "" {
return nil, fmt.Errorf("RTK's installer ran in WSL %s, but rtk still isn't found there: %s", distro, installerSaid(string(out), 3))
}
return ReadRTK(), nil
}