Files
magpie/internal/agent/running_darwin_test.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

49 lines
1.4 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/proc"
)
// /api/state asks StaleCopies for Claude and Codex on every call, and ps
// -p over a user's 25 copies of claude took 1-2s on a busy Mac, so every
// page switch and every save waited on it (#1962, #2341). listRunning
// finds a copy, its command line and its age without ps: a ps that only
// fails here is not run.
func TestRunningNeedsNoPs(t *testing.T) {
bin := t.TempDir()
os.WriteFile(filepath.Join(bin, "ps"), []byte("#!/bin/sh\nexit 1\n"), 0o755)
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
cmd := proc.Command("sleep", "314.159")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { cmd.Process.Kill(); cmd.Wait() })
started := time.Now()
var got []runningProc
for range 50 {
if got = listRunning(`sleep 314\.159`); len(got) > 0 {
break
}
time.Sleep(20 * time.Millisecond)
}
if len(got) != 1 {
t.Fatalf("found %d copies of sleep 314.159, want 1: %+v", len(got), got)
}
p := got[0]
if !strings.Contains(p.cmd, "sleep 314.159") {
t.Errorf("command line %q, want sleep 314.159", p.cmd)
}
// the kernel's start has whole seconds here, and the test may be slow
if lim := time.Since(started) + 2*time.Second; p.up < 0 || p.up > lim {
t.Errorf("up %v, want between 0 and %v", p.up, lim)
}
}