fix(viewer): invalidate dismissed BCF server operations

Merged
LO
21d ago
+562-42

A topic pull could finish after its dialog was dismissed, loading cancelled topics and showing a toast. When local topics exist, this pull only starts after the user explicitly confirms replacement. Dismissing sign-in could also save a new server connection without adopting its author, leaving the reopened dialog connected under one account while new topics used the old identity.

The product decision is that clicking “Load topics” and then closing cancels the operation: it loads nothing and shows no message. The run signal reaches pull HTTP requests and fences the saved project preference. Sign-in sends the same signal through password, pasted-token, client-credentials and OAuth flows, including identity resolution and token relays; cancellation is checked immediately before saving the connection. A completed sign-in still adopts an email-shaped server author.

Use useAsyncRun's signal and ownership predicates directly. Form teardown aborts OAuth callback waiting and closes its popup; no separate controller or pending-run ref remains. One dialog effect cleanup invalidates project listing and resets the pull. The generation counter only fences project-list requests; pull callbacks use one ownership fence each. Preserve the existing status paragraph.

Real dialog/form tests hold token exchange, identity response decoding, topic response decoding and OAuth callback delivery across dismissal. They assert persisted connection/project choice, author identity, transport cancellation, local project state and the absence of cancelled-pull feedback. Service regressions also cover dynamic registration, token relays, retained request signals and refusal to refresh after a cancelled response returns 401.

Rebased onto fresh main after #9590 merged, with one BCF-only commit above main; none of #9590’s commits are carried in this PR. The final migration’s validation-inside-try behavior is preserved.

Validation at 650c2776850f00174ab7a13c8f56eec675f2b069 (Pop, Node v24.22.0, direct local execution under an 8 GiB scope; no Rust/WASM rebuild):

B1 checks cancellation immediately before connection persistence. N1/N3 abort pull HTTP work and prevent saving a cancelled project choice. N2 keeps one close cleanup and reserves the generation counter for project listing. N4 preserves the existing status paragraph.

The first focused load exposed borrowed full-engine glue missing main’s new ENGINE_BINARY_URL marker. The unmodified append_engine_binary_marker function from scripts/build-wasm.sh was run against a staging copy of the borrowed glue; its generated JS was copied back. The binary and committed types were unchanged. Final focused and family tests ran after this artifact refresh.

Every command below ran inside systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 with PATH=/home/louistrue/build-workers/toolchains/node-v24.22.0-linux-x64/bin:/usr/local/bin:/usr/bin:/bin; runtime tests retained individual 8 GiB scopes.

pnpm install --frozen-lockfile --ignore-scripts
pnpm exec turbo build --filter=@ifc-lite/rhino3dm --filter=@ifc-lite/wasm-lifecycle --filter=@ifc-lite/mutations --filter=@ifc-lite/geometry --filter=@ifc-lite/collab --filter=@ifc-lite/clash --only --concurrency=1 --force
pnpm exec turbo typecheck --filter=@ifc-lite/viewer --filter=@ifc-lite/viewer-embed --only --concurrency=1 --force
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 env TEST_PATTERN='BCFServerOwnership|bcf-server-cancellation|BCFServerDialog.pull-ownership|useAsyncRun.test' pnpm test --filter=@ifc-lite/viewer --only
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 env TEST_PATTERN='[Bb][Cc][Ff].*test|useAsyncRun.test|ReportDeletion.*test|useBcfFromChange.*test|ReviewDecision.single-flight|LayerReviewSection.single-flight|ClashPanel.manual-groups' pnpm test --filter=@ifc-lite/viewer --only
NODE_OPTIONS=--import=file:///home/louistrue/.t3/scratch/e5c7-ownership/serialize-unused-locals.mjs pnpm lint
node scripts/check-module-size.mjs
node scripts/check-source-text-assertions.mjs
node scripts/check-ui-drift.mjs
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 node --test scripts/check-unused-locals-baseline.test.mjs
systemd-run --user --scope -q -p MemoryMax=8G -p MemorySwapMax=0 node scripts/pre-handoff.mjs

All commands passed. The family run reported 533 tests and 0 skips. Both typecheck configurations include test sources; Turbo cache reuse was forced off. Root lint includes the full unused-locals census.

Lint used an execution-only Node preload that serializes the unchanged census worker processes through a private lock, measuring every package with the same compiler arguments and baseline under the combined 8 GiB limit.

Census scheduling helper
import { spawnSync } from 'node:child_process';

// Keep the repository's complete census under one 8 GiB scope without changing its source.
if (process.argv[1]?.endsWith('/scripts/lib/unused-locals-worker.mjs')
    && process.env.OWNERSHIP_CENSUS_SERIALIZED !== '1') {
  const result = spawnSync('flock', [
    '/home/louistrue/.t3/scratch/e5c7-ownership/unused-locals-direct.lock',
    process.execPath, ...process.argv.slice(1),
  ], {
    encoding: 'utf8', maxBuffer: 32 * 1024 * 1024,
    env: { ...process.env, OWNERSHIP_CENSUS_SERIALIZED: '1' },
  });
  if (result.stdout) process.stdout.write(result.stdout);
  if (result.stderr) process.stderr.write(result.stderr);
  if (result.error || result.signal) {
    console.error(result.error ?? `Serialized census worker exited with ${result.signal}`);
    process.exit(1);
  }
  process.exit(result.status ?? 1);
}

