From 300d93ed36c692cbcde3fbf0cbd2d9a50c4b0ea7 Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Fri, 13 Mar 2026 19:20:34 -0700 Subject: [PATCH] refactor: clarify ambiguous literal argument APIs Use named enums and methods where this follow-up touches real code, and reframe the new argument-comment lint examples around legacy APIs so the docs do not present `bool` and `Option` positional literals as the preferred design. Co-authored-by: Codex --- codex-rs/cli/src/main.rs | 4 +- codex-rs/core/src/config/edit.rs | 15 +++++-- codex-rs/core/src/memories/usage.rs | 6 ++- .../core/src/tools/handlers/unified_exec.rs | 27 +++++++++++-- .../src/tools/handlers/unified_exec_tests.rs | 40 ++++++++++++++----- codex-rs/tui/src/app.rs | 14 ++++--- codex-rs/tui/src/chatwidget.rs | 8 ++++ tools/argument-comment-lint/README.md | 31 +++++++++++--- tools/argument-comment-lint/src/lib.rs | 22 ++++++---- .../ui/comment_matches.rs | 6 +-- .../ui/comment_matches_multiline.rs | 4 +- .../ui/comment_mismatch.rs | 4 +- .../ui/comment_mismatch.stderr | 6 +-- .../ui/uncommented_literal.rs | 8 ++-- .../ui/uncommented_literal.stderr | 18 ++++----- 15 files changed, 151 insertions(+), 62 deletions(-) diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index d6f2681e53..e689c686f0 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -841,7 +841,7 @@ async fn enable_feature_in_config(interactive: &TuiCli, feature: &str) -> anyhow let codex_home = find_codex_home()?; ConfigEditsBuilder::new(&codex_home) .with_profile(interactive.config_profile.as_deref()) - .set_feature_enabled(feature, true) + .enable_feature(feature) .apply() .await?; println!("Enabled feature `{feature}` in config.toml."); @@ -854,7 +854,7 @@ async fn disable_feature_in_config(interactive: &TuiCli, feature: &str) -> anyho let codex_home = find_codex_home()?; ConfigEditsBuilder::new(&codex_home) .with_profile(interactive.config_profile.as_deref()) - .set_feature_enabled(feature, false) + .disable_feature(feature) .apply() .await?; println!("Disabled feature `{feature}` in config.toml."); diff --git a/codex-rs/core/src/config/edit.rs b/codex-rs/core/src/config/edit.rs index 8d0011b5e8..4b9fd19d94 100644 --- a/codex-rs/core/src/config/edit.rs +++ b/codex-rs/core/src/config/edit.rs @@ -857,11 +857,20 @@ impl ConfigEditsBuilder { self } - /// Enable or disable a feature flag by key under the `[features]` table. - pub fn set_feature_enabled(mut self, key: &str, enabled: bool) -> Self { + /// Enable a feature flag by key under the `[features]` table. + pub fn enable_feature(mut self, key: &str) -> Self { self.edits.push(ConfigEdit::SetPath { segments: vec!["features".to_string(), key.to_string()], - value: value(enabled), + value: value(true), + }); + self + } + + /// Disable a feature flag by key under the `[features]` table. + pub fn disable_feature(mut self, key: &str) -> Self { + self.edits.push(ConfigEdit::SetPath { + segments: vec!["features".to_string(), key.to_string()], + value: value(false), }); self } diff --git a/codex-rs/core/src/memories/usage.rs b/codex-rs/core/src/memories/usage.rs index 8a86babb42..bf2cc1b946 100644 --- a/codex-rs/core/src/memories/usage.rs +++ b/codex-rs/core/src/memories/usage.rs @@ -102,7 +102,11 @@ fn shell_command_for_invocation(invocation: &ToolInvocation) -> Option<(Vec command, Err(_) => return true, @@ -154,7 +158,11 @@ impl ToolHandler for UnifiedExecHandler { let command = get_command( &args, session.user_shell(), - turn.tools_config.allow_login_shell, + if turn.tools_config.allow_login_shell { + LoginShellPolicy::Allow + } else { + LoginShellPolicy::Disallow + }, ) .map_err(FunctionCallError::RespondToModel)?; @@ -317,10 +325,22 @@ impl ToolHandler for UnifiedExecHandler { } } +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(crate) enum LoginShellPolicy { + Allow, + Disallow, +} + +impl LoginShellPolicy { + fn allows_login_shell(self) -> bool { + matches!(self, Self::Allow) + } +} + pub(crate) fn get_command( args: &ExecCommandArgs, session_shell: Arc, - allow_login_shell: bool, + login_shell_policy: LoginShellPolicy, ) -> Result, String> { let model_shell = args.shell.as_ref().map(|shell_str| { let mut shell = get_shell_by_model_provided_path(&PathBuf::from(shell_str)); @@ -329,6 +349,7 @@ pub(crate) fn get_command( }); let shell = model_shell.as_ref().unwrap_or(session_shell.as_ref()); + let allow_login_shell = login_shell_policy.allows_login_shell(); let use_login_shell = match args.login { Some(true) if !allow_login_shell => { return Err( diff --git a/codex-rs/core/src/tools/handlers/unified_exec_tests.rs b/codex-rs/core/src/tools/handlers/unified_exec_tests.rs index ee31aefc14..6a9cd0b3c9 100644 --- a/codex-rs/core/src/tools/handlers/unified_exec_tests.rs +++ b/codex-rs/core/src/tools/handlers/unified_exec_tests.rs @@ -18,8 +18,12 @@ fn test_get_command_uses_default_shell_when_unspecified() -> anyhow::Result<()> assert!(args.shell.is_none()); - let command = - get_command(&args, Arc::new(default_user_shell()), true).map_err(anyhow::Error::msg)?; + let command = get_command( + &args, + Arc::new(default_user_shell()), + LoginShellPolicy::Allow, + ) + .map_err(anyhow::Error::msg)?; assert_eq!(command.len(), 3); assert_eq!(command[2], "echo hello"); @@ -34,8 +38,12 @@ fn test_get_command_respects_explicit_bash_shell() -> anyhow::Result<()> { assert_eq!(args.shell.as_deref(), Some("/bin/bash")); - let command = - get_command(&args, Arc::new(default_user_shell()), true).map_err(anyhow::Error::msg)?; + let command = get_command( + &args, + Arc::new(default_user_shell()), + LoginShellPolicy::Allow, + ) + .map_err(anyhow::Error::msg)?; assert_eq!(command.last(), Some(&"echo hello".to_string())); if command @@ -55,8 +63,12 @@ fn test_get_command_respects_explicit_powershell_shell() -> anyhow::Result<()> { assert_eq!(args.shell.as_deref(), Some("powershell")); - let command = - get_command(&args, Arc::new(default_user_shell()), true).map_err(anyhow::Error::msg)?; + let command = get_command( + &args, + Arc::new(default_user_shell()), + LoginShellPolicy::Allow, + ) + .map_err(anyhow::Error::msg)?; assert_eq!(command[2], "echo hello"); Ok(()) @@ -70,8 +82,12 @@ fn test_get_command_respects_explicit_cmd_shell() -> anyhow::Result<()> { assert_eq!(args.shell.as_deref(), Some("cmd")); - let command = - get_command(&args, Arc::new(default_user_shell()), true).map_err(anyhow::Error::msg)?; + let command = get_command( + &args, + Arc::new(default_user_shell()), + LoginShellPolicy::Allow, + ) + .map_err(anyhow::Error::msg)?; assert_eq!(command[2], "echo hello"); Ok(()) @@ -82,8 +98,12 @@ fn test_get_command_rejects_explicit_login_when_disallowed() -> anyhow::Result<( let json = r#"{"cmd": "echo hello", "login": true}"#; let args: ExecCommandArgs = parse_arguments(json)?; - let err = get_command(&args, Arc::new(default_user_shell()), false) - .expect_err("explicit login should be rejected"); + let err = get_command( + &args, + Arc::new(default_user_shell()), + LoginShellPolicy::Disallow, + ) + .expect_err("explicit login should be rejected"); assert!( err.contains("login shell is disabled by config"), diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index a125ed7d78..e126efe05c 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -860,12 +860,15 @@ impl App { continue; } let effective_enabled = self.config.features.enabled(feature); - self.chat_widget - .set_feature_enabled(feature, effective_enabled); if effective_enabled { - builder = builder.set_feature_enabled(feature_key, true); + self.chat_widget.enable_feature(feature); + } else { + self.chat_widget.disable_feature(feature); + } + if effective_enabled { + builder = builder.enable_feature(feature_key); } else if feature.default_enabled() { - builder = builder.set_feature_enabled(feature_key, false); + builder = builder.disable_feature(feature_key); } else { // If the feature already default to `false`, we drop the key // in the config file so that the user does not miss the feature @@ -5156,8 +5159,7 @@ mod tests { app.config .features .set_enabled(Feature::GuardianApproval, true)?; - app.chat_widget - .set_feature_enabled(Feature::GuardianApproval, true); + app.chat_widget.enable_feature(Feature::GuardianApproval); let current_session_policy = app.config.permissions.approval_policy.value(); app.update_feature_flags(vec![(Feature::GuardianApproval, false)]) diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index a217ee3904..d59298dfdb 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -7649,6 +7649,14 @@ impl ChatWidget { true } + pub(crate) fn enable_feature(&mut self, feature: Feature) -> bool { + self.set_feature_enabled(feature, true) + } + + pub(crate) fn disable_feature(&mut self, feature: Feature) -> bool { + self.set_feature_enabled(feature, false) + } + fn initial_collaboration_mask( _config: &Config, models_manager: &ModelsManager, diff --git a/tools/argument-comment-lint/README.md b/tools/argument-comment-lint/README.md index de8d8f11d7..5634292809 100644 --- a/tools/argument-comment-lint/README.md +++ b/tools/argument-comment-lint/README.md @@ -16,31 +16,50 @@ self-descriptive at the callsite. ## Behavior -Given: +When you own the API, prefer a clearer shape over positional literal arguments: ```rust -fn create_openai_url(base_url: Option, retry_count: usize) -> String { +enum BaseUrl { + Default, + Custom(String), +} + +struct RetryCount(usize); + +fn create_openai_url(base_url: BaseUrl, retry_count: RetryCount) -> String { let _ = (base_url, retry_count); String::new() } ``` -This is accepted: +```rust +create_openai_url(BaseUrl::Default, RetryCount(3)); +``` + +When a minimal refactor needs to keep a legacy signature, `/*param=*/` comments +make those call sites readable: ```rust -create_openai_url(/*base_url=*/ None, /*retry_count=*/ 3); +fn legacy_create_openai_url(base_url: Option, retry_count: usize) -> String { + let _ = (base_url, retry_count); + String::new() +} +``` + +```rust +legacy_create_openai_url(/*base_url=*/ None, /*retry_count=*/ 3); ``` This is warned on by `argument_comment_mismatch`: ```rust -create_openai_url(/*api_base=*/ None, 3); +legacy_create_openai_url(/*api_base=*/ None, 3); ``` This is only warned on when `uncommented_anonymous_literal_argument` is enabled: ```rust -create_openai_url(None, 3); +legacy_create_openai_url(None, 3); ``` ## Development diff --git a/tools/argument-comment-lint/src/lib.rs b/tools/argument-comment-lint/src/lib.rs index 5ba7de5077..662d586b40 100644 --- a/tools/argument-comment-lint/src/lib.rs +++ b/tools/argument-comment-lint/src/lib.rs @@ -58,24 +58,28 @@ rustc_session::declare_lint! { /// This lint only runs when the callee resolves to a concrete function or /// method with available parameter names. /// + /// If you own the API, prefer a clearer signature such as enums, named + /// methods, or newtypes. This lint is primarily for legacy signatures + /// where a minimal refactor is the better tradeoff. + /// /// ### Example /// /// ```rust - /// fn create_openai_url(base_url: Option) -> String { + /// fn legacy_create_openai_url(base_url: Option) -> String { /// String::new() /// } /// - /// create_openai_url(/*api_base=*/ None); + /// legacy_create_openai_url(/*api_base=*/ None); /// ``` /// /// Use instead: /// /// ```rust - /// fn create_openai_url(base_url: Option) -> String { + /// fn legacy_create_openai_url(base_url: Option) -> String { /// String::new() /// } /// - /// create_openai_url(/*base_url=*/ None); + /// legacy_create_openai_url(/*base_url=*/ None); /// ``` pub ARGUMENT_COMMENT_MISMATCH, Warn, @@ -96,25 +100,27 @@ rustc_session::declare_lint! { /// ### Known problems /// /// This lint is opinionated, so it is `allow` by default. + /// Prefer reshaping APIs you control instead of relying on comments for + /// new code; this lint exists for legacy call sites and minimal refactors. /// /// ### Example /// /// ```rust - /// fn create_openai_url(base_url: Option) -> String { + /// fn legacy_create_openai_url(base_url: Option) -> String { /// String::new() /// } /// - /// create_openai_url(None); + /// legacy_create_openai_url(None); /// ``` /// /// Use instead: /// /// ```rust - /// fn create_openai_url(base_url: Option) -> String { + /// fn legacy_create_openai_url(base_url: Option) -> String { /// String::new() /// } /// - /// create_openai_url(/*base_url=*/ None); + /// legacy_create_openai_url(/*base_url=*/ None); /// ``` pub UNCOMMENTED_ANONYMOUS_LITERAL_ARGUMENT, Allow, diff --git a/tools/argument-comment-lint/ui/comment_matches.rs b/tools/argument-comment-lint/ui/comment_matches.rs index 67382e9b23..7c0380235e 100644 --- a/tools/argument-comment-lint/ui/comment_matches.rs +++ b/tools/argument-comment-lint/ui/comment_matches.rs @@ -1,12 +1,12 @@ #![warn(argument_comment_mismatch)] -fn create_openai_url(base_url: Option, retry_count: usize) -> String { +fn legacy_create_openai_url(base_url: Option, retry_count: usize) -> String { let _ = (base_url, retry_count); String::new() } fn main() { let base_url = Some(String::from("https://api.openai.com")); - create_openai_url(base_url, 3); - create_openai_url(/*base_url=*/ None, 3); + legacy_create_openai_url(base_url, 3); + legacy_create_openai_url(/*base_url=*/ None, 3); } diff --git a/tools/argument-comment-lint/ui/comment_matches_multiline.rs b/tools/argument-comment-lint/ui/comment_matches_multiline.rs index 151ebf47eb..50dabe9374 100644 --- a/tools/argument-comment-lint/ui/comment_matches_multiline.rs +++ b/tools/argument-comment-lint/ui/comment_matches_multiline.rs @@ -1,13 +1,13 @@ #![warn(argument_comment_mismatch)] #![warn(uncommented_anonymous_literal_argument)] -fn run_git_for_stdout(repo_root: &str, args: Vec<&str>, env: Option<&str>) -> String { +fn legacy_run_git_for_stdout(repo_root: &str, args: Vec<&str>, env: Option<&str>) -> String { let _ = (repo_root, args, env); String::new() } fn main() { - let _ = run_git_for_stdout( + let _ = legacy_run_git_for_stdout( "/tmp/repo", vec!["rev-parse", "HEAD"], /*env=*/ None, diff --git a/tools/argument-comment-lint/ui/comment_mismatch.rs b/tools/argument-comment-lint/ui/comment_mismatch.rs index 2104a2c8e8..88bba4e75d 100644 --- a/tools/argument-comment-lint/ui/comment_mismatch.rs +++ b/tools/argument-comment-lint/ui/comment_mismatch.rs @@ -1,10 +1,10 @@ #![warn(argument_comment_mismatch)] -fn create_openai_url(base_url: Option) -> String { +fn legacy_create_openai_url(base_url: Option) -> String { let _ = base_url; String::new() } fn main() { - let _ = create_openai_url(/*api_base=*/ None); + let _ = legacy_create_openai_url(/*api_base=*/ None); } diff --git a/tools/argument-comment-lint/ui/comment_mismatch.stderr b/tools/argument-comment-lint/ui/comment_mismatch.stderr index 2eb64f3254..d4f1959c67 100644 --- a/tools/argument-comment-lint/ui/comment_mismatch.stderr +++ b/tools/argument-comment-lint/ui/comment_mismatch.stderr @@ -1,8 +1,8 @@ warning: argument comment `/*api_base=*/` does not match parameter `base_url` - --> $DIR/comment_mismatch.rs:9:45 + --> $DIR/comment_mismatch.rs:9:52 | -LL | let _ = create_openai_url(/*api_base=*/ None); - | ^^^^ +LL | let _ = legacy_create_openai_url(/*api_base=*/ None); + | ^^^^ | = help: use `/*base_url=*/` note: the lint level is defined here diff --git a/tools/argument-comment-lint/ui/uncommented_literal.rs b/tools/argument-comment-lint/ui/uncommented_literal.rs index a64804c780..b62e39c09a 100644 --- a/tools/argument-comment-lint/ui/uncommented_literal.rs +++ b/tools/argument-comment-lint/ui/uncommented_literal.rs @@ -3,16 +3,16 @@ struct Client; impl Client { - fn set_flag(&self, enabled: bool) {} + fn set_legacy_flag(&self, enabled: bool) {} } -fn create_openai_url(base_url: Option, retry_count: usize) -> String { +fn legacy_create_openai_url(base_url: Option, retry_count: usize) -> String { let _ = (base_url, retry_count); String::new() } fn main() { let client = Client; - let _ = create_openai_url(None, 3); - client.set_flag(true); + let _ = legacy_create_openai_url(None, 3); + client.set_legacy_flag(true); } diff --git a/tools/argument-comment-lint/ui/uncommented_literal.stderr b/tools/argument-comment-lint/ui/uncommented_literal.stderr index a4b04d5b5d..8168945b46 100644 --- a/tools/argument-comment-lint/ui/uncommented_literal.stderr +++ b/tools/argument-comment-lint/ui/uncommented_literal.stderr @@ -1,8 +1,8 @@ warning: anonymous literal-like argument for parameter `base_url` - --> $DIR/uncommented_literal.rs:16:31 + --> $DIR/uncommented_literal.rs:16:38 | -LL | let _ = create_openai_url(None, 3); - | ^^^^ help: prepend the parameter name comment: `/*base_url=*/ None` +LL | let _ = legacy_create_openai_url(None, 3); + | ^^^^ help: prepend the parameter name comment: `/*base_url=*/ None` | note: the lint level is defined here --> $DIR/uncommented_literal.rs:1:9 @@ -11,16 +11,16 @@ LL | #![warn(uncommented_anonymous_literal_argument)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ warning: anonymous literal-like argument for parameter `retry_count` - --> $DIR/uncommented_literal.rs:16:37 + --> $DIR/uncommented_literal.rs:16:44 | -LL | let _ = create_openai_url(None, 3); - | ^ help: prepend the parameter name comment: `/*retry_count=*/ 3` +LL | let _ = legacy_create_openai_url(None, 3); + | ^ help: prepend the parameter name comment: `/*retry_count=*/ 3` warning: anonymous literal-like argument for parameter `enabled` - --> $DIR/uncommented_literal.rs:17:21 + --> $DIR/uncommented_literal.rs:17:28 | -LL | client.set_flag(true); - | ^^^^ help: prepend the parameter name comment: `/*enabled=*/ true` +LL | client.set_legacy_flag(true); + | ^^^^ help: prepend the parameter name comment: `/*enabled=*/ true` warning: 3 warnings emitted