Files
magpie/internal/sessions/codex_zst_warm_test.go
T
yetone 5466cc7604 sessions: a packed Codex rollout whose stamp changed is decompressed once at start, for both Sessions and Requests
The start's warm-up (gui warmSessions) parses every changed session file
for the Sessions page (statsFor -> refresh -> parse), then reads every
changed file's calls for Requests (usage.QueryPage -> prefetchSources ->
ReadCallSource -> readCalls). A packed rollout (.jsonl.zst) is decompressed
whole for each read, so when the Codex app repacked all 11,591 of them on
10-20 every one was decompressed and scanned twice (~48 GB of zstd read
twice; 75 GB of zstd allocation in 67 min, 37.8 GB on each path through
scanAt).

refresh's reader now takes a packed Codex rollout's calls along
(callsAlong) when their shard is stale too: one scanAt, each line shown to
both heads (codexHead, callHead) exactly as on a scan of its own, and
handed to codexBody and/or codexCallLine as each head asked. The calls
are then written and kept as readCalls would (writeCalls, keepCalls under
the generation check), so the Requests read that follows finds an
up-to-date shard. The ride holds the file's calls lock, taken before the
read slot as readCalls takes it, so a readCalls of the same file waits for
the ride rather than read it again, and no lock order is reversed. A scan
that fails leaves the calls to readCalls, as before. Plain rollouts, other
agents and a file whose calls are already current are read as before.

The warm-up already reads with a quarter of the cores (quiet.go: readSlot,
#1665) on both paths; this halves the work done under that cap, nothing
more is changed there.

Verified (sandbox HOME, Go caches pinned):
- TestPackedRolloutReadOnceForSessionsAndCalls (new): 3 packed rollouts
  (the two fixtures + a 40-call one). Calls-then-sessions scans each twice
  (the reference); sessions-then-calls with nothing kept, and again after
  every file's mtime changes, scans each once, and the calls, List and
  StatsAt are reflect.DeepEqual to the reference. With callsAlong replaced
  by nil in refresh it fails: "first read: ...zst scanned 2 times, want
  once" for all 3 files, and the same after repacking (6 errors). Restored:
  ok. -race -count=20: ok.
- Real rollouts: copies of 120 and of 180 of ~/.codex/sessions' packed
  rollouts (528 MB / 739 MB compressed), warm-up order (List, StatsAt, then
  ReadCallSource with 8 readers), with nothing kept ("cold") and after every
  mtime changed ("stamp"); old = callsAlong nil:
    120 files, full speed:  zst scans 240 -> 120; wall cold 0.84/0.90s ->
      0.52/0.48s, stamp 0.99/0.70s -> 0.43/0.52s; alloc 0.45-0.48 GB ->
      0.43-0.44 GB cold, 0.38-0.41 GB -> 0.38-0.39 GB stamp.
    180 files, inside sessions.Warm (3 readers of 14 cores): zst scans
      360 -> 180; wall cold 1.97/2.18s -> 1.48/1.59s, stamp 1.89/2.22s ->
      1.35/1.63s; alloc 0.57-0.63 GB both ways.
  Lists, stats and every source's calls, JSON-encoded, are byte-identical
  old vs new in all four phases (cmp).
  Allocation hardly moves: in a pprof of the 120-file run, 482 MB of ~800
  MB is scanAt gathering lines longer than its 1 MiB buffer (the packed
  rollouts' multi-MB session_meta lines), which only one of the two
  readers ever gathered, and zstd's own history buffers are 120-144 MB.
  What the change saves is the second decompression: CPU and disk reads.
- gofmt -l on the changed files: nothing. GOOS=linux go vet -tags nogui
  ./... ok; GOOS=windows go vet -tags nogui ./... ok.
- go test -timeout 45m ./internal/sessions/ ./internal/usage/
  ./internal/gui/: ok 13.8s / 17.1s / 81.7s. go test -race
  ./internal/sessions/: ok 44.5s after the next commit's test fix (red on
  origin/main a9304efe0 without it, TestManageReadsOnlyTheAgentsItShows).
Not run: the full ./... suite, the GUI Playwright suite (no GUI change),
a live magpie start against all 11,591 rollouts, Windows/Linux runs.
2026-10-20 23:50:02 +08:00

118 lines
3.0 KiB
Go

package sessions
import (
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"testing"
"time"
)
// TestPackedRolloutReadOnceForSessionsAndCalls: the start's warm-up parses
// every changed session file for the Sessions page, then reads every
// changed file's calls for Requests. A packed rollout is decompressed whole
// for each read, so when the Codex app repacked all of them (11,591 on
// 10-20) each was decompressed twice. Its calls are now read on the
// parse's own pass, and what both pages get is what the two reads gave.
func TestPackedRolloutReadOnceForSessionsAndCalls(t *testing.T) {
_, codex := setup(t)
writeLines(t, filepath.Join(codex, "sessions", "2026", "09", "21", "rollout-2026-09-21T10-00-00-0190aaaa-1111-7222-8333-444455556666.jsonl"), longRollout(0, 40)...)
for _, p := range rollouts(t, codex) {
pack(t, p, false)
}
// nothing kept: neither the summaries nor the calls' shards
forget := func() {
os.Remove(CachePath())
os.RemoveAll(callCacheDir())
Reset()
}
var mu sync.Mutex
scans := map[string]int{}
scanHook = func(p string, off int64) {
if strings.HasSuffix(p, zstSuffix) {
mu.Lock()
scans[p]++
mu.Unlock()
}
}
t.Cleanup(func() { scanHook = nil })
counted := func() map[string]int {
mu.Lock()
defer mu.Unlock()
out := scans
scans = map[string]int{}
return out
}
readCallsAll := func() map[string][]Call {
out := map[string][]Call{}
for _, s := range CallSources() {
if s.Agent == "codex" {
out[s.Path] = ReadCallSource(s)
}
}
return out
}
day := time.Date(2026, 9, 30, 0, 0, 0, 0, time.UTC)
// as before: calls read first, then the summaries, each on a scan of
// its own; the reference for what both pages get
forget()
counted()
wantCalls := readCallsAll()
wantList, wantStats := List(0), StatsAt(0, day)
before := counted()
n := 0
for p, k := range before {
if k != 2 {
t.Fatalf("calls, then sessions: %s scanned %d times, want 2", p, k)
}
n++
}
if n != 3 {
t.Fatalf("%d packed rollouts scanned, want 3: %v", n, before)
}
calls := 0
for _, cs := range wantCalls {
calls += len(cs)
}
if calls < 40 {
t.Fatalf("only %d calls read: the fixture tests nothing", calls)
}
check := func(name string) {
t.Helper()
list, stats := List(0), StatsAt(0, day)
got := readCallsAll()
for p, k := range counted() {
if k != 1 {
t.Errorf("%s: %s scanned %d times, want once", name, p, k)
}
}
if !reflect.DeepEqual(got, wantCalls) {
t.Errorf("%s: calls differ from their own read's", name)
}
if name == "first read" && (!reflect.DeepEqual(list, wantList) || !reflect.DeepEqual(stats, wantStats)) {
t.Errorf("%s: sessions differ from a read without the calls", name)
}
}
// the warm-up's order with nothing kept: sessions, then calls
forget()
counted()
check("first read")
// every rollout repacked: both pages' kept reads are stale
later := time.Now().Add(-time.Hour)
for p := range wantCalls {
if err := os.Chtimes(p, later, later); err != nil {
t.Fatal(err)
}
}
Reset()
counted()
check("after repacking")
}