Files
magpie/internal/agent/looks.go
T
yetone 245f07b19e agent: the pages that poll name rows from a list built once a while
The Sessions page polls its list and its stats every 15s, the Usage
page its clients, and each asked agent.Clients() only for each agent's
id, name and icon. Clients() builds every agent (30+ constructors):
2.5 ms, 378 KB and 3411 allocations a call on the Windows box with
discovery's real 2s look window, mostly os.Stat, filepath.Join and
Getenv. On the real-account run (t69, 4 min) agent.All under the
sessions handler was 22 MB of 210 MB allocated.

agent.Looks keeps that id/name/shared-name/icon list for 30s, built
again at once when the plugins' agents change (agentplug's set) or the
Agents page's refresh (Rescan). A new omp or QwenPaw profile shows by
its id for up to 30s. Nothing is kept in a test binary unless a test
says, so tests that add an agent read it at once. The GUI's sessions
list, stats, one session, markdown export, trash, manage and reads
views, and usageapi.Clients, read Looks.

Tests: TestLooksBuiltOnceAWhile — six asks build once, the list equals
Clients(), a plugin agent switched on and off shows at once, Rescan
builds again. Broken by hand (memo check made false): "six asks built
the agents 6 times; want once".

Run on the Windows box: go test ./internal/agent ./internal/gui
./internal/usageapi at the pre-rebase base 0cc91af3d: usageapi ok, agent
and gui with 38 reds. Those tests, run at 8aae124b0 with and without this
change: the same 37 fail on both (#6068). TestLooks/TestPlugged/TestRescan
ok and go vet ok at 8aae124b0. mote (macOS) refused ssh all session, so no
macOS run and no real-account measurement yet.

Rebased on 156861b3a after the runs; its new commits touch library,
plugin and site only.
2026-11-11 12:02:52 +08:00

79 lines
2.0 KiB
Go

package agent
import (
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/agentplug"
)
// Look is how an agent of Clients() is named and drawn.
type Look struct {
ID, Name, Shared, Icon string
}
// looksFresh is how long Looks keeps what it built. Only a new omp or
// QwenPaw profile changes the list without a plugin changing; its rows
// show the agent's id until then, or until the Agents page's refresh
// (Rescan). Nothing is kept in a test binary, where a test that adds an
// agent reads it at once, unless the test says.
var looksFresh = looksFreshInApp
const looksFreshInApp = 30 * time.Second
func init() {
if testing.Testing() {
looksFresh = 0
}
}
var looksKept struct {
sync.Mutex
at time.Time
plugged []*agentplug.Agent
list []Look
built int // times the list was built, for the tests
}
// Looks is each agent of Clients() as it is named and drawn, in that
// order, for pages that only name and draw rows by agent: the Sessions
// and Usage pages ask on every poll, and Clients() builds every agent,
// ~2.5 ms and ~380 KB of stats, paths and environment each time. It is
// built again after looksFresh, or at once when the plugins' agents
// change. What it returns is shared: callers only read it.
func Looks() []Look {
plugged, _ := agentplug.List()
k := &looksKept
k.Lock()
defer k.Unlock()
if k.list != nil && time.Since(k.at) < looksFresh && samePlugged(k.plugged, plugged) {
return k.list
}
cs := Clients()
k.built++
list := make([]Look, len(cs))
for i, a := range cs {
list[i] = Look{a.ID, a.Name, a.SharedName(), a.Icon}
}
k.at, k.plugged, k.list = time.Now(), plugged, list
return list
}
func samePlugged(a, b []*agentplug.Agent) bool {
return len(a) == len(b) && (len(a) == 0 || &a[0] == &b[0])
}
// forgetLooks has the next Looks build again.
func forgetLooks() {
looksKept.Lock()
looksKept.list = nil
looksKept.Unlock()
}
func looksBuilt() int {
looksKept.Lock()
defer looksKept.Unlock()
return looksKept.built
}