Developers

Sync protocol

Two endpoints, client-generated ids, last-writer-wins. Simple enough to implement in an afternoon, deterministic enough that two clients replaying the same log end up identical.

Last updated 15 November 2026

Where this stands
This page describes the protocol a client must follow.

Model

  • Two row types: projects and captures. Both are scoped to a workspace. Ids are generated by the client and are globally unique.
  • Every row carries version (integer, starts at 0, bumped by the writer on each edit), updatedAt (ms since epoch, set by the writer), and optionally deletedAt (a tombstone; deleted rows are never removed, only marked).
  • Bytes are not rows. A capture lists its attachments (crop, viewport, pin, clip, frames, voice) and each carries an assetId once uploaded; the bytes go to object storage through the assets endpoints.
  • Capture is local-first. Saving never waits on the network. Sync is additive and can run whenever a session exists.

Push

POST /api/v1/sync/push with up to 500 rows across both arrays. For every row the server loads its current copy and picks a winner:

  1. No current copy: incoming wins.
  2. Incoming version is higher: incoming wins.
  3. Same version and incoming updatedAt is greater than or equal: incoming wins.
  4. Otherwise the server copy wins.

Rows that won are written and their ids listed in accepted. Rows that lost are not written; the winning server copy is returned in superseded and the client should overwrite its local row with it. A push never fails because of a conflict.

The caller needs a seat that captures: a Viewer seat is refused with 402 quota, naming the seat that would allow it.

Clock clamping

When the incoming row wins, the server stores updatedAt = min(incoming.updatedAt, serverTime). A client whose clock runs ahead cannot park a row in the future and make it unbeatable, and pull cursors (which are server times) stay monotonic. Clocks that run behind are not corrected; such rows simply lose ties they might otherwise have won.

Asset ids are sticky

If the winning capture carries an attachment without an assetId and the server already holds one for that attachment id, the server keeps it. A client that has not finished uploading cannot wipe a stored asset by accident. Dropping the attachment from the list is the only way to let one go, and that is deliberate.

Pull

GET /api/v1/sync/pull?cursor=<cursor>&limit=<n> returns the rows changed after the cursor, ordered by updatedAt and then id, tombstones included, at most limit projects and limit captures. more is true when either was cut at limit.

Cursors

  • The cursor is an opaque string the server hands back as cursor in every pull. Store it as it is and send it back; never build or read one. Leave it out to read the whole workspace from the start.
  • It marks the last row returned by its updatedAt and id, so rows that share one updatedAt are never skipped, however many there are.
  • Repeat while more is true. A cursor this server did not write is refused with 400 invalid; pull from the start.
  • Because clamping guarantees no row is stored with a future updatedAt, a cursor never needs to be rewound.

Pulled rows are applied locally with the same winner rule as above, so a pull cannot regress an edit you made since your last push.

Assets

  1. POST /api/v1/assets/upload with the attachment's id, itemId, role, contentType and bytes. The server checks the type against the role, the per-kind size limit and the workspace's storage allowance.
  2. PUT the bytes to uploadUrl with the returned headers. The slot expires after ten minutes. The bytes go directly to object storage.
  3. Push the capture with that attachment's assetId set to the returned value.
  4. Readers call GET /api/v1/assets/:id and get a URL valid for seven days. Assets outside the caller's workspace return 404.

Idempotency

  • Pushing the same row twice is a no-op the second time: same version, same updatedAt, incoming wins on the tie and overwrites itself with identical content. Retrying a failed push is safe.
  • Pulling the same page twice yields the same rows; applying them twice is harmless.
  • Requesting an upload slot twice creates two asset records, and each counts against storage from the moment it is granted. A slot whose bytes never arrive stops counting about a day after it expired. Only the one you reference from the capture matters; one whose bytes did arrive but is never referenced counts until the capture is deleted, or, with retention, until its period passes. A deleted capture takes no uploads (410). An attachment the plan's retention removed comes back from a pull with removedAt set and no assetId; do not upload it again (the upload is refused with 410).
  • There is no request id or idempotency key header. None is needed for the operations above.

What a client must do

  1. Keep version and updatedAt on every local row. Bump version and set updatedAt = Date.now() on every local edit, including deletes (set deletedAt too).
  2. Mark edited rows dirty. On sync, push dirty rows in batches of at most 500, then clear the dirty mark for ids in accepted and replace local rows with anything in superseded.
  3. Upload images before pushing the capture that references them, or push first and push again once the ids are known. Both orders converge.
  4. Pull from the stored cursor until more is false, applying the winner rule locally. Store the new cursor only after the page is applied.
  5. Resolve nothing by hand. The rules are deterministic; a conflict the user needs to see is a product decision for later, not a protocol one.

Example exchange

A member edits a capture offline, comes back online, and finds a teammate changed it in the meantime.

The teammate's copy is at version 4, so it wins despite the older updatedAt. The client adopts it, including the filed reference. Then it catches up on everything else:

The client stores the returned cursor for its next pull. The second row is a tombstone; the client marks its local copy deleted.

Live events

Polling answers what changed; GET /api/v1/events answers when. It is a server-sent event stream (text/event-stream) of thin ticks for the caller's workspace: each one says that something of a kind changed and nothing more, and the client reacts by calling the reads it already has.

  • kind is one of captures, inbox, filings, labels, members, billing (the plan or the subscription changed). A captures tick means run a sync pull or refresh the list; an inbox tick means refetch the counts; a filings tick replaces polling a queued filing every two seconds. Ticks never carry records, so the stream can leak nothing a read would not answer.
  • A comment line about every 25 seconds keeps proxies from closing an idle stream; ignore it.
  • The server ends every stream after ten-odd minutes. Reconnect and carry on: the reconnect re-checks your token, so a revoked key or a removed member stops hearing a workspace within that bound.
  • There is no Last-Event-ID resume. Ticks carry no history: on reconnect, refetch what you show, which is exactly what your polling fallback does. The stream makes a client faster, never more correct, so losing it is never worse than not having it.
  • Both tokens work; an access key needs the read scope. Opening the stream is charged as one request against the usual limits, and the ticks themselves consume no budget. A workspace holds a bounded number of open streams per server process; past it the request answers 429.
  • In @app/sdk-api: client.events.subscribe({ onTick, signal }): one call is one connection (built on fetch, because EventSource cannot send a bearer header); reconnect by calling it again with backoff.

Reference

  • Schemas: packages/contracts/src/api.ts (SyncCapture, SyncProject, request and response types).
  • Rules: packages/domain/src/capture/sync.ts (pickWinner, ChangeSync).
  • Decision records: docs/adr/0004-storage-and-sync.md; live events docs/adr/0013-live-events.md.
  • Endpoint details: API.