From 4026bd380d062ae500e69added60ba0efef59cea Mon Sep 17 00:00:00 2001 From: breken Date: Thu, 24 Sep 2026 21:36:51 -0700 Subject: [PATCH] fix(engine): decode JSON escapes in stop_sequences MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parse_inference_options_json copied each stop sequence verbatim from the first quote to the next one, so an escaped character was never decoded: "\n" became a literal backslash and n, "é" stayed six ASCII bytes, and "\"" split the string. The stop never matched and generation ran to max_tokens. When the escaped quotes were odd in number and stop_sequences was the last option (the order python/cactus/server.py builds), the scan wrapped to the start of the JSON and appended forever: the call hangs and grows memory without bound (9.3 GB in 10 s). Read the array with parse_json_string_array_field, which the custom vocabulary option already uses and which decodes escapes. Signed-off-by: breken --- cactus-engine/src/utils.h | 18 +-------------- .../tests/test_inference_options.cpp | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 17 deletions(-) create mode 100644 cactus-engine/tests/test_inference_options.cpp diff --git a/cactus-engine/src/utils.h b/cactus-engine/src/utils.h index 5e84e6850..ba7a534a8 100644 --- a/cactus-engine/src/utils.h +++ b/cactus-engine/src/utils.h @@ -1699,23 +1699,7 @@ inline InferenceOptions parse_inference_options_json(const std::string& json) { options.enable_thinking_if_supported = (json.substr(pos, 4) == "true"); } - pos = json.find("\"stop_sequences\""); - if (pos != std::string::npos) { - pos = json.find('[', pos); - if (pos != std::string::npos) { - size_t end_pos = json.find(']', pos); - size_t seq_pos = json.find('"', pos); - - while (seq_pos != std::string::npos && seq_pos < end_pos) { - size_t seq_start = seq_pos + 1; - size_t seq_end = json.find('"', seq_start); - if (seq_end != std::string::npos) { - options.stop_sequences.push_back(json.substr(seq_start, seq_end - seq_start)); - } - seq_pos = json.find('"', seq_end + 1); - } - } - } + options.stop_sequences = parse_json_string_array_field(json, "stop_sequences"); return options; } diff --git a/cactus-engine/tests/test_inference_options.cpp b/cactus-engine/tests/test_inference_options.cpp new file mode 100644 index 000000000..08a4907e5 --- /dev/null +++ b/cactus-engine/tests/test_inference_options.cpp @@ -0,0 +1,23 @@ +#include "test_utils.h" +#include "../src/utils.h" + +using cactus::ffi::parse_inference_options_json; + +bool test_stop_sequences_decode_json_escapes() { + auto options = parse_inference_options_json( + R"({"stop_sequences": ["\n\n", "C:\\", "caf\u00e9", "\"\""], "max_tokens": 64})"); + return options.stop_sequences == std::vector{"\n\n", "C:\\", "caf\xc3\xa9", "\"\""}; +} + +bool test_stop_sequence_with_quote_as_last_option() { + auto options = parse_inference_options_json(R"({"max_tokens": 64, "stop_sequences": ["\""]})"); + return options.stop_sequences == std::vector{"\""}; +} + +int main() { + TestUtils::TestRunner runner("Inference Options Tests"); + runner.run_test("stop_sequences_decode_json_escapes", test_stop_sequences_decode_json_escapes()); + runner.run_test("stop_sequence_with_quote_as_last_option", test_stop_sequence_with_quote_as_last_option()); + runner.print_summary(); + return runner.all_passed() ? 0 : 1; +}