Files
magpie/internal/sessions/parse_versions_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

94 lines
2.2 KiB
Go

package sessions
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"sync"
"testing"
)
// The index an older magpie kept names no agent: its parses stand on the
// update, are named on the next read, and a later raise of Claude Code's
// parseVersions then reads Claude Code's files alone again, not Codex's
// (#2278: each raise of cacheVersion read a whole history again).
func TestParseVersionRaiseReadsThatAgentAlone(t *testing.T) {
setup(t)
unraised(t)
var mu sync.Mutex
read := map[string]int{}
scanHook = func(p string, off int64) {
mu.Lock()
read[p]++
mu.Unlock()
}
t.Cleanup(func() { scanHook = nil })
agents := func() map[string]int {
mu.Lock()
defer mu.Unlock()
n := map[string]int{}
for p := range read {
n[agentOf(p)]++
}
read = map[string]int{}
return n
}
List(0)
Saved()
first := agents()
if first["claude"] == 0 || first["codex"] == 0 {
t.Fatalf("the fixture reads %v: it tests nothing", first)
}
// as an older magpie left it: no agent named, no versions
c := savedIndex(t)
for _, s := range c.Files {
if s != nil {
if s.Agent == "" {
t.Fatalf("a parse kept without its agent: %+v", s)
}
s.Agent = ""
}
}
c.Parsers = nil
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 n := agents(); n["claude"]+n["codex"] != 0 {
t.Fatalf("the update read %v again", n)
}
parseVersions = map[string]int{"claude": 1}
Reset()
List(0)
Saved()
if n := agents(); n["codex"] != 0 || n["claude"] != first["claude"] {
t.Fatalf("Claude Code's version raised: read %v, want claude:%d alone", n, first["claude"])
}
// and kept at the new version: the next start reads nothing
Reset()
List(0)
if n := agents(); n["claude"]+n["codex"] != 0 {
t.Fatalf("the start after read %v again", n)
}
}
// agentOf is the agent whose session folder holds p.
func agentOf(p string) string {
switch {
case strings.HasPrefix(p, os.Getenv("CLAUDE_CONFIG_DIR")+string(filepath.Separator)):
return "claude"
case strings.HasPrefix(p, os.Getenv("CODEX_HOME")+string(filepath.Separator)):
return "codex"
}
return "other"
}