# Phase 5 — Atomics and memory model Non-normative. Part of the [implementation plan](README.md). **Goal:** atomic types with all five orderings and fences, an interpreter that exhibits exactly the outcomes the memory model allows, systematic schedule exploration, and record/replay. **Exit criterion:** every litmus test passes with exact equality between observed and allowed outcomes; atomic-publication conformance tests pass in AIR and `.aji`. **Spec inputs:** spec 04 §20–§30, §42; spec 05 §13; spec 11; `reference/concurrency-litmus.md`. Decision record: [RFC 0015](../rfcs/0015-atomics-memory-model.md) (Accepted, revision 2; supersedes RFC 0003). **Blocking questions:** none. Q-015 was resolved by RFC 0015; Q-017 moved to Phase 8 with record/replay. **Owner decision (2026-10-05, after the Phase 4 exit review).** Phase 5 starts with the RFC. Implementation is on hold until all three of these are done: 1. the RFC is reviewed and accepted; 2. the M/C regression on the frozen Phase 4 commit passes (P5-0); 3. the normative litmus corpus is ready (P5-1a). ```text Phase 4 commit frozen (tag p4-frozen) ├── private M/C regression (P5-0) └── RFC (P5-1) → review / Accepted → litmus corpus (P5-1a) → implementation (P5-2 …) ``` - **Scope.** Scalar atomics and memory ordering only. Hazard pointers, epoch reclamation, RCU, lock-free collections and atomic `iref`s or references are left to a later RFC. Q-023 is not touched before Phase 5. - **No atomics agent benchmark before the RFC.** Without a normative model there is no correct result to measure. The litmus corpus comes first. Each litmus test states its set of allowed outcomes and its set of forbidden outcomes, not a single expected result. - **Model statement.** The Phase 3 sentence "a native execution equals some interleaving of the deterministic scheduler" is not enough for relaxed atomics. Phase 5 replaces it with three layers: - ordinary safe memory: DRF-SC; - atomic memory: the Aji atomic memory model; - unsafe code: its own proof obligations. - **Questions the RFC must close before it can be accepted:** - `sequenced-before`, `synchronizes-with` and `happens-before`; - data race, and the DRF-SC guarantee; - the five orderings, and which are allowed for load, store, RMW and CAS; - `compare_exchange` success and failure orderings; - fences; - mixing atomic and non-atomic access; - whether `atomic.weak` is a separate effect; - which atomic types the format guarantees and which depend on the target; - what counts as observable behavior of the deterministic interpreter; - how the deterministic interleaving model relates to weak memory; - what a native backend must preserve; - the safe/unsafe boundary for atomic pointers and reclamation. ## Invariants - no atomic operation without an explicit, valid ordering; - safe code never accesses atomic storage as ordinary storage; - DRF-SC holds for programs without ordinary-memory races; - the interpreter neither forbids allowed outcomes (SB `r1=0,r2=0`) nor produces forbidden ones (MP). ## Tasks | ID | Status | Task | Depends | Output | Done when | |---|---|---|---|---|---| | P5-0 | done (gate: owner verdict pending) | M/C regression on the frozen Phase 4 commit (tag `p4-frozen`): M, C × Aji/source × N1–N3, 6 cells, same agents, tasks, evaluators and procedure as P6A-9 | — | [`docs/evaluation/results/2026-10-06-p5-0.md`](../evaluation/results/2026-10-06-p5-0.md) | see "Regression gate" below; 6/6 PASS; R3 found frontend defect A (`ref` through `mut`), fixed with a regression test | | P5-1 | done | RFC 0015: scalar atomics and the Aji memory model (supersedes RFC 0003); closes the owner's question list; resolves Q-015; scope of Q-017 | — | `docs/rfcs/0015-…` | Accepted by the owner and integrated into spec 04, 05, 07a, 11, 15 | | P5-1a | todo | Litmus enumerator `tools/litmus` (an executable oracle derived from spec 04 §20, not the definition) and the normative corpus: the classes MP, SB, LB, release/acquire publication, `seq_cst` ordering (IRIW, SB with `seq_cst`), relaxed counter, CAS success/failure, fence publication, forbidden non-atomic race, atomic/non-atomic mixing, plus the handoffs (mutex, channel, spawn/join). Each test has allowed and forbidden outcome sets | P5-1 | `tools/litmus/`, `reference/concurrency-litmus.md`, `tests/litmus/` | the enumerator's allowed set equals every stated set, disagreements diagnosed (spec 04 §20.5); owner review | | P5-2 | done | Contract tables: `atomic.new`, `atomic.take`, `atomic.load`, `atomic.store`, RMW, `atomic.cmpxchg`, `atomic.fence` | P5-1 | spec 04 §42, spec 07a | all spec 07 §4 fields present, with the ordering tables of spec 04 §24 (integrated with RFC 0015) | | P5-2a | todo | Publish feature `atomics` (depends on `effects`, `ownership`) | P5-3 | `reference/air-features.md`, `src/air/features.zig` | feature_not_declared / missing_dependency tests | | P5-3 | todo | AIR parse/print, types (linear `atomic`, AtomicScalar, not IslandSafe), ordering tables, `E4202` | P5-1a | `src/air/`, `src/verifier/` | `E4201` accept/reject for every operation and every cmpxchg success/failure pair; `E4202`; `E1001`, `E2111`, `E0006` cases | | P5-4 | todo | `atomic` / `atomic.weak` effects (spec 05 §13); weak sites in `aji report` | P5-3 | verifier + report | each non-`seq_cst` ordering without `atomic.weak` is `E3101`; `seq_cst` needs only `atomic`; weak sites listed | | P5-5 | todo | Interpreter: atomic cells with message histories, views, weak reads drawn from the schedule stream; atomic operations are scheduling points | P5-3 | `src/interp/` | sound on the corpus and the differential test; no extra draw without weak accesses; existing records unchanged | | P5-6 | todo | Happens-before tracking and dynamic race detector (validates the verifier) | P5-5 | `src/interp/` | any race the verifier missed aborts the test run with a diagnostic | | P5-7 | todo | Exhaustive bounded exploration of schedules and weak-read choices | P5-5 | `src/interp/` | for each litmus test, the outcome set observed equals the oracle's allowed set exactly | | P5-8 | dropped | Record and replay: moved to Phase 8 (P8-10) by RFC 0015 D13 | — | — | — | | P5-9 | todo | Litmus runner and differential test (random small programs; interpreter outcomes ⊆ oracle outcomes) | P5-7 | `tests/litmus/`, `zig build litmus` | all corpus tests pass with exact equality; differential test in CI | | P5-9a | todo | `.aji` layer 10 (spec 15 §12), Quick Reference subsection after mutexes, source coverage rows, bundle measurement | P5-4 | `src/frontend/`, `docs/agent/` | `.aji` run/reject tests for every row; Quick Reference excerpts checked; growth recorded against RFC 0015's estimate | | P5-10 | todo | Phase exit review | all | plan status | exit criterion passes | ## Regression gate (P5-0) These criteria were fixed before the runs. M and C are a regression corpus only. They decide no semantic question, and the language is not tuned to them. - **Baseline.** The P6A-9 Aji/source cells (tag `p6a-candidate`): M 3/3, C 2/3. N3 on C wrote no program. - **Hard gates.** A failure of any of these blocks Phase 5 code: - R1: all four Aji references (M and C × Aji/AIR and Aji/source) still pass their frozen evaluators on `p4-frozen`; - R2: no FAIL is classified `toolchain_bug = yes` or `evaluator_integration_bug = yes`; - R3: the source cells show no IR-form code (`E0001`–`E0006`, `E5001`, `E5002`), no `E7900` and no `E1001`. - **Checked and explained, but not gates on their own:** - R4: a diagnostic code not seen in the P6A-9 source cells (`E7001`, `E7005`, `E7006`, `E7007`); - R5: a cell that passed in P6A-9 and fails now; - R6: the source/AIR ratio of the agents' programs (`mechanical_air_eliminated_raw`, 0.82–0.88 in P6A-9). Each of these is classified: toolchain, evaluator, or model. Only a toolchain or language cause is a regression. - **Reported only.** One run per cell is noise for these: output tokens, uncached input, tool calls, repair cycles, documentation read (the Quick Reference grew by about 640 tokens with §7 Graphs). ## Conformance tests to add Negative corpus entry owned by this phase: invalid atomic ordering. Litmus: MP, SB, mutex handoff, channel handoff, spawn/join, invalid ordinary race.