Files
magpie/internal/sessions/call_versions_test.go
T
yetone 30f29564eb sessions: a change to one agent's calls reads that agent's files again, not every shard (#2278)
callCacheVersion named every shard, so raising it for a fix to one
agent's calls threw away every agent's calls and re-read the whole
history on the next start: on a real history of ~4,700 transcripts
that is minutes of CPU and the ~310MB peak, for a change to Claude
alone.

Each agent now has its own number in callParseVersions. A shard frame
records the number its agent had when it was written (callDiskState
.Parser), and loadCalls drops a frame whose number is not the current
one, so only that agent's files are read again. CallCacheVersion()
adds the raised numbers, so the usage caches built on calls
(requestDiskBinary, heat_disk) still start again when any agent's
calls change. callCacheVersion stays for a change to every agent or to
the shards' form.

Existing shards are kept: the map is empty, gob leaves a zero Parser
out, and CallCacheVersion() is still "calls-v15".

Run on mote (zone worktree at this head):
- go vet ./internal/sessions ./internal/usage for darwin, linux and
  windows: ok. go build ./...: ok.
- go test ./internal/sessions ./internal/usage: ok.
- go test -race -count=3 -run 'TestCallParseVersionRaise|Calls|Shard|Frame'
  ./internal/sessions: ok.
- TestCallParseVersionRaiseReadsThatAgentAlone: a Claude transcript and
  a Codex rollout; raising claude scans the Claude file alone, with the
  same calls, and the next start scans nothing.
- Broken by hand (the Parser check in loadCalls removed): red at
  call_versions_test.go:67, "scanned map[], want Claude's file alone".

Not changed: internal/gui's TestCoveredWindowStaysUp is killed at 30s
on mote, as in #4125.
2026-10-31 12:34:40 +08:00

78 lines
2.3 KiB
Go

package sessions
import (
"path/filepath"
"reflect"
"strings"
"sync"
"testing"
)
// TestCallParseVersionRaiseReadsThatAgentAlone: raising callCacheVersion
// for a change to one agent's calls read every agent's history again after
// the update. Raising that agent's callParseVersions reads its files alone,
// once, and keeps every other agent's shards.
func TestCallParseVersionRaiseReadsThatAgentAlone(t *testing.T) {
d := setupCalls(t)
cc := filepath.Join(d.claude, "projects", "-work-app", "sess1.jsonl")
writeLines(t, cc, claudeMsg("m1", "claude-opus-5-5", 100, 20, 5000, 1000, 1), claudeMsg("m2", "claude-opus-5-5", 7, 8, 0, 0, 2))
cx := filepath.Join(d.codex, "sessions", "2026", "09", "20", "rollout-2026-09-20T10-00-00-0190aaaa-1111-7222-8333-444455556666.jsonl")
writeLines(t, cx, append(cxTurnLines(0, "t", "gpt-6-astra", "high"), tokenCountLine(1, cxUse(100, 0, 0, 1, 0), cxUse(100, 0, 0, 1, 0)))...)
var mu sync.Mutex
scans := map[string]int{}
scanHook = func(p string, off int64) {
mu.Lock()
scans[p]++
mu.Unlock()
}
t.Cleanup(func() { scanHook = nil })
counted := func() map[string]int {
mu.Lock()
defer mu.Unlock()
out := scans
scans = map[string]int{}
return out
}
read := func() map[string][]Call {
out := map[string][]Call{}
for _, s := range CallSources() {
out[s.Path] = ReadCallSource(s)
}
return out
}
want := read()
if len(want[cc]) != 2 || len(want[cx]) != 1 {
t.Fatalf("calls read: %d Claude, %d Codex", len(want[cc]), len(want[cx]))
}
if n := counted(); n[cc] != 1 || n[cx] != 1 {
t.Fatalf("first read scanned %v", n)
}
Reset()
read()
if n := counted(); len(n) != 0 {
t.Fatalf("a start with every shard current scanned %v", n)
}
unraised := CallCacheVersion()
callParseVersions = map[string]int{"claude": 1}
t.Cleanup(func() { callParseVersions = map[string]int{} })
if CallCacheVersion() == unraised || !strings.Contains(CallCacheVersion(), "claude=1") {
t.Fatalf("raised Claude's calls, and what is made of them is still %q", CallCacheVersion())
}
Reset()
got := read()
if n := counted(); n[cc] != 1 || n[cx] != 0 {
t.Fatalf("Claude's calls raised: scanned %v, want Claude's file alone", n)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("calls after the raise differ")
}
Reset()
read()
if n := counted(); len(n) != 0 {
t.Fatalf("the start after the raise scanned %v", n)
}
}