From dd56e1158dad8ad2d6211df571c38e8e8e47b0c5 Mon Sep 17 00:00:00 2001 From: davetha Date: Mon, 19 Oct 2026 03:07:26 +0000 Subject: [PATCH] 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. --- tests/gbnf_test.cpp | 16 ++- tests/sample_test.cpp | 7 +- vendor/UPSTREAM | 18 ++++ .../llama/common/json-schema-to-grammar.cpp | 79 ++++++++++++++- ...ma-shorthand-classes-are-written-out.patch | 97 +++++++++++++++++++ 5 files changed, 212 insertions(+), 5 deletions(-) create mode 100644 vendor/patches/0018-json-schema-shorthand-classes-are-written-out.patch diff --git a/tests/gbnf_test.cpp b/tests/gbnf_test.cpp index c120b1d2..60aa78d1 100644 --- a/tests/gbnf_test.cpp +++ b/tests/gbnf_test.cpp @@ -1705,6 +1705,19 @@ TEST(json_schema_grammars_admit_exactly_their_instances) { { R"({"type":"string","pattern":"x|yz"})", { R"("x")", R"("ayzb")" }, { R"("y")" } }, { R"({"type":"array","items":{"type":"string","pattern":"[0-9]+"}})", { R"(["1","x2"])", R"(["x\"1"])", "[]" }, { R"(["x"])", R"(["1","x"])" } }, + /* \d \w \s and their negations (0018), as tool schemas send them: oh-my-pi's workpool tool + * carries "\S" thirteen times. A negated class must not cross the closing quote. */ + { R"({"type":"string","pattern":"\\S"})", { R"("a")", R"(" a ")" }, { R"("")", R"(" ")" } }, + { R"({"type":"array","items":{"type":"string","pattern":"\\S"}})", + { R"(["a"," b"])" }, { R"([" "])", R"(["a"," "])" } }, + { R"({"type":"string","pattern":"^\\d{4}-\\d{2}-\\d{2}$"})", + { R"("2026-10-19")" }, { R"("2026-1-19")", R"("20x6-10-19")" } }, + { R"({"type":"string","pattern":"^[\\w-]+$"})", { R"("a_b-1")" }, { R"("a b")", R"("")" } }, + { R"({"type":"string","pattern":"^-?\\d+(?:\\.\\d+)?$"})", + { R"("3")", R"("-3.14")" }, { R"("3.")", R"("x")" } }, + { R"({"type":"string","pattern":"^a\\sb$"})", { R"("a b")", R"("a\tb")" }, { R"("ab")" } }, + { R"({"type":"string","pattern":"^[\\s\\S]{2}$"})", { R"("ab")", R"("a ")" }, { R"("a")" } }, + { R"({"type":"string","pattern":"^\\D$"})", { R"("x")" }, { R"("7")" } }, { R"({"type":"array","items":{"type":"integer"},"minItems":1,"maxItems":3})", { "[1]", "[1,2,3]", "[-4,0]" }, { "[]", "[1,2,3,4]", R"(["a"])" } }, { R"({"type":"object","properties":{"a":{"type":"integer"},"b":{"type":"string"}},)" @@ -1725,7 +1738,8 @@ TEST(json_schema_grammars_admit_exactly_their_instances) { fprintf(stderr, " SKIP number bounds: the converter does not enforce minimum/maximum " "on \"number\"\n"); for (const char* schema : { R"({"type":"string","pattern":"^a|b"})", - R"({"type":"string","pattern":"a|b$"})" }) { + R"({"type":"string","pattern":"a|b$"})", + R"({"type":"string","pattern":"^[a\\S]$"})" }) { std::string gbnf, err; CHECK(grammar_from_json_schema(schema, "root", &gbnf, &err) < 0); } diff --git a/tests/sample_test.cpp b/tests/sample_test.cpp index baef11f2..3043f33d 100644 --- a/tests/sample_test.cpp +++ b/tests/sample_test.cpp @@ -1108,11 +1108,12 @@ TEST(json_schema_refuses_what_it_cannot_actually_enforce) { "{\"$ref\":\"https://example.com/s.json#/x\"}", "root", &gbnf, &err) < 0); CHECK(!err.empty()); - /* GBNF cannot spell \d. Copying it through would succeed here and produce a grammar that - * fails to load, an internal error on a schema we accepted. */ + /* GBNF cannot spell \b. Copying it through would succeed here and produce a grammar that + * fails to load, an internal error on a schema we accepted. (\d and its kin are written out + * as classes since vendor patch 0018.) */ err.clear(); CHECK(grammar_from_json_schema( - "{\"type\":\"string\",\"pattern\":\"^\\\\d+$\"}", "root", &gbnf, &err) < 0); + "{\"type\":\"string\",\"pattern\":\"^a\\\\bb$\"}", "root", &gbnf, &err) < 0); CHECK(!err.empty()); /* A stray ']' is in NON_LITERAL_SET but claimed by no arm of the pattern scanner, so the diff --git a/vendor/UPSTREAM b/vendor/UPSTREAM index 356bc758..0e21d000 100644 --- a/vendor/UPSTREAM +++ b/vendor/UPSTREAM @@ -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]. diff --git a/vendor/llama/common/json-schema-to-grammar.cpp b/vendor/llama/common/json-schema-to-grammar.cpp index df4846dc..46648333 100644 --- a/vendor/llama/common/json-schema-to-grammar.cpp +++ b/vendor/llama/common/json-schema-to-grammar.cpp @@ -330,6 +330,77 @@ static std::string anchor_pattern(const std::string & p) { return "^" + (starts ? "" : any) + "(?:" + core + ")" + (ends ? "" : any) + "$"; } +// RADIANCE: \d \w \s and their negations, which GBNF cannot spell, written out as the classes +// ECMA-262 gives them, before the scanner reads the pattern. A JSON string holds text, not raw +// bytes: the negated forms leave out '"', '\' and the control characters, which a string can only +// carry escaped, and admit any JSON escape instead, so a match never runs past the closing quote; +// \s adds the escapes \t \n \f \r for the same reason. In a class a positive shorthand becomes its +// ranges and [\s\S] and its kin become any text; a negated one mixed into a class has no spelling, +// so false is returned and the pattern refused. +static bool expand_shorthands(const std::string & p, std::string & out) { + static const std::string sp = " \\u00A0\\u1680\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000\\uFEFF"; + static const std::string esc = "\\\\(?:[\"\\\\/bfnrt]|u[0-9A-Fa-f]{4})"; + static const std::string raw = "\"\\\\\\x00-\\x1F"; + auto negated = [&](const std::string & cls) { return "(?:[^" + cls + raw + "]|" + esc + ")"; }; + out.clear(); + for (size_t i = 0; i < p.size(); ++i) { + const char c = p[i]; + if (c == '\\' && i + 1 < p.size()) { + const char n = p[++i]; + switch (n) { + case 'd': out += "[0-9]"; break; + case 'w': out += "[A-Za-z0-9_]"; break; + case 's': out += "(?:[" + sp + "]|\\\\[tnfr])"; break; + case 'D': out += negated("0-9"); break; + case 'W': out += negated("A-Za-z0-9_"); break; + case 'S': out += negated(sp); break; + default: out += c; out += n; break; + } + continue; + } + if (c != '[') { + out += c; + continue; + } + // The class runs to the first unescaped ']', as the scanner reads it. + size_t j = i + 1; + std::string body; + while (j < p.size() && p[j] != ']') { + if (p[j] == '\\' && j + 1 < p.size()) { + body += p.substr(j, 2); + j += 2; + } else { + body += p[j++]; + } + } + if (body == "\\s\\S" || body == "\\S\\s" || body == "\\d\\D" || body == "\\D\\d" || + body == "\\w\\W" || body == "\\W\\w") { + out += "(?:[^" + raw + "]|" + esc + ")"; + i = j; + continue; + } + std::string cls = "["; + for (size_t k = 0; k < body.size(); ++k) { + if (body[k] == '\\' && k + 1 < body.size()) { + const char n = body[++k]; + if (n == 'd') cls += "0-9"; + else if (n == 'w') cls += "A-Za-z0-9_"; + else if (n == 's') cls += sp; + else if (n == 'D' || n == 'W' || n == 'S') return false; + else { cls += '\\'; cls += n; } + } else { + cls += body[k]; + } + } + out += cls; + if (j < p.size()) { + out += ']'; + } + i = j; + } + return true; +} + static std::unordered_set ESCAPED_IN_REGEXPS_BUT_NOT_IN_LITERALS = {'^', '$', '.', '[', ']', '(', ')', '|', '{', '}', '*', '+', '?'}; static std::string replacePattern(const std::string & input, const std::regex & regex, const std::function & replacement) { @@ -397,7 +468,13 @@ private: } std::string _visit_pattern(const std::string & client_pattern, const std::string & name) { - const std::string pattern = anchor_pattern(client_pattern); + std::string expanded; + if (!expand_shorthands(client_pattern, expanded)) { + _errors.push_back("Unsupported pattern: \\D, \\W or \\S inside a character class " + "cannot be spelled in GBNF"); + return ""; + } + const std::string pattern = anchor_pattern(expanded); // RADIANCE: front()/back() on an empty string is UB, and `pattern` is client data straight // off the wire. The size test has to come first. if (pattern.size() < 2 || !(pattern.front() == '^' && pattern.back() == '$')) { diff --git a/vendor/patches/0018-json-schema-shorthand-classes-are-written-out.patch b/vendor/patches/0018-json-schema-shorthand-classes-are-written-out.patch new file mode 100644 index 00000000..f3f6b75f --- /dev/null +++ b/vendor/patches/0018-json-schema-shorthand-classes-are-written-out.patch @@ -0,0 +1,97 @@ +diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp +index df4846d..4664833 100644 +--- a/common/json-schema-to-grammar.cpp ++++ b/common/json-schema-to-grammar.cpp +@@ -330,6 +330,77 @@ static std::string anchor_pattern(const std::string & p) { + return "^" + (starts ? "" : any) + "(?:" + core + ")" + (ends ? "" : any) + "$"; + } + ++// RADIANCE: \d \w \s and their negations, which GBNF cannot spell, written out as the classes ++// ECMA-262 gives them, before the scanner reads the pattern. A JSON string holds text, not raw ++// bytes: the negated forms leave out '"', '\' and the control characters, which a string can only ++// carry escaped, and admit any JSON escape instead, so a match never runs past the closing quote; ++// \s adds the escapes \t \n \f \r for the same reason. In a class a positive shorthand becomes its ++// ranges and [\s\S] and its kin become any text; a negated one mixed into a class has no spelling, ++// so false is returned and the pattern refused. ++static bool expand_shorthands(const std::string & p, std::string & out) { ++ static const std::string sp = " \\u00A0\\u1680\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000\\uFEFF"; ++ static const std::string esc = "\\\\(?:[\"\\\\/bfnrt]|u[0-9A-Fa-f]{4})"; ++ static const std::string raw = "\"\\\\\\x00-\\x1F"; ++ auto negated = [&](const std::string & cls) { return "(?:[^" + cls + raw + "]|" + esc + ")"; }; ++ out.clear(); ++ for (size_t i = 0; i < p.size(); ++i) { ++ const char c = p[i]; ++ if (c == '\\' && i + 1 < p.size()) { ++ const char n = p[++i]; ++ switch (n) { ++ case 'd': out += "[0-9]"; break; ++ case 'w': out += "[A-Za-z0-9_]"; break; ++ case 's': out += "(?:[" + sp + "]|\\\\[tnfr])"; break; ++ case 'D': out += negated("0-9"); break; ++ case 'W': out += negated("A-Za-z0-9_"); break; ++ case 'S': out += negated(sp); break; ++ default: out += c; out += n; break; ++ } ++ continue; ++ } ++ if (c != '[') { ++ out += c; ++ continue; ++ } ++ // The class runs to the first unescaped ']', as the scanner reads it. ++ size_t j = i + 1; ++ std::string body; ++ while (j < p.size() && p[j] != ']') { ++ if (p[j] == '\\' && j + 1 < p.size()) { ++ body += p.substr(j, 2); ++ j += 2; ++ } else { ++ body += p[j++]; ++ } ++ } ++ if (body == "\\s\\S" || body == "\\S\\s" || body == "\\d\\D" || body == "\\D\\d" || ++ body == "\\w\\W" || body == "\\W\\w") { ++ out += "(?:[^" + raw + "]|" + esc + ")"; ++ i = j; ++ continue; ++ } ++ std::string cls = "["; ++ for (size_t k = 0; k < body.size(); ++k) { ++ if (body[k] == '\\' && k + 1 < body.size()) { ++ const char n = body[++k]; ++ if (n == 'd') cls += "0-9"; ++ else if (n == 'w') cls += "A-Za-z0-9_"; ++ else if (n == 's') cls += sp; ++ else if (n == 'D' || n == 'W' || n == 'S') return false; ++ else { cls += '\\'; cls += n; } ++ } else { ++ cls += body[k]; ++ } ++ } ++ out += cls; ++ if (j < p.size()) { ++ out += ']'; ++ } ++ i = j; ++ } ++ return true; ++} ++ + static std::unordered_set ESCAPED_IN_REGEXPS_BUT_NOT_IN_LITERALS = {'^', '$', '.', '[', ']', '(', ')', '|', '{', '}', '*', '+', '?'}; + + static std::string replacePattern(const std::string & input, const std::regex & regex, const std::function & replacement) { +@@ -397,7 +468,13 @@ private: + } + + std::string _visit_pattern(const std::string & client_pattern, const std::string & name) { +- const std::string pattern = anchor_pattern(client_pattern); ++ std::string expanded; ++ if (!expand_shorthands(client_pattern, expanded)) { ++ _errors.push_back("Unsupported pattern: \\D, \\W or \\S inside a character class " ++ "cannot be spelled in GBNF"); ++ return ""; ++ } ++ const std::string pattern = anchor_pattern(expanded); + // RADIANCE: front()/back() on an empty string is UB, and `pattern` is client data straight + // off the wire. The size test has to come first. + if (pattern.size() < 2 || !(pattern.front() == '^' && pattern.back() == '$')) {