From a21345c5f9c6aa8bbcdd69226f9ec811b755f2db Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Thu, 7 May 2026 05:50:25 +0300 Subject: [PATCH] Make nested enum config warnings non-blocking --- codex-rs/config/src/config_toml.rs | 26 +++++++- codex-rs/config/src/types.rs | 7 +++ codex-rs/core/config.schema.json | 3 + .../core/src/config/config_loader_tests.rs | 60 +++++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) diff --git a/codex-rs/config/src/config_toml.rs b/codex-rs/config/src/config_toml.rs index 5756fc1a6b..c55065c23d 100644 --- a/codex-rs/config/src/config_toml.rs +++ b/codex-rs/config/src/config_toml.rs @@ -37,6 +37,7 @@ use codex_model_provider_info::ModelProviderInfo; use codex_model_provider_info::OLLAMA_CHAT_PROVIDER_REMOVED_ERROR; use codex_model_provider_info::OLLAMA_OSS_PROVIDER_ID; use codex_model_provider_info::OPENAI_PROVIDER_ID; +use codex_model_provider_info::WireApi; use codex_protocol::config_types::ForcedLoginMethod; use codex_protocol::config_types::Personality; use codex_protocol::config_types::ReasoningSummary; @@ -1002,7 +1003,30 @@ fn deserialize_model_providers<'de, D>( where D: serde::Deserializer<'de>, { - let model_providers = HashMap::::deserialize(deserializer)?; + let raw_model_providers = HashMap::::deserialize(deserializer)?; + let mut model_providers = HashMap::new(); + for (key, value) in raw_model_providers { + let provider = match value.clone().try_into::() { + Ok(provider) => provider, + Err(err) => { + // Invalid `wire_api` is already reported by the schema warning + // pass. Remove just that leaf so the provider can fall back to + // its default wire API without hiding unrelated provider errors. + let mut without_wire_api = value; + let removed_wire_api = without_wire_api + .as_table_mut() + .and_then(|table| table.remove("wire_api")) + .is_some_and(|wire_api| wire_api.try_into::().is_err()); + if !removed_wire_api { + return Err(serde::de::Error::custom(err.to_string())); + } + without_wire_api + .try_into::() + .map_err(serde::de::Error::custom)? + } + }; + model_providers.insert(key, provider); + } validate_model_providers(&model_providers).map_err(serde::de::Error::custom)?; Ok(model_providers) } diff --git a/codex-rs/config/src/types.rs b/codex-rs/config/src/types.rs index 134bfb13cd..ed652f8eee 100644 --- a/codex-rs/config/src/types.rs +++ b/codex-rs/config/src/types.rs @@ -508,6 +508,7 @@ pub enum OtelExporterKind { } /// OTEL settings loaded from config.toml. Fields are optional so we can apply defaults. +#[serde_as] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema)] #[schemars(deny_unknown_fields)] pub struct OtelConfigToml { @@ -518,12 +519,18 @@ pub struct OtelConfigToml { pub environment: Option, /// Optional log exporter + #[serde(default)] + #[serde_as(deserialize_as = "DefaultOnError")] pub exporter: Option, /// Optional trace exporter + #[serde(default)] + #[serde_as(deserialize_as = "DefaultOnError")] pub trace_exporter: Option, /// Optional metrics exporter + #[serde(default)] + #[serde_as(deserialize_as = "DefaultOnError")] pub metrics_exporter: Option, } diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index 730416aa76..25b974304f 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -1710,6 +1710,7 @@ "$ref": "#/definitions/OtelExporterKind" } ], + "default": null, "description": "Optional log exporter" }, "log_user_prompt": { @@ -1722,6 +1723,7 @@ "$ref": "#/definitions/OtelExporterKind" } ], + "default": null, "description": "Optional metrics exporter" }, "trace_exporter": { @@ -1730,6 +1732,7 @@ "$ref": "#/definitions/OtelExporterKind" } ], + "default": null, "description": "Optional trace exporter" } }, diff --git a/codex-rs/core/src/config/config_loader_tests.rs b/codex-rs/core/src/config/config_loader_tests.rs index c1afd07075..c0d56e2bb6 100644 --- a/codex-rs/core/src/config/config_loader_tests.rs +++ b/codex-rs/core/src/config/config_loader_tests.rs @@ -25,6 +25,7 @@ use codex_config::loader::load_config_layers_state; use codex_config::loader::load_requirements_toml; use codex_config::version_for_toml; use codex_exec_server::LOCAL_FS; +use codex_model_provider_info::WireApi; use codex_protocol::config_types::TrustLevel; use codex_protocol::config_types::WebSearchMode; use codex_protocol::models::PermissionProfile; @@ -33,6 +34,7 @@ use codex_protocol::protocol::SandboxPolicy; use codex_utils_absolute_path::AbsolutePathBuf; use pretty_assertions::assert_eq; use std::collections::BTreeMap; +use std::collections::BTreeSet; use std::collections::HashMap; use std::path::Path; use tempfile::tempdir; @@ -178,6 +180,64 @@ context_size = "galactic" Ok(()) } +#[tokio::test] +async fn nested_invalid_enum_values_emit_warnings_without_poisoning_config() -> anyhow::Result<()> { + let tmp = tempdir().expect("tempdir"); + let contents = r#" +[model_providers.custom] +name = "Custom" +base_url = "https://example.invalid/v1" +wire_api = "telegraph" + +[otel.exporter.otlp-http] +endpoint = "http://localhost:4318/v1/logs" +protocol = "xml" + +[otel.trace_exporter.otlp-http] +endpoint = "http://localhost:4318/v1/traces" +protocol = "yaml" +"#; + let config_path = tmp.path().join(CONFIG_TOML_FILE); + std::fs::write(&config_path, contents).expect("write config"); + + let cwd = AbsolutePathBuf::try_from(tmp.path()).expect("cwd"); + let layers = load_config_layers_state( + LOCAL_FS.as_ref(), + tmp.path(), + Some(cwd), + &[] as &[(String, TomlValue)], + LoaderOverrides::default(), + CloudRequirementsLoader::default(), + &codex_config::NoopThreadConfigLoader, + ) + .await?; + + let (_effective_config, config_toml, enum_warnings) = + layers.deserialize_effective_config_with_warnings()?; + let otel = config_toml.otel.expect("otel config should deserialize"); + let expected_warnings = BTreeSet::from([ + "Ignoring invalid config value at model_providers.custom.wire_api: \"telegraph\"" + .to_string(), + "Ignoring invalid config value at otel.exporter.otlp-http.protocol: \"xml\"".to_string(), + "Ignoring invalid config value at otel.trace_exporter.otlp-http.protocol: \"yaml\"" + .to_string(), + ]); + + assert_eq!( + ( + config_toml + .model_providers + .get("custom") + .map(|provider| provider.wire_api), + otel.exporter, + otel.trace_exporter, + enum_warnings.into_iter().collect::>(), + ), + (Some(WireApi::Responses), None, None, expected_warnings) + ); + Ok(()) +} + #[tokio::test] async fn invalid_higher_precedence_enum_warns_without_mutating_merge() -> anyhow::Result<()> { let tmp = tempdir().expect("tempdir");