Files
magpie/internal/proc/probe.go
T
yetone b2f7818d2e proc: a probe never runs a Windows GUI program, so asking opencode's version doesn't open OpenCode there either (saint carol)
saint carol, on Windows with OpenCode's desktop app: opening magpie,
often right after an update, brings up OpenCode. af0cd09c9 covered only a
Mac's app bundles.

OpenCode's desktop app installs per user, to
%LOCALAPPDATA%\Programs\ai.opencode.desktop\OpenCode.exe (electron-builder
NSIS; @opencodedesktop before), with its CLI at
resources\opencode-cli.exe. The installer puts nothing on PATH, and
"Install CLI" is Mac-only. With that folder on PATH, `opencode` finds
OpenCode.exe, since Windows looks names up without case and PATHEXT adds
.exe. magpie's `opencode --version` at each start and after a CLI update
(proc.Version, library openCodeVersion, cliupdate runCheck) then started
the Electron app. Nothing else in magpie starts OpenCode on Windows: no
App Paths lookup, no opencode:// link, and scoop's opencode-desktop gives
a shortcut, not a shim.

ProbeContext now also refuses (cmd.Err, nothing started) a program whose
PE header names the GUI subsystem (guiExe: e_lfanew, PE signature,
optional header Subsystem == 2). Console programs run as before: CLIs,
bun's, npm's and WinGet's shims, opencode-cli.exe. Anything that isn't a
PE, or whose header can't be read, also runs as before. On the Windows
box, every program magpie probes that is installed there is console
(claude.exe, codex.exe, git.exe). The GUI ones on PATH are system32
tools, git-gui, gitk and Codex's codex-command-runner, which no probe
asks.

Tests:
- TestGuiPE: GUI PE32 and PE32+ headers are a window's program; console,
  a bad magic, a cut-short header, a script and empty bytes are not.
- TestProbeNeverOpensAWindowsApp (Windows): the install's layout is made
  from copies of the test binary, which notes that it ran (TestMain's
  MAGPIE_PROC_RAN). OpenCode.exe is patched to the GUI subsystem as
  Electron's is. With the folder on PATH, `opencode` resolves to
  OpenCode.exe and isn't run, and Version is "". opencode-cli.exe still
  answers 2.2.5.

Broken by hand on xiaomi-gpu: with guiExe out of ProbeContext,
TestProbeNeverOpensAWindowsApp goes red ("OpenCode.exe was run and said
2.2.5").

Run at 4b407bb79 plus this change (go test -count=1 -timeout 45m):
- mote (macOS): internal/proc, library, agent, provider, wslrun ok;
  GOOS=windows go vet ./... ok, GOOS=linux go vet -tags nogui ./... ok;
  gofmt -l empty.
- xiaomi-gpu (Windows, win-run): internal/proc ok. internal/library red
  on TestGoneAgentListedToTakeOut and TestCheckMixedServerEachSide, the
  same at 4b407bb79 without this change, both listed in #4115.
- magpie-desktop (Linux, wsl-run): internal/proc ok.

Not tried: the real OpenCode desktop app on Windows (not installed on the
test box; the layout is from OpenCode's electron-builder config and NSIS
include), the reporter's PATH, and a .cmd or .ps1 the user wrote that
starts OpenCode.exe (still run).
2026-11-02 16:24:51 +08:00

89 lines
2.8 KiB
Go

package proc
import (
"context"
"os/exec"
"sync"
"time"
)
// ProbeContext is CommandContext for a command magpie only asks something
// of and waits on for the answer: who a CLI says is signed in, its version.
// The CLI is often a script (a version manager's wrapper, a shim), and
// killing it on the timeout left what it had started running; a probe still
// asking when magpie quit or updated itself was left whole, adopted by init
// with nothing to end it, and one stuck in a loop held a core for hours. A
// probe leads a group of its own on Unix, the context ending kills the whole
// group, and EndProbes does the same for those still asking when magpie
// exits. ctx must end (a timeout): the probe is forgotten when it does.
func ProbeContext(ctx context.Context, name string, args ...string) *exec.Cmd {
ctx, cancel := context.WithCancel(ctx)
p := &probe{cancel: cancel, killing: make(chan struct{}), ended: make(chan struct{})}
cmd := CommandContext(ctx, name, args...)
// a probe never opens an app: asked its version, an app's own program
// puts its window up instead (appMain on a Mac, guiExe on Windows)
if cmd.Err == nil && (appMain(cmd.Path) || guiExe(cmd.Path)) {
cmd.Err = errAppMain
}
probeGroup(cmd, p)
probes.Lock()
probes.m[p] = struct{}{}
probes.Unlock()
context.AfterFunc(ctx, func() {
probes.Lock()
delete(probes.m, p)
probes.Unlock()
})
return cmd
}
type probe struct {
cancel context.CancelFunc
killOnce, once sync.Once
killing, ended chan struct{} // closed once its kill began, and once it was over
}
func (p *probe) kill() { p.killOnce.Do(func() { close(p.killing) }) }
func (p *probe) end() { p.once.Do(func() { close(p.ended) }) }
var probes = struct {
sync.Mutex
m map[*probe]struct{}
}{m: map[*probe]struct{}{}}
// endWait is how long EndProbes waits for a probe's kill to begin: one that
// had answered, or never started, has none.
var endWait = time.Second
// EndProbes kills every probe still running, with what it started, and
// waits until none of that is left: magpie is exiting or putting a new
// version in its place, and nothing would end them after. A kill that missed
// a process being forked signals the group again until it is empty, and an
// exit before that left the missed one running, adopted by init (#1278); the
// wait is bounded by how long a kill goes on (killFor on Unix).
func EndProbes() {
probes.Lock()
ps := make([]*probe, 0, len(probes.m))
for p := range probes.m {
ps = append(ps, p)
}
probes.Unlock()
for _, p := range ps {
p.cancel()
}
begun := time.Now().Add(endWait)
over := begun.Add(killWait())
for _, p := range ps {
select {
case <-p.killing:
case <-time.After(time.Until(begun)):
continue // one that had answered already, or never started
}
select {
case <-p.ended:
case <-time.After(time.Until(over)):
return
}
}
}