Each mutation below was run separately, restoring the original file before the next run; every test command used the same 8 GiB scope.

Guard mutationResultNamed failure or survivor explanation
fetch-before-dispatchkilledPR #9583: an already-cancelled token sign-in dispatches no identity request
fetch-after-responsekilledPR #9583 N3: an ignored abort followed by a late 401 cannot start a token refresh
fetch-bind-run-signalkilledPR #9583 B1: cancelled password identity decoding cannot persist a connection
fetch-preserve-request-signalkilledPR #9583: connected clients retain request cancellation
fetch-preserve-run-signalkilledPR #9583: connected clients retain run cancellation
signin-before-persistkilledPR #9583 B1: password sign-in identity-body keeps persistence and the real dialog author together
pull-before-persistkilledclosing (external) during a pull leaves an empty workspace empty and preserves the saved project choice (PR #9583)
identity-transport-signalkilledPR #9583 B1: cancelled password identity decoding cannot persist a connection
password-persist-signalkilledunmounting during sign-in suppresses the late onSignedIn callback
token-persist-signalkilledPR #9583 B1: token sign-in identity-body keeps persistence and the real dialog author together
client-persist-signalkilledPR #9583 B1: client credentials sign-in identity-body keeps persistence and the real dialog author together
password-discovery-signalkilledPR #9583 B1: cancelled password identity decoding cannot persist a connection
client-discovery-signalkilledPR #9583 B1: cancelled client credentials identity decoding cannot persist a connection
password-token-signalkilledPR #9583 B1: password sign-in token keeps persistence and the real dialog author together
client-token-signalkilledPR #9583 B1: client credentials sign-in token keeps persistence and the real dialog author together
token-discovery-signalkilledPR #9583: an already-cancelled token sign-in dispatches no identity request
oauth-discovery-signalkilledunmounting during OAuth discovery never navigates the abandoned popup
oauth-registration-signalkilledPR #9583: cancelling dynamic OAuth registration aborts its HTTP request
oauth-direct-token-signalkilledPR #9583 B1: OAuth direct token exchange receives cancellation
oauth-relay-token-signalkilledPR #9583 B1: OAuth relay token exchange receives cancellation
oauth-persist-signalkilledPR #9583 B1: oauth sign-in identity-body keeps persistence and the real dialog author together
relay-forward-signalkilledPR #9583 B1: OAuth relay token exchange receives cancellation
pull-transport-signalkilledclosing (external) during a pull leaves an empty workspace empty and preserves the saved project choice (PR #9583)
form-password-signalkilledunmounting during sign-in suppresses the late onSignedIn callback
form-token-signalkilledPR #9583 B1: token sign-in identity-body keeps persistence and the real dialog author together
form-client-signalkilledPR #9583 B1: client credentials sign-in token keeps persistence and the real dialog author together
form-oauth-signalkilledPR #9583 B1: oauth sign-in token keeps persistence and the real dialog author together
form-discovery-signalkilledunmounting during OAuth discovery never navigates the abandoned popup
form-popup-abortkilledunmounting during OAuth discovery never navigates the abandoned popup
form-callback-signalkilledunmounting an OAuth form closes its popup and cancels the callback listener
form-discovery-ownersurvivedThe signal-bound discovery transport independently rejects after cancellation; callback waiting also receives the aborted signal.
form-callback-ownersurvivedThe token transport refuses an already-aborted run before any exchange is dispatched.
form-success-ownersurvivedThe service rejects cancelled identity/token operations before saving or returning a successful connection.
form-error-ownersurvivedAn unmounted form has no rendered error UI; the cancellation and saved-state assertions remain protected by the service signal.
dialog-close-resetkilledclosing (external) during a pull leaves an empty workspace empty and preserves the saved project choice (PR #9583)
dialog-strict-reseedkilledclosing (strict-root) during a pull leaves an empty workspace empty and preserves the saved project choice (PR #9583)
dialog-list-invalidationsurvivedReopening or switching accounts starts a fresh loadProjects generation independently; the closed dialog does not render a late project list.
dialog-pull-signalkilledclosing (external) during a pull leaves an empty workspace empty and preserves the saved project choice (PR #9583)
pull-progress-ownersurvivedThe bound transport and service persistence fence reject cancelled pulls; the closed dialog has no visible progress UI.
pull-success-ownersurvivedCancelled pull services reject before returning a result, independently of the UI ownership fence.
pull-error-ownersurvivedCancelled operations have no open dialog error UI; reopen seeding clears transient errors.
pull-finally-ownersurvivedAbort-bound transport rejects abandoned pulls; these fixtures settle the active replacement before checking its completed UI.
account-change-resetkilledan account switch mid-pull frees Load topics for the new account, and the old pull never lands

The literal git grep -n useSingleFlight apps/viewer/src reports only three historical documentation comments on main, with no executable users or imports.

Coverage limits: The retained-free viewer check skipped because apps/viewer/dist is absent. No manual browser/GPU session was run.

Fresh-main audit at a1bd6a9c111b4db27635ed13072d4f904460c7e0: all seven touched production files remain below 400 lines, with no allowlist rows. The branch contains one BCF-only commit above main. Mutation checks: 43 total, 34 killed, nine explained survivors. No live BCF server or OAuth-provider session was exercised.

🤖 Generated with Claude Code