Semantic Systems / Language / Glyphs

Grammar-Constrained Decoding and Deterministic Local Tools for TinyRustLM

Report summary

The uploaded research brief supplies a specific implementation topic despite the surrounding request describing the topic as unspecified. This report therefore treats grammar-constrained browser-local generation and deterministic local tools for TinyRustLM as the governing research subject. It assum

Status
Research archive item
Category
Semantic Systems / Language / Glyphs
Length
9,057 words
Reading time
42 minutes
Report type
evaluation

Key topics

  • Semantic Systems / Language / Glyphs
  • Semantic Systems
  • Language
  • Glyphs
  • AI
  • SQL
  • Python
  • Runtime
  • Rust

Research provenance

Archive status
Research archive item
Content identity
sha256:c478130087aab1673f8bd5610dd97c0bf525ae14b6fcbe587d35d377016e4b5b

For citation, use the report title and canonical URL. Archival presence does not establish authorship or promote report statements into portfolio evidence.

Source availability: 50 citation markers in the source export have no recoverable source links. Those markers are omitted from this reader; any supplied bibliography and ordinary links remain. Check the original sources before relying on the cited claims.

This page renders the archived Markdown as safe, formatted HTML. It is background research and does not become a portfolio claim without evidence review.

Full report

On this page

Executive summary and assumptions

The uploaded research brief supplies a specific implementation topic despite the surrounding request describing the topic as unspecified. This report therefore treats grammar-constrained browser-local generation and deterministic local tools for TinyRustLM as the governing research subject. It assumes no access to TinyRustLM source code, model files, tokenizer fixtures, private schemas, tests, traces, or generated outputs, and it does not imply that any recommendation has been validated against the private implementation.

Executive recommendation. TinyRustLM should adopt one clean first-release architecture:

  1. A bounded deterministic EBNF subset as the canonical grammar language.
  2. A restricted JSON Schema adapter that lowers into the same internal representation.
  3. A byte-oriented predictive parser-state machine with bounded stack, rather than a character-only parser or unrestricted general-purpose regex engine.
  4. An exact token-ID-to-byte mapping and a shared tokenizer byte trie for incremental allowed-token discovery.
  5. A strict separation between syntactic admissibility, schema validity, semantic validation, deterministic computation, and tool authority.
  6. A fixed-precision decimal calculator and, initially, no other automatically executed tool except narrow read-only deterministic transforms.
  7. A versioned, hash-bound local tool envelope in which tool names map only to compiled application capabilities.
  8. No output repair, hidden retry-until-valid behavior, model-produced code execution, user-controlled file paths, or model-selected network destinations.
  9. Raw token decisions, allowed-token counts, stop reasons, constraint identities, and tool provenance retained for authorized local auditing.

Constrained decoding can guarantee membership in the implemented formal language, but not factual truth, semantic correctness, safety, or faithful fulfillment of user intent. The distinction is not merely theoretical: structured-generation implementations themselves warn that valid formatting does not imply semantic correctness and that aggressive masking can worsen content quality.

The proposed internal design deliberately sacrifices some grammar expressiveness for predictable compilation, deterministic rejection, small native Rust implementation size, and resistance to state-explosion attacks. Recent systems such as XGrammar, XGrammar 2, Outlines, LM Format Enforcer, and llguidance demonstrate that trie indexing, state caching, specialized JSON handling, and parser-aware token masks can make constrained generation practical. They also illustrate why TinyRustLM should adopt the underlying ideas rather than import a broad, dynamically extensible grammar ecosystem into a security-sensitive, zero- or low-dependency browser core.

Priority conclusion. The first three projects should be developed as a single gated program:

PriorityProjectPrimary outcomeIndicative durationMedium estimate
1Byte-accurate constrained-decoding coreDeterministic bounded parser and mask semantics14 weeks$240,000
2Tokenizer-byte conformance and trie engineCorrect multi-byte and cross-token behavior10 weeks, partly parallel$180,000
3Bounded JSON profile and semantic-validation layerSchema-valid JSON without conflating syntax and meaning12 weeks, partly parallel$220,000
4Deterministic calculator and local tool protocolExact local arithmetic and capability-bound calls10 weeks$175,000
5Capability and prompt-injection security evaluationConfused-deputy and data/control-plane protections8 weeks$150,000
6Browser performance and memory studyProven bounds across representative devices8 weeks$130,000
7End-to-end quality regression studyEvidence that constraints help rather than merely parse10 weeks$165,000
8Privacy-safe observability and replayAuditable failures without public data leakage6 weeks$105,000

The medium estimates are planning figures, not vendor quotes. They assume loaded engineering costs around $18,000–$22,000 per full-time-equivalent month, part-time security and quality review, browser/device testing, and approximately 15% contingency.

Assumptions.

AssumptionConsequence
The attached TinyRustLM brief supersedes the generic “unspecified topic” placeholder.The portfolio is concentrated in local AI reliability, security, and formal-language engineering rather than distributed across unrelated health, environmental, and economic subjects.
Model logits are locally accessible before sampling.Token masks can be applied without server calls or output repair.
The tokenizer can expose, or can be augmented to expose, exact generated byte sequences.Constrained mode is feasible; otherwise it must fail closed.
TinyRustLM can run a small Rust or WebAssembly core in a dedicated worker.Compilation and mask computation can be isolated from rendering and user-interface state.
The first release does not need backward compatibility with unpublished grammar formats.Only one canonical grammar and one tool protocol should survive promotion.
No network tools, arbitrary file paths, credentials, or general code execution are required initially.The capability boundary can remain small enough to audit rigorously.
Budgets are expressed in 2026 US dollars and include labor, testing, and contingency but not model-training infrastructure.Estimates are comparable but require recalibration against internal staffing rates.
All public sources in the bibliography were retrieved on August 1, 2026.Repository behavior should be pinned by release or commit before implementation begins.

Evidence base, claim taxonomy, and first-release scope

The evidence base combines recent primary research, official specifications, and pinned public implementations. Grammar-constrained decoding has shown benefits in information extraction, disambiguation, parsing, and structured generation, while JSONSchemaBench provides approximately 9,558 curated real-world schemas for evaluating efficiency, feature coverage, and output quality. Recent work also reports substantial gains from vocabulary indexing, context-independent token classification, just-in-time mask compilation, cross-grammar caching, and repetition compression. Those findings motivate the architecture but do not establish that the same performance or quality will hold for TinyRustLM’s unknown model and tokenizer.

Claim vocabulary.

ClaimPermitted meaningEvidence required before TinyRustLM may use the claimWhat it does not establish
Syntactically constrained textEvery completed output byte belongs to the named compiled grammar language.Grammar hash, compiler version, accepting parser state, exact generated tokens/bytes, no repair, and an independent parser test.Truth, usefulness, safety, or intent compliance.
Schema-valid JSONOutput parses as JSON and satisfies every feature in TinyRustLM’s declared JSON profile.Successful UTF-8 validation, duplicate-key rejection, profile validator success, schema hash, and supported-feature manifest.Full JSON Schema 2020-12 support or semantic correctness.
Semantically validated extractionValues pass application rules and are tied to evidence in the source.Valid source spans or evidence references, transformation records, unit checks, enum checks, and cross-field validation.That the source itself is true or authoritative.
Deterministic arithmeticThe same canonical expression, tool version, limits, and rounding configuration produce the same canonical result or error.Test vectors, checked arithmetic, fixed rounding rules, implementation hash, and replay tests across supported browsers.Correct interpretation of an ambiguous natural-language question.
Declared local tool invocationA model-proposed request was validated and dispatched to a compiled local capability under recorded authority.Tool/version and schema hashes, authorization state, request ID, input validation, output provenance, and no dynamic code or URL dispatch.That execution was appropriate, desired, or harmless outside the declared capability.
Code-formatted outputOutput conforms to a narrow formatting grammar, such as a fenced code block or a supported language fragment.Named grammar and accepting state.Compilation, functional correctness, safety, or absence of vulnerabilities.
Unconstrained conversationNormal sampling occurred without a formal output-language guarantee.Sampling configuration and stop reason.Any structural or semantic guarantee.
Auditable failureThe raw generation or tool attempt ended with an explicit machine-readable failure.Raw token/byte prefix where authorized, failure code, limits, identities, and stop reason.Successful task completion.

The practical consequence is that user-interface labels must say “JSON complete and profile-valid”, “semantic validation failed”, or “generation cancelled with partial raw output”, rather than the ambiguous “validated” or “correct.”

