On this page

ADR-0303: Compiler-Emitted Constrained Decoding Grammars (tenon grammar)

Context

Small and medium local models generating Tenon source code or app_actions JSON calls frequently waste tokens and retries on mechanical syntax hallucinations borrowed from Rust, Python, or C (+=, ++, --, /* ... */ block comments, 'c' char literals, pub import, record field mutation s.x = 2, missing return on -> T functions, -> vs : mismatches) or on calling disabled actions / undeclared JSON argument keys.

Because tenon-frontend is already the single source of truth for ordinary Tenon syntax, emitting constrained-decoding grammars directly from the frontend eliminates grammar drift and lets llama.cpp (GBNF) and llguidance (Lark EBNF) decoding engines structurally prevent invalid tokens before they are ever sampled.

Decision

  1. Frontend Grammar Emitter (crates/tenon-frontend/src/grammar.rs):

    • Pure, zero-dependency, #![forbid(unsafe_code)], 100% deterministic (BTreeMap/BTreeSet everywhere).
    • Supports two output formats: GrammarFormat::Gbnf (--format gbnf, llama.cpp GBNF) and GrammarFormat::Llguidance (--format llguidance, Lark/llguidance EBNF).
    • emit_ordinary_grammar(format): Emits a compact < 120-rule grammar matching tenon-frontend's exact ordinary Tenon syntax (top-of-file imports with aliases, pub restricted to fn/struct/enum/type/interface, struct with @attr, positional enum, type X = new T, interface, impl, fn with explicit-return ret-block on -> T, test, derive, let/var, with { ... }, match, if/else, while, for, ?, closures, vec/string/int/float/bool literals, // comments). Structurally forbids +=, -=, *=, /=, ++, --, /* ... */, 'a', pub import, pub impl, pub test, s.x = 2, implicit-return fn foo() -> T { x }, and -> vs : mismatches.
    • emit_actions_grammar(json_str, format, compact): Accepts either a JSON array of ActionSpecs or a full app_actions snapshot JSON ({"actions":[...]}). Filters strictly to enabled != false (making disabled actions grammatically impossible to emit) and generates exact per-action name + args schema rules enforcing declared keys only, required vs optional key subsets (using O(m) linear chain rules), string/integer/number/boolean/array/object/null types, enum literals, and exact decimal-interval minimum/maximum integer bounds. --compact strips optional whitespace for tight single-line Action Model decoding.
  2. Thin CLI Handler (crates/tenon/src/grammar_cmd.rs):

    • tenon grammar --surface ordinary [--format gbnf|llguidance] [--json]
    • tenon grammar --actions <file|-> [--format gbnf|llguidance] [--compact] [--json]
    • Reserved diagnostics:
      • E1180: invalid CLI flags, unknown format/surface, or conflicting mode flags.
      • E1181: I/O error reading actions input, invalid JSON, or malformed action schema.
      • E1182: no enabled actions available in the input snapshot/array.
  3. In-Tree GBNF Validator:

    • crates/tenon-frontend/src/grammar.rs includes a fast deterministic in-memory GBNF parser and memoized matcher under #[cfg(test)] that verifies rule counts < 120, full acceptance of valid Tenon & ActionCall fixtures, and grammar-level rejection of Rust/Python/C hallucinations, disabled actions, undeclared keys, out-of-bounds integers, and compact-mode whitespace.

Consequences