Files
magpie/internal/sessions/call_owned_test.go
T
yetone 50606d8c0b 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).
2026-10-27 11:42:49 +08:00

58 lines
2.0 KiB
Go

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)
}
})
}
}