After the Sessions caches dropped (five idle minutes), the next
/api/sessions/stats on `magpie web` took 2.3–4.1s on the real account
on mote while its handler ran ~220ms of CPU. Two goroutine dumps taken
1.2s and 1.8s into the ask both sat in ownSessionsOff → agentsHere →
agent.Detected → HasApp → desktopApp.version → spotlightBundles: one
mdfind per bundle id (13 of them, ~50ms each idle, far more under
load), serial under one lock, asked again whenever the 5-minute answer
expired. Every other agent.Detected caller (Providers, quit ask, gateway
pause, Sessions) paid the same.
- spotlightBundles asks one `mdfind -attr kMDItemCFBundleIdentifier`
for every id desktopApps names, and keeps the answer by id. Past
spotlightFresh a caller gets the kept answer at once while one
goroutine asks again; only the first ask after start waits, once for
every caller. A failed mdfind keeps the answer before it.
- agentsHere stops at the first detected agent instead of checking all.
Measured with the real account's data on mote, the Sessions stats ask
at 370s, after the drops (`magpie web`, sandbox HOME):
main (w27) 2.3–4.1s in three runs earlier today; 1.34s in this run,
while mote's load fell from ~180 to ~10
this (w28) 0.77s at load ~170, then 0.046s
The rest of the 0.77s is the stats themselves being read again.
Tests: TestSpotlightParse; TestSpotlightOneAskStaleNeverWaits (10 reads
are one ask; a stale read returns while the refresh is held; two stale
reads are one refresh). On mica: go vet ./internal/agent ./internal/gui,
go test -count=1 -timeout 45m ./internal/agent ./internal/gui ok
(280s, 186s).
Not tried: Spotlight off on the Mac (mdfind fails; the answer is none,
as before); Intel Mac.
94 lines
2.9 KiB
Go
94 lines
2.9 KiB
Go
package agent
|
|
|
|
import (
|
|
"slices"
|
|
satomic "sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSpotlightParse(t *testing.T) {
|
|
out := "/Applications/ChatGPT.app kMDItemCFBundleIdentifier = com.openai.codex\n" +
|
|
"/Users/a/.Trash/ChatGPT.app kMDItemCFBundleIdentifier = com.openai.codex\n" +
|
|
"/Applications/My Apps/Qoder.app kMDItemCFBundleIdentifier = com.qoder.ide\n" +
|
|
"/Applications/Other.app kMDItemCFBundleIdentifier = com.other\n"
|
|
m := spotlightParse(out, []string{"com.openai.codex", "com.qoder.ide", "dev.none"})
|
|
if got := m["com.openai.codex"]; !slices.Equal(got, []string{"/Applications/ChatGPT.app"}) {
|
|
t.Errorf("codex = %q", got)
|
|
}
|
|
if got := m["com.qoder.ide"]; !slices.Equal(got, []string{"/Applications/My Apps/Qoder.app"}) {
|
|
t.Errorf("qoder = %q", got)
|
|
}
|
|
if _, ok := m["dev.none"]; !ok || m["dev.none"] != nil {
|
|
t.Errorf("an id Spotlight has none of is kept as none: %v", m)
|
|
}
|
|
if _, ok := m["com.other"]; ok {
|
|
t.Errorf("an id not asked for is kept: %v", m)
|
|
}
|
|
}
|
|
|
|
// Every known bundle id is asked in one mdfind, and once the answer is
|
|
// stale a caller gets it at once while one ask runs again behind it: a
|
|
// Sessions read after five idle minutes waited 2.3s on 13 mdfinds.
|
|
func TestSpotlightOneAskStaleNeverWaits(t *testing.T) {
|
|
was := spotlightAsk
|
|
spotlight.Lock()
|
|
spotlight.m, spotlight.at, spotlight.asking, spotlight.settled = nil, time.Time{}, false, nil
|
|
spotlight.Unlock()
|
|
t.Cleanup(func() {
|
|
spotlightAsk = was
|
|
spotlight.Lock()
|
|
spotlight.m, spotlight.at, spotlight.asking, spotlight.settled = nil, time.Time{}, false, nil
|
|
spotlight.Unlock()
|
|
})
|
|
var asks satomic.Int32
|
|
release := make(chan struct{})
|
|
spotlightAsk = func(ids []string) map[string][]string {
|
|
if !slices.Contains(ids, "com.openai.codex") || len(ids) < 2 {
|
|
t.Errorf("asked for %q, want every known id", ids)
|
|
}
|
|
if asks.Add(1) > 1 {
|
|
<-release
|
|
}
|
|
return map[string][]string{"com.openai.codex": {"/Applications/ChatGPT.app"}}
|
|
}
|
|
for range 5 {
|
|
spotlightKept("com.openai.codex")
|
|
spotlightKept("com.qoder.ide")
|
|
}
|
|
if n := asks.Load(); n != 1 {
|
|
t.Fatalf("%d asks for 10 reads, want 1", n)
|
|
}
|
|
spotlight.Lock()
|
|
spotlight.at = time.Now().Add(-spotlightFresh)
|
|
spotlight.Unlock()
|
|
done := make(chan []string)
|
|
go func() { done <- spotlightKept("com.openai.codex") }()
|
|
select {
|
|
case got := <-done:
|
|
if !slices.Equal(got, []string{"/Applications/ChatGPT.app"}) {
|
|
t.Errorf("stale answer = %q", got)
|
|
}
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("a read of a stale answer waited for Spotlight")
|
|
}
|
|
spotlightKept("com.openai.codex")
|
|
close(release)
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for {
|
|
spotlight.Lock()
|
|
asking := spotlight.asking
|
|
spotlight.Unlock()
|
|
if !asking {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("the refresh never settled")
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
if n := asks.Load(); n != 2 {
|
|
t.Errorf("%d asks, want 2: one refresh for any number of stale reads", n)
|
|
}
|
|
}
|