diff --git a/internal/usage/heat_disk.go b/internal/usage/heat_disk.go index 2da9f3365..bc1680975 100644 --- a/internal/usage/heat_disk.go +++ b/internal/usage/heat_disk.go @@ -99,8 +99,7 @@ func heatDiskWrite(key string, cut, since time.Time, days []Day) error { // heatSettledKey names what the days before cut were counted of: each // chunk by its rows (a session file's through sourceDigest) under the // same root and meta, and the counting by the call shards' version and -// settledCountVersion. False: the chunks -// can't be named so (a probe's chunk made again for each query). +// settledCountVersion. False: no disk to keep the count on. func heatSettledKey(d heatDiskRef, cut, since time.Time, f Filter, identities map[string]sessions.CodexIdentity, settled [3][]*rowChunk, before heatSum) (string, bool) { if d.root == "" { return "", false @@ -126,9 +125,8 @@ func heatSettledKey(d heatDiskRef, cut, since time.Time, f Filter, identities ma return strings.Compare(a.Source.Path, b.Source.Path) }) for _, c := range set { - if c.serial == 0 { - return "", false - } + // a chunk without a serial (a probe's, made again for each + // query) is named by its rows below, as pairSerials allows b = fmt.Appendf(b[:0], "\x01%d\x00%s\x00%d\x00%d\x00%s\x00", k, c.Computer, c.Count, c.Latest.UnixNano(), c.Source.Path) h.Write(b) if c.Source.Path != "" { diff --git a/internal/usage/heatmap_test.go b/internal/usage/heatmap_test.go index d858c43a4..d6a848934 100644 --- a/internal/usage/heatmap_test.go +++ b/internal/usage/heatmap_test.go @@ -406,3 +406,35 @@ func TestHeatSettledKeyNamesTheZone(t *testing.T) { t.Fatal("another zone, the same key") } } + +// A probe's chunk (MCP and search calls) is made again for each query and +// has no serial. When it is among the settled days the count is still kept +// on disk, named by its rows, or every refresh counts the days again. +func TestHeatSettledKeyNamesAProbeChunk(t *testing.T) { + cut := time.Date(2026, 9, 28, 0, 0, 0, 0, time.UTC) + d := heatDiskRef{root: "r", meta: "m"} + probe := func(input int) *rowChunk { + c := &rowChunk{} + for i := 1; i <= 3; i++ { + c.add(Row{Record: Record{Time: cut.Add(-time.Duration(i) * time.Hour), Agent: "claude", Model: "m", Input: i * input}}, fmt.Sprint("msg", i), int64(i), false) + } + return c + } + key := func(c *rowChunk) string { + if c.serial != 0 { + t.Fatalf("serial %d, want a probe's 0", c.serial) + } + k, ok := heatSettledKey(d, cut, time.Time{}, Filter{}, nil, [3][]*rowChunk{{c}}, heatSum{}) + if !ok { + t.Fatal("a probe's chunk left the settled days unnamed") + } + return k + } + a, b := key(probe(10)), key(probe(10)) + if a != b { + t.Fatalf("the same rows, two keys: %s, %s", a, b) + } + if key(probe(11)) == a { + t.Fatal("other rows, the same key") + } +}