mirror of
https://github.com/openai/codex.git
synced 2026-05-15 08:42:34 +00:00
## 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.
312 lines
9.2 KiB
Rust
312 lines
9.2 KiB
Rust
use clap::Args;
|
||
use clap::FromArgMatches;
|
||
use clap::Parser;
|
||
use clap::ValueEnum;
|
||
use codex_utils_cli::CliConfigOverrides;
|
||
use codex_utils_cli::SharedCliOptions;
|
||
use std::path::PathBuf;
|
||
|
||
#[derive(Parser, Debug)]
|
||
#[command(
|
||
version,
|
||
override_usage = "codex exec [OPTIONS] [PROMPT]\n codex exec [OPTIONS] <COMMAND> [ARGS]"
|
||
)]
|
||
pub struct Cli {
|
||
/// Action to perform. If omitted, runs a new non-interactive session.
|
||
#[command(subcommand)]
|
||
pub command: Option<Command>,
|
||
|
||
/// Error out when config.toml contains fields that are not recognized by this version of Codex.
|
||
#[arg(long = "strict-config", global = true, default_value_t = false)]
|
||
pub strict_config: bool,
|
||
|
||
#[clap(flatten)]
|
||
pub shared: ExecSharedCliOptions,
|
||
|
||
/// Allow running Codex outside a Git repository.
|
||
#[arg(long = "skip-git-repo-check", global = true, default_value_t = false)]
|
||
pub skip_git_repo_check: bool,
|
||
|
||
/// Run without persisting session files to disk.
|
||
#[arg(long = "ephemeral", global = true, default_value_t = false)]
|
||
pub ephemeral: bool,
|
||
|
||
/// Do not load `$CODEX_HOME/config.toml`; auth still uses `CODEX_HOME`.
|
||
#[arg(long = "ignore-user-config", global = true, default_value_t = false)]
|
||
pub ignore_user_config: bool,
|
||
|
||
/// Do not load user or project execpolicy `.rules` files.
|
||
#[arg(long = "ignore-rules", global = true, default_value_t = false)]
|
||
pub ignore_rules: bool,
|
||
|
||
/// Legacy compatibility trap for the removed `--full-auto` flag.
|
||
#[arg(
|
||
long = "full-auto",
|
||
hide = true,
|
||
global = true,
|
||
default_value_t = false,
|
||
conflicts_with = "dangerously_bypass_approvals_and_sandbox"
|
||
)]
|
||
pub removed_full_auto: bool,
|
||
|
||
/// Path to a JSON Schema file describing the model's final response shape.
|
||
#[arg(long = "output-schema", value_name = "FILE")]
|
||
pub output_schema: Option<PathBuf>,
|
||
|
||
#[clap(skip)]
|
||
pub config_overrides: CliConfigOverrides,
|
||
|
||
/// Specifies color settings for use in the output.
|
||
#[arg(long = "color", value_enum, default_value_t = Color::Auto)]
|
||
pub color: Color,
|
||
|
||
/// Print events to stdout as JSONL.
|
||
#[arg(
|
||
long = "json",
|
||
alias = "experimental-json",
|
||
default_value_t = false,
|
||
global = true
|
||
)]
|
||
pub json: bool,
|
||
|
||
/// Specifies file where the last message from the agent should be written.
|
||
#[arg(
|
||
long = "output-last-message",
|
||
short = 'o',
|
||
value_name = "FILE",
|
||
global = true
|
||
)]
|
||
pub last_message_file: Option<PathBuf>,
|
||
|
||
/// Initial instructions for the agent. If not provided as an argument (or
|
||
/// if `-` is used), instructions are read from stdin. If stdin is piped and
|
||
/// a prompt is also provided, stdin is appended as a `<stdin>` block.
|
||
#[arg(value_name = "PROMPT", value_hint = clap::ValueHint::Other)]
|
||
pub prompt: Option<String>,
|
||
}
|
||
|
||
impl std::ops::Deref for Cli {
|
||
type Target = SharedCliOptions;
|
||
|
||
fn deref(&self) -> &Self::Target {
|
||
&self.shared.0
|
||
}
|
||
}
|
||
|
||
impl std::ops::DerefMut for Cli {
|
||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||
&mut self.shared.0
|
||
}
|
||
}
|
||
|
||
impl Cli {
|
||
pub fn removed_full_auto_warning(&self) -> Option<&'static str> {
|
||
if self.removed_full_auto {
|
||
return Some(
|
||
"warning: `--full-auto` is deprecated; use `--sandbox workspace-write` instead.",
|
||
);
|
||
}
|
||
|
||
None
|
||
}
|
||
}
|
||
|
||
#[derive(Debug, Default)]
|
||
pub struct ExecSharedCliOptions(SharedCliOptions);
|
||
|
||
impl ExecSharedCliOptions {
|
||
pub fn into_inner(self) -> SharedCliOptions {
|
||
self.0
|
||
}
|
||
}
|
||
|
||
impl std::ops::Deref for ExecSharedCliOptions {
|
||
type Target = SharedCliOptions;
|
||
|
||
fn deref(&self) -> &Self::Target {
|
||
&self.0
|
||
}
|
||
}
|
||
|
||
impl std::ops::DerefMut for ExecSharedCliOptions {
|
||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||
&mut self.0
|
||
}
|
||
}
|
||
|
||
impl Args for ExecSharedCliOptions {
|
||
fn augment_args(cmd: clap::Command) -> clap::Command {
|
||
mark_exec_global_args(SharedCliOptions::augment_args(cmd))
|
||
}
|
||
|
||
fn augment_args_for_update(cmd: clap::Command) -> clap::Command {
|
||
mark_exec_global_args(SharedCliOptions::augment_args_for_update(cmd))
|
||
}
|
||
}
|
||
|
||
impl FromArgMatches for ExecSharedCliOptions {
|
||
fn from_arg_matches(matches: &clap::ArgMatches) -> Result<Self, clap::Error> {
|
||
SharedCliOptions::from_arg_matches(matches).map(Self)
|
||
}
|
||
|
||
fn update_from_arg_matches(&mut self, matches: &clap::ArgMatches) -> Result<(), clap::Error> {
|
||
self.0.update_from_arg_matches(matches)
|
||
}
|
||
}
|
||
|
||
fn mark_exec_global_args(cmd: clap::Command) -> clap::Command {
|
||
cmd.mut_arg("model", |arg| arg.global(true))
|
||
.mut_arg("dangerously_bypass_approvals_and_sandbox", |arg| {
|
||
arg.global(true)
|
||
})
|
||
.mut_arg("bypass_hook_trust", |arg| arg.global(true))
|
||
}
|
||
|
||
#[derive(Debug, clap::Subcommand)]
|
||
pub enum Command {
|
||
/// Resume a previous session by id or pick the most recent with --last.
|
||
Resume(ResumeArgs),
|
||
|
||
/// Run a code review against the current repository.
|
||
Review(ReviewArgs),
|
||
}
|
||
|
||
#[derive(Args, Debug)]
|
||
struct ResumeArgsRaw {
|
||
// Note: This is the direct clap shape. We reinterpret the positional when --last is set
|
||
// so "codex resume --last <prompt>" treats the positional as a prompt, not a session id.
|
||
/// Conversation/session id (UUID) or thread name. UUIDs take precedence if it parses.
|
||
/// If omitted, use --last to pick the most recent recorded session.
|
||
#[arg(value_name = "SESSION_ID")]
|
||
session_id: Option<String>,
|
||
|
||
/// Resume the most recent recorded session (newest) without specifying an id.
|
||
#[arg(long = "last", default_value_t = false)]
|
||
last: bool,
|
||
|
||
/// Show all sessions (disables cwd filtering).
|
||
#[arg(long = "all", default_value_t = false)]
|
||
all: bool,
|
||
|
||
/// Optional image(s) to attach to the prompt sent after resuming.
|
||
#[arg(
|
||
long = "image",
|
||
short = 'i',
|
||
value_name = "FILE",
|
||
value_delimiter = ',',
|
||
num_args = 1
|
||
)]
|
||
images: Vec<PathBuf>,
|
||
|
||
/// Prompt to send after resuming the session. If `-` is used, read from stdin.
|
||
#[arg(value_name = "PROMPT", value_hint = clap::ValueHint::Other)]
|
||
prompt: Option<String>,
|
||
}
|
||
|
||
#[derive(Debug)]
|
||
pub struct ResumeArgs {
|
||
/// Conversation/session id (UUID) or thread name. UUIDs take precedence if it parses.
|
||
/// If omitted, use --last to pick the most recent recorded session.
|
||
pub session_id: Option<String>,
|
||
|
||
/// Resume the most recent recorded session (newest) without specifying an id.
|
||
pub last: bool,
|
||
|
||
/// Show all sessions (disables cwd filtering).
|
||
pub all: bool,
|
||
|
||
/// Optional image(s) to attach to the prompt sent after resuming.
|
||
pub images: Vec<PathBuf>,
|
||
|
||
/// Prompt to send after resuming the session. If `-` is used, read from stdin.
|
||
pub prompt: Option<String>,
|
||
}
|
||
|
||
impl From<ResumeArgsRaw> for ResumeArgs {
|
||
fn from(raw: ResumeArgsRaw) -> Self {
|
||
// When --last is used without an explicit prompt, treat the positional as the prompt
|
||
// (clap can’t express this conditional positional meaning cleanly).
|
||
let (session_id, prompt) = if raw.last && raw.prompt.is_none() {
|
||
(None, raw.session_id)
|
||
} else {
|
||
(raw.session_id, raw.prompt)
|
||
};
|
||
Self {
|
||
session_id,
|
||
last: raw.last,
|
||
all: raw.all,
|
||
images: raw.images,
|
||
prompt,
|
||
}
|
||
}
|
||
}
|
||
|
||
impl Args for ResumeArgs {
|
||
fn augment_args(cmd: clap::Command) -> clap::Command {
|
||
ResumeArgsRaw::augment_args(cmd)
|
||
}
|
||
|
||
fn augment_args_for_update(cmd: clap::Command) -> clap::Command {
|
||
ResumeArgsRaw::augment_args_for_update(cmd)
|
||
}
|
||
}
|
||
|
||
impl FromArgMatches for ResumeArgs {
|
||
fn from_arg_matches(matches: &clap::ArgMatches) -> Result<Self, clap::Error> {
|
||
ResumeArgsRaw::from_arg_matches(matches).map(Self::from)
|
||
}
|
||
|
||
fn update_from_arg_matches(&mut self, matches: &clap::ArgMatches) -> Result<(), clap::Error> {
|
||
*self = ResumeArgsRaw::from_arg_matches(matches).map(Self::from)?;
|
||
Ok(())
|
||
}
|
||
}
|
||
|
||
#[derive(Args, Debug)]
|
||
pub struct ReviewArgs {
|
||
/// Review staged, unstaged, and untracked changes.
|
||
#[arg(
|
||
long = "uncommitted",
|
||
default_value_t = false,
|
||
conflicts_with_all = ["base", "commit", "prompt"]
|
||
)]
|
||
pub uncommitted: bool,
|
||
|
||
/// Review changes against the given base branch.
|
||
#[arg(
|
||
long = "base",
|
||
value_name = "BRANCH",
|
||
conflicts_with_all = ["uncommitted", "commit", "prompt"]
|
||
)]
|
||
pub base: Option<String>,
|
||
|
||
/// Review the changes introduced by a commit.
|
||
#[arg(
|
||
long = "commit",
|
||
value_name = "SHA",
|
||
conflicts_with_all = ["uncommitted", "base", "prompt"]
|
||
)]
|
||
pub commit: Option<String>,
|
||
|
||
/// Optional commit title to display in the review summary.
|
||
#[arg(long = "title", value_name = "TITLE", requires = "commit")]
|
||
pub commit_title: Option<String>,
|
||
|
||
/// Custom review instructions. If `-` is used, read from stdin.
|
||
#[arg(value_name = "PROMPT", value_hint = clap::ValueHint::Other)]
|
||
pub prompt: Option<String>,
|
||
}
|
||
|
||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, ValueEnum)]
|
||
#[value(rename_all = "kebab-case")]
|
||
pub enum Color {
|
||
Always,
|
||
Never,
|
||
#[default]
|
||
Auto,
|
||
}
|
||
|
||
#[cfg(test)]
|
||
#[path = "cli_tests.rs"]
|
||
mod tests;
|