From cf24cd5a93ec4d03f55dbe20972ee1fa55a776ca Mon Sep 17 00:00:00 2001 From: Benjamin Carlsson Date: Tue, 1 Dec 2026 23:26:38 +0000 Subject: [PATCH] Centralize CLI config flag expansion (#65134) ## What changed Expand root feature toggles and auto-review flags in the config argument module before dispatch. Keep auto-review expansion for `resume` in its command-specific path. ## Testing Extend the `mcp add` argument test to check that a root `--disable` follows the collected `-c` overrides and that the command-scoped override still takes precedence. The test also checks that `-c` after `--` is forwarded to the MCP server command. GitOrigin-RevId: 6a7b2a8dddb9043dbfde6b8a20cd98cef5b39ab0 --- codex-rs/cli/src/config_override_args.rs | 17 ++++++- .../cli/src/config_override_args_tests.rs | 34 +++++++++++--- codex-rs/cli/src/main.rs | 46 ++++++++----------- 3 files changed, 63 insertions(+), 34 deletions(-) diff --git a/codex-rs/cli/src/config_override_args.rs b/codex-rs/cli/src/config_override_args.rs index 26f86a8e6b..bf87a77bde 100644 --- a/codex-rs/cli/src/config_override_args.rs +++ b/codex-rs/cli/src/config_override_args.rs @@ -1,6 +1,7 @@ -//! Preserve config overrides across command boundaries without changing typed child arguments. +//! Collect explicit config arguments in order before expanding root launch flags. use crate::MultitoolCli; +use crate::Subcommand; use clap::CommandFactory; use clap::FromArgMatches; use codex_utils_cli::collect_config_overrides; @@ -24,6 +25,20 @@ pub(super) fn try_parse_from( Ok(cli) } +/// Before expansion, the root contains every explicit `-c` exactly once. +/// Child copies still participate in command-specific precedence during dispatch. +pub(super) fn expand_flags(cli: &mut MultitoolCli) -> anyhow::Result<()> { + cli.config_overrides + .raw_overrides + .extend(cli.feature_toggles.to_overrides()?); + if !matches!(cli.subcommand, Some(Subcommand::Resume(_))) { + cli.interactive + .shared + .take_auto_review_config_overrides(&mut cli.config_overrides); + } + Ok(()) +} + #[cfg(test)] #[path = "config_override_args_tests.rs"] mod tests; diff --git a/codex-rs/cli/src/config_override_args_tests.rs b/codex-rs/cli/src/config_override_args_tests.rs index 54bdcc7866..4818b128f3 100644 --- a/codex-rs/cli/src/config_override_args_tests.rs +++ b/codex-rs/cli/src/config_override_args_tests.rs @@ -7,26 +7,46 @@ use pretty_assertions::assert_eq; #[test] fn preserves_scoped_overrides_and_forwarded_arguments() { - let cli = try_parse_from([ + let mut cli = try_parse_from([ "codex", - "--config=a=1", + "--config=features.unified_exec=true", + "--disable", + "unified_exec", "mcp", - "-cb=2", + "-cmodel=example", "add", "example", "-c", - "a=3", + "features.unified_exec=true", "--", "program", "-c", "payload", ]) .expect("parse"); - assert_eq!(cli.config_overrides.raw_overrides, ["a=1", "b=2", "a=3"]); - let Some(Subcommand::Mcp(mcp)) = cli.subcommand else { + assert_eq!( + cli.config_overrides.raw_overrides, + [ + "features.unified_exec=true", + "model=example", + "features.unified_exec=true" + ] + ); + super::expand_flags(&mut cli).expect("expand"); + let Some(Subcommand::Mcp(mut mcp)) = cli.subcommand else { panic!("expected mcp"); }; - assert_eq!(mcp.config_overrides.raw_overrides, ["a=3"]); + crate::prepend_config_flags(&mut mcp.config_overrides, cli.config_overrides); + assert_eq!( + mcp.config_overrides.raw_overrides, + [ + "features.unified_exec=true", + "model=example", + "features.unified_exec=true", + "features.unified_exec=false", + "features.unified_exec=true" + ] + ); let McpSubcommand::Add(add) = mcp.subcommand else { panic!("expected add"); }; diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index 4a75c36ccf..c10b225c52 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -1068,14 +1068,31 @@ async fn dispatch_cli( arg0_paths: Arg0DispatchPaths, remote_control_disabled: bool, ) -> anyhow::Result>>> { + let mut cli = config_override_args::try_parse_from(std::env::args_os()) + .unwrap_or_else(|error| error.exit()); + if (cli.interactive.cloud.enabled || cli.interactive.cloud.environment.is_some()) + && !matches!( + cli.subcommand, + None | Some(Subcommand::Agents(_) | Subcommand::Resume(_)) + ) + { + anyhow::bail!("--cloud supports interactive sessions, agents, and resume"); + } + if let Some(Subcommand::Fork(command)) = &cli.subcommand + && (command.config_overrides.0.cloud.enabled + || command.config_overrides.0.cloud.environment.is_some()) + { + anyhow::bail!("Cloud does not support fork"); + } + reject_unsupported_worktree_for_subcommand(cli.interactive.shared.worktree, &cli.subcommand)?; + config_override_args::expand_flags(&mut cli)?; let MultitoolCli { config_overrides: mut root_config_overrides, - feature_toggles, + feature_toggles: _, remote, mut interactive, subcommand, - } = config_override_args::try_parse_from(std::env::args_os()) - .unwrap_or_else(|error| error.exit()); + } = cli; // Retain the launch target through TUI exit, even if a launcher changes selection. let daemon_cli_executable = arg0_paths .codex_self_exe @@ -1084,24 +1101,6 @@ async fn dispatch_cli( interactive.daemon_cli_executable = daemon_cli_executable .clone() .and_then(|path| AbsolutePathBuf::from_absolute_path(path).ok()); - if (interactive.cloud.enabled || interactive.cloud.environment.is_some()) - && !matches!( - subcommand, - None | Some(Subcommand::Agents(_) | Subcommand::Resume(_)) - ) - { - anyhow::bail!("--cloud supports interactive sessions, agents, and resume"); - } - if let Some(Subcommand::Fork(command)) = &subcommand - && (command.config_overrides.0.cloud.enabled - || command.config_overrides.0.cloud.environment.is_some()) - { - anyhow::bail!("Cloud does not support fork"); - } - reject_unsupported_worktree_for_subcommand(interactive.shared.worktree, &subcommand)?; - // Fold --enable/--disable into config overrides so they flow to all subcommands. - let toggle_overrides = feature_toggles.to_overrides()?; - root_config_overrides.raw_overrides.extend(toggle_overrides); let agents_options = match &subcommand { Some(Subcommand::Agents(options)) => Some(options), _ => None, @@ -1134,11 +1133,6 @@ async fn dispatch_cli( interactive.no_daemon |= options.no_daemon; } let root_strict_config = interactive.strict_config; - if !matches!(subcommand, Some(Subcommand::Resume(_))) { - interactive - .shared - .take_auto_review_config_overrides(&mut root_config_overrides); - } reject_root_strict_config_for_subcommand(root_strict_config, &subcommand)?; if let Some(subcommand) = subcommand.as_ref() { profile_v2_for_subcommand(&interactive, subcommand)?;