Files
aji/docs/plan/phase-6-source-language.md
T
dev 154c39865f Phase 6A: .aji layer 8 (lock and mutexes, select, shields, cancellation)
- lock ref m as x { … } cancelled { … }: mutex.lock on a ref<mutex<T>>
  (a mutex is borrowed for the lock, a ref binding used as is); ok edge:
  mutex.borrow bound as x; at every normal exit borrow.end x then
  mutex.unlock; the guard is never a source value
- mutex.make/mutex.take; mutex<T> type; ref mr = m lends a mutex
- shield { … }: task.shield.begin/end at every exit (nesting by construction)
- task.check_cancel and task.join_cancel as outcome matches
- select: one borrow per endpoint, send values evaluated first and
  deduplicated, every edge binds its own value then the unsent values;
  bind count E7005, missing outcome E7004
- linear values never get frame storage (borrowed in place)
- liveness: select handled in the read and after walks
- Quick Reference: select edges bind `job` back (ok(x, job), closed(job));
  every Quick Reference example now passes (no PENDING)
- tests: 6 run tests with AIR equivalence (mutex counter, select draw,
  select same value, shield, join_cancel forwarding, cancelled recv), 10
  rejects (E4304 x3, E3001, E2101, E7005 x2, E7004, E3101 x2, E4101)
- spec 15 §7/§9, quick reference, bundle notes, plan
2026-10-05 18:02:11 +03:00

11 KiB
Raw Blame History

Phase 6 — Source language

Non-normative. Part of the implementation plan.

Goal: .aji source that lowers deterministically and explicitly to AIR, with diagnostics mapped back to source spans.

Phase 6A (brought forward, owner decision 2026-10-05). First a minimal frontend for the frozen semantics of Phases 0–3, with no new semantics. Its principle: .aji removes mechanical work but never hides a semantically significant action.

Removed SSA values, block parameters, the hand-written CFG, the features [...] header (inferred), intermediate values, boilerplate
Kept explicit move, borrow and mutable borrow, allocation, resource ownership, capabilities, effects, task spawn and join, channel ownership transfer, locking, cancellation, fallible I/O

Targets and the evaluation order are in README.md. Phase 6A is specified by RFC 0013. Generics, contracts, modules and the formatter (P6-4, P6-6, P6-8) stay for the full Phase 6.

Exit criterion: all reference programs and examples/ are authored primarily as .aji and pass the conformance corpus through the frontend.

Spec inputs: spec 01, 02, 03 §4, 06 §3–4, 13 §1–2; reference/grammar.ebnf.

Rule: a source feature is acceptable only if its lowering to AIR is deterministic, explicit and mechanically verifiable (spec 01 §8). The frontend is outside the TCB; the verifier still checks its output.

Phase 6A tasks (RFC 0013, spec 15)

