Files
magpie/internal/sessions/call_pack_test.go
T
yetone de58a3879b sessions: a cold read packs idle Claude sessions' calls into one file
shangyuan0818 on X: on Windows, Usage -> Sessions took 18 to 25 s with
long Claude Code sessions. The cause: a cold read wrote a calls-v15 .gob
shard and a .rev for every Claude session, about 21k files for 12k
sessions, and Windows Defender scans each file as it is closed.

Sessions not changed within the hour are now written into one
calls-v15/*.pack per pass (MPCALLS1 magic, the frames, a gob index of
path/offset/count, length, CRC32, magic). It is written to a temp file
and renamed, and flushed at the end of a pass, at checkpoints, in Saved
and every 32 MB. A session's own shard is read before the pack, so a
session that grows gets its own shard and its lines are appended there
as before. A bad pack means the source is read again. The hourly prune
sweeps packs no session points at. Working sessions keep a shard and a
.rev as before.

Measured on xiaomi-gpu (Windows 11, Defender on), fixture of 12,016
Claude session files, no magpie cache, first /api/sessions reply, the
runs alternating old and new:
  origin/main build: 31.67 s, 25.26 s, 20.74 s
  this change:        5.92 s,  5.85 s,  5.65 s
  restart with the pack written: 1.97 s and 2.16 s; then 0.25 to 0.31 s.
  The cache held 1 .pack and 4 .gob.

Tests (call_pack_test.go): TestColdSessionsReadWritesOnePack,
TestPackedCallsServeRequests, TestPackedSessionGrows. With the pack
branch turned off by hand the first fails (.gob:63 .rev:62) and the
second finds 0 packs.

Run at 4ace48feb (this change on origin/main 2ab2f6913):
- gofmt -l: clean.
- go test ./... -timeout 45m -count=1 on mote: 101 packages ok
  (internal/sessions 154 s), 3 red, none in code this touches, each
  filed: internal/cli TestHealthcheckFindsTheRunningGatewayWithoutMagpieAddr,
  TestUpdateRestartsTheGatewayItFindsWithoutMagpieAddr and
  TestUpdateFindsAMagpieThatWroteNoRecord (#6223: another session's ssh
  forward listened on mote's 127.0.0.1:3425); internal/gateway
  TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes (#7117) and
  TestLaneLimitRoutes (#5150); internal/gui
  TestAppReplacesItsOlderGatewayOnceIdle (#6297) and
  TestSettingsSaveKeepsWhatItDoesNotSend (#7127, red since f6a538ece).
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go
  vet ./..., go build, GOOS=linux go build -tags nogui, GOOS=windows go
  build: clean.
- internal/gui/tests/sessions-*.test.cjs (46 files): 322 of 322 in
  chromium, 322 of 322 in webkit.
- wsl-run go test ./internal/sessions: ok (25.3 s), at the base before
  the rebase.
- win-run go test ./internal/sessions: 4 reds, the same on origin/main
  07d19510f and filed as #6071 (TestAgySessions, TestAgyRowOnly,
  TestAntigravityAppFolder, TestCCListingKeepsItsNames). go vet clean.

Rebased onto fcbc1a2d0 (three gateway commits, none in
internal/sessions or its doc). At the pushed head: go test
./internal/sessions ok (209 s); go vet of it on darwin, linux (nogui)
and windows clean; go build clean.

Not tried: a real user's profile; the timings use a generated fixture
the size of the report.
2026-11-19 01:22:40 +08:00

210 lines
5.9 KiB
Go

package sessions
import (
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"testing"
"time"
)
// packFixture writes n Claude Code sessions, the first `working` of them
// changed now (their revision windows kept) and the rest two days ago.
func packFixture(t *testing.T, claude string, n, working int) []string {
t.Helper()
var paths []string
for s := 0; s < n; s++ {
path := filepath.Join(claude, "projects", "-work-pack", fmt.Sprintf("44444444-2222-3333-4444-%012d.jsonl", s))
var lines []string
for i := 0; i < 6; i++ {
lines = append(lines, strings.TrimSuffix(ccMsg(fmt.Sprintf("msg_%d_%d", s, i), fmt.Sprintf("2026-09-21T10:%02d:00Z", i), 100+i, 20+i), "\n"))
}
writeLines(t, path, lines...)
if s >= working {
old := time.Now().Add(-48 * time.Hour)
if err := os.Chtimes(path, old, old); err != nil {
t.Fatal(err)
}
}
paths = append(paths, path)
}
return paths
}
// packDirFiles counts the files in the calls cache, by kind.
func packDirFiles(t *testing.T) map[string]int {
t.Helper()
out := map[string]int{}
entries, _ := os.ReadDir(callCacheDir())
for _, e := range entries {
out[filepath.Ext(e.Name())]++
}
return out
}
// TestColdSessionsReadWritesOnePack: a cold Sessions read of a long
// Claude Code history made a shard and a revisions file for each session,
// each made, closed and renamed: ~21,000 files for 12,000 sessions, and on
// Windows Defender scanned each one as it closed, 15 s of the page's 19 s.
// The idle sessions' calls read along go to one pack a pass; one being
// written keeps its shard, which its new lines are appended to.
func TestColdSessionsReadWritesOnePack(t *testing.T) {
claude, codex := setup(t)
os.RemoveAll(codex) // Codex's calls are kept as before
paths := packFixture(t, claude, 60, 5)
Reset()
List(0)
Saved()
got := packDirFiles(t)
if got[".pack"] != 1 || got[".gob"] > 10 {
t.Fatalf("a cold read of 55 idle sessions and 5 being written wrote %v to the calls cache, want one .pack and shards for the 5 (and testdata's 3)", got)
}
for i, p := range paths {
_, err := os.Stat(callCachePath(p))
if working := i < 5; working != (err == nil) {
t.Fatalf("session %d (being written %v) has a shard of its own: %v", i, working, err == nil)
}
if _, err := os.Stat(callRevisionsPath(p)); err == nil && i >= 5 {
t.Fatalf("idle session %d has a revisions file", i)
}
}
}
// TestPackedCallsServeRequests: the calls a Sessions read packed are what
// Requests reads, after a restart too, without reading a session again;
// a pack cut off is as if it were not there, and its sessions are read
// again from their files.
func TestPackedCallsServeRequests(t *testing.T) {
claude, _ := setup(t)
paths := packFixture(t, claude, 20, 3)
readAll := func() map[string][]Call {
out := map[string][]Call{}
for _, s := range CallSources() {
if s.Agent == "claude" && strings.Contains(s.Path, "-work-pack") {
out[s.Path] = ReadCallSource(s)
}
}
return out
}
var mu sync.Mutex
scanned := map[string]int{}
scanHook = func(p string, _ int64) {
if strings.Contains(p, "-work-pack") {
mu.Lock()
scanned[p]++
mu.Unlock()
}
}
t.Cleanup(func() { scanHook = nil })
scans := func() int {
mu.Lock()
defer mu.Unlock()
n := 0
for _, c := range scanned {
n += c
}
scanned = map[string]int{}
return n
}
// the reference: calls read on their own, no cache
Reset()
want := readAll()
if len(want) != len(paths) {
t.Fatalf("%d sources read, want %d", len(want), len(paths))
}
for p, cs := range want {
if len(cs) != 6 {
t.Fatalf("%s: %d calls, want 6", p, len(cs))
}
}
Reset()
os.Remove(CachePath())
os.RemoveAll(callCacheDir())
List(0) // packs the calls it reads along
Saved()
scans()
if got := readAll(); !reflect.DeepEqual(got, want) {
t.Fatalf("calls from the live pack differ from a read of their own")
}
if n := scans(); n != 0 {
t.Fatalf("the packed calls were read again: %d scans", n)
}
Reset() // a restart: the pack is read from disk
if got := readAll(); !reflect.DeepEqual(got, want) {
t.Fatalf("calls from the pack on disk differ from a read of their own")
}
if n := scans(); n != 0 {
t.Fatalf("the packed calls were read again after a restart: %d scans", n)
}
// cut off: read again, and right
Reset()
os.Remove(CachePath())
for p := range want {
os.Remove(callCachePath(p))
os.Remove(callRevisionsPath(p))
}
packs, _ := filepath.Glob(filepath.Join(callCacheDir(), "*.pack"))
if len(packs) != 1 {
t.Fatalf("%d packs, want 1", len(packs))
}
b, err := os.ReadFile(packs[0])
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(packs[0], b[:len(b)/2], 0600); err != nil {
t.Fatal(err)
}
Reset()
if got := readAll(); !reflect.DeepEqual(got, want) {
t.Fatalf("calls with their pack cut off differ from a read of their own")
}
if n := scans(); n < len(paths) {
t.Fatalf("with the pack cut off only %d of %d sessions were read again", n, len(paths))
}
}
// TestPackedSessionGrows: a packed session that grows is read on, and its
// calls written whole to a shard of their own, which is read before the
// pack from then on.
func TestPackedSessionGrows(t *testing.T) {
claude, _ := setup(t)
paths := packFixture(t, claude, 4, 0)
Reset()
List(0)
Saved()
path := paths[0]
f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
t.Fatal(err)
}
f.WriteString(ccMsg("msg_grown", "2026-09-21T11:00:00Z", 500, 50))
f.Close()
var calls []Call
for _, s := range CallSources() {
if s.Path == path {
calls = ReadCallSource(s)
}
}
if len(calls) != 7 {
t.Fatalf("%d calls after the session grew, want 7", len(calls))
}
if _, err := os.Stat(callCachePath(path)); err != nil {
t.Fatalf("the grown session's calls have no shard of their own: %v", err)
}
Reset()
for _, s := range CallSources() {
if s.Path == path {
calls = ReadCallSource(s)
}
}
if len(calls) != 7 || calls[6].Msg != "msg_grown" {
t.Fatalf("after a restart: %d calls, want 7 ending in msg_grown", len(calls))
}
}