Repository navigation
grammar budget: exploring a grammar stops at a work limit and a time limit - #3037
Merged
Merged
Conversation
Owner
Author
|
Review: not ready. This change makes outputs differ from main, and it can refuse requests that main serves.
To land:
|
Owner
Author
|
Review fixes pushed (cfe3664):
Results:
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.
|
ashhart
marked this pull request as ready for review
November 12, 2026 23:06
This was referenced Nov 13, 2026
…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
force-pushed
the
security/grammar-budget-limits
branch
from
November 13, 2026 02:02
cfe3664 to
2f4f6c1
Compare
3 of 4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
Before:
grammar_budgetexplored 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 plainjson_objecttook 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.SECURITY.mdasks for private reports, so there is no public issue.atc overlap --json: not run.Checks on this head
f08922c01d079cc4ad6c23a182fa69bc54713fa9python3 tools/zig/lean_check.py, on the base and on this head: 345 on the base, 345 on this head, 0 new.zig fmt --checkon 2 files: passesgit diff --check: passeszig build -Dxgrammar=<xgrammar v0.2.8> test-grammar-budget test-grammar-tools: 8/8 steps succeeded; 52/52 tests passedzig 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 passedsecurity: exploring a grammar stops at its work limit, and a place past it keeps xgrammar's masksecurity: a grammar that takes longer than its time to explore gets no budget maskzig build testand 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
grammar_budget: true.tfg_budget_limitsis a test seam inlibtfgrammar, not a setting.libtfgrammarbuilt from xgrammar v0.2.8 on macOS. On the earlier base the library also compiled for Linux and Windows.Checklist
zig build -Dxgrammar=<xgrammar v0.2.8> test-grammar-budget: exit 1. 4 of 6 tests passed, 2 failed: the two new tests.For a change that reaches inference:
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. Not measured: no served run.Notes for the reviewer
maxLength500 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.mdsays so.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.