fix(viewer): preserve concurrent source catalog loads

Merged
LO
21d ago
+648-96

Concurrent folder loads and pagination could erase ready files or children, hide the selected folder behind another folder’s spinner, or let a retired request release a replacement lock. Initial area sync could also replace files loaded concurrently in a child folder.

Track on-demand work per container, scope the spinner to the selected container, reserve global flags for initial area sync, and preserve generation-owned locks. Pagination uses useAsyncRun for reentry, cancellation, and owner-checked settlement. Reset, replacement, manual sync, and unmount retire pending work; late results are rejected even when a provider ignores abort. Area sync clears old files at its start and merges its root page into the live file map; opening the area root does not start a duplicate eager sweep.

The fixes cover these review scenarios:

  • Folder refresh racing an open: track parent keys opened in the current sync generation, including opens whose fetch has already finished. Replace the area page while retaining only those parent pages from the live map; retire untouched old parent pages. The merged map is published to both state and the current persistence ref.
  • Uncached children: restore selectedContainerLoading to folder loading. The selected folder shows a labelled Subfolders spinner while its child list is empty. Its existing tree stays mounted so expanded branches survive. The child spinner clears when children arrive even if files remain pending.
  • Failed manual sync: a folder-only catalog cannot establish that a folder has no files. Empty-folder gating requires successful folder and file listings, and remains off during a file fetch or after failure. A successful empty file listing can still establish empty folders.
  • Complete persistence: useCatalogPages publishes each map and its current ref together before React commits. Sync and pagination persist from the current folder and file maps, so either completion order writes the combined complete catalog. On-demand writes and pages merge through that same setter; stale snapshots no longer decide persistence.

The earlier four regressions failed first on the rebased reviewed code: missing selected-folder loading, disabled folders after file failure, and missing complete cache in both completion orders. They now pass through mounted SourceBrowser UI and the mounted catalog hook with actual localStorage persistence. Cache reopening restores both folder pages and the files. The new spinner test also holds files after children arrive, preventing file loading from masquerading as proof of folder loading.

The new held-folder-refresh regression failed first at reviewed head 88cdee33c09fae7eb4c0f622d18c9bead778c23c: both mounted UI and hook tests lost Y’s completed child page. The area-first control already passed. The fixed tests assert that Y survives with its spinner off and X’s old, unreopened parent page is retired. Both completion orders remain covered.

Based on main 004934fa88572335b3d6762dad89158bd0c34b52. Validated head e79b2a099f72667969f89739c8d70a9458f461cd on Pop!_OS, Node 24.22.0, pnpm 10.8.1. Focus: 54 passed. Full touched families: 284 passed, 0 skipped. Every command below runs with an 8 GiB hard limit, zero swap, and two CPUs.

CommandResult
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 -p CPUQuota=200% -- env TEST_PATTERN='SourceBrowserFavouriteJump|useSourceCatalogSync|sourceCatalogSelectors' pnpm test --filter=@ifc-lite/viewer --only54 passed
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 -p CPUQuota=200% -- env TEST_PATTERN='components/sources/|lib/sources/|useAsyncRun' pnpm test --filter=@ifc-lite/viewer --only284 passed, 0 skipped
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 -p CPUQuota=200% -- python3 /tmp/aee6-legacy-check.pyPassed; no useSingleFlight references in changed viewer files
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 -p CPUQuota=200% -- pnpm install --frozen-lockfile --ignore-scriptsPassed
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 -p CPUQuota=200% -- env NODE_OPTIONS=--max-old-space-size=8192 TURBO_CONCURRENCY=1 pnpm typecheck:ciPassed; viewer, viewer-embed and test programs
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 -p CPUQuota=200% -- env AEE3_LINT_TSC_LOCK=/tmp/aee12-tsc-sources.lock NODE_OPTIONS=--import=file:///tmp/aee3-serialize-tsc.mjs pnpm lintPassed, including unused locals; no warnings in touched files
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 -p CPUQuota=200% -- node scripts/check-module-size.mjsPassed
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 -p CPUQuota=200% -- node scripts/check-source-text-assertions.mjsPassed
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 -p CPUQuota=200% -- node --test scripts/check-unused-locals-baseline.test.mjsPassed
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 -p CPUQuota=200% -- node scripts/pre-handoff.mjsPassed

Root pnpm build --filter=<package> --only refreshes @ifc-lite/{data,wasm-lifecycle,geometry,mutations,export,cache,clash,create,collab,rhino3dm,cli,renderer} under the same cap. Root typecheck:ci retains the full test-program coverage tail. No Rust/WASM build was run.

Validation ran directly in an isolated checkout of the same committed tree, under the requested caps, leaving the earlier queued fleet request and benchmark untouched. The lint command serializes compiler children to fit within 8 GiB, preserving flags, diagnostics and baselines. The preload was verified with a three-process lifetime-lock fixture.

Compiler serialization preload
import { openSync } from 'node:fs';
import { spawnSync } from 'node:child_process';

