validate.cpp

src/config/validate.cpp

@file validate.cpp @brief Config validation implementation.

Owners: @tvanfossen

grammar_search_paths

std::vector< std::filesystem::path > entropic::config::grammar_search_paths(const ParsedConfig &config, const std::filesystem::path &data_dir)

The directories a bare grammar stem is looked up in (gh#154).

src/config/validate.cpp:370

Source · 16 lines
std::vector<std::filesystem::path> grammar_search_paths(
    const ParsedConfig& config,
    const std::filesystem::path& data_dir)
{
    std::vector<std::filesystem::path> dirs;
    if (!config.config_dir.empty()) {
        dirs.push_back(config.config_dir / "grammars");
    }
    // The facade's data_dir fallback runs only when the config_dir load
    // registered nothing, so include it on exactly that condition —
    // otherwise this would accept a stem the registry will not hold.
    if (!data_dir.empty() && (dirs.empty() || !holds_grammars(dirs.front()))) {
        dirs.push_back(data_dir / "grammars");
    }
    return dirs;
}
Activity · 6 steps
lines 374–374std::vector<std::filesystem::path> dirs;lines 375–375◇ if (!config.config_dir.empty())lines 376–376dirs.push_back(config.config_dir / "grammars");lines 381–381◇ if (!data_dir.empty() && (dirs.empty() || !holds_grammars(dirs.front())))lines 382–382dirs.push_back(data_dir / "grammars");lines 384–384return dirs;

Requirements

REQ-INFER-007

holds_grammars

static bool entropic::config::holds_grammars(const std::filesystem::path &dir)

Whether a directory holds at least one .gbnf file.

src/config/validate.cpp:348

Source · 13 lines
static bool holds_grammars(const std::filesystem::path& dir)
{
    std::error_code ec;
    if (!std::filesystem::is_directory(dir, ec)) { return false; }
    bool found = false;
    for (const auto& entry : std::filesystem::directory_iterator(dir, ec)) {
        if (entry.path().extension() == ".gbnf") {
            found = true;
            break;
        }
    }
    return found;
}
Activity · 9 steps
lines 350–350std::error_code ec;lines 351–351◇ if (!std::filesystem::is_directory(dir, ec))lines 351–351return false;lines 352–352bool found = false;lines 353–353◇ for (const auto& entry : std::filesystem::directory_iterator(dir, ec))lines 354–354◇ if (entry.path().extension() == ".gbnf")lines 355–355found = true;lines 356–356break;lines 359–359return found;

Requirements

REQ-INFER-007

validate

std::string entropic::config::validate(const ModelConfig &config)

Validate a ModelConfig .

src/config/validate.cpp:47

Source · 34 lines
std::string validate(const ModelConfig& config)
{
    std::string err;

    if (config.context_length < 512 || config.context_length > 131072) {
        err = "context_length must be in [512, 131072], got "
              + std::to_string(config.context_length);
    } else if (config.adapter.empty()) {
        err = "adapter must not be empty";
    } else if (config.n_batch < 1) {
        err = "n_batch must be >= 1, got "
              + std::to_string(config.n_batch);
    } else if (config.allowed_tools.has_value()) {
        err = validate_allowed_tools(*config.allowed_tools);
    }

    // gh#144 (v2.12.0): a session pool and the gh#98 batch fan-out want
    // opposite KV buffers. Refuse the combination by name at CONFIGURE time
    // rather than picking one silently — either choice would fail later and
    // far from its cause (a pool without private streams, or seq_cp
    // asserting at decode).
    if (err.empty()) {
        err = session_pool_conflict_reason(config);
    }

    // gh#153 #42(iii) (v2.13.0): expert offload is EXPERIMENTAL and its
    // impossible combinations are refused by name here rather than
    // discovered as "the knob did nothing" after a multi-second load.
    if (err.empty()) {
        err = expert_offload_conflict_reason(config);
    }

    return err;
}
Activity · 17 steps
lines 49–49std::string err;lines 51–51◇ if (config.context_length < 512 || config.context_length > 131072)lines 52–53err = "context_length must be in [512, 131072], got " + std::to_string(config.context_length);lines 54–54elselines 54–54◇ if (config.adapter.empty())lines 55–55err = "adapter must not be empty";lines 56–56elselines 56–56◇ if (config.n_batch < 1)lines 57–58err = "n_batch must be >= 1, got " + std::to_string(config.n_batch);lines 59–59elselines 59–59◇ if (config.allowed_tools.has_value())lines 60–60err = validate_allowed_tools(*config.allowed_tools);lines 68–68◇ if (err.empty())lines 69–69err = session_pool_conflict_reason(config);lines 75–75◇ if (err.empty())lines 76–76err = expert_offload_conflict_reason(config);lines 79–79return err;
std::string entropic::config::validate(const ModelsConfig &config)

Validate ModelsConfig .

src/config/validate.cpp:89

Show on the map →

Called by none

Calls none

Source · 9 lines
std::string validate(const ModelsConfig& config)
{
    if (!config.tiers.empty()
        && config.tiers.count(config.default_tier) == 0) {
        return "default tier '" + config.default_tier
               + "' not in tiers";
    }
    return "";
}
Activity · 3 steps
lines 91–92◇ if (!config.tiers.empty() && config.tiers.count(config.default_tier) == 0)lines 93–94return "default tier '" + config.default_tier + "' not in tiers";lines 96–96return "";
std::string entropic::config::validate(const CompactionConfig &config)

Validate CompactionConfig .

src/config/validate.cpp:106

Show on the map →

Called by none

Calls none

Source · 33 lines
std::string validate(const CompactionConfig& config)
{
    std::string err;

    if (config.threshold_percent < 0.5f
        || config.threshold_percent > 0.99f) {
        err = "compaction.threshold_percent must be in [0.5, 0.99], got "
              + std::to_string(config.threshold_percent);
    } else if (config.warning_threshold_percent
               >= config.threshold_percent) {
        err = "compaction.warning_threshold_percent ("
              + std::to_string(config.warning_threshold_percent)
              + ") must be less than threshold_percent ("
              + std::to_string(config.threshold_percent) + ")";
    } else if (config.preserve_recent_turns < 1
               || config.preserve_recent_turns > 10) {
        err = "compaction.preserve_recent_turns must be in [1, 10], got "
              + std::to_string(config.preserve_recent_turns);
    } else if (config.tool_result_ttl < 1) {
        // v2.1.3 #6 / TTL clamp removal: tool_result_ttl was previously
        // documented as "1–20" but never actually validated. The upper
        // bound was always advisory — consumers can pick whatever
        // makes sense for their workload (long delegations on large
        // context windows benefit from large TTLs). Lower bound IS
        // enforced because TTL=0 would prune every result on the next
        // iteration, which is nonsensical and almost certainly a
        // misconfiguration rather than an intent.
        err = "compaction.tool_result_ttl must be >= 1, got "
              + std::to_string(config.tool_result_ttl);
    }

    return err;
}
Activity · 13 steps
lines 108–108std::string err;lines 110–111◇ if (config.threshold_percent < 0.5f || config.threshold_percent > 0.99f)lines 112–113err = "compaction.threshold_percent must be in [0.5, 0.99], got " + std::to_string(config.threshold_percent);lines 114–114elselines 114–115◇ if (config.warning_threshold_percent >= config.threshold_percent)lines 116–119err = "compaction.warning_threshold_percent (" + std::to_string(config.warning_threshold_percent) + ") must be less than threshold_percent (" + std::to_string(config.threshold_percent) + ")";lines 120–120elselines 120–121◇ if (config.preserve_recent_turns < 1 || config.preserve_recent_turns > 10)lines 122–123err = "compaction.preserve_recent_turns must be in [1, 10], got " + std::to_string(config.preserve_recent_turns);lines 124–124elselines 124–124◇ if (config.tool_result_ttl < 1)lines 133–134err = "compaction.tool_result_ttl must be >= 1, got " + std::to_string(config.tool_result_ttl);lines 137–137return err;
std::string entropic::config::validate(const PromptCacheConfig &config)

Validate PromptCacheConfig .

src/config/validate.cpp:302

Show on the map →

Called by none

Calls none

Source · 7 lines
std::string validate(const PromptCacheConfig& config)
{
    if (config.enabled && config.max_bytes == 0) {
        return "inference.prompt_cache: enabled=true but max_bytes=0";
    }
    return "";
}
Activity · 3 steps
lines 304–304◇ if (config.enabled && config.max_bytes == 0)lines 305–305return "inference.prompt_cache: enabled=true but max_bytes=0";lines 307–307return "";

Requirements

REQ-CFG-006 REQ-INFER-027

validate_allowed_tools

std::string entropic::config::validate_allowed_tools(const std::vector< std::string > &tools)

Validate allowed_tools entries use "server.tool" format.

src/config/validate.cpp:28

Show on the map →

Called by SCENARIO, SCENARIO, validate

Calls none

Source · 10 lines
std::string validate_allowed_tools(const std::vector<std::string>& tools)
{
    for (const auto& entry : tools) {
        if (entry.find('.') == std::string::npos) {
            return "allowed_tools entry '" + entry
                   + "' must use '{server_name}.{tool_name}' format";
        }
    }
    return "";
}
Activity · 4 steps
lines 30–30◇ for (const auto& entry : tools)lines 31–31◇ if (entry.find('.') == std::string::npos)lines 32–33return "allowed_tools entry '" + entry + "' must use '{server_name}.{tool_name}' format";lines 36–36return "";

Requirements

REQ-CFG-006

validate_classification_routable

std::string entropic::config::validate_classification_routable(const RoutingConfig &routing)

A configured classification_prompt is inert without a tier_map.

src/config/validate.cpp:222

Source · 10 lines
std::string validate_classification_routable(const RoutingConfig& routing) {
    bool has_prompt = routing.classification_prompt.has_value()
                   && !routing.classification_prompt->empty();
    if (routing.enabled && has_prompt && routing.tier_map.empty()) {
        return "routing.classification_prompt is set but routing.tier_map is "
               "empty — the router emits a digit that maps to no tier, so every "
               "route falls back to the default (configure routing.tier_map)";
    }
    return "";
}
Activity · 4 steps
lines 223–224bool has_prompt = routing.classification_prompt.has_value() && !routing.classification_prompt->empty();lines 225–225◇ if (routing.enabled && has_prompt && routing.tier_map.empty())lines 226–228return "routing.classification_prompt is set but routing.tier_map is " "empty — the router emits a digit that maps to no tier, so every " "route falls back to the default (configure routing.tier_map)";lines 230–230return "";

Requirements

REQ-CFG-006

validate_config

std::string entropic::config::validate_config(const ParsedConfig &config, std::vector< std::string > &warnings)

Validate the full ParsedConfig .

src/config/validate.cpp:482

Source · 32 lines
std::string validate_config(
    const ParsedConfig& config,
    std::vector<std::string>& warnings)
{
    s_log->info("Config validation start");
    std::string err = validate_model_tiers(config.models);

    if (err.empty()) {
        err = validate_routing(config.routing, config.models);
    }
    if (err.empty()) {
        err = validate(config.compaction);
    }
    if (err.empty()) {
        err = validate(config.prompt_cache);
    }
    if (err.empty()) {
        auto w = warn_auto_chain_without_targets(
            config.models.tiers, config.routing.handoff_rules);
        if (!w.empty()) {
            warnings.push_back(w);
        }
    }

    if (err.empty()) {
        s_log->info("Config validation passed ({} warning(s))",
                    warnings.size());
    } else {
        s_log->error("Config validation failed: {}", err);
    }
    return err;
}
Activity · 17 steps
lines 486–486s_log->info("Config validation start");lines 487–487std::string err = validate_model_tiers(config.models);lines 489–489◇ if (err.empty())lines 490–490err = validate_routing(config.routing, config.models);lines 492–492◇ if (err.empty())lines 493–493err = validate(config.compaction);lines 495–495◇ if (err.empty())lines 496–496err = validate(config.prompt_cache);lines 498–498◇ if (err.empty())lines 499–500auto w = warn_auto_chain_without_targets( config.models.tiers, config.routing.handoff_rules);lines 501–501◇ if (!w.empty())lines 502–502warnings.push_back(w);lines 506–506◇ if (err.empty())lines 507–508s_log->info("Config validation passed ({} warning(s))", warnings.size());lines 509–509elselines 510–510s_log->error("Config validation failed: {}", err);lines 512–512return err;

Requirements

REQ-CFG-006

validate_fallback_tier

std::string entropic::config::validate_fallback_tier(const std::string &fallback, const std::unordered_map< std::string, TierConfig > &tiers)

Check fallback_tier exists in tiers.

src/config/validate.cpp:148

Show on the map →

Called by SCENARIO, validate_routing

Calls none

Source · 9 lines
std::string validate_fallback_tier(
    const std::string& fallback,
    const std::unordered_map<std::string, TierConfig>& tiers)
{
    if (tiers.count(fallback) == 0) {
        return "routing.fallback_tier '" + fallback + "' not in tiers";
    }
    return "";
}
Activity · 3 steps
lines 152–152◇ if (tiers.count(fallback) == 0)lines 153–153return "routing.fallback_tier '" + fallback + "' not in tiers";lines 155–155return "";

Requirements

REQ-CFG-006

validate_handoff_rules

std::string entropic::config::validate_handoff_rules(const std::unordered_map< std::string, std::vector< std::string > > &rules, const std::unordered_map< std::string, TierConfig > &tiers)

Check all handoff_rules keys and values exist in tiers.

src/config/validate.cpp:187

Show on the map →

Called by SCENARIO, validate_routing

Calls none

Source · 18 lines
std::string validate_handoff_rules(
    const std::unordered_map<std::string, std::vector<std::string>>& rules,
    const std::unordered_map<std::string, TierConfig>& tiers)
{
    for (const auto& [source, targets] : rules) {
        if (tiers.count(source) == 0) {
            return "routing.handoff_rules key '" + source
                   + "' not in tiers";
        }
        for (const auto& target : targets) {
            if (tiers.count(target) == 0) {
                return "routing.handoff_rules['" + source
                       + "'] contains '" + target + "' not in tiers";
            }
        }
    }
    return "";
}
Activity · 7 steps
lines 191–191◇ for (const auto& [source, targets] : rules)lines 192–192◇ if (tiers.count(source) == 0)lines 193–194return "routing.handoff_rules key '" + source + "' not in tiers";lines 196–196◇ for (const auto& target : targets)lines 197–197◇ if (tiers.count(target) == 0)lines 198–199return "routing.handoff_rules['" + source + "'] contains '" + target + "' not in tiers";lines 203–203return "";

Requirements

REQ-CFG-006

validate_model_tiers

static std::string entropic::config::validate_model_tiers(const ModelsConfig &models)

Validate model tiers and router.

src/config/validate.cpp:317

Source · 23 lines
static std::string validate_model_tiers(const ModelsConfig& models)
{
    std::string err = validate(models);

    if (err.empty()) {
        for (const auto& [name, tier] : models.tiers) {
            err = validate(static_cast<const ModelConfig&>(tier));
            if (!err.empty()) {
                err = "models." + name + ": " + err;
                break;
            }
        }
    }

    if (err.empty() && models.router.has_value()) {
        err = validate(*models.router);
        if (!err.empty()) {
            err = "models.router: " + err;
        }
    }

    return err;
}
Activity · 12 steps
lines 319–319std::string err = validate(models);lines 321–321◇ if (err.empty())lines 322–322◇ for (const auto& [name, tier] : models.tiers)lines 323–323err = validate(static_cast<const ModelConfig&>(tier));lines 324–324◇ if (!err.empty())lines 325–325err = "models." + name + ": " + err;lines 326–326break;lines 331–331◇ if (err.empty() && models.router.has_value())lines 332–332err = validate(*models.router);lines 333–333◇ if (!err.empty())lines 334–334err = "models.router: " + err;lines 338–338return err;

Requirements

REQ-CFG-006

validate_routing

std::string entropic::config::validate_routing(const RoutingConfig &routing, const ModelsConfig &models)

Validate RoutingConfig against defined tiers.

src/config/validate.cpp:241

Source · 26 lines
std::string validate_routing(
    const RoutingConfig& routing,
    const ModelsConfig& models)
{
    std::string err;

    if (models.tiers.empty()) {
        /* no tiers — nothing to validate */
    } else if (routing.enabled && !models.router.has_value()) {
        err = "routing.enabled is true but models.router is not configured";
    } else {
        err = validate_fallback_tier(routing.fallback_tier, models.tiers);

        if (err.empty()) {
            err = validate_tier_map(routing.tier_map, models.tiers);
        }
        if (err.empty()) {
            err = validate_handoff_rules(routing.handoff_rules, models.tiers);
        }
        if (err.empty()) {
            err = validate_classification_routable(routing);
        }
    }

    return err;
}
Activity · 14 steps
lines 245–245std::string err;lines 247–247◇ if (models.tiers.empty())lines 249–249elselines 249–249◇ if (routing.enabled && !models.router.has_value())lines 250–250err = "routing.enabled is true but models.router is not configured";lines 251–251elselines 252–252err = validate_fallback_tier(routing.fallback_tier, models.tiers);lines 254–254◇ if (err.empty())lines 255–255err = validate_tier_map(routing.tier_map, models.tiers);lines 257–257◇ if (err.empty())lines 258–258err = validate_handoff_rules(routing.handoff_rules, models.tiers);lines 260–260◇ if (err.empty())lines 261–261err = validate_classification_routable(routing);lines 265–265return err;

Requirements

REQ-CFG-006

validate_tier_map

std::string entropic::config::validate_tier_map(const std::unordered_map< std::string, std::string > &tier_map, const std::unordered_map< std::string, TierConfig > &tiers)

Check all tier_map values exist in tiers.

src/config/validate.cpp:166

Show on the map →

Called by SCENARIO, validate_routing

Calls none

Source · 12 lines
std::string validate_tier_map(
    const std::unordered_map<std::string, std::string>& tier_map,
    const std::unordered_map<std::string, TierConfig>& tiers)
{
    for (const auto& [key, tier] : tier_map) {
        if (tiers.count(tier) == 0) {
            return "routing.tier_map['" + key + "'] = '" + tier
                   + "' not in tiers";
        }
    }
    return "";
}
Activity · 4 steps
lines 170–170◇ for (const auto& [key, tier] : tier_map)lines 171–171◇ if (tiers.count(tier) == 0)lines 172–173return "routing.tier_map['" + key + "'] = '" + tier + "' not in tiers";lines 176–176return "";

Requirements

REQ-CFG-006

warn_auto_chain_without_targets

std::string entropic::config::warn_auto_chain_without_targets(const std::unordered_map< std::string, TierConfig > &tiers, const std::unordered_map< std::string, std::vector< std::string > > &handoff_rules)

Warn if tier has auto_chain but no handoff_rules entry.

src/config/validate.cpp:276

Source · 18 lines
std::string warn_auto_chain_without_targets(
    const std::unordered_map<std::string, TierConfig>& tiers,
    const std::unordered_map<std::string, std::vector<std::string>>&
        handoff_rules)
{
    std::string warnings;
    for (const auto& [name, tier] : tiers) {
        if (tier.auto_chain.has_value() && !tier.auto_chain->empty()
            && handoff_rules.count(name) == 0) {
            if (!warnings.empty()) {
                warnings += "; ";
            }
            warnings += "tier '" + name
                        + "' has auto_chain but no handoff_rules entry";
        }
    }
    return warnings;
}
Activity · 7 steps
lines 281–281std::string warnings;lines 282–282◇ for (const auto& [name, tier] : tiers)lines 283–284◇ if (tier.auto_chain.has_value() && !tier.auto_chain->empty() && handoff_rules.count(name) == 0)lines 285–285◇ if (!warnings.empty())lines 286–286warnings += "; ";lines 288–289warnings += "tier '" + name + "' has auto_chain but no handoff_rules entry";lines 292–292return warnings;

Requirements

REQ-CFG-006

warn_unresolved_tier_grammars

std::string entropic::config::warn_unresolved_tier_grammars(const ParsedConfig &config, const std::vector< std::filesystem::path > &search_dirs)

Warn about a tier whose grammar: stem resolves to no .gbnf (gh#154).

src/config/validate.cpp:443

Show on the map →

Called by SCENARIO, warn_tier_grammars_step

Calls describe_search_dirs, grammar_stem_resolves

Source · 30 lines
std::string warn_unresolved_tier_grammars(
    const ParsedConfig& config,
    const std::vector<std::filesystem::path>& search_dirs)
{
    std::vector<std::string> unresolved;
    for (const auto& [name, tier] : config.models.tiers) {
        if (!tier.grammar.has_value()) { continue; }
        // The registry keys on the file stem: "compactor.gbnf" and
        // "compactor" name the same grammar (normalize_grammar_key).
        const auto stem = tier.grammar->stem().string();
        if (stem.empty() || grammar_stem_resolves(stem, search_dirs)) {
            continue;
        }
        unresolved.push_back("models." + name + " -> '" + stem + "'");
    }
    if (unresolved.empty()) { return ""; }

    std::string list;
    for (const auto& entry : unresolved) {
        if (!list.empty()) { list += ", "; }
        list += entry;
    }
    return "grammar not resolvable yet for " + list
         + " — searched " + describe_search_dirs(search_dirs)
         + ". Register each key with entropic_grammar_register_file() or "
           "entropic_grammar_register() before the first run on that tier, "
           "or place <stem>.gbnf in a grammar search path. A run on such a "
           "tier before then fails with ENTROPIC_ERROR_GRAMMAR_NOT_FOUND "
           "rather than decoding unconstrained (gh#154).";
}
Activity · 16 steps
lines 447–447std::vector<std::string> unresolved;lines 448–448◇ for (const auto& [name, tier] : config.models.tiers)lines 449–449◇ if (!tier.grammar.has_value())lines 449–449continue;lines 452–452const auto stem = tier.grammar->stem().string();lines 453–453◇ if (stem.empty() || grammar_stem_resolves(stem, search_dirs))lines 454–454continue;lines 456–456unresolved.push_back("models." + name + " -> '" + stem + "'");lines 458–458◇ if (unresolved.empty())lines 458–458return "";lines 460–460std::string list;lines 461–461◇ for (const auto& entry : unresolved)lines 462–462◇ if (!list.empty())lines 462–462list += ", ";lines 463–463list += entry;lines 465–471return "grammar not resolvable yet for " + list + " — searched " + describe_search_dirs(search_dirs) + ". Register each key with entropic_grammar_register_file() or " "entropic_grammar_register() before the first run on that tier, " "or place <stem>.gbnf in a grammar search path. A run on such a " "tier before then fails with ENTROPIC_ERROR_GRAMMAR_NOT_FOUND " "rather than decoding unconstrained (gh#154).";

Requirements

REQ-CFG-006 REQ-INFER-007