fix(collab): settle whenSynced on teardown, refuse writes after dispose, serialise history captures
Defect
Three lifecycle holes in @ifc-lite/collab (implicit-state-machines audit, collab lane):
- websocket provider (
providers/websocket.ts)- Reported
'connecting'underconnect: false, though no socket is opened. The webrtc provider already reports this honestly. whenSyncednever settled if the provider was destroyed before its first sync.destroy()kept consumer status listeners.connect()afterdestroy()reopened a socket.
- Reported
CollabSession.dispose()(session.ts)- Had no disposed flag and never cleared
statusListeners. whenSyncedhung forever for a session disposed mid-join. A caller parked onawait session.whenSynced(the viewer's join path) never resumed.transact/seedkept writing into a torn-down doc.
- Had no disposed flag and never cleared
attachHistorySidecar(branch/history.ts)- Had no in-flight guard. Two overlapping captures both diffed against the same
lastBaseline. Whicheverrecord()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().
- Had no in-flight guard. Two overlapping captures both diffed against the same
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'underconnect: false. whenSyncedrejects ("destroyed before it synced") on destroy.destroy()is idempotent: it announces'offline'once, then clears the listeners.connect()/disconnect()are no-ops after destroy.
- Initial status is
- session
dispose()is idempotent.- It rejects a pending
whenSyncedwith the new exportedCollabSessionDisposedErrorand clears status listeners. seed/transactthrowCollabSessionDisposedErroronce disposed.- New
disposedgetter andonDispose(listener)onCollabSession. - 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.tskeeps 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
HistoryDriverDetachedErrorand 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 twotscworkers exceed 8G.node scripts/check-api-surface.mjs: clean after--update. It addsCollabSessionDisposedErrorandHistoryDriverDetachedError.node scripts/check-module-size.mjsandnode 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 removed | Result |
|---|---|
ws initial 'offline' under connect:false | killed (connect:false status test) |
ws reject whenSynced on destroy | killed |
ws connect() no-op after destroy | killed |
session statusListeners.clear() | killed |
session reject whenSynced on dispose | killed |
session assertLive in transact / seed | killed (both) |
session onDispose immediate call when already disposed | killed |
| history detached check at capture turn | killed (2 tests) |
| history serialisation | killed |
| history timer skip while in flight | killed |
history session.onDispose(detach) | killed |
ws listeners.clear() / no-register after destroy | survives: 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 dispose | survives: same retention-only reason |
session if (disposed) return in dispose | survives: 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
whenSyncedon destroy is not exercised directly. The session-level dispose arm covers any provider. - The viewer's join path picks up the
whenSyncedrejection 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.