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 optionallydeletedAt(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 anassetIdonce 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:
- No current copy: incoming wins.
- Incoming
versionis higher: incoming wins. - Same
versionand incomingupdatedAtis greater than or equal: incoming wins. - 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
cursorin 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
updatedAtandid, so rows that share oneupdatedAtare never skipped, however many there are. - Repeat while
moreis true. A cursor this server did not write is refused with400 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
POST /api/v1/assets/uploadwith the attachment'sid,itemId,role,contentTypeandbytes. The server checks the type against the role, the per-kind size limit and the workspace's storage allowance.PUTthe bytes touploadUrlwith the returnedheaders. The slot expires after ten minutes. The bytes go directly to object storage.- Push the capture with that attachment's
assetIdset to the returned value. - Readers call
GET /api/v1/assets/:idand get a URL valid for seven days. Assets outside the caller's workspace return404.
Idempotency
- Pushing the same row twice is a no-op the second time: same
version, sameupdatedAt, 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 withremovedAtset and noassetId; 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
- Keep
versionandupdatedAton every local row. Bumpversionand setupdatedAt = Date.now()on every local edit, including deletes (setdeletedAttoo). - Mark edited rows dirty. On sync, push dirty rows in batches of at most 500, then clear the dirty mark for ids in
acceptedand replace local rows with anything insuperseded. - Upload images before pushing the capture that references them, or push first and push again once the ids are known. Both orders converge.
- Pull from the stored cursor until
moreis false, applying the winner rule locally. Store the new cursor only after the page is applied. - 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.
kindis one ofcaptures,inbox,filings,labels,members,billing(the plan or the subscription changed). Acapturestick means run a sync pull or refresh the list; aninboxtick means refetch the counts; afilingstick 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-IDresume. 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
readscope. 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 answers429. - In
@app/sdk-api:client.events.subscribe({ onTick, signal }): one call is one connection (built on fetch, becauseEventSourcecannot 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 eventsdocs/adr/0013-live-events.md. - Endpoint details: API.