Recommended first-release scope.

IncludeExclude or defer
Unconstrained conversation, summaries, rewriting, and explanationsGrammar constraints on ordinary prose by default
Exact choices and small enum classificationsArbitrary user-provided regex dialects
Bounded deterministic grammar modeGeneral ambiguous context-free grammars
Restricted JSON object/array schemasFull JSON Schema 2020-12
Required and optional properties in declared orderArbitrary object-property permutations
Bounded arrays, strings, nesting, and enumsUnbounded repetition or recursive schemas
Integer and finite decimal fields with explicit rangesNaN, infinities, arbitrary precision JSON numbers
Source-bound extraction validationSilent correction of unsupported values
Fixed-precision calculatorTrigonometry, symbolic algebra, arbitrary plugins
One or two compiled read-only local transformsNetwork, credentials, arbitrary file access, shell, JavaScript, SQL, or Rust execution
Explicitly confirmed bounded local state changes, after read-only capabilities are provenAutomatic side effects
Private local traces with user-controlled retentionPublic logging of prompts, outputs, arguments, paths, or credentials

JSON itself requires UTF-8 for interoperable exchange, recommends unique object names, prohibits leading zeroes in numbers, and permits lexical numeric magnitudes that may exceed interoperable binary64 limits. RFC 8259 specifically notes exact interoperability for integers only through the binary64-safe range of ±(2^53−1). TinyRustLM should therefore implement a deliberately stricter profile, not claim unrestricted JSON or unrestricted JSON Schema support.

JSON Schema Draft 2020-12 includes dynamic references, unevaluated-item semantics, Unicode-aware regular expressions, conditional composition, and other features whose complete interaction is disproportionate to a bounded first-release engine. The official language-agnostic test suite exists precisely because seemingly simple validators differ in edge cases. TinyRustLM should publish a supported-keyword matrix and reject every unknown keyword rather than ignore it.

Prioritized research portfolio

The following eight proposals form a coherent implementation and evaluation program. Priority scores are an internal decision model combining prerequisite value, expected risk reduction, user impact, feasibility, and reversibility. They are not empirical performance results.

xychart-beta
    title "Research portfolio priority score"
    x-axis ["Byte core","Tokenizer","JSON","Calculator","Security","Browser perf","Quality","Observability"]
    y-axis "Score" 0 --> 100
    bar [96,93,88,80,78,74,71,66]
RankResearch question or projectRationale and key literatureMethods and data sourcesTimelinePotential impact
1Can a bounded byte-level predictive parser provide complete prefix-safe token masks with deterministic compilation and failure behavior?This is the foundational safety boundary. Geng et al. demonstrate the general utility of grammars; Outlines frames generation as FSM transitions; XGrammar shows efficient CFG execution; GRAMMAR-LLM emphasizes transformation toward deterministic parsing.Implement a tiny EBNF parser, AST validator, nullable/FIRST/FOLLOW analysis, left-recursion and ambiguity rejection, byte-terminal automata, bounded parser stack, state accounting, and exhaustive tiny-language tests. Data: generated grammars, hand-written adversarial grammars, JSON lexical fragments, and differential tests against independent recognizers.14 weeksEstablishes the trustworthy core needed by every other structured-output feature.
2How can TinyRustLM map tokenizer IDs to exact bytes and mask tokens that cross terminal, escape, and Unicode boundaries?Tokenizers may contain leading-space conventions, byte fallback, partial UTF-8, or context-dependent cleanup. LM Format Enforcer uses a tokenizer prefix tree; XGrammar separates context-independent from context-dependent tokens; llguidance reports optimized mask computation over large vocabularies.Build exact byte fixtures for every token; classify special tokens; probe round-tripping and contextual decoding; construct a compact trie; compare full-vocabulary scan, trie traversal, prefix memoization, and parser-state caches. Data: the actual private tokenizer when authorized, plus public byte-BPE and SentencePiece fixtures for pre-integration development.10 weeksPrevents subtle false allows, false rejects, mojibake, invalid UTF-8, and unreplayable behavior.
3What bounded JSON Schema profile gives useful structured output without importing unbounded specification complexity?JSONSchemaBench shows wide feature diversity and varying engine coverage. RFC 8259, JSON Schema 2020-12, the official test suite, and JCS establish relevant syntax, validation, interoperability, and canonical identity constraints.Define a strict profile; compile supported schemas to parser states; run applicable official tests; classify all JSONSchemaBench schemas as supported, safely rejected, or outside scope; create extraction validators with evidence binding.12 weeksEnables reliable JSON and extraction while preserving honest claims about semantic correctness.
4Can a fixed-precision decimal calculator and versioned local tool envelope provide useful reasoning assistance without arbitrary execution?Rust supplies checked i128 operations; ECMAScript Numbers are binary64 and cannot represent all large integers or decimal results exactly; RFC 8785 recommends strings for values outside natural JSON/binary64 representation.Implement a Pratt or shunting-yard parser, checked fixed-decimal arithmetic, canonical errors, operation limits, schema-hashed envelopes, replay vectors, and one read-only transform.10 weeksImproves arithmetic reliability and establishes the pattern for all future tools.
5Does the proposed capability model resist prompt injection and confused-deputy attacks across every untrusted field?NIST’s Generative AI Profile calls for lifecycle risk management and structured evaluation. Browser permission specifications represent access to powerful features as user-granted states, supporting an explicit-authority design.Threat modeling, data/control-plane taint tracking, adversarial tool calls, injection in tool names/arguments/results/documents, stale-authority tests, and user studies of confirmation prompts.8 weeksReduces the risk that syntactically valid output is mistaken for authority.
6What parser, trie, and cache bounds remain responsive on realistic browser devices?XGrammar, XGrammar 2, and llguidance report low overhead through token classification, JIT compilation, caching, and specialized paths, but their serving environments differ from a browser-local small model.Benchmark cold compile, warm compile, mask latency distributions, peak memory, cancellation latency, and cache hit rates on low-, middle-, and high-tier devices. Use Web Workers to isolate computation; workers have independent event loops and no browsing context.8 weeksDetermines whether the design is product-viable and where specialized fast paths are justified.
7Do constraints and deterministic tools improve end-to-end correctness without harming helpfulness, ambiguity handling, or refusal quality?Existing literature reports structural and task gains, but constrained decoders can force low-probability continuations and may cause semantic regressions. JSONSchemaBench explicitly evaluates efficiency, coverage, and quality rather than syntax alone.Sealed unseen tasks across extraction, rewriting, classification, JSON, arithmetic, code explanation, and ambiguity handling. Compare unconstrained, syntax-constrained, syntax-plus-semantic-validation, and tool-assisted arms.10 weeksPrevents shipping a parser-perfect but less helpful model experience.
8What privacy-safe trace is sufficient for deterministic replay and public evidence without exposing user content?NIST emphasizes documentation, measurement, and lifecycle evaluation. JCS demonstrates why canonical representations and stable identities matter when hashing structured records.Define local trace schemas, retention controls, redaction tests, replay from token IDs and RNG state, public aggregate reports, and deletion verification.6 weeksMakes failures diagnosable while preserving the browser-local privacy promise.

Portfolio comparison.

ProjectDependency criticalityTechnical uncertaintySecurity valueDirect user valueMain evidence artifact
Byte coreVery highHighHighHighFormal contract, parser implementation, exhaustive corpus
Tokenizer/trieVery highVery high until tokenizer accessHighHighToken-byte manifest and boundary test suite
JSON/semanticsHighMediumHighVery highProfile specification and conformance report
Calculator/toolsMediumMediumVery highHighDeterministic vectors and tool protocol
Injection/authorityMediumHighVery highMediumThreat model and red-team results
Browser performanceMediumMediumMediumHighDevice benchmark report
Quality studyMediumHighMediumVery highSealed comparative evaluation
Observability/replayMediumLow to mediumHighMediumPrivacy-reviewed trace schema

Canonical architecture and implementation contract

Recommended architecture. The public contract should consist of a bounded EBNF grammar plus a restricted JSON Schema profile, both lowering into one immutable compiled constraint. Internally, TinyRustLM should use deterministic parser-state transitions over bytes, with bounded pushdown state for nesting and byte-DFA states for terminals. Grammars that are ambiguous under the implementation’s predictive analysis should be rejected, not interpreted through undocumented tie-breaking.

