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
This commit is contained in:
Benjamin Carlsson
2026-12-02 01:42:03 +00:00
committed by copyberry
parent b444ea738c
commit cf24cd5a93
3 changed files with 63 additions and 34 deletions
+16 -1
View File
@@ -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;
+27 -7
View File
@@ -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");
};
+20 -26
View File
@@ -1068,14 +1068,31 @@ async fn dispatch_cli(
arg0_paths: Arg0DispatchPaths,
remote_control_disabled: bool,
) -> anyhow::Result<Option<impl Future<Output = 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)?;