fix(viewer): end IDS and rule-set runs when their model set changes mid-run

Merged
LO
22d ago
+611-92

Defect

An in-flight IDS (useIDS.runValidation) or rule-set (useInformationValidation.run) validation published its report even when the models it read changed while it ran. useValidationEpoch only moves on run, cancel and definition change. The teardowns that change the model set reset flags and nothing else:

  • removeModel / clearAllModels call clearIdsValidationReport(), which sets idsLoading: false.
  • The session reset ("open a new file", idsSlice.teardown.ts) also sets idsLoading: false.
  • A collab peer edit swaps a model's store, and nothing resets anything.

The run carried on. It kept writing idsProgress while idsLoading was already false, then published a report whose snapshot named the removed model and recoloured its entity ids. After a reload, those ids can belong to unrelated entities.

Scenario

  1. Load federated A+B and run IDS (or a rule set).
  2. Remove B, clear all, open a new file, or let a peer edit replace B's store.
  3. The run completes and publishes a report for A+B.

Fix

  • Shared check, moved from clash. The federation-identity token and the whole-federation check move from lib/clash/federation-identity.ts to lib/federation/federation-identity.ts, as federationIsCurrent, recordGatheredModel and the new captureFederationIdentity. Clash imports them from there with the same token and logic, so its behaviour is unchanged. The clash file keeps only the clash parts: the per-result identity and the per-ref checks.
  • New hooks/validation/validationRunGuard.ts. It captures the identity of the models the run actually read and the store's validationRunGeneration.
    • The models are passed in by the hook rather than read from getState(). useIDS passes the render-time models and ifcDataStore it resolved the target from, so a retained stale runValidation closure cannot look current.
    • It subscribes for the run's lifetime, the same pattern as lib/scan-to-bim/run-detection.ts. When either signal changes, it calls the hook's own cancel (epoch bump, abort, loading/progress reset), deferred to a microtask.
    • The publish site also checks isCurrent(). That check refuses a run whose captured models were already stale at start, and a change the subscription cannot see. A refused IDS run clears its own progress.
  • Teardown now ends the run. The session-reset teardown arm bumps validationRunGeneration.
  • Why two signals. A session reset keeps models (it only nulls the top-level ifcDataStore), so the identity alone misses it in a federated session. A peer-edit store swap bumps nothing, so the generation alone misses that. Removal and clear-all are caught by the identity.
  • Models added mid-run keep a federated run, the same rule clash uses. A legacy single-store run that sees a federated model added is cancelled. That cancellation is spurious but harmless: nothing wrong is published, and a comment on runModels notes it.

Clash, compare and flow teardown are not changed. The shared helper does not make any of them a one-line change.

Tests

All test runs were capped with systemd-run --user --scope -p MemoryMax=8G -p MemorySwapMax=0. No run hit the cap.

  • src/hooks/useIDS.federation-change-mid-run.test.tsx, 6/6 at head:

    • remove model, clear all, session reset, entity-table swap, and model added (still publishes);
    • a retained runValidation from before a removal refuses to publish for the removed model.
    • The store is restored between tests.
    • Before the fix, the remove, clear-all and session-reset cases failed.
  • src/hooks/validation/useInformationValidation.federation-change-mid-run.test.tsx, 4/4 at head:

    • remove model, session reset, and an in-place swap the subscription cannot see (refused at publish);
    • an untouched model set still publishes.
    • Before the fix, the remove and session-reset cases failed.
  • src/store/teardown-registry.test.ts, 6/6 at head. validationRunGeneration is in both PINNED_OWNED_KEYS and the session-reset write list.

  • Mutation checks, each run on both new files with the source restored afterwards. Each mutation makes the named tests fail:

    MutationTests that fail
    M3: remove the publish-site isCurrent() check from both hooksIDS stale-closure, rule-set in-place swap
    M4: the guard captures store.getState() instead of the run's modelsIDS stale-closure
    M5: drop the session-reset bumpboth session-reset tests (IDS and rule set)
  • Related suites at head: 94 files in one run, covering every useClash*, useIDS*, useInformationValidation*, idsSlice, teardown, lib/clash, lib/compare, useValidation*, validationDraft, modelLifecycle and clearAllModels test. Results: 777 tests, 776 pass, 0 fail, 1 skipped. The skip is the #8040 Revit fixture, which is absent locally.

  • check-module-size: OK. check-source-text-assertions: OK.

Typecheck: root pnpm typecheck passed through the fleet on 8a2358c83 (job c8971083, exit 0, all 4696 test files in a typecheck program).

Unverified

  • No manual browser run of the scenario.
  • The full viewer suite was not run locally; it runs in the merge queue.

šŸ¤– Generated with Claude Code