/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.
41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
package agent
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// listRunningNow lists the processes a pgrep -f pattern matches, each with
|
|
// its command line from pgrep -lf and its start from the kernel. ps isn't
|
|
// run: on a Mac with a thousand processes and a busy CPU, ps -p over the
|
|
// 25 copies of claude a user keeps open took 1-2s, on every /api/state
|
|
// (#1962, #2341), where pgrep took 0.06s and a sysctl takes microseconds.
|
|
func listRunningNow(pat string) []runningProc {
|
|
out, _ := proc.Command("pgrep", "-lf", pat).Output()
|
|
var ps []runningProc
|
|
now := time.Now()
|
|
for _, l := range strings.Split(strings.TrimSpace(string(out)), "\n") {
|
|
id, cmd, ok := strings.Cut(strings.TrimSpace(l), " ")
|
|
pid, err := strconv.Atoi(id)
|
|
if !ok || err != nil {
|
|
continue
|
|
}
|
|
kp, err := unix.SysctlKinfoProc("kern.proc.pid", pid)
|
|
if err != nil || kp.Proc.P_pid != int32(pid) {
|
|
// gone since pgrep listed it
|
|
continue
|
|
}
|
|
start := time.Unix(kp.Proc.P_starttime.Unix())
|
|
if start.IsZero() || start.After(now) {
|
|
continue
|
|
}
|
|
ps = append(ps, runningProc{now.Sub(start), strings.TrimSpace(cmd)})
|
|
}
|
|
return ps
|
|
}
|