fix(collab): settle whenSynced on teardown, refuse writes after dispose, serialise history captures

Merged
LO
21d ago
+560-100

Defect

Three lifecycle holes in @ifc-lite/collab (implicit-state-machines audit, collab lane):

  1. websocket provider (providers/websocket.ts)
    • Reported 'connecting' under connect: false, though no socket is opened. The webrtc provider already reports this honestly.
    • whenSynced never settled if the provider was destroyed before its first sync.
    • destroy() kept consumer status listeners. connect() after destroy() reopened a socket.
  2. CollabSession.dispose() (session.ts)
    • Had no disposed flag and never cleared statusListeners.
    • whenSynced hung forever for a session disposed mid-join. A caller parked on await session.whenSynced (the viewer's join path) never resumed.
    • transact / seed kept writing into a torn-down doc.
  3. attachHistorySidecar (branch/history.ts)
    • Had no in-flight guard. Two overlapping captures both diffed against the same lastBaseline. Whichever record() resolved last won the baseline, even when it held the older state, so the next diff silently re-included or skipped changes.
    • The timer was not tied to session dispose.
    • A queued capture still recorded after detach().

Concrete scenario

A recipient opens a share link and clicks Leave before the relay syncs. The session is disposed, but await session.whenSynced in the join continuation never settles. A slow IDB history sidecar plus a manual "capture" landing during a timer capture records diffs against the wrong baseline.

Fix

  • websocket provider
    • Initial status is 'offline' under connect: false.
    • whenSynced rejects ("destroyed before it synced") on destroy.
    • destroy() is idempotent: it announces 'offline' once, then clears the listeners.
    • connect() / disconnect() are no-ops after destroy.
  • session
    • dispose() is idempotent.
    • It rejects a pending whenSynced with the new exported CollabSessionDisposedError and clears status listeners.
    • seed / transact throw CollabSessionDisposedError once disposed.
    • New disposed getter and onDispose(listener) on CollabSession.
    • Rejections that nobody awaits are marked handled (internal/promise.ts), so they don't raise unhandled-rejection noise. Awaiters still receive them.
  • history driver (moved into branch/history-driver.ts; history.ts keeps the sidecars)
    • Captures are serialised. An idle driver still snapshots synchronously at call time.
    • Timer ticks skip while a capture is in flight.
    • The driver detaches on session.onDispose.
    • A capture whose turn comes after detach rejects with HistoryDriverDetachedError and records nothing.

No persistence format change. Changeset: @ifc-lite/collab minor (new exports plus new CollabSession members). The API-surface snapshot is updated.

Commands

All test commands ran under systemd-run --user --scope -p MemoryMax=8G -p MemorySwapMax=0.

  • pnpm test:collab: collab 55 files, 316 passed / 2 skipped; collab-server 48 files, 301 passed.
  • pnpm turbo run typecheck build --filter=@ifc-lite/collab: 9/9 tasks succeeded.
  • pnpm lint: no errors. The 3 warnings are in untouched files. unused-locals: no new unused locals. Lint ran with a 24G cap because its two tsc workers exceed 8G.
  • node scripts/check-api-surface.mjs: clean after --update. It adds CollabSessionDisposedError and HistoryDriverDetachedError.
  • node scripts/check-module-size.mjs and node scripts/check-source-text-assertions.mjs: OK.

On the original sources, the new websocket tests fail for the stated reasons: expected 'connecting' to be 'offline', expected 'pending' to be 'rejected', and expected 1 to be +0 sockets.

Mutation table

Guard removedResult
ws initial 'offline' under connect:falsekilled (connect:false status test)
ws reject whenSynced on destroykilled
ws connect() no-op after destroykilled
session statusListeners.clear()killed
session reject whenSynced on disposekilled
session assertLive in transact / seedkilled (both)
session onDispose immediate call when already disposedkilled
history detached check at capture turnkilled (2 tests)
history serialisationkilled
history timer skip while in flightkilled
history session.onDispose(detach)killed
ws listeners.clear() / no-register after destroysurvives: retention only. y-websocket's destroy() already drops its observers, so no event can reach a retained listener. The guard releases consumer closures.
session no-register onStatus after disposesurvives: same retention-only reason
session if (disposed) return in disposesurvives: every teardown it guards is idempotent today. The flag keeps double dispose from depending on that.

Two guards I first added were redundant, so I deleted them rather than keeping them as survivors: provider.off(...) before destroy, and !disposed && in undo/redo (a destroyed UndoManager already returns false).

Not verified

  • The browser IndexedDB provider's own whenSynced on destroy is not exercised directly. The session-level dispose arm covers any provider.
  • The viewer's join path picks up the whenSynced rejection in #9554 (viewer attempt token).

šŸ¤– Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added configurable history snapshots that can be recorded on a schedule or on demand, with optional diffs between captures.
    • Added session disposal notifications and clear errors for operations attempted after disposal.
  • Bug Fixes
    • Providers configured not to connect now report an offline status immediately.
    • Pending sync operations now reject when a provider or session is disposed, and provider status updates reflect teardown.