ADR-0303:
Compiler-Emitted Constrained Decoding Grammars
(tenon grammar)
- Status: Accepted
- Date: 2026-11-28
- Wave / Idea: Wave 2 · Rank 18 · Idea #48
- Diagnostic Codes:
E1180–E1182(reservedE1180–E1184)
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
Frontend Grammar Emitter (
crates/tenon-frontend/src/grammar.rs):- Pure, zero-dependency,
#![forbid(unsafe_code)], 100% deterministic (BTreeMap/BTreeSeteverywhere). - Supports two output formats:
GrammarFormat::Gbnf(--format gbnf, llama.cpp GBNF) andGrammarFormat::Llguidance(--format llguidance, Lark/llguidance EBNF). emit_ordinary_grammar(format): Emits a compact< 120-rule grammar matchingtenon-frontend's exact ordinary Tenon syntax (top-of-file imports with aliases,pubrestricted tofn/struct/enum/type/interface,structwith@attr, positionalenum,type X = new T,interface,impl,fnwith explicit-returnret-blockon-> 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-returnfn foo() -> T { x }, and->vs:mismatches.emit_actions_grammar(json_str, format, compact): Accepts either a JSON array ofActionSpecs or a fullapp_actionssnapshot JSON ({"actions":[...]}). Filters strictly toenabled != false(making disabled actions grammatically impossible to emit) and generates exact per-actionname+argsschema rules enforcing declared keys only, required vs optional key subsets (using O(m) linear chain rules),string/integer/number/boolean/array/object/nulltypes,enumliterals, and exact decimal-intervalminimum/maximuminteger bounds.--compactstrips optional whitespace for tight single-line Action Model decoding.
- Pure, zero-dependency,
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.
In-Tree GBNF Validator:
crates/tenon-frontend/src/grammar.rsincludes a fast deterministic in-memory GBNF parser and memoized matcher under#[cfg(test)]that verifies rule counts< 120, full acceptance of valid Tenon &ActionCallfixtures, and grammar-level rejection of Rust/Python/C hallucinations, disabled actions, undeclared keys, out-of-bounds integers, and compact-mode whitespace.
Consequences
- Local coding models and Action Models can decode 100%
syntactically valid ordinary Tenon and state-conditioned
ActionCallJSON on the first pass with zero grammar-drift maintenance. tenon-frontendremains pure and zero-dependency;tenon grammaradds only a thin CLI wrapper.