Persistence: journals, replay, restore
CIG2 never saves session state; it journals inputs. Every step’s input (an action, a decision answer, a timer delivery, a decision expiry, an admin operation) is appended to the session’s journal before the step is broadcast. Each record carries the state digest after the step.
Restoring a table replays its journal through the real step functions (cig2.WithReplay). Closures (OnResolve, phase hooks, settle steps) come back without any game code, and the digest is checked at every step. Timers still pending are re-armed for their remaining time.
Turning it on
- A host:
host.Config{DataDir: "/var/lib/mygame"}or-data-dir. One process owns a data directory. - By hand:
cig2.WithJournal(j)with ajournal.FileStorejournal orjournal.Mem, then latercig2.WithReplay(records).
What your game must do
Be deterministic, which it already must be to pass cig2test:
s.Now(), nevertime.Nows.Rand(), nevermath/randfunctions- no package state
- no ranging a map while drawing randomness
Within one journaled step, s.Now() returns one instant, in UTC.
Config must marshal to JSON. Replay hands it to Setup as json.RawMessage; decode it with cig2.DecodeConfig. (Under Go’s JSON v2, json.RawMessage is an alias for jsontext.Value, so %T prints that name; it is the same type.) A []byte config is recorded as the JSON it holds.
Change state only through steps. A Session.Do that mutates a journaled session fails it (E103).
When replay diverges (E101)
The journal is left untouched, and the table is not restored. The usual cause is a rules change deployed while a game was in progress. Implement cig2.GameVersioned so the journal records your rules version and the error can name it.
Seat tokens
A host keeps only SHA-256 hashes of seat tokens, on disk and in memory, so tokens survive a restart.