Files
lemonade/test/cpp/test_routing_policy_store.cpp
Jeremy FowersandClaude Opus 5.5 b01156fb24 fix(server): watch extra_models_dir's nested folders on Linux and macOS (#3726)
* fix(server): watch extra_models_dir's nested folders on Linux and macOS

The Linux and macOS DirectoryWatchers watched extra_models_dir itself, which
reports changes to that directory's own entries only. A GGUF added to an
existing model folder, or a cache repo's refs/main moving to a new revision,
left the model list stale until something else invalidated it.

On Linux every folder below the root now gets an inotify watch, including
folders created after the watcher starts. Nested watches leave out IN_MODIFY
so a file being downloaded fires once when it is closed rather than on every
write.

On macOS the kqueue watcher is replaced with FSEvents, which reports changes
anywhere below the watched path. Recursive kqueue would need a descriptor per
folder, and a large model tree would exceed the default open-file limit.

Windows already polls the whole tree.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(server): fire macOS DirectoryWatcher once per download

FSEvents keeps reporting a file while it is written, so a download in
extra_models_dir fired the callback every latency window and pushed a
models-changed notification to every client each time. Wait for 200 ms
of quiet before calling back, matching Linux's IN_CLOSE_WRITE behavior.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VYbd5msDN6hLgsh4Rv5wAB

* test(routing): wait for the updated policy after a watcher reload

The watcher test accepted any engine swap, but a watcher event left over
from the first write can reload the unchanged file. With the macOS quiet
period that stale reload lands just before the edit, so the poll caught
the old policy and failed. Poll until the updated policy is loaded, and
drop the delete-then-write step that kqueue needed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VYbd5msDN6hLgsh4Rv5wAB

* fix(server): announce a new file on Linux only once it is closed

A regular file's IN_CREATE fired the callback while the file could still
be open, so a download that outlived the 100 ms debounce was listed
incomplete and then again on IN_CLOSE_WRITE. Skip IN_CREATE for a new
regular file, which always reports IN_CLOSE_WRITE; symlinks and hard
links never close, so they still count. Drop IN_MODIFY from the root
watch too, so a download straight into extra_models_dir also fires once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VYbd5msDN6hLgsh4Rv5wAB

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-02 16:48:43 +00:00

194 lines
6.6 KiB
C++

