Files
magpie/internal/agent/running_darwin.go
T
yetone 881772d7de agent: copies left on the old list are found without ps on macOS, so /api/state no longer waits seconds on it (#1962, #2341)
/api/state asks StaleCopies of Claude Code and Codex on every call, and
StaleCopies ran pgrep -f, then `ps -o etime=,command= -p <pids>` over
what it matched. On a Mac with ~1300 processes and a busy CPU, ps over
the 25 claude processes the owner keeps open took 0.8-2.2s (pgrep
0.06s): measured with the commands alone, and caught in a goroutine
dump of a sandbox magpie, whose /api/state goroutine sat in
os/exec.(*Cmd).Wait under StaleCopies (connectinfo.go:156). The GUI
fetches /api/state after every save (#1962: open DeepSeek, change
nothing, save: the save itself answers in ms since 7e33a298b, the
"Saving…" wait was this) and on every page switch and focus.

On macOS listRunning now asks pgrep -lf for each match's pid and
command line, and the kernel (sysctl kern.proc.pid) for its start; ps
isn't run. Other systems keep pgrep + ps (running_other.go, the old
code unchanged); Windows lists no processes for StaleCopies at all.

Measured on a sandbox magpie (sandbox HOME, 18 agents connected,
DeepSeek provider), GET /api/state x25, machine load 236-297:
  before (origin/main 5b267fbd8): median 3.373s, 3.013s (two runs)
  after:                          median 0.343s, 0.197s
Unchanged DeepSeek save + the GUI's following /api/state:
  before: save 0.007-0.017s, state 1.9-2.6s
  after:  save 0.007-0.016s, state 0.25-0.34s

Run:
- TestRunningNeedsNoPs (a ps on PATH that only fails; a real sleep
  child found with its command line and age). On the old ps path
  (running_other.go built on darwin by a temporary tag):
    running_darwin_test.go:38: found 0 copies of sleep 314.159, want 1: []
    --- FAIL: TestRunningNeedsNoPs (7.46s)
  With the fix: PASS.
- macOS, sandbox HOME: go test -count=1 -timeout 45m ./internal/agent/
  ./internal/gui/ -> ok gui 472.390s; agent hit the 45m timeout at
  load ~250 while TestDisconnectStaysThroughSync had run 10s (no hang:
  the slowest tests are Claude tier tests, 234s and 143s, which never
  reach running). Rerun with -v -timeout 90m: ok agent 3506.886s,
  TestRunningNeedsNoPs 0.11s and every Stale test PASS.
- gofmt -l clean; go vet ./internal/agent; GOOS=linux and GOOS=windows
  go vet -tags nogui ./... clean.

Not changed: a save that does change the provider still runs
SyncCatalog before it answers (2-6s with 18 agents under this load);
moving it behind the response changes what the response's moved list
and many tests rely on, and is left for a decision.
2026-10-20 20:23:51 +08:00

41 lines
1.2 KiB
Go

package agent
import (
"strconv"
"strings"
"time"
"golang.org/x/sys/unix"
"github.com/yetone/magpie/internal/proc"
)
// listRunning 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 listRunning(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
}