Deterministic Command Logs & Time-Travel Debugging
In-place state mutations erase the causal history that triggered a UI bug, making production corruption irreproducible. Recording every user intent as a serializable command in an append-only log enables deterministic replay, Undo/Redo, and bug-report time-travel debugging.
Deterministic Command Log & Time-Travel Replay
Trace how pure commands append to an immutable log and replay deterministically to any cursor index.
Timestamp and UUID are computed at dispatch time so the command object is 100% deterministic.
log.push({ seq: 2, type: 'ADD_ITEM', id: 'itm_9', ts: 1731770002 });- logLength
- 3
- cursor
- 2
- itemCount
- 2
Under the Hood
Event sourcing treats current UI state as a pure left-fold (reduce) over an ordered log of immutable commands starting from initialState. Replaying the exact sequence on another machine produces bit-identical state only if reducers remain deterministic.
- Pure command objects: Serialize
{ type, payload, timestamp, seq }without class instances, functions, or DOM nodes. - Deterministic reducers: Read
Date.now()orcrypto.randomUUID()at command creation time, never inside the reducer, so replay never diverges. - Checkpoint snapshots: Pair the log with periodic CRDT or structural-sharing checkpoints every $N$ commands so scrubbing to index 10,000 skips replaying from zero.
type Cmd = { type: 'ADD'; delta: number } | { type: 'SET'; value: number };
function replay(commands: Cmd[], targetIndex: number, initial = 0): number {
let state = initial;
for (let i = 0; i <= targetIndex; i++) {
const cmd = commands[i];
state = cmd.type === 'ADD' ? state + cmd.delta : cmd.value;
}
return state;
}Problem Genealogy
- 2014–2016 (Elm Architecture & Redux DevTools): Introduced time-travel debugging by keeping actions pure and serializable, though storing unbounded action histories leaked hundreds of megabytes in long sessions.
- 2018+ (Design Tools & Multiplayer Editors): Combined bounded command logs with periodic snapshots and inverse patches for sub-millisecond scrubbing.
Production Pitfalls
- Browser tab hits 2 GB OOM after 4 hours in a canvas editor: Retaining every mouse-move command and full-document snapshot in memory leaks unbounded heap; coalesce continuous gestures and cap log length with periodic checkpoints.
- Time-travel replay produces different state than live session: Calling
Math.random()or readingwindow.innerWidthinside the reducer breaks determinism on replay.
- When not to use client event sourcing: For simple CRUD forms, ephemeral hover/scroll UI, or high-frequency 120 Hz pointer streams without coalescing, storing an append-only command log wastes heap memory and CPU on replay.
Connections & Logical Next Steps
Structural sharing snapshots let command logs rewind state in O(1) without cloning the full document.
Replicated operation logs require deterministic command application to converge across offline clients.