Files
aji/docs/spec/11-determinism-and-replay.md
dev 2bbbd2135c rfc: 0015 scalar atomics and the Aji memory model accepted (revision 2) and integrated into the spec
The Aji RC11 profile (RC11 with NO-THIN-AIR; stronger than ISO C11/C++20) as
normative axioms in spec 04 §20; DRF-SC; linear atomic<T> for AtomicScalar
with atomic.new/atomic.take; ref<atomic<T>> operands; ordering tables incl.
cmpxchg failure table and two results; effects [atomic] / [atomic, atomic.weak];
target atomic_widths and E4203; E4202; enumerator as oracle (observed ⊆ allowed,
exact equality on the normative litmus corpus). Feature atomics stays reserved
until P5-2a. Q-015 resolved; record/replay and Q-017 moved to Phase 8.
RFC 0003 superseded. AIR bundle +1.9 %.
2026-10-06 00:30:35 +03:00

9.5 KiB
Raw Permalink Blame History

11. Determinism, Trace and Replay

1. Interpreter

The reference AIR interpreter defines executable semantic behavior for the language model.

2. Trace

Trace mode may record each AIR instruction and selected abstract-state changes.

Example:

#120 region.create -> r3
#121 region.alloc r3 4096 -> p8
#122 borrow.mut p8 -> b4
#123 store.u32 b4 10
#124 borrow.end b4

3. Deterministic mode

Deterministic execution controls or records sources of nondeterminism including:

  • task scheduling choices;
  • timer progression;
  • clock reads;
  • random inputs;
  • I/O completion ordering;
  • channel wake selection;
  • selected filesystem/network inputs.

4. Replay

A recorded execution should be replayable when program, runtime and replay-format compatibility requirements are met.

aji run --record failure.ajr app.aji
aji replay failure.ajr

In the deterministic profile the run record together with --seed, --schedule-seed and --quantum already reproduces a run exactly, weak-memory choices included (spec 04 §42). A replay file for hosted or native runs would also have to record reads-from choices, modification orders and the SC order; its format (Q-017) is Phase 8 work (RFC 0015).

5. Concurrency exploration

Test mode may systematically or pseudo-randomly vary scheduling at synchronization points.

Candidate scheduling points include:

  • atomic operations;
  • lock/unlock;
  • channel send/receive;
  • task yield/wake;
  • I/O completion;
  • cancellation observation.

Example:

aji test --interleavings=10000

6. Deterministic selection

When select has multiple ready branches, deterministic mode must choose according to a stable rule and record the decision.

Rule (RFC 0012, spec 04 §39): a draw from the schedule generator among the ready arms, counted in the run record's schedule.choices.

Weak-memory choices (which allowed write a non-seq_cst read reads, spec 04 §42) are drawn from the same generator and counted the same way.

7. Execution limits

Wall-clock time MUST NOT determine whether a run, test or verification terminates or what its outcome is.

7.1 Fuel

A runner MAY bound execution with fuel: the number of AIR instructions executed.

  • Every instruction consumes one unit of fuel when it begins execution, including terminators, assert and call.
  • The instructions executed inside a callee consume fuel separately; a call costs one unit regardless of the callee.
  • If no fuel remains when an instruction is about to begin, execution stops before that instruction and the run ends with the outcome limit_exceeded (spec 07a "Run result"), reporting limit and steps (the number of instructions executed, equal to limit).

Fuel exhaustion is a decision of the execution environment, not behavior of the program:

  • it is not a trap and has no T code;
  • a program's semantics are the same with or without a fuel limit, except that a limited run may end early with limit_exceeded;
  • for a given module, inputs and fuel limit, the outcome is deterministic.

The run outcomes are therefore:

not_instantiated  the environment did not start the program (spec 05 §10.2)
returned          the program finished
trapped           the program failed (T0xxx)
limit_exceeded    the environment stopped the program
blocked           the program can make no further progress (§8)

Running without a fuel limit is permitted (for example aji run program.air). Conformance, fuzz and stress runs MUST use a fuel limit.

7.2 Concurrent execution (from Phase 3)

With several tasks, fuel is global: the sum of instructions executed by all tasks (one executed instruction is one fuel unit and one quantum step; spec 04 §36.3). Two further, separate budgets apply to scheduling and MUST NOT be conflated with fuel:

Budget Bounds
fuel how long the program may execute
schedule budget how many interleavings schedule exploration examines
quantum how many instructions a task executes before a forced scheduling point

The scheduler, its choice rule, the schedule seed (independent of the program seed), the task limit and the run-record fields are specified in spec 04 §36; waits, cancellation and select choices in spec 04 §37–§39.

