Linora

0019. Sync v2: authoritative sequencer with a compact structural index

Status: Accepted (2026-11-27). Supersedes 0005.

Context

0005 had the server hold every open page document in memory to validate ops. Rejected: server memory would grow with document size (a 682K-node page is ~80 MB as a full doc) and the server would fall over with many users. New requirements:

Decision

Pipeline (same for WS and HTTP batches): authenticate (signed token) -> authorize (role/scope from token) -> zero-copy decode + stateless validation (gateway) -> structural validation, sequencing, journal group commit (room owner) -> ACK with per-op verdicts to the author -> BROADCAST of accepted ops, encoded once, to everyone else. Broadcast and ACK both happen after the journal commit, so no client ever sees an op the server could lose.

Stateless checks (gateway, every op, ScanBatch in shared/doc-api.ts): framing, op kind, varint bounds, prop id range, value type vs PROP_INFO, finite numbers and min/max, enum ranges, string/blob/key byte sizes, UTF-8, key syntax, ops per batch, CREATE id ownership (clientOf(id) === connection clientId), no writes to ROOT, rate limits, role/scope. Zero-copy over the received bytes with preallocated typed-array scratch; target < 300 ns per simple op.

Structural checks (room owner, StructIndex): node exists, id unique, parent exists and is a container, no reparent cycle, prop applicable to the node’s kind, no edits to deleted nodes, nodes-per-page limit. Uses a per-room structural index, no properties:

Column Type Bytes/node
id (sorted base array; position = slot) Float64 8
parent slot Int32 4
kind Uint8 1
flags (deleted, …) Uint8 1
total   14

Clients (local = canonical + pending):

Defence in depth: replicas apply sequenced batches with origin: 'sequenced', where any still-invalid op is a deterministic no-op. Canonical snapshots (pre-order, props sorted) make replicas byte-comparable in tests.

Dedupe and ids: batches carry clientSeq; the owner keeps the last committed clientSeq per connected client and, on resume, reads max(client_seq) for that client from the journal (one indexed query, only for clients that report pending batches). clientIds are allocated in blocks of 1,024 per file (one DB write per block) and bound to the user by a signed clientId grant (24 h), so a reconnecting client keeps its id space without server memory or a DB read.

Rules versioning: DOC_RULES_VERSION is sent in hello and stored per journal row. Any change that could make two builds disagree on validity bumps it. Mismatched clients reload. Deploys that bump it compact every page with a non-empty tail before switching.

Why a malicious or buggy client cannot break convergence or corrupt state

Alternatives

Consequences