ID Status Task Depends Output Done when
P6A-1 done Spec 15, grammar, E7xxx, E3101 suggested_effects, E0006 canonical_header, Quick Reference — spec 15, reference/grammar.ebnf, docs/agent/quick-reference.md RFC 0013 accepted
P6A-2 done Lexer and parser with spans (full 6A grammar; one precise E7001, no recovery) P6A-1 src/frontend/{lex,parse}.zig every spec 15 example parses; syntax errors are E7001
P6A-3 doing Names and source types: variables, functions, types, built-in operations, outcome arms P6A-2 src/frontend/check.zig E7002–E7007 accept/reject tests
P6A-4 doing Lowering: control flow, variables → SSA/block parameters, frame storage, lexical borrow ends, outcome matches, block forms (scope, shield, lock), select, byte literals, feature closure P6A-3 src/frontend/lower.zig lowered AIR verifies; lowering is a pure function
P6A-5 done Source spans on lowered AIR; verifier diagnostics and trap records carry source_location and air_location P6A-4 src/frontend/spans.zig, src/diag.zig every diagnostic of a .aji file points at source
P6A-6 done CLI: aji check/run/lower file.aji P6A-4 src/main.zig same flags and run record as for .air
P6A-7 todo .aji twins for the Phase 0–3 conformance tests (same outcome or code) and the reference programs P6A-5 tests/conformance/*/source/ twins pass; the reference programs in .aji pass --stress
P6A-8 done Source Reference Bundle (directory, QUICK.md front door, AIR reference only under air/), harness profile aji_source with per-group documentation metrics, Quick Reference examples checked against conformance programs, raw and normalized size metrics P6A-6 docs/agent/source-bundle.txt, tools/eval/languages/aji_source.json bundle measured; every Quick Reference example is an excerpt of a checked program
P6A-9 todo Dev regression on tasks M and C: the frozen Aji/AIR runs vs new Aji/source runs, same agents (all three native agents) P6A-8 results note criteria below reported
P6A-10 todo Phase 6A exit review all plan status targets reported
P6A-12 todo Source type-check completeness: errors fully determined by the source type system are reported by the frontend as E7xxx at the source construct, never left to the verifier on the lowered AIR (checkpoint 2: E1001 ×4 in a source run). Not a layer 7 blocker; to be closed before the Phase 6A freeze P6A-4 src/frontend/lower.zig for each verifier type rule reachable from source, a source test expecting the E7xxx
P6A-11 todo Malformed generated AIR is a compiler bug: when a .aji module passed the frontend but its AIR fails to parse or is structurally invalid, report an internal compiler diagnostic (with the source construct), not the AIR parse error mapped back (found by the M checkpoint: void fall-through lowered to ret %) P6A-4 src/check.zig, error registry a test that injects malformed lowering gets the internal diagnostic

P6A-9 criteria (owner decision, fixed before the runs)

Compared per agent with the frozen Aji/AIR run of the same task:

Measure Criterion
IR-form diagnostics (E0001–E0006, E5001, E5002; ajieval profile) decrease ≥ 80 %
agent output tokens decrease ≥ 50 %
tool calls decrease materially
Aji semantic guarantees unchanged (same verifier, same evaluator gates)
VSR no regression
uncached provider input (strong agents) substantial decrease
weakest agent (GLM) an earlier Aji/AIR failure becomes a PASS, or repair cycles drop sharply

Also reported:

  • wall time;
  • unique_docs_read and reading per group (quick_reference, source_docs, air_docs);
  • diagnostic classes;
  • source bytes, lowered AIR bytes (raw and normalized);
  • type mismatches (E1001 + E7005), counted apart from the IR-form classes so that relabeling cannot improve them.

Order (owner decision, after P6A-8):

  1. M checkpoint (diagnostic, not P6A-9). Task M only, Aji/source, the three native agents, run from the frozen commit tagged p6a-m-checkpoint. The Quick Reference and the Source Reference Bundle stay unchanged until this run is done, so the comparison stays clean. Bundle hash at the tag: sha256 24cca814c7be1826ea797a1cc9759d0e0dd996bc6561f7b46f0ad1f4e77a2843, over sha256sum of the files of zig-out/agent/source/, sorted.
  2. Compare with the frozen Aji/AIR runs and classify the effect. Nothing changes automatically on the strength of M.
  3. Task C waits for layers 7–8.
  4. Layer 7, then layer 8; the concurrency programs of the Quick Reference (tests/conformance/source/quickref/) leave PENDING and pass.
  5. Freeze one Phase 6A commit and run M + C × 3 agents: this is the official P6A-9.
  6. Phase 6A review (P6A-10); only then any decision about further frontend changes.

Type mismatches (E1001, E7005) stay a separate category from the IR-form diagnostics.

Status (2026-10-06). Steps 1–2 are done in two checkpoints (results). Checkpoint 1 found two toolchain defects, fixed in 222c0c8. Checkpoint 2 gave AIR 2/3 and source 3/3, with IR-form diagnostics at 0 in source. The owner closed the IR-tax diagnosis at the engineering level: layer 7 GO. Remaining order:

  1. layer 7 (tasks, channels);
  2. layer 8 (lock, select, cancellation);
  3. every concurrency example of the Quick Reference passes;
  4. Phase 6A candidate freeze;
  5. official P6A-9: M + C × 3 agents.

The cost hypothesis stays open: it needs repetitions.

Lowering layers (P6A-3/4), each with accepted (golden AIR), rejected (E7xxx) and verifier-caught tests in tests/conformance/source/:

Layer Content Status
1 scalars, expressions, functions, control flow, variables carried across joins, short-circuit and/or, casts done
2 structs (named construction, declaration-order lowering), enums (Enum::Variant), exhaustive match over enums (no _) and integers (_ required), match as an expression; outcome matches and or trap move to layer 5 with the fallible operations done
3–4 addressable values (frame storage chosen by lowering), lexical borrows (ref r = …;/mut r = …;), places and writes, arrays, slices, split_at, slice.copy, byte strings, proven indexing for the verifier's fact form; move comes with linear values in layer 5 done
5 explicit move for linear values (E7003 with the move x hint), own/own_array, regions with an explicit region borrow, outcome matches (match kind decided by the parser), or trap, built-in error enums, cap<mem.alloc> and cap.narrow.budget; linear aggregates stay rejected (E0006); aji lower --stats reports mechanical_air_eliminated done
6 intrinsics as plain calls (outcome match / or trap), resources by name with a per-operation borrow, explicit closes and data borrows, cap.narrow.prefix, byte-string paths, unsafe operations under effects [unsafe], trusted rejected, preserved parameter names; source/AIR run-record equivalence (equivalent:) for hello, file copy, TCP echo, prefix narrowing, random done
7 scope blocks, task.spawn/join/cancel/cancelled, task<T>/sender<T>/receiver<T>, channels (make, dup, send, recv, close_sender, close_receiver), endpoint operands by name, rebinding of variables on outcome edges, joins carry live variables only; source/AIR equivalence with the Phase 3 task and channel programs done
8 lock blocks over mutex<T> (guard never a source value; unlock at every exit), mutex.make/take, select (one borrow per endpoint, unsent values bound back on every edge), shield, task.check_cancel, task.join_cancel; linear values never in frame storage; source/AIR equivalence with the Phase 3 mutex, select and cancellation programs; every Quick Reference example passes done

Tasks (full Phase 6)

ID Status Task Depends Output Done when
P6-1 todo Complete the source grammar: expressions, match, borrow blocks, task.scope, select, defer, ?, generics, lifetimes, contracts, attributes — (may start after P2) reference/grammar.ebnf, spec 01 every construct in examples/ is covered; grammar is LL(k) or otherwise documented as unambiguous
P6-2 todo Lexer and parser with spans P6-1 src/frontend/{lex,parse}.zig all examples/ parse; syntax errors are registered diagnostics
P6-3 todo Modules, imports, name resolution P6-2 src/frontend/resolve.zig unresolved names produce registered diagnostics
P6-4 todo Type checking and monomorphized generics with structural constraints (Copy, Send, Share, Freeze, Local) P6-3 src/frontend/types.zig generic code instantiates deterministically; constraint violations diagnosed
P6-5 todo Lowering to AIR: control flow, ?, defer cleanup edges, borrow blocks, task scopes P6-4 src/frontend/lower.zig lowered AIR verifies; lowering is a pure function (same input ⇒ same AIR bytes)
P6-6 todo Contracts: requires/ensures/invariants → contract table; status classification P6-5 frontend + verifier each contract reported as PROVEN_STATICALLY / CHECKED_AT_RUNTIME / ASSUMED_UNSAFE / UNPROVEN
P6-7 todo Source spans in diagnostics for verifier errors on lowered AIR P6-5 src/diag.zig, lowering every conformance reject test has a source-level twin with the same code
P6-8 todo Formatter P6-2 aji fmt idempotent; output parses to the same AST
P6-9 todo Port reference programs and examples to .aji P6-5 tests/programs/, examples/ exit criterion passes
P6-10 todo Phase exit review all plan status exit criterion passes