Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

usage: Vias hands every caller the one copy it keeps, and a session's list is copied only when read (#2776)

yetone6359b2c27cb7
diff --git a/internal/gateway/effort_test.go b/internal/gateway/effort_test.go
index 3799d112f..bf65d26a3 100644
--- a/internal/gateway/effort_test.go
+++ b/internal/gateway/effort_test.go
@@ -221,7 +221,7 @@ func TestTraceRecordsTheEffortSent(t *testing.T) {
if strings.Join(got, ",") != "high,xhigh,high,max," {
t.Errorf("usage efforts %q", got)
}
- vs := usage.Vias(time.Time{})[usage.AgentOf("")+"|sess-1"]
+ vs := usage.Vias(time.Time{}).Of(usage.AgentOf("") + "|sess-1")
if len(vs) != 4 || vs[0].Model != "glm-5.3-flash" || vs[0].Effort != "high" || vs[0].Calls != 2 {
t.Errorf("vias %+v", vs)
}
diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go
index 27d306bf6..7f8a16d98 100644
--- a/internal/gui/sessions.go
+++ b/internal/gui/sessions.go
@@ -120,7 +120,7 @@ func sessionRoutes(mux *http.ServeMux, w Windows) {
}
vias := usage.Vias(since.Add(-time.Minute))
for _, s := range list {
- j := sessionJSON{Session: s, Name: s.Agent, Icon: "generic", Via: vias[s.Agent+"|"+s.ID]}
+ j := sessionJSON{Session: s, Name: s.Agent, Icon: "generic", Via: vias.Of(s.Agent + "|" + s.ID)}
if a := agents[s.Agent]; a != nil {
j.Name, j.Icon = a.SharedName(), a.Icon
}
@@ -226,7 +226,7 @@ func sessionRoutes(mux *http.ServeMux, w Windows) {
if since.IsZero() {
since = s.Last
}
- j.Via = usage.Vias(since.Add(-time.Minute))[s.Agent+"|"+s.ID]
+ j.Via = usage.Vias(since.Add(-time.Minute)).Of(s.Agent + "|" + s.ID)
j.Path = tilde(j.Path)
writeJSON(rw, j)
})
diff --git a/internal/gui/sessions_manage.go b/internal/gui/sessions_manage.go
index f56017237..7ec415175 100644
--- a/internal/gui/sessions_manage.go
+++ b/internal/gui/sessions_manage.go
@@ -207,7 +207,7 @@ func sessionManageRoutes(mux *http.ServeMux, w Windows) {
vias := usage.Vias(since.Add(-time.Minute))
for _, s := range list {
s.Path = tilde(s.Path)
- out.Sessions = append(out.Sessions, managedJSON{Managed: s, Via: vias[s.Agent+"|"+s.ID]})
+ out.Sessions = append(out.Sessions, managedJSON{Managed: s, Via: vias.Of(s.Agent + "|" + s.ID)})
}
sizes := sessions.GatewaySizes()
for _, g := range gateway {
@@ -218,7 +218,7 @@ func sessionManageRoutes(mux *http.ServeMux, w Windows) {
addCodexModelEffort(&s, modelEffort)
// its size is the conversation text magpie recorded of it
m := sessions.Managed{Session: s, Deletable: true, Size: sizes(s.Agent, s.ID)}
- out.Sessions = append(out.Sessions, managedJSON{Managed: m, Via: vias[s.Agent+"|"+s.ID]})
+ out.Sessions = append(out.Sessions, managedJSON{Managed: m, Via: vias.Of(s.Agent + "|" + s.ID)})
}
sort.SliceStable(out.Sessions, func(i, j int) bool {
return out.Sessions[i].Last.After(out.Sessions[j].Last)
diff --git a/internal/usage/client_agents_test.go b/internal/usage/client_agents_test.go
index a8795a6e3..e13c2c34c 100644
--- a/internal/usage/client_agents_test.go
+++ b/internal/usage/client_agents_test.go
@@ -51,7 +51,7 @@ func TestClientCountedAsAgent(t *testing.T) {
if sum.Agents[0].Calls != 3 || sum.Agents[0].Input != 22 {
t.Fatalf("Codex's row doesn't hold the bridge's calls: %+v", sum.Agents[0])
}
- if got := Vias(time.Time{}); len(got["codex|thread-1"]) != 1 || len(got["hermes-codex-thread-bridge|thread-1"]) != 0 {
+ if got := Vias(time.Time{}); len(got.Of("codex|thread-1")) != 1 || len(got.Of("hermes-codex-thread-bridge|thread-1")) != 0 {
t.Fatalf("Routing's sessions: %v", got)
}
g, ok := GatewaySessionByID("codex", "thread-1", nil)
diff --git a/internal/usage/gateway_sessions_test.go b/internal/usage/gateway_sessions_test.go
index f28dca083..9affd4158 100644
--- a/internal/usage/gateway_sessions_test.go
+++ b/internal/usage/gateway_sessions_test.go
@@ -64,7 +64,7 @@ func TestClientRequestPrefixedSessionsCount(t *testing.T) {
if len(summary.Sessions) != 1 || summary.Sessions[0].ID != "request-mine" {
t.Fatalf("the client's session is missing from Usage: %+v", summary.Sessions)
}
- if got := Vias(time.Time{}); len(got["pi|request-mine"]) != 1 {
+ if got := Vias(time.Time{}); len(got.Of("pi|request-mine")) != 1 {
t.Fatalf("the client's session is missing from Routing: %+v", got)
}
if _, ok := GatewaySessionByID("pi", "request-mine", nil); !ok {
@@ -100,11 +100,11 @@ func TestGatewaySessionsCachePricesWindowsAndRewrite(t *testing.T) {
}
}
vias := Vias(time.Time{})
- vias["pi|native"][0].Calls = 999
- if got := Vias(time.Time{})["pi|native"][0].Calls; got != 2 {
+ vias.Of("pi|native")[0].Calls = 999
+ if got := Vias(time.Time{}).Of("pi|native")[0].Calls; got != 2 {
t.Fatalf("caller mutated routing cache: %d", got)
}
- if got := Vias(at.Add(-time.Minute))["pi|native"][0].Calls; got != 1 {
+ if got := Vias(at.Add(-time.Minute)).Of("pi|native")[0].Calls; got != 1 {
t.Fatalf("routing window: %d", got)
}
if err := os.WriteFile(Path(), nil, 0600); err != nil {
@@ -113,7 +113,7 @@ func TestGatewaySessionsCachePricesWindowsAndRewrite(t *testing.T) {
if got := GatewaySessions(time.Time{}, nil); len(got) != 0 {
t.Fatalf("rewrite retained sessions: %+v", got)
}
- if got := Vias(time.Time{}); len(got) != 0 {
+ if got := Vias(time.Time{}); got.Len() != 0 {
t.Fatalf("rewrite retained routing: %+v", got)
}
}
diff --git a/internal/usage/growing_rewrite_test.go b/internal/usage/growing_rewrite_test.go
index c4937d23d..73f891665 100644
--- a/internal/usage/growing_rewrite_test.go
+++ b/internal/usage/growing_rewrite_test.go
@@ -76,7 +76,7 @@ func TestLogGrowingRewriteThenAppend(t *testing.T) {
}
vias := Vias(time.Time{})
for session, input := range map[string]int{"edited-prefix": 73, "appended-tail": 19} {
- v := vias["codex|"+session]
+ v := vias.Of("codex|" + session)
if len(v) != 1 || v[0].Calls != 1 || v[0].Tokens != input {
t.Fatalf("%s vias stale: %+v", session, v)
}
diff --git a/internal/usage/incremental_log_test.go b/internal/usage/incremental_log_test.go
index 0b1d6a739..7e50a3192 100644
--- a/internal/usage/incremental_log_test.go
+++ b/internal/usage/incremental_log_test.go
@@ -75,7 +75,7 @@ func TestUsageViasSkipsOldHistory(t *testing.T) {
settleLogClock(t)
historyLog(t, 4000)
since := time.Now().Add(-time.Hour)
- if v := Vias(since); len(v) != 1 || v["codex|s"][0].Calls != 1 {
+ if v := Vias(since); v.Len() != 1 || v.Of("codex|s")[0].Calls != 1 {
t.Fatal(v)
}
if n := testing.AllocsPerRun(2, func() { Vias(since) }); n > 1500 {
diff --git a/internal/usage/kept_strings_test.go b/internal/usage/kept_strings_test.go
index 4d50fe3e4..2c8b24585 100644
--- a/internal/usage/kept_strings_test.go
+++ b/internal/usage/kept_strings_test.go
@@ -54,8 +54,8 @@ func TestSessionCachesHoldNoDecodedBlocks(t *testing.T) {
if got := GatewaySessions(time.Time{}, nil); len(got) != blocks {
t.Fatalf("sessions: %d, want %d", len(got), blocks)
}
- if got := Vias(time.Time{}); len(got) != blocks {
- t.Fatalf("vias: %d, want %d", len(got), blocks)
+ if got := Vias(time.Time{}); got.Len() != blocks {
+ t.Fatalf("vias: %d, want %d", got.Len(), blocks)
}
// the index is kept by design; only what the caches hold is measured
logIndex.Lock()
diff --git a/internal/usage/plan_credits_test.go b/internal/usage/plan_credits_test.go
index f61c30390..f069c0ef4 100644
--- a/internal/usage/plan_credits_test.go
+++ b/internal/usage/plan_credits_test.go
@@ -49,10 +49,10 @@ func TestPlanCreditsSummed(t *testing.T) {
}
}
vias := Vias(time.Time{})
- if v := vias["claude-code|s1"]; len(v) != 1 || !near(v[0].Credits, 0.7) || v[0].Credited != 70 {
+ if v := vias.Of("claude-code|s1"); len(v) != 1 || !near(v[0].Credits, 0.7) || v[0].Credited != 70 {
t.Fatalf("session s1: %+v", v)
}
- if v := vias["codex|s2"]; len(v) != 2 || v[0].Credited+v[1].Credited != 1 {
+ if v := vias.Of("codex|s2"); len(v) != 2 || v[0].Credited+v[1].Credited != 1 {
t.Fatalf("session s2: %+v", v)
}
if got := FormatCredits(page.Sum); got != "0.7 credits" {
diff --git a/internal/usage/reported_test.go b/internal/usage/reported_test.go
index 5a9fc5244..7fd1aac03 100644
--- a/internal/usage/reported_test.go
+++ b/internal/usage/reported_test.go
@@ -199,7 +199,7 @@ func TestReportedTouchesNothingLive(t *testing.T) {
t.Fatalf("a reported session is a gateway session: %+v", g)
}
}
- if v := Vias(time.Time{})["claude|s-rep"]; len(v) != 0 {
+ if v := Vias(time.Time{}).Of("claude|s-rep"); len(v) != 0 {
t.Fatalf("a reported session was routed: %+v", v)
}
if !(Record{Source: SourceReported}).IsReported() || (Record{}).IsReported() {
diff --git a/internal/usage/review_test.go b/internal/usage/review_test.go
index bcdfe2ed9..75c5991c4 100644
--- a/internal/usage/review_test.go
+++ b/internal/usage/review_test.go
@@ -158,12 +158,12 @@ func TestZeroTokenFailuresDeduplicateAndRejectedStayVisible(t *testing.T) {
t.Fatal("local rejection consumed a real API call")
}
Append(r)
- if len(Vias(time.Time{})) != 0 {
+ if Vias(time.Time{}).Len() != 0 {
t.Fatal("rejected request appears as a session provider")
}
Append(Record{Time: at, Agent: "claude", Provider: "relay", Model: "m", Session: "s", Status: 200})
Append(Record{Time: at, Agent: "claude", Model: "m", Session: "s", Status: 400}) // legacy rejection
- if vias := Vias(time.Time{}); len(vias) != 1 || len(vias["claude|s"]) != 1 || vias["claude|s"][0].Calls != 1 {
+ if vias := Vias(time.Time{}); vias.Len() != 1 || len(vias.Of("claude|s")) != 1 || vias.Of("claude|s")[0].Calls != 1 {
t.Fatalf("session via includes a local rejection: %+v", vias)
}
var out bytes.Buffer
diff --git a/internal/usage/usage.go b/internal/usage/usage.go
index 684267961..116d119d0 100644
--- a/internal/usage/usage.go
+++ b/internal/usage/usage.go
@@ -999,7 +999,7 @@ type Via struct {
// Vias is what the gateway sent each session's calls to since a time, by
// agent id and the session's id ("codex|<id>"), the most calls first.
-func Vias(since time.Time) map[string][]Via {
+func Vias(since time.Time) ViaIndex {
return cachedVias(since)
}
diff --git a/internal/usage/vias_cache.go b/internal/usage/vias_cache.go
index de63bd367..1d821fc4b 100644
--- a/internal/usage/vias_cache.go
+++ b/internal/usage/vias_cache.go
@@ -14,7 +14,20 @@ var viasCache struct {
value map[string][]Via
}
-func cachedVias(since time.Time) map[string][]Via {
+// ViaIndex is Vias' answer: the one copy every caller shares, handed out
+// whole and read only through Of, which gives each session's list as a
+// copy of its own. Each ask cloned every session's list, ~66 MB a time
+// over ~720k calls, where a page reads the few sessions it lists (#2776).
+type ViaIndex struct{ m map[string][]Via }
+
+// Of is what the gateway sent one session's calls to ("agent|id"), the
+// most calls first; nil for a session it sent none of.
+func (x ViaIndex) Of(key string) []Via { return slices.Clone(x.m[key]) }
+
+// Len is how many sessions have a list.
+func (x ViaIndex) Len() int { return len(x.m) }
+
+func cachedVias(since time.Time) ViaIndex {
snapshot := readLogSnapshot()
clients := clientAgentsKey()
viasCache.Lock()
@@ -26,9 +39,5 @@ func cachedVias(since time.Time) map[string][]Via {
viasCache.version, viasCache.since, viasCache.value, viasCache.clients = snapshot.version, since, value, clients
}
}
- out := make(map[string][]Via, len(value))
- for k, vs := range value {
- out[k] = slices.Clone(vs)
- }
- return out
+ return ViaIndex{value}
}
diff --git a/internal/usage/vias_cache_test.go b/internal/usage/vias_cache_test.go
new file mode 100644
index 000000000..9a8f0a551
--- /dev/null
+++ b/internal/usage/vias_cache_test.go
@@ -0,0 +1,61 @@
+package usage
+
+import (
+ "encoding/json"
+ "fmt"
+ "os"
+ "path/filepath"
+ "runtime"
+ "testing"
+ "time"
+)
+
+// Vias hands every caller the one copy it keeps, not a clone of every
+// session's list: a Sessions page reads the few sessions it lists, and
+// each ask cloned all of them, ~66 MB a time over ~720k calls (#2776).
+// A list read through Of is still the caller's own.
+func TestViasHandOutTheKeptCopy(t *testing.T) {
+ pageHome(t)
+ settleLogClock(t)
+ const sessionCount = 20000
+ if err := os.MkdirAll(filepath.Dir(Path()), 0o700); err != nil {
+ t.Fatal(err)
+ }
+ f, err := os.Create(Path())
+ if err != nil {
+ t.Fatal(err)
+ }
+ enc := json.NewEncoder(f)
+ now := Clock()
+ for i := range sessionCount {
+ if err := enc.Encode(Record{Time: now.Add(-time.Duration(i) * time.Second), Agent: "pi", Session: fmt.Sprintf("s%d", i), Provider: "p", Model: "m", Input: 10, Status: 200}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := f.Close(); err != nil {
+ t.Fatal(err)
+ }
+ vias := Vias(time.Time{})
+ if vias.Len() != sessionCount {
+ t.Fatalf("%d sessions, want %d", vias.Len(), sessionCount)
+ }
+ vias.Of("pi|s1")[0].Calls = 999
+ if got := Vias(time.Time{}).Of("pi|s1")[0].Calls; got != 1 {
+ t.Fatalf("a caller changed the kept copy: %d calls", got)
+ }
+ const asks = 20
+ var before, after runtime.MemStats
+ runtime.GC()
+ runtime.ReadMemStats(&before)
+ for range asks {
+ Vias(time.Time{}).Of("pi|s7")
+ }
+ runtime.ReadMemStats(&after)
+ // a clone of 20000 lists is a few MB; the log's check and one list
+ // are tens of KB
+ per := (after.TotalAlloc - before.TotalAlloc) / asks
+ t.Logf("an ask allocated %d bytes", per)
+ if per > 256<<10 {
+ t.Fatalf("an ask allocated %d bytes", per)
+ }
+}