Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
usage: a packed block keeps its compressed bytes, not S2's worst-case buffer
yetone16d20c0b3d0e
diff --git a/internal/usage/pack_archive_test.go b/internal/usage/pack_archive_test.gonew file mode 100644index 000000000..2a956fa73--- /dev/null+++ b/internal/usage/pack_archive_test.go@@ -0,0 +1,62 @@+package usage++import (+ "fmt"+ "runtime"+ "testing"+ "time"+)++// A packed block keeps its compressed bytes and no more. s2.Encode's+// output is as large as the worst case, more than the rows' own bytes,+// and an archive cut from it (even clipped) held that whole allocation:+// several times the compressed bytes Bytes counts, for as long as the+// block is cached.+func TestPackedArchiveHoldsOnlyItsBytes(t *testing.T) {+ now := time.Now()+ block := func(k, n int) *rowChunk {+ c := &rowChunk{}+ for i := range n {+ r := Row{Record: Record{Time: now.Add(time.Duration(i) * time.Second), Agent: "claude", Provider: "anthropic", Model: "claude-opus-5-5", Input: 1200 + i%7, Output: 300, Session: fmt.Sprint("s", k, "-", i/50)}}+ c.add(r, fmt.Sprint("msg", i), int64(i), false)+ }+ return c+ }+ var base, after runtime.MemStats+ runtime.GC()+ runtime.ReadMemStats(&base)+ packed := make([]*rowChunk, 100)+ archives := 0+ for k := range packed {+ packed[k] = block(k, 2000).pack()+ if len(packed[k].Archive) == 0 {+ t.Fatalf("block %d: not packed", k)+ }+ archives += len(packed[k].Archive)+ }+ runtime.GC()+ runtime.ReadMemStats(&after)+ // the unpacked rows went with the collection: what stays is the+ // archives, their chunks' headers and the scratch the pool keeps+ if held := int64(after.HeapAlloc) - int64(base.HeapAlloc); held > int64(archives)*3/2+1<<20 {+ t.Fatalf("100 packed blocks hold %d KB for %d KB of archives", held>>10, archives>>10)+ }+ for k, c := range packed {+ back := c.unpack()+ if back == nil || len(back.Rows) != 2000 || back.row(1999).Input != 1200+1999%7 || back.row(1999).Session != fmt.Sprint("s", k, "-", 1999/50) {+ t.Fatalf("block %d: not read back", k)+ }+ }+ // blocks packed one after another through the same scratch keep their+ // own bytes+ a, b := &rowChunk{}, &rowChunk{}+ for i := range 100 {+ a.add(Row{Record: Record{Time: now, Agent: "a", Model: fmt.Sprint("ma", i)}}, "", int64(i), false)+ b.add(Row{Record: Record{Time: now, Agent: "b", Model: fmt.Sprint("mb", i)}}, "", int64(i), false)+ }+ pa := a.pack()+ pb := b.pack()+ if ua, ub := pa.unpack(), pb.unpack(); ua.row(99).Model != "ma99" || ub.row(99).Model != "mb99" {+ t.Fatalf("read back %q and %q", ua.row(99).Model, ub.row(99).Model)+ }+}diff --git a/internal/usage/request_snapshot.go b/internal/usage/request_snapshot.goindex c73300b31..4a1ac43cb 100644--- a/internal/usage/request_snapshot.go+++ b/internal/usage/request_snapshot.go@@ -24,11 +24,22 @@ import (// read out of them; nothing keeps a reference to one after unpack.var rowDecodes = sync.Pool{New: func() any { return new([]byte) }}+// rowEncodes are pack's scratch: the rows' bytes before compression and+// S2's output. s2.Encode makes its output as large as the worst case+// (MaxEncodedLen, more than the input), and an archive cut from it kept+// that whole allocation, several times the compressed bytes: the largest+// thing a started magpie held, ~17 MB for ~380k calls, and uncounted in+// Bytes. The archive is copied out of the scratch at its own length.+var rowEncodes = sync.Pool{New: func() any { return new([2][]byte) }}+func (c *rowChunk) pack() *rowChunk {if c == nil || len(c.Rows) < 64 || len(c.Archive) != 0 {return c}- data := make([]byte, 0, len(c.Rows)*64)+ scratch := rowEncodes.Get().(*[2][]byte)+ defer rowEncodes.Put(scratch)+ data := scratch[0][:0]+ defer func() { scratch[0] = data[:0] }()put := func(v uint64) { data = binary.AppendUvarint(data, v) }put(uint64(len(c.Rows)))put(uint64(len(c.Strings)))@@ -67,13 +78,14 @@ func (c *rowChunk) pack() *rowChunk {put(math.Float64bits(p.Credits))}}- archive := s2.Encode(nil, data)- if int64(len(archive)) >= c.Bytes {+ encoded := s2.Encode(scratch[1][:cap(scratch[1])], data)+ scratch[1] = encoded[:0]+ if int64(len(encoded)) >= c.Bytes {return c}next := *cnext.Rows, next.Strings, next.dict = nil, nil, nil- next.Archive, next.Locations = slices.Clip(archive), locations+ next.Archive, next.Locations = slices.Clone(encoded), locationsnext.Bytes = int64(len(next.Archive)+len(locations)*8) + codexActorBytes(next.codexActors)return &next}