diff --git a/internal/sessions/cache_decode.go b/internal/sessions/cache_decode.go index 682681a82..5846ef26b 100644 --- a/internal/sessions/cache_decode.go +++ b/internal/sessions/cache_decode.go @@ -145,4 +145,14 @@ func (st *state) share() { } } } + // a file of one day (15,345 of a long history's 15,624) has that day's + // models as its own: held once, they were ~10 of the 28MB kept. A kept + // state is never written: a parse writes a clone, which holds them apart. + if len(st.Days) == 1 { + for _, d := range st.Days { + if len(d.Models) == len(st.Models) && maps.Equal(d.Models, st.Models) { + d.Models = st.Models + } + } + } } diff --git a/internal/sessions/cache_decode_test.go b/internal/sessions/cache_decode_test.go index 38c2c84ed..99495cd06 100644 --- a/internal/sessions/cache_decode_test.go +++ b/internal/sessions/cache_decode_test.go @@ -94,6 +94,39 @@ func TestDecodeCacheSharesRepeats(t *testing.T) { } } +// 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.