/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 since7e33a298b, 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/main5b267fbd8): 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.
29 lines
754 B
Go
29 lines
754 B
Go
//go:build !darwin
|
|
|
|
package agent
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// listRunning lists the processes a pgrep -f pattern matches, with how
|
|
// long each has run and its command line, from ps.
|
|
func listRunning(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
|
|
}
|