json-schema: \d \w \s and their negations are written out as classes

\d \w \s \D \W \S were refused in a literal ("Unsupported regex escape") and copied raw into a
class, where GBNF cannot load them. Tool schemas use them freely: oh-my-pi's workpool tool sends
"\S" thirteen times, so with 0017 its requests got past the anchors and were refused here.

Each shorthand is written out as its ECMA-262 class before the scanner reads the pattern. The
negated forms leave out '"', '\' and the control characters and admit any JSON escape instead, so
a match cannot run past the closing quote; \s also admits \t \n \f \r. In a class a positive
shorthand becomes its ranges and [\s\S] and its kin become any text; a negated shorthand mixed into
a class is refused. vendor/patches/0018, vendor/UPSTREAM; gbnf_test cases, and sample_test's
"cannot spell" case moves from \d to \b.
This commit is contained in:
davetha authored and StillDeadcode committed 2026-10-19 10:38:06 +02:00
1 parent 52f47505c2
commit dd56e1158d
5 files changed
+212 -5

No files matched your search

+18
View File
@@ -102,6 +102,7 @@ vendor/llama/common/regex-partial.{h,cpp} <- llama.cpp common/*
patches/0014-json-schema-a-not-of-every-type-is-never.patch
patches/0016-chat-analyse-a-template-once.patch
patches/0017-json-schema-a-pattern-without-anchors-searches.patch
patches/0018-json-schema-shorthand-classes-are-written-out.patch
On 0003: a `pattern` in a `response_format` reaches _visit_pattern() as client data, and three
of its paths do not survive that. `front()`/`back()` run on an empty string; `seq.back()` runs
@@ -506,3 +507,20 @@ PATCH SERIES
backslashes is a literal and does not count as an anchor. tests/gbnf_test.cpp
(json_schema_grammars_admit_exactly_their_instances) holds the converter to the search
reading, on plain strings and on array items, and pins the refusal.
patches/0018-json-schema-shorthand-classes-are-written-out.patch
\d \w \s and their negations were refused in a literal ("Unsupported regex escape") and
copied raw inside a class, where GBNF cannot load them. Tool schemas use them freely:
oh-my-pi's workpool tool carries "\S" thirteen times, so with 0017 the request got past
the anchors and was refused here instead. Each shorthand is now written out before the
scanner reads the pattern, as the class ECMA-262 gives it: \d [0-9], \w [A-Za-z0-9_], \s the
ECMA-262 space characters. A JSON string holds text, so the negated forms leave out '"',
'\' and the control characters and admit any JSON escape instead -- a match cannot run past
the closing quote -- and \s also admits the escapes \t \n \f \r. In a class a positive
shorthand becomes its ranges ([\w-], [\da-f]) and [\s\S] and its kin become any text; a
negated shorthand mixed into a class has no GBNF spelling and is refused with a message
that says so. The escapes are judged on the encoded text, so \D admits an escape such as
\u0031 that decodes to a digit. sample_test's "cannot spell" case moves from \d to \b,
which is still refused; gbnf_test covers \S on strings and array items, \d dates and
decimals, [\w-], \s with an escaped tab, [\s\S] and \D, and the refused [a\S].