Principle:4thfever Cultivation world simulator Mutation Serialization
| Knowledge Sources | |
|---|---|
| Domains | Concurrency_Control, Software_Architecture |
| Last Updated | 2026-10-22 14:00 GMT |
Overview
A concurrency control pattern that serializes all state-mutating operations through a single lock, preventing race conditions between the simulation's own step loop and external API-driven interventions.
Description
Mutation Serialization addresses a fundamental tension in interactive simulations: the simulation runs its own step loop (advancing time, executing phases), while external agents can issue commands (create avatars, pause, set phenomena) at any time. Without coordination, these concurrent writes corrupt world state.
The pattern introduces a mutation lock on the runtime object:
- `runtime.run_mutation(callable, *args, **kwargs)` — acquires an exclusive lock, executes the callable, and releases
- `runtime.run_mutation_measured(callable, ...)` — same, but also records timing metrics
- The simulation's `step()` also runs inside the mutation lock
- External command endpoints wrap their service calls in `run_mutation`
This ensures only one mutation happens at a time, whether it originates from the simulation step or an API command. Queries (reads) do not need the lock and can proceed concurrently.
Usage
Use this principle when:
- A simulation has its own execution loop AND accepts external write commands
- Concurrent writes to shared game state would cause data corruption
- You want reads (queries) to remain lock-free for performance
- The simulation step and API commands share the same world object
Theoretical Basis
The core invariant is single-writer, multi-reader:
# Abstract algorithm description (NOT real implementation)
class Runtime:
_mutation_lock = asyncio.Lock()
async def run_mutation(self, fn, *args, **kwargs):
async with self._mutation_lock:
return await fn(*args, **kwargs)
def run_mutation_sync(self, fn, *args, **kwargs):
with self._mutation_lock:
return fn(*args, **kwargs)
# Simulation step runs inside the lock
async def simulation_loop(runtime):
while running:
await runtime.run_mutation(simulator.step)
await asyncio.sleep(tick_interval)
# External commands also run inside the lock
@router.post("/command/avatar/create")
async def create_avatar(req):
return await runtime.run_mutation(
command_service.create_avatar, req
)
# Queries do NOT need the lock
@router.get("/query/world/state")
def get_world_state():
return query_service.get_world_state()
Key design properties:
- Exclusive mutation: only one mutation (step or command) executes at any time
- Lock-free reads: query endpoints read world state without acquiring the lock, accepting eventual consistency
- Measured mutations: `run_mutation_measured` wraps the callable with timing instrumentation
- Graceful degradation: if the world is not initialized, mutations return a 503 error
Practical Guide
When implementing mutation serialization:
- Route ALL writes through the lock: every API command and the simulation step must acquire the mutation lock
- Keep mutations fast: long-running LLM calls inside a mutation will block all other commands; consider background tasks for non-critical work
- Use measured variants for instrumentation: `run_mutation_measured` records timing that helps identify slow mutations
- Don't lock reads: query endpoints should read state directly; the simulation produces consistent snapshots per step
- Handle initialization specially: the init flow runs inside `run_mutation` to prevent commands from seeing a half-built world