/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.
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
package agent
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestRunningListedOnceInAWhile: /api/state asked pgrep again on every
|
|
// call; a pattern asked again within runningFor is not listed again, an
|
|
// age read later has grown, a restart's own look replaces what was kept,
|
|
// and a test's fake is asked each time.
|
|
func TestRunningListedOnceInAWhile(t *testing.T) {
|
|
pat := `no-such-process-[0-9]{9}-lately`
|
|
runs := listRuns.Load()
|
|
for range 6 {
|
|
if ps := runningLately(pat); len(ps) != 0 {
|
|
t.Fatalf("found %+v", ps)
|
|
}
|
|
}
|
|
if n := listRuns.Load() - runs; n != 1 {
|
|
t.Fatalf("listed %d times in a row, want 1", n)
|
|
}
|
|
|
|
lately.put(pat, time.Now(), []runningProc{{10 * time.Second, "codex app-server"}})
|
|
ps := runningLately(pat)
|
|
if len(ps) != 1 || ps[0].cmd != "codex app-server" || ps[0].up < 10*time.Second || ps[0].up > 12*time.Second {
|
|
t.Fatalf("kept copy read back as %+v", ps)
|
|
}
|
|
|
|
// a restart waiting for the app to end looks now, and finds it gone
|
|
runs = listRuns.Load()
|
|
listRunning(pat)
|
|
if ps := runningLately(pat); len(ps) != 0 || listRuns.Load() != runs {
|
|
t.Fatalf("after a fresh look: %+v, %d listings", ps, listRuns.Load()-runs)
|
|
}
|
|
|
|
lately.put(pat, time.Now().Add(-runningFor-time.Second), []runningProc{{time.Second, "codex"}})
|
|
if ps := runningLately(pat); len(ps) != 0 || listRuns.Load() != runs+1 {
|
|
t.Fatalf("an old listing was used: %+v, %d listings", ps, listRuns.Load()-runs)
|
|
}
|
|
|
|
defer func(r func(string) []runningProc) { running = r }(running)
|
|
asked := 0
|
|
running = func(string) []runningProc { asked++; return nil }
|
|
runningLately(pat)
|
|
runningLately(pat)
|
|
if asked != 2 {
|
|
t.Fatalf("a test's running asked %d times, want 2", asked)
|
|
}
|
|
}
|