Files
aji/docs/dev/architecture.md

6.5 KiB
Raw Permalink Blame History

Reference Implementation Architecture

Non-normative. Describes how the Zig reference implementation is organised. The spec defines behavior; this file defines where code goes.

Pipeline

          .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.

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.

Structural queries share a per-module table over resolved nominal declarations. A reverse-dependency worklist computes the existing boolean predicates, saturated traversal height and nearest forbidden source-element depth; named queries then read that immutable table. Existential properties start false, universal properties true, height increases and witness distance decreases to their existing depth limits. Cycles therefore terminate without a traversal timeout; existing layout/type checks still decide rejection. The verifier rebuilds the table rather than trusting one supplied by a caller. Source lowering builds it after finite-layout checking, preserves diagnostic order and uses the same declaration snapshot. The witness search retains field order and its depth-32 boundary, pruning subtrees with no reachable witness. Tests count evaluations and compare the cached predicates against uncached derivations on small acyclic modules. This changes implementation cost, not AIR meaning, ownership classes or runtime behavior.

The budget stage separately memoizes accounted sizes by nominal index and depth. Including depth preserves the previous cutoff and overflow diagnostics exactly, with at most V × (V + 2) nominal states; shared paths no longer expand exponentially. Verification returns a module carrying read-only caches for subsequent report/interpreter queries. Replacing a module's declarations invalidates its caches. Standalone uncached property/size helpers retain their recursive derivations for small differential tests.

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.