On this page

0321. Full Capability Trace Record & Deterministic Replay (--record-caps / --replay-caps)

Context

Tenon's capability-gated CLI runtime (Cli, Fs, Env, Clock, Process, Net, Stdin) already supports in-memory virtualization via VirtualCaps (ADR-0296) for deterministic unit testing. However, debugging real-world production or CI failures in effectful CLI programs still requires reproducing the exact external environment — command-line arguments, environment variables, monotonic clock readings, stdin bytes, filesystem snapshots, subprocess outputs, and network responses.

Developers and AI agents need a built-in, zero-dependency record-and-replay workflow in tenon interp so any failing or interesting execution can be captured into a single deterministic JSON artifact (--record-caps <file>) and replayed anywhere offline with zero host OS syscalls (--replay-caps <file>).

Decision

  1. Deterministic Capability Trace Format (tenon-cap-trace-1, crates/tenon-compiler/src/interp/cap_trace.rs):

    • Zero-dependency deterministic JSON serializer (CapTrace::to_json) and strict fail-closed parser (CapTrace::from_json).
    • Schema identifier "schema": "tenon-cap-trace-1". Unknown top-level or entry fields, duplicate keys, malformed hex, unsupported schemas, or trailing tokens fail closed.
    • All arbitrary byte buffers (args, env keys/values, stdin_hex, file data_hex, process stdout_hex/stderr_hex, network response_hex) are encoded as lowercase hexadecimal so arbitrary non-UTF-8 bytes, NUL bytes, quotes, and control characters round-trip 100% losslessly and deterministically.
    • Captures:
      • args: Vec<Vec<u8>>
      • env: BTreeMap<Vec<u8>, Option<Vec<u8>>> (distinguishing present vs absent lookups)
      • clock_ms: Vec<i64> (ordered sequence of observed clock.now_ms() readings)
      • stdin: Vec<u8> (exact bytes read from stdin)
      • fs_reads: BTreeMap<String, RecordedFsEntry> (file, dir, symlink, or error errno snapshots)
      • proc_runs: Vec<RecordedProcRun> (cmd, args, status, stdout, stderr, os_error)
      • net_connects: Vec<RecordedNetConnect> (addr, response, err_code)
  2. Interpreter Recording & Replay Hooks (crates/tenon-compiler/src/interp/ & driver/interp_run.rs):

    • pub enum CapTraceMode { Off, Record(PathBuf), Replay(PathBuf) }.
    • Record mode (--record-caps <file>): Runs normally against live OS capabilities, records every observed args, env, clock.now_ms(), stdin, fs (restrict/open/stat/exists/list), process (run/run_input/stream), and net (connect + read) interaction into CapTrace, and atomically writes the JSON file (temp file + rename) on exit — including non-zero exits and traps so failing runs remain replayable.
    • Replay mode (--replay-caps <file>): Parses <file> fail-closed (E1255 on missing/unreadable/malformed trace), hydrates VirtualCaps with active = true, capture_stdio = false (so program stdout/stderr still flow to host output), and replay_strict = true, seeds reader slot 0 with recorded stdin, and executes main with zero host OS syscalls.
    • Clock readings pop chronologically from clock_queue; exceeding the recorded clock sequence in strict replay mode fails closed with E1256.
    • Process runs and network connects replay chronologically from proc_replay_queue and net_replay_queue so repeated calls to the same command or address with differing outputs reproduce identically.
  3. CLI Interface (crates/tenon/src/cli/compile.rs):

    • tenon interp [--dev] [--fuel N] [--record-caps <file> | --replay-caps <file>] [path]
    • Combining --record-caps and --replay-caps in the same invocation is rejected immediately with diagnostic E1255.
  4. Fail-Closed Diagnostics (E1255–E1256):

    • E1255: Capability trace I/O, schema, syntax, or flag conflict error (--record-caps / --replay-caps).
    • E1256: Strict capability trace replay overflow (e.g. clock.now_ms() invoked more times than recorded).

Consequences