Files
magpie/internal/proc/guiexe.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

50 lines
1.6 KiB
Go

package proc
import (
"encoding/binary"
"io"
"os"
)
// guiExe reports whether the program at path is a Windows program of the
// GUI subsystem (IMAGE_SUBSYSTEM_WINDOWS_GUI in its PE header): one that
// opens a window, has no console, and never answers --version on one.
// OpenCode's desktop app is OpenCode.exe in
// %LOCALAPPDATA%\Programs\ai.opencode.desktop, an Electron program; with
// that folder on PATH, Windows' case-blind search finds it as opencode,
// and magpie's version check put OpenCode's window up at every start and
// after each update (saint carol on Discord). Its CLI (resources\
// opencode-cli.exe), npm's, bun's and scoop's shims are console programs
// and are run. Anything that isn't a PE, or whose header can't be read,
// is run as before.
func guiExe(path string) bool {
f, err := os.Open(path)
if err != nil {
return false
}
defer f.Close()
var b [4096]byte
n, _ := io.ReadFull(f, b[:])
return guiPE(b[:n])
}
const peSubsystemGUI = 2
// guiPE reads the subsystem from a PE image's first bytes: DOS header,
// PE signature at e_lfanew, the 20-byte COFF header, then the optional
// header, whose Subsystem is at 68 in PE32 and PE32+ alike.
func guiPE(b []byte) bool {
if len(b) < 0x40 || b[0] != 'M' || b[1] != 'Z' {
return false
}
pe := int(binary.LittleEndian.Uint32(b[0x3c:]))
opt := pe + 4 + 20
if pe < 0x40 || opt+70 > len(b) || string(b[pe:pe+4]) != "PE\x00\x00" {
return false
}
if magic := binary.LittleEndian.Uint16(b[opt:]); magic != 0x10b && magic != 0x20b {
return false
}
return binary.LittleEndian.Uint16(b[opt+68:]) == peSubsystemGUI
}