Files
magpie/internal/sessions/scan_buffer_test.go
T
yetone 85e4c693ee sessions: a packed rollout's long line is gathered once, at its own size
scanAt reads a line longer than its 1 MiB read buffer as several chunks.
For a plain file, a7e2c7235 (#2515) reads ahead to the end of the line and
gathers it into one buffer of exactly that size. A packed (.zst) rollout
can't read ahead, so it still grew the line by append, a chunk at a time.
That allocated about 3.8 times the line. A cold read of 120 of the owner's
real packed rollouts allocated 2.2 GB, and 1.61 GB of it was this append.

Lines the head says no to were already stepped over without being
gathered. On this corpus the gathered long lines were all wanted ones.
There were 63 user messages with pasted images on the session side
(422 MB), which codexHead lets through for Prompts and Title. There were
17 compactions (70 MB). There were also 14 plain-file messages (19 MB),
which were already sized exactly. These lines are read whole:
cxFastPrompt looks through every content part, and cxCompactionIn uses
gjson to find fields anywhere in the line. So the line still has to be
handed over whole, and this change is about how it is gathered.

- A packed file's long line is copied into pooled 1 MiB blocks until its
  end. Then it is gathered once, into a buffer of its own length.
- That buffer comes from a pool (longLines) and goes back to it when the
  scan ends. A line from it is good only until then, which is the same
  rule as for the read buffer. Under the tests (poisonReleased) the
  buffer is filled with 0xFF when it is given back.
- The plain-file read-ahead path is unchanged, apart from taking its
  buffer from the same pool.

Measured on copies in a sandbox: 120 real .jsonl.zst rollouts (1.30 GB
packed, 1.95 GB unpacked; the 60 largest and 60 recent), 30 plain Codex
.jsonl (213 MB) and 40 Claude Code project .jsonl (60 MB). CODEX_HOME and
CLAUDE_CONFIG_DIR pointed at the copies, with a fresh testenv home each
run. Each run was List(100000), StatsAt(3650, fixed now), ReadCallSource
for each of the 190 CallSources, and Calls(time.Time{}), in its own
process. Three runs each, at load average ~30-40:

                    TotalAlloc         wall
  base 9afdeb89c    2161/2198/2203 MB  3.12/2.39/2.52 s
  this commit        668/ 678/ 724 MB  2.54/2.32/2.22 s

All 4 JSON encodings (List, StatsAt, every source's calls, and Calls)
are byte-identical to the base's, checked with cmp on all 3 runs of
each binary. After the change, pprof puts scanAt's own allocation at
~134 MB for long-line buffers and ~96 MB for blocks that the pool
dropped at a GC, down from 1.79 GB.

Tests (internal/sessions/scan_buffer_test.go):
- TestScanGathersAPackedLongLineOnce: a .jsonl.zst with a wanted 20 MiB
  line, a 20 MiB line the head says no to, and two short lines. The
  wanted line is handed over byte for byte. The scan allocates at most
  2.5 times the line plus 16 MiB for the decoder's window. It passed
  -count=20. With the base scanAt put back by hand it is red 3 of 3:
  "allocated 106639008/117266584/106633368 bytes, want at most
  69206078".
- TestBlocksKeepALineInOrder: blocks give back across a block's end
  what they were given.

Run in a sandbox HOME, Go caches pinned, set -o pipefail:
- gofmt -l internal cmd: none.
- GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet -tags
  nogui ./...: both exit 0.
- go test -count=1 -timeout 45m ./internal/sessions/ ./internal/usage/
  ./internal/gui/: ok (23.8s, 21.1s, 105.7s).
- go test -count=1 -race ./internal/sessions/: ok (52.9s).
- Not run: the full suite, the GUI Playwright suite (no asset changed),
  the Windows box, a live magpie's RSS.

Siblings not changed here:
- traces.go's TraceReader reads plain rollouts with ReadBytes. That read
  is bounded by its 8 MiB poll budget.
- frames.go's dsh frames append a line split across frames.
- plugin/host.go's readLine is plugin IPC, not session files.
2026-10-21 00:07:49 +08:00

285 lines
9.7 KiB
Go

package sessions
import (
"bytes"
"context"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/klauspost/compress/zstd"
)
// A scan of a small session file takes a buffer the size of the file, not
// 1 MiB: a start reads thousands of them, and their buffers were most of
// what it allocated (#2776).
func TestScanOfASmallFileTakesASmallBuffer(t *testing.T) {
p := filepath.Join(t.TempDir(), "s.jsonl")
if err := os.WriteFile(p, []byte(`{"type":"user","n":1}`+"\n"+`{"type":"assistant","n":2}`+"\n"), 0o600); err != nil {
t.Fatal(err)
}
const scans = 200
lines := 0
var before, after runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&before)
for range scans {
if _, err := scanAt(p, 0, nil, func(b []byte, _, _ int64) bool { lines++; return true }); err != nil {
t.Fatal(err)
}
}
runtime.ReadMemStats(&after)
if lines != 2*scans {
t.Fatalf("read %d lines, want %d", lines, 2*scans)
}
// 16 KiB each, with room for the file handles and anything else
if per := (after.TotalAlloc - before.TotalAlloc) / scans; per > 64<<10 {
t.Fatalf("a scan of a two-line file allocated %d bytes", per)
}
}
// A line longer than the buffer (the file grew after its size was read) is
// still handed over whole.
func TestScanReadsALineLongerThanItsBuffer(t *testing.T) {
p := filepath.Join(t.TempDir(), "s.jsonl")
if err := os.WriteFile(p, []byte("{}\n"), 0o600); err != nil {
t.Fatal(err)
}
f, err := os.Open(p)
if err != nil {
t.Fatal(err)
}
size := scanBuffer(f, 0)
f.Close()
long := `{"text":"` + strings.Repeat("x", 3*size) + `"}`
if err := os.WriteFile(p, []byte(long+"\n{}\n"), 0o600); err != nil {
t.Fatal(err)
}
var got []int
if _, err := scanAt(p, 0, nil, func(b []byte, _, _ int64) bool { got = append(got, len(b)); return true }); err != nil {
t.Fatal(err)
}
if len(got) != 2 || got[0] != len(long) || got[1] != 2 {
t.Fatalf("lines %v, want [%d 2]", got, len(long))
}
}
// Every other reader of a session file sizes its buffer as scanAt does:
// the calls readers of MiniMax Code, agy and WorkBuddy, dsh's packed
// frames and a search's first look each took 1 MiB per file read (#2776).
func TestReadersOfASmallFileTakeASmallBuffer(t *testing.T) {
dir := t.TempDir()
write := func(name, body string) string {
t.Helper()
p := filepath.Join(dir, name)
if err := os.WriteFile(p, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
return p
}
line := `{"type":"user","n":1}` + "\n"
plain := write("s.jsonl", line+line)
enc, err := zstd.NewWriter(nil)
if err != nil {
t.Fatal(err)
}
framed := write("s.v3.jsonl.zstd", string(enc.EncodeAll([]byte(line+line), nil)))
enc.Close()
for _, c := range []struct {
name string
read func()
}{
{"minimax", func() { readMiniMaxCalls(file{path: plain, key: "minimax-code:s"}) }},
{"agy", func() { readAgyCalls(file{path: plain, key: "agy:s"}) }},
{"workbuddy", func() { readWorkBuddyCalls(file{agent: "workbuddy", path: plain, key: "workbuddy:s"}) }},
{"frames", func() { scanFrames(framed, 0, 0, func([]byte) bool { return true }) }},
{"search", func() { mayHold(context.Background(), plain, "absent") }},
} {
t.Run(c.name, func(t *testing.T) {
const reads = 50
c.read() // anything a first read sets up once
var before, after runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&before)
for range reads {
c.read()
}
runtime.ReadMemStats(&after)
if per := (after.TotalAlloc - before.TotalAlloc) / reads; per > 256<<10 {
t.Fatalf("a read of a two-line file allocated %d bytes", per)
}
})
}
}
// scanAllocated is what one scan of the file at p allocated, and the
// lengths of the lines it handed over.
func scanAllocated(t *testing.T, p string) (uint64, []int) {
t.Helper()
var got []int
var before, after runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&before)
if _, err := scanAt(p, 0, nil, func(b []byte, _, _ int64) bool { got = append(got, len(b)); return true }); err != nil {
t.Fatal(err)
}
runtime.ReadMemStats(&after)
return after.TotalAlloc - before.TotalAlloc, got
}
// A line longer than the read buffer (a pasted screenshot's base64) is
// gathered into one buffer of its own size. Grown a buffer's worth at a
// time it allocated ~5 times its length, for each reader of each file, and
// a start of sessions holding images allocated GBs of it (#2515).
func TestScanGathersALongLineOnce(t *testing.T) {
p := filepath.Join(t.TempDir(), "s.jsonl")
long := `{"type":"user","data":"` + strings.Repeat("x", 8<<20) + `"}`
if err := os.WriteFile(p, []byte(long+"\n{}\n"), 0o600); err != nil {
t.Fatal(err)
}
alloc, got := scanAllocated(t, p)
if len(got) != 2 || got[0] != len(long) || got[1] != 2 {
t.Fatalf("lines %v, want [%d 2]", got, len(long))
}
// the line once and the 1 MiB read buffer, with room for both; grown
// a chunk at a time it was ~38 MiB
if want := uint64(len(long)) * 3 / 2; alloc > want {
t.Fatalf("a scan of an %d-byte line allocated %d bytes, want at most %d", len(long), alloc, want)
}
}
// A line past maxLine is stepped over without being gathered first.
func TestScanStepsOverATooLongLineUngathered(t *testing.T) {
p := filepath.Join(t.TempDir(), "s.jsonl")
long := `{"type":"user","data":"` + strings.Repeat("x", maxLine+(4<<20)) + `"}`
if err := os.WriteFile(p, []byte(long+"\n{}\n"), 0o600); err != nil {
t.Fatal(err)
}
alloc, got := scanAllocated(t, p)
if len(got) != 1 || got[0] != 2 {
t.Fatalf("lines %v, want [2]", got)
}
// the 1 MiB read buffer and the look ahead, with room; gathered first
// it was ~150 MiB
if alloc > 8<<20 {
t.Fatalf("a scan stepping over a %d-byte line allocated %d bytes", len(long), alloc)
}
}
// Scans of big files share their read buffers: each took a new one of up
// to 1 MiB, and a live magpie's scans allocated 3.8 GB of them in 48
// minutes while ~157 MB was live.
func TestScansShareTheirBuffers(t *testing.T) {
if raceOn {
t.Skip("-race drops pooled buffers at random")
}
p := filepath.Join(t.TempDir(), "big.jsonl")
line := `{"type":"assistant","text":"` + strings.Repeat("x", 4000) + `"}` + "\n"
writeFile(t, p, strings.Repeat(line, 600)) // ~2.4 MB
const scans = 50
lines := 0
scan := func() {
if _, err := scanAt(p, 0, nil, func([]byte, int64, int64) bool { lines++; return true }); err != nil {
t.Fatal(err)
}
}
scan()
var before, after runtime.MemStats
runtime.ReadMemStats(&before)
for range scans {
scan()
}
runtime.ReadMemStats(&after)
if lines != 600*(scans+1) {
t.Fatalf("read %d lines", lines)
}
// a GC between scans may let a pooled buffer go: room for a few
if per := (after.TotalAlloc - before.TotalAlloc) / scans; per > 128<<10 {
t.Fatalf("a scan of a 2.4 MB file allocated %d bytes", per)
}
}
// The tests run with a buffer given back filled with garbage: what makes
// a line kept past its scan fail in them.
func TestALineKeptPastItsScanIsGarbageInTests(t *testing.T) {
r, release := lineReader(bytes.NewReader([]byte("{\"a\":1}\n")), 16<<10)
b, err := r.ReadSlice('\n')
if err != nil {
t.Fatal(err)
}
release()
if !bytes.Equal(b, bytes.Repeat([]byte{0xFF}, len(b))) {
t.Fatalf("a line kept past its scan reads %q", b)
}
}
// A packed rollout's long line (a pasted screenshot in a prompt) has no
// length to read ahead, so it was grown a read buffer at a time and
// allocated ~3.8 times its size; a cold read of 120 real packed rollouts
// allocated 1.6 GB of 2.2 GB there. It is now gathered once, at its own
// size, and handed over whole; a line its head says no to is not gathered
// at all.
func TestScanGathersAPackedLongLineOnce(t *testing.T) {
p := filepath.Join(t.TempDir(), "rollout.jsonl.zst")
wanted := `{"type":"want","data":"` + strings.Repeat("x", 20<<20) + `"}`
unwanted := `{"type":"skip","data":"` + strings.Repeat("y", 20<<20) + `"}`
enc, err := zstd.NewWriter(nil)
if err != nil {
t.Fatal(err)
}
body := enc.EncodeAll([]byte(wanted+"\n{}\n"+unwanted+"\n{}\n"), nil)
enc.Close()
if err := os.WriteFile(p, body, 0o600); err != nil {
t.Fatal(err)
}
skip := func(b []byte) bool { return !bytes.Contains(b[:min(len(b), 64)], []byte(`"skip"`)) }
none := func([]byte) bool { return false } // sets up the decoder a packed scan keeps
scan := func(head func([]byte) bool) (uint64, []int) {
var got []int
var before, after runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&before)
if _, err := scanAt(p, 0, head, func(b []byte, _, _ int64) bool {
if len(b) > 2 && !bytes.Equal(b, []byte(wanted)) {
t.Fatalf("a %d-byte line handed over is not the one written", len(b))
}
got = append(got, len(b))
return true
}); err != nil {
t.Fatal(err)
}
runtime.ReadMemStats(&after)
return after.TotalAlloc - before.TotalAlloc, got
}
if _, got := scan(none); len(got) != 0 {
t.Fatalf("lines %v handed over, want none", got)
}
alloc, got := scan(skip)
if len(got) != 3 || got[0] != len(wanted) || got[1] != 2 || got[2] != 2 {
t.Fatalf("lines %v, want [%d 2 2]", got, len(wanted))
}
// the line once at its size, and at worst once more into blocks the
// pool let go, with 16 MiB for the decoder's window; grown a chunk at
// a time it was ~107 MB past the window
if most := uint64(len(wanted))*5/2 + 16<<20; alloc > most {
t.Fatalf("a scan of a packed %d-byte line allocated %d bytes, want at most %d", len(wanted), alloc, most)
}
}
// Blocks give back what they were given in order, across a block's end.
func TestBlocksKeepALineInOrder(t *testing.T) {
var g blocks
defer g.release()
var want []byte
for i, n := range []int{700 << 10, 900 << 10, 1, 1 << 20, 3} {
p := bytes.Repeat([]byte{byte('a' + i)}, n)
g.add(p)
want = append(want, p...)
}
if got := g.appendTo([]byte("x")); !bytes.Equal(got, append([]byte("x"), want...)) || g.n != len(want) {
t.Fatalf("blocks gave back %d bytes (n %d), not the %d given", len(got)-1, g.n, len(want))
}
}