Commit Graph
3 Commits
Author SHA1 Message Date
yetone cb167fcbd8 usage: CodexModelEffort's allocation bar is a packed row a call, not a visit (#2278)
TestCodexModelEffortLetsUnrelatedCallsGo held a build of
CodexModelEffort to a visit of the log and a half. Since dc6f6d902 a
visit decodes into a lent buffer, and allocated 74 KB or 289 KB as the
pool kept that buffer through the test's GC or not, so the bar moved
under a build of ~430 KB and the test was red now and then (1 of 2 runs
on mote at the change below, "a build allocated 430336 bytes, a visit
73848").

The bar is now what #2776 was about: a build over 3000 unrelated calls
allocates less than one packed row (216 B) for each of them, 648 KB.
Builds measured 215 to 430 KB.

Run at this head, on mote:
- go test ./internal/usage -run TestCodexModelEffortLetsUnrelatedCallsGo
  -count=30: ok.
- go test ./internal/usage -timeout 45m: ok (23.2s).
Not tried: reverting #2776's fix to see the bar go red.
2026-10-29 00:14:17 +08:00
yetone 1313b047f5 usage: a Codex thread's model and effort are read through the request index
CodexModelEffort, which the Sessions list asks for, read the ledger again
on every build: sessions.AgentCalls(since, "codex") opened all of Codex's
shards (12k rollouts, 15.6k shards on the reporter-sized sandbox) after
any new call, beside the Requests page reading the same files into its
own index (#3493). It now reads through the request index as a page
does: readRequestBlocks (moved out of queryPage), the page's pairing and
visit (pairedRows, moved out of buildRequestBlocksOf), and the sums added
row by row as they are visited (effortSums), so a build after a call
reads only the files that changed, and the first of the Sessions list
and the Requests page reads them for both. The LogCalls path still
builds from codexEffortRows.

The visit's row is one per visit now: as a returned closure's local it
was moved to the heap, one allocation a row, which
TestRequestPageAppendDoesNotRepriceHistory caught (10890 allocations over
8000 records against 2000).

Measured before the rebase onto dbbe97991 (its parent ffe493773 adds
rows for magpie's own probes, none in this sandbox), on a sandbox copy of real data (sbmem: 12,069 Codex
rollouts, ~416k rows), base 22a7f86c9 against this, /api/sessions then
the Requests page then /api/sessions, one CPU profile over each run:
  total CPU          16.75s -> 9.40s  (first run 20.10s -> 8.32s)
  CodexModelEffort    4.21s -> 1.65s
  sessions.loadCalls  5.61s -> 1.22s  (now only under the first build;
                                       the page that follows reads none)
  wall, first run: sessions 4.97s -> 2.84s, requests 7.72s -> 2.01s
  (load average 78-133 from other sessions' suites, so wall is noisy)
/api/sessions of both binaries: 4,930 Codex sessions, 4,604 model/effort
entries, every token count equal and every cost equal within 1e-9
(summation order).

Tests, at this head after the rebase: go test ./internal/usage ./internal/sessions ./internal/usageapi;
./internal/gui -run 'Session|Codex|Effort'; ./internal/usage -race -run
'Codex|Effort|RequestPage'; gofmt; GOOS=linux and windows go vet -tags
nogui ./internal/usage ./internal/gui. TestCodexEffortRowsAreTheWholeLedgers
now also holds codexEffortOfIndex to the whole ledger's sums for All,
Today and Week; it goes red with a gateway call's actor broken by hand.

Not tried: a live Codex session writing while the list polls (the build
after a call is the page's incremental read, measured in 22a7f86c9).
2026-10-25 04:28:31 +08:00
yetone 227b864a45 usage: CodexModelEffort keeps only the gateway calls a Codex session file's call can be matched to (#2776)
CodexModelEffort built the whole ledger of the period and kept the rows
that carry a Codex thread. Every gateway call became a Record (in
gatewayRecords' slice), a packed row (gatewayMatchesDetails' chunk) and
a priced Row (ledgerMatched), all held at once, though only a gateway
call paired with a Codex session file's call by its response id can
carry a thread. On the 720k-call sandbox this build was 7.6 GB of the
start's 18 GB allocated and most of its peak.

codexEffortRows visits the log once and keeps only the gateway calls
with a response id, request id or session (native, else magpie's) that
one of the session files' calls has, or an agy call when one is agy's.
No other gateway call can change gatewayMatchesDetails' answer: the
request-id match looks up only the locals' ids, the fallback groups are
looked up only by the locals' sessions, and a response id's uniqueness
is counted over every call that has it, all of which are kept. Only the
paired calls are priced into rows, and the rows are the whole ledger's
thread rows in the same order, so the sums (floats included) are the
same.

TestCodexEffortRowsAreTheWholeLedgers compares the rows with the whole
ledger's for All, Today and 7d, with Codex's reader and every agent's:
a pair by response id, an ambiguous response id, a session-and-tokens
match, a call two days old, and unrelated calls around them. With the
session test dropped from matchingRecords by hand it is red (all,
every agent: the session-matched call shows as a row of its own).

TestCodexModelEffortLetsUnrelatedCallsGo builds over 3000 unrelated
gateway calls and allows a visit of the log and a half. With the old
build put back (LedgerOf): red, 18,347,128 bytes against a visit's
1,198,680. With the fix: 1,321,824 against 1,321,744.

720k-call sandbox, cold start, the four first asks together, back to
back (load 48.7 then 14.7, so the times are not comparable; the memory
is what this measures):
  before (beb9c40d5): peak RSS 4411 MB, heapSys 5513 MB, live 574 MB
  after:              peak RSS 2427 MB, heapSys 2413 MB, live 542 MB
  manage?agent=codex answered in 41.5 s before, 23.1 s after.

Full suite on this series' final tree (aa678b482 before this note was
added), rebased on origin/main f76201a84: go test -p 4 ./... -timeout
45m, exit 0, 64 packages ok and none failed (gateway 1783 s, gui 361 s),
41 minutes at load ~190-200. gofmt -l: no files. go vet: darwin, linux
(-tags nogui) and windows exit 0. The GUI suite wasn't run: no file
under internal/gui/assets changed.
2026-10-19 14:34:18 +08:00