# syncbase — shared state for people and their agents

> Markdown twin of https://syncbase.submodule.nl/. Full API reference: https://syncbase.submodule.nl/docs.md

syncbase is a local-first sync engine: a database that runs inside your app and at the edge. Reads come from local memory. Writes apply instantly. Every change reaches every collaborator — browser tab, phone or agent — in one hop.

- 0 ms reads and optimistic writes against the local replica
- 1 hop from a write to every subscriber (edge fan-out → sequencer → push)
- <1 MB of memory to answer a lookup from immutable segments with range reads
- 0 databases: Durable Objects and R2 are the whole backend

## 01 — Why a sync engine

Apps no longer have one user per screen. A person opens a task, three agents start working on it, a teammate comments while the person rewrites the plan. Everyone is a writer, and agents write fast, in parallel, for minutes at a time. Request–response apps were built for one writer who waits, and break in four ways:

1. **Stale views** — the agent finished; the tab still says running.
2. **Lost writes** — two writers save the same row; the last request wins the whole row.
3. **Waiting** — every click is a round trip; every agent step a refetch.
4. **Glue** — cache keys, polling, sockets, retry queues and offline mode, each slightly wrong.

A sync engine replaces all of it with one primitive: a replicated database with a single order of events.

## 02 — Architecture

| Layer | Runs on | Does |
| --- | --- | --- |
| Client replica | Rust → WASM in a Worker, OPFS | Segments + WAL, optimistic overlay, reactive models |
| Fan-out | Durable Object at the edge | Hibernated sockets, sync-group filtering, presence, CRDT relay, write coalescing |
| Sequencer | Durable Object, one per workspace | Validate, run commands, assign `seq`, hot WAL, flush L0 segments |
| Segments | R2 + edge cache | Immutable `SBSEG001` files, posting-list indexes, compacted out of band |

Write path: write locally → sequence → fan out → rebase (drop overlay, apply, replay pending) → compact.

## 03 — Built for agents

- **Same SDK everywhere** — an agent is a `SyncedStore` in Bun, Node or a Worker.
- **Overlays** — draft into a named overlay; a person promotes or discards it in O(1).
- **Commands** — named, deterministic handlers run on client and server; intent survives concurrency.
- **CRDT fields** — stream tokens into text a person is editing; merges commutatively.
- **Presence** — ephemeral awareness frames through the edge, never stored.
- **Sync groups** — an agent's token lists the groups it may see; it receives nothing else.

## 05 — Why syncbase

- Linear's sync architecture (partial loading, sync groups, segment store, semantic commands) as an open library.
- One segment format from R2 to OPFS: a cold start is a cacheable range read.
- No database to run: Durable Objects + R2.
- Three merge rules in one model: per-field LWW, commands, CRDT fields.
- Named overlays as a primitive for drafts and agent proposals.
- One Rust core: WASM in browsers and Workers, UniFFI for Swift and Kotlin; verified with property-based state machines, crash-consistency tests and Quint specs.

| | syncbase | Postgres sync (Zero, Electric) | Document CRDTs (Jazz, Automerge, Liveblocks) | Event-sourced SQLite (LiveStore) |
| --- | --- | --- | --- | --- |
| Source of truth | Own engine on DOs + R2 | Your Postgres | Documents merged on every peer | Event log per store |
| Writes | Optimistic, offline, rebased by seq | Server mutators or your API | Local, merged commutatively | Local events, rebased |
| Conflicts | Per-field LWW · commands · CRDT fields | Server decides | CRDT for every value | Event reducers |
| Partial sync | Sync groups + index subscriptions | Queries or shapes | Per document | Per store |
| Cold start | Range reads from edge cache | Replicate query results | Load documents | Replay or snapshot |

## 07 — Limits (v0.1)

- No SQL engine or incremental view maintenance.
- No Postgres/CDC ingest.
- ~1,000 requests/s per sequencer DO (batched by fan-out; shard with partitioned sequencers).
- Write policies are TypeScript hooks, not a declarative language.
