Skip to content

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

sessions: a live file's calls read back from its shard are read on from, not copied

yetone50606d8c0b4c
diff --git a/internal/sessions/call_cache.go b/internal/sessions/call_cache.go
index 0c3ee5b7c..6479ab004 100644
--- a/internal/sessions/call_cache.go
+++ b/internal/sessions/call_cache.go
@@ -265,8 +265,10 @@ func readCalls(f file) *callFile {
callsMu.Lock()
old, generation := callCache[f.path], callGeneration
callsMu.Unlock()
+ owned := false
if old == nil {
old = loadKept(f)
+ owned = true
}
if old != nil && old.Size == f.size && old.Mod == f.mod.UnixNano() {
callsMu.Lock()
@@ -325,7 +327,7 @@ func readCalls(f file) *callFile {
if r, ok := lineCallReaders[f.agent]; ok && r.kept != nil && old != nil && !r.kept(old) {
old = nil
}
- st := prepareCalls(f, withCallRevisions(old))
+ st := prepareCalls(f, withCallRevisions(old), owned)
continued := old != nil && st.Off == old.Off && st.Off > 0
start := 0
if continued {
@@ -398,14 +400,16 @@ func callsAlong(f file) *callsRide {
callsMu.Lock()
old, generation := callCache[f.path], callGeneration
callsMu.Unlock()
+ owned := false
if old == nil {
old = loadKept(f)
+ owned = true
}
if old != nil && old.Size == f.size && old.Mod == f.mod.UnixNano() {
lock.Unlock()
return nil
}
- return &callsRide{f: f, st: prepareCalls(f, old), generation: generation, lock: lock}
+ return &callsRide{f: f, st: prepareCalls(f, old, owned), generation: generation, lock: lock}
}
// ride is a scan's head and line for both readers: each head sees every
diff --git a/internal/sessions/call_owned_test.go b/internal/sessions/call_owned_test.go
new file mode 100644
index 000000000..1c33648ef
--- /dev/null
+++ b/internal/sessions/call_owned_test.go
@@ -0,0 +1,57 @@
+package sessions
+
+import (
+ "path/filepath"
+ "testing"
+ "unsafe"
+)
+
+// A live file's calls read back from its shard are read on from as they
+// are: no other reader has them, and a long session's were copied whole
+// on every change of it. Calls a reader shares (the cache's) are copied.
+func TestCallsFromTheShardAreNotCopied(t *testing.T) {
+ for _, agent := range []string{"claude", "codex"} {
+ t.Run(agent, func(t *testing.T) {
+ d := setupCalls(t)
+ var path, extra string
+ if agent == "claude" {
+ path = filepath.Join(d.claude, "projects", "fixture", "session.jsonl")
+ writeLines(t, path, claudeMsg("one", "claude-opus-5-5", 10, 2, 30, 4, 1))
+ extra = claudeMsg("two", "claude-opus-5-5", 10, 2, 30, 4, 2)
+ } else {
+ path = filepath.Join(d.codex, "sessions", "rollout-2026-09-20T10-00-00-0190aaaa-1111-7222-8333-444455556666.jsonl")
+ writeLines(t, path, append(cxTurnLines(0, "t1", "gpt-6-astra", "high"), tokenCountLine(1, cxUse(100, 40, 0, 10, 2), cxUse(100, 40, 0, 10, 2)))...)
+ extra = tokenCountLine(2, cxUse(200, 80, 0, 20, 4), cxUse(100, 40, 0, 10, 2))
+ }
+ source := func() CallSource {
+ t.Helper()
+ for _, s := range CallSources() {
+ if s.Path == path {
+ return s
+ }
+ }
+ t.Fatal("source missing")
+ return CallSource{}
+ }
+ if cs := ReadCallSource(source()); len(cs) != 1 {
+ t.Fatal(cs)
+ }
+ appendText(t, path, extra+"\n")
+ s := source()
+ f := file{path: s.Path, key: s.Key, agent: s.Agent, size: s.Size, mod: s.Modified}
+ kept := loadKept(f)
+ if kept == nil || len(kept.Calls) != 1 {
+ t.Fatalf("the shard holds %v", kept)
+ }
+ if st := prepareCalls(f, withCallRevisions(kept), true); unsafe.SliceData(st.Calls) != unsafe.SliceData(kept.Calls) {
+ t.Fatal("calls read from the shard were copied to be read on from")
+ }
+ if st := prepareCalls(f, withCallRevisions(kept), false); unsafe.SliceData(st.Calls) == unsafe.SliceData(kept.Calls) {
+ t.Fatal("calls another reader shares were read on from in place")
+ }
+ if cs := ReadCallSource(s); len(cs) != 2 {
+ t.Fatal(cs)
+ }
+ })
+ }
+}
diff --git a/internal/sessions/calls.go b/internal/sessions/calls.go
index 3574c9d37..c41a705a3 100644
--- a/internal/sessions/calls.go
+++ b/internal/sessions/calls.go
@@ -369,7 +369,9 @@ func headOf(path string) string {
}
// prepareCalls clones the continuation so a published index is never changed.
-func prepareCalls(f file, old *callFile) *callFile {
+// owned: old was read from disk for this read alone (loadKept), so its
+// calls are taken as they are rather than copied for st.
+func prepareCalls(f file, old *callFile, owned bool) *callFile {
// The disk shard stores final calls, not a second copy of every usage
// observation. Rebuild continuation only when a cold source grows.
if f.agent == "codex" && old != nil && (old.CX == nil || old.CX.Usage == nil) {
@@ -384,7 +386,11 @@ func prepareCalls(f file, old *callFile) *callFile {
head := headOf(f.path)
var st *callFile
if old != nil && !packed(f.path) && f.size >= old.Size && old.Off <= f.size && sameHead(head, old.Head, old.HeadSize) && old.ContentHash != "" && prefixHash(f.path, old.Off) == old.ContentHash {
- st = old.clone()
+ if owned {
+ st = old.cloneState()
+ } else {
+ st = old.clone()
+ }
} else {
st = &callFile{Agent: f.agent}
if f.agent == "codex" {
@@ -403,10 +409,21 @@ func prepareCalls(f file, old *callFile) *callFile {
}
func (st *callFile) clone() *callFile {
- c := *st
+ c := st.cloneState()
c.Calls = append([]Call(nil), st.Calls...)
- c.Claude = st.Claude.clone()
c.Msgs, c.Began, c.Strs = maps.Clone(st.Msgs), maps.Clone(st.Began), maps.Clone(st.Strs)
+ return c
+}
+
+// cloneState is a copy of st's reading state that shares its calls, and
+// their Msgs, Began and Strs. The state may be a kept continuation's
+// (restoreCallContinuation), so it is copied; the calls of a file just
+// read from disk have no other reader, and copying them on every change
+// of a live session was 160 MB of eight Requests polls (of 3.9 GB) on
+// real history.
+func (st *callFile) cloneState() *callFile {
+ c := *st
+ c.Claude = st.Claude.clone()
if st.CX != nil {
r := *st.CX
r.Turns = maps.Clone(r.Turns)