Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

usage: a turned page reuses the last pairing while its chunks are the same

yetone4982ded8a6dd
diff --git a/internal/usage/request_cells.go b/internal/usage/request_cells.go
index c3434e8bc..f33bedbd1 100644
--- a/internal/usage/request_cells.go
+++ b/internal/usage/request_cells.go
@@ -101,6 +101,8 @@ type partKey struct {
// the index keeps for the next.
type partCache struct {
old, next map[uint64]*chunkPart
+ // pairs is the last pairing of the period, and pairsNext this page's
+ pairs, pairsNext *pairMemo
}
// partSets bounds the partKeys kept: a page is open on one period and
diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go
index e2d168891..e24195e32 100644
--- a/internal/usage/request_page.go
+++ b/internal/usage/request_page.go
@@ -482,6 +482,9 @@ type requestIndex struct {
// names a call changes (meta), and read without the lock, since a
// part is never written once made
parts map[partKey]map[uint64]*chunkPart
+ // pairs are the last pairing of each of a few periods (pairMemo),
+ // kept as parts are
+ pairs map[Period]*pairMemo
}
// heatPage is a heatmap's page, of the gateway log's snapshot version,
@@ -669,7 +672,7 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C
var cache *partCache
pk := partKey{p: p, f: f, since: p.Since(now), until: p.Until(now), loc: now.Location()}
if p.Last() == 0 && p != heatmapPeriod {
- cache = &partCache{old: idx.parts[pk], next: map[uint64]*chunkPart{}}
+ cache = &partCache{old: idx.parts[pk], next: map[uint64]*chunkPart{}, pairs: idx.pairs[p]}
}
shared := idx
idx = shared.private()
@@ -695,6 +698,14 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C
shared.parts = map[partKey]map[uint64]*chunkPart{}
}
shared.parts[pk] = cache.next
+ if shared.pairs == nil || len(shared.pairs) >= partSets && shared.pairs[p] == nil {
+ shared.pairs = map[Period]*pairMemo{}
+ }
+ if cache.pairsNext != nil {
+ shared.pairs[p] = cache.pairsNext
+ } else {
+ delete(shared.pairs, p)
+ }
}
if len(shared.pages) >= 16 {
shared.pages = map[pageKey]RequestPage{}
@@ -894,6 +905,7 @@ func (idx *requestIndex) adopt(st requestState) {
idx.pages = nil
idx.heats = nil
idx.parts = nil
+ idx.pairs = nil
idx.key = ""
}
if idx.key != st.key {
@@ -1361,7 +1373,12 @@ func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time,
// cache's for the chunks it has them for (nil: every chunk's read).
func buildRequestBlocksIn(p Period, f Filter, offset, limit int, now time.Time, gateways, chunks, others []*rowChunk, names map[string]string, identities map[string]sessions.CodexIdentity, cache *partCache) RequestPage {
since := p.Since(now)
- ps := pairRows(p, now, gateways, chunks, others, identities)
+ var ps *pairedRows
+ if cache != nil {
+ ps, cache.pairsNext = pairRowsMemo(p, now, gateways, chunks, others, identities, cache.pairs)
+ } else {
+ ps = pairRows(p, now, gateways, chunks, others, identities)
+ }
out := RequestPage{Rows: []Row{}, Agents: []string{}, Providers: []string{}, Purposes: []string{}, By: map[string][]Share{}}
chartFilter := f
chartFilter.Day = ""
diff --git a/internal/usage/request_pairs.go b/internal/usage/request_pairs.go
new file mode 100644
index 000000000..efa494fcb
--- /dev/null
+++ b/internal/usage/request_pairs.go
@@ -0,0 +1,139 @@
+package usage
+
+import (
+ "slices"
+ "time"
+
+ "github.com/yetone/magpie/internal/sessions"
+)
+
+// pairMemo is what pairRows found over one list of chunks, named by their
+// serials: which rows another one stands for, and the Codex actors and
+// compactions it gave each chunk. A page turned, or asked for again
+// before anything changed, unpacked every session file three times over to
+// pair the same rows again: about half of a warm page (#3493).
+type pairMemo struct {
+ since, until time.Time
+ // serials are the chunks' in pairRows' order, each set ended by a 0;
+ // a chunk without a serial (the probes') is named by its rows instead
+ serials []uint64
+ skip, matched []serialRef
+ actors map[uint64][]string
+ compactions map[uint64]map[int]*bool
+}
+
+type serialRef struct {
+ serial uint64
+ index int
+}
+
+// pairSerials names the chunks of a pairing, or is false when two have
+// no serial and so can't be told apart.
+func pairSerials(sets ...[]*rowChunk) ([]uint64, bool) {
+ var out []uint64
+ unnamed := false
+ for _, set := range sets {
+ for _, c := range set {
+ s := c.serial
+ if s == 0 {
+ if unnamed || len(c.Archive) != 0 {
+ return nil, false
+ }
+ unnamed = true
+ s = unnamedSerial(c)
+ }
+ out = append(out, s)
+ }
+ out = append(out, 0)
+ }
+ return out, true
+}
+
+// unnamedSerial stands for a chunk made again for each query, the probes':
+// its rows are named by their place in the log and their time.
+func unnamedSerial(c *rowChunk) uint64 {
+ h := mix(6, uint64(len(c.Rows)))
+ for i := range c.Rows {
+ h = mix(h, uint64(c.Rows[i].Order), uint64(c.Rows[i].Time.UnixNano()))
+ }
+ return h | 1
+}
+
+// pairRowsMemo is pairRows, through old when it paired these chunks over
+// the same window. The memo it returns is for the next call (nil: none).
+func pairRowsMemo(p Period, now time.Time, gateways, chunks, others []*rowChunk, identities map[string]sessions.CodexIdentity, old *pairMemo) (*pairedRows, *pairMemo) {
+ since, until := p.Since(now), p.Until(now)
+ serials, ok := pairSerials(gateways, chunks, others)
+ if !ok {
+ return pairRows(p, now, gateways, chunks, others, identities), nil
+ }
+ if old != nil && old.since.Equal(since) && old.until.Equal(until) && slices.Equal(old.serials, serials) {
+ return old.apply(serials, identities, gateways, chunks, others), old
+ }
+ ps := pairRows(p, now, gateways, chunks, others, identities)
+ return ps, memoOf(ps, serials, append(append(slices.Clone(gateways), chunks...), others...))
+}
+
+// memoOf is ps as a memo; originals are the chunks ps.all was cloned from.
+func memoOf(ps *pairedRows, serials []uint64, originals []*rowChunk) *pairMemo {
+ m := &pairMemo{since: ps.since, until: ps.until, serials: serials}
+ named := make(map[*rowChunk]uint64, len(ps.all))
+ k := 0
+ for i, c := range ps.all {
+ for serials[k] == 0 {
+ k++
+ }
+ named[c] = serials[k]
+ k++
+ if !slices.Equal(c.codexActors, originals[i].codexActors) {
+ if m.actors == nil {
+ m.actors = map[uint64][]string{}
+ }
+ m.actors[serials[k-1]] = c.codexActors
+ }
+ if c.codexCompactions != nil {
+ if m.compactions == nil {
+ m.compactions = map[uint64]map[int]*bool{}
+ }
+ m.compactions[serials[k-1]] = c.codexCompactions
+ }
+ }
+ refs := func(set map[rowRef]bool) []serialRef {
+ out := make([]serialRef, 0, len(set))
+ for ref := range set {
+ out = append(out, serialRef{named[ref.Chunk], ref.Index})
+ }
+ return out
+ }
+ m.skip, m.matched = refs(ps.skip), refs(ps.matched)
+ return m
+}
+
+// apply is the pairing m remembers, on clones of these chunks.
+func (m *pairMemo) apply(serials []uint64, identities map[string]sessions.CodexIdentity, sets ...[]*rowChunk) *pairedRows {
+ var all []*rowChunk
+ by := make(map[uint64]*rowChunk, len(serials))
+ k := 0
+ for _, set := range sets {
+ for _, c := range cloneCodexChunks(set) {
+ s := serials[k]
+ k++
+ if a, ok := m.actors[s]; ok {
+ c.codexActors = a
+ }
+ c.codexCompactions = m.compactions[s]
+ c.codexIdentities = identities
+ by[s] = c
+ all = append(all, c)
+ }
+ k++
+ }
+ refs := func(list []serialRef) map[rowRef]bool {
+ out := make(map[rowRef]bool, len(list))
+ for _, ref := range list {
+ out[rowRef{by[ref.serial], ref.index}] = true
+ }
+ return out
+ }
+ return &pairedRows{all: all, skip: refs(m.skip), matched: refs(m.matched), since: m.since, until: m.until}
+}
diff --git a/internal/usage/request_pairs_test.go b/internal/usage/request_pairs_test.go
new file mode 100644
index 000000000..eee2dd91d
--- /dev/null
+++ b/internal/usage/request_pairs_test.go
@@ -0,0 +1,44 @@
+package usage
+
+import (
+ "path/filepath"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/sessions"
+)
+
+// A page turned over the same chunks reuses the last pairing. It must give
+// the page a fresh pairing gives: a gateway call its rollout stands for is
+// counted once, and so is a call a forked file copies, on every page
+// and in the sums.
+func TestRequestPairsReusedAcrossPages(t *testing.T) {
+ pageHome(t)
+ now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
+ dir := filepath.Join(sessions.CodexDir(), "sessions", "2026", "09", "30")
+ writeCodexIdentity(t, "main-a", "native-main", "vscode")
+ writeCodexCall(t, filepath.Join(dir, "rollout-2026-09-30T10-00-00-main-a.jsonl"), "main-a", "native-main", `"vscode"`, "resp-paired", "m", "low", now.Add(-time.Minute), 60, 6)
+ writeCodexCall(t, filepath.Join(dir, "rollout-2026-09-30T10-00-01-solo-b.jsonl"), "solo-b", "native-solo", `"vscode"`, "resp-solo", "m", "low", now.Add(-2*time.Minute), 30, 3)
+ // A fork copies solo-b's call; it is counted once.
+ writeCodexCall(t, filepath.Join(dir, "rollout-2026-09-30T10-00-02-fork-c.jsonl"), "fork-c", "native-solo", `"vscode"`, "resp-solo", "m", "low", now.Add(-2*time.Minute), 30, 3)
+ Append(Record{Time: now.Add(-time.Minute), Agent: "codex", Provider: "openai", Model: "m", Input: 60, Output: 6, Status: 200, ResponseID: "resp-paired"})
+ Append(Record{Time: now.Add(-3 * time.Hour), Agent: "claude", Provider: "openai", Model: "m", Input: 7, Output: 1, Status: 200})
+
+ for _, p := range []Period{All, Today, Week} {
+ want := pageFromLedger(p, Filter{}, 0, 100, LedgerOf(p, Filter{}))
+ if want.Total != 3 || want.Sum.Input != 97 {
+ t.Fatalf("%v: fixture pairs to %d rows, %d input", p, want.Total, want.Sum.Input)
+ }
+ for offset := 0; offset < 3; offset++ {
+ got := QueryPage(p, Filter{}, offset, 1)
+ if got.Total != 3 || got.Sum.Input != 97 || len(got.Rows) != 1 {
+ t.Fatalf("%v page %d: %d rows, %d input, %d shown", p, offset, got.Total, got.Sum.Input, len(got.Rows))
+ }
+ if got.Rows[0].ResponseID != want.Rows[offset].ResponseID || got.Rows[0].Input != want.Rows[offset].Input {
+ t.Fatalf("%v page %d: %+v, want %+v", p, offset, got.Rows[0].Record, want.Rows[offset].Record)
+ }
+ }
+ equalPage(t, QueryPage(p, Filter{}, 0, 100), want)
+ equalPage(t, QueryPage(p, Filter{Query: "main-a"}, 0, 100), pageFromLedger(p, Filter{Query: "main-a"}, 0, 100, LedgerOf(p, Filter{Query: "main-a"})))
+ }
+}