Skip to content

Game state (Automerge)

One document per game

The Engine's game document (Chronicle) wraps an Automerge document. A deck or pile (Stack), board and its zones (Space), and shoe or draw pool that combines stacks (Source) read and write parts of that same document. Write custom game state through game:setState.

Automerge tracks which changes depend on which earlier changes. It is a conflict-free replicated data type (CRDT): peers can apply each other's changes and converge on the same document. A sync update carries document changes, not commands to run a second time. The game document emits state:changed and the Engine emits state:updated.

Mutation boundary

Application code should call the asynchronous engine.dispatch(type, payload) API. The TypeScript ActionRegistry handler opens the Chronicle transaction. This single path preserves action history, checkpointing, events, policies, and dispatch error reporting. Direct caller-side session.change() bypasses those lifecycle guarantees.

What convergence does not provide

CRDT merge guarantees convergence, not legality. For example, two peers or forks can each draw the same top card and place it in a different hand. Merging those changes can leave the card in both hands even though each draw was locally legal. Use an authoritative server to validate moves in one order and send per-player views when the game has hidden information. Use peer sync only when the game can handle the results of concurrent changes.