usage: Codex's effort reuses the pairing the page of its period just made (#3493)

The warm-up builds the Requests page of All and then Codex's model and
effort over All. The page keeps its pairing of the calls (pairMemo, by
chunk serials), but the effort paired every session file's calls again
from nothing, the warm-up's step 3. It now reads requestIndex.pairs[p]
before taking its private copy and pairs through pairRowsMemo, so
unchanged chunks keep their pairing, and it keeps the new pairing for
the next page or effort. Storing a period's pairing is one helper,
keepPairs, that queryPage uses too.

Run at this head (on e1f5f9c16, which origin/main still was):
- TestCodexEffortReusesPagePairing (new): the page pairs once, the
  effort after it pairs 0 times, and equals an effort that pairs on its
  own. Broken by hand (memo -> nil): red, "the effort after the page
  paired again (2 pairings)".
- go test ./internal/usage ./internal/gui ./internal/sessions: ok.
  -race ./internal/usage: ok. go vet -tags nogui GOOS=linux and
  GOOS=windows for usage and gui, gofmt: clean.
- Real data (a copy of the owner's sessions, 421,292 calls), warm-up
  three runs each after one to write the cache file, e1f5f9c16 -> this:
  step 3 0.52-0.58s -> 0.24s; warm-up alloc 1.24-1.29GB -> 1.06GB;
  warm-up 1.80-1.85s -> 1.50s (one 1.77s). Max RSS unchanged (~310-333MB).
- CodexModelEffort(All)'s JSON hash on that data, four runs alternating
  base and this: identical every time (78f93906...).

Not tried: the effort of a period whose page was not built first (it
pairs as before, from nil).
This commit is contained in:
yetone
2026-10-25 08:05:33 +08:00
parent e1f5f9c164
commit b1f44d099e
2 changed files with 62 additions and 9 deletions
+25 -9
View File
@@ -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)
+37
View File
@@ -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)
}
}