diff --git a/internal/sessions/calls.go b/internal/sessions/calls.go index f3487443c..cd2631f05 100644 --- a/internal/sessions/calls.go +++ b/internal/sessions/calls.go @@ -72,6 +72,9 @@ type callFile struct { Claude *claudeUsageState ContentHash string dirty map[int]bool + // readSize is the size of the file being read (prepareCalls), for + // growCalls + readSize int64 // bigFrame: read from a frame of more than framePatches (loadCalls) bigFrame bool Size int64 @@ -409,9 +412,28 @@ func prepareCalls(f file, old *callFile, owned bool) *callFile { } st.dirty = map[int]bool{} st.Head, st.HeadSize, st.Path = hashHead(head), len(head), f.path + st.readSize = f.size return st } +// growCalls makes room in st.Calls, when it is full, for the calls the +// rest of the file holds at the rate read so far: append grows a long +// list by a quarter at a time, and a Claude session's calls were copied +// into ~5 lists their size on the way (616 MB of a cold start's reading +// on mote, ~13% of all it allocated). The room is at most twice the +// calls so far, as append's own doubling of a short list. +func growCalls(st *callFile) { + n := len(st.Calls) + if n < cap(st.Calls) || n < 256 || st.End <= 0 || st.readSize <= st.End { + return + } + // what the rest is likely to hold, and an eighth more + want := min(int(int64(n)*st.readSize/st.End)*9/8, 2*n) + if want > n+n/4+1 { + st.Calls = slices.Grow(st.Calls, want-n) + } +} + func (st *callFile) clone() *callFile { c := st.cloneState() c.Calls = append([]Call(nil), st.Calls...) @@ -967,6 +989,7 @@ func claudeCall(st *callFile, b []byte, l *ccCall) { if c.Error == "" { c.Compact, st.CompactMs = st.CompactMs, 0 } + growCalls(st) st.Calls = append(st.Calls, c) } diff --git a/internal/sessions/claude_reuse_test.go b/internal/sessions/claude_reuse_test.go index c633c9f0d..8a03673c9 100644 --- a/internal/sessions/claude_reuse_test.go +++ b/internal/sessions/claude_reuse_test.go @@ -143,3 +143,49 @@ func TestClaudeMessageLookupAllocations(t *testing.T) { t.Fatalf("a message with only its request: %q", q.key) } } + +// TestClaudeCallsListGrownToTheFile: a Claude session's calls list grows +// to the rate the file was read at so far, not by append's quarters: the +// lists made on the way to 20k calls hold ~2.8 times them, ~5.2 times by +// append, and the last ends within an eighth or so of its length. A file +// whose second half holds fewer calls ends at no more than twice them. +func TestClaudeCallsListGrownToTheFile(t *testing.T) { + at := time.Date(2026, 9, 20, 12, 0, 0, 0, time.UTC) + read := func(n int, sparse func(i int) int) (made int, st *callFile) { + // made: the calls the lists past 256 had room for + var lines [][]byte + size := int64(0) + for i := range n { + b := append(reuseLine(i, at), bytes.Repeat([]byte(" "), sparse(i))...) + lines = append(lines, b) + size += int64(len(b)) + 1 + } + st = &callFile{Agent: "claude", readSize: size, Msgs: map[string]int{}, dirty: map[int]bool{}} + end, last := int64(0), -1 + for _, b := range lines { + st.At, end = end, end+int64(len(b))+1 + st.End = end + claudeCallLine(st, b) + if c := cap(st.Calls); c != last && len(st.Calls) > 256 { + made += c + } + last = cap(st.Calls) + } + return made, st + } + n := 20000 + made, st := read(n, func(int) int { return 0 }) + if len(st.Calls) != n { + t.Fatalf("%d calls of %d", len(st.Calls), n) + } + t.Logf("even: lists of %d calls past 256, cap %d for %d", made, cap(st.Calls), n) + if made > 7*n/2 || cap(st.Calls) > n*5/4 { + t.Fatalf("lists of %d calls were made, the last of %d, for %d calls", made, cap(st.Calls), n) + } + // the second half's lines ten times as long + made, st = read(n, func(i int) int { return 9 * 600 * (i * 2 / n) }) + t.Logf("sparse end: lists of %d calls, cap %d for %d", made, cap(st.Calls), n) + if len(st.Calls) != n || cap(st.Calls) > 2*n { + t.Fatalf("%d calls in a list of %d", len(st.Calls), cap(st.Calls)) + } +}