8. Execution profiles

Decision record: RFC 0007.

The run record gains fields for observable output. aji run uses the deterministic profile by default; --profile hosted uses the real host.

Aspect deterministic hosted
stdout / stderr captured; reported as "stdout" / "stderr" strings in the run record, which is printed on stdout the process's real streams; the run record is written to a separate result channel (below)
stdin from --stdin TEXT (empty by default) real stdin
files in-memory file system seeded by --vfs PATH=TEXT; files written during the run are reported as "files" in the run record real file system, limited by capability scopes
network in-process loopback: net.connect to a listening address of the same run queues a connection that net.accept returns; sends are buffered; an operation that would wait for an event that can never occur ends the run with the environment outcome blocked (below) real sockets
clock the number of instructions executed so far (1 ns per instruction) real monotonic clock
random the generator of §8.1, seeded by --seed N (default 0) host CSPRNG

Quiescence. blocked is a property of the whole execution, never of one operation. In the deterministic profile the run ends as blocked exactly when all of the following hold at once:

  • the execution is unfinished (some execution unit has neither returned nor trapped);
  • no execution unit is runnable;
  • the runtime knows no future deterministic event (a pending connection, buffered data, an expiring timer, a completing I/O operation, …) that could make one runnable.

An operation that cannot continue now only suspends its execution unit; while another unit is runnable or a future event is known, the run continues. In Phase 2 there is a single execution unit and no timers, so a wait that nothing can satisfy is immediately global quiescence; Phase 3 extends the set of units and event sources without changing this definition. The run then ends with the environment outcome

{"status":"blocked","kind":"no_future_event","function":"…","block":"…","inst":0}

and aji run exits with code 7. This is not an io_error and not a trap: the program did not ask for a timeout, so none is invented. In the hosted profile the same operation simply blocks. A program that needs a timeout states it explicitly (timers and select, Phase 3). The outcomes are now:

program outcome:      returned, trapped
environment outcome:  not_instantiated, limit_exceeded, blocked

Result channel. In the hosted profile the program owns stdout and stderr, so the run record cannot share them unambiguously. The machine-readable record goes to a separate channel: aji run --profile hosted --result-file PATH (a --result-fd N form may follow; ajid has its own control channel). Without --result-file the CLI may print a human-readable summary on stderr after the program ends; that summary is not a machine interface.

8.0 Host errors in the hosted profile

The hosted profile maps host failures to io_error (spec 06 §7) as follows; anything not listed is other, and no host error number is observable by the program:

Host condition io_error
file or path component not found, not a directory not_found
access or permission denied, read-only file system permission_denied
path already exists (exclusive creation) already_exists
bad or too long path name, path is a directory invalid_input
no space left, disk quota exceeded no_space
broken pipe broken_pipe
connection refused connection_refused
connection reset by peer connection_reset
address in use address_in_use
address not available address_unavailable

Paths outside a capability's scope and malformed paths or addresses are rejected before any host call (permission_denied, invalid_input), exactly as in the deterministic profile. Host handles still open when the execution instance ends are released by the runtime; this is not observable by the program (spec 06 §6, invariant 2).

8.1 Deterministic random generator

The deterministic profile uses xoshiro256** with SplitMix64 seeding. All arithmetic is on unsigned 64-bit integers, wrapping modulo 2^64; rotl(x, k) is (x << k) | (x >> (64 − k)).

Seeding from seed: let x = seed; four times, x = x + 0x9e3779b97f4a7c15, z = x, z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9, z = (z ^ (z >> 27)) * 0x94d049bb133111eb, output z ^ (z >> 31). The four outputs are the state s[0], s[1], s[2], s[3] in that order.

Next output: result = rotl(s[1] * 5, 7) * 9; t = s[1] << 17; s[2] ^= s[0]; s[3] ^= s[1]; s[1] ^= s[2]; s[0] ^= s[3]; s[2] ^= t; s[3] = rotl(s[3], 45); return result.

random.fill of n bytes consumes ceil(n / 8) outputs in order and writes each output's bytes least significant first; the unused bytes of the last output are discarded, so every call starts at a fresh output. The generator state is shared by all random.read capabilities of the run.

Golden vector, seed = 0, successive calls of 1, 7, 8 and 17 bytes:

1:  b4
7:  2a 45 56 49 78 1f 6e
8:  e0 e6 33 49 9d 84 5f 1a
17: 2c 2d 2d 26 f1 94 a5 6a 59 2e 84 1f 4a ad a5 bb ca

A conforming implementation MUST produce exactly these bytes.