diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index 66830a1ab..06cd0060a 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -9,6 +9,7 @@ import ( "crypto/sha256" "encoding/json" "fmt" + "hash/maphash" "io" "maps" "os" @@ -1175,8 +1176,12 @@ func (idx *requestIndex) trim(bound int64) { // references for matches, never a full hydrated row for every call. func visibleLocal(chunks []*rowChunk) map[rowRef]bool { duplicates := map[rowRef]bool{} - seen := map[string]bool{} - rows := lentRows() // a kept message id is cloned + // a message id seen is kept as two 64-bit hashes of it, not a copy of + // the decoded chunk's string: a long history's ~400k ids held ~7MB of + // copies at the start's peak, and a key of these is a string header's + // size. Two ids alike in both are ~1e-28 likely. + seen := map[[2]uint64]struct{}{} + rows := lentRows() defer rows.release() for _, c := range chunks { open := rows.of(c) @@ -1186,17 +1191,19 @@ func visibleLocal(chunks []*rowChunk) map[rowRef]bool { if msg == "" { continue } - if seen[msg] { + k := [2]uint64{maphash.String(msgSeeds[0], msg), maphash.String(msgSeeds[1], msg)} + if _, ok := seen[k]; ok { duplicates[rowRef{c, i}] = true } else { - // a copy: a decoded chunk's strings are one allocation - seen[strings.Clone(msg)] = true + seen[k] = struct{}{} } } } return duplicates } +var msgSeeds = [2]maphash.Seed{maphash.MakeSeed(), maphash.MakeSeed()} + // matchKey narrows candidates before comparing their end times. Request IDs // are consumed first; fallback matching still requires uniqueness both ways. // The agent isn't in it: a session's id names the one conversation, and a diff --git a/internal/usage/request_page_test.go b/internal/usage/request_page_test.go index aa7b3b760..33fde1011 100644 --- a/internal/usage/request_page_test.go +++ b/internal/usage/request_page_test.go @@ -311,3 +311,21 @@ func TestRequestPageRanksModelAtEachProvider(t *testing.T) { }) } } + +// A Claude message written again in another file (a resumed or forked +// session's copy) is counted once, at its first file, and only it: a +// message id seen once, or none, is never left out. +func TestVisibleLocalLeavesOutRepeatedMessages(t *testing.T) { + a, b := &rowChunk{}, &rowChunk{} + a.add(Row{}, "msg_1", 0, false) + a.add(Row{}, "msg_2", 1, false) + b.add(Row{}, "msg_1", 0, false) + b.add(Row{}, "", 1, false) + b.add(Row{}, "msg_3", 2, false) + b.add(Row{}, "msg_2", 3, false) + a, b = a.pack(), b.pack() + got := visibleLocal([]*rowChunk{a, b}) + if len(got) != 2 || !got[rowRef{b, 0}] || !got[rowRef{b, 3}] { + t.Fatalf("left out %v, want b's rows 0 and 3 only", got) + } +}