Architecture decision record · ADR-0016

Collaboration: the op log server and the client session (milestone M11, part 1)

The server (createDocServer in @ember-lattice/server) runs the submits of one doc one at a time.

Status
Proposed
Deciders
Joe Gaudet
Created
Updated
PRD
PRD
Related
None

Context

Decisions 22 and 23 ask for a server-authoritative op log and full collaboration in 1.0: presence, cursors, selections and conflict handling. Plan section 8.4 gives the model. Spike S7 says that every op names IDs, so only two conflicts remain: two writes to one cell, and an edit to a deleted row. ADR 0015 gives the ops (SheetOp), the FormulaEngine seam and the Workbook model.

Decision drivers

Not recorded in the original ADR.

Options considered

Not recorded in the original ADR.

Decision

1. The server holds the truth, and checks each op against the current state

The server (createDocServer in @ember-lattice/server) runs the submits of one doc one at a time. It does not transform an op against each op after its baseSeq. It checks each op against the current doc:

  • An op that names a deleted or unknown row, column or sheet drops. Two writes to one cell need no rule: the server applies them in order, so the last one wins.
  • The server keeps tombstones: the full row and column order, with the deleted IDs in their old places. Thus, an insert before a deleted row goes before the next live row. A range end of a late formula, merge, format or style on a deleted row moves inward, as Excel does.
  • A submit with no op left gets reject { reason: 'conflict' }, and no seq.

2. The server sends the ops as it applied them, to every client

ops { seq, clientId, opId, ops, changes } goes to every client, the sender too. The sender needs the changes of its own formulas, and it must see an op that the server changed or dropped. ack goes only to a resent submit that already has a seq. Submits are idempotent on (clientId, opId).

3. The client keeps a shadow of the server state

CollabSession keeps a second Workbook with an engine that computes nothing. The shadow takes only the server ops and changes, in seq order. The shadow uses the Workbook rules, so the client has no second reducer.

  • A local op changes the view at once and goes to the server.
  • A server op that touches no cell of a waiting op goes to the view directly.
  • A server op that conflicts with a waiting op, or any waiting op that changes the structure, makes the view load the shadow again and apply the waiting ops on top. The result is what the server makes when the waiting ops arrive.
  • When no op waits, the view takes the server values.

4. The wire format

Version 1. Client: join, submit, presence. Server: snapshot, ops, ack, reject, presence, leave. parseClientMessage is the trust boundary. It never throws, it rejects unknown keys, and it has limits: 1,000 ops for each submit, 10,000 IDs for each insert or delete, 32,767 characters for each text, and IDs that match [\w.-]{1,64}. Error values must be one of the seven Excel errors, because the engine reads an error value as input text. attachSocket limits the message size (default 1 MB) before it parses JSON. The client package keeps a copy of the types; the test harness assigns each server message to the client type, so a difference fails the type check.

5. Store, snapshots and history

CollabStore is an interface with an in-memory implementation. A doc exists when it has a snapshot. The server saves a snapshot (with the tombstones and the computed values) each N submits. Each log entry keeps its changes, so a client that reconnects gets the missed ops with their values. history(docId) gives the log, and docAt(docId, seq) rebuilds the doc at any seq.

A resend after a server restart is applied once: the server reads the (clientId, opId) pairs from the full log when it opens a doc.

6. Presence and soft locks

Presence (cursor, selection, the cell in edit mode) goes over the same socket and is not stored. The server keeps the last presence of each open link and sends leave on close. A client drops a peer after 30 s without a message, and sends its own presence each 15 s. The color comes from the client ID. lockAt gives the user who edits a cell.

7. Undo

Undo uses the inverse ops that the client records when it applies a local op. A later server write to the same cell removes the inverse for that cell. Thus, undo never writes over another user. A row, column or sheet delete has no inverse in this part: the IDs are gone, and an undo must bring back tombstoned IDs.

Consequences

  • Only the server needs transform rules. The client follows the server.
  • A conflict costs the client a full model load and an engine load. Conflicts are rare, but a large sheet with many writers will feel it.
  • The wire carries SheetOp only. Notes, frozen panes, conditional formats and validations (the Workbook model ops) are remaining work.
  • Remaining work: the Sheet surface integration (cursors, selections and lock hints on the grid), the Rails and Python op log ports, a Postgres store, undo of deletes, batched recalc on the server, and row transactions for plain grids.

Confirmation

Evidence

  • Server: 33 tests (wire parser 20, core 12, WebSocket 1). Client: 8 session tests and the convergence test.
  • Spike S7 (fast-check): 10 clients, random setCell (values and formulas), row and column inserts and deletes, merges and unmerges, random delivery order on each link, and lost links with reconnects. After all messages arrive, the 10 clients and the server hold the same rows, columns, merges, values, formulas (by ID) and computed values. The test also loads the final doc into a new engine and compares the values. 100 runs in the suite (2 clients run IronCalc, 8 show the server values only). Manual stress runs passed: 6,000 runs with null-engine clients, then 3,000 runs with 2 IronCalc clients.
  • The test found two faults, now fixed:
    • The client did not take the server values of its own op while other local ops waited.
    • A batch that sets a plain value and then a formula on one cell kept the plain value as the computed value. The engine diff compares with its own last read, so it gave no change. The server now sends the engine value of those cells. Workbook.apply has the same fault with a local engine (sheet package; not fixed here).
  • With 2 of the 10 clients on IronCalc, the test found that the engine of the view wrote its own values after the server values. When no op waits, the session now copies the server values into the view after the engine work of the view ends.
  • The review found two more faults, now fixed with a test each:
    • A transport that does not wait for receive let a submit run before its join. The server now runs the messages of each link in order.
    • An op that the engine refuses (for example, a bad name ref) left the model ahead of the log. The server now rebuilds the model from the log and rejects the submit (invalid).
  • IronCalc gives #VALUE! after an incremental delete of all the rows of a range, and #REF! after a fresh load of the same formula. The test compares only the error kind there.

Throughput

LATTICE_PERF=1 pnpm vitest run packages/server/tests/collab/throughput.test.ts. One doc, one setCell for each submit, IronCalc in the same process, memory store. Apple M4.

Cells (half formulas) Submits/s Time for each
100 2,727 0.37 ms
1,000 326 3.1 ms
10,000 31 33 ms

The cost grows with the cell count. IronCalc recalculates every formula, and the adapter reads all cells with content after each recalc (ADR 0015). A large doc needs a batch of submits for each recalc, or a faster diff in the engine.

Interview record

Each question of the interview with its answer and its source. A fact from the code or a document, a question that the author did not ask, and an assumption are rows too.

QuestionAnswerSource
Where does the text of this page come from?From the Markdown file docs/adr/0016-collaboration.md at commit 4c77ac4. The text of each section is the text of the Markdown file. A section that the Markdown file does not have says so.Document: docs/adr/0016-collaboration.md

Changelog

DateAuthorChange
2026-12-02Joe GaudetStatus in the Markdown file: proposed, 2026-12-02.
2026-12-03Joe GaudetCommit 7ce4af2: Lattice 1.0 (#1)
2026-12-04Joe Gaudet (agent)Converted from docs/adr/0016-collaboration.md to this page. The text is the text of the Markdown file.

The adr skill of skillsaw wrote this page. The HTML in this file is the source of truth. The diagrams are the Mermaid sources in it.