fix(viewer): refuse re-entrant async submits in report deletion, BCF create and review decision

Merged
LO
22d ago
+2001-467

Defect

Some async viewer handlers can run again while their own work is still in flight. A double-click or Enter+click runs them twice. When an older run settles late, it can clear a newer run's busy flag, write over newer state, or act on a form or panel that is already closed.

  • C-9 ReportDeletionPreview: a double-click on Confirm ran two removal passes. A rejected removal left Keep stuck disabled.
  • C-11 ReviewDecision: a double Save wrote twice. A late Clear wiped text typed after the press, and a rejected write was unhandled.
  • C-2, every BCF "capture a viewpoint, then create" flow and every other async BCF action. The full inventory is below.

Fix

  • hooks/useSingleFlight.ts returns { busy, run(task), release() }.
    • A synchronous ref lease refuses re-entry before React re-renders.
    • Only the run that owns the lease clears busy.
    • release() and unmount make the in-flight run stale; its isCurrent() turns false.
    • run resolves { status: 'refused' | 'stale' | 'done' }.
    • The planned useAsyncRun can absorb it and add an AbortSignal, as the hook's doc says.
  • hooks/bcf/capture-viewpoint-for-create.ts is the single "capture the viewpoint for a new topic" step, shared by BCFPanel, useBcfFromChange and ClashPanel.
    • A capture that throws or resolves null counts as a failure.
    • It is always logged; the captureViewpointFailed toast shows only while the create is still current.
    • Nothing is created.

BCF-area sweep: every async user-triggered handler

