fix: stop deep-cloning the model for snapshots, undo history, and slides - #81
Merged
Merged
Conversation
Undo history, milestone snapshots, and presentation slides each kept a
full JSON.parse(JSON.stringify(...)) copy of c4Nodes/c4Relations on every
capture, so memory scaled with model size × (undo depth + milestone count
+ slide count). Since the store is zustand+immer and every mutation goes
through set((state) => {...}) producers, past state is never mutated in
place — a captured reference stays correct forever, with new objects
produced only for the paths that actually change. Switch snapshot/history/
slide capture to reference the live model directly instead of cloning it,
relying on that copy-on-write guarantee (Immer's own documented undo
pattern). One site (selectMilestone's preserveLayout restore) still needs
a real clone since it mutates specific nodes outside a producer — that one
does a shallow map copy instead of a full deep clone.
Adds a regression test asserting the sharing doesn't leak: editing the
live model after a snapshot, undo/redo across multiple edits, and
propagating a milestone edit forward must all leave older snapshots exactly
as they were.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_captureState, up to 100 entries), milestone snapshots (createSnapshot/restoreSnapshot/selectMilestone/commitMilestoneChanges), and presentation slides (addPresentationSlide/goToSlide/previewSlide/captureSlideViewport) each deep-cloned the full model (JSON.parse(JSON.stringify(...))) on every capture — memory scaled with model size × (undo depth + milestone count + slide count).zustand+immer, and every mutation already goes throughset((state) => {...})producers, so a past state reference is never mutated in place — Immer produces new objects only for the paths that change. Switched all of these capture sites to reference the live model directly instead of cloning it (Immer's own documented undo-history pattern).selectMilestone'spreserveLayoutrestore) mutates specific nodes outside a producer, so it now does a shallow map copy instead of a full deep clone, rather than a blind reference swap.setAppMode's__preModeLayoutbackup (found while auditing, same category of issue).Test plan
npm run typecheckpassesnpm run test— 375/375 tests pass (36 files), including newtests/snapshotMemorySharing.test.tswhich specifically guards against reference-sharing leaking mutations across snapshots/undo/propagatenpm run dev:web+ headless Chromium on a real 29-node/25-relation sample model: created/selected milestones, edited while a milestone was active (dirty prompt), committed via both Propagate and New milestone, added/previewed presentation slides across different view types (structure, sequence, hierarchy), ran live Present mode with slide navigation, and exercised undo/redo — no console errors, no data/visual corruption🤖 Generated with Claude Code