fix(model-directives): harden suffix configuration

This commit is contained in:
elky
2026-07-22 14:19:09 +08:00
parent 713010fa0a
commit 856605defa
12 changed files with 1464 additions and 105 deletions
+40 -2
View File
@@ -124,8 +124,11 @@ impl ModelDirectivePolicySnapshot {
Some(suffixes) => suffixes
.iter()
.filter(|suffix| {
model_directive_suffix_has_builtin_mapping(suffix)
|| mappings.is_some_and(|mappings| mappings.contains_key(*suffix))
if model_directive_suffix_has_builtin_mapping(suffix) {
model_directive_builtin_suffix_supported_for_api_format(&api_format, suffix)
} else {
mappings.is_some_and(|mappings| mappings.contains_key(*suffix))
}
})
.map(String::as_str)
.collect::<Vec<_>>(),
@@ -159,6 +162,12 @@ impl ModelDirectivePolicySnapshot {
}
}
fn model_directive_builtin_suffix_supported_for_api_format(api_format: &str, suffix: &str) -> bool {
default_model_directive_suffixes(api_format)
.iter()
.any(|candidate| candidate.eq_ignore_ascii_case(suffix))
}
fn model_directive_policy_snapshot_from_config_reads<E>(
enabled: Result<Option<serde_json::Value>, E>,
settings: Result<Option<serde_json::Value>, E>,
@@ -656,6 +665,35 @@ mod tests {
);
}
#[test]
fn model_directives_search_does_not_enable_the_unsupported_fast_suffix() {
let default_snapshot =
ModelDirectivePolicySnapshot::from_config_values(Some(&json!(true)), None);
assert!(default_snapshot
.resolve_reasoning("openai:responses", Some("gpt-5.4-fast"))
.enabled());
assert!(!default_snapshot
.resolve_reasoning("openai:search", Some("gpt-5.4-fast"))
.enabled());
let explicitly_configured = ModelDirectivePolicySnapshot::from_config_values(
Some(&json!(true)),
Some(&json!({
"reasoning_effort": {
"api_formats": {
"openai:search": {
"suffixes": ["fast"],
"mappings": {}
}
}
}
})),
);
assert!(!explicitly_configured
.resolve_reasoning("openai:search", Some("gpt-5.4-fast"))
.enabled());
}
#[test]
fn openai_reasoning_defaults_cover_the_complete_effort_contract() {
for effort in ["none", "minimal", "low", "medium", "high", "xhigh"] {