Files
magpie/internal/proc
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
..
gui, proc: the Gateway card quits an older magpie serving the gateway and takes it over, or restarts the gateway (leslie_luo on Discord) — the card said an older magpie served the gateway and to quit it, with no way to tell which process that was. With an older magpie on the port it now has "Quit magpie {v} and take over": the process listening on the gateway's port is found (lsof on a Mac, /proc on Linux, Get-NetTCPConnection on Windows), its program path checked to be magpie's (magpie, magpie-*, dial, .exe/.old), asked to quit (SIGTERM, then SIGKILL after 5 s; TerminateProcess on Windows) and this magpie serves the gateway at once instead of within 15 s. Otherwise the card has "Restart gateway" ("Start gateway" when none runs); a magpie of this version or newer gets no button. Anything else on the port is never ended: a program that isn't magpie, one of another user or an administrator, one that won't quit, or a magpie a LaunchAgent/systemd unit/scheduled task starts again is said under the button with its path and pid, in en/zh/ja/de. TestGatewayFixQuitsOnlyAMagpie (copies of the test binary on a port of its own: one named other-server answering as magpie is left running, one named magpie is quit and the gateway is served here, then restarted) and gateway-takeover.test.cjs (WebKit and Chromium, en and zh, no scroll, no left border) fail without the change; TestListeningOn/TestIsMagpie pass on macOS and in a Linux container. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 15:05:31 +08:00
gui, proc: the Gateway card quits an older magpie serving the gateway and takes it over, or restarts the gateway (leslie_luo on Discord) — the card said an older magpie served the gateway and to quit it, with no way to tell which process that was. With an older magpie on the port it now has "Quit magpie {v} and take over": the process listening on the gateway's port is found (lsof on a Mac, /proc on Linux, Get-NetTCPConnection on Windows), its program path checked to be magpie's (magpie, magpie-*, dial, .exe/.old), asked to quit (SIGTERM, then SIGKILL after 5 s; TerminateProcess on Windows) and this magpie serves the gateway at once instead of within 15 s. Otherwise the card has "Restart gateway" ("Start gateway" when none runs); a magpie of this version or newer gets no button. Anything else on the port is never ended: a program that isn't magpie, one of another user or an administrator, one that won't quit, or a magpie a LaunchAgent/systemd unit/scheduled task starts again is said under the button with its path and pid, in en/zh/ja/de. TestGatewayFixQuitsOnlyAMagpie (copies of the test binary on a port of its own: one named other-server answering as magpie is left running, one named magpie is quit and the gateway is served here, then restarted) and gateway-takeover.test.cjs (WebKit and Chromium, en and zh, no scroll, no left border) fail without the change; TestListeningOn/TestIsMagpie pass on macOS and in a Linux container. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 15:05:31 +08:00
gui, proc: the Gateway card quits an older magpie serving the gateway and takes it over, or restarts the gateway (leslie_luo on Discord) — the card said an older magpie served the gateway and to quit it, with no way to tell which process that was. With an older magpie on the port it now has "Quit magpie {v} and take over": the process listening on the gateway's port is found (lsof on a Mac, /proc on Linux, Get-NetTCPConnection on Windows), its program path checked to be magpie's (magpie, magpie-*, dial, .exe/.old), asked to quit (SIGTERM, then SIGKILL after 5 s; TerminateProcess on Windows) and this magpie serves the gateway at once instead of within 15 s. Otherwise the card has "Restart gateway" ("Start gateway" when none runs); a magpie of this version or newer gets no button. Anything else on the port is never ended: a program that isn't magpie, one of another user or an administrator, one that won't quit, or a magpie a LaunchAgent/systemd unit/scheduled task starts again is said under the button with its path and pid, in en/zh/ja/de. TestGatewayFixQuitsOnlyAMagpie (copies of the test binary on a port of its own: one named other-server answering as magpie is left running, one named magpie is quit and the gateway is served here, then restarted) and gateway-takeover.test.cjs (WebKit and Chromium, en and zh, no scroll, no left border) fail without the change; TestListeningOn/TestIsMagpie pass on macOS and in a Linux container. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 15:05:31 +08:00