sessions: a live file's calls read back from its shard are read on from, not copied
Requests evicts a file's expanded calls after each read, so every change
of a live session loads its calls from the disk shard (loadKept) and
then prepareCalls cloned them whole to read on from. The loaded copy
has no other reader, so the clone was a second full copy of the
session's calls on every poll, held at once with the first while the
chunk was built. readCalls and callsAlong now say when the calls came
from disk, and prepareCalls then copies only the reading state
(cloneState: the Claude window, Codex's maps, which can be a kept
continuation's) and keeps the calls, Msgs and Began. Calls from the
cache are still copied.
Measured on real history (rss-home, owner's ~/.claude and ~/.codex,
read-only), ab5.sh, 90s warm-up then 8 GUI polls 15s apart, alloc
between the heaps:
before (v0.1.1673 head): callFile.clone 160 MB, prepareCalls 201 MB;
footprint 264-412 MB, Requests 287-722 ms
after: clone gone from the profile; footprint 201-367 MB, Requests
144-472 ms
The history is live, so loadCalls itself varied run to run (741 MB vs
172 MB); the clone's share is the part this change removes.
Run at this head (2e165fe9d + this change):
go test -timeout 45m ./internal/sessions/ ok
go test -timeout 45m ./internal/usage/ ./internal/usageapi/ ok
GOOS=linux/windows go vet -tags nogui ./internal/sessions/ ok
gofmt -l internal/sessions clean
New TestCallsFromTheShardAreNotCopied (claude, codex): red with the
owned branch turned off by hand ("calls read from the shard were
copied to be read on from"), green with it.
Not tried: a GUI run (no GUI change).
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user