Files
magpie/internal/sessions/parse_versions_test.go
T
yetone c07ca663cd sessions: a change to one agent's parser reads that agent's files again, not every file (#2278)
cacheVersion was raised 7 times since 10-28, three on 10-30 alone, each
for one agent's parser (Claude Desktop's entrypoint, Droid's skills,
OpenCode/Cline/dsh tool calls...). Each raise threw the whole kept index
away, so every user's first start after the update read their whole
history again: on the 52GB Codex + 13GB Claude Code history on mote,
~4 minutes of CPU and the fans on, the slowness users report after an
update.

Now each kept parse names its agent, the index keeps parseVersions as
they were (Parsers), and decodeCache drops only the parses of an agent
whose version moved. A change to one agent's parser raises
parseVersions[agent]; cacheVersion stays for changes to the index
itself. An index kept by an older magpie names no agents: its parses
stand while no version has moved, and refresh names them on the next
read (a copy, as readers hold the old one) and saves, so a later raise
keeps them too.

Not changed: calls-vN (call_cache.go) still names one version for every
agent's call shards; it was raised once since 10-01.

Run on mote (Go 1.26.8):
- gofmt clean; go vet ./internal/sessions ./internal/usage ./internal/gui
  for darwin and windows, ./internal/sessions ./internal/usage for linux:
  ok
- go test -count=1 ./internal/sessions ./internal/usage: ok
- go test -race -count=3 -run TestParseVersion ./internal/sessions: ok
- Broken by hand: decodeCache keeping every parse makes both
  TestParseVersionsReadOneAgentAgain and
  TestParseVersionRaiseReadsThatAgentAlone red; refresh not naming an
  old index's parses makes the latter red (read claude:3 codex:2).

Real history (the 52GB Codex + 13GB Claude Code copy, 16,619 kept
parses, index left by v0.1.2314), warm starts, 75s each:
- v0.1.2314: 10.7s CPU, 275MB peak
- this change, first start on the old index: 13.7s, 259MB; nothing
  read again, all 16,619 parses named and saved
- the same with parseVersions{"claude": 1}: 47.0s, 269MB, Claude Code's
  files alone read again (a cacheVersion raise: ~4:00)
- the next start: 13.2s, 261MB
- heatmap, requests, overview 0/7/30 and stats 0/30 answers
  byte-identical to v0.1.2314's in every run.
2026-10-31 11:53:10 +08:00

100 lines
2.3 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)
defer func(v map[string]int) { parseVersions = v }(parseVersions)
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
b, err := os.ReadFile(CachePath())
if err != nil {
t.Fatal(err)
}
var c cacheFile
if err := json.Unmarshal(b, &c); err != nil {
t.Fatal(err)
}
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
if b, err = json.Marshal(c); 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"
}