@@ -394,11 +394,25 @@ impl Args for SessionTuiCli {
394394
395395impl clap:: FromArgMatches for SessionTuiCli {
396396 fn from_arg_matches ( matches : & clap:: ArgMatches ) -> Result < Self , clap:: Error > {
397- TuiCli :: from_arg_matches ( matches) . map ( Self )
397+ let cli = TuiCli :: from_arg_matches ( matches) ?;
398+ if cli. thread_name . is_some ( ) {
399+ return Err ( clap:: Error :: raw (
400+ clap:: error:: ErrorKind :: ArgumentConflict ,
401+ "`--name` is only supported when starting a fresh interactive session" ,
402+ ) ) ;
403+ }
404+ Ok ( Self ( cli) )
398405 }
399406
400407 fn update_from_arg_matches ( & mut self , matches : & clap:: ArgMatches ) -> Result < ( ) , clap:: Error > {
401- self . 0 . update_from_arg_matches ( matches)
408+ self . 0 . update_from_arg_matches ( matches) ?;
409+ if self . 0 . thread_name . is_some ( ) {
410+ return Err ( clap:: Error :: raw (
411+ clap:: error:: ErrorKind :: ArgumentConflict ,
412+ "`--name` is only supported when starting a fresh interactive session" ,
413+ ) ) ;
414+ }
415+ Ok ( ( ) )
402416 }
403417}
404418
@@ -942,6 +956,7 @@ async fn cli_main(arg0_paths: Arg0DispatchPaths) -> anyhow::Result<()> {
942956 let root_remote_auth_token_env = remote. remote_auth_token_env ;
943957 let root_strict_config = interactive. strict_config ;
944958 reject_root_strict_config_for_subcommand ( root_strict_config, & subcommand) ?;
959+ reject_root_thread_name_for_subcommand ( interactive. thread_name . as_deref ( ) , & subcommand) ?;
945960 if let Some ( subcommand) = subcommand. as_ref ( ) {
946961 profile_v2_for_subcommand ( & interactive, subcommand) ?;
947962 }
@@ -2003,6 +2018,56 @@ fn reject_root_strict_config_for_subcommand(
20032018 }
20042019}
20052020
2021+ fn reject_root_thread_name_for_subcommand (
2022+ thread_name : Option < & str > ,
2023+ subcommand : & Option < Subcommand > ,
2024+ ) -> anyhow:: Result < ( ) > {
2025+ if thread_name. is_none ( ) {
2026+ return Ok ( ( ) ) ;
2027+ }
2028+ let Some ( subcommand_name) = subcommand_name_for_thread_name_reject ( subcommand) else {
2029+ return Ok ( ( ) ) ;
2030+ } ;
2031+ anyhow:: bail!(
2032+ "`--name` is only supported when starting a fresh interactive session, not `codex {subcommand_name}`"
2033+ )
2034+ }
2035+
2036+ fn subcommand_name_for_thread_name_reject ( subcommand : & Option < Subcommand > ) -> Option < & ' static str > {
2037+ match subcommand {
2038+ None => None ,
2039+ Some ( Subcommand :: Exec ( _) ) => Some ( "exec" ) ,
2040+ Some ( Subcommand :: Review ( _) ) => Some ( "review" ) ,
2041+ Some ( Subcommand :: Login ( _) ) => Some ( "login" ) ,
2042+ Some ( Subcommand :: Logout ( _) ) => Some ( "logout" ) ,
2043+ Some ( Subcommand :: Mcp ( _) ) => Some ( "mcp" ) ,
2044+ Some ( Subcommand :: Plugin ( _) ) => Some ( "plugin" ) ,
2045+ Some ( Subcommand :: McpServer ( _) ) => Some ( "mcp-server" ) ,
2046+ Some ( Subcommand :: AppServer ( app_server) ) => {
2047+ Some ( app_server_subcommand_name ( app_server. subcommand . as_ref ( ) ) )
2048+ }
2049+ Some ( Subcommand :: RemoteControl ( remote_control) ) => Some ( remote_control. subcommand_name ( ) ) ,
2050+ #[ cfg( any( target_os = "macos" , target_os = "windows" ) ) ]
2051+ Some ( Subcommand :: App ( _) ) => Some ( "app" ) ,
2052+ Some ( Subcommand :: Completion ( _) ) => Some ( "completion" ) ,
2053+ Some ( Subcommand :: Update ) => Some ( "update" ) ,
2054+ Some ( Subcommand :: Doctor ( _) ) => Some ( "doctor" ) ,
2055+ Some ( Subcommand :: Sandbox ( _) ) => Some ( "sandbox" ) ,
2056+ Some ( Subcommand :: Debug ( _) ) => Some ( "debug" ) ,
2057+ Some ( Subcommand :: Execpolicy ( _) ) => Some ( "execpolicy" ) ,
2058+ Some ( Subcommand :: Apply ( _) ) => Some ( "apply" ) ,
2059+ Some ( Subcommand :: Resume ( _) ) => Some ( "resume" ) ,
2060+ Some ( Subcommand :: Archive ( _) ) => Some ( "archive" ) ,
2061+ Some ( Subcommand :: Unarchive ( _) ) => Some ( "unarchive" ) ,
2062+ Some ( Subcommand :: Fork ( _) ) => Some ( "fork" ) ,
2063+ Some ( Subcommand :: Cloud ( _) ) => Some ( "cloud" ) ,
2064+ Some ( Subcommand :: ResponsesApiProxy ( _) ) => Some ( "responses-api-proxy" ) ,
2065+ Some ( Subcommand :: StdioToUds ( _) ) => Some ( "stdio-to-uds" ) ,
2066+ Some ( Subcommand :: ExecServer ( _) ) => Some ( "exec-server" ) ,
2067+ Some ( Subcommand :: Features ( _) ) => Some ( "features" ) ,
2068+ }
2069+ }
2070+
20062071/// Return the selected subcommand name when a root-level `--strict-config`
20072072/// flag should be rejected after parsing.
20082073///
@@ -3046,6 +3111,46 @@ mod tests {
30463111 ) ;
30473112 }
30483113
3114+ #[ test]
3115+ fn root_name_flag_parses_for_fresh_interactive_session ( ) {
3116+ let cli = MultitoolCli :: try_parse_from ( [ "codex" , "--name" , "desc-pay refund panel" ] )
3117+ . expect ( "parse" ) ;
3118+
3119+ assert_eq ! (
3120+ cli. interactive. thread_name. as_deref( ) ,
3121+ Some ( "desc-pay refund panel" )
3122+ ) ;
3123+ assert ! ( cli. subcommand. is_none( ) ) ;
3124+ }
3125+
3126+ #[ test]
3127+ fn root_name_flag_is_rejected_for_subcommands ( ) {
3128+ let cli = MultitoolCli :: try_parse_from ( [ "codex" , "--name" , "work" , "resume" ] )
3129+ . expect ( "root flag parses before post-parse validation" ) ;
3130+
3131+ let err = reject_root_thread_name_for_subcommand (
3132+ cli. interactive . thread_name . as_deref ( ) ,
3133+ & cli. subcommand ,
3134+ )
3135+ . expect_err ( "root --name should not apply to subcommands" ) ;
3136+
3137+ assert_eq ! (
3138+ err. to_string( ) ,
3139+ "`--name` is only supported when starting a fresh interactive session, not `codex resume`"
3140+ ) ;
3141+ }
3142+
3143+ #[ test]
3144+ fn session_commands_reject_scoped_name_flag ( ) {
3145+ let err = MultitoolCli :: try_parse_from ( [ "codex" , "resume" , "--name" , "work" ] )
3146+ . expect_err ( "resume --name should be rejected" ) ;
3147+ assert_eq ! ( err. kind( ) , clap:: error:: ErrorKind :: ArgumentConflict ) ;
3148+
3149+ let err = MultitoolCli :: try_parse_from ( [ "codex" , "fork" , "--name" , "work" ] )
3150+ . expect_err ( "fork --name should be rejected" ) ;
3151+ assert_eq ! ( err. kind( ) , clap:: error:: ErrorKind :: ArgumentConflict ) ;
3152+ }
3153+
30493154 #[ test]
30503155 fn resume_model_flag_applies_when_no_root_flags ( ) {
30513156 let interactive =
0 commit comments