// One binary, the suites below run in order in one process. Each keeps its scratch // directories and its own failure count; the only shared state is the process-wide // models directory, which every suite sets on entry. #include "lemon/models/artifact_ref.h" #include "lemon/models/artifact_source.h" #include "lemon/models/fetchable_repository.h" #include "lemon/utils/path_utils.h" #include #include #include #include #include #include #include #include "lemon/backends/backend_ops.h" #include "lemon/model_manager.h" #include "lemon/models/artifact_resolver.h" #include #include // Pins the artifact-source seam: what a checkpoint is (ArtifactRef), what a // source answers (ArtifactSource), and the registration that carries a host's // descriptor without a second table of facts. namespace source { // Pins the artifact-source seam: what a checkpoint is (ArtifactRef), what a // source answers (ArtifactSource), and the registration that carries a host's // descriptor without a second table of facts. using lemon::models::ArtifactKind; using lemon::models::ArtifactRef; using lemon::models::Acquisition; using lemon::models::Deletion; using lemon::models::FetchableRepository; using lemon::models::LocalRef; using lemon::models::Presence; using lemon::models::RegistryDescriptor; using lemon::models::RegistryId; using lemon::models::RepoListing; using lemon::models::RepoRef; using lemon::models::Revision; using lemon::models::SourcePlacement; using lemon::models::Staleness; static int g_failures = 0; static void check(const std::string& name, bool ok) { std::printf("[%s] %s\n", ok ? "PASS" : "FAIL", name.c_str()); if (!ok) ++g_failures; } static RegistryDescriptor fake_descriptor() { return RegistryDescriptor{RegistryId("zzfake"), {"zzfake", "zf"}, "Fake Host", "zf", "zzfake--", "trunk", ""}; } // The whole interface has to be implementable from the value types alone, or the // seam leaks something the host cannot supply. class FakeRepository : public FetchableRepository { public: FakeRepository(ArtifactRef ref, SourcePlacement placement) : FetchableRepository(std::move(ref), placement) {} ArtifactKind kind() const override { return ArtifactKind::Registry; } std::string resolve() const override { return ""; } Presence present() const override { return Presence{}; } Acquisition acquisition() const override { return Acquisition{}; } Deletion deletion() const override { return Deletion{}; } RegistryId id() const override { return RegistryId("zzfake"); } Revision default_revision() const override { return Revision{"trunk", false}; } RepoListing list(const std::string& repo_id, const std::string& revision) const override { return RepoListing{repo_id, Revision{revision, false}, {}}; } std::string file_url(const RepoRef& ref) const override { return "zf://" + ref.repo_id + "/" + ref.path; } bool can_go_stale() const override { return true; } Revision active_revision(const LocalRef&) const override { return Revision{"trunk", false}; } void set_active_revision(const LocalRef&, const Revision&) override {} Staleness compare(const LocalRef&, const RepoListing& listing) const override { Staleness staleness; staleness.remote_snapshot_id = listing.revision.id; return staleness; } void commit(const LocalRef&, const Revision&) override {} std::string cache_dir_name(const std::string& repo_id) const override { return "zzfake--" + repo_id; } }; LEMONADE_REGISTER_ARTIFACT_REPOSITORY(FakeRepository, fake_descriptor()); // A second host claiming the same id: reported, not silently decided by link order. class DuplicateRepository : public FakeRepository { public: using FakeRepository::FakeRepository; RegistryId id() const override { return RegistryId("zzfake"); } }; LEMONADE_REGISTER_ARTIFACT_REPOSITORY(DuplicateRepository, fake_descriptor()); static void test_two_refs_are_the_same_checkpoint_only_when_their_provenance_is() { ArtifactRef ref; ref.registry = RegistryId("huggingface"); ref.repo_id = "someone/somewhere"; ref.variant = "fp16"; check("a checkpoint is the same ref when nothing moved", ref == ArtifactRef(ref)); ArtifactRef other_variant = ref; other_variant.variant = "q4"; check("a different variant is a different checkpoint", !(ref == other_variant)); ArtifactRef optional = ref; optional.required = false; check("a checkpoint that need not be fetched is not the same ref", !(ref == optional)); } static void test_registration_builds_the_host_it_names() { const RegistryId fake = RegistryId("zzfake"); const std::unique_ptr repository = lemon::models::create_repository(fake, ArtifactRef{}); check("a registered host builds", repository != nullptr); if (repository != nullptr) { check("and reports its own id", repository->id() == fake); check("its default revision is not inherited from another host", repository->default_revision().id == "trunk"); check("it answers for its own cache namespace", repository->cache_dir_name("org/repo") == "zzfake--org/repo"); check("a source knows what kind it is", repository->kind() == ArtifactKind::Registry); } check("an unregistered host builds nothing", lemon::models::create_repository(RegistryId("zznobody"), ArtifactRef{}) == nullptr); } static void test_registration_carries_the_descriptor() { const RegistryDescriptor* descriptor = lemon::models::descriptor_for(RegistryId("zzfake")); check("a registered host has a descriptor row", descriptor != nullptr); if (descriptor != nullptr) { check("the row came from the registration, not a second table", descriptor->cache_dir_prefix == "zzfake--" && descriptor->default_revision == "trunk"); check("looking up by id does not resolve an alias", lemon::models::descriptor_for(RegistryId("zf")) == nullptr); } const std::vector registered = lemon::models::registered_repository_ids(); check("the registered list includes the new host", std::find(registered.begin(), registered.end(), RegistryId("zzfake")) != registered.end()); } static void test_a_host_claimed_twice_is_reported() { const std::vector conflicts = lemon::models::repository_registration_conflicts(); check("the duplicate claim is reported rather than won silently", std::find(conflicts.begin(), conflicts.end(), RegistryId("zzfake")) != conflicts.end()); } static void test_the_builtin_hosts_are_untouched() { // A test registering a host must not disturb the rows production reads. const RegistryDescriptor* huggingface = lemon::models::descriptor_for(RegistryId("huggingface")); check("the default host still resolves", huggingface != nullptr); if (huggingface != nullptr) { check("with its own namespace", huggingface->cache_dir_prefix == "models--"); } } // The built-in hosts are reached the same way as any registered one, so these // assert the port's answers without a network. static void test_the_built_in_hosts_answer_through_the_port() { const std::unique_ptr huggingface = lemon::models::create_repository(RegistryId("huggingface"), ArtifactRef{}); const std::unique_ptr modelscope = lemon::models::create_repository(RegistryId("modelscope"), ArtifactRef{}); check("both hosts build from registration alone", huggingface != nullptr && modelscope != nullptr); if (huggingface == nullptr || modelscope == nullptr) { return; } check("each reports the host it was built for", huggingface->id().str() == "huggingface" && modelscope->id().str() == "modelscope"); check("each keeps its own cache namespace", huggingface->cache_dir_name("org/repo") == "models--org--repo" && modelscope->cache_dir_name("org/repo") == "modelscope--models--org--repo"); check("each keeps its own default revision", huggingface->default_revision().id == "main" && modelscope->default_revision().id == "master"); check("a branch default is not pinned", !huggingface->default_revision().pinned); check("both can go stale", huggingface->can_go_stale() && modelscope->can_go_stale()); check("deleting a registry model removes its cache directory", huggingface->deletion().kind == lemon::models::Deletion::Kind::CacheDir); } static void test_the_host_is_stamped_into_the_source() { ArtifactRef without_host; without_host.repo_id = "org/repo"; const std::unique_ptr repository = lemon::models::create_repository(RegistryId("modelscope"), without_host); check("a caller that names no host still gets one", repository != nullptr && repository->id().str() == "modelscope"); } static void test_the_recorded_revision_round_trips() { namespace fs = std::filesystem; const fs::path temp = fs::temp_directory_path() / ("registry_repo_" + std::to_string( std::chrono::steady_clock::now() .time_since_epoch() .count())); fs::create_directories(temp); // The repository resolves a local ref against the models dir, not the cache // dir: get_hf_cache_dir() is what it asks. lemon::utils::set_models_dir(lemon::utils::path_to_utf8(temp)); const std::unique_ptr repository = lemon::models::create_repository(RegistryId("huggingface"), ArtifactRef{}); if (repository == nullptr) { check("the port builds a source to record a revision on", false); return; } const LocalRef local{"org/repo"}; check("nothing is recorded before a download", repository->active_revision(local).id.empty()); check("and a source says why it is not present", !repository->present().complete && !repository->present().reason.empty()); const std::string commit(40, 'a'); repository->set_active_revision(local, Revision{commit, false}); const Revision recorded = repository->active_revision(local); check("the revision a download committed is read back", recorded.id == commit); check("a full commit SHA is pinned", recorded.pinned); const lemon::models::Staleness newer = repository->compare(local, RepoListing{"org/repo", Revision{"b", false}, {}}); check("a different remote snapshot is an update", newer.update_available); check("both snapshot ids are reported", newer.local_snapshot_id == commit && newer.remote_snapshot_id == "b"); check("the same snapshot is not an update", !repository->compare(local, RepoListing{"org/repo", Revision{commit, true}, {}}) .update_available); // Never-downloaded, not overwritten: an empty revision writes nothing, which // is how a partial download keeps its recorded ref. check("an unrecorded model reports no update rather than a false one", !repository->compare(LocalRef{"org/never-downloaded"}, RepoListing{"org/never-downloaded", Revision{"c", false}, {}}) .update_available); fs::remove_all(temp); } int run() { test_two_refs_are_the_same_checkpoint_only_when_their_provenance_is(); test_registration_builds_the_host_it_names(); test_registration_carries_the_descriptor(); test_a_host_claimed_twice_is_reported(); test_the_builtin_hosts_are_untouched(); test_the_built_in_hosts_answer_through_the_port(); test_the_host_is_stamped_into_the_source(); test_the_recorded_revision_round_trips(); if (g_failures == 0) { std::printf("All artifact source tests passed.\n"); return 0; } std::printf("%d artifact source test(s) failed.\n", g_failures); return 1; } } // namespace source namespace resolver { using lemon::ModelInfo; using lemon::backends::DownloadPolicy; using lemon::models::ArtifactKind; using lemon::models::ArtifactRef; using lemon::models::ArtifactSource; using lemon::models::ArtifactSourceResolver; using lemon::models::Acquisition; using lemon::models::Deletion; using lemon::models::FetchableRepository; using lemon::models::LocalRef; using lemon::models::Presence; using lemon::models::RegistrationError; using lemon::models::RegistryId; using lemon::models::RepoListing; using lemon::models::RepoRef; using lemon::models::Revision; using lemon::models::SourcePlacement; using lemon::models::Staleness; static int g_failures = 0; static void check(const std::string& name, bool ok) { std::printf("[%s] %s\n", ok ? "PASS" : "FAIL", name.c_str()); if (!ok) ++g_failures; } namespace { class SpyRepository : public FetchableRepository { public: SpyRepository(ArtifactRef ref, SourcePlacement placement) : FetchableRepository(std::move(ref), placement) {} ArtifactKind kind() const override { return ArtifactKind::Registry; } std::string resolve() const override { return ""; } Presence present() const override { return Presence{}; } Acquisition acquisition() const override { return Acquisition{}; } Deletion deletion() const override { return Deletion{}; } RegistryId id() const override { return RegistryId("zzhost"); } Revision default_revision() const override { return Revision{"trunk", false}; } RepoListing list(const std::string& repo_id, const std::string&) const override { return RepoListing{repo_id, Revision{}, {}}; } std::string file_url(const RepoRef& ref) const override { return ref.path; } bool can_go_stale() const override { return true; } Revision active_revision(const LocalRef&) const override { return Revision{}; } void set_active_revision(const LocalRef&, const Revision&) override {} Staleness compare(const LocalRef&, const RepoListing&) const override { return Staleness{}; } void commit(const LocalRef&, const Revision&) override {} std::string cache_dir_name(const std::string& repo_id) const override { return repo_id; } }; RegistryId g_built_host; ArtifactRef g_built_ref; ArtifactSourceResolver::Hooks base_hooks() { ArtifactSourceResolver::Hooks hooks; hooks.download_policy = [](const ModelInfo& info) { if (info.recipe == "cloud") return DownloadPolicy::None; if (info.recipe == "flm") return DownloadPolicy::SelfManaged; return DownloadPolicy::Repository; }; hooks.repository_for = [](const RegistryId& id, const ArtifactRef& ref, const SourcePlacement& placement) -> std::unique_ptr { g_built_host = id; g_built_ref = ref; return std::make_unique(ref, placement); }; return hooks; } ModelInfo model(const std::string& recipe, const std::string& checkpoint, const std::string& source = "", const std::string& registry_source = "") { ModelInfo info; info.model_name = "test.model"; info.recipe = recipe; info.checkpoints["main"] = checkpoint; info.source = source; info.registry_source = registry_source; return info; } std::string temp_file() { namespace fs = std::filesystem; fs::path path = fs::temp_directory_path() / ("resolver_" + std::to_string( std::chrono::steady_clock::now().time_since_epoch().count()) + ".gguf"); std::ofstream(path) << "bytes"; return path.string(); } } // namespace static void test_a_cloud_entry_has_no_bytes() { const ArtifactSourceResolver resolver(base_hooks()); const std::unique_ptr source = resolver.source_for(model("cloud", "someone/somewhere"), "main"); check("a cloud route has no artifact kind", source->kind() == ArtifactKind::None); check("and reports itself downloaded", source->present().complete); check("nothing is fetched", source->acquisition().kind == Acquisition::Kind::NotApplicable); check("and nothing is deleted", source->deletion().kind == Deletion::Kind::Nothing); } static void test_a_backend_that_owns_its_weights_delegates_both() { const ArtifactSourceResolver resolver(base_hooks()); const std::unique_ptr source = resolver.source_for(model("flm", "gemma3:4b"), "main"); check("a self-managed recipe is its own kind", source->kind() == ArtifactKind::SelfManaged); const Acquisition acquisition = source->acquisition(); check("the fetch is delegated", acquisition.kind == Acquisition::Kind::Delegated); check("and the text names who fetches it", acquisition.delegate == "flm"); check("so removal is delegated too", source->deletion().kind == Deletion::Kind::Delegate); } // Before this, the source answered `false` beside a row that said `downloaded: true`. static void test_a_self_managed_checkpoint_carries_the_rows_answer() { const ArtifactSourceResolver resolver(base_hooks()); ModelInfo installed = model("flm", "gemma3:4b"); installed.downloaded = true; installed.resolved_paths["main"] = "/flm/store/gemma3-4b"; const std::unique_ptr placed = resolver.source_for(installed, "main"); const Presence present = placed->present(); check("a model the delegate installed answers present", present.complete); check("and says where the delegate keeps it", present.path == "/flm/store/gemma3-4b" && placed->resolve() == present.path); check("a present answer carries no complaint", present.reason.empty()); ModelInfo absent = model("flm", "gemma3:4b"); absent.downloaded = false; const Presence missing = resolver.source_for(absent, "main")->present(); check("a delegate without the model answers absent", !missing.complete); check("and the reason names the delegate, not a wire nobody plugged in", missing.reason.find("flm") != std::string::npos && missing.reason.find("no presence probe") == std::string::npos); ModelInfo second = model("flm", "gemma3:4b"); second.downloaded = true; second.checkpoints["mmproj"] = "gemma3:4b-mmproj"; check("a second checkpoint of that model agrees with the first", resolver.source_for(second, "mmproj")->present().complete); } static void test_the_origin_decides_whose_bytes_they_are() { const ArtifactSourceResolver resolver(base_hooks()); const std::unique_ptr placed = resolver.source_for(model("llamacpp", "/models/thing.gguf", "local_path"), "main"); check("a placed file is local", placed->kind() == ArtifactKind::Local); check("and deleting the model leaves it", placed->deletion().kind == Deletion::Kind::Nothing); const std::unique_ptr scanned = resolver.source_for(model("llamacpp", "/models/thing.gguf", "extra_models_dir"), "main"); check("a scanned file is left too", scanned->deletion().kind == Deletion::Kind::Nothing); const std::unique_ptr uploaded = resolver.source_for(model("llamacpp", "/cache/upload.gguf", "local_upload"), "main"); check("but a copy lemond wrote is lemond's to remove", uploaded->deletion().kind == Deletion::Kind::CacheDir); check("and it resolves to the recorded copy", uploaded->resolve() == "/cache/upload.gguf"); // A Windows absolute path holds a colon; only the source keeps it from being // read as a repository named "C" with a variant. const std::unique_ptr windows = resolver.source_for(model("llamacpp", "C:\\models\\thing.gguf", "local_path"), "main"); check("a windows path stays one file", windows->kind() == ArtifactKind::Local); check("and its colon is not read as a variant", windows->resolve() == "C:\\models\\thing.gguf"); } static void test_an_explicit_scheme_outranks_the_default_host() { const ArtifactSourceResolver resolver(base_hooks()); ModelInfo info = model("llamacpp", "hf://someone/somewhere"); info.registry_source = "modelscope"; const std::unique_ptr source = resolver.source_for(info, "main"); check("the scheme in the checkpoint wins", g_built_host.str() == "huggingface" && source->kind() == ArtifactKind::Registry); check("and is stripped from the repository it names", g_built_ref.repo_id == "someone/somewhere"); } static void test_the_host_of_a_repository_comes_from_the_entry_then_the_config() { ArtifactSourceResolver::Hooks hooks = base_hooks(); hooks.default_registry_id = [] { return std::string("modelscope"); }; const ArtifactSourceResolver resolver(hooks); resolver.source_for(model("llamacpp", "someone/somewhere:fp16", "", "modelscope"), "main"); check("a pinned registry is honored", g_built_host.str() == "modelscope"); check("the variant is carried on the ref", g_built_ref.variant == "fp16"); resolver.source_for(model("llamacpp", "someone/somewhere"), "main"); check("an unpinned entry takes the configured default", g_built_host.str() == "modelscope"); } static void test_no_host_and_no_default_is_an_error_not_a_miss() { const ArtifactSourceResolver resolver(base_hooks()); bool threw = false; try { resolver.source_for(model("llamacpp", "someone/somewhere"), "main"); } catch (const RegistrationError&) { threw = true; } check("an unpinned entry with no default registry reports a registration error", threw); } static void test_presence_asks_the_backend_that_reads_the_file() { // No hook injects the validator: the ops of the recipe that reads the file answer. const ArtifactSourceResolver resolver(base_hooks()); const std::string path = temp_file(); ModelInfo any_reader = model("whispercpp", path, "local_path"); check("a file that exists is present", resolver.source_for(any_reader, "main")->present().complete); // llamacpp opens a .gguf and looks for the magic, so a file without it is not a // model that is really there. ModelInfo gguf_reader = model("llamacpp", path, "local_path"); const Presence presence = resolver.source_for(gguf_reader, "main")->present(); check("a file the backend rejects is not present", !presence.complete); check("and says why", !presence.reason.empty()); std::filesystem::remove(path); } static void test_a_lazily_acquired_checkpoint_is_not_required() { ArtifactSourceResolver::Hooks hooks = base_hooks(); hooks.default_registry_id = [] { return std::string("huggingface"); }; const ArtifactSourceResolver resolver(hooks); ModelInfo info = model("whispercpp", "org/repo:model.bin"); info.checkpoints["npu_cache"] = "org/repo:model.bin.rai"; check("an ordinary checkpoint is required", resolver.source_for(info, "main")->ref().required); check("a lazily acquired one is not", !resolver.source_for(info, "npu_cache")->ref().required); // A recipe that fetches everything at pull time requires all of its checkpoints. ModelInfo other = model("llamacpp", "org/repo:main.gguf"); other.checkpoints["npu_cache"] = "someone/compiled:rai"; check("another backend's side cache is still required", resolver.source_for(other, "npu_cache")->ref().required); } static void test_a_checkpoint_that_binds_to_nothing_is_reported() { const ArtifactSourceResolver resolver(base_hooks()); bool threw = false; try { resolver.source_for(model("llamacpp", "notarepo"), "main"); } catch (const RegistrationError&) { threw = true; } check("a name that is neither repository nor file is a registration error", threw); } static void test_a_file_on_disk_outranks_the_repo_id_heuristic() { const std::string path = temp_file(); const ArtifactSourceResolver resolver(base_hooks()); const std::unique_ptr source = resolver.source_for(model("llamacpp", path), "main"); check("a path that exists is a file, though it carries a slash", source->kind() == ArtifactKind::Local); check("and resolves to itself", source->resolve() == path); std::filesystem::remove(path); } static void test_a_missing_org_repo_still_binds_to_its_host() { ArtifactSourceResolver::Hooks hooks = base_hooks(); hooks.default_registry_id = [] { return std::string("huggingface"); }; const ArtifactSourceResolver resolver(hooks); const std::unique_ptr source = resolver.source_for(model("llamacpp", "someone/somewhere"), "main"); check("bytes that are not here are fetched, not reported missing", source->kind() == ArtifactKind::Registry); std::filesystem::remove(temp_file()); } int run() { test_a_cloud_entry_has_no_bytes(); test_a_backend_that_owns_its_weights_delegates_both(); test_a_self_managed_checkpoint_carries_the_rows_answer(); test_the_origin_decides_whose_bytes_they_are(); test_an_explicit_scheme_outranks_the_default_host(); test_the_host_of_a_repository_comes_from_the_entry_then_the_config(); test_no_host_and_no_default_is_an_error_not_a_miss(); test_presence_asks_the_backend_that_reads_the_file(); test_a_lazily_acquired_checkpoint_is_not_required(); test_a_checkpoint_that_binds_to_nothing_is_reported(); test_a_file_on_disk_outranks_the_repo_id_heuristic(); test_a_missing_org_repo_still_binds_to_its_host(); if (g_failures == 0) { std::printf("All artifact resolver tests passed.\n"); return 0; } std::printf("%d artifact resolver test(s) failed.\n", g_failures); return 1; } } // namespace resolver int main() { int failures = 0; std::printf("\n=== source ===\n"); failures |= source::run(); std::printf("\n=== resolver ===\n"); failures |= resolver::run(); return failures; }