// Unit tests for Router-backed ClassifierServices wiring (#2384). #include "lemon/backends/onnxruntime/ort_server_health.h" #include "lemon/error_types.h" #include "lemon/routing_classifier_services.h" #include #include #include #include #include #include #include #include #include using lemon::ClassifierContext; using lemon::ClassifierPtr; using lemon::Decision; using lemon::MatchExpr; using lemon::ModelType; using lemon::RouteContext; using lemon::RoutePolicy; using lemon::RoutingPolicyEngine; using lemon::Rule; using lemon::Score; using lemon::json; static int g_failures = 0; static void check(const char* name, bool ok) { std::printf("[%s] %s\n", ok ? "PASS" : "FAIL", name); if (!ok) ++g_failures; } static bool near(double a, double b, double eps = 1e-9) { return std::fabs(a - b) <= eps; } static RouteContext route_context(const std::string& input) { RouteContext ctx; ctx.input = input; ctx.params.chars = input.size(); return ctx; } static MatchExpr leaf(json value) { MatchExpr expr; expr.op = MatchExpr::Op::Leaf; expr.leaf = std::move(value); return expr; } static Rule rule(const std::string& id, MatchExpr match, const std::string& route_to) { Rule out; out.id = id; out.match = std::move(match); out.route_to = route_to; return out; } static ClassifierPtr make_semantic_classifier() { return lemon::make_classifier(json{ {"id", "topic"}, {"type", "semantic_similarity"}, {"model", "embedder"}, {"reference_phrases", { {"coding", {"write code"}}, {"math", {"integral"}}, }}, }); } static ClassifierPtr make_model_classifier() { return lemon::make_classifier(json{ {"id", "pii"}, {"type", "classifier"}, {"model", "pii-model"}, {"labels", {"PII", "NO_PII"}}, {"default_label", "PII"}, }); } static ClassifierPtr make_fail_closed_model_classifier() { return lemon::make_classifier(json{ {"id", "pii"}, {"type", "classifier"}, {"model", "pii-model"}, {"labels", {"PII", "NO_PII"}}, {"default_label", "PII"}, {"on_error", "match_true"}, }); } static void test_embed_uses_router_embeddings_shape() { std::vector loaded; json seen_request; auto services = lemon::make_classifier_services_from_router_calls( [&](const json& request) { seen_request = request; return json{{"data", json::array({json{{"embedding", {1.0, 2.0, 3.0}}}})}}; }, [](const json&) { return json::object(); }, [&](const std::string& model) { loaded.push_back(model); }); auto vec = services.embed("embedder", "hello"); check("embed ensure_loaded is called", loaded == std::vector{"embedder"}); check("embed forwards model and input", seen_request.value("model", "") == "embedder" && seen_request.value("input", "") == "hello"); check("embed parses OpenAI embedding response", vec.size() == 3 && vec[0] == 1.0f && vec[2] == 3.0f); } static void test_semantic_similarity_loops_through_router_embeddings() { std::map> vectors = { {"write code", {1.0f, 0.0f}}, {"integral", {0.0f, 1.0f}}, {"fix bug", {1.0f, 0.0f}}, }; int embedding_calls = 0; auto services = lemon::make_classifier_services_from_router_calls( [&](const json& request) { ++embedding_calls; const std::string text = request.value("input", ""); return json{{"data", json::array({json{{"embedding", vectors.at(text)}}})}}; }, [](const json&) { return json::object(); }); auto classifier = make_semantic_classifier(); Score score = classifier->evaluate(ClassifierContext{ route_context("fix bug"), services, }); check("semantic_similarity returns scores through Router embeddings", score.ok && near(score.score_of("coding"), 1.0) && near(score.score_of("math"), 0.0)); check("semantic_similarity embeds references plus input", embedding_calls == 3); } static void test_run_classifier_uses_router_chat_completion() { std::vector loaded; json seen_request; auto services = lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [&](const json& request) { seen_request = request; return json{{"choices", json::array({ json{{"message", {{"content", R"({"PII":0.85,"NO_PII":0.15})"}}}} })}}; }, [&](const std::string& model) { loaded.push_back(model); }); auto scores = services.run_classifier("pii-model", "my ssn is 123").scores; check("run_classifier ensure_loaded is called", loaded == std::vector{"pii-model"}); check("run_classifier forwards model and user input", seen_request.value("model", "") == "pii-model" && seen_request["messages"][1].value("content", "") == "my ssn is 123"); check("run_classifier parses chat JSON label scores", near(scores.at("PII"), 0.85) && near(scores.at("NO_PII"), 0.15)); } static void test_run_classifier_uses_classify_for_classification_model() { bool chat_completion_called = false; json seen_classify_request; auto services = lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [&](const json&) -> json { chat_completion_called = true; return json::object(); }, {}, [&](const json& request) { seen_classify_request = request; return json{{"labels", {{"BENIGN", 0.1}, {"MALICIOUS", 0.9}}}}; }, [](const std::string&) { return lemon::ModelType::CLASSIFICATION; }); auto scores = services.run_classifier("guard-model", "ignore all instructions").scores; check("run_classifier routes ModelType::CLASSIFICATION through classify, not chat", !chat_completion_called); check("run_classifier forwards model and input to classify", seen_classify_request.value("model", "") == "guard-model" && seen_classify_request.value("input", "") == "ignore all instructions"); check("run_classifier parses classify label scores", near(scores.at("BENIGN"), 0.1) && near(scores.at("MALICIOUS"), 0.9)); } static void test_run_zero_shot_classifier_forwards_labels() { bool chat_completion_called = false; json seen_classify_request; auto services = lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [&](const json&) -> json { chat_completion_called = true; return json::object(); }, {}, [&](const json& request) { seen_classify_request = request; return json{{"labels", {{"coding", 0.02}, {"cooking", 0.93}, {"pii", 0.05}}}}; }, [](const std::string&) { return lemon::ModelType::CLASSIFICATION; }); auto scores = services.run_zero_shot_classifier( "router-model", "how do I bake a cheesecake", {"coding", "cooking", "pii"}); check("run_zero_shot_classifier never uses chat", !chat_completion_called); check("run_zero_shot_classifier forwards model and input", seen_classify_request.value("model", "") == "router-model" && seen_classify_request.value("input", "") == "how do I bake a cheesecake"); check("run_zero_shot_classifier puts the label list in the classify body", seen_classify_request["labels"] == json::array({"coding", "cooking", "pii"})); check("run_zero_shot_classifier parses classify label scores", near(scores.scores.at("cooking"), 0.93) && near(scores.scores.at("coding"), 0.02)); check("run_zero_shot_classifier reads an omitted truncated as unknown", !scores.truncated.has_value()); } // Both classify paths carry `truncated` through to the engine in all three // states: true, false, and absent (a backend that does not report cuts). static void test_classify_paths_parse_truncated() { json truncated = true; auto services = lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [](const json&) { return json::object(); }, {}, [&](const json&) { json response = {{"labels", {{"PII", 0.2}, {"NO_PII", 0.8}}}}; if (!truncated.is_null()) response["truncated"] = truncated; return response; }, [](const std::string&) { return lemon::ModelType::CLASSIFICATION; }); for (const json& value : {json(true), json(false), json()}) { truncated = value; const std::optional want = value.is_null() ? std::nullopt : std::optional(value.get()); const std::string name = value.is_null() ? "absent" : value.dump(); check(("run_classifier parses truncated: " + name).c_str(), services.run_classifier("pii-model", "text").truncated == want); check(("run_zero_shot_classifier parses truncated: " + name).c_str(), services.run_zero_shot_classifier("pii-model", "text", {"PII", "NO_PII"}) .truncated == want); } } // The version rule OnnxRuntimeServer applies to ort-server's /health body. static void test_ort_server_reports_truncation_from_0_4_0() { using lemon::backends::onnxruntime::reports_truncation; check("0.4.0 reports truncation", reports_truncation(json{{"version", "0.4.0"}})); check("0.10.0 compares numerically, not as text", reports_truncation(json{{"version", "0.10.0"}})); check("1.0.0 reports truncation", reports_truncation(json{{"version", "1.0.0"}})); check("0.3.9 does not", !reports_truncation(json{{"version", "0.3.9"}})); check("0.3.7's /health, which carries no version, does not", !reports_truncation(json{{"status", "ok"}, {"onnxruntime", "1.27.0"}})); check("an unparseable version does not", !reports_truncation(json{{"version", "dev"}})); check("a non-string version does not", !reports_truncation(json{{"version", 4}})); check("an error body does not", !reports_truncation(json{{"error", "not loaded"}})); } static void test_run_zero_shot_classifier_loads_before_resolving_type() { // Same cold-start ordering requirement as run_classifier: the resolver // reports LLM until the backend is alive. bool loaded = false; bool classify_called = false; auto services = lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [](const json&) { return json::object(); }, [&](const std::string&) { loaded = true; }, [&](const json&) { classify_called = true; return json{{"labels", {{"a", 0.3}, {"b", 0.7}}}}; }, [&](const std::string&) { return loaded ? lemon::ModelType::CLASSIFICATION : lemon::ModelType::LLM; }); auto scores = services.run_zero_shot_classifier("router-model", "text", {"a", "b"}); check("run_zero_shot_classifier loads the model before resolving its type", loaded && classify_called && near(scores.scores.at("b"), 0.7)); } static void test_run_zero_shot_classifier_rejects_non_classification_model() { bool chat_called = false; bool classify_called = false; auto services = lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [&](const json&) -> json { chat_called = true; return json::object(); }, {}, [&](const json&) -> json { classify_called = true; return json::object(); }, [](const std::string&) { return lemon::ModelType::LLM; }); bool threw = false; try { services.run_zero_shot_classifier("chat-model", "text", {"a", "b"}); } catch (const std::exception&) { threw = true; } check("run_zero_shot_classifier errors instead of falling back to chat", threw && !chat_called && !classify_called); } static lemon::ClassifierServices systemone_services( std::function systemone, lemon::ModelType type, bool* loaded = nullptr) { return lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [](const json&) { return json::object(); }, [loaded](const std::string&) { if (loaded) *loaded = true; }, [](const json&) { return json::object(); }, // Like Router::get_model_type on a cold start: LLM until the model loads. [type, loaded](const std::string&) { return (loaded == nullptr || *loaded) ? type : lemon::ModelType::LLM; }, std::move(systemone)); } static void test_run_systemone_sends_one_question_and_returns_its_answer() { // Not alphabetical on purpose: the model scores options by position. const nlohmann::ordered_json question = { {"type", "choice"}, {"instructions", "Which team?"}, {"criteria", {{"shipping", nullptr}, {"billing", nullptr}}}, }; nlohmann::ordered_json seen; bool loaded = false; auto services = systemone_services( [&](const nlohmann::ordered_json& request) { seen = request; return json{{"model", "decider"}, {"answers", {{"question", {{"type", "choice"}, {"probabilities", {{"billing", 0.8}, {"shipping", 0.2}}}}}}}}; }, lemon::ModelType::CLASSIFICATION, &loaded); json answer = services.run_systemone("decider", "my SSN is 123-45-6789", question); check("run_systemone loads the model", loaded); check("run_systemone sends the model and the input as the state", seen.value("model", "") == "decider" && seen.value("state", "") == "my SSN is 123-45-6789"); check("run_systemone sends exactly one question, options in their order", seen["questions"].size() == 1 && seen["questions"]["question"].dump() == question.dump()); check("run_systemone returns the answer to that question", answer["probabilities"].value("billing", 0.0) == 0.8); } static void test_run_systemone_rejects_non_classification_model() { bool systemone_called = false; auto services = systemone_services( [&](const nlohmann::ordered_json&) { systemone_called = true; return json::object(); }, lemon::ModelType::LLM); bool threw = false; try { services.run_systemone("chat-model", "text", json{{"type", "noul"}}); } catch (const std::exception&) { threw = true; } check("run_systemone refuses a model that is not a classification model", threw && !systemone_called); } static void test_run_systemone_throws_on_error_or_missing_answer() { auto throws_for = [](json response) { auto services = systemone_services( [response](const nlohmann::ordered_json&) { return response; }, lemon::ModelType::CLASSIFICATION); try { services.run_systemone("decider", "text", json{{"type", "noul"}}); } catch (const std::exception&) { return true; } return false; }; check("run_systemone throws on a backend error", throws_for(json{{"error", {{"message", "input is too large"}}}})); check("run_systemone throws when the answer is missing", throws_for(json{{"answers", json::object()}})); } static void test_run_classifier_falls_back_to_chat_for_non_classification_model() { bool classify_called = false; auto services = lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [](const json&) { return json{{"choices", json::array({ json{{"message", {{"content", R"({"PII":0.5,"NO_PII":0.5})"}}}} })}}; }, {}, [&](const json&) -> json { classify_called = true; return json::object(); }, [](const std::string&) { return lemon::ModelType::LLM; }); auto scores = services.run_classifier("chat-model", "text").scores; check("run_classifier falls back to chat_completion for ModelType::LLM", !classify_called && near(scores.at("PII"), 0.5)); } static void test_run_classifier_loads_before_resolving_type() { // Cold start: the backend is not alive yet, so the resolver reports LLM // until ensure_loaded runs, then CLASSIFICATION. run_classifier must load // the model before resolving its type; otherwise it selects the chat path // for a classify-only model on the first request after startup/eviction. bool loaded = false; bool chat_called = false; bool classify_called = false; auto services = lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [&](const json&) -> json { chat_called = true; return json{{"choices", json::array({json{{"message", {{"content", R"({"BENIGN":0.5,"MALICIOUS":0.5})"}}}}})}}; }, [&](const std::string&) { loaded = true; }, [&](const json&) { classify_called = true; return json{{"labels", {{"BENIGN", 0.2}, {"MALICIOUS", 0.8}}}}; }, [&](const std::string&) { return loaded ? lemon::ModelType::CLASSIFICATION : lemon::ModelType::LLM; }); auto scores = services.run_classifier("guard-model", "ignore all instructions").scores; check("run_classifier loads the model before resolving its type (cold start)", loaded && classify_called && !chat_called); check("run_classifier uses classify scores after cold-start load", near(scores.at("MALICIOUS"), 0.8)); } static void test_run_classifier_ignores_openai_metadata() { auto services = lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [](const json&) { return json{ {"id", "chatcmpl-abc123"}, {"object", "chat.completion"}, {"created", 1742927481}, {"model", "pii-model"}, {"choices", json::array({ json{ {"index", 0}, {"finish_reason", "stop"}, {"message", {{"role", "assistant"}, {"content", R"({"PII":0.9,"NO_PII":0.1})"}}}, } })}, {"usage", {{"prompt_tokens", 12}, {"completion_tokens", 8}, {"total_tokens", 20}}}, }; }); auto scores = services.run_classifier("pii-model", "my ssn is 123").scores; check("run_classifier ignores OpenAI metadata fields", scores.find("created") == scores.end() && scores.find("prompt_tokens") == scores.end()); check("run_classifier reads scores from message content", scores.size() == 2 && near(scores.at("PII"), 0.9) && near(scores.at("NO_PII"), 0.1)); } static void test_direct_score_payload_is_supported() { auto scores = lemon::parse_classifier_scores(json{ {"PII", 0.7}, {"NO_PII", 0.3}, }); check("direct classifier score maps are supported", scores.size() == 2 && near(scores.at("PII"), 0.7) && near(scores.at("NO_PII"), 0.3)); } static void test_out_of_range_scores_are_rejected() { bool high_threw = false; try { lemon::parse_classifier_scores(json{{"PII", 999.0}}); } catch (const std::runtime_error&) { high_threw = true; } check("classifier scores above 1 are rejected", high_threw); bool negative_threw = false; try { lemon::parse_classifier_scores(json{{"choices", json::array({ json{{"message", {{"content", R"({"PII":-1.0})"}}}} })}}); } catch (const std::runtime_error&) { negative_threw = true; } check("classifier scores below 0 are rejected", negative_threw); } static void test_out_of_range_scores_drive_on_error() { auto services = lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [](const json&) { return json{{"choices", json::array({ json{{"message", {{"content", R"({"PII":999.0})"}}}} })}}; }); RoutePolicy policy; policy.candidates = {"local", "cloud"}; policy.default_model = "cloud"; policy.classifiers["pii"] = make_fail_closed_model_classifier(); policy.rules = { rule("keep-private", leaf(json{{"classifier", "pii"}, {"min_score", 0.5}}), "local"), }; RoutingPolicyEngine engine(std::move(policy), services); Decision decision = engine.route(route_context("my ssn is 123"), false); check("out-of-range model scores become classifier failure and apply on_error", decision.route_to == "local" && decision.matched_rule == "keep-private"); } // A residency conflict must escape the whole engine, not just the classifier. // Each classifier sits behind two catch-alls that exist to turn backend // trouble into on_error (ClassifierBandCondition::score_for) and then into a // fail-open default (RoutingPolicyEngine::route). Both would otherwise report // a successful route and hide a conflict only the caller can resolve, so these // drive the engine end to end rather than calling evaluate() directly. static bool route_propagates_residency_conflict(const ClassifierPtr& classifier, lemon::ClassifierServices services) { RoutePolicy policy; policy.candidates = {"local", "cloud"}; policy.default_model = "cloud"; policy.classifiers["probe"] = classifier; policy.rules = { rule("keep-private", leaf(json{{"classifier", "probe"}, {"min_score", 0.5}}), "local"), }; RoutingPolicyEngine engine(std::move(policy), std::move(services)); try { (void)engine.route(route_context("my ssn is 123"), false); } catch (const lemon::RouterResidencyConflictException&) { return true; } catch (...) { return false; } return false; } static lemon::RouterResidencyConflictException residency_conflict() { return lemon::RouterResidencyConflictException("probe-model", "resident-model", "the recipe requires exclusive NPU access"); } static void test_zero_shot_residency_conflict_propagates_through_route() { lemon::ClassifierServices services; services.run_zero_shot_classifier = [](const std::string&, const std::string&, const std::vector&) -> lemon::ClassifierScores { throw residency_conflict(); }; // on_error=match_true would otherwise swallow the conflict into a confident // match on "local", which is exactly the silent success being guarded here. ClassifierPtr zero_shot = lemon::make_classifier(json{ {"id", "probe"}, {"type", "zero_shot"}, {"model", "probe-model"}, {"labels", {"PII", "NO_PII"}}, {"default_label", "PII"}, {"on_error", "match_true"}, }); check("zero_shot: residency conflict propagates through engine.route", route_propagates_residency_conflict(zero_shot, std::move(services))); } static void test_systemone_residency_conflict_propagates_through_route() { lemon::ClassifierServices services; services.run_systemone = [](const std::string&, const std::string&, const nlohmann::ordered_json&) -> json { throw residency_conflict(); }; ClassifierPtr systemone = lemon::make_classifier(json{ {"id", "probe"}, {"type", "systemone"}, {"model", "probe-model"}, {"question", {{"type", "noul"}, {"instructions", "Does the message contain personal data?"}}}, {"default_label", "true"}, {"on_error", "match_true"}, }); check("systemone: residency conflict propagates through engine.route", route_propagates_residency_conflict(systemone, std::move(services))); } static void test_classifier_residency_conflict_propagates_through_route() { lemon::ClassifierServices services; services.run_classifier = [](const std::string&, const std::string&) -> lemon::ClassifierScores { throw residency_conflict(); }; check("classifier: residency conflict propagates through engine.route", route_propagates_residency_conflict(make_fail_closed_model_classifier(), std::move(services))); } static void test_llm_residency_conflict_propagates_through_route() { lemon::ClassifierServices services; services.chat = [](const std::string&, const std::string&, const std::string&) -> std::string { throw residency_conflict(); }; ClassifierPtr llm = lemon::make_classifier(json{ {"id", "probe"}, {"type", "llm"}, {"model", "probe-model"}, {"prompt", "Pick a model."}, {"labels", {"local", "cloud"}}, {"default_label", "local"}, {"on_error", "match_true"}, }); check("llm: residency conflict propagates through engine.route", route_propagates_residency_conflict(llm, std::move(services))); } static void test_semantic_similarity_residency_conflict_propagates_through_route() { lemon::ClassifierServices services; services.embed = [](const std::string&, const std::string&) -> std::vector { throw residency_conflict(); }; ClassifierPtr semantic = lemon::make_classifier(json{ {"id", "probe"}, {"type", "semantic_similarity"}, {"model", "probe-model"}, {"reference_phrases", {{"PII", {"my social security number"}}}}, {"default_label", "PII"}, {"on_error", "match_true"}, }); check("semantic_similarity: residency conflict propagates through engine.route", route_propagates_residency_conflict(semantic, std::move(services))); } // The guard must stay narrow: an ordinary backend failure still becomes // on_error, so the re-throws above cannot be widened into "classifiers throw". static void test_ordinary_backend_failure_still_applies_on_error() { lemon::ClassifierServices services; services.run_zero_shot_classifier = [](const std::string&, const std::string&, const std::vector&) -> lemon::ClassifierScores { throw std::runtime_error("backend down"); }; RoutePolicy policy; policy.candidates = {"local", "cloud"}; policy.default_model = "cloud"; policy.classifiers["probe"] = lemon::make_classifier(json{ {"id", "probe"}, {"type", "zero_shot"}, {"model", "probe-model"}, {"labels", {"PII", "NO_PII"}}, {"default_label", "PII"}, {"on_error", "match_true"}, }); policy.rules = { rule("keep-private", leaf(json{{"classifier", "probe"}, {"min_score", 0.5}}), "local"), }; RoutingPolicyEngine engine(std::move(policy), std::move(services)); Decision decision = engine.route(route_context("my ssn is 123"), false); check("zero_shot: a non-residency backend failure still applies on_error", decision.route_to == "local" && decision.matched_rule == "keep-private"); } static void test_model_classifier_routes_with_router_services() { auto services = lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [](const json&) { return json{{"choices", json::array({ json{{"message", {{"content", R"({"PII":0.9,"NO_PII":0.1})"}}}} })}}; }); RoutePolicy policy; policy.candidates = {"local", "cloud"}; policy.default_model = "cloud"; policy.classifiers["pii"] = make_model_classifier(); policy.rules = { rule("keep-private", leaf(json{{"classifier", "pii"}, {"min_score", 0.5}}), "local"), }; RoutingPolicyEngine engine(std::move(policy), services); Decision decision = engine.route(route_context("my ssn is 123"), true); check("model-backed classifier routes through Router services", decision.route_to == "local" && decision.matched_rule == "keep-private"); check("model-backed classifier trace carries score", decision.trace.size() == 1 && decision.trace[0].score.has_value() && near(*decision.trace[0].score, 0.9)); } static void test_chat_service_extracts_text() { auto services = lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [](const json& request) { check("chat forwards system prompt", request["messages"][0].value("content", "") == "route this"); return json{{"choices", json::array({ json{{"message", {{"content", "large-model"}}}} })}}; }); check("chat extracts assistant content", services.chat("router-model", "route this", "hard problem") == "large-model"); } // #2405 review: the router-model invocation must be a deliberately constrained // classifier call — thinking disabled through the same cross-backend // normalization as normal Lemonade requests (so a Qwen3-style router does not // emit blocks or spend unbounded time reasoning), and output tightly // bounded. These assertions capture the exact request handed to // Router::chat_completion. static void test_chat_service_is_constrained_invocation() { json captured; auto services = lemon::make_classifier_services_from_router_calls( [](const json&) { return json::object(); }, [&captured](const json& request) { captured = request; return json{{"choices", json::array({ json{{"message", {{"content", "{\"model\":\"x\"}"}}}} })}}; }); services.chat("router-model", "route this", "hard problem"); check("chat sets stream=false", captured.value("stream", true) == false); check("chat sets temperature=0", captured.value("temperature", 1.0) == 0.0); check("chat bounds output with a tight max_tokens", captured.contains("max_tokens") && captured["max_tokens"].is_number_integer() && captured["max_tokens"].get() > 0 && captured["max_tokens"].get() <= 256); const json response_format = captured.value("response_format", json::object()); check("chat requests backend-neutral JSON object output", response_format == json{{"type", "json_object"}}); check("chat does not leak a backend-specific JSON-schema envelope", !response_format.contains("schema") && !response_format.contains("json_schema")); check("chat disables thinking via /no_think injection (cross-backend form)", captured["messages"][1].value("content", "").rfind("/no_think\n", 0) == 0); check("chat strips handled thinking fields before dispatch", !captured.contains("enable_thinking") && !captured.contains("thinking")); check("chat keeps the user input after the /no_think prefix", captured["messages"][1].value("content", "") == "/no_think\nhard problem"); check("chat system message carries the composed prompt untouched", captured["messages"][0].value("content", "") == "route this"); } // #2405 review: the llm router's history policy is "latest user turn only" // (the frozen v1 RouteContext contract). This pins that a multi-turn request // yields exactly the last user turn — earlier turns cannot affect the route // because they never reach the context, and therefore never reach the router. static void test_build_route_context_multi_turn_latest_user_turn_only() { json request = { {"model", "router"}, {"messages", json::array({ {{"role", "user"}, {"content", "first turn about databases"}}, {{"role", "assistant"}, {"content", "sure, here is a schema"}}, {{"role", "user"}, {"content", "now write a poem"}}, })}, }; RouteContext ctx = lemon::build_route_context(request, "router"); check("multi-turn: input is the latest user turn only", ctx.input == "now write a poem"); check("multi-turn: chars measures the latest turn, not the conversation", ctx.params.chars == std::string("now write a poem").size()); } static void test_build_route_context_chat_typed_parts_and_image() { json request = { {"model", "router"}, {"messages", json::array({ {{"role", "user"}, {"content", json::array({ {{"type", "text"}, {"text", "describe this"}}, {{"type", "image_url"}, {"image_url", {{"url", "data:image/png;base64,AAAA"}}}}, })}}, })}, }; RouteContext ctx = lemon::build_route_context(request, "router"); check("chat typed parts collect text", ctx.input == "describe this" && ctx.params.chars == ctx.input.size()); check("chat image_url part sets has_images", ctx.params.has_images); } static void test_build_route_context_responses_typed_input_message() { json request = { {"model", "router"}, {"input", json::array({ {{"role", "user"}, {"content", json::array({ {{"type", "input_text"}, {"text", "what is this"}}, {{"type", "input_image"}, {"image_url", "data:image/png;base64,AAAA"}}, })}}, })}, }; RouteContext ctx = lemon::build_route_context(request, "router"); check("responses input_text part collects text", ctx.input == "what is this"); check("responses input_image part sets has_images", ctx.params.has_images); } static void test_build_route_context_responses_bare_parts() { json request = { {"model", "router"}, {"input", json::array({ {{"type", "input_text"}, {"text", "hello"}}, {{"type", "input_image"}, {"image_url", "data:image/png;base64,AAAA"}}, })}, }; RouteContext ctx = lemon::build_route_context(request, "router"); check("responses bare input_text collects text", ctx.input == "hello"); check("responses bare input_image sets has_images", ctx.params.has_images); } static void test_build_route_context_responses_string_input_no_image() { json request = { {"model", "router"}, {"input", "plain text prompt"}, }; RouteContext ctx = lemon::build_route_context(request, "router"); check("responses string input collects text", ctx.input == "plain text prompt"); check("responses string input has no image", !ctx.params.has_images); } static void test_build_route_context_responses_uses_last_user_message() { json request = { {"model", "router"}, {"input", json::array({ {{"role", "assistant"}, {"content", json::array({ {{"type", "input_text"}, {"text", "here is some code"}}, })}}, {{"role", "user"}, {"content", json::array({ {{"type", "input_text"}, {"text", "thanks that helps"}}, })}}, })}, }; RouteContext ctx = lemon::build_route_context(request, "router"); check("responses uses only last user message, not earlier assistant turn", ctx.input == "thanks that helps"); } // total_chars is the whole-conversation counterpart of chars (#2958): every // text part the request carries, across every role, so a policy can route on // conversation size rather than on the size of the latest turn. static void test_build_route_context_total_chars_all_roles() { const std::string system_text = "you are a helpful assistant"; const std::string user_first = "first turn about databases"; const std::string assistant_text = "sure, here is a schema"; const std::string tool_text = "{\"rows\": 42}"; const std::string user_last = "go on"; json request = { {"model", "router"}, {"messages", json::array({ {{"role", "system"}, {"content", system_text}}, {{"role", "user"}, {"content", user_first}}, {{"role", "assistant"}, {"content", assistant_text}}, {{"role", "tool"}, {"content", tool_text}}, {{"role", "user"}, {"content", user_last}}, })}, }; RouteContext ctx = lemon::build_route_context(request, "router"); const std::size_t expected = system_text.size() + user_first.size() + assistant_text.size() + tool_text.size() + user_last.size(); check("total_chars sums text across every role", ctx.params.total_chars == expected); check("chars still measures the latest user turn only", ctx.params.chars == user_last.size()); check("a long history with a short last turn has total_chars > chars", ctx.params.total_chars > ctx.params.chars); } // Guards the short-circuit trap: the image scan stops at the first hit, so // totalling alongside it must not stop with it. static void test_build_route_context_total_chars_counts_past_first_image() { json request = { {"model", "router"}, {"messages", json::array({ {{"role", "user"}, {"content", json::array({ {{"type", "text"}, {"text", "describe this"}}, {{"type", "image_url"}, {"image_url", {{"url", "data:image/png;base64,AAAA"}}}}, })}}, {{"role", "assistant"}, {"content", "that is a cat"}}, {{"role", "user"}, {"content", "and this one?"}}, })}, }; RouteContext ctx = lemon::build_route_context(request, "router"); const std::size_t expected = std::string("describe this").size() + std::string("that is a cat").size() + std::string("and this one?").size(); check("total_chars counts messages after the first image", ctx.params.total_chars == expected); check("has_images still set when totalling shares the pass", ctx.params.has_images); } static void test_build_route_context_total_chars_utf8_bytes() { // "café" is 4 code points but 5 UTF-8 bytes; two messages => 10 bytes. json request = { {"model", "router"}, {"messages", json::array({ {{"role", "assistant"}, {"content", "caf\xC3\xA9"}}, {{"role", "user"}, {"content", "caf\xC3\xA9"}}, })}, }; RouteContext ctx = lemon::build_route_context(request, "router"); check("total_chars counts UTF-8 bytes, not code points", ctx.params.total_chars == 10); } // The legacy completions forms carry no history, so the total is the routing // input itself — total_chars == chars. static void test_build_route_context_total_chars_prompt_form() { json string_request = {{"model", "router"}, {"prompt", "plain text prompt"}}; RouteContext string_ctx = lemon::build_route_context(string_request, "router"); check("prompt string: total_chars == chars", string_ctx.params.total_chars == string_ctx.params.chars && string_ctx.params.total_chars == std::string("plain text prompt").size()); json array_request = { {"model", "router"}, {"prompt", json::array({"first part", "second part"})}, }; RouteContext array_ctx = lemon::build_route_context(array_request, "router"); check("prompt array: total_chars == chars", array_ctx.params.total_chars == array_ctx.params.chars && array_ctx.params.total_chars == std::string("first part\nsecond part").size()); json input_string_request = {{"model", "router"}, {"input", "plain text prompt"}}; RouteContext input_string_ctx = lemon::build_route_context(input_string_request, "router"); check("input string: total_chars == chars", input_string_ctx.params.total_chars == input_string_ctx.params.chars); } static void test_build_route_context_total_chars_responses_input_array() { json role_tagged = { {"model", "router"}, {"input", json::array({ {{"role", "assistant"}, {"content", json::array({ {{"type", "input_text"}, {"text", "here is some code"}}, })}}, {{"role", "user"}, {"content", json::array({ {{"type", "input_text"}, {"text", "thanks that helps"}}, })}}, })}, }; RouteContext ctx = lemon::build_route_context(role_tagged, "router"); check("responses input array: total_chars sums all items", ctx.params.total_chars == std::string("here is some code").size() + std::string("thanks that helps").size()); check("responses input array: chars is the last user item only", ctx.params.chars == std::string("thanks that helps").size()); json bare = { {"model", "router"}, {"input", json::array({ {{"type", "input_text"}, {"text", "hello"}}, {{"type", "input_image"}, {"image_url", "data:image/png;base64,AAAA"}}, })}, }; RouteContext bare_ctx = lemon::build_route_context(bare, "router"); check("responses bare parts: images contribute nothing to total_chars", bare_ctx.params.total_chars == std::string("hello").size()); } static void test_build_route_context_reads_max_tokens() { json request = { {"model", "router"}, {"messages", json::array({{{"role", "user"}, {"content", "hi"}}})}, {"max_tokens", 256}, }; RouteContext ctx = lemon::build_route_context(request, "router"); check("build_route_context: max_tokens populates expected_output_tokens", ctx.params.expected_output_tokens.has_value() && *ctx.params.expected_output_tokens == 256); } static void test_build_route_context_reads_max_completion_tokens_when_max_tokens_absent() { json request = { {"model", "router"}, {"messages", json::array({{{"role", "user"}, {"content", "hi"}}})}, {"max_completion_tokens", 512}, }; RouteContext ctx = lemon::build_route_context(request, "router"); check("build_route_context: max_completion_tokens populates expected_output_tokens " "when max_tokens is absent", ctx.params.expected_output_tokens.has_value() && *ctx.params.expected_output_tokens == 512); } static void test_build_route_context_max_tokens_wins_over_max_completion_tokens() { json request = { {"model", "router"}, {"messages", json::array({{{"role", "user"}, {"content", "hi"}}})}, {"max_tokens", 128}, {"max_completion_tokens", 512}, }; RouteContext ctx = lemon::build_route_context(request, "router"); check("build_route_context: max_tokens takes precedence when both are present", ctx.params.expected_output_tokens.has_value() && *ctx.params.expected_output_tokens == 128); } static void test_build_route_context_reads_max_output_tokens_for_responses() { json request = { {"model", "router"}, {"input", "summarize this"}, {"max_output_tokens", 256}, }; RouteContext ctx = lemon::build_route_context(request, "router"); check("build_route_context: /v1/responses' max_output_tokens populates " "expected_output_tokens", ctx.params.expected_output_tokens.has_value() && *ctx.params.expected_output_tokens == 256); } static void test_build_route_context_expected_output_tokens_absent_when_unset() { json request = { {"model", "router"}, {"messages", json::array({{{"role", "user"}, {"content", "hi"}}})}, }; RouteContext ctx = lemon::build_route_context(request, "router"); check("build_route_context: expected_output_tokens is nullopt when neither field is sent", !ctx.params.expected_output_tokens.has_value()); } static void test_build_route_context_ignores_non_positive_or_non_integer_max_tokens() { json zero_request = { {"model", "router"}, {"messages", json::array({{{"role", "user"}, {"content", "hi"}}})}, {"max_tokens", 0}, }; RouteContext zero_ctx = lemon::build_route_context(zero_request, "router"); check("build_route_context: max_tokens: 0 is treated as absent", !zero_ctx.params.expected_output_tokens.has_value()); json negative_request = { {"model", "router"}, {"messages", json::array({{{"role", "user"}, {"content", "hi"}}})}, {"max_tokens", -10}, }; RouteContext negative_ctx = lemon::build_route_context(negative_request, "router"); check("build_route_context: a negative max_tokens is treated as absent", !negative_ctx.params.expected_output_tokens.has_value()); json string_request = { {"model", "router"}, {"messages", json::array({{{"role", "user"}, {"content", "hi"}}})}, {"max_tokens", "unlimited"}, }; RouteContext string_ctx = lemon::build_route_context(string_request, "router"); check("build_route_context: a non-integer max_tokens is treated as absent", !string_ctx.params.expected_output_tokens.has_value()); } static lemon::CostServices fake_cost_services() { lemon::CostServices services; services.cost_of = [](const std::string& candidate) -> lemon::CostInfo { lemon::CostInfo info; if (candidate == "model-a") { info.cost_tier = "free"; info.latency_ms_hint = 12.0; } else if (candidate == "model-b") { info.cost_tier = "medium"; info.cost_input_per_million = 3.0; info.cost_output_per_million = 15.0; } return info; }; return services; } static void test_estimated_cost_attached_on_matched_rule() { RoutePolicy policy; policy.candidates = {"model-a", "model-b"}; policy.default_model = "model-b"; policy.rules = { rule("prefer-a", leaf(json{{"keywords_any", json::array({"alpha"})}}), "model-a"), }; RoutingPolicyEngine engine(std::move(policy), lemon::ClassifierServices{}, fake_cost_services()); Decision decision = engine.route(route_context("alpha path"), false); check("matched rule routes to model-a", decision.route_to == "model-a" && !decision.default_used); check("matched rule attaches estimated_cost", decision.outputs.contains("estimated_cost") && decision.outputs["estimated_cost"].value("cost_tier", "") == "free" && near(decision.outputs["estimated_cost"].value("latency_ms_hint", -1.0), 12.0) && !decision.outputs["estimated_cost"].contains("cost_input_per_million")); } static void test_estimated_cost_attached_on_default() { RoutePolicy policy; policy.candidates = {"model-a", "model-b"}; policy.default_model = "model-b"; policy.rules = { rule("prefer-a", leaf(json{{"keywords_any", json::array({"alpha"})}}), "model-a"), }; RoutingPolicyEngine engine(std::move(policy), lemon::ClassifierServices{}, fake_cost_services()); Decision decision = engine.route(route_context("no match here"), false); check("default routes to model-b", decision.route_to == "model-b" && decision.default_used); check("default attaches estimated_cost", decision.outputs.contains("estimated_cost") && decision.outputs["estimated_cost"].value("cost_tier", "") == "medium" && near(decision.outputs["estimated_cost"].value("cost_input_per_million", -1.0), 3.0) && near(decision.outputs["estimated_cost"].value("cost_output_per_million", -1.0), 15.0)); } static void test_estimated_cost_omitted_when_candidate_has_no_cost_data() { lemon::CostServices services; services.cost_of = [](const std::string&) { return lemon::CostInfo{}; }; RoutePolicy policy; policy.candidates = {"model-a", "model-b"}; policy.default_model = "model-b"; policy.rules = { rule("prefer-a", leaf(json{{"keywords_any", json::array({"alpha"})}}), "model-a"), }; RoutingPolicyEngine engine(std::move(policy), lemon::ClassifierServices{}, std::move(services)); Decision decision = engine.route(route_context("alpha path"), false); check("no estimated_cost when CostInfo is empty", !decision.outputs.contains("estimated_cost")); } // #2763 review: cost_of is caller-injected and must never make route() throw. static void test_estimated_cost_swallows_cost_of_exception() { lemon::CostServices services; services.cost_of = [](const std::string&) -> lemon::CostInfo { throw std::runtime_error("boom"); }; RoutePolicy policy; policy.candidates = {"model-a", "model-b"}; policy.default_model = "model-b"; policy.rules = { rule("prefer-a", leaf(json{{"keywords_any", json::array({"alpha"})}}), "model-a"), }; RoutingPolicyEngine engine(std::move(policy), lemon::ClassifierServices{}, std::move(services)); Decision matched; bool matched_threw = false; try { matched = engine.route(route_context("alpha path"), false); } catch (...) { matched_threw = true; } check("route() does not throw when cost_of throws on the matched-rule path", !matched_threw && matched.route_to == "model-a" && !matched.outputs.contains("estimated_cost")); Decision defaulted; bool default_threw = false; try { defaulted = engine.route(route_context("no match here"), false); } catch (...) { default_threw = true; } check("route() does not throw when cost_of throws on the default path", !default_threw && defaulted.route_to == "model-b" && !defaulted.outputs.contains("estimated_cost")); } // #2763 review: engine-attached cost must not clobber a rule author's own // outputs["estimated_cost"]. static void test_estimated_cost_preserves_author_set_value() { Rule prefer_a = rule("prefer-a", leaf(json{{"keywords_any", json::array({"alpha"})}}), "model-a"); prefer_a.outputs = json{{"estimated_cost", "author-provided"}}; RoutePolicy policy; policy.candidates = {"model-a", "model-b"}; policy.default_model = "model-b"; policy.rules = {prefer_a}; RoutingPolicyEngine engine(std::move(policy), lemon::ClassifierServices{}, fake_cost_services()); Decision decision = engine.route(route_context("alpha path"), false); check("author-set outputs.estimated_cost is not overwritten by the engine", decision.outputs.contains("estimated_cost") && decision.outputs["estimated_cost"] == json("author-provided")); } static void test_resolve_cost_info_typed_and_extras() { // Typed fields (cloud discovery path from PR #1785) win when present. lemon::CostInfo from_typed = lemon::resolve_cost_info(std::optional{1.25}, std::optional{10.0}, {}); check("typed cost_input_per_million is used when present", from_typed.cost_input_per_million.has_value() && near(*from_typed.cost_input_per_million, 1.25)); check("typed cost_output_per_million is used when present", from_typed.cost_output_per_million.has_value() && near(*from_typed.cost_output_per_million, 10.0)); // Hand-authored extras path when typed fields are absent. std::map extras = { {"cost_tier", "free"}, {"cost_input_per_million", 0.0}, {"latency_ms_hint", 8.5}, }; lemon::CostInfo from_extras = lemon::resolve_cost_info(std::nullopt, std::nullopt, extras); check("extras cost_tier is read", from_extras.cost_tier.has_value() && *from_extras.cost_tier == "free"); check("extras cost_input_per_million is read when typed is absent", from_extras.cost_input_per_million.has_value() && near(*from_extras.cost_input_per_million, 0.0)); check("extras latency_ms_hint is read", from_extras.latency_ms_hint.has_value() && near(*from_extras.latency_ms_hint, 8.5)); check("absent typed prices stay nullopt without extras", !lemon::resolve_cost_info(std::nullopt, std::nullopt, {}) .cost_input_per_million.has_value()); // Precedence: typed wins when both typed and extras are set. std::map both = {{"cost_input_per_million", 99.0}, {"cost_output_per_million", 88.0}}; lemon::CostInfo from_both = lemon::resolve_cost_info(std::optional{1.25}, std::optional{10.0}, both); check("typed price wins over extras when both present", from_both.cost_input_per_million.has_value() && near(*from_both.cost_input_per_million, 1.25)); check("typed output price wins over extras when both present", from_both.cost_output_per_million.has_value() && near(*from_both.cost_output_per_million, 10.0)); } static void test_resolve_cost_info_rejects_unknown_cost_tier() { std::map extras = {{"cost_tier", "cheap"}}; lemon::CostInfo info = lemon::resolve_cost_info(std::nullopt, std::nullopt, extras); check("unrecognized cost_tier is dropped", !info.cost_tier.has_value()); } static void test_cost_info_to_json() { lemon::CostInfo empty; check("empty CostInfo serializes to empty object", empty.to_json().empty()); lemon::CostInfo info; info.cost_tier = "low"; info.cost_input_per_million = 2.5; json out = info.to_json(); check("to_json emits cost_tier", out.value("cost_tier", "") == "low"); check("to_json emits cost_input_per_million", near(out.value("cost_input_per_million", -1.0), 2.5)); check("to_json omits unset fields", !out.contains("cost_output_per_million") && !out.contains("latency_ms_hint")); } int main() { test_embed_uses_router_embeddings_shape(); test_semantic_similarity_loops_through_router_embeddings(); test_run_classifier_uses_router_chat_completion(); test_run_classifier_uses_classify_for_classification_model(); test_run_zero_shot_classifier_forwards_labels(); test_classify_paths_parse_truncated(); test_ort_server_reports_truncation_from_0_4_0(); test_run_zero_shot_classifier_loads_before_resolving_type(); test_run_zero_shot_classifier_rejects_non_classification_model(); test_run_systemone_sends_one_question_and_returns_its_answer(); test_run_systemone_rejects_non_classification_model(); test_run_systemone_throws_on_error_or_missing_answer(); test_run_classifier_falls_back_to_chat_for_non_classification_model(); test_run_classifier_loads_before_resolving_type(); test_run_classifier_ignores_openai_metadata(); test_direct_score_payload_is_supported(); test_out_of_range_scores_are_rejected(); test_out_of_range_scores_drive_on_error(); test_zero_shot_residency_conflict_propagates_through_route(); test_systemone_residency_conflict_propagates_through_route(); test_classifier_residency_conflict_propagates_through_route(); test_llm_residency_conflict_propagates_through_route(); test_semantic_similarity_residency_conflict_propagates_through_route(); test_ordinary_backend_failure_still_applies_on_error(); test_model_classifier_routes_with_router_services(); test_chat_service_extracts_text(); test_chat_service_is_constrained_invocation(); test_build_route_context_multi_turn_latest_user_turn_only(); test_build_route_context_chat_typed_parts_and_image(); test_build_route_context_responses_typed_input_message(); test_build_route_context_responses_bare_parts(); test_build_route_context_responses_string_input_no_image(); test_build_route_context_responses_uses_last_user_message(); test_build_route_context_total_chars_all_roles(); test_build_route_context_total_chars_counts_past_first_image(); test_build_route_context_total_chars_utf8_bytes(); test_build_route_context_total_chars_prompt_form(); test_build_route_context_total_chars_responses_input_array(); test_build_route_context_reads_max_tokens(); test_build_route_context_reads_max_completion_tokens_when_max_tokens_absent(); test_build_route_context_max_tokens_wins_over_max_completion_tokens(); test_build_route_context_reads_max_output_tokens_for_responses(); test_build_route_context_expected_output_tokens_absent_when_unset(); test_build_route_context_ignores_non_positive_or_non_integer_max_tokens(); test_estimated_cost_attached_on_matched_rule(); test_estimated_cost_attached_on_default(); test_estimated_cost_omitted_when_candidate_has_no_cost_data(); test_estimated_cost_swallows_cost_of_exception(); test_estimated_cost_preserves_author_set_value(); test_resolve_cost_info_typed_and_extras(); test_resolve_cost_info_rejects_unknown_cost_tier(); test_cost_info_to_json(); if (g_failures == 0) { std::printf("All routing classifier service tests passed.\n"); } else { std::printf("%d routing classifier service test(s) failed.\n", g_failures); } return g_failures == 0 ? 0 : 1; }