Files
magpie/internal/agent/running_other.go
T
yetone da0005df81 agent: /api/state lists running agents once in 3s, not with a pgrep each call
/api/state asks StaleCopies for every wired agent, and each pattern
started `pgrep -lf`, which walks every process's arguments. On a Mac
with ~1000 processes that was most of a state call: 45ms each, on every
page switch, save and GUI poll, with a pgrep child seen in 25 of 40
samples.

StaleCopies now reads runningLately: a pattern's listing is kept for
3s, with each process's start (not its age), so an age read later is
still right. A fresh listing (a restart waiting for its app to end)
replaces what was kept, so the look that finds the app gone is what the
next poll sees. A test that fakes running is asked each time. The
waits in apprestart still list now.

Run (mote, this tree):
- go vet ./internal/agent ./internal/gui; GOOS=linux and GOOS=windows
  go vet ./internal/agent: ok.
- TestRunningListedOnceInAWhile, TestRunningNeedsNoPs: ok.
  Broken by hand (every call lists): red, "listed 6 times in a row,
  want 1".
- go test ./internal/agent ./internal/provider: ok. ./internal/gui:
  only TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners/macOS_26_SDK (#3949), both red on main too.

Real accounts (sandbox HOME from a copy of the live accounts, on this
Mac with its real Codex app, daemons and CLI running):
- /api/state, 10 calls after 200: v0.1.2519 43-60ms, this 8.4-21ms
  (twice each, alternating). pgrep children in 40 samples: 26 and 24
  before, 1 and 1 after.
- Each wired agent's stale copies (Codex: app, two daemons, cli, each
  with its start) are the same from both binaries, read twice 4s apart.

Not run: the full ./... suite; Linux and Windows runs of the agent
package (vet only).

Picked onto 9be72c948 (origin/main at push); not run again there.
2026-11-01 10:32:21 +08:00

29 lines
760 B
Go

//go:build !darwin
package agent
import (
"strings"
"github.com/yetone/magpie/internal/proc"
)
// listRunningNow lists the processes a pgrep -f pattern matches, with how
// long each has run and its command line, from ps.
func listRunningNow(pat string) []runningProc {
out, _ := proc.Command("pgrep", "-f", pat).Output()
pids := strings.Fields(string(out))
if len(pids) == 0 {
return nil
}
out, _ = proc.Command("ps", "-o", "etime=,command=", "-p", strings.Join(pids, ",")).Output()
var ps []runningProc
for _, l := range strings.Split(strings.TrimSpace(string(out)), "\n") {
et, cmd, _ := strings.Cut(strings.TrimSpace(l), " ")
if up, ok := elapsed(et); ok {
ps = append(ps, runningProc{up, strings.TrimSpace(cmd)})
}
}
return ps
}