CRDT & Collaboration Architecture
Sourdaw's project truth is an Automerge CRDT document. This single decision shapes four subsystems at once: how state is written, how undo works, how projects persist, and how multiple people edit the same project. This document describes that write path end to end and the collaboration transports built around it.
It complements:
DAW System Architecture— truth/projection state model (§6)TypeScript Module Architecture— where CrdtDocument sits among modules- The
crdt-collaborationagent skill — the operational rules for working in this system
1. The write path
Every mutation of project truth flows through one funnel:
UI / command palette / AI / MIDI learn
│
▼
executeAppAction(action) src/modules/Command/useCases/executeAppAction.ts
│
├─ handler lookup (registered in bootstrap, one map per module)
├─ noop check + describe() (pre-captures undo info)
├─ setSemanticContext(...) from CrdtDocument/stores
│
▼
Automerge storage transaction the document is the write target
│
▼
projections refresh stores CrdtDocument/useCases/projection/
│
▼
subscribers re-render useStore(Store<T>)
Two consequences fall out of this shape:
- Undo is an inverse-action stack beside the document, not Automerge history. An undoable handler's
describe()runs beforeexecute()and returns an inverseAppAction. After the transaction commits,executeAppActionpushes that pair as an undo entry ontoundoStore(src/modules/Command/stores/undoStore.ts);undoreplays the inverse throughexecuteAppActionwithskipUndo, so an undo is itself an ordinary document write. Code outside the funnel can register a closure entry (pushUndoEntry, acallbackentry inmodels/UndoEntry.ts) for state the document does not hold, such as audio buffers. Invariants:- The stack is per session and per tab: it holds only this editor's actions, never a peer's, and is mirrored to
sessionStoragesolely so a reload can restore it. The mirror is discarded unless the project id and the document witness (captureDurableDocumentWitness) still match, because an inverse is only sound against the document it was captured from. - An inverse is checked against the current document only by its own handler. A single entry refuses when the inverse's
executereturnsconflict, assetClipGaindoes when the clip's gain no longer equals theexpectedGainthe inverse captured; an atomic group, whose members all carry an inverse action, replays as one batch and also refuses when any member'svalidaterejects. A refusal writes nothing and keeps the entry. A group that mixes callback or inert entries undoes member by member, newest first, so a conflict part-way through keeps the newer members undone and leaves the conflicting member and every older one at the head, retryable. Undo adds one admission of its own: a recordedtrimClipStartinverse withoutmidiOffsetBeats, captured before MIDI trim offsets were stored, replays only when its clip still resolves to one eligible target and, in an atomic group, every inverse before it in the batch is a known metadata action (getLegacyTrimInverseReplayFailure); otherwise it refuses as a conflict. A mixed group replays each member alone, so it checks a legacy trim against its own clip only. An inverse with neither check writes unconditionally, so its undo overwrites a newer remote edit of the same target. - Undo steps past a refused unit onto the first unit beneath that is not fully inert, dropping the fully inert units in between, and undoes that unit only when every one of its members resolves to a handler declaring
canReportConflict, the flag a handler sets once itsexecutehas a genuine conflict path; the refused unit stays at the head, retryable. The inert units passed are dropped even when the unit beneath is refused, as the head's own inert purge drops them. - An entry whose inverse is
nullis inert: undo drops it without writing, and redo never re-applies it. A document write outsideexecuteAppActiongets no entry unless its writer pushes a callback entry itself. - The document's own history is a separate record and not the undo stack.
executeAppActionattaches a semantic context, so each Automerge change carries a readable message, andactionHistoryStoreholds labelled entries with metadata only, no inverse.revertActionreplays through an in-memory, session-only replay capability (src/modules/Command/stores/actionReplayCapabilities.ts, bounded to the newest 200 entries) and returnsunavailablewhen none exists, for example after a reload.
- The stack is per session and per tab: it holds only this editor's actions, never a peer's, and is mirrored to
- Collaboration is a byproduct, not a feature. Concurrent editors merge through the same document, so multi-user editing reuses the single-user write path exactly. The price of admission is rule 1: everything goes through the funnel.
2. Ownership: CrdtDocument vs. Collaboration
These two modules are easy to conflate. The split:
| Concern | Owner | Examples |
|---|---|---|
| Document lifecycle | CrdtDocument | create/load/save, compaction, branches (crdtBranching/), merge (crdtMerge/), semantic action history, persistence queue |
| Projections | CrdtDocument | projection/projectProjection.ts, setupProjectionBridge.ts — document → stores |
| File format | CrdtDocument | .sdaw encode/decode (useCases/sdawFileFormat/), native persistence (repositories/nativeCrdtPersistence/) |
| Transport & sessions | Collaboration | WebRTC peers, invites/QR, presence, cursors, asset transfer |
| LAN discovery | Collaboration + daw-collab | mDNS advertise/browse, collab_* commands |
A useful test: if the code would still make sense with no network in the world, it belongs to CrdtDocument; if it would still make sense with no document, it belongs to Collaboration.
3. Persistence
In the browser, CRDT-backed stores persist through the Automerge storage adapter (src/infra/store/storage/createAutomergeStorage.ts) — the same pluggable StorageAdapter slot used by memory and localStorage adapters, so a store's persistence backend is a wiring decision, not a code change.
Natively, the daw-collab crate owns the .sdaw bundle format (crates/daw-collab/src/persistence.rs):
4B magic "SDAW"
2B format version (u16)
.. per-document Automerge saves
TypeScript encode/decode lives in CrdtDocument/useCases/sdawFileFormat/ (encodeSdawFile.ts, decodeSdawFile.ts). Filesystem access is repository-layer only (repositories/nativeCrdtPersistence/) — components never touch project files.
4. The three transports
Collaboration ships three independent connectivity paths. They share the document model and nothing else:
- Serverless WebRTC. Manual offer/answer exchange, with QR-code invites (
qrcodedependency) for phone-to-desktop pairing.Collaboration/repositories/peerConnection.tswraps rawRTCPeerConnection;generateInvite.ts/acceptAnswer.tsdrive the handshake. No server involved — signaling is copy-paste or camera. - WebSocket relay. A standalone package at repo-root
server/(sourdaw-collab-server): a smallwsrelay on port 8787 handling session/peer registry, host migration, and the message typesjoin/leave/action/cursor/sync-request/sync-response/state-update. It is deliberately not in the pnpm workspace (ownpackage-lock.jsonand tsconfig);scripts/health-gates-server.shbuilds it during explicit full validation. Startup requires a 32–128 character base64urlCOLLAB_AUTH_TOKEN; clients sendsourdawand that token as WebSocket subprotocols. That token admits a client to the relay; it does not admit it to a room. Room membership is a separate per-session capability:joincarries thesessionSecretthe invite minted, the relay stores the creator's and compares every later joiner's in constant time. Naming a session id therefore admits nobody by itself. Every client-supplied identifier is capped in bytes before it can key a map. The relay binds127.0.0.1unlessCOLLAB_HOSTis explicitly set, and refuses to start on a non-loopback host in cleartext: serve TLS by setting bothCOLLAB_TLS_CERTandCOLLAB_TLS_KEY, or accept an unencrypted bind explicitly withCOLLAB_ALLOW_CLEARTEXT=1. Connection, session, peer, payload, rate, and outbound-buffer limits are configurable. - Native LAN. The
daw-collabcrate advertises and browses mDNS (_sourdaw._tcp.local.) and exposes session management through thecollab_*native commands (create/save/load/merge/apply + advertising/browsing lifecycle).
5. Invariants
- Serializable truth only. No runtime handles (AudioContext, nodes, worklets, plugin instances, editor windows) in the document or CRDT-backed stores. Automerge must serialize and merge every value.
- Projections are disposable. Stores fed from the document are derived views. Rebuild them; never patch them by hand; never treat them as truth.
- Semantics travel with actions. Automerge guarantees convergence, not meaning. The semantic context attached at dispatch time is what makes document history reviewable; the inverse captured at dispatch time is what makes undo sane.
- One owner per write. Cross-feature intent goes through
executeAppAction; subscribers react, they do not become writers.
References
.agents/skills/crdt-collaboration/SKILL.md— operational rules for this systemsrc/modules/Collaboration/AGENTS.md— transport-layer detaildocs/03-state-management.md— store mechanics