Skip to content

Cap per-thread transcripts and flush the store off the event loop (fixes #89) - #121

Merged
currentbits merged 1 commit into
mainfrom
coder/cap-transcript-growth-and-stop-whole-sto-ecb01d
Aug 16, 2026
Merged

currentbits merged 1 commit into
mainfrom
coder/cap-transcript-growth-and-stop-whole-sto-ecb01d

Conversation

@currentbits

Copy link
Copy Markdown
Owner

Why

A few heavy long-lived threads progressively stalled the main process during active runs (#89, found in the 2026-08-16 improvement audit):

  • appendMessage/appendWorkLog never capped transcript size, so the one-JSON-blob store grew to megabytes.
  • Every debounced flush re-stringified the entire blob synchronously (~19 ms at 15 MB) plus a synchronous writeFileSync on the event loop.
  • lastPushByThread in the runner retained per-thread message arrays, unbounded, for the process lifetime.

What changed

  • Per-thread retention caps (electron/store.js): 1000 messages / 500 work-log items per thread, with overflow slack (100/50). Crossing cap + slack drops the oldest entries back to the cap and plants a single event marker so the gap is visible in the transcript instead of silent. The slack amortizes the index-shifting drop, which breaks the runner's prefix diff and forces one full transcript push, over ~100 appends instead of every append. The renderer already merges shrinking transcripts correctly (src/threadPatch.ts).
  • Async debounced flush: save()'s flush now stringifies once per burst (bounded by the caps) and writes tmp-then-rename via fs.promises, off the event loop. saveNow() stays synchronous for the exit hook, shutdown and tests. A write-generation counter keeps an in-flight async flush from renaming a stale payload over a saveNow(); stale tmp files are discarded. Flush failures re-mark dirty and retry instead of throwing async.
  • lastPushByThread (electron/runner.js): retains shallow slices sharing element refs with the store, so with the caps its per-thread footprint is bounded. Comments updated.
  • Exit hook is now armed once instead of registering one listener per save() call.

Not taken: per-thread store files (the heavier option from the issue). The stringify itself still runs on the main thread but is now bounded by the caps; if profiling shows it again, per-thread files are the next step.

Tests

  • New: cap boundary at cap+slack untouched, marker shape and single-marker-on-append, work-log cap, coalesced async flush, and a gated-write test proving a stale async flush never renames over a saveNow().
  • Electron suite: 767/768. The one failure (budget-spend.test.js settings shape, missing notifications key) reproduces on the clean base and is pre-existing from the Desktop-notification preference: global toggle and per-thread mute #87 work, unrelated to this change.

Docs: decision logged in docs/ISSUES.md; Store section of docs/ARCHITECTURE.md updated.

  • PR created by the Kimi Code agent

@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 0cf65b7 into main Aug 16, 2026
1 check failed
@currentbits
currentbits deleted the coder/cap-transcript-growth-and-stop-whole-sto-ecb01d 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.

1 participant