flowchart LR
    A[Untrusted grammar or schema bytes] --> B[Bounded UTF-8 parser]
    B --> C[AST and feature validation]
    C --> D[Nullable FIRST FOLLOW analysis]
    D --> E{Deterministic and within limits?}
    E -- No --> F[Explicit compile failure]
    E -- Yes --> G[Byte terminal automata]
    G --> H[Bounded parser-state machine]
    H --> I[Canonical encoding and SHA-256 identity]
    I --> J[Atomic publication]
    J --> K[Tokenizer byte-trie product]
    K --> L[Allowed token mask]
    L --> M[Sampling]
    M --> N[Raw token and stop trace]

Formalism comparison.

FormalismExpressivenessIncremental mask suitabilityCompilation and memoryDenial-of-service concernsTinyRustLM disposition
Safe regex subsetRegular languagesExcellent after DFA compilationPredictable if operators and repetitions are boundedState explosion from alternation/repetition; catastrophic behavior if delegated to a backtracking enginePermit only as syntax sugar lowered to the deterministic grammar core
General regexOften includes backreferences and implementation-specific features beyond regular languagesPoorly portableUnpredictableReDoS, engine divergence, huge state spacesProhibit
EBNFHuman-readable context-free notationGood when restricted to deterministic formsModerateAmbiguity, nullable cycles, left recursion, unbounded repetitionCanonical external grammar with strict bounded subset
GBNF-style grammarPractical and familiar in local-model ecosystemsGood but dialect semantics varyModerateCompatibility burden and ambiguous feature expectationsDo not preserve a legacy dialect; borrow only clearly specified constructs
DFARegular onlyExcellent O(1) state transitionPotentially large but easy to boundExponential determinizationUse for byte terminals and regular fragments
Visibly pushdown automatonHandles nesting where stack action is symbol-visible, including JSON-like delimitersExcellent for suitable languagesPredictableLess general than arbitrary grammar; schema compilation complexity remainsUseful conceptual model and possible JSON fast path, not sole public formalism
General parser-state machineBroad CFG supportPossible, as XGrammar and XGrammar 2 illustratePotentially expensive; ambiguous-state sets may growGrammar attacks and unpredictable latencyRestrict to deterministic predictive states for version one
JSON Schema subsetHigh application valueGood after specialized compilationFeature-dependentReferences, unions, regex, recursion, and unevaluated semantics can explodeSupport a declared, fail-closed profile
Application typed schemasNarrow but directly tied to capabilitiesExcellentLowest riskSchema drift and versioningPreferred interface for local tools

Research systems support broader grammars than the proposed TinyRustLM subset. XGrammar uses optimized context-free execution; XGrammar 2 adds Earley-based masking, dynamic dispatch, JIT compilation, and cross-grammar caching; llguidance supports a large JSON Schema subset and CFGs. Those are useful upper-bound references, but TinyRustLM’s zero-dependency and adversarial-input constraints justify a narrower deterministic core.

Canonical grammar input contract: trg-1.

ElementNormative recommendation
EnvelopeA canonical JSON object with format: "trg-1", start, rules, and limits. The envelope is data only and never interpolated into Rust, JavaScript, shell, SQL, prompts, URLs, or paths.
EncodingUTF-8 only; byte-order mark rejected; malformed UTF-8 rejected at the first offending byte.
Maximum source size65,536 bytes.
Rule countAt most 512.
Identifier syntaxASCII [A-Za-z_][A-Za-z0-9_]{0,63}.
NamesExactly one definition per name; case-sensitive; duplicates rejected.
Start symbolExactly one declared existing rule.
OperatorsSequence, ordered-independent choice that must pass disjoint-FIRST analysis, grouping, optional, and bounded repetition {m,n}.
Repetition0 ≤ m ≤ n ≤ 256; no bare * or +. Specialized JSON strings may use a separately declared byte/scalar bound up to 4,096.
TerminalsUTF-8 string literals, bounded ASCII byte classes, and a small set of compiler-provided primitives such as JSON_STRING_CHAR and UTF8_SCALAR.
RecursionAllowed only if non-left-recursive, non-nullable, and within static and runtime depth limits.
Grammar-reference depthMaximum 32 during analysis.
Runtime stack depthMaximum 64 frames.
AST nodesMaximum 8,192.
Literal bytesMaximum 32,768 aggregate and 1,024 per literal.
AlternationsMaximum 64 branches per choice and 4,096 total branches.
CommentsNone in version one. Removing comments avoids encoding and canonicalization differences.
Unknown syntax/featuresHard error with byte offset and stable error code.
Nullable cyclesRejected.
Left recursionDirect and indirect left recursion rejected.
AmbiguityAny predictive-table conflict or overlapping terminal decision rejected.
Canonical orderingRules sorted by UTF-8 byte order for identity calculation; original ordering retained only for diagnostics.
IdentitySHA-256 over the canonical envelope, compiler semantic version, and profile version.
PublicationCompilation is atomic: no partially usable constraint enters the cache.

Safe compilation pipeline.

  1. Enforce input byte and UTF-8 limits before allocation proportional to declarations.
  2. Parse into an arena-indexed AST with checked integer arithmetic.
  3. Resolve names and reject duplicates, missing references, reserved names, and unreachable rules if strict mode is enabled.
  4. Compute nullable sets to a fixed point, charging every update against a cycle budget.
  5. Detect nullable cycles and direct or indirect left recursion.
  6. Compute FIRST and FOLLOW sets and reject predictive conflicts.
  7. Expand bounded repetition symbolically; do not duplicate subtrees naively.
  8. Compile string literals and character primitives into byte-level automata.
  9. Construct parser-state templates and intern structurally identical states.
  10. Enforce state, transition, stack, and memory ceilings after every expansion phase.
  11. Serialize the compiled artifact deterministically, hash it, and run internal self-checks.
  12. Publish the artifact with a single atomic pointer swap only after all checks pass.

Recommended initial ceilings are 65,536 parser or terminal states, 1,048,576 transitions, 32 MiB compiled memory per constraint, 64 MiB total constraint cache, and 50 million charged compilation operations. These are engineering starting points requiring browser measurement, not externally sourced constants.

A simple upper-bound accounting model is:

\[ M_{\text{compiled}} \le H + S \cdot b_s + T \cdot b_t + B_{\text{terminal}} + B_{\text{diagnostic}} \]

where \(S\) is state count, \(T\) is transition count, \(b_s\) and \(b_t\) are implementation-specific fixed record sizes, and all additions and multiplications use checked arithmetic. Compilation must abort before an allocation if the projected bound exceeds the configured budget.

Token, byte, and Unicode reconciliation. The grammar consumes bytes, while global output validity is tracked by an incremental UTF-8 validator. A token is not treated as a Unicode character or ordinary string. Each vocabulary entry must have a verified exact byte sequence corresponding to generation, with the following classifications:

Token classConstrained-mode treatment
Ordinary token with stable bytesInsert into the tokenizer trie.
Token ending in a partial UTF-8 sequencePermitted if the parser and UTF-8 state can accept the prefix; continuation must arrive in later tokens.
Byte-fallback tokenTreat as its exact byte value.
Leading-space convention tokenMap to the actual emitted space byte plus remaining bytes, not the visual marker used in tokenizer files.
EOSNever inserted as ordinary bytes; allowed only when the parser is accepting and UTF-8 state is complete.
BOS, padding, control, role, or other special tokenForbidden during constrained content generation unless the protocol explicitly reserves it as a stop token.
Context-dependent or cleanup-dependent decode tokenConstrained mode disabled until a stable raw-byte path is locally verified.
Token with malformed or unknowable mappingPermanently forbidden for that tokenizer/constraint pair and recorded in diagnostics.

UTF-8 validation must reject overlong encodings, surrogate-code-point encodings, invalid continuation bytes, and values above U+10FFFF. Unicode explicitly treats non-shortest and irregular sequences as illegal because alternate interpretations create interoperability and security problems.

Allowed-token algorithm.