// Unit tests for the directory-backed RoutingPolicyStore (#2383).
#include "fake_classifier_services.h"
#include "lemon/routing_policy_store.h"
#include <chrono>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
#include <thread>
#ifndef ROUTING_FIXTURE_DIR
#define ROUTING_FIXTURE_DIR "test/cpp/fixtures/routing"
#endif
namespace fs = std::filesystem;
using lemon::Decision;
using lemon::RouteContext;
using lemon::RoutingPolicyStore;
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 json load_fixture(const std::string& name) {
std::ifstream in(std::string(ROUTING_FIXTURE_DIR) + "/" + name);
std::stringstream ss;
ss << in.rdbuf();
return json::parse(ss.str());
}
static void write_json(const fs::path& path, const json& doc) {
std::ofstream out(path);
out << doc.dump(2) << "\n";
}
static fs::path make_temp_dir() {
fs::path dir = fs::temp_directory_path();
dir /= "routing_policy_store_test_" +
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
fs::create_directories(dir);
return dir;
}
static RouteContext request(const std::string& input) {
RouteContext ctx;
ctx.input = input;
ctx.params.chars = input.size();
return ctx;
}
static void test_reload_swaps_snapshot() {
fs::path dir = make_temp_dir();
fs::path policy_path = dir / "router.json";
json doc = load_fixture("l1_keywords.json");
write_json(policy_path, doc);
lemon::testing::FakeClassifierServices fake;
RoutingPolicyStore store(dir.string(), fake.make());
auto first_snapshot = store.reload();
auto first_engine = store.get_engine("user.Router-Keywords");
check("initial reload loads one engine",
first_snapshot->engines.size() == 1 && first_engine != nullptr);
Decision first = first_engine->route(request("please fix this stack trace"), false);
check("initial engine routes with original policy",
first.route_to == "vllm.qwen3-32b");
doc["routing"]["rules"][0]["route_to"] = "Qwen3-8B-GGUF";
write_json(policy_path, doc);
auto second_snapshot = store.reload();
auto second_engine = store.get_engine("user.Router-Keywords");
check("manual reload swaps engine shared_ptr",
second_snapshot->engines.size() == 1 && second_engine != nullptr &&
second_engine != first_engine);
Decision second = second_engine->route(request("please fix this stack trace"), false);
check("reloaded engine uses updated policy",
second.route_to == "Qwen3-8B-GGUF");
fs::remove_all(dir);
}
static void test_directory_watcher_reload() {
fs::path dir = make_temp_dir();
fs::path policy_path = dir / "router.json";
json doc = load_fixture("l1_keywords.json");
write_json(policy_path, doc);
lemon::testing::FakeClassifierServices fake;
RoutingPolicyStore store(dir.string(), fake.make());
store.start_watching();
auto first_engine = store.get_engine("user.Router-Keywords");
check("watcher initial load has engine", first_engine != nullptr);
std::this_thread::sleep_for(std::chrono::milliseconds(250));
doc["routing"]["rules"][0]["route_to"] = "Qwen3-8B-GGUF";
write_json(policy_path, doc);
// A watcher event left over from the first write can reload the old file,
// so wait for the updated policy rather than for any engine swap.
std::shared_ptr<const lemon::RoutingPolicyEngine> next_engine;
std::string routed_to;
for (int i = 0; i < 40; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
next_engine = store.get_engine("user.Router-Keywords");
if (next_engine && next_engine != first_engine) {
routed_to = next_engine->route(request("please fix this stack trace"), false).route_to;
if (routed_to == "Qwen3-8B-GGUF") break;
}
}
check("DirectoryWatcher triggers engine swap",
next_engine != nullptr && next_engine != first_engine);
check("watcher-reloaded engine uses updated policy", routed_to == "Qwen3-8B-GGUF");
store.stop_watching();
fs::remove_all(dir);
}
static void test_invalid_policy_reports_error() {
fs::path dir = make_temp_dir();
fs::path policy_path = dir / "bad.json";
json doc = load_fixture("l1_keywords.json");
doc["routing"]["rules"][0]["route_to"] = "missing-model";
write_json(policy_path, doc);
lemon::testing::FakeClassifierServices fake;
RoutingPolicyStore store(dir.string(), fake.make());
auto snapshot = store.reload();
check("invalid policy does not load an engine", snapshot->engines.empty());
check("invalid policy records clear error", !snapshot->errors.empty() &&
snapshot->errors.begin()->second.find("not declared") != std::string::npos);
fs::remove_all(dir);
}
static void test_uppercase_json_extension_loads_policy() {
fs::path dir = make_temp_dir();
fs::path policy_path = dir / "router.JSON";
json doc = load_fixture("l1_keywords.json");
write_json(policy_path, doc);
lemon::testing::FakeClassifierServices fake;
RoutingPolicyStore store(dir.string(), fake.make());
auto snapshot = store.reload();
check("uppercase .JSON policy file extension is accepted",
snapshot->engines.count("user.Router-Keywords") == 1);
fs::remove_all(dir);
}
static void test_duplicate_policy_model_names_report_errors() {
fs::path dir = make_temp_dir();
json doc = load_fixture("l1_keywords.json");
write_json(dir / "first.json", doc);
write_json(dir / "second.json", doc);
lemon::testing::FakeClassifierServices fake;
RoutingPolicyStore store(dir.string(), fake.make());
auto snapshot = store.reload();
check("duplicate policy model names do not load an arbitrary engine",
snapshot->engines.empty());
check("duplicate policy model names record per-file errors",
snapshot->errors.size() == 2 &&
snapshot->errors.begin()->second.find("duplicate collection.router model_name") !=
std::string::npos);
fs::remove_all(dir);
}
int main() {
test_reload_swaps_snapshot();
test_directory_watcher_reload();
test_invalid_policy_reports_error();
test_uppercase_json_extension_loads_policy();
test_duplicate_policy_model_names_report_errors();
if (g_failures == 0) {
std::printf("All routing policy store tests passed.\n");
} else {
std::printf("%d routing policy store test(s) failed.\n", g_failures);
}
return g_failures == 0 ? 0 : 1;
}