Files
aji/docs/dev/architecture.md
T
dev b00bac5937 Phase 2: audit report (P2-10)
- spec 10 §6.1: schema of the format-0 audit report (canonical JSON)
- aji report FILE: entry capabilities, per-function effect graph with
  declared/used/unused effects (fixed point over calls, unsafe stopped
  at trusted callees), intrinsics, allocations, runtime and proven
  checks, unsafe operations; capability kinds; unsafe summary
- golden unit test; runs over the whole conformance corpus without a
  crash
2026-10-04 15:03:44 +03:00

100 lines
4.9 KiB
Markdown

# Reference Implementation Architecture
Non-normative. Describes how the Zig reference implementation is organised. The spec defines behavior; this file defines where code goes.
## Pipeline
```text
.aji source text AIR (.air)
│ │
frontend: lex → parse → lower (P6) │
│ │
└──────────────► AIR module ◄────────┘
│ air: parse / print / (binary, later)
▼
verifier passes in spec 08 order
│
┌───────────────┼───────────────┐
▼ ▼ ▼
interpreter audit reports LLVM backend (P7)
(+ det. scheduler, (unsafe, effects,
trace, replay) capabilities)
```
Nothing executes or lowers unverified safe AIR (spec 07 §7). `check.zig` is the single front door used by the CLI, the conformance runner and later `ajid`.
## Source layout
Current files are marked ✓; the rest is the planned layout. Split a file into a directory when it passes roughly 1 000 lines or gains a second responsibility.
```text
src/
root.zig ✓ library root, re-exports
main.zig ✓ `aji` CLI
check.zig ✓ parse → verify front door
diag.zig ✓ diagnostic envelope, error-code registry
air.zig ✓ re-exports
air/
module.zig ✓ types, module, function, block, instruction, value ids
features.zig ✓ feature registry and validation
parse.zig ✓ text AIR lexer and parser
print.zig ✓ canonical text serializer
binary.zig binary AIR (after opcode freeze, RFC 0004)
verifier.zig ✓ stage pipeline
verifier/
cfg.zig ✓ ssa.zig ✓ types.zig ✓ init.zig ✓ borrow.zig ✓ bounds.zig ✓ budget.zig ✓
ownership.zig region.zig island.zig
resource.zig effects.zig caps.zig
send_share.zig tasks.zig atomics.zig
unsafe.zig
interp.zig ✓ re-exports
interp/
machine.zig ✓ values, frames, frame storage and borrows, dispatch
memory.zig heap, regions, islands, provenance, generations
sched.zig deterministic scheduler, seeds, choice recording
weak.zig weak-memory model for atomics (P5)
trace.zig replay.zig
runtime/ intrinsics: io, fs, net, clock, random (capability-gated)
frontend/ .aji lexer, parser, lowering, formatter (P6)
backend/llvm/ AIR → LLVM IR (P7)
report.zig ✓ audit report (spec 10 §6.1)
ajid/ long-running agent protocol server (P8)
tools/
conformance.zig ✓ conformance runner
tests/
conformance/ ✓ .air accept/reject (and later run) tests
```
## Trusted computing base
From `AGENTS.md`: verifier, AIR interpreter semantics, backend, unsafe runtime modules, ABI/platform descriptions. In this tree that is `air/`, `verifier/`, `interp/`, `runtime/`, `backend/`. The frontend, CLI, `ajid` and report generators are outside the TCB: a frontend bug can produce wrong AIR, but the verifier still rejects unsafe AIR.
Rules for TCB code:
- no heuristics in the verifier: every rejection maps to a spec rule and a registry code;
- no dependency outside the Zig standard library;
- every verifier pass is a pure function of the AIR module and the target description;
- the interpreter never relies on host undefined behavior to implement Aji semantics.
## Determinism rules
These hold for the whole toolchain, not only deterministic mode:
- iteration over hash maps never affects output; sort before emitting;
- diagnostics are emitted in a defined order (by source position, then code);
- no wall-clock time, randomness or environment variables influence verification or diagnostics;
- the interpreter's sources of nondeterminism (scheduling, clock, random, I/O ordering) go through `interp/sched.zig` so they can be seeded and recorded.
## Memory management in the implementation
- Per-compilation arena for AIR and verifier state; free it as a whole.
- `std.testing.allocator` in unit tests to catch leaks.
- No global mutable state; pass allocators and `std.Io` explicitly.
## Zig conventions
- Zig 0.17.0, formatted with `zig fmt`.
- Errors that are Aji diagnostics are values (`diag.Diagnostic`), not Zig errors. Zig errors are reserved for I/O failure and out-of-memory.
- Unit tests live next to the code (`test` blocks); behavior visible to users is tested through `tests/conformance`.
- Doc comments (`//!`, `///`) cite the spec section they implement, e.g. `// spec 03 §8.2`.