Files
magpie/internal/agent/claudescience_wsl.go
T
yetone 2b22425b45 agent: Claude Science in a WSL distro is found and run on magpie from Windows
shuitunlulu_ (Discord) has Claude Science in WSL2, and Windows magpie
didn't find it. It is now a WSL twin (claude-science@wsl:<distro>), found
by the probe's claude-science (~/.local/bin or PATH). Its daemon is
restarted there through wsl.exe with ANTHROPIC_BASE_URL set, the same steps
as this machine's (claudescience.go is now one scienceHost for here and for
each distro), and v0.1.3334's keeper puts it back on magpie in each running
distro that has a note (claude-science-wsl-<distro>.json). With no note,
nothing runs in WSL.

The daemon takes http only to a loopback address and sends its Claude
sign-in as the key. In mirrored networking it gets the gateway's address.
Under NAT magpie starts a relay in the distro: socat on
127.0.0.1:<gateway port> to Windows' WSL address. The daemon gets
http://127.0.0.1:<port>/<WSL key>; the gateway's lanGuard takes the key
off the path (takeWSLPathKey) before fromWSL judges it. Connect says so
when the distro has no socat. Disconnect stops the relay with the
connections it forked, as one process group. When Windows' firewall keeps
WSL out, the row shows the same firewall drift and rule as other WSL
agents.

