fix(viewer): keep saving 2D markup after a same-commit model round trip
Defect (implicit-state-machines, drawing persistence)
Save latch.
drawingMarkupSave.ts's raw store subscription arms the module-levelrestoringModelIdon everyactiveModelIdmove.- 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
restoringModelIdstays'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
activeModelIdmove, and the hook reads it throughsubscribeModelActivations/modelActivationEpochwithuseSyncExternalStore. 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;syncSourceModelre-runs model-removed), so the eviction runs in the entry points, after theirset(review follow-up):removeModelandclearAllModelseach make one call intomodelSlice.sessionRelease.ts(releaseRemovedModelSession/releaseAllModelSessions). That helper releases the appearance assets and the markup snapshots together. Its doc carries the ordering rule: it runs after theset, because thatset's markup transition snapshots the removed model when it was the active one.resetViewerStatecalls 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
clearAllModelsand afterresetViewerState. - Ordering: removing the ACTIVE model, whose markup the transition snapshots inside
removeModel'sset, 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 anduseViewControlsfile, plusmodelSlice*and the BCF capture tests), run undersystemd-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 fixturesplus a wasm build. pnpm typecheckat 2f9fb64b27, under the Pop node profile: 136/136 Turbo tasks, including viewer and viewer-embed.pnpm lintat 2f9fb64b27, run locally under admission with the dists built: exit 0. That covers every step throughcheck-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 packagedists.)check-module-size: OK.check-source-text-assertions: OK.check-unused-locals-baseline.test.mjs: 1/1.
Mutation table
| Mutation | Result |
|---|---|
| epoch dropped from the effect deps | killed (sameCommitReturn) |
| epoch not bumped | killed (sameCommitReturn) |
removeModel does not call the release | killed (eviction: removed; eviction: active model removed) |
clearAllModels does not call the release | killed (eviction: clear all) |
removeModel releases BEFORE its set | killed (eviction: active model removed) |
| helper's remove path forgets no markup | killed (eviction: removed; eviction: active model removed) |
| helper's clear-all path forgets no markup | killed (eviction: clear all) |
resetViewerState does not evict | killed (eviction: session reset) |
forgetLiveMarkup(id) clears everything | killed (eviction control: another inactive model removed) |
| subscription does not notify epoch listeners | survived. 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