Files
magpie/internal/usage/codex_fold_ids_test.go
T
yetone fddd7ff08f usage: pairing keeps its rows as bits and its groups by sums, and lets its pairs go (#2278)
A cold start pairs the whole history, and pairing held its row sets as
map[rowRef]bool, ~40 bytes a row, several sets of most rows at once.
They are now refSet: a bitset a chunk, a bit a row. matchScratch's
groupOf was keyed by the whole matchKey (a session string and its
tokens); it is keyed by a 128-bit maphash sum of them now, and the
sessions map is filled when a group is made. pairedRows' pairs and
compactions maps were filled and never read; they are gone.

Measured on mote, a copy of the real full history (52G Codex, 13G
Claude), GODEBUG=gctrace=1, 120s of /api polls, 3 rounds each,
alternating, v0.1.2143 (807048470) against this change:
  peak live heap  459 -> 439 MB (512/426/440 vs 470/433/413)
  heap at GC      543 -> 517 MB
  footprint peak  677 -> 641 MB
The inuse profile at the peak shows groupOf 28.7 -> 11.8 MB and the
matched set gone from matchedBlocks. This is a small part of the peak;
the rest is the full re-pair itself, which is next.

Run at this tree (rebased onto dca3e2d71 from 807048470), on mote:
go vet ./internal/usage; GOOS=linux|windows|darwin go vet -tags nogui
./... all ok; go test -count=1 ./internal/usage ok (30.1s), with the
new TestRefSet and TestMatchKeySum (no allocation).
Not run: the full suite, the GUI suite.
2026-10-30 13:42:11 +08:00

59 lines
2.0 KiB
Go

package usage
import (
"fmt"
"maps"
"testing"
"time"
)
// A chunk's Codex ids are read once while its serial stands: a poll that
// folds the same session files again reads none of them, and folds them
// the same. A chunk made again (a live session's) is read again.
func TestFoldReadsAChunksIDsOnce(t *testing.T) {
now := time.Date(2026, 10, 27, 12, 0, 0, 0, time.UTC)
chunk := func(ids ...string) *rowChunk {
c := &rowChunk{}
for i, id := range ids {
c.add(Row{Record: Record{Time: now.Add(-time.Duration(i) * time.Minute), Agent: "codex", Model: "native-model", Input: 80, Output: 8,
Session: "native-session", ResponseID: id}, Source: "log"}, "", int64(i), false)
}
for i := range 200 { // enough for the archive to be smaller than the rows
c.add(Row{Record: Record{Time: now.Add(-time.Hour), Agent: "claude", Model: "m", Session: "s", ResponseID: fmt.Sprint("other-", i)}}, "", int64(len(ids)+i), false)
}
packed := c.pack()
if len(packed.Archive) == 0 {
t.Fatal("the chunk wasn't packed")
}
packed.serial = nextChunkSerial()
return packed
}
a, b := chunk("resp-1", "resp-2"), chunk("resp-1", "resp-3")
var read []*rowChunk
foldHashing = func(c *rowChunk) { read = append(read, c) }
t.Cleanup(func() { foldHashing = nil })
first := refMap(foldCodexRows([]*rowChunk{a, b}))
if len(read) != 2 || len(first) != 1 || !first[rowRef{b, 0}] {
t.Fatalf("first fold read %d chunks, skipped %v; want both read and b's resp-1 skipped", len(read), first)
}
read = nil
again := refMap(foldCodexRows([]*rowChunk{a, b}))
if len(read) != 0 {
t.Fatalf("a fold of the same chunks read %d of them again", len(read))
}
if !maps.Equal(first, again) {
t.Fatalf("the same chunks folded %v, then %v", first, again)
}
read = nil
live := chunk("resp-1", "resp-4")
folded := refMap(foldCodexRows([]*rowChunk{a, live}))
if len(read) != 1 || read[0] != live {
t.Fatalf("a chunk made again wasn't the one read: %d read", len(read))
}
if len(folded) != 1 || !folded[rowRef{live, 0}] {
t.Fatalf("the live chunk's copy of a's call = %v", folded)
}
}