Run:
- gofmt -l clean
- go vet: darwin, GOOS=linux -tags nogui, GOOS=windows (mote)
- go test ./internal/agent ./internal/gateway on mote (macOS)
- go test ./internal/agent on xiaomi-gpu (Windows 11): 26 FAIL; 25 fail
  the same at origin/main c7ebb6f8d on that box (#4115, #5019, #3700,
  #5344, #2808, #4785, #2958, #1827, #4786). The 26th,
  TestHermesProfilesFollowMagpieModel, passed 5/5 alone on both and
  failed only in the whole-package run (#4115, #2540). Every
  ClaudeScience test passed there.
- At the pushed head (rebased on 9dd7aabca, which changes neither
  package): go vet darwin + windows and -run 'ClaudeScience|WSL' of agent
  and gateway ok on mote.
- Full suite, -timeout 45m: not finished at push. It waits on the lock
  behind other sessions' runs on mote and on magpie-desktop; the
  packages it changes (agent, gateway) were run above.
- Real run against magpie-desktop (Ubuntu 24.04, NAT, claude-science
  0.1.72 in ~/.local/bin) from a Windows test binary, with a sandbox
  XDG_CONFIG_HOME and a stand-in gateway on 127.0.0.1 and 172.17.32.1:
  - Connect took about 10s, and /proc/<daemon>/environ had
    ANTHROPIC_BASE_URL=http://127.0.0.1:<port>/wsl-...
  - The relay outlived wsl.exe (setsid, its own SID).
  - With the firewall blocking the test binary, the row's drift named the
    firewall and the New-NetFirewallRule.
  - With a temporary allow rule for the test binary (removed afterwards),
    curl in WSL through the relay reached the stand-in with the key path,
    and no drift showed.
  - Daemon and relay were killed as a WSL restart would; one keeper pass
    brought both back.
  - Disconnect left no relay and no relay forks (a held curl fork
    included), and the daemon ran without the variable.

Broken by hand:
- relay := false: the NAT test went red six ways.
- Without lanGuard's takeWSLPathKey line: TestWSLKeyInPath got 403.
- Check's firewall branch off: TestClaudeScienceInWSLFirewall red.
- A plain kill of the relay pid left a fork connecting on the real distro;
  that is what led to the process-group kill.

Not tried:
- The real magpie GUI on Windows with this build.
- A daemon's own model request through the relay; only curl was sent.
- Bridged networking.
- A distro without socat; that path was run only in the fake.

Reported-by: shuitunlulu_ (Discord)
2026-11-09 17:46:13 +08:00

206 lines
7.4 KiB
Go

package agent
import (
"fmt"
"net"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/gateway"
)
// Claude Science in a WSL distro (shuitunlulu_ on Discord): its daemon
// runs in the distro, so magpie on Windows starts it there through
// wsl.exe, with ANTHROPIC_BASE_URL set, as it does this machine's
// (claudescience.go), and keeps it so from the keeper while the distro
// runs; it never starts a stopped distro to.
//
// The daemon takes http only to a loopback address, and sends its Claude
// sign-in as its key. In mirrored (or consomme) networking the distro's
// 127.0.0.1 is Windows', and it is given the gateway's own address. Under
// NAT it is given a relay magpie starts in the distro: socat on
// 127.0.0.1:<the gateway's port> there, to Windows' address on the WSL
// network, which the gateway listens on while on loopback; the WSL key,
// which it can't send as a key, goes first in the address's path, where
// the gateway takes it off (gateway.takeWSLPathKey).
// scienceWSLScript runs claude-science in the distro: $1 is the address
// the daemon is given ("": none), the rest its arguments. The program is
// where its installer puts it (~/.local/bin), else on the login PATH.
const scienceWSLScript = `g=$1; shift; b="$HOME/.local/bin/claude-science"; [ -x "$b" ] || b=$(command -v claude-science 2>/dev/null) || { echo "claude-science isn't installed"; exit 127; }; ` +
`if [ -n "$g" ]; then ANTHROPIC_BASE_URL=$g; export ANTHROPIC_BASE_URL; else unset ANTHROPIC_BASE_URL; fi; exec "$b" "$@" </dev/null`
// scienceRelayScript starts the relay: $1 is the port it listens on at
// 127.0.0.1, $2 where it leads. One running to the same place is left
// running; one to another (Windows' address moved with a restart of WSL)
// is stopped first, with the connections it forked (its own process
// group, as setsid starts it). Its pid and where it leads are kept in
// ~/.claude-science/magpie-relay; a pid there is taken for it only while
// /proc says it is socat.
const scienceRelayScript = `port=$1; to=$2; d="$HOME/.claude-science"; pf="$d/magpie-relay"; ` +
`if [ -r "$pf" ]; then read -r pid was < "$pf"; if [ -n "$pid" ] && [ "$(cat /proc/$pid/comm 2>/dev/null)" = socat ]; then [ "$was" = "$to" ] && exit 0; kill -TERM -"$pid" 2>/dev/null || kill "$pid" 2>/dev/null; sleep 0.3; fi; rm -f "$pf"; fi; ` +
`[ -z "$to" ] && exit 0; ` +
`command -v socat >/dev/null 2>&1 || { echo "socat isn't installed in the distro (sudo apt install socat)"; exit 3; }; ` +
`mkdir -p "$d"; setsid socat TCP-LISTEN:$port,bind=127.0.0.1,reuseaddr,fork TCP:$to </dev/null >/dev/null 2>&1 & pid=$!; sleep 0.5; ` +
`if [ "$(cat /proc/$pid/comm 2>/dev/null)" = socat ]; then echo "$pid $to" > "$pf"; exit 0; fi; ` +
`echo "socat couldn't listen on 127.0.0.1:$port there (taken by another program?)"; exit 4`
// scienceWSLKeeps are the keepers' states of the distros' daemons, by
// distro.
var scienceWSLKeeps = struct {
sync.Mutex
keep map[string]*scienceKeeper
mu map[string]*sync.Mutex
}{keep: map[string]*scienceKeeper{}, mu: map[string]*sync.Mutex{}}
// scienceWSLMark is magpie's note of the daemon it started in distro
// name: one file a distro, as each keeps its own.
func scienceWSLMark(name string) string {
safe := strings.Map(func(r rune) rune {
if r == '-' || r == '_' || r == '.' || r >= '0' && r <= '9' || r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' {
return r
}
return '_'
}, name)
return filepath.Join(appdir.Config(), "claude-science-wsl-"+safe+".json")
}
// scienceWSLRun runs script in distro name with args; a var for tests.
var scienceWSLRun = func(name string, timeout time.Duration, script string, args ...string) ([]byte, error) {
return wslRun(timeout, append([]string{"-d", name, "-e", "sh", "-lc", script, "sh"}, args...)...)
}
// scienceInWSL is the Claude Science of distro d.
func scienceInWSL(d distro) *scienceHost {
scienceWSLKeeps.Lock()
k := scienceWSLKeeps.keep[d.Name]
if k == nil {
k = &scienceKeeper{}
scienceWSLKeeps.keep[d.Name] = k
}
mu := scienceWSLKeeps.mu[d.Name]
if mu == nil {
mu = &sync.Mutex{}
scienceWSLKeeps.mu[d.Name] = mu
}
scienceWSLKeeps.Unlock()
home := d.local(d.Home)
relay := !d.Mirrored && d.Gateway != ""
h := &scienceHost{
id: ScienceID + "@wsl:" + d.Name,
// found by the probe: the script finds it again where it runs
bin: func() string {
if d.Has["bin:claude-science"] {
return "claude-science"
}
return ""
},
run: func(_, gw string, args ...string) ([]byte, error) {
out, err := scienceWSLRun(d.Name, 90*time.Second, scienceWSLScript, append([]string{gw}, args...)...)
if err != nil && len(out) > 0 {
err = fmt.Errorf("%w: %s", err, strings.TrimSpace(string(out)))
}
return out, err
},
dir: func() string {
p, err := d.open(filepath.Join(home, ".claude-science"))
if err != nil {
return filepath.Join(home, ".claude-science")
}
return p
},
stat: func(string) (os.FileInfo, error) {
return os.Stat(filepath.Join(home, ".local", "bin", "claude-science"))
},
gw: func() string {
if !relay {
return d.base()
}
k := gateway.WSLKey()
if k == "" {
return ""
}
return "http://127.0.0.1:" + gateway.Port() + "/" + k
},
mark: func() string { return scienceWSLMark(d.Name) },
keep: k,
mu: mu,
}
if relay {
to := net.JoinHostPort(d.Gateway, gateway.Port())
h.ready = func() error { return scienceRelay(d.Name, gateway.Port(), to) }
h.unready = func() error { return scienceRelay(d.Name, gateway.Port(), "") }
}
return h
}
// scienceRelay starts the relay in distro name on port to to, or stops
// it for to "".
func scienceRelay(name, port, to string) error {
out, err := scienceWSLRun(name, 30*time.Second, scienceRelayScript, port, to)
if err != nil {
if said := strings.TrimSpace(string(out)); said != "" {
return fmt.Errorf("the relay to magpie's gateway in WSL %s: %s", name, said)
}
return fmt.Errorf("the relay to magpie's gateway in WSL %s: %w", name, err)
}
return nil
}
// scienceIn is Claude Science's row at a place: a WSL distro's
// (wslKinds); there is none elsewhere.
func scienceIn(p place) *Agent {
if p.wsl == nil {
return &Agent{ID: ScienceID, Name: scienceName, detect: func() bool { return false }}
}
h := scienceInWSL(*p.wsl)
a := scienceAgent(h)
notice := a.Notice
d := *p.wsl
// the relay leads to Windows' address on the WSL network, where the
// gateway listens and Windows' firewall may keep WSL out: its row says
// so as any agent pointed there would (reach.go), though its one value
// isn't a model of magpie's, which is what Drift asks that of
check := a.Check
a.Check = func() string {
if c := check(); c != "" || !h.wired() {
return c
}
if u := a.unreachable(a.Fields[0], magpieID); u != nil {
return u.Detail
}
return ""
}
a.Notice = func() string {
if d.noRelay {
return noticeWSLNoRelay.say("distro", d.Name, "net", d.Net, "agent", scienceName)
}
if notice != nil {
return notice()
}
return ""
}
return a
}
// wslScienceHosts are the Claude Sciences of the running distros magpie
// connected: none, and no wsl.exe run, while it connected none.
func wslScienceHosts() []*scienceHost {
marks, _ := filepath.Glob(filepath.Join(appdir.Config(), "claude-science-wsl-*.json"))
if len(marks) == 0 {
return nil
}
var out []*scienceHost
for _, d := range wslReady(ScienceID) {
h := scienceInWSL(d)
if h.wired() {
out = append(out, h)
}
}
return out
}