Convex sync (local-first → server) #
Status: Implemented in single-user / dogfood mode (no auth). Verified end-to-end —
push + reactive pull — on the simulator against the dev deployment.
Date: 2026-06-24.
Extends: local-first.md.
Code: server convex/sync.ts + convex/schema.ts; client src/data/sync/ (engine, owner)
- reconcile in
src/data/task-store.ts; mounted as<ConvexSync/>insrc/app/_layout.tsx.
Context #
The local-first foundation has shipped: the local MMKV store is the single source of truth, Convex is out of the runtime path, and the app works with no account and no network. We now want signed-in users to sync their tasks across devices (e.g. an iPad and an Android phone).
Requirements (from the product owner):
- Edits made offline queue locally and flush to the server on reconnect.
- Writes are idempotent — a replayed/retried write never creates a duplicate.
- Multiple clients stream writes concurrently and must converge.
- Convex is the source of truth — on conflict, the server wins.
- Clients keep a queue of optimistic writes (UI stays responsive/offline-capable).
Hard constraint: this must not regress the no-account / offline default. The sync engine is inert until a user signs in; signed-out behaviour is exactly today's.
Decision #
Adopt an optimistic local cache + offline outbox over an authoritative server. Two axes, deliberately separated (they sound contradictory but aren't):
- Data path is local-first: the UI always reads/writes the local store and never blocks on the network — signed in or out.
- Conflict authority is the server: when local optimistic state and the server disagree, the server wins. Local writes are provisional until confirmed.
Concretely:
- No CRDT (no Yjs). Rationale below.
- Identity = idempotency. Tasks already carry a client-generated UUID. Convex rows
key on
(ownerId, clientId)with a unique index, so every write is an upsert by clientId — replaying it, from any client, any number of times, cannot duplicate. - Outbox = a dirty-set, not an operation log. Mark touched tasks dirty and push their current state. Ten offline edits to one task collapse into one upsert.
- Conflict resolution = whole-row last-write-wins by
updatedAt. Deletes ride along as thedeletedAttombstone (already in the schema). - Convergence = Convex's reactive subscription. All writes funnel through Convex, so the server is the single serialization point; clients reconcile server → local.
- The session boundary stays device-local and is never synced — it describes this device's current pass boundary, deleting a whole category of multi-device conflict. The visible Done count is derived from synced task rows completed after that boundary, so remote completions still update the current device's count.
- Counters never need cross-device merge — single-writer by construction.
resistance/exposureCountare written only byassess, i.e. only during a Plan session, which is single-device — so concurrent increments don't arise. The UX "Ready" count is derived from task status; "Done" is derived from synced task completions after the local session boundary. No synced counter exists to merge.
Why not a CRDT (Yjs)? #
CRDTs earn their complexity when there is no central authority and peers must merge fine-grained concurrent edits to a shared mutable structure. We have the opposite of every premise:
| CRDTs are for… | Glide's situation |
|---|---|
| No central server; peers merge | Convex is a central serialization point |
| Co-edited shared documents (text/rich) | Independent task records, coarse ops |
| Character-level concurrent edits | create / assess / complete, one op |
| Multi-user real-time collaboration | Single user, multiple devices |
A central authoritative server is the thing CRDTs work hard to avoid needing — we already have a good reactive one. Yjs would add a CRDT runtime, document encoding, and a "peers converge on their own" model that fights "Convex decides," for zero benefit here. The single-user / multi-device framing is what makes this the easy sync case. (If this ever became a shared, real-time co-edited board, revisit.)
Design #
Convex schema (additions) #
Mirror LocalTask (drop the legacy context/minutes/lane) and add ownership + sync
keys. The Convex _id stays internal; clientId is the cross-device identity.
tasks: defineTable({
ownerId: v.id('users'), // scopes every row to an account
clientId: v.string(), // the client-generated UUID (TaskId)
title: v.string(),
resistance: v.number(),
status: taskStatus, // 'open' | 'ready' | 'done'
exposureCount: v.number(),
createdAt: v.number(),
updatedAt: v.number(), // the LWW key
lastDecision: v.optional(decision),
completedAt: v.optional(v.number()),
deletedAt: v.optional(v.number()), // tombstone
})
.index('by_owner_client', ['ownerId', 'clientId']) // unique upsert target
.index('by_owner_updated', ['ownerId', 'updatedAt']), // incremental pulls
The existing per-operation mutations (createTask/assess/complete/…) become
obsolete under this model — sync is push/pull, not per-op RPC. Remove them when sync
lands (keep convex/tasks.ts as reference until then).
Server functions #
pushTasks({ tasks: TaskRow[] })— for each row, upsert by(ownerId, clientId): insert if absent, else patch only ifincoming.updatedAt >= stored.updatedAt(LWW). Idempotent by construction.ownerIdcomes from the authenticated identity, never the client. Returns each row's appliedupdatedAtso the client can confirm.getTasks({ since?: number })— reactive query returning the caller's rows (optionally only those withupdatedAt > since, viaby_owner_updated) for the subscription. Tombstoned rows are included so deletes propagate; the client filters them (useBoardalready dropsdeletedAt).
Client (extends the local store) #
LocalTask already has id / updatedAt / deletedAt. Add two fields:
ownerId: string | null— null for local-only rows; stamped when the local workspace is combined with an account.syncedAt?: number— theupdatedAtthe server last confirmed. A row is dirty iffupdatedAt > (syncedAt ?? 0)— no separate dirty-set needed, and it records exactly which version is pending.
Every mutator already bumps updatedAt; that alone now marks the row dirty.
A sync engine behind the task-repository.ts seam, active only when signed in + online:
- Push loop: collect rows where
updatedAt > syncedAt→pushTasks(batch). On ack, setsyncedAt = updatedAtonly for rows whoseupdatedAtdidn't advance during the push (an edit mid-flight keeps the row dirty for the next round). - Pull loop: subscribe to
getTasks. For each server rowR(clientIdC):- no local row → insert
R. - local row clean (
updatedAt == syncedAt) → overwrite withR(server wins). - local row dirty → keep local; our pending edit carries a newer
updatedAt, so it wins when it pushes. (Globally: the write with the newestupdatedAtwins everywhere.)
- no local row → insert
- Reconnect = re-run the push drain; the subscription auto-resumes. No special path.
Reconciliation on sign-in #
Tasks created while signed out have ownerId == null. Creating a new account automatically
combines every live local task with that account. Signing into an existing account first
downloads its snapshot, then asks whether to combine tasks or use synced tasks.
Replacement is unavailable until that snapshot succeeds, so an offline/network failure never
clears the local workspace.
Changing only ownership does not bump updatedAt: doing so would make an unchanged local
copy falsely beat a genuinely newer server edit. Moving a live task between accounts preserves
its content timestamp and clears syncedAt, making it eligible to push. Foreign-account
tombstones are discarded because they cannot represent a deletion in the destination account.
Auth (prerequisite) #
Sync needs an ownerId, which needs auth. Recommend Sign in with Apple (iOS-first
feel) via Convex Auth, Android later. The engine stays inert until an identity exists.
Consequences #
Pros
- Small — a
pushTasksmutation +getTasksquery + a sync engine and two store fields; ~a few hundred lines. Reuses the foundation (client UUIDs,updatedAt, tombstones, seam). - Offline-robust, multi-client convergent, idempotent, no CRDT runtime.
- Different devices editing different tasks never conflict — independent rows just both apply.
Limits / risks
updatedAtis a wall-clock LWW key → clock skew across devices can let a stale edit win ("last to sync" vs "last edited"). Accepted for one user's roughly-synced devices (see §Decisions) — "the last edit I made wins" is the intended semantics. Escape hatch if skew ever bites: a hybrid logical clock (HLC).- Counters don't merge — and don't need to.
resistance/exposureCountchange only during planning (sole writer:assess), which is single-device, so there's no concurrent increment to lose. Not a risk; called out only because it looks like one. - Requires auth before any sync exists.
Decisions & open questions #
updatedAtauthority — RESOLVED: client wall-clock. The LWW key is each device's wall-clock at edit time, so "the last edit I made wins" regardless of server arrival order. Validated against the prototype (prototypes/convex-sync,client-clockmode). Accepted caveat: relies on devices' clocks roughly agreeing — a badly-wrong device clock could let a not-actually-last edit win. A non-issue for NTP-synced consumer devices; revisit with a hybrid logical clock only if it ever bites.- Counter exactness — RESOLVED: not needed.
resistance/exposureCountare written only during planning (assessis the sole writer), which is single-device — single writer by construction, so concurrent offline increments don't arise. The UX counts aren't synced counters anyway (Ready = derived from status; Done = device-local tally). There is no counter to merge. If a future feature ever incremented a counter from multiple devices, the fit would be an append-only decision log (immutable events keyed by a client event-id → idempotent by the same pattern as tasks; yields the count and history for free). Rejected:@convex-dev/sharded-counter— it targets write contention a single-user app doesn't have, itsadd()isn't idempotent under the at-least-once outbox (forcing a dedup table), and it splits the model. - Auth provider: Sign in with Apple via Convex Auth — confirm.
- Push mechanics: batch size, backoff, partial-failure handling.
Prototype plan (next) #
A throwaway spike to validate the risky bits before committing:
- Add the
ownerId/clientIdschema +pushTasks/getTasks; stub auth with a fixedownerId. - Add
ownerId/syncedAtto the store + a minimal sync engine behind the repository seam. - Drive two simulators (iPad + phone) and watch convergence via the reactive pull.
- Kill the network mid-edit, queue several creates, reconnect → confirm the outbox flushes.
- Double-push the same batch → confirm idempotency (no duplicates).
- Bump the same task's
resistanceon both devices offline → observe the LWW outcome and decide whether counter exactness matters.