feat(viewer): save and reopen a portable federation setup
Summary
Closes #3930.
Lets a user save which models make up a federation — load order, visibility, and the alignment anchor — to a small portable JSON file, and reopen it later (possibly on a different machine), matching saved slots back to local files.
What the file references vs. embeds
Each slot stores the model's original filename, byte size, and a content fingerprint (computeSourceFingerprint, the same sampled 64-bit hash the geometry cache already uses). It never embeds file bytes, an absolute path, a FileSystemFileHandle, or credentials — reopening always requires re-selecting the actual files. This keeps the format both small and genuinely portable.
Alignment is replayed, not baked in
Rather than serialize per-model transform matrices, the file records only which slot was the federation anchor. On reopen, every matched model is re-added through the canonical addModel load path and realignFederation() is re-run against the restored anchor — the same alignment logic a fresh multi-file load performs. This avoids a second alignment pipeline and keeps the file simple.
Missing/mismatched models are always visible
Matching is one-to-one and content-first: content fingerprint, then name+size, then name-only (flagged as a possible collision), then missing. applyFederationSetup always reports restored / partial / failed plus explicit missing and mismatched slot names — never a bare "success" when some models weren't found. A review dialog shows every slot's status before anything is applied.
Determinism
Slots are built from Array.from(models.values()) (a Map's insertion order = actual load order, never re-sorted) and serialized with an explicit, fixed key order — no hash-map iteration ordering and no embedded timestamp, so saving the same federation state twice produces byte-identical JSON (tested).
Where it's reachable
Command palette: "Save Federation Setup" / "Open Federation Setup" (dispatches window events handled by FederationSetupControls, mounted once from useFileCommands — the same pattern ShareDialog uses — so it works regardless of which toolbar style is active).
Analogue matched
The viewer's own handleRefresh (toolbar/useFileCommands.tsx) — "rebuild the federation from fresh bytes, preserving id + order + state" via sequential addModel(...) calls — is the closest existing pattern, since the CLI's delivery-recipe.ts saved-definition file referenced in earlier guidance does not exist on upstream/main yet (still open as #3934). Content fingerprinting reuses the existing sourceFingerprint.ts / cache-key pattern rather than inventing a new hash.
Test plan
-
apps/viewer/src/lib/federation/federationSetupFile.test.ts— 16 tests: round-trip, determinism (two saves of the same state are byte-identical), strict/loud validation of malformed files (bad version, zero slots, two anchors, missing field), and the keyed-write trap (two federated models sharing a filename resolve correctly by content, one-to-one, regardless of file pick order). - Mutation testing: removed the anchor-uniqueness check, the one-to-one match guarantee, the format-version check, and the "resolved" classification — each caught by a test.
-
pnpm typecheck(viewer + all deps) — clean. -
node scripts/check-module-size.mjs,check-test-wiring.mjs,check-source-text-assertions.mjs— all pass (CommandPalette.tsx addition trimmed to stay at its existing 849-line budget). - Not unit-tested (UI wiring, thin glue over already-tested primitives):
useFederationSetup.ts's calls intoaddModel/realignFederation, andFederationSetupControls.tsx's dialog/file-input wiring. Left to CI/manual QA per the viewer's runner constraints. - Full 666-file viewer suite left to CI (not run locally per repo guidance); ran the targeted 102 tests covering touched files, toolbar/export parity, command palette, and federation load-gate/model-slice — all green.
🤖 Generated with Claude Code
https://claude.ai/code/session_01QPHChk3Ve9N519A4kY7436
Summary by CodeRabbit
- New Features
- Save the current federation setup—including model order, visibility, collapsed state, and alignment anchor—for later reuse.
- Reopen saved setups by selecting the setup file and matching local model files.
- Review matched, mismatched, and missing models before restoring.
- Receive clear results for complete, partial, or failed restorations, including alignment-anchor status.