A long history's kept parses held 28MB live after a start's decode of
sessions.json (15,624 files). 15,345 of them have exactly one day, and
that day's model map is the file's own, so every such file held two
equal maps of ~700B each. decodeCache's share now points the day at the
file's map when they are equal.
A kept state is never written: use, unuse and dropUse are the only
writers of Models, and every parser (sessions.go, calls.go, calls_dsh,
dsh, droid, grok, kimi, cline) writes a clone, which copies the two
maps apart.
Run, at this head on 42ab93551:
- the real sessions.json (sbpairs, 22.8MB): live heap after decode
28MB -> 21MB, 3 of 3.
- StatsAt(3650) over the real sessions in a sandbox HOME, digest of
the 96 days before 10-24: a21dc1d1... with this change (twice, the
second from the cache it wrote) and with the sharing turned off
(twice): the same.
- TestDecodeCacheSharesOneDaysModels: red with the sharing removed
("holds its models twice") and red with clone keeping the shared map
("the kept state was written").
- go test -count=1 ./internal/sessions ./internal/usage ./internal/gui
ok; go test -race ./internal/sessions ok; GOOS=linux and
GOOS=windows go vet -tags nogui on the three ok; gofmt -l clean.
Not tried: a full go test ./... (one suite per machine).
161 lines
6.7 KiB
Go
161 lines
6.7 KiB
Go
package sessions
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"fmt"
|
||
"reflect"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
"unsafe"
|
||
)
|
||
|
||
// The kept file decoded on a few goroutines is what one json.Unmarshal of
|
||
// it was, and is refused where that was.
|
||
func TestDecodeCacheIsUnmarshal(t *testing.T) {
|
||
unmarshal := func(b []byte, zone string) (map[string]*state, bool) {
|
||
var c cacheFile
|
||
if json.Unmarshal(b, &c) != nil || c.Version != cacheVersion || c.Zone != zone || c.Files == nil {
|
||
return nil, false
|
||
}
|
||
return c.Files, true
|
||
}
|
||
var many strings.Builder
|
||
fmt.Fprintf(&many, `{"version":%d,"zone":"z","files":{`, cacheVersion)
|
||
for i := range 3000 {
|
||
if i > 0 {
|
||
many.WriteString(",")
|
||
}
|
||
fmt.Fprintf(&many, `"/s/%d.jsonl":{"size":%d,"mod":%d,"title":"té %d","models":{"m":{"input":%d}},"claude":[{"key":"message:%d","m":{"request":"r%d"}}],"days":{"2026-10-%02d":{"models":{"m":{"output":%d}}}}}`, i, i, i*7, i, i, i, i, 1+i%28, i)
|
||
}
|
||
many.WriteString(`}}`)
|
||
v := cacheVersion
|
||
cases := map[string]string{
|
||
"many": many.String(),
|
||
"escaped keys": fmt.Sprintf(`{"version":%d,"zone":"z","files":{"/a\"b\\cé.jsonl":{"size":1},"/n":null}}`, v),
|
||
"twice": fmt.Sprintf(`{"zone":"z","files":{"/a":{"size":1},"/a":{"size":2}},"version":%d}`, v),
|
||
"empty": fmt.Sprintf(`{"version":%d,"zone":"z","files":{}}`, v),
|
||
"no files": fmt.Sprintf(`{"version":%d,"zone":"z","files":null}`, v),
|
||
"old version": fmt.Sprintf(`{"version":%d,"zone":"z","files":{"/a":{"size":1}}}`, v-1),
|
||
"other zone": fmt.Sprintf(`{"version":%d,"zone":"y","files":{"/a":{"size":1}}}`, v),
|
||
"bad file": fmt.Sprintf(`{"version":%d,"zone":"z","files":{"/a":{"size":"one"}}}`, v),
|
||
"bad late": strings.Replace(many.String(), `"size":2999`, `"size":"x"`, 1),
|
||
"cut": many.String()[:many.Len()-5],
|
||
"trailing": many.String() + " x",
|
||
"no comma": fmt.Sprintf(`{"version":%d,"zone":"z","files":{"/a":{"size":1} "/b":{"size":2}}}`, v),
|
||
"garbage": "garbage",
|
||
"empty bytes": "",
|
||
}
|
||
for name, in := range cases {
|
||
want, wantOK := unmarshal([]byte(in), "z")
|
||
got, ok := decodeCache([]byte(in), "z")
|
||
if ok != wantOK || !reflect.DeepEqual(got, want) {
|
||
wj, _ := json.Marshal(want)
|
||
gj, _ := json.Marshal(got)
|
||
t.Errorf("%s: got %v %.300s, want %v %.300s", name, ok, gj, wantOK, wj)
|
||
}
|
||
}
|
||
}
|
||
|
||
// A title, cwd or model that many kept files repeat is held once, not
|
||
// once a file: 23.7k sessions a script ran with one prompt held 11 MB of
|
||
// copies of it.
|
||
func TestDecodeCacheSharesRepeats(t *testing.T) {
|
||
var b strings.Builder
|
||
fmt.Fprintf(&b, `{"version":%d,"zone":"z","files":{`, cacheVersion)
|
||
for i := range 2000 {
|
||
if i > 0 {
|
||
b.WriteString(",")
|
||
}
|
||
fmt.Fprintf(&b, `"/s/%d.jsonl":{"size":%d,"title":"重要执行方式 %d","cwd":"/w/p","model":"gpt-5.5","codex_identity":{"id":"c%d","role":"subagent"}}`, i, i, i%2, i)
|
||
}
|
||
b.WriteString(`}}`)
|
||
files, ok := decodeCache([]byte(b.String()), "z")
|
||
if !ok || len(files) != 2000 {
|
||
t.Fatalf("decoded %d files, %v", len(files), ok)
|
||
}
|
||
held := map[string]map[*byte]bool{}
|
||
for _, st := range files {
|
||
for name, s := range map[string]string{"title": st.Title, "cwd": st.Cwd, "model": st.Model, "role": st.CodexIdentity.Role} {
|
||
if held[name] == nil {
|
||
held[name] = map[*byte]bool{}
|
||
}
|
||
held[name][unsafe.StringData(s)] = true
|
||
}
|
||
}
|
||
for name, want := range map[string]int{"title": 2, "cwd": 1, "model": 1, "role": 1} {
|
||
if len(held[name]) != want {
|
||
t.Errorf("%s: %d copies held, want %d", name, len(held[name]), want)
|
||
}
|
||
}
|
||
if st := files["/s/3.jsonl"]; st.Title != "重要执行方式 1" || st.CodexIdentity.ID != "c3" {
|
||
t.Fatalf("file 3: %+v", st)
|
||
}
|
||
}
|
||
|
||
// A file of one day holds that day's models once, and a parse of it, from
|
||
// a clone, counts a call once in each: the kept state is as it was read.
|
||
func TestDecodeCacheSharesOneDaysModels(t *testing.T) {
|
||
m := `{"gpt-5.5":{"input":10,"output":2,"cache_read":0,"cache_write":0}}`
|
||
b := fmt.Sprintf(`{"version":%d,"zone":"z","files":{"/one":{"size":1,"models":%s,"days":{"2026-10-24":{"m":%s,"a":5}}},"/other":{"size":1,"models":%s,"days":{"2026-10-24":{"m":{"o3":{"input":1,"output":0,"cache_read":0,"cache_write":0}}}}},"/two":{"size":1,"models":%s,"days":{"2026-10-23":{"m":%s},"2026-10-24":{"a":1}}}}}`, cacheVersion, m, m, m, m, m)
|
||
files, ok := decodeCache([]byte(b), "z")
|
||
if !ok || len(files) != 3 {
|
||
t.Fatalf("decoded %d files, %v", len(files), ok)
|
||
}
|
||
same := func(a, b map[string]Tokens) bool {
|
||
return reflect.ValueOf(a).UnsafePointer() == reflect.ValueOf(b).UnsafePointer()
|
||
}
|
||
one, other, two := files["/one"], files["/other"], files["/two"]
|
||
if !same(one.Models, one.Days["2026-10-24"].Models) {
|
||
t.Errorf("a file of one day holds its models twice")
|
||
}
|
||
if same(other.Models, other.Days["2026-10-24"].Models) || same(two.Models, two.Days["2026-10-23"].Models) {
|
||
t.Errorf("models held once that aren't the same, or of a file of two days")
|
||
}
|
||
c := one.clone()
|
||
c.use("2026-10-24", "gpt-5.5", Tokens{Input: 1})
|
||
if c.Models["gpt-5.5"].Input != 11 || c.Days["2026-10-24"].Models["gpt-5.5"].Input != 11 {
|
||
t.Fatalf("a parse counted %d and %d, want 11 in each", c.Models["gpt-5.5"].Input, c.Days["2026-10-24"].Models["gpt-5.5"].Input)
|
||
}
|
||
c.dropUse("2026-10-24", "gpt-5.5", Tokens{Input: 11, Output: 2})
|
||
if len(c.Models) != 0 || c.Days["2026-10-24"] == nil || len(c.Days["2026-10-24"].Models) != 0 {
|
||
t.Fatalf("a parse took back to %v and %+v", c.Models, c.Days["2026-10-24"])
|
||
}
|
||
if one.Models["gpt-5.5"].Input != 10 || one.Days["2026-10-24"].Models["gpt-5.5"].Input != 10 {
|
||
t.Fatalf("the kept state was written: %v", one.Models)
|
||
}
|
||
}
|
||
|
||
// A save writes the file json.Marshal would, byte for byte, a file's parse
|
||
// at a time: keys in order and escaped as Marshal escapes them, a file of
|
||
// no parse, an empty index and none at all.
|
||
func TestEncodeCacheIsMarshal(t *testing.T) {
|
||
at := time.Date(2026, 10, 25, 7, 0, 0, 0, time.UTC)
|
||
files := map[string]*state{"/b/<a&b>.jsonl": {Size: 3, Mod: 9, Title: "a <b> & \"c\"
", Start: at, Last: at, Models: map[string]Tokens{"m": {Input: 1}}}, "/a/x.jsonl": nil}
|
||
for i := range 3000 {
|
||
files[fmt.Sprintf("/s/%04d.jsonl", i)] = &state{Size: int64(i), Cwd: strings.Repeat("d", i%300), Start: at, Last: at.Add(time.Duration(i) * time.Second)}
|
||
}
|
||
for _, c := range []cacheFile{
|
||
{Version: cacheVersion, Zone: "28800/28800", Files: files},
|
||
{Version: cacheVersion, Zone: "<z>", Files: map[string]*state{}},
|
||
{Version: cacheVersion, Zone: "0/0"},
|
||
} {
|
||
want, err := json.Marshal(c)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
var got bytes.Buffer
|
||
if err := encodeCache(&got, c); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if !bytes.Equal(got.Bytes(), want) {
|
||
n := 0
|
||
for n < min(got.Len(), len(want)) && got.Bytes()[n] == want[n] {
|
||
n++
|
||
}
|
||
t.Fatalf("%d files: %d bytes, want %d; apart at %d: %.80q", len(c.Files), got.Len(), len(want), n, got.Bytes()[n:])
|
||
}
|
||
}
|
||
}
|