Skip to content

fix: stop deep-cloning the model for snapshots, undo history, and slides - #81

Merged
tomasz-zajac-oss merged 1 commit into
mainfrom
fix/dedup-snapshot-memory
Sep 14, 2026
Merged

tomasz-zajac-oss merged 1 commit into
mainfrom
fix/dedup-snapshot-memory

Conversation

@tomasz-zajac-oss

Copy link
Copy Markdown
Collaborator

Summary

  • Undo history (_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).
  • The store is zustand + immer, and every mutation already goes through set((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).
  • One site (selectMilestone's preserveLayout restore) 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.
  • Also fixed the same pattern in setAppMode's __preModeLayout backup (found while auditing, same category of issue).
  • First step of a larger conversation about scaling model loading for very large diagrams — this is the low-risk, in-memory piece; on-disk format is untouched.

Test plan

  • npm run typecheck passes
  • npm run test — 375/375 tests pass (36 files), including new tests/snapshotMemorySharing.test.ts which specifically guards against reference-sharing leaking mutations across snapshots/undo/propagate
  • Manual smoke test via npm 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

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>
@tomasz-zajac-oss
tomasz-zajac-oss merged commit 0f1521c into main Sep 14, 2026
2 checks passed
@tomasz-zajac-oss
tomasz-zajac-oss deleted the fix/dedup-snapshot-memory branch September 14, 2026 18:25
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