Files
magpie/internal/proc/proc.go
T
yetoneandvincent f4068ccc25 proc: with Detect agents in WSL off, no wsl.exe is started on any path (#4872)
With Settings' Detect agents in WSL off (noWSLAgents), every Claude
request still reached wsl.exe: wslReady (wslClaudeStandIn,
wslCodexStandIn) and wslClaudeClassifier listed the running distros
without asking wslLooks, the gate #1264 put on wslAgents, wslHomes and
WSLRunning. wslrun's Up, In and running weren't gated either (only
Find was). On a computer without WSL, Windows' inbox wsl.exe answers
each ask with a window offering to install WSL, one per request.

The fix is at the one choke point every command magpie starts goes
through (TestNoCommandBypassesProc): proc.Command and CommandContext
(and so ProbeContext, wslrun.Tool.Command/Probe, the Claude TUI
helper's ConPTY start, which checks cmd.Err) refuse wsl.exe by any
name or path (wsl, WSL.EXE, C:\Windows\System32\wsl.exe) with
proc.ErrWSLOff while the setting is off, as a probe of an app's own
program is refused. The paths that reach it: agent's wslRun/wslExec
(lists, probes, links, chmod, reach), wslrun.Run/Find/In/Up/Stop,
library's cmdvars, mcp_wsl, mcp_check and rtk_wsl, claudetui's
wslread, the gateway's Claude Code in WSL. The settings read is
settings.Shared() and only for a wsl name. Not gated: the Sessions
page's resume line for a WSL session (a string a terminal runs on the
user's click), whose sessions aren't listed with detection off.
skygunner/vincent's stand-in fix is kept as well, so the stand-in and
classifier return before taking wsl's lock or recording a failed list.

Run:
- mote-run: go vet ./internal/proc ./internal/agent ./internal/wslrun;
  GOOS=windows go vet of proc, agent, wslrun, claudetui, library,
  gateway; GOOS=linux go vet of proc, agent, wslrun, claudetui: clean.
  go test -count=1 ./internal/proc ./internal/wslrun ./internal/claudetui:
  ok; -run 'WSL|Wsl|Dsh' ./internal/agent ./internal/library
  ./internal/sessions: ok. gofmt -l: nothing.
- win-run on xiaomi-gpu (Windows 11, real wsl.exe present): the four new
  tests plus TestWSLDetectionOff, TestWSLClaudeStandIn,
  TestWSLCodexStandIn, TestNoCommandBypassesProc: ok; -v shows each new
  test PASS; go test ./internal/proc ./internal/wslrun whole: ok.
  TestWSLRunnerRefusedWhenOff runs agent's real wslRun there.

Broken by hand (wslOff's check made `if false && …`, agent/wsl.go at
origin/main, new tests kept), on mote: TestWSLOffStartsNoWSL red for
every name and maker, TestRunRefusedWhenOff "Run ran wsl.exe: exit
status 1", TestWSLRunnerRefusedWhenOff red, TestWSLStandInAsksNothing
WhenOff red with three "-l --running -q" asks.

Not tried: the reporter's own Windows without WSL (the install window
itself); the full suite runs after the push under the suite lock.

Reported-by: 陌离 (Discord)
Co-authored-by: vincent <vincent@users.noreply.github.com>
2026-11-06 21:28:24 +08:00

50 lines
1.8 KiB
Go

// Package proc makes the commands magpie runs: agents' CLIs, the probes
// that ask them what they have, the system's own tools. Every one is made
// here rather than with os/exec, so that on Windows none of them opens a
// window.
//
// The desktop app on Windows is linked as a GUI program and has no console.
// A console program it starts (devin, claude, codex, cursor-agent, grok, the
// probes behind them) is given a new console by Windows, and with it a
// window, one for every run; so a child of a magpie with no console is
// started with CREATE_NO_WINDOW, which gives it a console without one. A
// magpie run in a terminal, or the desktop app started from one with a
// command, lets its children share that console as before, so Ctrl+C there
// reaches them too. Nothing magpie starts reads or writes the terminal
// itself: its output is piped back, which a hidden console doesn't change.
package proc
import (
"context"
"os/exec"
"time"
)
// Command is exec.Command, with no window of its own on Windows.
func Command(name string, args ...string) *exec.Cmd {
cmd := exec.Command(name, args...)
wslOff(cmd, name)
batch(cmd)
hide(cmd)
return cmd
}
// CommandContext is exec.CommandContext, with no window of its own on
// Windows.
func CommandContext(ctx context.Context, name string, args ...string) *exec.Cmd {
cmd := exec.CommandContext(ctx, name, args...)
wslOff(cmd, name)
// the context ending kills the command, but not what it started: a CLI
// that is a script running node leaves node holding the output, and
// Output waited for it however long it took (#123) — its pipes are
// closed a moment after instead
cmd.WaitDelay = waitDelay
// on Windows what it started is killed with it (#2341)
cancelTree(cmd)
batch(cmd)
hide(cmd)
return cmd
}
var waitDelay = 2 * time.Second