Files
magpie/internal/sessions/cache_binary_test.go
T
yetone ff6f6337f1 sessions: the kept parses are written binary, not JSON
The Sessions and Usage index (sessions.json, ~18k files on a long
history) was JSON. Every start decoded all of it through reflection,
and every save, which happens at most once a minute while a session is
live, marshalled it whole again. It is now a compact binary form
(cache_binary.go):
- A magic and a version header.
- Then length-prefixed records, varints for the numbers.
- Interned names, as the JSON path interned them.
- The rare nested parts (Claude Code's window, a Codex identity,
  StateMore) as JSON blobs.
Records are decoded in parallel.

A JSON index from an older magpie is still read. loadCache saves it
again at once, so it turns binary on the first start even when no
session changed; before, a save came only after a file changed.

A binary file is refused whole, never half read: when it is cut
anywhere, has a byte added, or is of another zone or version. That
means one full read again, as a refused JSON file meant before.
TestCacheBinaryKnowsEveryField fails when state, day or Tokens gains a
field that the binary form doesn't carry.

On the real acctp history, on mote (18,346 files):
- The file is 14.8 MB, down from 23.3 MB.
- Decoding takes 19 ms, down from 69 ms, and allocates 28 MB, down
  from 58 MB.
- Encoding takes 10 ms, down from 98 ms, and allocates 3.1 MB, down
  from 32.5 MB.
- The heap kept after decoding is the same: 24 MB.
- /api/sessions from a w94 build on the binary index and from a w92
  build (before) on the JSON index answer the same bytes (5000
  sessions). Neither rewrote the index.
- The page-open footprint peak was 152 to 214 MB on both builds at
  load 60 to 80. It is too noisy to claim a change.

Run:
- mica-run go test -count=1 -timeout 45m ./internal/sessions
  ./internal/usage ./internal/gui: all ok.
- GOOS=linux and GOOS=windows go vet ./internal/sessions
  ./internal/usage: ok. go vet ./internal/sessions: ok.
- gofmt -l: clean.
- TestCacheBinaryRefusesBadFiles cuts the file at every length, adds a
  byte, changes the zone and the version, and flips every byte; it
  checks that a bad file is never accepted and never panics.

Broken by hand:
- The decoder skipped Provider: TestCacheBinaryIsJSON went red.
- loadCache stopped re-saving a JSON index:
  TestCacheJSONRewrittenOnStart went red, the file still JSON.

At the pushed head (rebased on 500f39886): go test ./internal/sessions
./internal/testenv ok.

Not tried: Windows and Linux runs of the package; the mapping read is
shared code and unchanged there.
2026-11-16 02:58:09 +08:00

211 lines
7.3 KiB
Go

