Skip to content

Debounce Store.save() so streaming no longer rewrites the whole 15 MB store - #47

Merged
currentbits merged 3 commits into
mainfrom
coder/store-every-stream-event-rewrites-the-en-b015fa
Aug 15, 2026
Merged

currentbits merged 3 commits into
mainfrom
coder/store-every-stream-event-rewrites-the-en-b015fa

Conversation

@currentbits

Copy link
Copy Markdown
Owner

Fixes #27.

The problem

Store.save() (electron/store.js) did a JSON.stringify of all data plus a writeFileSync of the entire file, synchronously. It is called from ~97 production sites — including the per-block assistant/tool stream handlers in electron/runner.js and services.getThreadDetail, which full-saves on every thread selection just to stamp lastVisitedAt.

On a real store (15 MB / 7000+ messages) the stringify alone measures ~19 ms, so the main process stalled on every token block of every concurrent worker.

The change

Fixed at the shared function rather than at the hot call sites, so all ~97 callers benefit and no sibling path is left broken:

  • save() now sets a dirty flag and coalesces writes onto a 250 ms timer.
  • The old synchronous body moves to saveNow(), for callers that need the bytes on disk right now.
  • A process.once("exit") hook — registered only while a write is pending, removed on flush — writes synchronously on exit. That covers app quit, smoke.js and scripts/acceptance.js in one place, with no per-caller quit wiring and no listener leak across the many Store instances tests create.
  • The debounce timer is unref'd so it never holds the event loop open.

A burst of N stream events that used to cost N × (19 ms stringify + 15 MB write) now costs one, at most 250 ms later. Worst case on SIGKILL is 250 ms of stream text.

Tests

  • Two new cases in electron/test/store.test.js: 20 save() calls produce exactly one writeFileSync, and saveNow() flushes a pending debounced save.
  • Tests that read the file back switch to saveNow() (mechanical rename).
  • Five tests relied on a services.* call writing through synchronously before reloading from disk; they now flush first.

Verification

  • node --test electron/test/store.test.js — 32/32 pass, including both new cases.
  • npm run test:electron — the failing set is byte-identical to the pre-change baseline (verified by diffing sorted not ok lines before and after). This worktree fails a large block of provider/gh-dependent tests either way; no test regressed.
  • npm run typecheck — clean.

Not included

Deliberately left as separate work, both noted in a ponytail: comment on save() or in the issue:

  • Async writes and per-thread message files. The remaining cost is the sync stringify on the flush tick, which async I/O would not remove.

  • Pruning the 41-of-48 archived threads that grew the store to 15 MB. That is what caused the size and is worth doing independently of this fix.

  • PR created by the Claude Code agent

Willem and others added 3 commits August 15, 2026 12:40
… Create an OG card for twitter etc to put on the website make
… Agent task shows 'Starting agent' and 'Agent working' checkm
store.save() did a JSON.stringify of all data plus a writeFileSync of
the entire file, and ~97 production call sites hit it -- including the
per-block assistant/tool stream handlers in runner.js and
services.getThreadDetail on every thread selection. On a real 15 MB
store the stringify alone measures ~19 ms, blocking the main process on
every token block of every concurrent worker.

save() now sets a dirty flag and coalesces onto a 250 ms timer; the old
synchronous body moves to saveNow(). Every existing caller gets the
coalescing for free. A process "exit" hook registered only while a write
is pending flushes synchronously, so app quit, smoke.js and
acceptance.js stay durable without per-caller wiring; the timer is
unref'd so it never holds the loop open.

Tests that read the file back switch to saveNow(), and five that relied
on a services.* call writing through synchronously now flush first.

Co-Authored-By: Claude Opus 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 c136b32 into main Aug 15, 2026
1 check failed
@currentbits
currentbits deleted the coder/store-every-stream-event-rewrites-the-en-b015fa branch October 3, 2026 16:07
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.

Store: every stream event rewrites the entire 15 MB store synchronously

1 participant