diff --git a/internal/agent/looks.go b/internal/agent/looks.go new file mode 100644 index 000000000..a57960b8d --- /dev/null +++ b/internal/agent/looks.go @@ -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 +} diff --git a/internal/agent/looks_test.go b/internal/agent/looks_test.go new file mode 100644 index 000000000..891fe22fb --- /dev/null +++ b/internal/agent/looks_test.go @@ -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") + } +} diff --git a/internal/agent/rescan.go b/internal/agent/rescan.go index f5ee76c17..a0262afbb 100644 --- a/internal/agent/rescan.go +++ b/internal/agent/rescan.go @@ -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) diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index da65e4583..fa6dd4579 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -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 diff --git a/internal/gui/sessions_manage.go b/internal/gui/sessions_manage.go index f7894ca27..44a87206d 100644 --- a/internal/gui/sessions_manage.go +++ b/internal/gui/sessions_manage.go @@ -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 } diff --git a/internal/gui/sessions_reads.go b/internal/gui/sessions_reads.go index 04f514822..9a5ad0805 100644 --- a/internal/gui/sessions_reads.go +++ b/internal/gui/sessions_reads.go @@ -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) } diff --git a/internal/gui/usage.go b/internal/gui/usage.go index 6e84a5741..83fb89c8c 100644 --- a/internal/gui/usage.go +++ b/internal/gui/usage.go @@ -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