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.
This commit is contained in:
yetone
2026-11-11 12:02:52 +08:00
parent 5399c33728
commit 245f07b19e
7 changed files with 163 additions and 24 deletions
+78
View File
@@ -0,0 +1,78 @@
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
}
+60
View File
@@ -0,0 +1,60 @@
package agent
import (
"slices"
"testing"
"github.com/yetone/magpie/internal/agentplug"
)
// The pages that only name and draw rows by agent poll Looks, which builds
// every agent once for a while, not on each ask; a plugin's agent coming
// or going, and the Agents page's refresh, build it again at once.
func TestLooksBuiltOnceAWhile(t *testing.T) {
pluggedHome(t)
old := looksFresh
looksFresh = looksFreshInApp
t.Cleanup(func() { looksFresh = old; forgetLooks() })
forgetLooks()
has := func(id string) bool {
return slices.ContainsFunc(Looks(), func(l Look) bool { return l.ID == id })
}
before := looksBuilt()
first := Looks()
for range 5 {
Looks()
}
if n := looksBuilt() - before; n != 1 {
t.Fatalf("six asks built the agents %d times; want once", n)
}
cs := Clients()
if len(first) != len(cs) {
t.Fatalf("%d looks, %d clients", len(first), len(cs))
}
for i, a := range cs {
if l := first[i]; l != (Look{a.ID, a.Name, a.SharedName(), a.Icon}) {
t.Fatalf("look %d = %+v, client %s %q %q %q", i, l, a.ID, a.Name, a.SharedName(), a.Icon)
}
}
if has("aider") {
t.Fatal("aider looked before its plugin is on")
}
pluggedHome(t, aiderSrc)
if !has("aider") {
t.Fatal("a plugin's agent switched on isn't looked until the memo runs out")
}
pluggedHome(t)
agentplug.Reload()
if has("aider") {
t.Fatal("a plugin's agent switched off is still looked")
}
before = looksBuilt()
Rescan()
Looks()
if looksBuilt() == before {
t.Fatal("the Agents page's refresh didn't build the looks again")
}
}
+1
View File
@@ -15,6 +15,7 @@ import (
// server), and where each command was found on PATH (proc.LookPath).
func Rescan() {
proc.Forget()
forgetLooks() // a profile made since shows by its name
// where updates come from is asked again, too: a version published
// after magpie started showed no update until the memo ran out (#4980)
+11 -11
View File
@@ -126,12 +126,12 @@ func sessionRoutes(mux *http.ServeMux, w Windows) {
if n <= 0 {
n = sessions.All
}
agents := map[string]*agent.Agent{}
agents := map[string]agent.Look{}
// what a row is named and drawn as, beside the list, for listTag
var drawn strings.Builder
for _, a := range agent.Clients() {
for _, a := range agent.Looks() {
agents[a.ID] = a
drawn.WriteString(a.ID + "\x00" + a.SharedName() + "\x00" + a.Icon + "\x00")
drawn.WriteString(a.ID + "\x00" + a.Shared + "\x00" + a.Icon + "\x00")
}
out := sessionsJSON{Sessions: []sessionJSON{}, Terminal: terminalHere() && !isWeb(w),
Dirs: []string{}}
@@ -199,8 +199,8 @@ func sessionRoutes(mux *http.ServeMux, w Windows) {
// drawn as the agent it ran in: Claude Desktop's Code tab's
// sessions are in Claude Code's folder (Session.App)
j.Name, j.Icon, j.Via = s.Agent, "generic", viaOf(vias, s)
if a := agents[cmp.Or(s.App, s.Agent)]; a != nil {
j.Name, j.Icon = a.SharedName(), a.Icon
if a, ok := agents[cmp.Or(s.App, s.Agent)]; ok {
j.Name, j.Icon = a.Shared, a.Icon
}
j.Name = wslName(j.Name, s)
j.Path = tilde(j.Path)
@@ -228,8 +228,8 @@ func sessionRoutes(mux *http.ServeMux, w Windows) {
n, _ := strconv.Atoi(r.URL.Query().Get("days"))
st, gen := statsForGen(n)
names := map[string]string{}
for _, a := range agent.Clients() {
names[a.ID] = a.SharedName()
for _, a := range agent.Looks() {
names[a.ID] = a.Shared
}
agents := map[string]string{}
for _, d := range st.Days {
@@ -298,9 +298,9 @@ func sessionRoutes(mux *http.ServeMux, w Windows) {
addCodexModelEffort(&s, usage.CodexModelEffort(usage.All))
}
j := sessionJSON{Session: s, Name: s.Agent, Icon: "generic"}
for _, a := range agent.Clients() {
for _, a := range agent.Looks() {
if a.ID == cmp.Or(s.App, s.Agent) {
j.Name, j.Icon = a.SharedName(), a.Icon
j.Name, j.Icon = a.Shared, a.Icon
}
}
j.Name = wslName(j.Name, s)
@@ -499,9 +499,9 @@ func sessionMarkdown(agentID, id string) (stem string, md []byte, err error) {
s = gatewaySession(g)
}
name := s.Agent
for _, a := range agent.Clients() {
for _, a := range agent.Looks() {
if a.ID == s.Agent {
name = a.SharedName()
name = a.Shared
}
}
var b bytes.Buffer
+10 -10
View File
@@ -71,20 +71,20 @@ type trashedJSON struct {
Icon string `json:"icon"`
}
func agentLooks() map[string]*agent.Agent {
out := map[string]*agent.Agent{}
for _, a := range agent.Clients() {
func agentLooks() map[string]agent.Look {
out := map[string]agent.Look{}
for _, a := range agent.Looks() {
out[a.ID] = a
}
return out
}
func trashJSON(looks map[string]*agent.Agent) []trashedJSON {
func trashJSON(looks map[string]agent.Look) []trashedJSON {
out := []trashedJSON{}
for _, t := range sessions.Trash() {
j := trashedJSON{Trashed: t, Name: t.Agent, Icon: "generic"}
if a := looks[t.Agent]; a != nil {
j.Name, j.Icon = a.SharedName(), a.Icon
if a, ok := looks[t.Agent]; ok {
j.Name, j.Icon = a.Shared, a.Icon
}
j.Cwd = tilde(j.Cwd)
for i := range j.Items {
@@ -178,8 +178,8 @@ func sessionManageRoutes(mux *http.ServeMux, w Windows) {
counts := map[string]*manageAgentJSON{}
for _, a := range agents {
j := manageAgentJSON{AgentCount: a, Name: a.Agent, Icon: "generic"}
if l := looks[a.Agent]; l != nil {
j.Name, j.Icon = l.SharedName(), l.Icon
if l, ok := looks[a.Agent]; ok {
j.Name, j.Icon = l.Shared, l.Icon
}
counts[a.Agent] = &j
}
@@ -190,8 +190,8 @@ func sessionManageRoutes(mux *http.ServeMux, w Windows) {
j := counts[s.Agent]
if j == nil {
j = &manageAgentJSON{AgentCount: sessions.AgentCount{Agent: s.Agent}, Name: s.Agent, Icon: "generic"}
if l := looks[s.Agent]; l != nil {
j.Name, j.Icon = l.SharedName(), l.Icon
if l, ok := looks[s.Agent]; ok {
j.Name, j.Icon = l.Shared, l.Icon
}
counts[s.Agent] = j
}
+2 -2
View File
@@ -55,8 +55,8 @@ func readsNow() readsJSON {
looks := agentLooks()
for _, a := range agents {
j := readAgentJSON{Agent: a, Name: a, Icon: "generic", On: r.Reads(a)}
if l := looks[a]; l != nil {
j.Name, j.Icon = l.SharedName(), l.Icon
if l, ok := looks[a]; ok {
j.Name, j.Icon = l.Shared, l.Icon
}
out.Found = append(out.Found, j)
}
+1 -1
View File
@@ -42,7 +42,7 @@ var (
func init() {
usageapi.Clients = func() []usageapi.Client {
var out []usageapi.Client
for _, a := range agent.Clients() {
for _, a := range agent.Looks() {
out = append(out, usageapi.Client{ID: a.ID, Name: a.Name, Icon: a.Icon})
}
return out