Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
agent: copies left on the old list are found without ps on macOS, so /api/state no longer waits seconds on it (#1962, #2341)
yetone881772d7deb9
diff --git a/internal/agent/connectinfo.go b/internal/agent/connectinfo.goindex 39157f78a..530936ae1 100644--- a/internal/agent/connectinfo.go+++ b/internal/agent/connectinfo.go@@ -13,7 +13,6 @@ import ("time""github.com/yetone/magpie/internal/edit"- "github.com/yetone/magpie/internal/proc""github.com/yetone/magpie/internal/provider""github.com/yetone/magpie/internal/settings")@@ -147,22 +146,7 @@ type runningProc struct {// running lists the processes a pgrep -f pattern matches. A var so tests// can say.-var running = func(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-}+var running = listRunning// changedAt is when magpie last changed what the agent reads at start. It// is kept in the stash with a digest of what that was, so neither thediff --git a/internal/agent/running_darwin.go b/internal/agent/running_darwin.gonew file mode 100644index 000000000..8824acad1--- /dev/null+++ b/internal/agent/running_darwin.go@@ -0,0 +1,40 @@+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+}diff --git a/internal/agent/running_darwin_test.go b/internal/agent/running_darwin_test.gonew file mode 100644index 000000000..0641b0689--- /dev/null+++ b/internal/agent/running_darwin_test.go@@ -0,0 +1,48 @@+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)+ }+}diff --git a/internal/agent/running_other.go b/internal/agent/running_other.gonew file mode 100644index 000000000..39aeb1d1f--- /dev/null+++ b/internal/agent/running_other.go@@ -0,0 +1,28 @@+//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+}