diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index 4b70e94e3..afb007b42 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -730,14 +730,7 @@ 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) - } + shared.keepPairs(p, cache.pairsNext) } if len(shared.pages) >= 16 { shared.pages = map[pageKey]RequestPage{} @@ -762,15 +755,20 @@ func codexEffortOfIndex(st requestState, p Period, now time.Time) map[string][]s shared.Lock() shared.keepWhileAsked() shared.adopt(st) + // the page of p just before, the warm-up's of All, paired these rows: + // a build after it paired every session file's calls again (#3493) + memo := shared.pairs[p] idx := shared.private() shared.Unlock() b := readRequestBlocks(idx, st, p, now, sessions.ReadCallSource) sums := effortSums{} - pairRows(p, now, b.gateways, b.chunks, b.others, st.codexIdentities).visit(func(_ timedRef, r *Row) { sums.add(r) }) + ps, next := pairRowsMemo(p, now, b.gateways, b.chunks, b.others, st.codexIdentities, memo) + ps.visit(func(_ timedRef, r *Row) { sums.add(r) }) b.priced.release() shared.Lock() if shared.keep(idx, b.on) { shared.noteBuilt(b) + shared.keepPairs(p, next) shared.trim(requestCacheBytes) } shared.Unlock() @@ -958,6 +956,18 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti // adopt drops what idx keeps of a state other than st: every chunk when // what prices or names a call changed (meta), the pages when anything // did (key). idx is locked. +// keepPairs keeps memo as p's last pairing (nil: there is none to keep). +func (idx *requestIndex) keepPairs(p Period, memo *pairMemo) { + if idx.pairs == nil || len(idx.pairs) >= partSets && idx.pairs[p] == nil { + idx.pairs = map[Period]*pairMemo{} + } + if memo != nil { + idx.pairs[p] = memo + } else { + delete(idx.pairs, p) + } +} + func (idx *requestIndex) adopt(st requestState) { if idx.root != st.root || idx.meta != st.meta { idx.root, idx.meta = st.root, st.meta @@ -1419,7 +1429,13 @@ type pairedRows struct { since, until time.Time } +// pairing, in tests, is told each time pairRows pairs chunks' rows. +var pairing func() + func pairRows(p Period, now time.Time, gateways, chunks, others []*rowChunk, identities map[string]sessions.CodexIdentity) *pairedRows { + if pairing != nil { + pairing() + } gateways, chunks, others = cloneCodexChunks(gateways), cloneCodexChunks(chunks), cloneCodexChunks(others) duplicateSkip := foldCodexRows(chunks) pairs, compactions := codexChunkResponsePairs(gateways, chunks, duplicateSkip) diff --git a/internal/usage/request_pairs_test.go b/internal/usage/request_pairs_test.go index eee2dd91d..6984f0f70 100644 --- a/internal/usage/request_pairs_test.go +++ b/internal/usage/request_pairs_test.go @@ -2,6 +2,7 @@ package usage import ( "path/filepath" + "reflect" "testing" "time" @@ -42,3 +43,39 @@ func TestRequestPairsReusedAcrossPages(t *testing.T) { equalPage(t, QueryPage(p, Filter{Query: "main-a"}, 0, 100), pageFromLedger(p, Filter{Query: "main-a"}, 0, 100, LedgerOf(p, Filter{Query: "main-a"}))) } } + +// Codex's effort over a period whose page was just built pairs no row +// again: the warm-up's page of All paired every session file's calls, and +// the effort read after it paired them all over (#3493). It is the effort +// a pairing of its own gives. +func TestCodexEffortReusesPagePairing(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", "high", now.Add(-2*time.Minute), 30, 3) + writeCodexCall(t, filepath.Join(dir, "rollout-2026-09-30T10-00-02-fork-c.jsonl"), "fork-c", "native-solo", `"vscode"`, "resp-solo", "m", "high", 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"}) + pairings := 0 + pairing = func() { pairings++ } + t.Cleanup(func() { pairing = nil }) + + QueryPage(All, Filter{}, 0, 50) + if pairings != 1 { + t.Fatalf("the page paired %d times, want 1", pairings) + } + got := CodexModelEffort(All) + if pairings != 1 { + t.Fatalf("the effort after the page paired again (%d pairings)", pairings) + } + if rows := got["main-a"]; len(rows) != 1 || rows[0].Input != 60 { + t.Fatalf("effort = %+v", got) + } + requestCache.Lock() + requestCache.pairs = nil + requestCache.Unlock() + if want := codexEffortOfIndex(readRequestState(now), All, now); pairings != 2 || !reflect.DeepEqual(got, want) { + t.Fatalf("effort through the page's pairing %+v, a pairing of its own %+v (%d pairings)", got, want, pairings) + } +}