Skip to content

perf: load only the session-cache members a date range can use - #1542

Merged
iamtoruk merged 10 commits into
mainfrom
perf/bounded-load
Sep 24, 2026
Merged

iamtoruk merged 10 commits into
mainfrom
perf/bounded-load

Conversation

@iamtoruk

Copy link
Copy Markdown
Member

Closes #1504. First stage of bounded cache loading.

What was wrong

A ranged query (overview -p today, status, the menu bar) loaded every session-cache shard in its month scope whole, then kept only what the range needed. On a heavy corpus that is gigabytes for a few hundred MB of today's data, and it runs out of heap. Shards past V8's string limit (~512 MB) also failed to save silently, so those users re-parsed everything on every run. Codex project discovery loaded the whole results cache just to read project names.

What changed

  • Shards are written one session per line (still valid JSON, older builds read them). A legacy single-line shard is parsed once and rewritten on its next save.
  • A ranged load keeps in full only the cached files the range can report on. Every other file is held as a small in-memory stub (fingerprint, per-turn dedup keys, position). Stubs take part in reconcile, dedup pre-seeds, query-time dedup replay in main's order, orphan surfacing and eviction, and contribute nothing to summaries, which is exactly what main does with those files. A changed stub loads its one member on demand, so main's append/re-parse path runs unchanged.
  • A shard holding stubs is saved by a streaming merge: copy unchanged lines, replace dirty members, drop evicted and moved ones (deleted transcripts that are only stubs are still removed), append new ones, keep the published name when nothing changed. Nothing range-filtered or sliced is ever written.
  • Codex discovery reads a small codex-projects index written on flush.
  • status loads once for today and month. The pre-lock view is released before the canonical reload.
  • Providers whose parse reads cached entries outside the range (hermes, quickdesk, antigravity, devin, gemini) and durable providers still load in full.

Verification

Figures, against main, with frozen pricing: 108/108 outputs identical (overview, report, status text/json/menubar-json, sessions, models, export; today, yesterday, week, month, 3 months, all) on a real corpus (5,415 cached sessions), a synthetic heavy corpus and an edge corpus (sessions spanning midnight and three months, moved, shrunk and deleted files, forks, resumed copies, spawn anchors, WSL offline members), under local, Pacific/Kiritimati and America/Adak time zones, fresh and across upgrade and downgrade. Cache members identical, order included. Longer ranges never total less than shorter ones inside them. Serve child through period switches, churn and deletions: identical. Five processes plus a serve child at once, and 30 SIGKILLs mid-run: no damaged shard.

Speed and memory, warm, median of 3 to 5:

main this PR
real, overview -p today 0.84 s, 973 MB 0.66 s, 277 MB
real, status --format menubar-json 1.78 s, 1,076 MB 1.60 s, 353 MB
real, report -p all 2.05 s, 938 MB 1.89 s, 632 MB
heavy, overview -p today 4.3 s, 3,457 MB 3.6 s, 1,164 MB
heavy, report -p all 13.5 s, 3,758 MB 12.8 s, 3,682 MB

At a 1 GB heap cap on the heavy corpus main crashes on overview -p today and the menubar payload; this PR passes both.

tsc clean, full suite green except RootDeepProjectLabels.test.tsx, which fails identically on main in this checkout (duplicate React from a symlinked node_modules). test:locks 36/36. Seven new tests fail on main's source: stub dedup replay and first-paint order, stub-only eviction on disk, partial-bucket streaming merge and published-name reuse, legacy shard upgrade, codex-projects discovery, stub members equal to a full load.

Not covered here

The first run after upgrading still parses a huge legacy shard whole once. Heaps under ~768 MB on the heavy corpus stay out of reach while today's own data is ~430 MB. Per-day pieces, batched detail views and the cold path are the next stages.

A shard is still one JSON object, so older builds read it unchanged, but it
is written through a stream and never exists as one string. Past V8's string
limit the save used to throw and the cache silently never persisted; reads of
such a shard now go member by member.
A ranged load streams its in-scope shards and keeps in full only the files
with a call in the range, spawn anchors, failure markers, turn-less files and
WSL paths. Every other file becomes a stub: its fingerprint, bucket, PR flag,
dedup keys per turn and where its member line lives. A stub stands in for its
entry everywhere but the summaries it cannot contribute to: reconcile, the
seenMsgIds and parser dedup pre-seeds, the query-time key replay in serve
order, orphan surfacing, eviction and the emptiness checks. A stub whose file
changed loads its member first, so the append or re-parse runs as before, and
a wider request in the same process loads the stubs it now needs.

A bucket holding stubs is rewritten as a full load would write it, copying
the stubs' lines from the shard they came from; unchanged, the published
shard is kept. A single-line shard from an older build is rewritten on the
next save. Providers whose parse reads cached entries no report shows
(hermes, quickdesk, antigravity, devin, gemini) still load in full.
Discovery only needs each rollout's project, but it loaded the whole codex
results file (hundreds of MB on a large corpus) on every run to get it.
flushCodexCache now also writes codex-projects.v15.json, keyed by the same
fingerprint, and discovery reads that. A missing index is built once from the
results file.
status asks for today, then the month. A command can now declare the union of
its ranges, so the first load already holds the month in full and the second
request finds it there; each request keeps its own month scope and summaries.
Once the refresh lock is acquired or another process completed the refresh,
the snapshot loaded before the wait is never used again, but it stayed
referenced while the reload read a second view.
A range that spans a shard's months needs nearly every member, and streaming
it one member at a time only added allocation churn (about 200MB more RSS on
report -p all over a 1GB cache). The streaming reader also reuses one read
buffer instead of allocating a fresh one per chunk.
The previous rule also required the range to reach past the shard's newest
month, which a range ending now never does, so report -p all still streamed
every shard.
Members of a megabyte and more span the 1MB reads, and joining their pieces
as Buffers allocated off-heap memory that only a GC returns: report -p all
over a 1GB cache peaked about 170MB above main in RSS. Decoding the pieces
with a StringDecoder keeps them on the heap and brings every streamed range
below main, so the whole-file parse for wide ranges goes again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: cli The codeburn CLI and its core parsing/reporting engine enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

overview -p today crashes with heap OOM on large corpora (cache-load layer)

2 participants