allowed_tokens(compiled, parser_state, utf8_state, token_trie):
    key = (
        compiled.hash,
        tokenizer.hash,
        compiler.version,
        parser_state.id,
        utf8_state,
        special_token_policy.version
    )

    if mask_cache.contains(key):
        return mask_cache[key]

    allowed = empty_bitset(vocabulary_size)
    work = stack()
    work.push(token_trie.root, parser_state, utf8_state)

    while work not empty:
        cancellation_checkpoint()

        trie_node, pstate, ustate = work.pop()

        for edge_byte, child in trie_node.children:
            next_utf8 = utf8_step(ustate, edge_byte)
            if next_utf8 is INVALID:
                continue

            next_parser = parser_step(pstate, edge_byte, next_utf8)
            if next_parser is DEAD:
                continue

            work.push(child, next_parser, next_utf8)

            for token_id in child.terminal_token_ids:
                if token_is_complete_and_legal(token_id, next_parser, next_utf8):
                    allowed.set(token_id)

    if parser_state.accepting and utf8_state.complete:
        allowed.set(EOS)

    mask_cache.insert_if_within_budget(key, allowed)
    return allowed

The naive vocabulary scan costs approximately

\[ O\left(V\bar{L}C_p\right) \]

per generation step, where \(V\) is vocabulary size, \(\bar L\) average token-byte length, and \(C_p\) parser transition cost. Trie traversal costs

\[ O\left(E_{\text{visited}}C_p + A\right), \]

where \(E_{\text{visited}}\) is the number of trie edges compatible with the current parser prefix and \(A\) is the number of allowed terminal token IDs. In the worst case the trie still approaches the total vocabulary byte count, but shared prefixes avoid repeated parser work. LM Format Enforcer publicly describes this parser/trie intersection approach, while XGrammar and Outlines describe vocabulary indexes and cached state/token mappings.

Cache identity must include tokenizer bytes, special-token policy, grammar hash, compiler version, parser state, UTF-8 state, and any JSON-profile mode. A cache entry from one tokenizer or compiler must never be reused by another merely because token IDs or grammar text appear similar.

Sampling order and edge cases.

The normative order should be:

  1. Read raw model logits.
  2. Convert every NaN logit to forbidden and increment an audit counter; retain positive or negative infinity only according to a declared policy, preferably failing if positive infinity appears.
  3. Apply the grammar-allowed mask.
  4. Apply independently forbidden special-token and policy masks.
  5. Apply repetition or frequency penalties only to remaining tokens.
  6. Apply temperature.
  7. Apply minimum-probability filtering, if supported.
  8. Apply top-k.
  9. Apply top-p.
  10. Normalize probabilities.
  11. Draw once from the recorded RNG state.
  12. Advance parser, UTF-8, repetition, and trace state atomically.

An empty allowed set produces constraint_empty_set, never repair or automatic retry. A set containing only EOS terminates without an RNG draw. At temperature zero, select the maximum logit and break exact ties by smallest token ID. EOS is impossible before an accepting parser state. If all surviving logits are non-finite or all probability mass is removed, stop with an explicit sampling failure.

Bitwise deterministic replay should be claimed only for the same model build, tokenizer, compiled constraint, numerical kernel, sampling implementation, and stored RNG state—or when the original post-model logits are retained. Cross-device floating-point execution may otherwise differ at close probability boundaries.

Streaming, stopping, and cancellation. Raw token IDs and bytes are the authoritative stream. UI text may display only complete UTF-8 scalars; trailing incomplete byte sequences remain buffered. A partial JSON document may be shown as “structured output in progress” but must not be deserialized into application objects or displayed as completed JSON. Cancellation returns a status record with raw partial bytes or text where locally authorized, parser state, and completed: false. It never appends brackets, closes strings, inserts missing fields, or retries.

Bounded JSON profile: trj-1.

FeatureSupported behavior
RootObject or array; tool arguments require an object.
EncodingValid UTF-8 only, no BOM.
NestingMaximum 32 containers.
Object orderSchema declaration order only.
Required fieldsSupported.
Optional fieldsSupported only in declaration order; once a later field is emitted, an earlier omitted field cannot appear.
Duplicate keysImpossible under compiled schema and rejected by independent validation.
Additional propertiesfalse only in version one; missing keyword interpreted as rejection during schema compilation, not silently as JSON Schema’s broader default.
StringsStandard JSON escapes; valid Unicode scalar values; no lone surrogate escapes.
String limitsminLength and maxLength, maximum 4,096 scalar values and 16,384 UTF-8 bytes.
NumbersRFC 8259 lexical grammar; no leading zero; no NaN or infinities.
IntegersSigned 64-bit by default, optionally narrower explicit ranges. Larger integers represented as strings.
Decimal numbersFinite binary64-compatible JSON numbers for interchange; exact decimal tool values returned as strings.
Booleans and nullSupported.
EnumsExact strings, booleans, null, or bounded numeric literals.
ArraysHomogeneous items, minItems, and maxItems; maxItems ≤ 256.
TuplesprefixItems supported up to 32 positions, with no trailing items unless explicitly bounded.
Local referencesAcyclic local $defs references only.
UnionsDiscriminated object union only, using one required constant tag field.
Canonical schema identityJCS-like canonical encoding and SHA-256; canonicalization is for identity, not post-generation repair.
Unknown keywordsCompile-time rejection.

RFC 8259 allows duplicate object names syntactically but warns that receiving implementations behave unpredictably, while JCS requires unique property names and valid Unicode for stable canonicalization. JCS also rejects NaN and infinity and recommends string representations for values that do not fit natural binary64 interchange.

Deferred JSON Schema features should include remote $ref, recursive and dynamic references, pattern, patternProperties, unevaluatedProperties, unevaluatedItems, general oneOf/anyOf, not, if/then/else, contains, dependentSchemas, complex multipleOf, content vocabularies, and assertion-level format. Deferral is not a statement that these features are undesirable; it reflects their interaction complexity and state-growth risks.

Syntax-to-semantics boundary. An extraction result should use an application schema similar to:

{
  "status": "supported | unknown | unsupported | conflict",
  "value": "schema-typed value or null",
  "evidence": [
    {
      "source_id": "local-content-hash",
      "start_byte": 120,
      "end_byte": 145,
      "quoted_sha256": "…",
      "transform": "exact | trim | casefold | parse_integer | parse_decimal"
    }
  ],
  "unit": "declared-unit-or-null",
  "validation": {
    "enum_member": true,
    "range_ok": true,
    "cross_field_ok": true
  }
}

Only listed transformations are permitted. A value absent from the source must become unknown; a present but schema-inexpressible value must become unsupported; inconsistent evidence must become conflict. Unit conversions require an explicit deterministic conversion rule and must preserve the original evidence value. Semantic failure remains visible and does not trigger model rewriting.

Deterministic calculator. The calculator should avoid binary floating-point as its normative arithmetic type. ECMAScript’s default Number is IEEE 754 binary64, and its JSON serialization maps NaN and infinities to null, behavior unsuitable for an exact calculator boundary. Rust’s i128 exposes checked addition, multiplication, division, power, and related operations suitable for a bounded native decimal representation.

Recommended calculator contract:

PropertySpecification
Input grammarASCII digits, ., operators + - * / % ^, parentheses, commas, and allowlisted function names.
LocaleAlways . decimal point; no grouping separators.
Numeric representationSigned i128 coefficient plus scale 0…18.
Literal digitsMaximum 38 significant decimal digits, subject to i128 range.
Addition/subtractionAlign scales with checked powers of ten; error on overflow.
MultiplicationChecked coefficient multiplication; scale sum must be ≤18 unless explicit rounding is requested.
DivisionCaller-independent default output scale of 18; round half to even; divide-by-zero error.
ExponentiationInteger exponents only, −18…18; negative exponents use bounded division.
ModuloInteger operands only.
Functionsabs, min, max, round, floor, ceil; no trig, logarithms, randomness, dates, or user-defined functions.
Expression sizeMaximum 4,096 bytes and 512 lexical tokens.
AST depthMaximum 64.
Operation budget10,000 charged primitive operations.
UnitsDeferred; version one is dimensionless.
OutputDecimal string, scale, rounding mode, exactness flag, and implementation version.
ErrorsStable codes such as syntax, overflow, division_by_zero, inexact_disallowed, limit, and unsupported_function.

Example result:

{
  "protocol": "tinycalc-1",
  "status": "ok",
  "expression_sha256": "…",
  "value": "0.333333333333333333",
  "scale": 18,
  "rounding": "half_even",
  "exact": false,
  "operations": 1,
  "implementation": "tinycalc-rust-1.0.0"
}

Local tool envelope.

