bundled_models.cpp

src/config/bundled_models.cpp

@file bundled_models.cpp @brief BundledModels registry implementation.

Owners: @tvanfossen

auto_discover_and_load

std::string entropic::config::BundledModels::auto_discover_and_load()

Auto-discover and load bundled_models.yaml.

src/config/bundled_models.cpp:269

Source · 35 lines
std::string BundledModels::auto_discover_and_load() {
    // Discovery order mirrors data_dir.cpp::resolve_data_dir so the
    // registry is found the same way as prompts/grammars/schemas:
    //   1. ENTROPIC_DATA_DIR env (explicit operator override)
    //   2. binary-relative via dladdr (portable across install prefixes)
    //   3-5. compile-time install path, source tree, CWD (dev/build-host)
    // Pre-v2.3.6 this list was only 3-5 — purely compile-time/CWD — so
    // a relocated install (any machine that isn't the build host, incl.
    // container-staged release binaries) silently loaded zero models.
    std::vector<std::filesystem::path> candidates;
    if (const char* env = std::getenv("ENTROPIC_DATA_DIR"); env && *env) {
        candidates.emplace_back(std::filesystem::path(env) / "bundled_models.yaml");
    }
    if (auto from_lib = share_dir_from_library(); !from_lib.empty()) {
        candidates.emplace_back(from_lib / "bundled_models.yaml");
    }
    candidates.emplace_back(
        std::filesystem::path(CONFIG_ENTROPIC_DATA_DIR) / "bundled_models.yaml");
    candidates.emplace_back(
        std::filesystem::path(CONFIG_ENTROPIC_SOURCE_DATA_DIR) / "bundled_models.yaml");
    candidates.emplace_back("data/bundled_models.yaml");

    for (const auto& path : candidates) {
        if (std::filesystem::is_regular_file(path)) {
            auto err = load(path);
            if (err.empty()) {
                s_log->info("pre-loaded {} bundled model(s) from {}",
                            entries_.size(), path.string());
                return "";
            }
            s_log->warn("bundled models load failed: {} — {}", path.string(), err);
        }
    }
    return "bundled_models.yaml not found in default paths";
}
Activity · 16 steps
lines 278–278std::vector<std::filesystem::path> candidates;lines 279–279◇ if (const char* env = std::getenv("ENTROPIC_DATA_DIR"); env && *env)lines 280–280candidates.emplace_back(std::filesystem::path(env) / "bundled_models.yaml");lines 282–282◇ if (auto from_lib = share_dir_from_library(); !from_lib.empty())lines 283–283candidates.emplace_back(from_lib / "bundled_models.yaml");lines 285–286candidates.emplace_back( std::filesystem::path(CONFIG_ENTROPIC_DATA_DIR) / "bundled_models.yaml");lines 287–288candidates.emplace_back( std::filesystem::path(CONFIG_ENTROPIC_SOURCE_DATA_DIR) / "bundled_models.yaml");lines 289–289candidates.emplace_back("data/bundled_models.yaml");lines 291–291◇ for (const auto& path : candidates)lines 292–292◇ if (std::filesystem::is_regular_file(path))lines 293–293auto err = load(path);lines 294–294◇ if (err.empty())lines 295–296s_log->info("pre-loaded {} bundled model(s) from {}", entries_.size(), path.string());lines 297–297return "";lines 299–299s_log->warn("bundled models load failed: {} — {}", path.string(), err);lines 302–302return "bundled_models.yaml not found in default paths";

Requirements

REQ-CFG-003 REQ-CFG-008

find_by

std::string entropic::config::BundledModels::find_by(const std::string &family, const std::string &size_label, const std::string &quant) const

