Skip to content

perf(store): throttle envelope writes, cap parsed transcripts (#1475) - #1481

Merged
currentbits merged 4 commits into
mainfrom
coder/perf-f-store-stop-envelope-rewrites-fc61c9
Oct 5, 2026
Merged

currentbits merged 4 commits into
mainfrom
coder/perf-f-store-stop-envelope-rewrites-fc61c9

Conversation

@currentbits

Copy link
Copy Markdown
Owner

Part of #1475, main-process finding 1 (rewriting the envelope while streaming took 92% of main-process JS time).

What changed

  • Throttled envelope writes. coder-store.json is written at most once every 2 s from the debounced save() path. Transcript and work-log files keep the 250 ms debounce. saveNow() (quit, shutdown, tests) isn't throttled. A flush that deletes a transcript file always writes the envelope in the same commit, so a crash can't leave a thread row whose transcript is already gone.
  • Capped transcript cache. At most 8 parsed transcripts are kept in memory, least recently read evicted first. A transcript is only evicted if its file on disk is current: never while its thread is working, has unsaved changes, is mid-flush or is still on the legacy blob. Every write path already copies the array before changing it (appendMessage, updateMessage and setMessages all slice), so no caller holds an array it later edits in place.

Verified independently

  • Benchmark (scripts/perf/store-stream-bench.js), run on a clone of the real store (5.8 MiB envelope), 20 s simulated stream:

    main branch
    envelope writes 73 (21.1 MiB/s) 10 (2.9 MiB/s)
    main-thread stringify 1,267 ms 171 ms
  • Randomized stress test: 3,000 ops across 30 threads (append, update, read), with eviction active. No mismatches against the in-memory model, live or after reload. Browsing 30 threads peaks at 10 transcripts in memory; main keeps all 30.

  • Electron suite: 4132 of 4132 passing.

  • New tests: store-flush-throttle.test.js fails on main (7 failures).

Trade-offs

  • If the app crashes (not a normal quit), it can lose up to 2 s of envelope changes: thread rows, usage and settings. Transcripts aren't affected.
  • The cap is soft. Eviction runs only when a transcript is loaded from disk, and it skips transcripts with unsaved changes, so you can briefly be above 8.

🤖 Generated with Claude Code

Willem and others added 4 commits October 5, 2026 20:32
Every debounced flush rewrote the whole ~6 MB envelope, even when a stream
had only appended to its message shard. Track envelope dirtiness apart from
the shard dirty sets: a 250 ms flush now writes only the dirty shards, and
the envelope rides along at most once per ENVELOPE_THROTTLE_MS (2 s).
Shard deletes still commit together with the envelope. saveNow(), the exit
hook and every quit path still write the envelope immediately and do not
start a throttle window. Atomic tmp + fsync + rename and the .bak are
unchanged.

Trade-off: a crash (not a quit) can lose up to 2 s of envelope-only changes
(thread rows, usage, settings); transcripts and work logs are unaffected.

scripts/perf/store-stream-bench.js, one 30 s stream on a /tmp copy of the
real 5.8 MiB store:
  main:   99 envelope writes, 571.78 MiB (19.02 MiB/s), stringify 3920 ms
  branch: 14 envelope writes,  80.86 MiB ( 2.70 MiB/s), stringify  459 ms

Co-Authored-By: Claude Opus 5.5 <[email protected]>
_messagesHydrated kept every opened thread's parsed transcript for the
session (+3-5 MB heap per large thread, never released). Track read order
and, past MAX_HYDRATED_THREADS (8), drop the least recently read thread back
to its shard; the next read re-parses it. A thread is only evicted when its
shard on disk is current: never while working, dirty, mid-flush, deleted or
still on the legacy inline blob.

Real-data load of a /tmp copy (2,103 threads, all 315,379 messages read):
same sha256 of envelope + work logs + transcripts as main, with 8 threads
left hydrated instead of 2,103.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Appends inside the window write the shard only; envelope changes coalesce;
saveNow and the exit hook still write at once; a crash loses at most the
window; shard deletes commit with the envelope; the cache keeps running
and dirty threads and rehydrates evicted ones. All but the delete guard
fail against main's store.js.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replays PM1's stream shape (40 deltas/s, assistant message every 1.8 s,
tool call every 2 s, 30 s) through the runner's Store calls on a clone of a
/tmp copy of the real store; refuses paths outside /tmp.

  main:   99 envelope writes, 571.78 MiB (19.02 MiB/s), stringify 3920 ms
  branch: 14 envelope writes,  80.86 MiB ( 2.70 MiB/s), stringify  459 ms

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@currentbits
currentbits merged commit 060d82a into main Oct 5, 2026
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant