# 10. Diagnostics and Agent Protocol ## 1. Goal Aji diagnostics are an API for automated repair, not only human-readable messages. ## 2. Stable codes Every verifier/compiler error has: - stable code; - stable machine kind; - structured fields; - optional human explanation; - optional non-binding repair candidates. Code-specific `reason` values MUST be registered and stable; consumers MUST NOT derive them from human messages. E7006 requires the categories in spec 15 §10 and `reference/error-codes.md`. Diagnostic JSON objects MUST have unique member names. Code-specific fields MUST NOT shadow envelope names (`code`, `kind`, `severity`, `file`, `line`, `col`, `message`, `repair_candidates`, `related_locations`). E3202 for an unsupported narrowing form and E7002 for an unknown capability kind use `capability_kind`; E2102 for unlock with a live guard borrow identifies `guard` and `borrow` separately. These fields change no rejection or primary-position rule. `related_locations` MAY provide ordered, non-binding factual context; the covered forms below MUST provide exactly one record for the already selected conflict. Other forms MUST NOT invent a value or task. | Diagnostic field | Related `role` | Related `value` | Position | |---|---|---|---| | E2101/E2104 `conflicts_with` | `conflicting_borrow` | same as `conflicts_with` | that value's definition | | E4302 `task` | `task_spawn` | same as `task` | the task's creating spawn, retained through cancelled-join handle replacement | A record MUST serialize `role`, `value`, then `file`, `line`, `col` when located (the primary position's conventions), then `air_line`, `air_col` for lowered AIR (spec 15 §10). `related_locations` MUST be omitted when empty and otherwise appended after existing envelope fields. Roles and identities MUST be factual; positions identify syntax, not execution order or a repair. Primary positions/fields, first-conflict selection, diagnostic order and duplicate suppression MUST remain unchanged; discarded duplicates MUST NOT contribute related records. No borrow, loan, acceptance rule or runtime behavior changes. Example: ```json { "code": "E2104", "kind": "memory.region_generation", "function": "decode", "value": "%17", "region": "%3", "expected_generation": 5, "found_generation": 4, "reset_instruction": 84, "use_instruction": 91 } ``` A rejected source module MAY produce multiple diagnostic JSON lines under spec 15 §10. Each line retains the same envelope; consumers MUST read the complete diagnostic stream rather than treating stdout as one JSON object. ## 3. Repair candidates The compiler MAY emit structured suggestions. The following kinds are illustrative: ```json { "repair_candidates": [ {"kind": "move_reset_after_use"}, {"kind": "copy_value_before_reset"} ] } ``` Suggestions never modify semantics automatically. Each `repair_candidates` element MUST have string `kind`; it MAY additionally have string `hint` and a nonempty ordered array of strings `alternatives`. Serialization order MUST be `kind`, then `hint`, then `alternatives`; absent optional members MUST be omitted. Existing kind-only objects remain valid. Alternatives are spellings for the construct/unit registered for that kind, not byte edits or a claim that the repaired program verifies. Their order MUST be deterministic and implies no preference. In particular, choosing a checked versus wrapping cast remains the agent's semantic decision. Agents MUST select advice by `kind`, not by parsing `hint` or `message`. Source advice kinds, fixed hints, applicability and alternative units are registered in `reference/error-codes.md`. An E7001/E7005 diagnostic MUST NOT carry more than one registered source advice candidate; when a registered trigger applies, it MUST carry exactly that candidate. Advice MUST come from the rejecting parser/type rule, MUST NOT change accepted syntax or inference, and MUST preserve the existing code, location, payload and message. Imported source and formatter syntax failures MUST carry the same applicable advice; E7900 replacement MUST discard it. ## 4. ajid `ajid` is the long-running machine interface. Initial methods: ```text parse typecheck verify compile run test trace inspect explain benchmark ``` ## 5. Expected repair loop ```text agent patch ↓ ajid.verify ↓ structured errors ↓ agent repair ↓ ajid.test ↓ schedule exploration ↓ benchmark ``` ## 6. Verification report The toolchain should emit an audit artifact containing: - unsafe sites; - remaining runtime checks; - eliminated bounds checks; - ownership/borrow proof summaries; - capability requirements; - effect graph; - weak atomic sites; - lock-order findings; - contract status. ### 6.1 Audit report, format 0 `aji report FILE` verifies the module and, if it is accepted, prints one canonical JSON document (spec 07a "Canonical record": UTF-8, no whitespace, fixed field order). A rejected module produces its diagnostics and no report. The report is derived from the verified AIR only; it never depends on a run. ```json { "report": "audit", "air": 0, "module": "", "features": ["…"], "entry": {"function": "main", "capabilities": [{"parameter": "out", "kind": "io.write"}]}, "functions": [ { "name": "", "trusted": null, "effects_declared": ["…"], "effects_used": ["…"], "effects_unused": ["…"], "calls": ["", "…"], "intrinsics": ["", "…"], "allocations": 0, "runtime_checks": {"overflow": 0, "division": 0, "shift": 0, "cast": 0, "bounds": 0, "assert": 0, "guard": 0}, "proven_checks": {"bounds": 0}, "unsafe_operations": [{"block": "entry", "inst": 0, "op": "borrow.index.unchecked"}], "atomic_operations": [{"block": "entry", "inst": 3, "op": "atomic.load", "ordering": "acquire", "weak": true}] } ], "capability_kinds": ["…"], "unsafe": {"trusted_functions": ["…"], "unsafe_functions": ["…"], "operations": 0} } ``` Fields: - `entry`: `@main` and its capability parameters in order, or `null` if there is no `@main`. - `effects_used`: the effects the function can actually perform — its intrinsics' effects, `mem.alloc` if it allocates, `unsafe` if it contains unsafe operations, and the `effects_used` of its callees — computed to a fixed point over the call graph. A `trusted` function does not propagate `unsafe` to its callers (spec 13 §6.3); its own `effects_used` does contain `unsafe`, which it discharges. `effects_unused` is `effects_declared` minus `effects_used`: declared authority the code never exercises, a candidate for removal. - `calls`, `intrinsics`: distinct callees and intrinsic names in order of first occurrence; together with `effects_*` this is the effect graph. - `runtime_checks`: instructions whose safety condition is checked at runtime and may trap — `overflow` (checked `add`/`sub`/`mul`, and `div`/`rem` for signed `MIN / -1`), `division` (`div`/`rem` by zero), `shift` (`shl`/`shr`), `cast` (`cast.checked`), `bounds` (`borrow.index`, `slice.sub`, `slice.split`, `slice.copy`), `assert`, `guard`. - `proven_checks.bounds`: `borrow.index.proven` instructions, whose check the verifier eliminated. - `atomic_operations` (feature `atomics`, spec 04 §25): every atomic memory operation in order. `ordering` is the operation's ordering, or `success.failure` for `atomic.cmpxchg`. `weak` is true for a non-`seq_cst` access: a site where interleaving reasoning is not enough. `effects_used` contains `atomic` for every such operation and `atomic.weak` for every weak one. - `capability_kinds`: distinct capability kinds appearing in any type of the module, sorted. - All lists of names are in order of first occurrence in the module unless stated otherwise; effect lists are sorted. Ownership/borrow proof summaries, lock-order findings and contract status are added to this document by the phases that introduce them, as new fields; existing fields keep their meaning.