Repository navigation
A request regex is bounded twice: the pattern length at the door, the NFA builder state budget behind it - #181
Open
AgentSparko wants to merge 2 commits into
Open
AgentSparko wants to merge 2 commits into
AgentSparko wants to merge 2 commits into
Conversation
…he NFA builder's state budget behind it
This branch has not been deployed
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.
Two walls on the request regex, from the engine audit: the pattern's length at the door, the NFA builder's state budget behind it. A test that failed before the fix and passes after is included.
Critical: a request regex could ask for a billion automaton states
structured_outputs: {"regex": ...}was accepted from any request and compiled on thehandler's thread. The builder caps each repetition at 1,000 copies, but nested
repetitions compose:
((\d{1000}){1000}){1000}asks for ~10^9 NFA states, and the NFAis built whole before the DFA's own 20,000-state cap is ever asked. On this class of
box the OOM freezes the machine — one request, no auth beyond what the port allows.
Two walls now:
server/compat.py— a request regex longer thanACT_LIMITS["regex_chars"](8,192characters, the act path's own limit, so the door is consistent with SET_CONSTRAINT)
is a 400 that names
structured_outputs.engine/grammar.py—NFA_STATE_BUDGET = 250_000: the NFA builder raisesGrammarErroronce it has created that many states, so composition cannot explode nomatter which door the pattern came in (regex, choice list, json_schema, tools).
The budget number, honestly. Measured NFA sizes of legitimate grammars (this box,
engine venv):
json_object(depth 3) 6,955 states; a realisticjson_schema~9.9k;the tool-call language for the bench's 4 tools 3,326; for 20 agent-style tools 29,842
(DFA 14,951 — the pre-existing 20,000-state DFA cap, not the budget, is what a very
large tool set hits first). 250,000 is ~8x the largest grammar measured and ~1.7x what
a ~100-tool request would build; it bounds one build to tens of MB and about a second,
while every grammar real traffic sends compiles untouched. The test asserts both sides:
the refusal names the budget and arrives with the automaton still at or under it, and
json_object / json_schema / the twenty-tool call language still compile.
Tests
tests/test_grammar_dos.py— nested repetitions die at the state budget (before:(\d{1000}){300}built its 300k states and only then hit the DFA cap; the audit's10^9 pattern built until the box died); an over-long request regex is a 400 (before:
it passed as a pattern); legitimate grammars still compile.
Regression run (CPU, engine venv)
tests/test_grammar.py26 passed ·tests/test_set_constraint.py12 passed ·tests/test_toolformats.py6 passed.Notes for the reviewer
compile_regexcall, so the SRV-619 parts path (calls × documentcompiled apart) gets the budget per part, as before per whole.
choiceandjson_schemapatterns are not length-capped at the door (they arebounded by the body limit); the state budget is what holds them, and it is the wall
that matters — length alone never bounded the composed repetition counts.
budget is documented at
NFA_STATE_BUDGETand in the structured_pattern comment.