Skip to content

grammar budget: exploring a grammar stops at a work limit and a time limit - #3037

Merged
ashhart merged 3 commits into
mainfrom
security/grammar-budget-limits
Nov 13, 2026
Merged

ashhart merged 3 commits into
mainfrom
security/grammar-budget-limits

Conversation

@ashhart

@ashhart ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

What this changes

Before: grammar_budget explored a grammar's parser states on the request thread with no bound but the 16,384-state cap, and each state kept a buffer sized for the whole vocabulary. An ambiguous 21-byte regex had not finished after 7 minutes, and plain json_object took 1.5 GB over a 248,320-token vocabulary. After: exploring stops at a fixed count of steps and masks only the states it reached, a build still running after a minute is refused with HTTP 400, and a state keeps only its own successors.

  • Issue: n/a. SECURITY.md asks for private reports, so there is no public issue.
  • Stacked on: main.
  • atc overlap --json: not run.

Checks on this head

  • Head commit: f08922c01d079cc4ad6c23a182fa69bc54713fa9
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 345 on the base, 345 on this head, 0 new.
  • Tests run, with the command and pass, fail and skip counts:
    • zig fmt --check on 2 files: passes
    • git diff --check: passes
    • zig build -Dxgrammar=<xgrammar v0.2.8> test-grammar-budget test-grammar-tools: 8/8 steps succeeded; 52/52 tests passed
    • zig build test-server-cpu: 5/5 steps succeeded; 1325/1332 tests passed (7 skipped)
    • zig build test-golden test-server-security test-pull-security: 76/76 steps succeeded; 23/23 tests passed
  • New tests:
    • security: exploring a grammar stops at its work limit, and a place past it keeps xgrammar's mask
    • security: a grammar that takes longer than its time to explore gets no budget mask
  • CI: runs on this push.
  • Not run, and why: The mask comparison and the timings were made on the earlier base and not repeated on this head. The memory fix and the count of vocabulary walk steps have no test: both need a large vocabulary. zig build test and every step that runs on a GPU were not run: this Mac's GPU was serving a model, and no CUDA, ROCm or Intel hardware was available.

Receipt

  • Main against branch: could not be run. The budget masks were compared in the real library on the CPU, which is where they are built. No served run was made.
  • Boxes it reaches: any backend, only for requests that send grammar_budget: true.
  • New switch? Default, accepted values, profile row: none. tfg_budget_limits is a test seam in libtfgrammar, not a setting.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: this head on Zig 0.17.0, on an Apple silicon Mac, CPU-only steps. No GPU and no checkpoint was used.
  • Paths and platforms the change reaches, which I ran and which I could not: libtfgrammar built from xgrammar v0.2.8 on macOS. On the earlier base the library also compiled for Linux and Windows.
  • Exactness: not measured. For a grammar that explores within the limit the masks are equal to main's: 14,128 masks across eight ordinary grammars were compared on the earlier base. A long bounded string is cut earlier, as the notes say.
  • Main against this branch, same machine and checkpoint: not run, for the reason above.
  • Decode speed before and after: not measured.
  • Prompt speed before and after: not measured.
  • Licence of any new weights or drafter: n/a.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. Main with only this branch's tests added, zig build -Dxgrammar=<xgrammar v0.2.8> test-grammar-budget: exit 1. 4 of 6 tests passed, 2 failed: the two new tests.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

For a change that reaches inference:

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. Not measured: no served run.
  • No precision traded for speed. The bits change for one case, and the notes say which.
  • Decode and prompt processing are no slower than main. Not measured: no served run.

Notes for the reviewer

  • Output changes for one case. Over a 248,320-token vocabulary, a string with maxLength 500 or more keeps its budget masks for about its first 300 characters. Main keeps them to the end of a 500 or 1,000 cap. Strings capped at 400 or less are not affected. Past the cut the reply keeps xgrammar's own mask, so it can be cut mid-string as it is without the option. docs/api.md says so.
  • The work limit and the one-minute limit are constants measured on one Mac with one vocabulary.

Suggested changelog line

grammar_budget: exploring a grammar stops at a work limit and masks only what it reached, a build still running after a minute is refused, and its memory no longer grows with the vocabulary for each state. A long bounded string is masked for about its first 300 characters.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Review: not ready. This change makes outputs differ from main, and it can refuse requests that main serves.

  1. The time limit refuses requests depending on machine speed and load. Running out of time drops the tables and sets ok = false (tf_budget.cc near :353), and the server then returns a 400. The result is cached per grammar for the last 8 grammars, so one slow run under load keeps refusing that grammar until it is evicted. That also contradicts the docs line saying the same grammar is always cut at the same place.
  2. The work limit changes the mask itself. When the work runs out, the partial tables go live. Unexplored states count as one token from completion, and Mask falls back to xgrammar's own bits there. For a grammar over the limit, the mask allows more tokens than main's, so replies can differ from main's or stop where main would complete.
  3. The headroom is small. 2^31 is about twice json_object, so a schema with several bounded strings, or regex {m,n} repeats, could reach it.

To land:

  • Drop the time refusal. Count parser work deterministically instead, or keep only a very distant guard that is not cached.

  • Raise the work limit.

  • Show on a set of real JSON schemas and tool grammars that the masks stay byte-identical to main's.

  • Fix the comment near :264: the clock is read on every parser step, not every 2^20.

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Review fixes pushed (cfe3664):

  • No more clock. The time limit, its refusal and its cache entry are gone. Exploring counts parser work deterministically, so the same grammar gets the same answer on any machine and under any load.
  • No partial masks. A grammar past the work count gets a 400 that names the limit, and its partial tables are dropped. Every grammar under the limit runs the same exploration as main and gets main's masks.
  • Limit raised. It goes from 2^31 to 2^38 steps, about 256 times what json_object takes. The docs now say exactly that, rather than "unusually wide".

Results:

  • grammar, test-grammar-budget, test-grammar-tools and test-server-security: 15/15 steps, 75/75 tests.
  • The server's grammar_budget tests: 21/21, including the new 400 case.
  • Lean: 345 on both base and branch.

Not measured: real grammars' work counts. A grammar main serves that runs past 2^38 steps would now be refused. At that size, main spends minutes exploring it before the first token, which is the denial of service this closes.

  • Synthena (Ash's Agent)

…limit

grammar_budget explored a grammar's parser states on the request thread with no bound but the state
cap, and each state kept a buffer sized for the whole vocabulary. Exploring now stops at a fixed count
of steps and masks only the states it reached, a build still running after a minute is refused, and a
state keeps only its own successors. A long bounded string reaches the step limit early: see docs/api.md.
…'s masks below it

- The time limit is gone: no clock is read, so the same grammar gets the same
  verdict on every machine and under any load, and the cache keeps only that.
- A grammar whose exploring passes the work limit is refused with HTTP 400 that
  names the limit. Its partial tables are dropped and a matcher armed for it keeps
  xgrammar's bits, so no mask comes from part of a grammar.
- Below the limit exploring is main's: the count only adds up, and each parser
  step and token walk is the one main makes, so the masks are main's.
- The limit goes from 2^31 to 2^38 steps, about 256 times what json_object takes
  over a 250,000-token vocabulary.
- tfg_budget_ready answers 2 past the limit; tfg_budget_limits takes the work alone.
@ashhart
ashhart force-pushed the security/grammar-budget-limits branch from cfe3664 to 2f4f6c1 Compare November 13, 2026 02:02
@ashhart
ashhart merged commit acce63d into main Nov 13, 2026
2 of 10 checks passed
@ashhart
ashhart deleted the security/grammar-budget-limits branch November 13, 2026 02:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant