Commit Graph
5 Commits
Author SHA1 Message Date
yetone c93d9e0c0b usage: an unchanged Codex effort ask is told the kept answer, not a copy (#2278)
CodexModelEffort copied every thread's consumption for each ask
(cloneEffort), though the kept answer is only replaced by a later build,
never written, and its callers (the Sessions list, overview and a
session's details) only index it and copy their thread's slice
(addCodexModelEffort, addCodexSummaryModelEffort). On the #2278 bench
(15G .codex) the copies were ~230 MB allocated every 80 s of an open
Sessions page. Asks are now told the kept map itself; the doc comment
says it is read-only.

TestCodexModelEffortReadsLedgerOnlyWhenCallsChange wrote into the answer
to check a caller's copy was its own. That guarantee is now the GUI's:
TestCodexModelEffortCopiedPerSession checks a session and a summary get
their own slice. TestCodexModelEffortAsksShareTheKeptAnswer checks two
unchanged asks get the same map.

Run at this tree on mote (macOS 27, Go 1.26.8):
- go test -count=1 ./internal/usage ./internal/sessions ./internal/gui:
  usage ok, sessions ok; gui red only in TestMainWindowCorners/
  macOS_26_SDK, which is #3949 (filed, red on macOS 27 on main).
- GOOS=linux,windows,darwin go vet -tags nogui ./internal/usage
  ./internal/gui: ok.
Broken by hand: maps.Clone back in the kept-answer return makes
TestCodexModelEffortAsksShareTheKeptAnswer red ("told a copy"); dropping
the GUI's slices.Clone makes TestCodexModelEffortCopiedPerSession red
("written through a session's copy: 999").
Not run: the full ./... suite, and the bench A/B (the 230 MB is from the
v0.1.1967 heap profile, not yet re-measured).
2026-10-29 06:30:58 +08:00
yetone 9fed5dc8c1 usage: a Codex effort past its minute is told while the next is built behind it
The Sessions page asks for every Codex thread's consumption on each 15s
poll. Once the kept answer was a minute old and any session file had
changed (a working Codex or Claude Code changes one all the time), the
ask built the ledger of all history inside the request, so /api/sessions
held for 0.8 to 1.3 s about once a minute. Now that ask is told the kept
answer and the build runs behind it, one at a time as before; the next
poll has the change. A period's first answer, and one for another home,
are still waited for.

effortBehind is off in the usage tests' TestMain, as effortFresh is, so
the tests that write a change and ask again still see it at once.
TestCodexModelEffortHeldForAMinute now holds the build and checks that
the ask after the minute, and one during the build, get the last answer,
and that the ask after the build gets the change without a third build.

Run at 5e3dd1d52 plus this change (main since gained e7173958e and
aa766289b, agent and gui only; re-run below at the rebased head):
- go test ./internal/usage/ ./internal/usageapi/ ./internal/gui/ ok
- go test -race -count=5 -run TestCodexModelEffort ./internal/usage/ ok
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/usage/ ok
- broken by hand (the background branch made unreachable):
  TestCodexModelEffortHeldForAMinute red, "after the minute the ask
  waited for the build"
- real history (rss-home over the live ~/.codex and ~/.claude, 120s
  CPU profile over 8 GUI-like polls, rss-bin/cpu6.sh), /api/sessions
  per poll:
    before: 1238 207 138 210 194 980 295 288 ms; 815 140 197 158 413 484 151 231 ms
    after:   348 141 330 177 263 233 135 263 ms; 449 267 203 210 161 239 317 290 ms
  pairRows CPU unchanged (0.79s vs 0.81/0.91s): the build moved, it
  wasn't added. Requests page max 260 ms after vs 404 ms before on the
  second pair.

At the rebased head (on e7173958e): go build ok; go test ./internal/usageapi/
./internal/gui/ ok; ./internal/usage/ ok except
TestUsageReadAfterAppendHashesNothingOnAFineClock/whole-second_stamp, a
known wall-clock flake taken by ggbdpq (#3511): 1 of 20 red on a clean
origin/main e7173958e worktree, 0 of 20 with this change.

Not tried: the Windows box and the Linux app; nothing here is OS-specific.
2026-10-27 11:24:15 +08:00
yetone f3c4d7b7a7 usage: Codex effort reads only the session chunks that have Codex threads
CodexModelEffort (the Usage page's model and effort per Codex thread,
read again each minute) unpacked every chunk of the ledger, Claude
Code's and every other agent's too, to sum the few rows a Codex actor
owns. codexEffortOfIndex now visits only chunks with a Codex actor
(pairedRows.visitActors), and effortSums.add clones the model and
effort it keeps, since lentRows lends its strings.

Run at this head (d5f86d991 + this): go test ./internal/usage
./internal/usageapi ./internal/gui ok; TestCodexModelEffort* -race
-count=3 ok; gofmt clean; GOOS=linux and GOOS=windows go vet -tags
nogui ./internal/usage clean. TestCodexModelEffortIsTheWholeLedgers
now checks a chunk was read and the Claude Code session's wasn't: red
with the skip taken out by hand, and red with the clone taken out
(the sums read the next chunk's strings).
A/B on real session files, 8 sequential polls of state, sessions,
stats, usage, usage/requests, quotas and providers, 15 s apart, the
previous commit's build vs this: CodexModelEffort 305 -> 245 MB,
codexEffortOfIndex 238 -> 176 MB; against v0.1.1661 twice, 304 -> 253
and 308 -> 245 MB. Totals moved with live sessions being written
beside the runs (the sandbox HOME links ~/.codex/sessions and
~/.claude/projects): ReadCallSource read 46 to 398 MB a run on the
same binaries, so the totals don't measure this change.
Not changed: pairRowsMemo's misses in buildRequestBlocksIn and
readRequestState's listing, the next hotspots.
Rebased on 33ccc6188 (library, not usage) and run again at the pushed
head: usage and usageapi ok.
2026-10-27 09:33:42 +08:00
yetone 61d656c696 usage: a Codex thread's consumption is built again at most once a minute
The Sessions page asks CodexModelEffort(All) on every 15 s poll. It
built the ledger of all history again whenever any session file or the
gateway log had changed, and with Codex and Claude Code at work they
change between nearly every two polls. In a 3.5-minute real run that was
2.49 GB allocated, the largest single cost of a poll. An answer now
stands for effortFresh (a minute) before a change is built into it, and
an ask inside the minute is answered before the sources' state is read
(that listing and hashing is most of an unchanged ask's cost). Another
home's answer is never held (requestRoot). The page's own rows, totals
and the requests page are read as they change; only this per-thread
model/effort breakdown can be up to a minute behind.

Tests: TestCodexModelEffortHeldForAMinute holds the answer inside the
minute (no build, the old value), builds the change after it, and
builds for a new home at once. It fails with the check turned off by
hand ("inside the minute: builds 2 ... want 1"). The package's TestMain
sets effortFresh to 0, so every other test still sees a change at once.

Measured on a copy of the owner's real history, 12 poll cycles 15 s
apart after a warm start, origin/main a3d4f8c5c against this tree:
CodexModelEffort's allocation over the polls 754 MB -> 548 MB (Codex
was quieter in this window than in the 3.5-minute run above);
api/sessions 280 ms -> 237 ms mean.

Run at this tree: go test ./internal/usage/ ./internal/sessions/ ok;
./internal/cli ./internal/davsync ./internal/gui ./internal/middleware
./internal/otlp ./internal/tui ./internal/tune ./internal/usageapi ok;
gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui of usage
and sessions ok. Not run: internal/agent and internal/gateway, which
don't call CodexModelEffort.
2026-10-27 07:46:32 +08:00
yetone 8a1b5916f3 sessions, usage, gui: one read of every call at a time, the Codex effort built once and kept, and the Sessions page asks for an overview once, so the app no longer grows to 85 GB (#2278)
On a long history (the reporter's: 68k call files in calls-v15, 415 MB;
42 MB usage.jsonl; 75k calls in 7 days), the app's main process grew to
85 GB in about 90 minutes and the gateway froze. `magpie serve` alone held
83-153 MB for 20 minutes (the reporter's follow-up), so the growth is in
the app's Sessions path:
/api/sessions/overview -> usage.CodexModelEffort -> LedgerOfAt ->
sessions.Calls(all history).

Why it grew:
- CodexModelEffort's cache key changes on every gateway write, and it
  never kept an answer when usage.jsonl was missing or grew during the
  build. So every /api/sessions, overview, /one and /manage ask with a Codex
  session rebuilt the all-history ledger, gob-decoding every shard (only
  64 stay in memory).
- Nothing joined concurrent asks.
- The page's 15s reload asked for an overview again while the last one
  was still under way, so slow asks piled up.

What changes:
- sessions.Calls and the new AgentCalls go through sharedCalls. An ask
  while the same read is under way waits for it and gets a copy, and
  reads run one at a time.
- AgentCalls(since, "codex") reads Codex's files only. pruneCalls still
  sees every file.
- CodexModelEffort is built from Codex's calls plus the gateway's records.
  Only those name a Codex actor, and TestCodexModelEffortIsTheWholeLedgers
  checks the answer equals the whole ledger's on a mixed Claude + Codex +
  gateway fixture.
  - One build per period at a time. An ask during a build gets the last
    answer for the same home (root), or waits if there is none.
  - The answer is kept when usage.jsonl is missing or grows during the
    build.
  - At most 8 periods are kept.
- page.js: sessOverview shares one promise per query, so the filter's ask
  and the 15s reload's ask for the same overview send one request.
- docs/subsystems/gateway-sessions.md says this.

Reproduced (temporary test, not committed): a sandbox HOME with 62k
Claude Code session files, 5.9k Codex rollouts and 100k gateway records.
Sessions routes in-process, 4 rounds x 6 concurrent asks
(overview?days=0 and /api/sessions), a usage record appended every 300ms:
  before (2b0037f41): 7m39s total, slowest ask 2m01s; 66264 MB allocated
    (2761 MB per ask); peak heap in use 4620 MB, peak Go sys 6274 MB,
    peak RSS 5325 MB
  after:              20.4s total, slowest ask 7.1s; 20728 MB allocated
    (863 MB per ask); peak heap in use 1168 MB, peak Go sys 1487 MB,
    peak RSS 1415 MB
Cold warm-up (index plus every shard) was 21m before and 18.5s after on the
same home. The after run reused the shards on disk, so it is not a
comparable number.

Tests, each red on the old code:
- TestConcurrentCallsShareOneRead: 8 reads, not 1. It also goes red when
  the join is broken by hand.
- TestCodexModelEffortConcurrentAsksBuildOnce: 8 builds.
- TestCodexModelEffortAnswersLastWhileBuilding: 3 builds.
- TestCodexModelEffortKeptWithoutLogAndAcrossAWrite: 2 builds.
- sessions-overview-once.test.cjs: 2 overview requests, not 1. That is
  chromium and webkit; en, zh, zh-TW and ja at 1100/900px, de at 420px.
  It was red with the old page.js served.
TestAgentCallsReadsOneAgent and TestCodexModelEffortIsTheWholeLedgers pass
on both versions.

Run:
- gofmt -l: clean.
- go vet -tags nogui for darwin, linux and windows: sessions, usage,
  gui. Before that, the whole tree for linux and windows.
- go test -race on ./internal/sessions/ and ./internal/usage/.
- go test of internal/sessions, internal/usage and internal/gui at the
  rebased head (on 95e7c6bcc): green. gui was red once, in a run whose
  output was cut, at load ~100; it was green on two reruns.
- Full suite: SUITE_LINE
- GUI: the 13 files that ask for sessions/overview, at the rebased head,
  in chromium and webkit: 198/198. Before the rebase, those plus every
  usage*.test.cjs,
  in chromium and webkit: 749 pass. The 24 failures are usage-fold
  (#2163) and usage-agents-hide (#2276). The same 24 fail with the old
  page.js on 2b0037f41, and both are filed.

Not tried: the real app on the reporter's data; an hour-long run.
2026-10-15 23:08:00 +08:00