Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

sessions: a scan's read buffer is the size of what is left of the file, 1 MiB at most (#2776)

yetoned7e02ae08820
diff --git a/internal/sessions/scan_buffer_test.go b/internal/sessions/scan_buffer_test.go
new file mode 100644
index 000000000..8844a66f1
--- /dev/null
+++ b/internal/sessions/scan_buffer_test.go
@@ -0,0 +1,63 @@
+package sessions
+
+import (
+ "os"
+ "path/filepath"
+ "runtime"
+ "strings"
+ "testing"
+)
+
+// 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))
+ }
+}
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index 1807a354e..3fd94942b 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -1371,7 +1371,7 @@ func scanAt(path string, off int64, head func([]byte) bool, fn func(b []byte, st
}
return off, err
}
- r := bufio.NewReaderSize(f, 1<<20)
+ r := bufio.NewReaderSize(f, scanBuffer(f, off))
var long []byte
var n int64 // bytes of the line so far
skip, seen := false, false
@@ -1413,6 +1413,22 @@ func scanAt(path string, off int64, head func([]byte) bool, fn func(b []byte, st
}
}
+// scanBuffer is the read buffer scanAt needs for what is left of f from off:
+// 1 MiB at most, and no more than that rest of a plain file. Every scan
+// took 1 MiB, and a start's read of ~4,600 mostly small session files
+// (each read again from where it was left) allocated ~9 GB of them, which
+// the GC's heap target followed up (#2776). A line longer than the buffer
+// (a file grown since) is still read whole, through scanAt's long line.
+func scanBuffer(f io.Reader, off int64) int {
+ const most, least = 1 << 20, 16 << 10
+ if pf, ok := f.(*os.File); ok {
+ if info, err := pf.Stat(); err == nil {
+ return int(min(most, max(least, info.Size()-off+1)))
+ }
+ }
+ return most
+}
+
// packed says whether a session file is compressed: a Codex rollout the
// Codex app has packed (.zst), a dsh session (.zstd). A packed rollout is
// written whole, so it is read whole again whenever it changes.