sessions: a listing's gateway titles ask once whether anything was recorded, not once a session (#2278)

Each poll of the Sessions list and of every stats range titled each of
~5000 gateway sessions with GatewayTitle, which made the recordings
folder's name and Stat-ed it each time. With nothing recorded, as for
most users, that was the whole cost: os.Stat 62 MB and gatewayDir 18 MB
of 90 s of polls while an agent worked.

GatewayTitler makes the folder's name and asks once, and gives "" with
no work when nothing is recorded; gatewayListed takes it. GatewayTitle
is GatewayTitler()(agent, id), for one session.

Bench (mote, ~/r2278/full, hmw.sh second runs), e14 = bed604efb, e15 =
this: GatewayTitle 81 MB -> gone, gatewayListed 100 -> 19 MB, polls 170
x 200 in both; total 2308 -> 2350 MB is the run-to-run band.

TestGatewayTitlerAsksOnce: with nothing recorded a title makes 0
allocations; after a turn is recorded a titler made then finds what
GatewayTitle finds. Broken by hand (Stat and the folder's name again per
session): red.

At 825db07aa on bed604efb (mote): gofmt clean; go vet -tags nogui
./internal/gui/ ./internal/sessions/ for linux, windows, darwin ok;
go test ./internal/sessions/ ok; go test ./internal/gui/ -run
'Gateway|Sessions|Stats' ok.
This commit is contained in:
yetone
2026-10-30 19:12:35 +08:00
parent bed604efb3
commit 1334934881
4 changed files with 64 additions and 12 deletions
+2 -1
View File
@@ -3,6 +3,7 @@ package gui
import (
"testing"
"github.com/yetone/magpie/internal/sessions"
"github.com/yetone/magpie/internal/usage"
)
@@ -15,7 +16,7 @@ func TestGatewayListCopiesNoDays(t *testing.T) {
for range 365 {
g.Daily = append(g.Daily, usage.GatewayDayUsage{Date: "2026-10-19", Agent: "pi", Model: "m"})
}
if e := gatewayListed(g); len(e.Daily) != 0 || len(e.Models) != 1 {
if e := gatewayListed(g, sessions.GatewayTitle); len(e.Daily) != 0 || len(e.Models) != 1 {
t.Fatalf("a listed session copied %d days and %d models, want 0 and 1", len(e.Daily), len(e.Models))
}
if d := gatewayDays(g); len(d) != len(g.Daily) {
+8 -6
View File
@@ -69,15 +69,15 @@ func gatewayDays(g usage.GatewaySession) []sessions.ExternalDayUsage {
// gatewayListed is a gateway session as a list shows it: titled by what it
// was first asked, when magpie recorded it, not by its ses_… id (YOU on
// Discord)
func gatewayListed(g usage.GatewaySession) sessions.ExternalSession {
// Discord). title is a sessions.GatewayTitler's.
func gatewayListed(g usage.GatewaySession, title func(agent, id string) string) sessions.ExternalSession {
e := gatewayExternal(g)
e.Title = sessions.GatewayTitle(g.Agent, g.ID)
e.Title = title(g.Agent, g.ID)
return e
}
func gatewaySession(g usage.GatewaySession) sessions.Session {
return sessions.MergeExternal(nil, []sessions.ExternalSession{gatewayListed(g)})[0]
return sessions.MergeExternal(nil, []sessions.ExternalSession{gatewayListed(g, sessions.GatewayTitle)})[0]
}
func sessionRoutes(mux *http.ServeMux, w Windows) {
@@ -115,8 +115,9 @@ func sessionRoutes(mux *http.ServeMux, w Windows) {
}
gateway := listedGateway(usage.GatewaySessions(time.Time{}, nativeKeys))
ext := make([]sessions.ExternalSession, 0, len(gateway))
title := sessions.GatewayTitler()
for _, g := range gateway {
ext = append(ext, gatewayListed(g))
ext = append(ext, gatewayListed(g, title))
}
list = sessions.MergeExternal(list, ext)
if len(list) > n {
@@ -604,8 +605,9 @@ func withGatewaySessions(st sessions.Stats, days int, nativeKeys map[string]bool
}
gs, gd := usage.GatewaySessionWindowExcept(since, nativeKeys)
ext := make([]sessions.ExternalSession, 0, len(gs))
title := sessions.GatewayTitler()
for _, g := range gs {
e := gatewayListed(g)
e := gatewayListed(g, title)
e.Daily = gatewayDays(g)
ext = append(ext, e)
}
+37
View File
@@ -0,0 +1,37 @@
package sessions
import (
"os"
"testing"
"time"
)
// A listing's titler asks once whether magpie recorded anything: with
// nothing recorded, each of its many sessions costs nothing, not a Stat and
// a folder name (#2278). Once something is recorded, a titler made after
// finds the title as GatewayTitle does.
func TestGatewayTitlerAsksOnce(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if _, err := os.Stat(gatewayDir()); err == nil {
t.Fatal("a fresh config folder has recordings")
}
title := GatewayTitler()
if n := testing.AllocsPerRun(100, func() { _ = title("pi", "ses_1") }); n != 0 {
t.Fatalf("a session's title with nothing recorded made %v allocations", n)
}
if err := SetGatewayRecording(true, false); err != nil {
t.Fatal(err)
}
turn := GatewayTurn{Agent: "pi", Session: "ses_1", Time: time.Now().UTC(),
Input: []Part{{Role: "user", Kind: "text", Text: "Deploy the staging server"}}}
if err := SaveGatewayTurn(turn); err != nil {
t.Fatal(err)
}
title = GatewayTitler()
if got := title("pi", "ses_1"); got != "Deploy the staging server" || got != GatewayTitle("pi", "ses_1") {
t.Fatalf("titler's title %q, GatewayTitle's %q", got, GatewayTitle("pi", "ses_1"))
}
if got := title("pi", "ses_2"); got != "" {
t.Fatalf("another session's title %q", got)
}
}
+17 -5
View File
@@ -304,18 +304,30 @@ const gatewayTitleLines = 8
// GatewayTitle is what a gateway session was first asked, made one short
// line, from the first turns magpie recorded of it; "" when it recorded
// none or nothing the user wrote.
func GatewayTitle(agent, id string) string {
func GatewayTitle(agent, id string) string { return GatewayTitler()(agent, id) }
// GatewayTitler is GatewayTitle for a listing's many sessions: whether
// magpie recorded anything at all is asked once, not once a session. A
// Stat and the folder's name for each of ~5000 gateway sessions on each
// poll were ~80 MB of 90 s of polls (#2278).
func GatewayTitler() func(agent, id string) string {
root := gatewayDir()
if _, err := os.Stat(root); err != nil {
// nothing recorded at all, as for most
return func(string, string) string { return "" }
}
return func(agent, id string) string { return gatewayTitleIn(root, agent, id) }
}
func gatewayTitleIn(root, agent, id string) string {
if agent == "" || id == "" {
return ""
}
if _, err := os.Stat(gatewayDir()); err != nil {
return "" // nothing recorded at all, as for most
}
gatewayMu.Lock()
defer gatewayMu.Unlock()
now := time.Now().UTC()
for d := 7; d >= 0; d-- {
dir := filepath.Join(gatewayDir(), now.AddDate(0, 0, -d).Format(time.DateOnly), gatewayIdentity(agent, id))
dir := filepath.Join(root, now.AddDate(0, 0, -d).Format(time.DateOnly), gatewayIdentity(agent, id))
entries, err := os.ReadDir(dir)
if err != nil {
continue