radiance 1.0.3

- A tool whose schema bounds a string or an array past 2000 (maxLength, minLength, maxItems,
  minItems) no longer fails every request carrying it (issue #5). The bound is held exactly, up to
  about 200,000; past that the request is refused with a 400 naming the tool's property and the
  count. A bound past 2^31, such as Number.MAX_SAFE_INTEGER, is no bound.
- Compiling a grammar with a long bounded repetition is linear in the bound instead of quadratic:
  maxLength 32000 compiles in 0.7 s instead of 2.1 s.
This commit is contained in:
Deadcode committed 2026-10-04 16:30:28 +02:00
1 parent 8e3463c173
commit f48786483d
8 files changed
+376 -78

No files matched your search

+15 -3
View File
@@ -98,6 +98,7 @@ vendor/llama/common/regex-partial.{h,cpp} <- llama.cpp common/*
patches/0009-chat-a-cdata-section-is-punctuation-not-content.patch
patches/0011-chat-add_bos-must-come-from-the-inputs.patch
patches/0012-json-schema-a-schema-no-value-satisfies.patch
patches/0013-json-schema-a-count-past-int-is-no-bound.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
@@ -429,6 +430,17 @@ PATCH SERIES
`string="true|false"` format), which give every argument an arm of its own and so never see
the object, give an optional one of that kind no arm; a model that writes it reaches the
undeclared-argument arm of 0010 where the format has one. core/text/chatfmt.cpp, the
declared-format grammar, does the same outside this tree. Where a value is required (a required property, the root, an array's items, a union
with no alternative left) it is still refused, by name. Every other `not` is refused as 0004
refuses it.
declared-format grammar, does the same outside this tree. Where a value is required (a
required property, the root, an array's items, a union with no alternative left) it is still
refused, by name. Every other `not` is refused as 0004 refuses it.
patches/0013-json-schema-a-count-past-int-is-no-bound.patch
minLength, maxLength, minItems and maxItems were read with get<int>(), which casts: a bound
past INT_MAX wrapped, and Number.MAX_SAFE_INTEGER -- a common way for a schema generator to
write "no limit" -- came out as -1 and the grammar failed to parse, failing the request with
it. A count at or past INT_MAX is now INT_MAX, which build_repetition already reads as no
maximum. That is exact for anything served here: no context holds a string or an array that
long. A count that is negative or not a number is refused by name. A float count is
truncated, where maxItems used to ignore one. The other half of the same report -- any count
past 2000 refused -- was the GBNF parser's, and is fixed in core/sample/gbnf.cpp.
+24 -5
View File
@@ -904,6 +904,26 @@ public:
// which is how TypeBox's Type.Never and Arktype's "never" are exported, the latter as an
// optional property no call may set. A `not` of the empty schema makes its whole schema
// unsatisfiable whatever sits beside it, so the sibling keywords do not matter.
// RADIANCE: a count -- minLength, maxLength, minItems, maxItems -- as the int build_repetition
// takes, or `dflt` where the schema has none. get<int>() casts, so a bound past INT_MAX wrapped:
// Number.MAX_SAFE_INTEGER, a common way to write "no limit", came out as -1 and the grammar did
// not parse. A count at or past INT_MAX is INT_MAX, which build_repetition reads as no maximum;
// no context the engine serves holds a string or an array that long, so the bound changes
// nothing a generation can write. A count that is not a non-negative number is refused.
int _schema_count(const json & schema, const char * key, int dflt, const std::string & name) {
if (!schema.contains(key)) {
return dflt;
}
const json & v = schema[key];
const double d = v.is_number() ? v.get<double>() : -1.0;
if (!(d >= 0)) {
_errors.push_back(std::string(key) + " of \"" + (name.empty() ? std::string("root") : name) +
"\" must be a non-negative number");
return dflt;
}
return d >= (double) std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : (int) d;
}
static bool is_unsatisfiable(const json & schema) {
if (schema.is_boolean()) return !schema.get<bool>();
if (!schema.is_object() || !schema.contains("not")) return false;
@@ -1059,9 +1079,8 @@ public:
return _add_rule(rule_name, rule);
}
std::string item_rule_name = visit(items, name + (name.empty() ? "" : "-") + "item");
int min_items = schema.contains("minItems") ? schema["minItems"].get<int>() : 0;
json max_items_json = schema.contains("maxItems") ? schema["maxItems"] : json();
int max_items = max_items_json.is_number_integer() ? max_items_json.get<int>() : std::numeric_limits<int>::max();
int min_items = _schema_count(schema, "minItems", 0, name);
int max_items = _schema_count(schema, "maxItems", std::numeric_limits<int>::max(), name);
return _add_rule(rule_name, "\"[\" space " + build_repetition(item_rule_name, min_items, max_items, "\",\" space") + " \"]\" space");
}
@@ -1077,8 +1096,8 @@ public:
}
if (schema_type == "string" && (schema.contains("minLength") || schema.contains("maxLength"))) {
std::string char_rule = _add_primitive("char", PRIMITIVE_RULES.at("char"));
int min_len = schema.contains("minLength") ? schema["minLength"].get<int>() : 0;
int max_len = schema.contains("maxLength") ? schema["maxLength"].get<int>() : std::numeric_limits<int>::max();
int min_len = _schema_count(schema, "minLength", 0, name);
int max_len = _schema_count(schema, "maxLength", std::numeric_limits<int>::max(), name);
return _add_rule(rule_name, "\"\\\"\" " + build_repetition(char_rule, min_len, max_len) + " \"\\\"\" space");
}
if (schema_type == "integer" && (schema.contains("minimum") || schema.contains("exclusiveMinimum") || schema.contains("maximum") || schema.contains("exclusiveMaximum"))) {
@@ -0,0 +1,54 @@
diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp
index 95640fb..0bf2b72 100644
--- a/common/json-schema-to-grammar.cpp
+++ b/common/json-schema-to-grammar.cpp
@@ -904,6 +904,26 @@ public:
// which is how TypeBox's Type.Never and Arktype's "never" are exported, the latter as an
// optional property no call may set. A `not` of the empty schema makes its whole schema
// unsatisfiable whatever sits beside it, so the sibling keywords do not matter.
+ // RADIANCE: a count -- minLength, maxLength, minItems, maxItems -- as the int build_repetition
+ // takes, or `dflt` where the schema has none. get<int>() casts, so a bound past INT_MAX wrapped:
+ // Number.MAX_SAFE_INTEGER, a common way to write "no limit", came out as -1 and the grammar did
+ // not parse. A count at or past INT_MAX is INT_MAX, which build_repetition reads as no maximum;
+ // no context the engine serves holds a string or an array that long, so the bound changes
+ // nothing a generation can write. A count that is not a non-negative number is refused.
+ int _schema_count(const json & schema, const char * key, int dflt, const std::string & name) {
+ if (!schema.contains(key)) {
+ return dflt;
+ }
+ const json & v = schema[key];
+ const double d = v.is_number() ? v.get<double>() : -1.0;
+ if (!(d >= 0)) {
+ _errors.push_back(std::string(key) + " of \"" + (name.empty() ? std::string("root") : name) +
+ "\" must be a non-negative number");
+ return dflt;
+ }
+ return d >= (double) std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : (int) d;
+ }
+
static bool is_unsatisfiable(const json & schema) {
if (schema.is_boolean()) return !schema.get<bool>();
if (!schema.is_object() || !schema.contains("not")) return false;
@@ -1059,9 +1079,8 @@ public:
return _add_rule(rule_name, rule);
}
std::string item_rule_name = visit(items, name + (name.empty() ? "" : "-") + "item");
- int min_items = schema.contains("minItems") ? schema["minItems"].get<int>() : 0;
- json max_items_json = schema.contains("maxItems") ? schema["maxItems"] : json();
- int max_items = max_items_json.is_number_integer() ? max_items_json.get<int>() : std::numeric_limits<int>::max();
+ int min_items = _schema_count(schema, "minItems", 0, name);
+ int max_items = _schema_count(schema, "maxItems", std::numeric_limits<int>::max(), name);
return _add_rule(rule_name, "\"[\" space " + build_repetition(item_rule_name, min_items, max_items, "\",\" space") + " \"]\" space");
}
@@ -1077,8 +1096,8 @@ public:
}
if (schema_type == "string" && (schema.contains("minLength") || schema.contains("maxLength"))) {
std::string char_rule = _add_primitive("char", PRIMITIVE_RULES.at("char"));
- int min_len = schema.contains("minLength") ? schema["minLength"].get<int>() : 0;
- int max_len = schema.contains("maxLength") ? schema["maxLength"].get<int>() : std::numeric_limits<int>::max();
+ int min_len = _schema_count(schema, "minLength", 0, name);
+ int max_len = _schema_count(schema, "maxLength", std::numeric_limits<int>::max(), name);
return _add_rule(rule_name, "\"\\\"\" " + build_repetition(char_rule, min_len, max_len) + " \"\\\"\" space");
}
if (schema_type == "integer" && (schema.contains("minimum") || schema.contains("exclusiveMinimum") || schema.contains("maximum") || schema.contains("exclusiveMaximum"))) {