package sessions
import (
"bytes"
"encoding/json"
"fmt"
"os"
"reflect"
"slices"
"strings"
"testing"
"time"
)
// richCache is a kept index with every field of a state, a day and Tokens
// set somewhere, and the rare parts (Claude Code's window, a Codex
// identity, StateMore) on some files.
func richCache(t *testing.T) cacheFile {
t.Helper()
var claude *claudeUsageState
if err := json.Unmarshal([]byte(`[{"key":"message:1","m":{"request":"r1","usage":{"at":"2026-10-24T01:02:03Z","model":"m","input":3},"call":2}}]`), &claude); err != nil {
t.Fatal(err)
}
at := time.Date(2026, 10, 24, 1, 2, 3, 4, time.UTC)
east := time.Date(2026, 10, 24, 9, 0, 0, 0, time.FixedZone("", 8*3600))
tok := Tokens{Input: 1, Output: -2, CacheRead: 3, CacheWrite: 1 << 40, CacheWrite1h: 5}
full := &state{
Agent: "claude", Claude: claude, Head: digest{1, 2, 3}, HeadSize: 4096, CdRead: true, ContentHash: digest{9: 7},
Size: 1 << 33, Mod: -5, Off: 77, ID: "id-1", CodexIdentity: &CodexIdentity{ID: "c", Role: "subagent", ParentSession: "p", RootSession: "r"},
Cwd: "/w/é", Title: "a <b> & \"c\" \x00", Start: at, Last: east,
Models: named[Tokens]{{"a", tok}, {"b", Tokens{}}},
Days: named[*day]{
{"2026-10-23", nil},
{"2026-10-24", &day{Models: named[Tokens]{{"a", tok}}, Active: 99, Hours: hours{hourEntry(3, 5), hourEntry(23, 1)}, Prompts: 2, Replies: 3,
Tools: named[int]{{"Bash", 4}, {"Read", 1}}, Skills: named[int]{{"pdf", 1}}}},
{"2026-10-25", &day{}},
},
Entry: "claude-desktop", Msg: "msg_1", MsgModel: "m", MsgUse: tok, MsgDay: "2026-10-24", Model: "gpt-5.5", Total: &Tokens{Input: 8},
Provider: "openai",
StateMore: &StateMore{DBRevision: "rev", Named: "n", Convs: []string{"c1"}, Since: at, CodexSpawnNames: map[string]string{"a": "b"}},
}
files := map[string]*state{"/full": full, "/nil": nil, "/empty": {}, "/k\x00e\"y": {Agent: "codex", Size: 1}}
for i := range 3000 {
files[fmt.Sprintf("/s/%04d.jsonl", i)] = &state{Size: int64(i), Cwd: strings.Repeat("d", i%300), Title: "t", Start: at, Last: at.Add(time.Duration(i) * time.Second),
Models: named[Tokens]{{"m", Tokens{Input: i}}}, Days: named[*day]{{"2026-10-24", &day{Models: named[Tokens]{{"m", Tokens{Input: i}}}}}}}
}
return cacheFile{Version: cacheVersion, Zone: "z", Parsers: map[string]int{}, Files: files}
}
// What the binary form reads back is what the JSON form it replaced read
// back, field for field.
func TestCacheBinaryIsJSON(t *testing.T) {
unraised(t)
for _, c := range []cacheFile{
richCache(t),
{Version: cacheVersion, Zone: "z", Files: map[string]*state{}},
{Version: cacheVersion, Zone: "z"},
} {
j, err := json.Marshal(c)
if err != nil {
t.Fatal(err)
}
want, wantOK := decodeCache(j, "z")
var b bytes.Buffer
if err := encodeCache(&b, c); err != nil {
t.Fatal(err)
}
if !isBinaryCache(b.Bytes()) || b.Len() >= len(j) {
t.Fatalf("binary %d bytes, JSON %d", b.Len(), len(j))
}
got, ok := decodeCache(b.Bytes(), "z")
if ok != wantOK || !reflect.DeepEqual(got, want) {
gj, _ := json.Marshal(got)
wj, _ := json.Marshal(want)
t.Fatalf("%d files: got %v %.400s, want %v %.400s", len(c.Files), ok, gj, wantOK, wj)
}
if f := got["/full"]; f != nil && (f.Last.Location().String() != want["/full"].Last.Location().String() || !f.Last.Equal(want["/full"].Last)) {
t.Fatalf("last %v, want %v", f.Last, want["/full"].Last)
}
}
}
// A kept file cut anywhere, with a byte added, of another version or zone
// is refused, never half read and never a panic.
func TestCacheBinaryRefusesBadFiles(t *testing.T) {
unraised(t)
small := cacheFile{Version: cacheVersion, Zone: "z", Files: map[string]*state{}}
for k, v := range richCache(t).Files {
if !strings.HasPrefix(k, "/s/") {
small.Files[k] = v
}
}
var b bytes.Buffer
if err := encodeCache(&b, small); err != nil {
t.Fatal(err)
}
all := b.Bytes()
if files, ok := decodeCache(all, "z"); !ok || len(files) != len(small.Files) {
t.Fatalf("whole file: %d files, %v", len(files), ok)
}
for n := len(cacheMagic); n < len(all); n++ {
if files, ok := decodeCache(all[:n], "z"); ok {
t.Fatalf("cut at %d of %d: read %d files", n, len(all), len(files))
}
}
if _, ok := decodeCache(append(slices.Clip(all), 0), "z"); ok {
t.Fatal("a byte added was read")
}
if _, ok := decodeCache(all, "y"); ok {
t.Fatal("another zone was read")
}
small.Version--
b.Reset()
encodeCache(&b, small)
if _, ok := decodeCache(b.Bytes(), "z"); ok {
t.Fatal("another version was read")
}
// every byte flipped in turn: refused or read, never a panic
for i := len(cacheMagic); i < len(all); i++ {
bad := slices.Clone(all)
bad[i] ^= 0xff
decodeCache(bad, "z")
}
}
// Every field of a state the JSON form kept is in the binary form: a new
// field fails here until appendBinary and reader.state carry it.
func TestCacheBinaryKnowsEveryField(t *testing.T) {
fields := func(v any) []string {
var out []string
ty := reflect.TypeOf(v)
for i := range ty.NumField() {
out = append(out, ty.Field(i).Name)
}
return out
}
for _, c := range []struct {
v any
want []string
}{
{state{}, []string{"Agent", "Codex", "parsePath", "Claude", "Head", "HeadSize", "CdRead", "ContentHash", "Size", "Mod", "Off", "ID", "CodexIdentity", "Cwd", "Title", "Start", "Last", "Models", "Days", "Entry", "Msg", "MsgModel", "MsgUse", "MsgDay", "Model", "Total", "Provider", "StateMore"}},
{day{}, []string{"Models", "Active", "Hours", "Prompts", "Replies", "Tools", "Skills"}},
{Tokens{}, []string{"Input", "Output", "CacheRead", "CacheWrite", "CacheWrite1h"}},
} {
if got := fields(c.v); !slices.Equal(got, c.want) {
t.Errorf("%T has fields %v; the binary cache (cache_binary.go) carries %v: add the new one there and here", c.v, got, c.want)
}
}
}
// A JSON index an older magpie kept is read, and the next save writes it
// in the binary form.
func TestCacheJSONReadThenBinary(t *testing.T) {
unraised(t)
c := richCache(t)
j, _ := json.Marshal(c)
files, ok := decodeCache(j, "z")
if !ok || len(files) != len(c.Files) {
t.Fatalf("JSON: %d files, %v", len(files), ok)
}
var b bytes.Buffer
if err := encodeCache(&b, cacheFile{Version: cacheVersion, Zone: "z", Files: files}); err != nil {
t.Fatal(err)
}
again, ok := decodeCache(b.Bytes(), "z")
if !ok || !reflect.DeepEqual(again, files) {
t.Fatal("the JSON index written in binary reads back otherwise")
}
}
// savedIndex is the index on disk as it was saved, every file in it.
func savedIndex(t *testing.T) cacheFile {
t.Helper()
b, err := os.ReadFile(CachePath())
if err != nil {
t.Fatal(err)
}
c, ok := readCacheBinary(b)
if !isBinaryCache(b) || !ok {
t.Fatalf("cache file: %.200q", b)
}
return c
}
// An older magpie's JSON index, with no session changed since, is written
// again in the binary form on the first start: a save comes only when a
// file changes, so it stayed JSON, read the slow way on every start.
func TestCacheJSONRewrittenOnStart(t *testing.T) {
setup(t)
unraised(t)
List(0)
Saved()
c := savedIndex(t)
if len(c.Files) == 0 {
t.Fatal("the fixture keeps no parse: it tests nothing")
}
b, err := json.Marshal(c)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(CachePath(), b, 0o644); err != nil {
t.Fatal(err)
}
Reset()
List(0)
Saved()
if again := savedIndex(t); len(again.Files) != len(c.Files) {
t.Fatalf("rewritten with %d files, want %d", len(again.Files), len(c.Files))
}
}