Files
magpie/internal/sessions/stats_subagent_test.go
T
yetone 42e6aee534 sessions: the stats and id lookups don't build every session's subagents
Why: in a 48-minute heap profile of the user's magpie,
gui.combinedSessionStats → statsFor → StatsAndNative → statsIn
allocated 3.9 GB, and gui.warmSessions → Warm 5.7 GB. Their scans went
through assemble → subagentsOf → ccTask: the stats built every
session's subagents (each one's task read from its file), only to
count tokens and tell which sessions are listed. Every statsFor call
in the profile came from the warm-up's steps.

What changed:

- assemble is split. assembleListed holds the part that decides
  whether a session is listed and gives its agent, id, title, models
  and usage. assemble adds resume, carry, transcript and subagents on
  top.
- These callers now use assembleListed: the stats' native-key and
  summary loops (stats.go), Titles, the Codex provider lookup
  (codex_provider.go) and manage's id lookup.
- List, One and manage's listing still use assemble.
- The docs/subsystems/gateway-sessions.md subagent section says
  which callers build subagents.

Tests:

- TestStatsReadNoSubagentFile writes a subagent with no meta and no
  task in its first lines, then simulates a restart (Saved, Reset,
  memos cleared). It then runs StatsAndNative(0), StatsAndNative(30)
  and StatsFor(7) three times and expects 0 scans of that file. List
  still shows the subagent.
- Broken by hand: the stats back on assemble make it go red with
  "the stats read an unchanged subagent's file 1 times". Green again
  once restored.

Measured on a read-only APFS clone of the user's ~/.claude/projects
and ~/.codex/sessions/2026/10, in a sandbox HOME, at load ~500 from
other sessions. The whole series against origin/main dee5c9d75; this
commit is the change to the stats rows.

| Pass | origin/main | This series |
| --- | --- | --- |
| warm StatsAndNative(0) | 596.7 MB, 157,195 allocs, 6.2–9.0 s | 28.9 MB, 73,976 allocs, 0.08–1.1 s |
| warm StatsAndNative(30) | 594.9 MB, 6.8 s | 28.7 MB, 0.8 s |
| restart StatsAndNative(0) | 3,106.7 MB, 509,239 allocs, 30.0 s | 48.2 MB, 241,721 allocs, 1.9 s |
| restart StatsAndNative(30) | 594.9 MB, 9.6 s | 28.7 MB, 0.5 s |

Run at the final tree of this series:

- gofmt -l internal: nothing.
- GOOS=linux and GOOS=windows go vet -tags nogui ./...: clean.
- go test -count=1 -timeout 45m ./internal/sessions/ ./internal/gui/:
  - sessions: ok, 366.7s.
  - gui: only TestMainWindowCorners failed, "measuring the windows:
    signal: killed". It also fails alone on an untouched origin/main
    export, at load ~500. Filed as #1936 and #2645. Every other gui
    test passed.
- go test -race -count=3 on the new tests: ok.
2026-10-20 20:32:04 +08:00

44 lines
1.3 KiB
Go

package sessions
import (
"path/filepath"
"testing"
)
// The stats, which the Sessions page and the overview poll, read no
// subagent's file to tell which sessions are listed: only the files that
// changed are read. They built every session's subagents, and each
// subagent's task was read again from its file at every start, and on
// every poll for one whose first lines name none: 3.5 GB of a live
// magpie's 6.3 GB of session scans in 48 minutes.
func TestStatsReadNoSubagentFile(t *testing.T) {
inZone(t, 0)
claude, _ := setup(t)
sub := filepath.Join(claude, "projects", "-work-app", "11111111-2222-3333-4444-555555555555", "subagents", "agent-b2.jsonl")
writeFile(t, sub, ccHookLine(0)+ccHookLine(1))
StatsAndNative(0) // every file read once
Saved()
// a restart: the parses come back from the disk, nothing else kept
Reset()
ccTasks.Clear()
ccMetas.Clear()
scans := scansOf(t, sub)
for range 3 {
StatsAndNative(0)
StatsAndNative(30)
StatsFor(7)
}
if n := scans(); n != 0 {
t.Fatalf("the stats read an unchanged subagent's file %d times", n)
}
// the Sessions list still names the subagent and reads its task
s := find(t, List(0), "claude", "11111111-2222-3333-4444-555555555555")
found := false
for _, sa := range s.Subagents {
found = found || sa.ID == "b2"
}
if !found {
t.Fatalf("subagents %+v", s.Subagents)
}
}