Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
sessions: a line longer than the read buffer is gathered once, at its own size (#2515)
yetonea7e2c7235823
diff --git a/internal/sessions/scan_buffer_test.go b/internal/sessions/scan_buffer_test.goindex a81278fb3..ebaacfaed 100644--- a/internal/sessions/scan_buffer_test.go+++ b/internal/sessions/scan_buffer_test.go@@ -112,3 +112,57 @@ func TestReadersOfASmallFileTakeASmallBuffer(t *testing.T) {})}}++// 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)+ }+}diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.goindex f689dd23a..9e234b57b 100644--- a/internal/sessions/sessions.go+++ b/internal/sessions/sessions.go@@ -1383,6 +1383,15 @@ func scanAt(path string, off int64, head func([]byte) bool, fn func(b []byte, stseen = trueskip = skip || !head(chunk)}+ if pf, ok := f.(*os.File); ok && !skip && len(long) == 0 {+ // gathered in one buffer of the line's own size, not grown a+ // chunk at a time (~5 times the line allocated, #2515)+ if l := lineLen(pf, off, maxLine+r.Size()); l > maxLine+r.Size() {+ skip = true+ } else if l > cap(long) {+ long = make([]byte, 0, l)+ }+ }if !skip && len(long)+len(chunk) <= maxLine {long = append(long, chunk...)} else {@@ -1413,6 +1422,25 @@ func scanAt(path string, off int64, head func([]byte) bool, fn func(b []byte, st}}+// lineLen is the length, its newline included, of the line at off in f,+// read ahead without moving f: 0 when it has no end yet, and past most+// (but no further) for a longer one.+func lineLen(f *os.File, off int64, most int) int {+ buf := make([]byte, 64<<10)+ n := 0+ for n <= most {+ k, err := f.ReadAt(buf, off+int64(n))+ if i := bytes.IndexByte(buf[:k], '\n'); i >= 0 {+ return n + i + 1+ }+ n += k+ if err != nil {+ return 0+ }+ }+ return n+}+// scanBuffer is the read buffer a reader of f needs for what is left of it// from off: 1 MiB at most, and no more than that rest of a plain file.// Every line reader of a session file sizes its buffer by it. Every scan