Files
magpie/internal/proc/list_windows.go
T
yetone 1987fe220c agent restart: a script run on the app's own executable is not the app (WorkBuddy's sidecar)
Northes (Discord #feedback, 11-04): WorkBuddy's Agents row kept "重启
WorkBuddy 后生效" after they quit and reopened it, and its Restart quit
WorkBuddy and never opened it again. #3700 (Raters0) is the second half on
WorkBuddy AI.

Cause, from WorkBuddy 5.5.6's app.asar (read only) and its processes on the
Windows box: WorkBuddy starts a sidecar as `<its own executable>
.../app.asar/main/sidecar-entry.js --token ...` (ELECTRON_RUN_AS_NODE,
detached). The sidecar outlives the app's quit and the next start reuses
it, and its prewarm and `--serve` CLIs run as `<executable>
.../cli/bin/codebuddy ...`. The daemon is `WorkBuddy.exe ...
daemon-app-server-entry.js --stdio`. All of them match the running check's
`Contents/MacOS/<exe>` pattern (and WorkBuddy.exe's image on Windows), so:
- (a) findRestartable took the oldest match, the sidecar, as the app's
  start, so magpie's change was always "after" it and the row never
  cleared, however often the app restarted. On Windows the earliest start
  was used too.
- (b) reopenBundle waited for every process under Contents/ to end; the
  sidecar never does, so after 30s it said "still open" and never ran
  `open`.

Fix: runsScript(cmd, exe) tells a script run on the app's executable (a
.js/.cjs/.mjs argument, or a first non-flag argument that is an absolute
path) from the app itself and its helpers (--type=..., /prefetch:,
-psn_, a deep link). The macOS running check and the reopen's wait skip
such processes (appProcs), and the Windows check reads each process's
command line (proc.CommandLine, new) and skips them too. The reopen still
waits for real helpers (renderer, GPU). This is shared by every restartable
app: Claude desktop, WorkBuddy, WorkBuddy AI and the Codex app.

Tests (red on the old code: a copy of the base with runsScript returning
false failed all three, since = the sidecar's 2h, the Windows since the
sidecar's, and 6 RunsScript rows):
- TestWorkBuddySidecarOutlivesQuitOnMac (WorkBuddy and WorkBuddy AI, a
  space in the bundle): a 2h-old sidecar and prewarm beside the app; the
  row is behind by the app's start, a manual reopen clears it, the
  sidecar alone is not "running", Restart quits then opens.
- TestWorkBuddySidecarOutlivesQuitOnWindows: same, and Restart closes and
  ends only the app's tree, then opens it.
- TestRunsScript: real Windows command lines from WorkBuddy on the box,
  macOS ones, and #3700's `codebuddy --serve` line.

Run at this head (rebased on d6d3e6cc8):
- mote: gofmt -l clean; GOOS=windows, linux and darwin go vet
  ./internal/agent ./internal/proc; go test -count=1 ./internal/agent
  ./internal/proc ok.
- xiaomi-gpu (win-run): go test ./internal/agent ./internal/proc -run
  'Restart|Sidecar|RunsScript|WorkBuddy|List': the new tests pass; red are
  TestWorkBuddyRestartOnMac and TestWorkBuddyOwnSaveAfterStartIsNoChange/
  darwin (red the same at origin/main d6d3e6cc8, #4115) and
  TestWithViaKeepsTheJoinedList (#4786). The new mac test skips on Windows
  for #4115's reason.
- Real processes, uncommitted tests: on mote a fake "WorkBuddy AI.app"
  (Go sleeper as Electron) with a sidecar started first, through real
  pgrep and sysctl: since is the app's (0.66s), the sidecar alone is not
  running. On xiaomi-gpu a fake WorkBuddy.exe sidecar and app:
  readWinProc's command lines are the real ones and runsScript tells
  them apart.

Not tried: the real WorkBuddy or WorkBuddy AI (not run, ~/.workbuddy not
touched); a real Restart end to end through osascript/open or on Windows
(the quit/open steps are faked); Linux (no restartable app there).

Refs #3700
2026-11-04 14:25:43 +08:00

96 lines
3.3 KiB
Go

package proc
import (
"context"
"strings"
"unsafe"
"golang.org/x/sys/windows"
)
// list walks a Toolhelp snapshot for the processes named prefix*, case
// aside, and reads each one's command line from the process itself. It
// used to ask WMI through a new PowerShell each time, which cost a .NET
// start and a WMI query every few seconds while a Codex daemon was
// watched (#3467); this takes no child process and no WMI. A process
// whose command line isn't readable (another user's, a protected one)
// is listed with its id alone, as WMI listed it.
func list(ctx context.Context, prefix string) ([]Process, error) {
snap, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
if err != nil {
return nil, err
}
defer windows.CloseHandle(snap)
prefix = strings.ToLower(prefix)
var ps []Process
e := windows.ProcessEntry32{Size: uint32(unsafe.Sizeof(windows.ProcessEntry32{}))}
for err = windows.Process32First(snap, &e); err == nil; err = windows.Process32Next(snap, &e) {
if err := ctx.Err(); err != nil {
return nil, err
}
name := windows.UTF16ToString(e.ExeFile[:])
if e.ProcessID == 0 || !strings.HasPrefix(strings.ToLower(name), prefix) {
continue
}
ps = append(ps, Process{PID: int(e.ProcessID), Args: commandLine(e.ProcessID)})
}
if err != windows.ERROR_NO_MORE_FILES {
return nil, err
}
return ps, nil
}
// CommandLine is the command line process pid was started with, "" when
// it can't be read.
func CommandLine(pid int) string { return commandLine(uint32(pid)) }
// commandLine is a process's command line as it was started, "" when it
// can't be read.
func commandLine(pid uint32) string {
h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, pid)
if err != nil {
return ""
}
defer windows.CloseHandle(h)
// the answer is a UNICODE_STRING followed by its text; ask for the
// size first, since a command line can run to 32K characters
var n uint32
_ = windows.NtQueryInformationProcess(h, windows.ProcessCommandLineInformation, nil, 0, &n)
if n < uint32(unsafe.Sizeof(windows.NTUnicodeString{})) {
return ""
}
buf := make([]byte, n)
if err := windows.NtQueryInformationProcess(h, windows.ProcessCommandLineInformation, unsafe.Pointer(&buf[0]), n, &n); err != nil {
return ""
}
return strings.TrimSpace((*windows.NTUnicodeString)(unsafe.Pointer(&buf[0])).String())
}
// family walks a Toolhelp snapshot of every process, with its parent's
// id and its command line.
func family(ctx context.Context) ([]Kin, error) {
snap, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
if err != nil {
return nil, err
}
defer windows.CloseHandle(snap)
var ps []Kin
e := windows.ProcessEntry32{Size: uint32(unsafe.Sizeof(windows.ProcessEntry32{}))}
for err = windows.Process32First(snap, &e); err == nil; err = windows.Process32Next(snap, &e) {
if err := ctx.Err(); err != nil {
return nil, err
}
if e.ProcessID != 0 {
ps = append(ps, Kin{PID: int(e.ProcessID), PPID: int(e.ParentProcessID), Args: commandLine(e.ProcessID)})
}
}
if err != windows.ERROR_NO_MORE_FILES {
return nil, err
}
return ps, nil
}
// endFamily is endTree, which tells what pid started from a later process
// given an old parent's id by its start.
func endFamily(pid int, _ []int) { _ = endTree(pid) }