diff --git a/internal/usage/derived_idle.go b/internal/usage/derived_idle.go index 6aca8126c..e981bc415 100644 --- a/internal/usage/derived_idle.go +++ b/internal/usage/derived_idle.go @@ -78,6 +78,24 @@ func dropDerived() int { heatBefores.by = nil heatBefores.Unlock() n += dropEffortPeriods() + n += dropDigestTurns() + return n +} + +// dropDigestTurns lets go of the serials sourceDigest met this turn and +// the one before (577 KB of a real history's idle heap). They only order +// which digests heat-digests.bin keeps first when there are more than it +// holds; what was made is written out before they go. +func dropDigestTurns() int { + sourceDigestsSave() + d := &sourceDigests + d.Lock() + defer d.Unlock() + if len(d.made) != 0 { // made meanwhile: written with them next time + return 0 + } + n := len(d.by) + len(d.last) + d.by, d.last = nil, nil return n } diff --git a/internal/usage/heat_digest_disk_test.go b/internal/usage/heat_digest_disk_test.go index 6dd015646..6dc6a76a2 100644 --- a/internal/usage/heat_digest_disk_test.go +++ b/internal/usage/heat_digest_disk_test.go @@ -121,3 +121,43 @@ func TestSourceDigestsKeptAcrossStarts(t *testing.T) { t.Fatalf("kept %d, want 1", got.len()) } } + +// The serials a turn met go when idle, after what was made is written: +// the next key hashes nothing again. +func TestDigestTurnsGoWhenIdle(t *testing.T) { + pageHome(t) + cut := time.Date(2026, 9, 28, 0, 0, 0, 0, time.UTC) + d := heatDiskRef{root: "r", meta: "m"} + c := &rowChunk{serial: 1<<63 | 7} + c.Source.Agent, c.Source.Key, c.Source.Path = "codex", "k", "/s/7" + c.add(Row{Record: Record{Time: cut.Add(-time.Hour), Agent: "codex", Model: "m", Input: 3}}, "msg", 1, false) + sourceDigests.Lock() + sourceDigests.by, sourceDigests.last, sourceDigests.made = nil, nil, nil + sourceDigestsDisk.path, sourceDigestsDisk.kept, sourceDigestsDisk.mapped = "", nil, nil + sourceDigests.Unlock() + key := func() (string, int) { + sourceDigests.Lock() + was := sourceDigestsHashed + sourceDigests.Unlock() + k, ok := heatSettledKey(d, cut, time.Time{}, Filter{}, nil, [3][]*rowChunk{{c}}, heatSum{}) + if !ok { + t.Fatal("not named") + } + sourceDigests.Lock() + defer sourceDigests.Unlock() + return k, sourceDigestsHashed - was + } + first, _ := key() + if n := dropDigestTurns(); n == 0 { + t.Fatal("no serials let go") + } + sourceDigests.Lock() + by, last := sourceDigests.by, sourceDigests.last + sourceDigests.Unlock() + if by != nil || last != nil { + t.Fatalf("kept %d and %d serials when idle", len(by), len(last)) + } + if again, n := key(); n != 0 || again != first { + t.Fatalf("after idle: hashed %d, key %s, was %s", n, again, first) + } +} diff --git a/internal/usage/heat_disk.go b/internal/usage/heat_disk.go index 5b0fee773..c77d5f81b 100644 --- a/internal/usage/heat_disk.go +++ b/internal/usage/heat_disk.go @@ -203,6 +203,7 @@ func sourceDigest(c *rowChunk, rows *chunkRows) [sha256.Size]byte { } d.by[c.serial] = struct{}{} d.Unlock() + derivedUsed() // by and last go when idle (dropDigestTurns) return sum }