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).
150 lines
4.1 KiB
Go
150 lines
4.1 KiB
Go
package sessions
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestPlainRolloutReadOnceForSessionsAndCalls: a plain rollout read from
|
|
// its first line, at a start with nothing kept or after a restart once it
|
|
// grew (neither read keeps its Codex window on disk), has its calls read on
|
|
// the summary's own pass, as a packed one has. Read apart, a cold start
|
|
// read every rollout twice: 3.5 GB of reads for 1.7 GB of real rollouts
|
|
// (#4369). A rollout read on from where it was is read by each as before.
|
|
func TestPlainRolloutReadOnceForSessionsAndCalls(t *testing.T) {
|
|
_, codex := setup(t)
|
|
long := filepath.Join(codex, "sessions", "2026", "09", "21", "rollout-2026-09-21T10-00-00-0190aaaa-1111-7222-8333-444455556666.jsonl")
|
|
writeLines(t, long, longRollout(0, 40)...)
|
|
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][]int64{}
|
|
scanHook = func(p string, off int64) {
|
|
if strings.HasSuffix(p, ".jsonl") && strings.Contains(p, "rollout-") {
|
|
mu.Lock()
|
|
scans[p] = append(scans[p], off)
|
|
mu.Unlock()
|
|
}
|
|
}
|
|
t.Cleanup(func() { scanHook = nil })
|
|
counted := func() map[string][]int64 {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
out := scans
|
|
scans = map[string][]int64{}
|
|
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)
|
|
// reference: calls read first, then the summaries, each on its own scan
|
|
reference := func() (map[string][]Call, []Session, Stats) {
|
|
forget()
|
|
calls := readCallsAll()
|
|
list, stats := List(0), StatsAt(0, day)
|
|
counted()
|
|
return calls, list, stats
|
|
}
|
|
type read struct {
|
|
calls map[string][]Call
|
|
list []Session
|
|
stats Stats
|
|
scans map[string][]int64
|
|
}
|
|
// the warm-up's order: sessions, then calls
|
|
run := func() read {
|
|
list, stats := List(0), StatsAt(0, day)
|
|
calls := readCallsAll()
|
|
return read{calls, list, stats, counted()}
|
|
}
|
|
same := func(name string, got read) {
|
|
t.Helper()
|
|
calls, list, stats := reference()
|
|
if !reflect.DeepEqual(got.calls, calls) {
|
|
t.Errorf("%s: calls differ from their own read's", name)
|
|
}
|
|
if !reflect.DeepEqual(got.list, list) || !reflect.DeepEqual(got.stats, stats) {
|
|
t.Errorf("%s: sessions differ from a read without the calls", name)
|
|
}
|
|
}
|
|
grow := func(from, to int) {
|
|
f, err := os.OpenFile(long, os.O_APPEND|os.O_WRONLY, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, l := range longRollout(from, to) {
|
|
f.WriteString(l + "\n")
|
|
}
|
|
f.Close()
|
|
}
|
|
|
|
calls, _, _ := reference()
|
|
n := 0
|
|
for _, cs := range calls {
|
|
n += len(cs)
|
|
}
|
|
if len(calls) != 3 || n < 40 {
|
|
t.Fatalf("%d rollouts, %d calls: the fixture tests nothing", len(calls), n)
|
|
}
|
|
|
|
forget()
|
|
counted()
|
|
got := run()
|
|
if len(got.scans) != 3 {
|
|
t.Errorf("first read: %d rollouts scanned, want 3: %v", len(got.scans), got.scans)
|
|
}
|
|
for p, offs := range got.scans {
|
|
if len(offs) != 1 || offs[0] != 0 {
|
|
t.Errorf("first read: %s scanned from %v, want once from 0", filepath.Base(p), offs)
|
|
}
|
|
}
|
|
same("first read", got)
|
|
|
|
// the next line, in the same run as a first read: each reader reads on
|
|
// from where it was (the calls' window was dropped as a moved root's)
|
|
forget()
|
|
run()
|
|
grow(40, 42)
|
|
got = run()
|
|
if len(got.scans) != 1 || len(got.scans[long]) == 0 {
|
|
t.Errorf("an append: %v scanned, want the long rollout only", got.scans)
|
|
}
|
|
for _, off := range got.scans[long] {
|
|
if off == 0 {
|
|
t.Errorf("an append: the long rollout read again from its first line: %v", got.scans[long])
|
|
}
|
|
}
|
|
same("an append", got)
|
|
|
|
// grown while magpie was closed: neither read keeps its window on
|
|
// disk, so both read it from its first line, on one pass
|
|
forget()
|
|
run()
|
|
grow(42, 43)
|
|
Reset()
|
|
counted()
|
|
got = run()
|
|
if offs := got.scans[long]; len(got.scans) != 1 || len(offs) != 1 || offs[0] != 0 {
|
|
t.Errorf("grown after a restart: %v scanned, want the long rollout once from 0", got.scans)
|
|
}
|
|
same("grown after a restart", got)
|
|
}
|