Skip to content

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

sessions: the overview points to the sessions it sums up, not copies (#2278)

yetone3ec9babe7d20
diff --git a/internal/sessions/overview_alloc_test.go b/internal/sessions/overview_alloc_test.go
new file mode 100644
index 000000000..9fef4db68
--- /dev/null
+++ b/internal/sessions/overview_alloc_test.go
@@ -0,0 +1,30 @@
+package sessions
+
+import (
+ "fmt"
+ "runtime"
+ "testing"
+ "unsafe"
+)
+
+// The overview points to the sessions it sums up and copies only the few
+// it shows: the Sessions page's poll copied every Summary four times, ~1.5
+// GB in two minutes over 34k sessions while an agent worked (#2278).
+func TestOverviewCopiesOnlyItsTop(t *testing.T) {
+ st := Stats{From: "2026-10-01", To: "2026-10-28"}
+ for i := range 5000 {
+ st.Sessions = append(st.Sessions, Summary{Key: fmt.Sprint("claude:", i), Agent: "claude", ID: fmt.Sprint(i), Tokens: Tokens{Input: i, Output: 1}, Cost: float64(i), Active: int64(i), Days: []int{i % 28}})
+ }
+ st.Overview("", "", "", 8) // the maps' first growth
+ var before, after runtime.MemStats
+ runtime.ReadMemStats(&before)
+ o := st.Overview("", "", "", 8)
+ runtime.ReadMemStats(&after)
+ if o.Count != 5000 || len(o.Top["tokens"]) != 8 || o.Top["tokens"][0].ID != "4999" || o.Top["cost"][7].ID != "4992" {
+ t.Fatalf("count %d, top %d, first %q", o.Count, len(o.Top["tokens"]), o.Top["tokens"][0].ID)
+ }
+ all := uint64(len(st.Sessions)) * uint64(unsafe.Sizeof(Summary{}))
+ if got := after.TotalAlloc - before.TotalAlloc; got > all/2 {
+ t.Fatalf("the overview allocated %d bytes; its sessions are %d", got, all)
+ }
+}
diff --git a/internal/sessions/stats.go b/internal/sessions/stats.go
index 8cb9ed5d1..189bedf07 100644
--- a/internal/sessions/stats.go
+++ b/internal/sessions/stats.go
@@ -553,9 +553,13 @@ func (st Stats) Overview(agent, model, cwd string, n int) Overview {
out.Days = make([]int, int(math.Round(last.Sub(first).Hours()/24))+1)
}
out.Messages, out.Output = make([]int, len(out.Days)), make([]int, len(out.Days))
- var in []Summary
- var spent []int
- for _, s := range st.Sessions {
+ // the sessions are pointed to, not copied: a Summary is ~400 bytes, and
+ // the Sessions page's poll copied all of them four times, ~1.5 GB in
+ // two minutes over 34k sessions while an agent worked (#2278)
+ in := make([]*Summary, 0, len(st.Sessions))
+ spent := make([]int, 0, len(st.Sessions))
+ for i := range st.Sessions {
+ s := &st.Sessions[i]
if agent != "" && s.Agent != agent || cwd != "" && s.Cwd != cwd || model != "" && !slices.Contains(s.Models, model) {
continue
}
@@ -583,25 +587,29 @@ func (st Stats) Overview(agent, model, cwd string, n int) Overview {
rank := func(p float64) int { return spent[max(0, int(math.Ceil(p*float64(len(spent))))-1)] }
out.Median, out.P90 = rank(0.5), rank(0.9)
}
- for by, v := range map[string]func(Summary) float64{
- "tokens": func(s Summary) float64 { return float64(s.Input + s.Output) },
- "cost": func(s Summary) float64 { return s.Cost },
- "active": func(s Summary) float64 { return float64(s.Active) },
+ top := make([]*Summary, len(in))
+ for by, v := range map[string]func(*Summary) float64{
+ "tokens": func(s *Summary) float64 { return float64(s.Input + s.Output) },
+ "cost": func(s *Summary) float64 { return s.Cost },
+ "active": func(s *Summary) float64 { return float64(s.Active) },
} {
- top := slices.Clone(in)
+ copy(top, in)
sort.SliceStable(top, func(i, j int) bool { return v(top[i]) > v(top[j]) })
k := 0
for k < len(top) && k < n && v(top[k]) > 0 {
k++
}
- out.Top[by] = top[:k]
+ out.Top[by] = make([]Summary, k)
+ for i, s := range top[:k] {
+ out.Top[by][i] = *s
+ }
}
return out
}
// shapes buckets the sessions that told their messages, or for minutes
// those that were at work.
-func shapes(in []Summary) map[string]Shape {
+func shapes(in []*Summary) map[string]Shape {
out := map[string]Shape{}
for by, edges := range shapeEdges {
sh := Shape{Edges: edges, Counts: make([]int, len(edges))}
@@ -636,7 +644,7 @@ func shapes(in []Summary) map[string]Shape {
}
// toolUse sums up the sessions' tool calls, the n most called.
-func toolUse(in []Summary, first time.Time, n int) ToolUse {
+func toolUse(in []*Summary, first time.Time, n int) ToolUse {
out := ToolUse{Top: []ToolStat{}, Categories: []ToolStat{}, Weeks: []ToolWeek{}}
tools, cats := map[string]*ToolStat{}, map[string]*ToolStat{}
weeks := map[string]map[string]int{}
@@ -701,7 +709,7 @@ func toolUse(in []Summary, first time.Time, n int) ToolUse {
}
// skillUse sums up the skills the sessions called up, the n most called.
-func skillUse(in []Summary, n int) SkillUse {
+func skillUse(in []*Summary, n int) SkillUse {
out := SkillUse{Top: []Skill{}}
skills := map[string]*Skill{}
projects := map[string]map[string]int{}
diff --git a/internal/sessions/stats_tools_test.go b/internal/sessions/stats_tools_test.go
index fd92d3358..a449e3f46 100644
--- a/internal/sessions/stats_tools_test.go
+++ b/internal/sessions/stats_tools_test.go
@@ -112,7 +112,7 @@ func TestStatsToolsAndSkills(t *testing.T) {
// TestShapesMinutesWithoutMessages: a session whose agent tells no messages
// (pi, grok…) still counts by its minutes at work, and only there.
func TestShapesMinutesWithoutMessages(t *testing.T) {
- sh := shapes([]Summary{
+ sh := shapes([]*Summary{
{Agent: "claude", Prompts: 2, Replies: 3, ToolCalls: 4, Active: 600},
{Agent: "pi", Active: 1200},
{Agent: "grok"},