// Bound the existing lint worker pool's compiler overlap without changing diagnostics.
if (process.argv[1]?.endsWith('/typescript/bin/tsc')) {
  const path = process.env.AEE3_LINT_TSC_LOCK;
  if (!path) throw new Error('AEE3_LINT_TSC_LOCK is required for serialized lint');
  const descriptor = openSync(path, 'a', 0o600);
  const result = spawnSync('flock', ['--exclusive', '3'], {
    stdio: ['ignore', 'ignore', 'pipe', descriptor],
  });
  if (result.error || result.status !== 0) {
    throw new Error(`Cannot acquire TypeScript lint lock: ${result.error?.message ?? result.stderr}`);
  }
  // Keep this descriptor open: Linux flock follows the shared open description
  // after the flock child exits, and the compiler's exit releases it.
}

Pre-push allowlist comparison against fresh origin/main 004934fa88572335b3d6762dad89158bd0c34b52 uses the repository’s line counter and allowlist parser. No allowlist rows changed.
No changed file is allowlisted; all changed production modules remain within 400 lines.

Mutation command, one restored mutation at a time: systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 -p CPUQuota=200% -- pnpm test --filter=@ifc-lite/viewer --only, with TEST_PATTERN set to SourceBrowserFavouriteJump, useSourceCatalogSync, or those two plus sourceCatalogSelectors. All 15 checks rerun at this head were killed by behavioral tests; no survivors. Six target generation reset, open registration, completed-open retention, retirement and the still-pending parent case; nine recheck the selected-folder spinner, failed-sync gating and current-map persistence invariants. The previously deleted selected-folder loading term is restored and independently killed by the uncached-folder probe.

Current guard / invariantNamed failing test
sync-parent-generation-reset#9567 folder refresh preserves this generation's children and retires old parents: children first
sync-parent-registration#9567 folder refresh preserves this generation's children and retires old parents: children first
sync-parent-retain#9567 folder refresh preserves this generation's children and retires old parents: children first
sync-parent-retire#9567 folder refresh preserves this generation's children and retires old parents: children first
sync-parent-missing-page#9567 folder refresh preserves this generation's children and retires old parents: area first
selected-folder-spinner#9567 an uncached selected folder shows its own folder spinner while child fetch is held
selected-folder-empty-loading#9567 an uncached selected folder shows its own folder spinner while child fetch is held
selected-folder-loaded-children#9567 an uncached selected folder shows its own folder spinner while child fetch is held
complete-needs-files#9567 failed manual recursive file sync leaves subfolders enabled
current-catalog-snapshot#9567 overlapping sync and pagination persist the complete live catalog: files first
published-catalog-state#9567 overlapping sync and pagination persist the complete live catalog: files first
merge-current-pagesC-5 load more folders preserves a folder opened while the page was pending
persist-current-folders#9567 overlapping sync and pagination persist the complete live catalog: files first
persist-current-files#9567 overlapping sync and pagination persist the complete live catalog: files first
sync-parent-completed-open#9567 folder refresh preserves this generation's children and retires old parents: children first
Retained ownership mutation evidence (24 killed at the previous reviewed head)

These guards and their behavior remain unchanged. The three earlier per-map merge mutations are superseded by the new shared merge-current-pages check above.

GuardNamed failing test
source-generation-deleteKilled — C-5 an old folder completion cannot release a replacement generation folder
source-release-openKilled — #9567 opening a cached area releases an old pagination flight without starting another sync
source-release-resetKilled — C-5 reset releases the old pagination owner immediately
source-folder-flightKilled — C-5 same-tick folders load-more presses request one page
source-file-flightKilled — C-5 same-tick files load-more presses request one page
source-sync-folder-lockKilled — #9567 manual sync retires folder locks without letting old finally release the new request
source-sync-paging-releaseKilled — #9567 manual sync retires the pagination flight before the old page resolves
container-startKilled — survives the folders-before-files window of a direct-children provider
container-settleKilled — does not call a favourite gone while its folder has file pages outstanding
container-resetKilled — #9567 manual sync retires folder locks without letting old finally release the new request
container-selectedKilled — #9567 selected Y files render while X is still loading with eager per-folder files
container-spinner-deliveryKilled — #9567 selected Y files render while X is still loading with eager per-folder files
container-recursive-areaKilled — waits for a slow file listing instead of giving up on the first empty one
container-root-areaKilled — #9567 a folder finishing cannot clear the initial area file spinner
container-area-ownerKilled — #9567 on-demand completion leaves initial area file loading owned by the area
container-favourite-deliveryKilled — survives the folders-before-files window of a direct-children provider
paging-folder-signalKilled — #9567 reset aborts an in-flight folders page and rejects its late result
paging-file-signalKilled — #9567 reset aborts an in-flight files page and rejects its late result
paging-shared-abortKilled — #9567 reset aborts an in-flight folders page and rejects its late result
paging-shared-settleKilled — #9567 manual sync retires the pagination flight before the old page resolves
area-file-resetKilled — #9567 manual sync removes prior folder files before its replacement area page arrives
area-root-skipKilled — #9567 a folder finishing cannot clear the initial area file spinner
loading-open-clearKilled — #9567 cached-open clears the rendered container loading set
loading-reset-clearKilled — #9567 reset clears the rendered container loading set

Pre-handoff skipped coverage: SKIPPED retained-free viewer: apps/viewer/dist absent; build the default viewer first.

Unverified: live provider backends were not exercised. Mounted UI tests use deferred provider transport and real loading, selection, folder gating and cache persistence. Per-folder catalogs are not persisted; only complete flat-subtree/recursive catalogs are cacheable.

🤖 Generated with Claude Code