On this page

0322. Structured Oracle Diagnostics (tenon-diag-2) & Sub-Root Constrained Grammars (tenon grammar --root)

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:

  1. Unstructured type-error prose: Existing JSON diagnostics emit code, severity, message, file, line, col, span, and optional help/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 recover expected_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).
  2. 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 bare fn, item, or expr without accepting imports or unrelated top-level declarations.

Decision

  1. Additive Structured Oracle Diagnostic Fields (tenon-diag-2, crates/tenon-frontend/src/diag.rs):

    • Extend DiagnosticInner (boxed inside pointer-sized Diagnostic) with five optional additive fields, omitted from render_json whenever None or 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 matching expected_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): attach expected_type, actual_type, type-matching in_scope bindings, and ranked repair candidates with exact FixEdits:
        • result<T, E> where T expected: Rank 1 ? (when enclosing function returns a matching result and not inside a block arg), Rank 2 .unwrap_or(<example>).
        • option<T> where T expected: Rank 1 ? (when enclosing function returns option and not inside a block arg), Rank 2 ?? <example>.
        • T where result<T, E> expected: Rank 1 wrap in ok(...).
        • T where option<T> expected: Rank 1 wrap in some(...).
      • Function and method call arity/argument errors (E0301, E0302): attach callee_signature (e.g. fn foo(i64, str) -> bool).
      • Unknown field (E0404) and unknown method (E0653, E0662, E0803): attach ranked candidates (Levenshtein distance <= 2, deterministic (distance, name) ordering) with exact replacement FixEdits (plus Rank 1 canonical alias when an unambiguous builtin alias such as vec.append -> push or str.strip -> trim matches).
  2. Sub-Root Constrained Grammars (tenon grammar --surface ordinary --root file|item|fn|expr):

    • Add GrammarRoot (File [default], Item, Fn, Expr) and emit_ordinary_grammar_with_root(format, root) in crates/tenon-frontend/src/grammar.rs (keeping emit_ordinary_grammar(format) as a backward-compatible wrapper for GrammarRoot::File).
    • Support all four roots in both gbnf (root ::= ...) and llguidance (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-nl
      • item: ws-nl top-item ws-nl
      • fn: ws-nl ("pub" req-sp)? fn-decl ws-nl
      • expr: ws-nl expr ws-nl
    • Expose --root file|item|fn|expr on tenon grammar (crates/tenon/src/grammar_cmd.rs), rejecting --root when combined with --actions with E1180, and including "root" in --json output for --surface ordinary.

Consequences