{
  "protocol": "tinyrust-tool-1",
  "request_id": "019…",
  "composition": {
    "model_id": "sha256:…",
    "tokenizer_id": "sha256:…",
    "prompt_policy_id": "sha256:…"
  },
  "tool": {
    "name": "calculator.evaluate",
    "version": "1.0.0",
    "capability": "deterministic.compute"
  },
  "schemas": {
    "input_sha256": "…",
    "output_sha256": "…"
  },
  "authorization": {
    "class": "automatic_read_only",
    "user_gesture_id": null,
    "granted": true
  },
  "limits": {
    "input_bytes": 4096,
    "output_bytes": 8192,
    "operations": 10000,
    "timeout_ms": 100
  },
  "input": {
    "expression": "1 / 3"
  }
}

The tool name is matched against a compiled enum. It never names Rust functions, JavaScript functions, executable files, paths, URLs, modules, or network endpoints. The request ID may use a standards-conforming UUID, but uniqueness is bookkeeping rather than authority. RFC 9562 is the current UUID standard.

erDiagram
    MODEL_COMPOSITION ||--o{ TOOL_REQUEST : proposes
    TOOL_DEFINITION ||--o{ TOOL_REQUEST : constrains
    INPUT_SCHEMA ||--|| TOOL_DEFINITION : validates
    OUTPUT_SCHEMA ||--|| TOOL_DEFINITION : validates
    AUTHORIZATION ||--|| TOOL_REQUEST : permits
    TOOL_REQUEST ||--o| TOOL_RESULT : produces
    TOOL_RESULT ||--|| PROVENANCE : records
    TOOL_RESULT ||--o{ REINSERTION_RECORD : reinserts_as_untrusted_data

    MODEL_COMPOSITION {
        string model_hash
        string tokenizer_hash
        string policy_hash
    }
    TOOL_DEFINITION {
        string name
        string version
        string capability
        string implementation_hash
    }
    AUTHORIZATION {
        string class
        bool granted
        string user_gesture_id
    }
    TOOL_REQUEST {
        string request_id
        string input_schema_hash
        int max_input_bytes
        int timeout_ms
    }
    TOOL_RESULT {
        string status
        string output_schema_hash
        bool truncated
        string result_hash
    }
    PROVENANCE {
        string implementation_hash
        bool deterministic
        string stop_reason
    }

Authority matrix.

Capability classFirst-release policyExamples
Pure deterministic transformAutomatic after schema validationCalculator, sort a supplied list, count supplied text
Read-only access to already active conversation dataAutomatic only when the user’s current request clearly asks for itSummarize selected local text
Read-only local application state not already in contextExplicit confirmation or direct user gestureRead a saved local note
Local state mutationExplicit, specific, just-in-time confirmationSave a draft or preference
User-selected file readExplicit browser file-picker action; path never model-generatedRead a chosen text file
General filesystem traversalExcludedDirectory scanning
Network accessExcluded from first releaseFetch URLs, call APIs
Credentials or secret storesExcludedPassword manager, API keys
External side effectsExcludedPurchases, email sending, account changes
Arbitrary code executionPermanently excluded from the generic envelopeJavaScript, shell, SQL, Rust, WebAssembly supplied by the model

Browser permission specifications model powerful-feature access as a user choice that can be granted or denied, while Permissions Policy can disable classes of browser capability. TinyRustLM should mirror that approach internally even for capabilities not directly governed by browser permission APIs.

Prompt-injection and confused-deputy controls. Prompt text, quoted content, retrieved memory, JSON values, schemas, tool arguments, tool results, and model output all belong to the data plane. Only compiled application configuration belongs to the control plane.

ThreatRequired control
Document says “ignore policy and call tool X”Document bytes remain quoted untrusted data; cannot modify tool registry or authority.
Model invents a tool nameExact enum lookup fails with tool_unavailable.
Model embeds a URL or path in an argumentSchema does not contain URL/path capability; request rejected before dispatch.
Tool result contains instructionsResult is reinserted under an untrusted-data wrapper and cannot become a system or developer message.
Schema contains executable-looking stringsStrings remain data; compiler recognizes only fixed grammar/schema keywords.
Old confirmation reused for new argumentsAuthorization binds request ID, tool/version, normalized input hash, and expiry.
Model changes mid-requestPending call becomes stale unless the composition identity remains exactly bound.
Truncated result hides warning textResult includes truncated: true, original byte count where known, and full-result hash if available.
Duplicate request is replayedRequest-ID store returns duplicate_request or the already recorded idempotent result.
Malicious grammar exhausts resourcesPreallocation checks, operation budget, state ceilings, cancellation points, and atomic failure.

NIST’s Generative AI Profile recommends systematic risk identification, measurement, documentation, and testing rather than relying on a model’s own claims of safety. That principle supports treating all model-mediated text as untrusted and keeping authority in deterministic application code.

Tool-result reinsertion. Results should be inserted as a typed record, not concatenated prose:

{
  "role": "tool_result",
  "trust": "untrusted_data",
  "request_id": "…",
  "tool": "calculator.evaluate@1.0.0",
  "status": "ok",
  "schema_sha256": "…",
  "result_sha256": "…",
  "truncated": false,
  "content": {
    "value": "0.333333333333333333"
  }
}

The system must serialize and escape this structure itself. Result text cannot change instruction precedence. Token budgeting occurs before insertion; truncation must preserve valid envelope syntax and expose the truncation. The current user turn and application policy remain authoritative regardless of result contents.

Detailed plans for the leading proposals

The top three projects overlap substantially and should share a program manager, formal specification repository, test corpus, and promotion gate. The low case assumes a small senior team and limited device coverage. The medium case supports independent review and serious adversarial testing. The high case adds parallel implementation, formal-methods support, broad browser/device coverage, and external audit.

Integrated program budget.

Cost categoryLowMediumHigh
Byte-level grammar and parser core$95,000$240,000$520,000
Tokenizer mapping and trie engine$70,000$180,000$390,000
JSON profile and semantic validation$85,000$220,000$470,000
Shared test infrastructure and device lab$25,000$70,000$180,000
Independent security/formal review$15,000$80,000$220,000
Contingency and integration$30,000$95,000$230,000
Integrated total$320,000$885,000$2,010,000

Some labor is double-counted if the projects are priced separately; the integrated total reflects shared staffing and infrastructure.

Byte-accurate constrained-decoding core.

Plan elementDetail
Research objectiveProve, through construction and testing, that every allowed next token preserves the existence of at least one valid completion under the supported deterministic grammar.
Required expertiseFormal languages, parser construction, Rust systems programming, property-based testing, integer/memory safety, and model sampling integration.
Core outputstrg-1 specification; parser and AST; static analyses; compiled-state format; mask/sampling interface; error taxonomy; exhaustive tiny-grammar oracle; fuzz corpus.
Low staffingOne principal Rust/parser engineer, one part-time test engineer.
Medium staffingOne formal-language lead, two Rust engineers, one test/verification engineer, part-time security review.
High staffingTwo independent implementations or one implementation plus mechanized model, dedicated fuzzing specialist, external audit, and broad browser integration.
Low/medium/high$95,000 / $240,000 / $520,000
Duration14 weeks nominal; 18–20 weeks with independent implementation.

Milestones:

WeeksMilestoneExit evidence
1–2Freeze grammar syntax, limits, error codes, and claim languageReviewed specification; all unspecified behavior converted to rejection or an explicit rule
3–4Build bounded parser and arena ASTGolden parse tests, byte offsets, malformed UTF-8 tests, allocation-bound tests
5–6Implement reference resolution, nullable, FIRST/FOLLOW, cycle, left-recursion, and ambiguity checksAdversarial grammar corpus and deterministic diagnostics
7–8Compile byte terminals and predictive parser statesIndependent recognizer differential tests
9–10Implement prefix viability, EOS rules, and parser-state cloningExhaustive enumeration over tiny alphabets and vocabularies
11–12Integrate mask/sampling order, cancellation, and raw traceReplay tests and explicit empty-set failures
13–14Fuzzing, browser worker integration, resource tuning, and gate reviewNo crashes, hangs, silent repair, or limit bypass in sealed corpus

Primary risks and mitigations:

RiskMitigation
Deterministic subset proves too restrictiveMeasure rejection against real target schemas before expanding; add only one feature at a time with a bounded compilation proof.
FIRST/FOLLOW analysis misses byte-terminal overlapPerform overlap analysis on compiled byte automata, not merely source-level character labels.
State interning produces identity bugsCanonical immutable state records, content hashes in debug builds, and independent slow reference interpreter.
Resource ceilings reject legitimate inputsPublish limits and collect rejection telemetry locally; increase limits only after device measurements.
Parser accepts a prefix with no valid completionExhaustive small-language tests and backward viability checks for bounded constructs.
Optimization alters semanticsMaintain an unoptimized reference path and differential-test every optimized mask.

Tokenizer-byte conformance and trie engine.

Plan elementDetail
Research objectiveEstablish a complete, auditable mapping from token IDs to generated bytes and prove correct mask behavior across token boundaries.
Required expertiseTokenizer internals, Unicode/UTF-8, Rust compact data structures, cache design, browser profiling, and sampling integration.
Core outputsTokenizer identity manifest; exact token-byte table; special-token policy; trie builder; partial UTF-8 state machine; mask cache; tokenizer conformance report.
Low staffingOne tokenizer/systems engineer plus part-time test engineer.
Medium staffingOne tokenizer lead, one Rust performance engineer, one verification engineer.
High staffingIndependent decoder probes, multiple tokenizer families, broad device benchmarking, and external Unicode review.
Low/medium/high$70,000 / $180,000 / $390,000
Duration10 weeks, beginning once the parser transition interface is stable.

Milestones:

WeeksMilestoneExit evidence
1–2Obtain authorized tokenizer fixture and define byte identity procedureHashable vocabulary manifest and classified special tokens
3Run single-token and contextual decode probesStable/unstable token report; constrained mode fails closed for unstable mappings
4Implement strict incremental UTF-8 state machineUnicode conformance vectors, overlong and surrogate rejection
5–6Build compact byte trie and reference vocabulary scannerExact equality of allowed sets on synthetic cases
7Add parser/trie product traversal and cancellationBoundary-spanning token corpus passes
8Add bounded caches and cache-identity testsNo cross-tokenizer or cross-grammar contamination
9–10Performance study and integration hardeningp50/p95/p99 mask latency, memory, and cancellation report

Primary risks and mitigations:

RiskMitigation
Tokenizer exposes only cleaned textAdd a raw-byte adapter or disable constrained mode; do not infer bytes from display decoding.
Tokens split UTF-8 code pointsTrack partial sequences as first-class state rather than rejecting all such tokens.
Trie consumes excessive memoryShare tokenizer trie across constraints, use packed child ranges, and impose a tokenizer-byte ceiling.
Allowed-set cache becomes unboundedByte-accounted LRU with per-constraint and global caps; no cache entry is necessary for correctness.
Special tokens leak into structured outputSeparate special-token mask applied independently of grammar acceptance.
Contextual decoding cannot be stabilizedMark the tokenizer unsupported for constrained mode.

Bounded JSON profile and semantic validation.

Plan elementDetail
Research objectiveDeliver useful schema-constrained JSON with explicit feature coverage and a separate source-evidence semantic validator.
Required expertiseJSON and JSON Schema, formal-language compilation, Unicode, extraction evaluation, API/schema design, and Rust validation.
Core outputstrj-1 profile; schema parser; profile compiler; JSON validator; semantic evidence schema; supported-feature manifest; conformance and benchmark report.
Low staffingOne schema/compiler engineer, one part-time application engineer.
Medium staffingOne JSON Schema lead, one compiler engineer, one extraction/evaluation engineer, part-time security review.
High staffingIndependent validator, broad schema-corpus analysis, domain-specific extraction studies, and external interoperability review.
Low/medium/high$85,000 / $220,000 / $470,000
Duration12 weeks.

Milestones:

WeeksMilestoneExit evidence
1–2Freeze profile keywords, limits, property order, number policy, and deferralsNormative profile and supported-feature matrix
3–4Parse schemas and canonicalize identitiesStable schema hashes, duplicate-key and unknown-key rejection
5–6Compile objects, arrays, enums, strings, numbers, and local referencesGrammar-equivalence and independent-validation tests
7Run applicable official JSON Schema test casesPass/fail/unsupported report with no ignored cases
8Classify JSONSchemaBench schemasCoverage by feature and complexity; explicit safe rejection rate
9–10Build semantic extraction validator and evidence bindingSpan, transformation, unit, enum, and cross-field tests
11–12End-to-end model trials and hardeningSyntax, schema, semantic, latency, and content-quality results

Primary risks and mitigations:

RiskMitigation
Users assume full JSON Schema supportRequire profile: "trj-1" and expose rejected keywords in diagnostics.
Ordered properties reduce model qualityCompare fixed-order and limited permutation approaches experimentally before any expansion.
JSON-valid values remain factually wrongRequire semantic status and evidence fields for extraction use cases.
Numeric interoperability differs between Rust and JavaScriptRestrict JSON numbers and return exact large/decimal values as strings.
Unicode length and escape accounting divergeDefine scalar-value counting and independently test raw UTF-8 versus \u escapes.
Real-world schema coverage is too lowUse benchmark classification to choose the next bounded feature, rather than enabling broad keywords ad hoc.

Integrated timeline.

gantt
    title TinyRustLM constrained-generation research program
    dateFormat  YYYY-MM-DD
    axisFormat  %b %d

    section Shared specification
    Claim taxonomy and limits             :a1, 2026-08-10, 14d
    Test corpus architecture               :a2, 2026-08-10, 21d

    section Byte core
    Grammar parser and AST                 :b1, 2026-08-24, 21d
    Static analyses                        :b2, after b1, 21d
    Parser-state compiler                  :b3, after b2, 21d
    Sampling and streaming integration     :b4, after b3, 21d
    Hardening and gate                     :b5, after b4, 14d

    section Tokenizer and trie
    Token-byte fixture and probes          :c1, 2026-09-07, 21d
    UTF-8 and trie engine                  :c2, after c1, 21d
    Cache and performance work             :c3, after c2, 21d
    Integration gate                       :c4, after c3, 14d

    section JSON and semantics
    JSON profile freeze                    :d1, 2026-09-21, 14d
    Schema compiler                        :d2, after d1, 28d
    Official and corpus conformance        :d3, after d2, 21d
    Semantic evidence validator            :d4, after d2, 28d
    End-to-end gate                        :d5, after d3, 21d

    section Program promotion
    Security review                        :e1, 2026-11-30, 21d
    Sealed quality evaluation              :e2, 2026-12-07, 28d
    Release decision                       :milestone, e3, 2027-01-11, 0d

Failure semantics, testing, performance, and clean adoption

Failure taxonomy. Every failure returns a stable machine-readable code, human-readable local diagnostic, constraint/tool identity, and raw stop state where retention is authorized. No failure is converted into a plausible-looking answer.

FailureRequired behavior
grammar_utf8Reject source at exact byte offset.
grammar_syntaxReject with expected-token category; do not attempt alternate parsing dialects.
grammar_unknown_featureReject and name the unsupported construct.
grammar_duplicate_ruleReject both definitions.
grammar_nullable_cycleReject before automaton generation.
grammar_left_recursionReject with rule cycle.
grammar_ambiguousReject with conflicting state or FIRST-set evidence.
grammar_state_limitAbort compilation atomically and report measured/projected counts.
grammar_memory_limitAbort before exceeding allocation budget.
tokenizer_unstable_bytesDisable constrained mode for the tokenizer.
constraint_empty_setStop immediately and preserve raw generated prefix.
constraint_token_limitReturn incomplete structured output with completed: false.
constraint_invalid_eosTreat premature EOS as forbidden; if forced externally, return failure.
sampling_nonfiniteStop when no finite allowed distribution remains.
semantic_validation_failedReturn syntax-valid raw result plus explicit semantic errors; do not rewrite.
calculator_syntaxNo evaluation.
calculator_overflowNo saturation or wrapping.
calculator_division_by_zeroCanonical error result.
tool_unavailableNo fallback to similarly named tool.
tool_unauthorizedNo execution and no automatic confirmation substitution.
tool_timeoutCancel work, preserve provenance, ignore late result.
cancelledReturn partial raw data and no completed object.
stale_resultReject result whose request, composition, schema, or authority binding no longer matches.
duplicate_requestReturn recorded idempotent outcome or explicit duplicate failure.
model_switchedInvalidate pending generation/tool proposal unless explicitly rebound.

Privacy-safe observability.

Private local traces may contain:

  • model, tokenizer, prompt-policy, grammar, schema, compiler, and tool implementation hashes;
  • generated token IDs and exact generated bytes when the user has authorized diagnostic retention;
  • allowed-token count at each step;
  • selected token rank and pre/post-mask score summaries;
  • parser-state ID, UTF-8 state, cache hit/miss, compile and mask timings;
  • RNG algorithm/version and initial state;
  • stop/failure reason;
  • tool request ID, capability, schema hashes, authorization class, result status, truncation, and deterministic provenance.

Public aggregate reports must exclude prompts, outputs, document text, tool input values, file names, private paths, credentials, user identifiers, and per-session traces. They may report distributions such as compile time, mask time, allowed-set size, failure frequency, schema-feature coverage, and quality scores.

Conformance and adversarial corpus.

Test familyRequired cases
Exhaustive tiny languagesAll strings through bounded length for two- to four-symbol alphabets; compare parser, trie mask, and brute-force completion oracle.
Token boundariesToken contains multiple terminals; token ends halfway through an escape; token crosses property/value punctuation; multiple token IDs share bytes.
UTF-8Every sequence length, split at every byte boundary, overlong forms, invalid continuations, surrogate encodings, maximum U+10FFFF, and above-range values.
Grammar limitsExactly-at-limit and one-over-limit sizes, rules, alternatives, nesting, repetitions, states, transitions, and memory.
Grammar pathologiesLeft recursion, indirect left recursion, nullable cycle, ambiguous choices, unreachable rules, empty language, and impossible start rule.
JSON stringsQuotes, reverse solidus, control escapes, raw non-ASCII, surrogate pairs, lone surrogates, maximum length, and mixed raw/escaped equivalents.
JSON objectsDuplicate names, wrong order, omitted required property, optional-property combinations, extra property, and discriminator collisions.
JSON numbers-0, leading zero, exponent extremes, safe-integer boundaries, i64 boundaries, binary64 overflow, and excessive precision.
ArraysEmpty, exact min/max, one over max, tuple boundaries, and deep legal nesting.
Semantic extractionExact span, normalized span, absent value, unsupported value, conflicting evidence, unit mismatch, enum mismatch, and cross-field contradiction.
CalculatorPrecedence, associativity, unary signs, scale alignment, rounding ties, overflow, division by zero, negative exponent, and operation limit.
Tool envelopeUnknown names, changed versions, incorrect schema hashes, oversized input, malicious strings in every field, stale authorization, duplicate ID, and late result.
InjectionInstructions in quoted text, schema descriptions, property names, tool arguments, errors, provenance, and tool-result values.
StreamingCancellation after every token/byte, partial UTF-8, partial escape, partial number, partial object, and only-EOS state.
ReplaySame trace reproduces token sequence; changed model/tokenizer/grammar/compiler fails identity check.

The official JSON Schema Test Suite should be vendored at a pinned revision, but only tests applicable to the declared profile should be classified as required. Every remaining test must be marked unsupported rather than silently skipped. JSONSchemaBench should be used for coverage and performance analysis, not as the sole correctness oracle.

Quality experiment design.

The principal experiment should use four arms:

ArmBehavior
BaselineUnconstrained generation, no repair
Syntax constrainedGrammar mask only
Syntax plus semanticsGrammar mask followed by explicit semantic validator
Deterministic assistanceSame as appropriate arm plus calculator or read-only tool

Tasks should cover ordinary conversation, summarization, rewriting, extraction, JSON generation, code explanation, ambiguity handling, practical reasoning, and arithmetic. The evaluation set must be sealed after prompt and feature decisions. Metrics should include completion rate, valid syntax, profile validity, semantic accuracy, evidence accuracy, unsupported/unknown calibration, helpfulness, instruction adherence, refusal appropriateness, truncation, latency, peak memory, compile time, allowed-token counts, and mask intervention severity.

A useful intervention metric is:

\[ I = \frac{1}{N}\sum_{t=1}^{N} \left[ \log p_t^{\text{unmasked}}(x_t^\*) - \log p_t^{\text{unmasked}}(x_t) \right], \]

where \(x_t^\*\) is the model’s unconstrained top token and \(x_t\) the constrained selected token. High intervention indicates that the grammar repeatedly forces the model away from its preferred distribution and should be correlated with semantic quality. LM Format Enforcer exposes similar token-level diagnostics and explicitly cautions that syntactic enforcement can increase hallucination or force low-weight tokens.

Performance formulas and targets.

QuantityFormula or measurement
Trie memory\(M_{\text{trie}} \approx N_n b_n + N_e b_e + Vb_{\text{id}}\)
Full scan cost\(O(V\bar L C_p)\) per generated token
Trie product cost\(O(E_{\text{visited}}C_p + A)\)
Mask-cache memory\(\sum_i (\text{key}_i + \lceil V/8\rceil + \text{metadata}_i)\) for dense bitsets; use sparse IDs when \(A \ll V\)
Runtime parser memory\(O(D_{\text{stack}} + D_{\text{terminal}})\) per sequence
Compilation upper boundChecked state, transition, AST-node, byte, operation, and wall-clock budgets
End-to-end overhead\((T_{\text{constrained}}-T_{\text{baseline}})/T_{\text{baseline}}\)
Cancellation latencyTime from cancellation signal to worker acknowledgment and resource release

Initial product targets should be treated as hypotheses: median warm mask computation under 0.25 ms, p99 under 2 ms on a representative middle-tier desktop, cold compilation under 50 ms for ordinary schemas, peak compiled constraint under 8 MiB for typical cases, and cancellation acknowledgment under 16 ms. Public systems report substantially lower typical per-token overhead under server-class conditions, including llguidance’s reported sub-50-microsecond average over its benchmark and XGrammar’s reported near-zero end-to-end overhead, but browser-local targets must be measured independently.

TDD backlog and promotion gates.

StageBuild firstPromotion gate
Byte automatonLiteral bytes and exact-choice grammarExhaustive tiny-vocabulary equality with brute-force oracle
Tiny tokenizerHand-written tokens including partial UTF-8Every allowed token preserves a valid completion
JSON lexical subsetStrings, numbers, booleans, nullRFC lexical tests and malformed-input rejection
Deterministic parserObjects and bounded arraysNo ambiguity, nullable cycle, or left recursion accepted
Sampling integrationMask, penalties, temperature, top-k/p, EOSDeterministic replay and all edge-case failures explicit
StreamingComplete-scalar display and partial-state UINo partial object represented as complete
CalculatorFixed decimal and canonical errorsCross-language golden vectors
Read-only toolCalculator or supplied-text transformInjection corpus cannot alter authority
Browser integrationDedicated worker and cancellationMemory, latency, and cleanup targets met
Sealed evaluationAll four experiment armsPredeclared quality and regression thresholds met

Recommended release gates are: 100% pass on required conformance tests; zero silent repair; zero unknown-key acceptance; no crash or hang in adversarial corpus; no tool dispatch outside the compiled registry; exact replay on the supported reference environment; syntactic-validity improvement without a statistically or practically unacceptable decline in semantic correctness or helpfulness; and privacy review of every trace field.

Clean deletion rules. Because no legacy compatibility is required, any unpublished format or mechanism should be deleted if it:

  • requires answer repair or retry-until-valid to meet its success criterion;
  • cannot map tokens to exact bytes;
  • ignores unknown grammar or schema constructs;
  • permits unbounded repetition, recursion, state growth, or memory use;
  • has nondeterministic compilation or conflicting grammar interpretations;
  • needs general regex, dynamic code loading, or executable descriptions;
  • exposes user-controlled paths or destinations;
  • allows tool results to change authority;
  • lacks an explicit raw failure state;
  • performs worse than the canonical protocol on both quality and resource use without a unique required capability;
  • cannot be independently tested against the reference interpreter.

Critical unknowns requiring authorized local access.

UnknownWhy it mattersVerification needed
Exact tokenizer format and byte semanticsDetermines whether the core is implementable and whether tokens split UTF-8 or grammar unitsExport token ID, raw bytes, special-token flags, and contextual decode probes
Model sampling pipelineDetermines where masks, penalties, temperature, and EOS currently occurCode inspection and deterministic trace fixture
Vocabulary size and token-byte distributionDrives trie memory and mask representationTokenizer statistics and representative device profile
Current stop-token behaviorPremature stops can violate grammar completionGeneration traces and sampler tests
Current streaming/UI pathUI may currently decode or normalize text before the core sees itBrowser trace and code inspection
Existing JSON/tool formatsUnpublished formats should be removed rather than maintained in parallelRepository and test inventory
Browser and device support floorDetermines memory, worker, WebAssembly, and latency budgetsProduct requirements and device matrix
Model quality under ordered JSON propertiesFixed ordering may help or harm particular modelsSealed A/B evaluation
Whether byte fallback is availableAffects completeness for Unicode and uncommon bytesTokenizer fixture
Private logging and retention policyDetermines whether raw token traces may be storedPrivacy and security review
Existing local capabilitiesDetermines the realistic first tool beyond calculatorCapability inventory and threat classification
Model/composition switching behaviorPending tool calls and replay must bind to exact compositionApplication state-machine inspection

No final conformance claim should be made before the tokenizer and sampler are inspected. In particular, a grammar engine cannot safely be “adapted later” to an unknown tokenizer by applying character-level rules to token IDs.

Gap analysis by discipline.

AreaCurrent public evidenceRemaining gap for TinyRustLM
Formal constrained decodingStrong evidence that grammar masks can enforce structure and improve some tasksNo proof that a deterministic bounded subset covers TinyRustLM’s target schemas
Token maskingPublic trie/index/cache strategies existExact private tokenizer semantics are unknown
JSON SchemaStandards, test suite, and benchmark are matureProduct-specific supported subset and ordered-property quality are unresolved
UnicodeNormative validity rules are clearTokenizer interaction and display pipeline require local verification
CalculatorChecked integer primitives and decimal design are straightforwardCross-browser serialization and required precision need product decisions
Tool securityCapability and permission principles are establishedActual local state boundaries and confirmation UX are unknown
QualityRecent benchmarks consider syntax, coverage, and qualityTinyRustLM’s small local model may respond differently under strong masks
PerformanceServer-side engines report low overheadBrowser-local CPU, WebAssembly, memory, and worker overhead remain unmeasured
ObservabilityHash-bound, privacy-aware traces are feasibleRetention consent, storage quota, and export design require policy decisions

Recommended immediate next steps.

SequenceActionDecision produced
1Freeze claim vocabulary and prohibit repair, hidden retry, arbitrary execution, unknown-feature acceptance, and unbound tools in writingPrevents architecture drift while implementation is explored
2Obtain an authorized tokenizer manifest and sampler trace fixtureGo/no-go for byte-accurate constrained mode
3Implement the tiny brute-force reference interpreter before the optimized compilerCreates an independent correctness oracle
4Freeze trg-1 and trj-1 limits before optimizationMakes resource and compatibility tests meaningful
5Build exhaustive tiny-vocabulary and partial-UTF-8 testsVerifies the hardest token/byte boundary early
6Implement exact-choice and minimal JSON constraintsProduces the first end-to-end vertical slice
7Integrate sampling and streaming with explicit failure recordsVerifies no repair and correct EOS/cancellation behavior
8Build the fixed-decimal calculator and envelope as the first toolExercises deterministic execution and provenance without side effects
9Run official JSON tests and JSONSchemaBench classificationQuantifies supported coverage and safe rejection
10Conduct injection, browser performance, and sealed quality evaluationsDetermines release readiness
11Delete all unsuccessful unpublished formatsPreserves one auditable protocol
12Publish a local conformance statement limited to actually demonstrated claimsAvoids overclaiming syntax as truth or safety

The first irreversible decision should not be a broad grammar feature. It should be the token-byte identity contract, because every compiler, trie, cache, mask, stream, replay record, and JSON guarantee depends on it.

Annotated primary-source register

All sources below were retrieved on August 1, 2026. Repository releases should be pinned to exact source commits in the implementation lockfile; a release tag alone is not sufficient for reproducible security review.

Source and revision/dateTypeRelevance and caution
Geng et al., “Grammar-Constrained Decoding for Structured NLP Tasks without Finetuning,” EMNLP 2023, December 2023Peer-reviewed primary researchDemonstrates input-dependent grammars and task-level gains in extraction, disambiguation, and parsing. Supports the value of constrained decoding, not TinyRustLM-specific performance.
Willard and Louf, “Efficient Guided Generation for Large Language Models,” arXiv:2307.09702, July 2023Primary research and Outlines foundationFrames guided generation using FSM transitions and vocabulary indexing. Useful for regular-language and index design.
Dong et al., “XGrammar,” arXiv:2411.15100, November 2024Primary systems researchDescribes context-independent token prechecking, persistent stacks, and grammar/inference co-design. Performance claims come from its evaluated serving settings.
Li et al., “XGrammar 2,” arXiv:2601.04426, January 2026Recent primary systems researchAdds dynamic dispatch, JIT compilation, cross-grammar caching, Earley parsing, and repetition compression. Relevant as a design comparison, though TinyRustLM should initially remain narrower.
Geng et al., “Generating Structured Outputs from Language Models: Benchmark and Studies,” arXiv:2501.10868, January 2025Primary benchmark researchIntroduces JSONSchemaBench and evaluates efficiency, coverage, and quality. Important because it treats syntactic validity as only one evaluation dimension.
GRAMMAR-LLM, Findings of ACL 2025, July 2025Peer-reviewed primary researchProvides evidence for deterministic grammar transformations and task evaluation. Does not establish universal benefit for all generation tasks.
XGrammar repository, release v0.2.1, May 17, 2026Pinned public implementation familyCurrent public implementation reference for structured generation and XGrammar 2 evolution. Pin an exact commit before code-level comparison.
llguidance repository, v1.0.0 designation June 23, 2025Rust implementationParticularly relevant to Rust parser engineering, JSON Schema coverage, and reported mask latency. Its broad grammar surface should not be copied without TinyRustLM’s bounded threat model.
Outlines repository, release v1.3.0, May 13, 2026Public structured-generation implementationDemonstrates typed interfaces, JSON/regex/grammar generation, and model integrations. Its larger Python ecosystem differs substantially from a zero-dependency browser core.
outlines-core repository, release 0.2.14, January 9, 2026Rust implementationUseful reference for schema-to-regex, vocabulary indexing, and Rust state guides. Special-token handling warning reinforces the need for an explicit token-byte contract.
LM Format Enforcer repository, release v0.11.2, August 9, 2025Public implementationDescribes character-parser/token-trie intersection and exposes diagnostics showing when masks force low-probability tokens. Also explicitly separates format guarantees from semantic correctness.
Guidance repository, release 0.3.2, March 18, 2026Public implementationUseful API and integration comparison. Examples should not be interpreted as medical or semantic validation evidence.
RFC 8259, JSON, December 2017Internet standardNormative base for JSON syntax, UTF-8 interoperability, strings, numbers, and duplicate-name concerns. Older than the preferred five-year window but foundational and still controlling.
JSON Schema Draft 2020-12, published June 16, 2022Official specificationDefines the broader schema language against which TinyRustLM must accurately describe its subset.
JSON Schema Test Suite, release 23.1.0, May 12, 2023Official conformance suiteLanguage-agnostic validator tests. Vendor at an exact commit and classify every test as required, unsupported, or inapplicable.
JSONSchemaBench repository and 9,558-schema corpusPrimary benchmark dataSuitable for feature-coverage and performance analysis; not a substitute for formal conformance tests.
RFC 8785, JSON Canonicalization Scheme, June 2020IETF informational specificationUseful for stable schema/envelope hashing, unique keys, valid Unicode, and numeric interoperability. Canonicalization must not become output repair.
Unicode UTF-8 corrigendum and conformance materialsOfficial Unicode guidanceEstablishes rejection of illegal, overlong, and irregular UTF-8. Essential for byte-level token processing.
ECMAScript Number and JSON serialization specifications, 2024–2027 editionsOfficial language specificationDocuments binary64 Number semantics and JSON serialization behavior for NaN and infinity. Supports returning exact calculator decimals as strings.
Rust i128 documentation, Rust 1.97.1, July 14, 2026Official implementation documentationProvides checked arithmetic primitives for a no-third-party-crate fixed-decimal calculator. Pin TinyRustLM’s actual compiler version.
NIST AI RMF 1.0, January 2023, and Generative AI Profile NIST AI 600-1, July 2024Official risk-management guidanceSupports lifecycle documentation, testing, measurement, and explicit risk controls. It is governance guidance, not a constrained-decoding algorithm.
W3C Permissions and Permissions Policy working drafts, October 2025Official web-platform specificationsUseful conceptual basis for user-granted powerful capabilities and feature disabling. Draft status means TinyRustLM should not depend on unsupported browser semantics.
RFC 9562, UUIDs, May 2024Internet standardSuitable for request identifiers. UUID uniqueness does not confer authorization or replay protection by itself.