HandlerStatus
BCFPanel create form: open-capture, Recapture, CreateConverted: capture and create flights; close releases both; create is checked against isCurrent; capture-for-create helper. The snapshot is offered only while a viewport renderer exists, so with no model loaded Create adds the topic without a viewpoint
BCFPanel Capture 3D (handleCaptureViewpoint)Converted: shared viewpoint flight; both capture buttons disabled while it runs; failures logged and toasted
BCFPanel Capture 2D (useSectionViewpointCapture.capture)Converted: same viewpoint flight as 3D
BCFPanel Export (now bcf/useBcfArchiveActions)Converted: export flight; the ribbon item is disabled while it runs
BCFPanel Import (file input, now bcf/useBcfArchiveActions)Converted: the latest pick wins (release, then run); an earlier, slower read is discarded and does not clear the loading state
BCFPanel Edit topic, delete topic or viewpoint, activate viewpoint, zoom, add comment, statusOut of scope: synchronous store writes
compare/useBcfFromChange capture and submitConverted: capture and submit flights; Cancel or focusing another change releases both; helper
ClashPanel "BCF topic" and the manual-group BCF actionConverted: one shared flight; a panel that has closed creates nothing and raises no toast. The clash action uses the helper. The group action, after its "models changed" check, treats a null viewpoint as a capture failure
layers/LayerReviewSection open review, Comment (capture, then post), exportConverted: one flight; nothing is posted after unmount. Comment's capture goes through the helper, so a null capture posts nothing and shows a proper toast instead of a raw destructuring error
BCFServerDialog Load topics (handlePull)Converted: pull flight; disconnect or an account switch releases it, and the existing generation guard still drops late results
BCFServerDialog loadProjectsAlready guarded: session-generation counter drops stale lists
BCFServerConnectForm ConnectConverted: connect flight; the popup still opens synchronously inside the click
BCFPublicationPanel Check project and Publish (act)Converted: one flight
BCFPublicationPanel project-list effectAlready guarded: live flag
BCFOutboxEntry overwrite, discard, check, send again, "this is ours"Converted: one flight per entry
BCFDraftReconcile apply and draft-unplacedConverted: one flight; buttons disabled
BCFDraftsDialog ExportConverted: export flight; disabled while it runs
BCFDraftsDialog async edits (merge, split, remove member)Converted: an edit flight. A refused edit's failure is still logged, and switching batch releases the flight so the new batch isn't held. Each edit is computed from the batch as it was when pressed, so a disabled <fieldset> holds every editor control until the edit lands, and a second edit can't overwrite it from the same stale batch. Synchronous edits still apply immediately
BCFDraftsDialog outbox-attention connect effectConverted: live flag, so a superseded connection doesn't land
BCFDraftsDialog Import archive (file input)Out of scope: one change event per pick, and adoption is additive (it never replaces a batch), so nothing is clobbered
useBcfDraftActions draft from groups or selection (finish)Converted: draft flight; drafting is its busy
hooks/useBCF, hooks/bcf/*No user-triggered handlers: these are the capture implementation and camera helpers

Tests

Every test that guards against a second press makes both presses inside one act(), so it proves the ref lease rather than a re-rendered disabled control. Capture flows run the real viewport capture with only GPU completion held, through the shared test/bcf-held-capture.ts. Server flows use the existing loopback BCF peer or fake fetch. Every assertion compares plain values.

New or extended suites:

  • hooks/useSingleFlight (4), ReportDeletion.single-flight (2), ReviewDecision.single-flight (3).
  • BCFPanel.create-single-flight (7), including no model loaded: Create adds the topic without a viewpoint.
  • BCFPanel.actions-single-flight (3): Capture 3D, Export, Import with the latest pick winning.
  • BCFPanel.section-viewpoint: +1, for a double Capture 2D.
  • compare/useBcfFromChange.single-flight (7).
  • ClashPanel.bcf-single-flight (11), including a group capture that resolves null.
  • layers/LayerReviewSection.single-flight (3), including a capture that resolves null.
  • ClashPanel.manual-groups: now mounts a viewport renderer stub, because a group topic needs its viewpoint.
  • bcf/BCFServerDialog: +2, for Connect and Load topics.
  • bcf/BCFDraftsDialog.single-flight (3): Publish, Export, async edits plus the held editor.
  • bcf/useBcfDraftActions.single-flight (1).

Mutation check: I deleted or bypassed each guard and re-ran its tests (under an 8G memory cap). 40 of 45 mutants were killed. They include:

  • the hook's re-entry guard, owner-checked finally and unmount release;
  • every isCurrent check after a capture;
  • the stale-capture, close-release and toast-gate guards in BCFPanel, compare and Clash;
  • the helper's toast gate, its toast, and its null-as-failure branch;
  • the BCFPanel Capture 3D/2D, Export and both Import guards;
  • the server Connect and Load-topics flights;
  • the drafts Export, async-edit and held-editor guards, and the draft-action flight;
  • the C-9 and C-11 guards;
  • this round's three: the snapshot offered without a viewport, a group null viewpoint silently dropped, and a layer-review null capture destructured.

Survivors (5), kept on purpose:

  • Publication act flight: double Publish already sends exactly 5 writes without it, because the outbox deduplicates below the UI. The flight is a UI backstop that no test can distinguish.
  • Outbox entry, reconcile and drafts connect-effect guards: converted, but with no test. Showing an effect needs a persisted publication record or a clash draft run; left as follow-ups.
  • Group-flow catch toast gate: closing the panel invalidates the group's scene check, which returns null before anything can throw.

Red on the original components (earlier rounds): C-9 made 2 removal calls; C-2 left Submit enabled and showed the stale snapshot; C-11 made 2 writes. The compare and clash failure-toast tests and the null-capture tests also fail against their parents (the same as the "no toast" and "null is success" mutants).

Green: the new tests plus the neighbouring suites (BCFPanel*, bcf/, compare/, ComparePanel, ClashPanel*, Clash*Group, layers/, review/, ReportDeletion*, bcf-archive-activity, lib/bcf*, hooks/bcf, useBCF) pass 555/555, run under an 8G memory cap.

Local guards: all 19 Node guards pass, including check-module-size (+0 added, ^0 raised, v1 lowered, -1 deleted) and check-source-text-assertions. Lint steps pass: changesets, test glob coverage, main-entry guards, i18n literals, jsx-a11y, type scale. Unused locals: tsc --noUnusedLocals in apps/viewer-embed gives 182, the baseline. The archive split had left BCFPanel's React import unused (+1); it is removed in c93524b5c5. scripts/check-unused-locals-baseline.test.mjs passes. Locally, check-unused-locals cannot measure packages/cli or packages/embed-sdk, because they are unbuilt in this worktree and untouched by this PR; CI Lint is the authority there.

Typecheck: the local viewer typecheck (which includes tests) is clean. pnpm typecheck on the fleet at 3b14a3c816 passed, 134/134 tasks.

Test-harness finding (round 1): one test process grew to about 117 GB on the original code. A failing assert.equal(domNode, null) made node's assert deep-inspect the happy-dom node graph. The assertion now compares booleans.

Behaviour changes worth noting

  • When a viewpoint was requested and the capture fails (throws or resolves null), the create now creates nothing and toasts while it is still current. This applies to BCFPanel, compare, Clash and Clash groups, and layer review. These used to create or post without the viewpoint, fail silently, or show a raw error.
  • With no model loaded, nothing changes: the BCF panel offers no snapshot (there is no viewport), and Create adds the topic without a viewpoint, as before.
  • While an async draft edit lands, the draft editors are disabled for that moment.
  • A @ifc-lite/viewer patch changeset describes these changes.

Module-size split (behaviour-identical)

  • BCFPanel's archive import/export moved verbatim into bcf/useBcfArchiveActions.ts; the only change is the ChangeEvent type import.
  • BCFTopicDetail's "capture will include" list moved into BCFCaptureWillInclude, next to the capture buttons.
  • Budgets: BCFPanel lowered from 529 to 474; the BCFTopicDetail row was deleted (now 389 lines). ClashPanel is within its existing budget.

Not verified

  • The full viewer suite was not run (the neighbouring suites above were).
  • None of this was tried in a real browser.
  • BCFPanel.tsx (about 535 lines) and ClashPanel.tsx (about 1225 lines) were already over the ~400-line rule and are not split here.

šŸ¤– Generated with Claude Code