A kept parse holds a model or two, a day or two and a few tools, and Go's map of one entry is a whole group of eight: ~500 bytes for one model's Tokens, where a sorted slice entry is 56. Over a long history (15.9k kept parses) state.Models, Days, day.Models, Tools and Skills held 18.4MB as maps and 4.2MB as named[V], a slice kept in name order. It is written as the same JSON object a map writes, byte for byte, so the parse cache saved before reads unchanged; names are interned with unique.Make, and plain integers in ints and Tokens are read without reflection. Measured with the real ~/.codex and ~/.claude histories (sandbox HOME, warm cache, 40s after start), base3393e61d0vs this change, two alternating runs each: live heap 102 / 105MB -> 92 / 91MB footprint 164 / 167MB -> 156 / 150MB Cache decode at start: ~58ms -> ~61ms (best of 7). Side by side on the same data, /api/sessions/overview?agent=codex and /api/stats?days=30 are identical; the other overview queries differed only in the two Claude sessions being written during the run. TestNamedIsAMapInJSON: marshal is byte-equal to a map's (HTML-escaped key included), round trip, a repeated name keeps its last value, an unknown Tokens field is ignored, omitempty leaves none out, and 1.5, "1", [1], 1e3 and a too-big int fail as they fail into a map. Broken by hand (the int fast path accepting 1.5) it went red. Run at this head (rebased on2beed3b04): gofmt clean; go build ./...; GOOS=linux go vet -tags nogui sessions, gui, usage; GOOS=windows vet sessions; go test -timeout 45m ./internal/sessions ./internal/usage ./internal/gui ./internal/gateway all ok. Not run: the full ./... suite and the GUI node tests (no GUI change).
34 lines
1.5 KiB
Go
34 lines
1.5 KiB
Go
package sessions
|
||
|
||
import (
|
||
"fmt"
|
||
"testing"
|
||
"time"
|
||
)
|
||
|
||
// PAMI on Discord: Claude Code's transcript says which cache writes were
|
||
// kept for an hour (cache_creation's ephemeral_1h_input_tokens), which
|
||
// Anthropic bills at 2× input, not 1.25×. Both readers keep them apart; a
|
||
// line without the split reads as before.
|
||
func TestClaudeCacheWriteSplit(t *testing.T) {
|
||
for _, fast := range []bool{false, true} {
|
||
t.Run(fmt.Sprint("fast=", fast), func(t *testing.T) {
|
||
s := &state{}
|
||
at := time.Date(2026, 9, 20, 12, 0, 0, 0, time.UTC)
|
||
id := `"id":"a","model":"m"`
|
||
if fast {
|
||
id = `"model":"m","id":"a"`
|
||
}
|
||
claudeLine(s, []byte(fmt.Sprintf(`{"type":"assistant","timestamp":%q,"requestId":"req-a","message":{%s,"role":"assistant","content":[],"usage":{"input_tokens":10,"output_tokens":2,"cache_read_input_tokens":30,"cache_creation_input_tokens":3000,"cache_creation":{"ephemeral_5m_input_tokens":1000,"ephemeral_1h_input_tokens":2000}}}}`,
|
||
at.Format(time.RFC3339Nano), id)), true)
|
||
claudeLine(s, claudeUsageLine(at.Add(time.Second), "b", "req-b", "m", Tokens{Input: 1, CacheWrite: 500}, "", fast), true)
|
||
if want := (Tokens{Input: 11, Output: 2, CacheRead: 30, CacheWrite: 3500, CacheWrite1h: 2000}); s.Models.of("m") != want {
|
||
t.Fatalf("tokens = %+v, want %+v", s.Models.of("m"), want)
|
||
}
|
||
})
|
||
}
|
||
if got := ccTokens(1, 2, 3, 0, &ccCacheCreation{Ephemeral1h: 40}); got.CacheWrite != 40 || got.CacheWrite1h != 40 {
|
||
t.Errorf("a split with no total = %+v", got)
|
||
}
|
||
}
|