mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
889ee018e7
## Why Codex intentionally ignores unknown `config.toml` fields by default so older and newer config files keep working across versions. That leniency also makes typo detection hard because misspelled or misplaced keys disappear silently. This change adds an opt-in strict config mode so users and tooling can fail fast on unrecognized config fields without changing the default permissive behavior. This feature is possible because `serde_ignored` exposes the exact signal Codex needs: it lets Codex run ordinary Serde deserialization while recording fields Serde would otherwise ignore. That avoids requiring `#[serde(deny_unknown_fields)]` across every config type and keeps strict validation opt-in around the existing config model. ## What Changed ### Added strict config validation - Added `serde_ignored`-based validation for `ConfigToml` in `codex-rs/config/src/strict_config.rs`. - Combined `serde_ignored` with `serde_path_to_error` so strict mode preserves typed config error paths while also collecting fields Serde would otherwise ignore. - Added strict-mode validation for unknown `[features]` keys, including keys that would otherwise be accepted by `FeaturesToml`'s flattened boolean map. - Kept typed config errors ahead of ignored-field reporting, so malformed known fields are reported before unknown-field diagnostics. - Added source-range diagnostics for top-level and nested unknown config fields, including non-file managed preference source names. ### Kept parsing single-pass per source - Reworked file and managed-config loading so strict validation reuses the already parsed `TomlValue` for that source. - For actual config files and managed config strings, the loader now reads once, parses once, and validates that same parsed value instead of deserializing multiple times. - Validated `-c` / `--config` override layers with the same base-directory context used for normal relative-path resolution, so unknown override keys are still reported when another override contains a relative path. ### Scoped `--strict-config` to config-heavy entry points - Added support for `--strict-config` on the main config-loading entry points where it is most useful: - `codex` - `codex resume` - `codex fork` - `codex exec` - `codex review` - `codex mcp-server` - `codex app-server` when running the server itself - the standalone `codex-app-server` binary - the standalone `codex-exec` binary - Commands outside that set now reject `--strict-config` early with targeted errors instead of accepting it everywhere through shared CLI plumbing. - `codex app-server` subcommands such as `proxy`, `daemon`, and `generate-*` are intentionally excluded from the first rollout. - When app-server strict mode sees invalid config, app-server exits with the config error instead of logging a warning and continuing with defaults. - Introduced a dedicated `ReviewCommand` wrapper in `codex-rs/cli` instead of extending shared `ReviewArgs`, so `--strict-config` stays on the outer config-loading command surface and does not become part of the reusable review payload used by `codex exec review`. ### Coverage - Added tests for top-level and nested unknown config fields, unknown `[features]` keys, typed-error precedence, source-location reporting, and non-file managed preference source names. - Added CLI coverage showing invalid `--enable`, invalid `--disable`, and unknown `-c` overrides still error when `--strict-config` is present, including compound-looking feature names such as `multi_agent_v2.subagent_usage_hint_text`. - Added integration coverage showing both `codex app-server --strict-config` and standalone `codex-app-server --strict-config` exit with an error for unknown config fields instead of starting with fallback defaults. - Added coverage showing unsupported command surfaces reject `--strict-config` with explicit errors. ## Example Usage Run Codex with strict config validation enabled: ```shell codex --strict-config ``` Strict config mode is also available on the supported config-heavy subcommands: ```shell codex --strict-config exec "explain this repository" codex review --strict-config --uncommitted codex mcp-server --strict-config codex app-server --strict-config --listen off codex-app-server --strict-config --listen off ``` For example, if `~/.codex/config.toml` contains a typo in a key name: ```toml model = "gpt-5" approval_polic = "on-request" ``` then `codex --strict-config` reports the misspelled key instead of silently ignoring it. The path is shortened to `~` here for readability: ```text $ codex --strict-config Error loading config.toml: ~/.codex/config.toml:2:1: unknown configuration field `approval_polic` | 2 | approval_polic = "on-request" | ^^^^^^^^^^^^^^ ``` Without `--strict-config`, Codex keeps the existing permissive behavior and ignores the unknown key. Strict config mode also validates ad-hoc `-c` / `--config` overrides: ```text $ codex --strict-config -c foo=bar Error: unknown configuration field `foo` in -c/--config override $ codex --strict-config -c features.foo=true Error: unknown configuration field `features.foo` in -c/--config override ``` Invalid feature toggles are rejected too, including values that look like nested config paths: ```text $ codex --strict-config --enable does_not_exist Error: Unknown feature flag: does_not_exist $ codex --strict-config --disable does_not_exist Error: Unknown feature flag: does_not_exist $ codex --strict-config --enable multi_agent_v2.subagent_usage_hint_text Error: Unknown feature flag: multi_agent_v2.subagent_usage_hint_text ``` Unsupported commands reject the flag explicitly: ```text $ codex --strict-config cloud list Error: `--strict-config` is not supported for `codex cloud` ``` ## Verification The `codex-cli` `strict_config` tests cover invalid `--enable`, invalid `--disable`, the compound `multi_agent_v2.subagent_usage_hint_text` case, unknown `-c` overrides, app-server strict startup failure through `codex app-server`, and rejection for unsupported commands such as `codex cloud`, `codex mcp`, `codex remote-control`, and `codex app-server proxy`. The config and config-loader tests cover unknown top-level fields, unknown nested fields, unknown `[features]` keys, source-location reporting, non-file managed config sources, and `-c` validation for keys such as `features.foo`. The app-server test suite covers standalone `codex-app-server --strict-config` startup failure for an unknown config field. ## Documentation The Codex CLI docs on developers.openai.com/codex should mention `--strict-config` as an opt-in validation mode for supported config-heavy entry points once this ships.
247 lines
8.5 KiB
Rust
247 lines
8.5 KiB
Rust
use crate::config_manager::ConfigManager;
|
|
use codex_core::CodexThread;
|
|
use codex_core::ThreadManager;
|
|
use codex_core::config::Config;
|
|
use codex_protocol::ThreadId;
|
|
use codex_protocol::protocol::McpServerRefreshConfig;
|
|
use codex_protocol::protocol::Op;
|
|
use std::io;
|
|
use std::sync::Arc;
|
|
use tracing::warn;
|
|
|
|
pub(crate) async fn queue_strict_refresh(
|
|
thread_manager: &Arc<ThreadManager>,
|
|
config_manager: &ConfigManager,
|
|
) -> io::Result<()> {
|
|
config_manager
|
|
.load_latest_config(/*fallback_cwd*/ None)
|
|
.await?;
|
|
let mut refreshes = Vec::new();
|
|
for thread_id in thread_manager.list_thread_ids().await {
|
|
let thread = thread_manager
|
|
.get_thread(thread_id)
|
|
.await
|
|
.map_err(|err| io::Error::other(format!("failed to load thread {thread_id}: {err}")))?;
|
|
let config =
|
|
build_refresh_config(thread_manager, config_manager, thread.config().await).await?;
|
|
refreshes.push((thread_id, thread, config));
|
|
}
|
|
for (thread_id, thread, config) in refreshes {
|
|
queue_refresh(thread_id, thread, config).await?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub(crate) async fn queue_best_effort_refresh(
|
|
thread_manager: &Arc<ThreadManager>,
|
|
config_manager: &ConfigManager,
|
|
) {
|
|
for thread_id in thread_manager.list_thread_ids().await {
|
|
let thread = match thread_manager.get_thread(thread_id).await {
|
|
Ok(thread) => thread,
|
|
Err(err) => {
|
|
warn!("failed to load thread {thread_id} for MCP refresh: {err}");
|
|
continue;
|
|
}
|
|
};
|
|
let config =
|
|
match build_refresh_config(thread_manager, config_manager, thread.config().await).await
|
|
{
|
|
Ok(config) => config,
|
|
Err(err) => {
|
|
warn!("failed to build MCP refresh config for thread {thread_id}: {err}");
|
|
continue;
|
|
}
|
|
};
|
|
if let Err(err) = queue_refresh(thread_id, thread, config).await {
|
|
warn!("{err}");
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn build_refresh_config(
|
|
thread_manager: &ThreadManager,
|
|
config_manager: &ConfigManager,
|
|
thread_config: Arc<Config>,
|
|
) -> io::Result<McpServerRefreshConfig> {
|
|
let config = config_manager
|
|
.load_latest_config_for_thread(thread_config.as_ref())
|
|
.await?;
|
|
let mcp_servers = thread_manager
|
|
.mcp_manager()
|
|
.configured_servers(&config)
|
|
.await;
|
|
Ok(McpServerRefreshConfig {
|
|
mcp_servers: serde_json::to_value(mcp_servers).map_err(io::Error::other)?,
|
|
mcp_oauth_credentials_store_mode: serde_json::to_value(
|
|
config.mcp_oauth_credentials_store_mode,
|
|
)
|
|
.map_err(io::Error::other)?,
|
|
})
|
|
}
|
|
|
|
async fn queue_refresh(
|
|
thread_id: ThreadId,
|
|
thread: Arc<CodexThread>,
|
|
config: McpServerRefreshConfig,
|
|
) -> io::Result<()> {
|
|
thread
|
|
.submit(Op::RefreshMcpServers { config })
|
|
.await
|
|
.map(|_| ())
|
|
.map_err(|err| {
|
|
io::Error::other(format!(
|
|
"failed to queue MCP refresh for thread {thread_id}: {err}"
|
|
))
|
|
})
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::extensions::guardian_agent_spawner;
|
|
use crate::extensions::thread_extensions;
|
|
use async_trait::async_trait;
|
|
use codex_arg0::Arg0DispatchPaths;
|
|
use codex_config::CloudRequirementsLoader;
|
|
use codex_config::LoaderOverrides;
|
|
use codex_config::ThreadConfigContext;
|
|
use codex_config::ThreadConfigLoadError;
|
|
use codex_config::ThreadConfigLoadErrorCode;
|
|
use codex_config::ThreadConfigLoader;
|
|
use codex_config::ThreadConfigSource;
|
|
use codex_core::config::ConfigOverrides;
|
|
use codex_core::init_state_db;
|
|
use codex_core::thread_store_from_config;
|
|
use codex_exec_server::EnvironmentManager;
|
|
use codex_login::AuthManager;
|
|
use codex_login::CodexAuth;
|
|
use codex_protocol::protocol::SessionSource;
|
|
use codex_utils_absolute_path::AbsolutePathBuf;
|
|
use pretty_assertions::assert_eq;
|
|
use std::sync::atomic::AtomicUsize;
|
|
use std::sync::atomic::Ordering;
|
|
use tempfile::TempDir;
|
|
|
|
#[tokio::test]
|
|
async fn strict_refresh_reports_thread_planning_failures() -> anyhow::Result<()> {
|
|
let (_temp_dir, thread_manager, config_manager, _loader) = refresh_test_state().await?;
|
|
|
|
let err = queue_strict_refresh(&thread_manager, &config_manager)
|
|
.await
|
|
.expect_err("strict refresh should fail");
|
|
|
|
assert_eq!(err.to_string(), "failed to load refresh config");
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn best_effort_refresh_attempts_every_loaded_thread() -> anyhow::Result<()> {
|
|
let (_temp_dir, thread_manager, config_manager, loader) = refresh_test_state().await?;
|
|
|
|
queue_best_effort_refresh(&thread_manager, &config_manager).await;
|
|
|
|
assert_eq!(loader.good_loads.load(Ordering::Relaxed), 1);
|
|
assert_eq!(loader.bad_loads.load(Ordering::Relaxed), 1);
|
|
Ok(())
|
|
}
|
|
|
|
async fn refresh_test_state() -> anyhow::Result<(
|
|
TempDir,
|
|
Arc<ThreadManager>,
|
|
ConfigManager,
|
|
Arc<CountingThreadConfigLoader>,
|
|
)> {
|
|
let temp_dir = TempDir::new()?;
|
|
let good_cwd = temp_dir.path().join("good");
|
|
let bad_cwd = temp_dir.path().join("bad");
|
|
std::fs::create_dir_all(&good_cwd)?;
|
|
std::fs::create_dir_all(&bad_cwd)?;
|
|
|
|
let initial_config_manager =
|
|
ConfigManager::without_managed_config_for_tests(temp_dir.path().to_path_buf());
|
|
let good_config = initial_config_manager
|
|
.load_for_cwd(
|
|
/*request_overrides*/ None,
|
|
ConfigOverrides::default(),
|
|
Some(good_cwd.clone()),
|
|
)
|
|
.await?;
|
|
let bad_config = initial_config_manager
|
|
.load_for_cwd(
|
|
/*request_overrides*/ None,
|
|
ConfigOverrides::default(),
|
|
Some(bad_cwd.clone()),
|
|
)
|
|
.await?;
|
|
|
|
let auth_manager = AuthManager::from_auth_for_testing(CodexAuth::from_api_key("dummy"));
|
|
let state_db = init_state_db(&good_config)
|
|
.await
|
|
.expect("refresh tests require state db");
|
|
let thread_store = thread_store_from_config(&good_config, Some(state_db.clone()));
|
|
let thread_manager = Arc::new_cyclic(|thread_manager| {
|
|
ThreadManager::new(
|
|
&good_config,
|
|
auth_manager,
|
|
SessionSource::Exec,
|
|
Arc::new(EnvironmentManager::default_for_tests()),
|
|
thread_extensions(guardian_agent_spawner(thread_manager.clone())),
|
|
/*analytics_events_client*/ None,
|
|
thread_store,
|
|
Some(state_db.clone()),
|
|
"11111111-1111-4111-8111-111111111111".to_string(),
|
|
/*attestation_provider*/ None,
|
|
)
|
|
});
|
|
thread_manager.start_thread(good_config).await?;
|
|
thread_manager.start_thread(bad_config).await?;
|
|
|
|
let loader = Arc::new(CountingThreadConfigLoader {
|
|
good_cwd: AbsolutePathBuf::try_from(good_cwd)?,
|
|
bad_cwd: AbsolutePathBuf::try_from(bad_cwd)?,
|
|
good_loads: AtomicUsize::new(0),
|
|
bad_loads: AtomicUsize::new(0),
|
|
});
|
|
let config_manager = ConfigManager::new(
|
|
temp_dir.path().to_path_buf(),
|
|
Vec::new(),
|
|
LoaderOverrides::without_managed_config_for_tests(),
|
|
/*strict_config*/ false,
|
|
CloudRequirementsLoader::default(),
|
|
Arg0DispatchPaths::default(),
|
|
loader.clone(),
|
|
);
|
|
|
|
Ok((temp_dir, thread_manager, config_manager, loader))
|
|
}
|
|
|
|
struct CountingThreadConfigLoader {
|
|
good_cwd: AbsolutePathBuf,
|
|
bad_cwd: AbsolutePathBuf,
|
|
good_loads: AtomicUsize,
|
|
bad_loads: AtomicUsize,
|
|
}
|
|
|
|
#[async_trait]
|
|
impl ThreadConfigLoader for CountingThreadConfigLoader {
|
|
async fn load(
|
|
&self,
|
|
context: ThreadConfigContext,
|
|
) -> Result<Vec<ThreadConfigSource>, ThreadConfigLoadError> {
|
|
if context.cwd.as_ref() == Some(&self.good_cwd) {
|
|
self.good_loads.fetch_add(1, Ordering::Relaxed);
|
|
}
|
|
if context.cwd.as_ref() == Some(&self.bad_cwd) {
|
|
self.bad_loads.fetch_add(1, Ordering::Relaxed);
|
|
return Err(ThreadConfigLoadError::new(
|
|
ThreadConfigLoadErrorCode::Internal,
|
|
/*status_code*/ None,
|
|
"failed to load refresh config",
|
|
));
|
|
}
|
|
Ok(Vec::new())
|
|
}
|
|
}
|
|
}
|