Repository navigation
Skip no-op state writes and bound save postponement in persistence - #7092
Conversation
orca-data.json (1.6MB live) was fully rewritten (pretty-print + tmp + rename) on every debounced save even when the state content had not changed - measured 3 rewrites/min (~5MB/min) on an idle production instance, and a sync-flush storm of identical multi-MB writes at every warm start via persistPtyBinding re-binds. - Content-hash guard in both writers: a save whose plaintext state hash matches the last successful write skips serialize+write+rename entirely. Hashing plaintext (not the payload) because encrypt() uses a random IV per call. Safe under flushOrThrow's durability contract: a matching hash means the file already holds exactly this state. - Debounce 300ms -> 1s trailing with a 5s max-wait. The old timer reset on every mutation with no bound, so sustained sub-interval mutation bursts could postpone the write indefinitely; now staleness is capped at 5s while bursts coalesce. Co-authored-by: Orca <[email protected]>
From adversarial review: an async writer that had already passed its generation check and dispatched the rename could have a sync flush interleave during the await, write fresher state, and record its hash - the async continuation then clobbered lastWrittenStateHash with a value describing content NOT on disk, making later saves (including the quit flush) silently skip. Re-check writeGeneration before recording. Twin edge: a hash-matching sync flush could skip its write while a stale dispatched rename lands afterwards, leaving stale content unchallenged. flushOrThrow now forces the sync write whenever an async write chain was in flight at entry, restoring the sync-write-last disk ordering. Co-authored-by: Orca <[email protected]>
|
Caution Review failedAn error occurred during the review process. Please try again later. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis change modifies the Store persistence write strategy in src/main/persistence.ts to use SHA-1 content hashing and a bounded debounce/max-wait mechanism, skipping redundant disk writes when state content is unchanged. New fields track the last written state hash and burst start time, and writeToDiskAsync/writeToDiskSync/flushOrThrow are updated to coordinate generation-guarded writes and forced durability during flush. Corresponding tests in src/main/persistence.test.ts are updated to use a 1000ms debounce interval (previously 300ms) and new tests are added covering write-skipping scenarios based on content/inode equality. Changes
Sequence Diagram(s)Included in the hidden review stack artifact above. Related Issues: Unable to determine from provided context. Related PRs: Unable to determine from provided context. Suggested labels: persistence, tests, performance Suggested reviewers: Unable to determine from provided context. 🐰 A hash to check before I write, ✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…tablyai#7092) * Skip no-op state writes and bound save postponement in persistence orca-data.json (1.6MB live) was fully rewritten (pretty-print + tmp + rename) on every debounced save even when the state content had not changed - measured 3 rewrites/min (~5MB/min) on an idle production instance, and a sync-flush storm of identical multi-MB writes at every warm start via persistPtyBinding re-binds. - Content-hash guard in both writers: a save whose plaintext state hash matches the last successful write skips serialize+write+rename entirely. Hashing plaintext (not the payload) because encrypt() uses a random IV per call. Safe under flushOrThrow's durability contract: a matching hash means the file already holds exactly this state. - Debounce 300ms -> 1s trailing with a 5s max-wait. The old timer reset on every mutation with no bound, so sustained sub-interval mutation bursts could postpone the write indefinitely; now staleness is capped at 5s while bursts coalesce. Co-authored-by: Orca <[email protected]> * Guard the state-hash against sync-flush interleaving mid-rename From adversarial review: an async writer that had already passed its generation check and dispatched the rename could have a sync flush interleave during the await, write fresher state, and record its hash - the async continuation then clobbered lastWrittenStateHash with a value describing content NOT on disk, making later saves (including the quit flush) silently skip. Re-check writeGeneration before recording. Twin edge: a hash-matching sync flush could skip its write while a stale dispatched rename lands afterwards, leaving stale content unchallenged. flushOrThrow now forces the sync write whenever an async write chain was in flight at entry, restoring the sync-write-last disk ordering. Co-authored-by: Orca <[email protected]> --------- Co-authored-by: Orca <[email protected]>
Follow-up to #7068/#7088 (fseventsd + write-churn audit): second of the deferred write-churn findings.
Problem
orca-data.json(1.6MB on the reference machine) is rewritten in full — pretty-printed serialize, tmp write, rename — on every debounced save, even when state content hasn't changed. Measured on an idle production instance: 3 rewrites/min, ~9.8MB written per 2 minutes (~7GB/day at idle rate; active use peaks far higher at the old 300ms cadence). Warm starts additionally trigger a sync-flush storm: every restored terminal re-asserts an identical PTY binding throughpersistPtyBinding, each forcing a full synchronous multi-MB write.Two defects compound:
Fix
encrypt()uses a random IV per call (byte-identical state produces different ciphertext every time). Safe underflushOrThrow's durability contract: a matching hash means the file already contains exactly this state. The hash is only recorded after a successful rename (and, on the async path, only when the write-generation check passes, so a racing sync flush's fresher hash is never clobbered).Evidence
(The residual writer — poll-refreshed cache entries with timestamps living inside the durable state file — is a separate design question, noted for follow-up.)
Unit (asserted): a mutation burst that returns to already-persisted state produces no write (inode unchanged — every real write is a tmp+rename, so an unchanged inode proves no I/O);
flush()on clean state skips; an identicalpersistPtyBindingre-bind (the warm-start storm) skips; sustained 500ms-interval mutations still hit disk within the 5s max-wait. 326 persistence tests pass (10 existing timing tests updated 300ms → 1s), plus 108 tests in adjacent suites.Trade-offs
JSON.stringify+ sha1 (~5-8ms on 1.6MB, off the critical path); no-op saves get dramatically cheaper (no serialize-for-disk, no I/O).Review
An independent adversarial review traced six invariants (hash-vs-disk coherence across all writer interleavings,
flushOrThrowdurability at every call site, max-wait math, backup rotation, startup seeding, external writers). It confirmed the design — including that backup rotation strictly improves (the old idle churn rotated five identical hourly snapshots, aging out distinct history) — and found one real race, fixed in the second commit: recording the hash after the async rename needed a write-generation re-check, and a hash-matching sync flush must force-write when an async rename was already dispatched, so the sync write always lands last.Made with Orca 🐋