From b9ecbde0d6af6c8b134ad8304862f76ecbdeacbe Mon Sep 17 00:00:00 2001 From: fl0l0u Date: Sun, 15 Nov 2026 19:27:57 +0000 Subject: [PATCH] util: one wording for the bounded read's refusal The refuse sites that read a container file all re-typed the same two-branch message -- over ? "carries far too many values" : "nests far too deep" -- six times, and the copies had already drifted: gen_config said "carries far too many JSON values", xstats "far too many values", tokenizer_config "it does not parse" for the discarded case. The bounds themselves (2^24, 256) were also spelled as literals in tokenizer_json's refusal, so a bound change there would make its message a lie. Json::bound_why(over, deep) is the phrase, owned beside the read that sets the flags, and the two bounds are exported as kContainerBudget / kContainerDepth so the one message that states them reads them from the constants. Every refuse site now calls it; the wording is one, the numbers cannot drift. No behaviour change -- the same flags map to the same failure paths, only the text they print is unified. --- core/gen_config.cpp | 6 ++---- core/lora/lora.cpp | 3 +-- core/place/xstats.cpp | 2 +- core/text/tokenizer_json.cpp | 11 +++++------ core/util/json.cpp | 8 +++++++- core/util/json.h | 7 +++++++ 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/core/gen_config.cpp b/core/gen_config.cpp index fab1bdd6..d2056c8a 100644 --- a/core/gen_config.cpp +++ b/core/gen_config.cpp @@ -99,8 +99,7 @@ bool gen_sampling_parse(const std::string& json_text, const char* src, GenSampli bool over = false, deep = false; Json j = Json::parse_container(json_text, &over, &deep); if (over || deep) { - RAD_WARN("%s: %s; ignoring it", src, - over ? "carries far too many JSON values" : "nests far too deep"); + RAD_WARN("%s: %s; ignoring it", src, Json::bound_why(over, deep)); return false; } if (j.is_discarded()) { RAD_WARN("%s: not valid JSON; ignoring it", src); return false; } @@ -222,8 +221,7 @@ bool gen_sampling_from_kv(const std::function& get, Ge if (over || deep) RAD_WARN("the container states generation.%s as \"%s\", which is not a JSON " "array of ids (%s); ignoring it", - k, log_sanitize(v).c_str(), - over ? "it carries far too many values" : "it nests far too deep"); + k, log_sanitize(v).c_str(), Json::bound_why(over, deep)); else if (!w.is_discarded()) j[k] = std::move(w); else RAD_WARN("the container states generation.%s as \"%s\", which is not a JSON " diff --git a/core/lora/lora.cpp b/core/lora/lora.cpp index 12f78237..f574d697 100644 --- a/core/lora/lora.cpp +++ b/core/lora/lora.cpp @@ -815,8 +815,7 @@ int load_adapter(const std::string& dir, const std::vector& sites, int max bool over = false, deep = false; const ojson cfg = ojson::parse_container(text, &over, &deep); if (over || deep) - return fail(err, RAD_E_FORMAT, - cfg_path + (over ? ": carries far too many JSON values" : ": nests far too deep")); + return fail(err, RAD_E_FORMAT, cfg_path + ": " + ojson::bound_why(over, deep)); if (cfg.is_discarded() || !cfg.is_object()) return fail(err, RAD_E_FORMAT, cfg_path + ": not a JSON object"); auto truthy = [&](const char* k) { diff --git a/core/place/xstats.cpp b/core/place/xstats.cpp index 9eb62e11..e78cdbfa 100644 --- a/core/place/xstats.cpp +++ b/core/place/xstats.cpp @@ -541,7 +541,7 @@ int xs_profile_load(const std::string& path, std::vector* out, i Json j = Json::parse_container(text, &over, &deep); if (over || deep || j.is_discarded()) { RAD_ERR("--expert-profile: %s is not JSON (%s)", path.c_str(), - over ? "far too many values" : deep ? "nests far too deep" : "malformed"); + Json::bound_why(over, deep)); return RAD_E_INVAL; } if (!j.is_object() || j.value("format", "") != "radiance-expert-stats/1" || diff --git a/core/text/tokenizer_json.cpp b/core/text/tokenizer_json.cpp index 43625137..8141fb67 100644 --- a/core/text/tokenizer_json.cpp +++ b/core/text/tokenizer_json.cpp @@ -587,9 +587,7 @@ void read_config_json(const std::string& dir, VocabBuild& out) { json c = json::parse_container(text, &over, &deep); if (over || deep || c.is_discarded()) { RAD_WARN("tokenizer: %s did not read as a bounded JSON document (%s); ids left unset", - path.c_str(), - over ? "it carries far too many values" - : deep ? "it nests far too deep" : "it does not parse"); + path.c_str(), json::bound_why(over, deep)); return; } std::unordered_map id_of; @@ -793,9 +791,10 @@ int parse_tokenizer_json_buf(std::string_view text, const char* origin, VocabBui bool over = false, deep = false; json j = json::parse_container(text, &over, &deep); if (over || deep) { - RAD_ERR("%s: %s; a tokenizer file may not carry more than 2^24 values or nest more " - "than 256 levels", - origin, over ? "carries far too many values" : "nests far too deep"); + RAD_ERR("%s: %s; a tokenizer file may not carry more than %lld values or nest more " + "than %d levels", + origin, json::bound_why(over, deep), + (long long)json::kContainerBudget, json::kContainerDepth); return RAD_E_FORMAT; } if (j.is_discarded()) { diff --git a/core/util/json.cpp b/core/util/json.cpp index ab85a91f..ac09fd0b 100644 --- a/core/util/json.cpp +++ b/core/util/json.cpp @@ -251,12 +251,18 @@ Json Json::parse_bounded(std::string_view text, int64_t budget, int max_depth, b Json Json::parse_container(std::string_view text, bool* over, bool* deep) { bool o = false, d = false; - Json j = parse_bounded(text, 1LL << 24, 256, &o, &d); + Json j = parse_bounded(text, kContainerBudget, kContainerDepth, &o, &d); if (over) *over = o; if (deep) *deep = d; return j; } +const char* Json::bound_why(bool over, bool deep) { + return over ? "carries far too many values" + : deep ? "nests far too deep" + : "is not valid JSON"; +} + Json Json::parse(std::string_view text) { return parse(text, nullptr, true, false); } Json Json::parse(std::istream& in) { diff --git a/core/util/json.h b/core/util/json.h index fec03274..7cdb950b 100644 --- a/core/util/json.h +++ b/core/util/json.h @@ -901,6 +901,13 @@ public: * to overflow a dump's or a destructor's stack on the way there. */ static Json parse_container(std::string_view text, bool* over = nullptr, bool* deep = nullptr); + /* The refusal message behind `over`/`deep`: one wording for all callers, so the bound and + * the phrase that names it live together instead of being re-typed at each refuse site + * (six near-copies had drifted). The phrase for whichever bound tripped. */ + static const char* bound_why(bool over, bool deep); + /* The container read's own bounds, for a caller that states them in its refusal. */ + static constexpr int64_t kContainerBudget = 1LL << 24; + static constexpr int kContainerDepth = 256; private: using difference_type = std::ptrdiff_t;