Files
magpie/internal/sessions/quiet_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

148 lines
4.1 KiB
Go

package sessions
import (
"fmt"
"os"
"path/filepath"
"sync"
"sync/atomic"
"testing"
"time"
)
// moreSessions adds n Claude Code sessions to setup's.
func moreSessions(t *testing.T, claude string, n int) {
t.Helper()
dir := filepath.Join(claude, "projects", "-work-many")
os.MkdirAll(dir, 0o755)
for i := range n {
p := filepath.Join(dir, fmt.Sprintf("33333333-2222-3333-4444-%012d.jsonl", i))
os.WriteFile(p, []byte(ccMsg(fmt.Sprintf("m%d", i), "2026-09-21T10:00:00.000Z", 100, 10)), 0o644)
}
}
// holdQuietSlots takes every one of the warm-up's reading slots, and gives
// them back when the test ends.
func holdQuietSlots(t *testing.T) {
for range cap(quietSlots) {
quietSlots <- struct{}{}
}
t.Cleanup(func() {
for len(quietSlots) > 0 {
<-quietSlots
}
})
}
// The warm-up at start reads every changed file before anyone asks for
// them; after an update that reads them all again it held every core
// (#1665). It reads only with the warm-up's few slots: with all of them
// taken, neither the index (Stats) nor the calls (ReadCallSource) read a
// file, and a page asking has them read at once.
func TestWarmReadsOnlyInItsSlots(t *testing.T) {
inZone(t, 0)
claude, _ := setup(t)
moreSessions(t, claude, 12)
holdQuietSlots(t)
warm := func(what string, read func()) {
t.Helper()
readPeak.Store(0)
done := make(chan struct{})
go Warm(func() { read(); close(done) })
time.Sleep(300 * time.Millisecond)
if n := readPeak.Load(); n != 0 {
t.Fatalf("%s: the warm-up read %d files at once with no slot free", what, n)
}
// a page asks: read at full speed, slots or not
asked := Ask()
defer asked()
select {
case <-done:
case <-time.After(20 * time.Second):
t.Fatalf("%s: a page asking didn't have the warm-up's reading go on", what)
}
if readPeak.Load() == 0 {
t.Fatalf("%s: nothing read", what)
}
}
warm("the index", func() { statsAt(0, statsNow) })
if got := len(List(0)); got < 12 {
t.Fatalf("%d sessions listed", got)
}
// the index read each file's calls along (callsRide): a line more in
// each leaves the calls something of their own to read
for _, s := range CallSources() {
appendText(t, s.Path, "\n")
}
warm("the calls", func() {
for _, s := range CallSources() {
ReadCallSource(s)
}
})
}
// Outside the warm-up the slots don't hold anything up.
func TestReadSlotOnlyWhileWarming(t *testing.T) {
holdQuietSlots(t)
got := make(chan struct{})
go func() { readSlot()(); close(got) }()
select {
case <-got:
case <-time.After(10 * time.Second):
t.Fatal("a read outside the warm-up waited for a slot")
}
}
// A long first read keeps what it has read on disk as it goes: a magpie
// quit before the end (its CPU full on a slow disk, #1665) kept nothing of
// a minutes-long read, so the next start read it all again. The files after
// the first three wait until a save is written with files still to read.
func TestLongReadKeepsWhatItRead(t *testing.T) {
inZone(t, 0)
claude, _ := setup(t)
moreSessions(t, claude, 12)
old := checkpointEvery
checkpointEvery = 5 * time.Millisecond
early := make(chan struct{})
var once sync.Once
saveHook = func() {
if progress.files.Load() < progress.todo.Load() {
once.Do(func() { close(early) })
}
}
var parsed atomic.Int32
parseHook = func() {
if parsed.Add(1) > 3 {
select {
case <-early:
case <-time.After(5 * time.Second):
}
}
}
t.Cleanup(func() { checkpointEvery, saveHook, parseHook = old, nil, nil })
statsAt(0, statsNow)
Saved()
select {
case <-early:
default:
t.Fatal("nothing kept on disk until every file was read")
}
}
// With a page waiting, the index reads with half the cores, not twice
// them: a first read held 22 of 32 cores beside the user's apps (#3238).
func TestAskedIndexReadsWithHalfTheCores(t *testing.T) {
inZone(t, 0)
claude, _ := setup(t)
moreSessions(t, claude, 4*scanReaders()+8)
parseHook = func() { time.Sleep(20 * time.Millisecond) }
t.Cleanup(func() { parseHook = nil })
readPeak.Store(0)
defer Ask()()
statsAt(0, statsNow)
if n := readPeak.Load(); n > int32(scanReaders()) || n < int32(min(2, scanReaders())) {
t.Fatalf("the index read %d files at once, want 2 to %d", n, scanReaders())
}
}