Principle:AIScientists Dev WorldSeed Event Sourcing Persistence
| Knowledge Sources | |
|---|---|
| Domains | Persistence, Event_Sourcing |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
An event-sourcing persistence model that records all simulation events as an append-only JSONL stream, snapshots world state per tick for replay, and writes metadata and summaries — making the run the source of truth for replay, gazette generation, and debugging.
Description
WorldSeed uses event sourcing as its persistence strategy. The `RunRecorder` writes to a per-run directory under `~/.worldseed/runs/{run_id}/` containing:
- stream.jsonl: An append-only JSON Lines file that is the source of truth. Each line is a typed event record (action, dm_call, event, register, whisper, gm_set, consequence, highlight, etc.) with tick, timestamp, and payload.
- state.json: The latest world state snapshot, written atomically every tick. The HTTP API reads from disk, not memory.
- snapshots/{tick}.json: Per-tick state snapshots enabling frontend replay.
- meta.json: Run metadata (scene_id, dm_model, start/end time, tick_count, agent_count).
- summary.json: Aggregated kind counts and token totals, written at finalize.
- transient.json: In-memory state (inbox, action queue, events) for pause/resume.
All writes are fire-and-forget — they never block the tick loop. Failed writes are logged but do not crash the simulation. SSE listeners are notified on each record for real-time streaming.
Usage
Use this principle to understand how WorldSeed persists simulation state for replay, debugging, and gazette generation. `RunRecorder` is created at world launch and passed to `WorldEngine`. The `NullRecorder` is a no-op for tests.
Theoretical Basis
Event sourcing separates state derivation from event recording:
# Abstract pattern (NOT real implementation)
def record(kind, tick, **data):
record = {"kind": kind, "tick": tick, "ts": now(), **data}
stream.write(json.dumps(record) + "\n")
notify_sse_listeners(record)
def save_state(entities, tick):
state = {"entities": entities}
atomic_write("state.json", state) # latest for API
write(f"snapshots/{tick}.json", state) # for replay
The append-only stream is immutable — it captures the full causal history. State snapshots are derived projections that can be rebuilt from the stream. This separation enables:
- Replay: Replay any tick from snapshots
- Audit: Full event log for debugging
- Resilience: State.json is atomic; stream is append-only