Look up a registry key by (family, size, quant) (gh#62).

src/config/bundled_models.cpp:162

Source · 14 lines
std::string BundledModels::find_by(
    const std::string& family,
    const std::string& size_label,
    const std::string& quant) const
{
    for (const auto& [key, entry] : entries_) {
        if (entry.family == family
            && entry.size_label == size_label
            && entry.quant == quant) {
            return key;
        }
    }
    return "";
}
Activity · 4 steps
lines 167–167◇ for (const auto& [key, entry] : entries_)lines 168–170◇ if (entry.family == family && entry.size_label == size_label && entry.quant == quant)lines 171–171return key;lines 174–174return "";

Requirements

REQ-CFG-003

load

std::string entropic::config::BundledModels::load(const std::filesystem::path &path)

Load registry from YAML file.

src/config/bundled_models.cpp:95

Show on the map →

Called by SCENARIO, SCENARIO, auto_discover_and_load, layered, load_registry, load_test_registry, parse

Calls parse_bundled_entry, parse_yaml, read_file

Source · 26 lines
std::string BundledModels::load(const std::filesystem::path& path)
{
    auto content = read_file(path);
    if (content.empty()) {
        return "cannot read " + path.string();
    }

    ryml::Tree tree;
    std::string err = parse_yaml(path.string(), content, tree);  // gh#211
    ryml::ConstNodeRef root;
    if (err.empty()) { root = tree.rootref(); }
    if (err.empty() && !root.is_map()) {
        err = "bundled_models.yaml root is not a mapping";
    }
    if (!err.empty()) { return err; }

    for (auto child : root) {
        BundledModelEntry entry;
        err = parse_bundled_entry(child, entry);
        if (!err.empty()) { break; }
        s_log->info("Registered bundled model: {} -> {} ({:.1f} GB)",
                    entry.key, entry.name, entry.size_gb);
        entries_[entry.key] = std::move(entry);
    }
    return err;
}
Activity · 20 steps
lines 97–97auto content = read_file(path);lines 98–98◇ if (content.empty())lines 99–99return "cannot read " + path.string();lines 102–102ryml::Tree tree;lines 103–103std::string err = parse_yaml(path.string(), content, tree);lines 104–104ryml::ConstNodeRef root;lines 105–105◇ if (err.empty())lines 105–105root = tree.rootref();lines 106–106◇ if (err.empty() && !root.is_map())lines 107–107err = "bundled_models.yaml root is not a mapping";lines 109–109◇ if (!err.empty())lines 109–109return err;lines 111–111◇ for (auto child : root)lines 112–112BundledModelEntry entry;lines 113–113err = parse_bundled_entry(child, entry);lines 114–114◇ if (!err.empty())lines 114–114break;lines 115–116s_log->info("Registered bundled model: {} -> {} ({:.1f} GB)", entry.key, entry.name, entry.size_gb);lines 117–117entries_[entry.key] = std::move(entry);lines 119–119return err;

Requirements

REQ-CFG-003

parse_bundled_entry

static std::string entropic::config::parse_bundled_entry(ryml::ConstNodeRef child, BundledModelEntry &entry)

Parse one bundled-model YAML entry into entry .

src/config/bundled_models.cpp:66

Show on the map →

Called by load

Calls extract, to_string

Source · 21 lines
static std::string parse_bundled_entry(ryml::ConstNodeRef child,
                                       BundledModelEntry& entry)
{
    entry.key = to_string(child.key());
    extract(child, "name", entry.name);
    extract(child, "url", entry.url);
    extract(child, "size_gb", entry.size_gb);
    extract(child, "adapter", entry.adapter);
    extract(child, "description", entry.description);
    extract(child, "mmproj_key", entry.mmproj_key);
    // gh#62 (v2.3.0): optional structured selectors. Entries that
    // don't declare them stay queryable by flat key only.
    extract(child, "provider",   entry.provider);
    extract(child, "family",     entry.family);
    extract(child, "size_label", entry.size_label);
    extract(child, "quant",      entry.quant);
    if (entry.name.empty()) {
        return "bundled model '" + entry.key + "' missing 'name'";
    }
    return "";
}
Activity · 14 steps
lines 69–69entry.key = to_string(child.key());lines 70–70extract(child, "name", entry.name);lines 71–71extract(child, "url", entry.url);lines 72–72extract(child, "size_gb", entry.size_gb);lines 73–73extract(child, "adapter", entry.adapter);lines 74–74extract(child, "description", entry.description);lines 75–75extract(child, "mmproj_key", entry.mmproj_key);lines 78–78extract(child, "provider", entry.provider);lines 79–79extract(child, "family", entry.family);lines 80–80extract(child, "size_label", entry.size_label);lines 81–81extract(child, "quant", entry.quant);lines 82–82◇ if (entry.name.empty())lines 83–83return "bundled model '" + entry.key + "' missing 'name'";lines 85–85return "";

Requirements

REQ-CFG-003

resolve

std::filesystem::path entropic::config::BundledModels::resolve(const std::string &value) const

Resolve a model reference to a filesystem path.

src/config/bundled_models.cpp:200

Show on the map →

Called by SCENARIO, describe_run, init_orchestrator_for_v219_family, parse_model_config, repoint_mmproj, reserve_forced_partial, resolve_model_path, run_four_arm_bench, use_small_default_model

Tested by

1 testsrun_prefill_residency_bench

Calls expand_home

Source · 41 lines
std::filesystem::path BundledModels::resolve(const std::string& value) const
{
    auto it = entries_.find(value);
    if (it == entries_.end()) {
        return expand_home(std::filesystem::path(value));
    }

    const auto filename = it->second.name + ".gguf";
    std::filesystem::path result;
    const char* reason = nullptr;

    // Priority 1: explicit operator override via env var always wins.
    // Deliberately does NOT check existence — the user may be pointing
    // at a download destination that hasn't been populated yet.
    if (const char* env = std::getenv("ENTROPIC_MODEL_DIR"); env && *env) {
        result = std::filesystem::path(env) / filename;
        reason = "ENTROPIC_MODEL_DIR";
    } else {
        // Priority 2-3: user home, then system. First existing file wins;
        // if neither exists, home_path is returned so error messages
        // point at the most-likely-intended location.
        const auto home_path =
            expand_home("~") / ".entropic" / "models" / filename;
        const auto sys_path =
            std::filesystem::path("/opt/entropic/models") / filename;
        if (std::filesystem::is_regular_file(sys_path)
            && !std::filesystem::is_regular_file(home_path)) {
            result = sys_path;
            reason = "/opt/entropic/models";
        } else {
            result = home_path;
            reason = std::filesystem::is_regular_file(home_path)
                ? "~/.entropic/models"
                : "fallback (file not found)";
        }
    }

    s_log->info("Model '{}' resolved to {} ({})",
                value, result.string(), reason);
    return result;
}
Activity · 20 steps
lines 202–202auto it = entries_.find(value);lines 203–203◇ if (it == entries_.end())lines 204–204return expand_home(std::filesystem::path(value));lines 207–207const auto filename = it->second.name + ".gguf";lines 208–208std::filesystem::path result;lines 209–209const char* reason = nullptr;lines 214–214◇ if (const char* env = std::getenv("ENTROPIC_MODEL_DIR"); env && *env)lines 215–215result = std::filesystem::path(env) / filename;lines 216–216reason = "ENTROPIC_MODEL_DIR";lines 217–217elselines 221–222const auto home_path = expand_home("~") / ".entropic" / "models" / filename;lines 223–224const auto sys_path = std::filesystem::path("/opt/entropic/models") / filename;lines 225–226◇ if (std::filesystem::is_regular_file(sys_path) && !std::filesystem::is_regular_file(home_path))lines 227–227result = sys_path;lines 228–228reason = "/opt/entropic/models";lines 229–229elselines 230–230result = home_path;lines 231–233reason = std::filesystem::is_regular_file(home_path) ? "~/.entropic/models" : "fallback (file not found)";lines 237–238s_log->info("Model '{}' resolved to {} ({})", value, result.string(), reason);lines 239–239return result;

Requirements

REQ-CFG-003

share_dir_from_library

std::filesystem::path entropic::config::anonymous_namespace{bundled_models.cpp}::share_dir_from_library()

/share/entropic derived from librentropic.so's on-disk location via dladdr.

src/config/bundled_models.cpp:40

Source · 15 lines
std::filesystem::path share_dir_from_library()
{
    Dl_info info = {};
    if (dladdr(reinterpret_cast<void*>(&share_dir_from_library), &info) == 0
        || info.dli_fname == nullptr) {
        return {};
    }
    std::error_code ec;
    auto abs_lib = std::filesystem::absolute(
        std::filesystem::path(info.dli_fname), ec);
    if (ec) {
        return {};
    }
    return abs_lib.parent_path().parent_path() / "share" / "entropic";
}
Activity · 8 steps
lines 42–42Dl_info info = {};lines 43–44◇ if (dladdr(reinterpret_cast<void*>(&share_dir_from_library), &info) == 0 || info.dli_fname == nullptr)lines 45–45return {};lines 47–47std::error_code ec;lines 48–49auto abs_lib = std::filesystem::absolute( std::filesystem::path(info.dli_fname), ec);lines 50–50◇ if (ec)lines 51–51return {};lines 53–53return abs_lib.parent_path().parent_path() / "share" / "entropic";

Requirements

REQ-CFG-003