This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/vendor/patches/0017-json-schema-a-pattern-without-anchors-searches.patch
T
davetha 52f47505c2 json-schema: a pattern without ^ or $ searches, as JSON Schema reads it
_visit_pattern() read only ^...$ and refused every other `pattern` with "Pattern must start
with '^' and end with '$'". JSON Schema patterns are unanchored (ECMA-262 search, validation
6.3.3). Since 1.8 constrains tool-call arguments with a grammar, a bare pattern on array items
failed the whole request: an opencode agent's tool list did, on every request.

A missing anchor is now written out as any string text on that side, (?:[^"\\]|\\.)*, not ".*"
('.' matches '"' here, so on array items ["1","x"] read as one string holding a digit). A
one-sided anchor beside a top-level '|' is still refused. vendor/patches/0017, vendor/UPSTREAM;
tests in gbnf_test (json_schema_grammars_admit_exactly_their_instances) and sample_test (an
empty pattern now matches every string).
2026-10-19 10:38:06 +02:00

61 lines
2.9 KiB
Diff

diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp
index 12e2756..df4846d 100644
--- a/common/json-schema-to-grammar.cpp
+++ b/common/json-schema-to-grammar.cpp
@@ -291,6 +291,45 @@ static inline bool is_gbnf_escape(char c) {
return false;
}
}
+
+// RADIANCE: a JSON Schema `pattern` is unanchored (ECMA-262 search semantics, JSON Schema
+// validation 6.3.3): "[0-9]+" admits any string holding a digit. _visit_pattern() reads only
+// ^...$, so a missing anchor is written out as "any string text" on that side. That filler is
+// not ".*": '.' here matches '"', and the string would run on past its closing quote. A
+// one-sided anchor beside a top-level '|' binds to one branch only, so that pattern is
+// returned as it came and refused.
+static std::string anchor_pattern(const std::string & p) {
+ static const std::string any = "(?:[^\"\\\\]|\\\\.)*";
+ const bool starts = !p.empty() && p.front() == '^';
+ size_t slashes = 0; // backslashes before a final '$': an odd count makes it a literal
+ if (!p.empty() && p.back() == '$') {
+ for (size_t k = p.size() - 1; k > 0 && p[k - 1] == '\\'; --k) slashes++;
+ }
+ const bool ends = p.size() > (starts ? 1u : 0u) && p.back() == '$' && slashes % 2 == 0;
+ if (starts && ends) {
+ return p;
+ }
+ const size_t from = starts ? 1 : 0;
+ const std::string core = p.substr(from, p.size() - from - (ends ? 1 : 0));
+ if (core.empty()) {
+ return "^" + any + "$";
+ }
+ if (starts != ends) {
+ int depth = 0;
+ bool in_class = false;
+ for (size_t k = 0; k < core.size(); ++k) {
+ const char c = core[k];
+ if (c == '\\') { ++k; continue; }
+ if (in_class) { if (c == ']') in_class = false; continue; }
+ if (c == '[') in_class = true;
+ else if (c == '(') depth++;
+ else if (c == ')') depth--;
+ else if (c == '|' && depth == 0) return p;
+ }
+ }
+ return "^" + (starts ? "" : any) + "(?:" + core + ")" + (ends ? "" : any) + "$";
+}
+
static std::unordered_set<char> ESCAPED_IN_REGEXPS_BUT_NOT_IN_LITERALS = {'^', '$', '.', '[', ']', '(', ')', '|', '{', '}', '*', '+', '?'};
static std::string replacePattern(const std::string & input, const std::regex & regex, const std::function<std::string(const std::smatch &)> & replacement) {
@@ -357,7 +396,8 @@ private:
return string_join(rules, " | ");
}
- std::string _visit_pattern(const std::string & pattern, const std::string & name) {
+ std::string _visit_pattern(const std::string & client_pattern, const std::string & name) {
+ const std::string pattern = anchor_pattern(client_pattern);
// 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() == '$')) {