Sync / foldkit-sync

What makes a Message durable

Replay runs a durable Message through update on another machine, later, possibly many times as the base beneath it moves. So it has to be a deterministic, state-only transition of the shared slice. make derives replay from update and refuses a durable Message whose transition:

  • returns a Command, because an effect cannot be replayed; or
  • changes a field outside shared, because that change would be lost.

A refused Message fails submit with ReplayError and writes nothing, so a mistake shows on the first edit instead of as diverging replicas later.

Replay also reads nothing but the slice. A durable Message whose update reads a local field (a filter that decides which todos to clear) shows one slice on this page and replays to another everywhere else, the server included. Sync.mount catches it as the Message is dispatched: it runs update again with every local field that is not at its initial value reset, and when the two slices differ it does not apply the Message and does not submit it. The failure reaches onPersistenceFailure as a ReplayError that names the fields: durable "ClearedShown" reads Model fields outside the shared projection: filter. A Message that reads a local field and still produces an equal slice is accepted, and so is one over a slice that holds pending edits. The check costs one more update per durable dispatch, and none while every local field is still at its initial value. replica.submit has no Model and cannot make it; a replica used without mount is not checked.

The consequence is a pattern you will use everywhere: an intent mints, a fact records. Keep a local intent Message (such as RequestedTodo { title }) whose update returns a Command that reads the clock or mints an id, then dispatches the pure, durable fact Message (CreatedTodo { id, title, at }) over the shared slice.

Put every nondeterministic input (ids, timestamps, the result of a provider call) inside the fact. update for the fact only applies it.

When the intent needs nothing outside the Model, say an id from a counter in the shared slice, return the fact as Sync.fact(message) instead of an ordinary Command. The mount applies it in the intent's own transition, before any later Message, so two intents dispatched together cannot read the same counter. Outside the mount it is an ordinary Command.

A durable update should also be idempotent where a retry could deliver one operation twice, as in CreatedTodo ignoring an id it already holds.