Implementation:AIScientists Dev WorldSeed RunRecorder
| Knowledge Sources | |
|---|---|
| Domains | Persistence, Event_Sourcing |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete event-sourcing recorder that persists simulation events as an append-only JSONL stream, snapshots world state per tick, and writes metadata and summaries to `~/.worldseed/runs/{run_id}/` provided by the WorldSeed library.
Description
The RunRecorder class writes to a per-run directory containing:
- stream.jsonl: Append-only typed event stream (source of truth). Event kinds: event, action, dm_call, perceive, register, wakeup, whisper, gm_set, gm_remove, gm_resolve, consequence, highlight.
- state.json: Latest world state snapshot (atomic write, API reads from disk).
- snapshots/{tick}.json: Per-tick snapshots for replay.
- meta.json: Run metadata (scene_id, dm_model, start/end time, status).
- summary.json: Aggregated counts and token totals (written at finalize).
- transient.json: In-memory state for pause/resume.
- counters.json: Engine counters (dm_call_count).
All writes are fire-and-forget — they never block the tick loop. SSE listeners are notified on each record. The `NullRecorder` is a no-op implementation for tests.
Usage
Import this class when you need to persist run data. Created at world launch and passed to `WorldEngine`. The gazette generator reads from the run directory this recorder creates.
Code Reference
Source Location
- Repository: WorldSeed
- File: src/worldseed/persistence.py
Signature
class RunRecorder:
def __init__(
self,
run_id: str,
config_path: Path | None,
scene_id: str,
dm_model: str,
resolved_config: dict[str, Any] | None = None,
) -> None: ...
def record(self, kind: str, tick: int, **data: Any) -> None: ...
def save_state(
self,
entities: list[dict[str, Any]],
tick: int,
*,
characters: dict[str, dict[str, Any]] | None = None,
) -> None: ...
def save_counters(self, **counters: Any) -> None: ...
def load_counters(self) -> dict[str, Any]: ...
def save_transient(self, data: dict[str, Any]) -> None: ...
def load_transient(self) -> dict[str, Any]: ...
def update_status(self, status: str) -> None: ...
def finalize(self, tick_count: int, agent_count: int) -> None: ...
class NullRecorder:
"""No-op recorder for tests."""
Import
from worldseed.persistence import RunRecorder, NullRecorder
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| run_id | str | Yes | Unique run identifier |
| config_path | Path | No | Path to scene config (copied to run dir) |
| scene_id | str | Yes | Scene identifier |
| dm_model | str | Yes | DM model name |
Outputs
| Name | Type | Description |
|---|---|---|
| run_dir | Path | Directory containing run data |
| stream.jsonl | File | Append-only event stream |
| state.json | File | Latest world state (atomic write) |
Usage Examples
Basic Recording
from worldseed.persistence import RunRecorder
from pathlib import Path
recorder = RunRecorder(
run_id="run_001",
config_path=Path("configs/teahouse.yaml"),
scene_id="teahouse",
dm_model="gpt-4o",
)
# Record events during simulation
recorder.record("action", tick=1, agent_id="agent_1",
action_type="move", params={"target": "garden"}, success=True)
# Save state snapshot
recorder.save_state(entities=[{"id": "agent_1", "type": "agent"}], tick=1)
# Finalize at shutdown
recorder.finalize(tick_count=100, agent_count=4)
With WorldEngine
from worldseed.world import WorldEngine
from worldseed.persistence import RunRecorder
recorder = RunRecorder("run_001", Path("configs/teahouse.yaml"), "teahouse", "gpt-4o")
engine = WorldEngine(config_path="configs/teahouse.yaml", recorder=recorder)
# Engine calls recorder.record() and recorder.save_state() automatically