Skip to content

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

sessions: a call shard's frames are read into a buffer kept between shards (#3493)

yetoneb686a8dfec96
diff --git a/internal/sessions/call_cache.go b/internal/sessions/call_cache.go
index 2f37fd8b9..7a983a498 100644
--- a/internal/sessions/call_cache.go
+++ b/internal/sessions/call_cache.go
@@ -515,12 +515,41 @@ func callText(c *Call) [13]*string {
return [13]*string{&c.Agent, &c.Session, &c.ThreadID, &c.Model, &c.Requested, &c.Effort, &c.Cwd, &c.Upstream, &c.AccountID, &c.UserID, &c.Error, &c.ErrorText, &c.File}
}
+// frameBuffer is what loadCalls reads a shard's frames into, kept between
+// shards: a cold Requests build reads every one of ~15.6k, and a new
+// buffer, slice of patches and slice of strings for each frame came to
+// ~400 MB of it (#3493). Nothing read out of a frame points into these:
+// gob copies every string and byte slice out of its input, and a patch's
+// call is copied into the file's calls.
+type frameBuffer struct {
+ data []byte
+ f callFrame
+}
+
+var frameBuffers = sync.Pool{New: func() any { return new(frameBuffer) }}
+
+// frame is k's frame emptied for the next to be decoded into it. gob
+// leaves out a field of the zero value and leaves the field it decodes
+// into as it was, so a patch or string is cleared from the last frame,
+// not only cut off: a reused slice is decoded into in place.
+func (k *frameBuffer) frame() *callFrame {
+ clear(k.f.Patches)
+ clear(k.f.Strings)
+ k.f = callFrame{Strings: k.f.Strings[:0], Patches: k.f.Patches[:0]}
+ return &k.f
+}
+
func loadCalls(f file) *callFile {
in, err := os.Open(callCachePath(f.path))
if err != nil {
return nil
}
defer in.Close()
+ k := frameBuffers.Get().(*frameBuffer)
+ defer func() {
+ k.frame() // the pool holds no file's strings
+ frameBuffers.Put(k)
+ }()
var st *callFile
var calls []Call
msgs := map[string]int{}
@@ -538,12 +567,17 @@ func loadCalls(f file) *callFile {
if size == 0 || size > 256<<20 {
return nil
}
- data := make([]byte, int(size))
+ data := k.data[:0]
+ if cap(data) < int(size) {
+ data = make([]byte, int(size))
+ }
+ data = data[:size]
+ k.data = data
if _, err = io.ReadFull(in, data); err != nil || crc32.ChecksumIEEE(data) != binary.LittleEndian.Uint32(header[4:]) {
return nil
}
- var frame callFrame
- if decodeFrame(data, &frame) != nil || frame.State.Path != f.path || frame.State.Agent != f.agent {
+ frame := k.frame()
+ if decodeFrame(data, frame) != nil || frame.State.Path != f.path || frame.State.Agent != f.agent {
return nil
}
if st != nil && frame.State.Off < st.Off {
diff --git a/internal/sessions/call_cache_load_test.go b/internal/sessions/call_cache_load_test.go
index a6c9336b1..5666c5b28 100644
--- a/internal/sessions/call_cache_load_test.go
+++ b/internal/sessions/call_cache_load_test.go
@@ -39,3 +39,41 @@ func TestLoadedCallsAreSizedOnce(t *testing.T) {
t.Fatal("appended calls not read back")
}
}
+
+// Shards are read one after another into one kept frame, and gob leaves a
+// field of the zero value out: a call read after one that had a value
+// there has none, not the last shard's.
+func TestLoadedCallsKeepNothingOfTheLastShard(t *testing.T) {
+ d := setupCalls(t)
+ write := func(name string, c Call, began time.Time) string {
+ path := filepath.Join(d.claude, "projects", "p", name+".jsonl")
+ c.Msg = "msg-" + name
+ st := &callFile{Path: path, Agent: "claude", Calls: []Call{c}, Began: map[string]time.Time{}}
+ if !began.IsZero() {
+ st.Began[c.Msg] = began
+ }
+ writeCalls(st, 0, false)
+ return path
+ }
+ full := write("full", Call{Time: time.Unix(100, 0), Agent: "claude", Session: "s1", Model: "claude-opus-5-5",
+ Tokens: Tokens{Input: 50, Output: 7, CacheRead: 9}, Compaction: true}, time.Unix(90, 0))
+ bareCall := Call{Time: time.Unix(200, 0), Agent: "claude"}
+ empty := write("empty", bareCall, time.Time{})
+ for range 3 {
+ if got := loadCalls(file{path: full, agent: "claude"}); got == nil || got.Calls[0].Input != 50 || !got.Calls[0].Compaction {
+ t.Fatal("the full shard not read back")
+ }
+ got := loadCalls(file{path: empty, agent: "claude"})
+ if got == nil || len(got.Calls) != 1 {
+ t.Fatal("the bare shard not read back")
+ }
+ want := bareCall
+ want.Msg, want.File = "msg-empty", empty
+ if c := got.Calls[0]; c.Input != 0 || c.Output != 0 || c.CacheRead != 0 || c.Compaction || c.Session != "" || c.Model != "" || c.Msg != want.Msg || !c.Time.Equal(want.Time) {
+ t.Fatalf("read after the full shard: %+v", c)
+ }
+ if b, ok := got.Began["msg-empty"]; ok {
+ t.Fatalf("began %v kept from the last shard", b)
+ }
+ }
+}