Deterministic Record & Replay

Hale's flight recorder (LOTUS_OBS_RECORD) captures a program's message delivery schedule, wire ingress payloads, and non-deterministic standard library…

Hale’s flight recorder (LOTUS_OBS_RECORD) captures a program’s message delivery schedule, wire ingress payloads, and non-deterministic standard library reads (std::time, std::rand, std::env). The hale replay command re-executes the program hermetically against that recording to reproduce bugs deterministically, verify behavior with --diff, or backtest modified code with --feed.

Terminal window
# 1. Record an execution to disk
LOTUS_OBS_RECORD=run.halerec hale run app.hl
# 2. Re-execute and verify that all schedules, events, and journals match
hale replay run.halerec app.hl --diff

Recording a Run (LOTUS_OBS_RECORD)

Section titled “Recording a Run (LOTUS_OBS_RECORD)”

Set LOTUS_OBS_RECORD=<path> to enable lossless flight recording (see spec/runtime.md § Lossless recording mode):

Environment Variable Default Behavior
LOTUS_OBS_RECORD=<path> Unset Writes a lossless .halerec recording to <path> (implies LOTUS_OBS=1; requires no attached observer).
LOTUS_OBS_RECORD_DURABLE=1 Unset Syncs each drain sweep and finalize trailer to disk (fdatasync on Linux, F_FULLFSYNC on macOS) and syncs the parent directory on file creation.
LOTUS_OBS_RECORD_ENV=full Withheld Captures full std::env variable values in the journal. By default, only variable names, existence, and byte lengths are recorded to protect secrets.

Unlike live observation (LOTUS_OBS=1), which drops telemetry records rather than slowing down the application, LOTUS_OBS_RECORD prioritizes completeness:

What a Recording Captures

Section titled “What a Recording Captures”

Every .halerec file records four streams:

  1. Per-consumer delivery schedule: Every queued handler invocation records (target locus, message id) on the consuming thread, capturing the exact interleaving across concurrent publishers.
  2. Public bus events: All publish and delivery events—including synchronous devirtualized direct-dispatch calls—with file-side identity maps for cross-run alignment.
  3. Payloads and wire ingress tape: Wire-encoded payloads (including all frames accepted by unix:// and udp:// listen bindings) are captured verbatim as an injectable ingress tape. In-process flat struct publishes are recorded as metadata only (declared byte size and publish identity).
  4. Non-deterministic input journal: Every call to std::time::now, std::time::monotonic, std::time::monotonic_ns, std::rand::next_int, std::os::getrandom, and std::env is journaled per consumer with its encoded arguments and return value (unless std::env values are withheld by default).

Replaying a Run (hale replay)

Section titled “Replaying a Run (hale replay)”

hale replay recompiles the source program using the same pipeline as hale run and executes it against the recording:

Command / Flag Description
hale replay run.halerec app.hl Re-executes app.hl (or a directory seed app/) using the recorded schedule, ingress tape, and input journal.
--diff Compares consume schedules, public bus events, payloads, and journals bidirectionally; exits non-zero on any divergence.
--diff --json Emits the --diff verdict and per-category coverage counts as a single JSON object.
--at <N> / --at <consumer>:<N> Sends SIGSTOP to the process at global consume #N or at <consumer>’s Nth consume so you can attach gdb or lldb at the exact step.
--allow-truncated Admits a crash-truncated recording (missing its finalize trailer) and replays the valid prefix. Under --diff, post-crash execution counts as surplus rather than divergence.
--feed Backtesting mode: injects the recorded wire ingress tape into modified code while leaving clocks, RNG, std::env, and argv live.
--allow-unmatched-feed Under --feed, permits the run to succeed even if some recorded ingress frames were not consumed.
--allow-live-effects Permits replaying a program whose call graph reaches unjournaled syscall, ffi, or unclassified effects.
--allow-unverified-model Bypasses the executable identity hash check to replay against modified source code or unstamped recordings.
--dev / --env <name> Passes the build flags used when the recording was created (required to match the recorded execution identity).
--target-cpu <native|portable|cpu> The CPU to rebuild for. Without it, the replay uses the CPU the recording was made with: the host’s by default, or the portable baseline for a --target-cpu portable build. A build for a named CPU is replayed with that name.

Three-Stage Replay Admission

Section titled “Three-Stage Replay Admission”

Before executing a single instruction of the target program, hale replay enforces three admission checks:

  1. Live-effect safety gate: Re-executing a program would normally repeat real external side effects. If the program’s effect frontier reaches syscall, ffi, or unclassified, hale replay refuses to run and names the offending call chain unless you pass --allow-live-effects. (bindings blocks do not trigger this refusal because bound network transports are suppressed hermetically during replay.)
  2. Executable identity verification: The recording header stores a framed SHA-256 hash covering the compiler version, runtime/stdlib sources, build flags (--dev, --env), and the relative paths, lengths, and contents of all source files in the seed. hale build and hale run produce identical hashes (DWARF debug info is excluded), so a recording captured from a compiled production binary (./myapp) replays cleanly against its source directory (hale replay run.halerec myapp/). Any edit to a function body or mismatch in build flags is rejected unless --allow-unverified-model or --feed is passed.
  3. Artifact integrity: The runtime re-reads the inherited file descriptor into private memory and validates every frame and entry count up to the finalize trailer.

During execution, the runtime serves journaled inputs per consumer and releases queued deliveries in the exact recorded order (holding early arrivals in a per-consumer reorder buffer). If execution runs past the recorded history, reads and deliveries fall back to live execution and increment divergence counters printed at exit.

Verifying Execution & Coverage (--diff)

Section titled “Verifying Execution & Coverage (--diff)”

Passing --diff fails the replay if any divergence occurs and prints a per-category verification summary:

replay matches the recording (26 ring records). Compared, by category:
public bus events: 20 across 1 consumer (publish + deliver, subject-aligned)
payloads: 10 (sizes and identities; contents not canonicalised)
queued consumes: not exercised: no queued consumer deliveries were recorded; dispatch was direct (valid — a synchronous delivery has no queue order to enforce), so no queued delivery order was verified
async schedule steps: not exercised: no async pool scheduling steps were recorded
journal reads: not exercised: no time, randomness or env reads were journaled
A category reported as not exercised was NOT verified by this match.

Understanding Category Coverage

Section titled “Understanding Category Coverage”

Hermetic Transports & Ingress Injection

Section titled “Hermetic Transports & Ingress Injection”

During hale replay, native unix:// and udp:// transport bindings run hermetically:

Backtesting with Feed Mode (--feed)

Section titled “Backtesting with Feed Mode (--feed)”

When you modify handler logic or strategy code and want to test your changes against a captured production traffic tape, use --feed:

Terminal window
hale replay prod-traffic.halerec app.hl --feed

--feed keeps the wire hermetic and injects the recorded bindings ingress tape, but drops replay constraints that only apply to identical code:

Single-Pool Determinism Without Recording

Section titled “Single-Pool Determinism Without Recording”

Any Hale program whose loci run on the default single-threaded main cooperative pool is deterministic by construction: given the same inputs, publishes and deliveries always execute in the exact same order (spec/testing.md § Determinism). For single-pool unit tests, combine @deterministic with standard hale test assertions; reserve LOTUS_OBS_RECORD and hale replay for multi-pool concurrency, async_io schedules, and captured network ingress.