Files
magpie/internal/sessions/codex_zst_warm_test.go
T
yetone 2140f5b935 sessions: a plain Codex rollout read from its start has its calls read on the summary's pass (#4369)
A start parses every changed session file for Sessions (refresh ->
parseWith), then reads every changed file's calls for Requests
(ReadCallSource -> readCalls). callsAlong already let a packed rollout
and an unread Claude session be read once for both; a plain .jsonl
rollout was read twice. On copies of the owner's real ~/.codex and
~/.claude (1.7 GB), a cold start scanned 931 times, 3.49 GB, every large
rollout x2.

callsAlong now takes a plain rollout too, when both reads start at its
first line: the summary it goes on from has no Codex window (nothing
kept, or kept on disk, which holds none), and parseWith lets the ride go
when either read goes on from an offset (calls.st.Off as well as s.Off),
so a read on from where it was is two incremental reads, as before. A
summary that still has its window is passed in so such a file's calls
shard isn't loaded only to be let go.

keepCalls names the root when none is named yet. A ride keeps a
rollout's calls (Codex window and all) during the Sessions parse, before
any Requests read has called pruneCalls; pruneCalls took the empty root
for a moved one and dropped everything kept. The shard on disk has no
window, so the next line read the rollout whole again (in the same run,
seconds after the start). TestHistoricalClaudeFilesDoNotDisplaceActive
Windows went red on exactly that ("rebuilt active call shard" for its
Codex rollout) until this.

TestWarmReadsOnlyInItsSlots' calls phase found nothing left to read,
since the index phase now reads every file's calls along; it appends a
line to each source first, so the calls have reading of their own.

Tests' forget (in the new test, claude_ride_test.go and
codex_zst_warm_test.go) calls Reset before removing the cache file:
Reset writes a save still pending, which put back the file just removed,
and the next List then went on from disk states (no window), red 1 in
~40 under -race. TestHistoricalClaudeFilesDoNotDisplaceActiveWindows
waits for the save (saved()) before writing its marker onto a cached
state the save's goroutine was encoding (a DATA RACE under -race).

Run on mote (macOS 27.2):
- TestPlainRolloutReadOnceForSessionsAndCalls (new): 3 plain rollouts.
  Nothing kept: each scanned once, from 0. Read, kept on disk, the long
  one grown and Reset: that one scanned once, from 0. An append in the
  same run: no scan from 0 (with keepCalls' root line removed by hand:
  "an append: the long rollout read again from its first line:
  [213219 0]", and the Historical test red as above). Calls, List and StatsAt reflect.DeepEqual to
  a calls-first read without the ride in each case. With plain rollouts
  kept out of callsAlong by hand: "first read: ... scanned from [0 0],
  want once from 0" for all 3. Restored: ok. -race -count=80 with
  the Packed, ClaudeSessionReadOnce, CodexSessionReadOn, Historical and
  WarmReads tests: ok (108.8s)
- Real data (copies of 1.7 GB of the owner's sessions, probe run
  sessions.List(200), usage QueryPage Today and Week; probe not
  committed): cold 931 scans / 3.49 GB -> 718 / 2.10 GB, no rollout read
  twice; no change 0 / 0; the first line appended to the newest rollout
  after the cold read: read whole by both readers -> read whole once
  (1 scan, 10.8 MB; the before run's newest was a 21.5 MB one, read
  twice). It is read whole at all because it was last changed on 10-31,
  past any window's idle time: a session left that long is read whole
  once on its next line, now in one pass. Later lines: 2 scans, 0 MB.
- go test -timeout 45m ./internal/sessions ./internal/usage
  ./internal/gateway ./internal/gui: ok for sessions (11.3s, -race 56.2s, at this tree), usage (30.2s), gateway (575.6s); internal/gui: ok but TestMainWindowCorners/macOS_26_SDK, red on macOS 27 as #3949 records (same message), untouched here
- GOOS=windows and GOOS=linux go vet ./internal/sessions: ok. gofmt -l:
  clean.

Not tried: a packed rollout's path is unchanged (it rode before).
2026-11-02 06:11:06 +08:00

120 lines
3.1 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() {
// Reset first: it writes a save still pending, which would put
// back a cache file removed before it
Reset()
os.Remove(CachePath())
os.RemoveAll(callCacheDir())
}
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")
}