fix(viewer): keep saving 2D markup after a same-commit model round trip

Merged
LO
21d ago
+301-14

Defect (implicit-state-machines, drawing persistence)

Save latch.

  • drawingMarkupSave.ts's raw store subscription arms the module-level restoringModelId on every activeModelId move.
  • Only useDrawing2DPersistence's restore effect (endRestore) disarms it, and that effect was keyed on [activeModelId, activeSourceFile].
  • When the model goes A→X→A before React renders, the commit sees no change, the effect never runs, and restoringModelId stays 'A'.
  • From then on, every markup save and section-config save for A is dropped (if (restoringModelId === modelId) return;) until another switch.

Reachability (verified by reading). recordRun (lib/model-placement/history.ts:70) calls setActiveModel(modelId) for each model it records for. bulk-target-run.ts records every group of a chunk synchronously and yields only between chunks. So a bulk edit over federated models whose first group belongs to X while A is active switches A→X→A in one task, and React commits that as a single update.

liveMarkupCache was never cleared. Its doc claimed stale entries were harmless because ids are crypto.randomUUID() and never reused. They are reused: room:<id> for a collab room, and explicit ids from flows (lib/flow/open-model.ts, local-models.ts). A removed model's snapshot was restored synchronously into the next model given its id. The restore effect usually overwrites it later, but the next model starts out showing the old markup.

Fix

  • The restore effect now also keys on an activation epoch. The save subscription bumps it on every activeModelId move, and the hook reads it through subscribeModelActivations/modelActivationEpoch with useSyncExternalStore. Every activation now restores and disarms the latch, including a same-commit return, which behaves like an ordinary revisit.
  • forgetLiveMarkup(modelId?) evicts snapshots together with their model. Teardowns stay pure patches (store/teardown.ts; syncSourceModel re-runs model-removed), so the eviction runs in the entry points, after their set (review follow-up):
    • removeModel and clearAllModels each make one call into modelSlice.sessionRelease.ts (releaseRemovedModelSession / releaseAllModelSessions). That helper releases the appearance assets and the markup snapshots together. Its doc carries the ordering rule: it runs after the set, because that set's markup transition snapshots the removed model when it was the active one.
    • resetViewerState calls it after its composed patch.

Module-size budget. modelSlice.ts sits exactly at its 630-line allowlist budget on main, and the earlier revision added 3 lines there (it failed the merge queue's Node guards). The helper keeps modelSlice.ts at 630 lines, net 0 vs main (+3/−3: one import replaced, one call per action). That is not golfing: the two per-model side effects moved into one named unit. The budget was not raised, and since the file stays at 630, its allowlist row is unchanged.

Scenario → test

useDrawing2DPersistence.sameCommitReturn.test.tsx. Model A has a saved measurement. Inside one act, the test calls setActiveModel('model-x'); setActiveModel('model-a'). A measurement drawn afterwards must be saved.

  • Base: ✖. The entry stays ['before']; the new measurement is never saved.
  • Fixed: ✔.

drawing2DSlice.liveMarkupEviction.test.ts (real store). room:1 is left with markup, then removed and re-added with the same id. Activating it must not show the old markup.

  • Base: ✖ (['old']).
  • The same check runs after clearAllModels and after resetViewerState.
  • Ordering: removing the ACTIVE model, whose markup the transition snapshots inside removeModel's set, must not leave that markup for the next model with its id.
  • Controls: a model that is still loaded keeps its snapshot, and so does a model when a different, inactive model is removed.

Commands (Pop; deps copied from the shepherd build)

Rebased onto origin/main 94ca2db4d6 (after #9534, #9540). Package dists were rebuilt for that base (TS only, no wasm).

  • Drawing/markup + modelSlice family at 2f9fb64b27, 100 files (every *rawing*, teardown, markupTransition, persistence and useViewControls file, plus modelSlice* and the BCF capture tests), run under systemd-run -p MemoryMax=8G … tsx --test: 674/674, 0 skipped.
  • Earlier, at 8a049a41d9: a wider 114-file run including the collab room files passed 827/828. The 1 skip is a collab room contract test that needs pnpm fixtures plus a wasm build.
  • pnpm typecheck at 2f9fb64b27, under the Pop node profile: 136/136 Turbo tasks, including viewer and viewer-embed.
  • pnpm lint at 2f9fb64b27, run locally under admission with the dists built: exit 0. That covers every step through check-lint-ran, including unused-locals, with 0 eslint errors and no warning in a touched file. (The earlier fleet failure was its clean checkout having no built package dists.)
  • check-module-size: OK. check-source-text-assertions: OK. check-unused-locals-baseline.test.mjs: 1/1.

Mutation table

MutationResult
epoch dropped from the effect depskilled (sameCommitReturn)
epoch not bumpedkilled (sameCommitReturn)
removeModel does not call the releasekilled (eviction: removed; eviction: active model removed)
clearAllModels does not call the releasekilled (eviction: clear all)
removeModel releases BEFORE its setkilled (eviction: active model removed)
helper's remove path forgets no markupkilled (eviction: removed; eviction: active model removed)
helper's clear-all path forgets no markupkilled (eviction: clear all)
resetViewerState does not evictkilled (eviction: session reset)
forgetLiveMarkup(id) clears everythingkilled (eviction control: another inactive model removed)
subscription does not notify epoch listenerssurvived. The intermediate activeModelId value already schedules the render that reads the new epoch, so the notification isn't observable today. It keeps useSyncExternalStore's subscribe contract honest.

🤖 Generated with Claude Code