0322.
Structured Oracle Diagnostics (tenon-diag-2) &
Sub-Root Constrained Grammars
(tenon grammar --root)
- Status: Accepted (2026-11-28)
- Date: 2026-11-28
- Plan: M30.1 (builds on ADR-0303)
Context
When AI coding agents and local SLMs repair Tenon type errors or synthesize single functions and expressions under constrained decoding, two structural bottlenecks dominate retry latency:
- Unstructured type-error prose: Existing JSON
diagnostics emit
code,severity,message,file,line,col,span, and optionalhelp/fix/edits. To choose a repair for a type mismatch, call-site error, or typoed field/method, an agent has to parse human prose to recoverexpected_type,actual_type, the callee's signature, compatible in-scope variables, or ranked candidate edits (?,.unwrap_or(...),??,ok(...),some(...), or edit-distance-closest field/method names). - File-only constrained decoding grammar:
tenon grammar --surface ordinary(ADR-0303) always emits a whole-file root production (root ::= ws-nl (import-decl nl-sep)* (top-item nl-sep)* top-item? ws-nl). Agents performing targeted function synthesis, single-item generation, or hole/expression infilling cannot constrain decoding to a barefn,item, orexprwithout accepting imports or unrelated top-level declarations.
Decision
Additive Structured Oracle Diagnostic Fields (
tenon-diag-2,crates/tenon-frontend/src/diag.rs):- Extend
DiagnosticInner(boxed inside pointer-sizedDiagnostic) with five optional additive fields, omitted fromrender_jsonwheneverNoneor empty so all existing JSON diagnostic consumers and tests remain 100% backward compatible:expected_type: Option<String>actual_type: Option<String>callee_signature: Option<String>in_scope: Vec<ScopeBinding>({ name, ty }, deterministic sorted order, capped at 12 bindings matchingexpected_type)candidates: Vec<FixCandidate>({ rank, label, edits }, where each edit uses file-relative UTF-8 byte spans)
- Provide fluent builders on
Diagnostic:.expected_type(...),.actual_type(...),.callee_signature(...),.in_scope(...), and.candidate(rank, label, edits). - Wire enrichment into
crates/tenon-compiler/src/types/on the highest-frequency type errors:- Type mismatches (
E0301,E0305,E0810,E0850): attachexpected_type,actual_type, type-matchingin_scopebindings, and ranked repaircandidateswith exactFixEdits:result<T, E>whereTexpected: Rank 1?(when enclosing function returns a matchingresultand not inside a block arg), Rank 2.unwrap_or(<example>).option<T>whereTexpected: Rank 1?(when enclosing function returnsoptionand not inside a block arg), Rank 2?? <example>.Twhereresult<T, E>expected: Rank 1 wrap inok(...).Twhereoption<T>expected: Rank 1 wrap insome(...).
- Function and method call arity/argument errors
(
E0301,E0302): attachcallee_signature(e.g.fn foo(i64, str) -> bool). - Unknown field (
E0404) and unknown method (E0653,E0662,E0803): attach rankedcandidates(Levenshtein distance<= 2, deterministic(distance, name)ordering) with exact replacementFixEdits (plus Rank 1 canonical alias when an unambiguous builtin alias such asvec.append->pushorstr.strip->trimmatches).
- Type mismatches (
- Extend
Sub-Root Constrained Grammars (
tenon grammar --surface ordinary --root file|item|fn|expr):- Add
GrammarRoot(File[default],Item,Fn,Expr) andemit_ordinary_grammar_with_root(format, root)incrates/tenon-frontend/src/grammar.rs(keepingemit_ordinary_grammar(format)as a backward-compatible wrapper forGrammarRoot::File). - Support all four roots in both
gbnf(root ::= ...) andllguidance(start: ...) formats while keeping rule count strictly under 120 and output 100% deterministic:file:ws-nl (import-decl nl-sep)* (top-item nl-sep)* top-item? ws-nlitem:ws-nl top-item ws-nlfn:ws-nl ("pub" req-sp)? fn-decl ws-nlexpr:ws-nl expr ws-nl
- Expose
--root file|item|fn|exprontenon grammar(crates/tenon/src/grammar_cmd.rs), rejecting--rootwhen combined with--actionswithE1180, and including"root"in--jsonoutput for--surface ordinary.
- Add
Consequences
Diagnosticremains pointer-sized (size_of::<Diagnostic>() == size_of::<usize>()), preserving zero heap allocation on the happy path.- Existing JSON diagnostic consumers see no breaking schema changes; oracle fields appear only when populated.
- Constrained decoders (llama.cpp GBNF and llguidance) can now enforce exact syntax for single-function or single-expression synthesis without permitting extraneous imports or declarations.