/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.
77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package agent
|
|
|
|
import (
|
|
"reflect"
|
|
"sync"
|
|
syncatomic "sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// listRunning lists the processes a pattern matches as they are now, for
|
|
// a caller that waits on them (a restart waiting for the app to end).
|
|
// What it finds replaces what runningLately kept, so a restart's last
|
|
// look, the one that finds the app gone, is what the next poll sees.
|
|
func listRunning(pat string) []runningProc {
|
|
ps := listRunningNow(pat)
|
|
lately.put(pat, time.Now(), ps)
|
|
return ps
|
|
}
|
|
|
|
// runningLately is running(pat) as it was up to runningFor ago.
|
|
// /api/state asks StaleCopies for every wired agent, and each pattern
|
|
// started a pgrep that walks every process's arguments: most of a state
|
|
// call's 45ms (9ms without it), on every page switch, save and poll.
|
|
// Each process's start is kept, not its age, so an age read later is
|
|
// still right.
|
|
const runningFor = 3 * time.Second
|
|
|
|
// listRuns counts the listings runningLately made (a test reads it).
|
|
var listRuns syncatomic.Uint64
|
|
|
|
var lately runningKept
|
|
|
|
type runningKept struct {
|
|
sync.Mutex
|
|
by map[string]keptRun
|
|
}
|
|
|
|
type keptRun struct {
|
|
at time.Time
|
|
start []time.Time
|
|
cmd []string
|
|
}
|
|
|
|
func (k *runningKept) put(pat string, now time.Time, ps []runningProc) {
|
|
r := keptRun{at: now}
|
|
for _, p := range ps {
|
|
r.start = append(r.start, now.Add(-p.up))
|
|
r.cmd = append(r.cmd, p.cmd)
|
|
}
|
|
k.Lock()
|
|
if k.by == nil {
|
|
k.by = map[string]keptRun{}
|
|
}
|
|
k.by[pat] = r
|
|
k.Unlock()
|
|
}
|
|
|
|
func runningLately(pat string) []runningProc {
|
|
// a test's own running is asked each time
|
|
if reflect.ValueOf(running).Pointer() != reflect.ValueOf(listRunning).Pointer() {
|
|
return running(pat)
|
|
}
|
|
now := time.Now()
|
|
lately.Lock()
|
|
r, ok := lately.by[pat]
|
|
lately.Unlock()
|
|
if !ok || now.Sub(r.at) >= runningFor || now.Before(r.at) {
|
|
listRuns.Add(1)
|
|
return listRunning(pat)
|
|
}
|
|
ps := make([]runningProc, len(r.cmd))
|
|
for i := range r.cmd {
|
|
ps[i] = runningProc{now.Sub(r.start[i]), r.cmd[i]}
|
|
}
|
|
return ps
|
|
}
|