diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index 3600dbee4a..9ae386c5a5 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -1093,12 +1093,18 @@ async fn dispatch_cli( Some(Subcommand::Agents(options)) => Some(options), _ => None, }; - if let Some(options) = agents_options + let session_remote = match &subcommand { + Some(Subcommand::Agents(options)) => Some(("agents", &options.remote)), + Some(Subcommand::Resume(options)) => Some(("resume", &options.remote)), + Some(Subcommand::Fork(options)) => Some(("fork", &options.remote)), + _ => None, + }; + if let Some((command, options)) = session_remote && let Some(root_endpoint) = &remote.remote - && let Some(agents_endpoint) = &options.remote.remote - && root_endpoint != agents_endpoint + && let Some(command_endpoint) = &options.remote + && root_endpoint != command_endpoint { - anyhow::bail!("`codex agents` received conflicting remote server endpoints"); + anyhow::bail!("`codex {command}` received conflicting remote server endpoints"); } let root_remote = agents_options .and_then(|options| options.remote.remote.clone()) diff --git a/codex-rs/cli/tests/queue.rs b/codex-rs/cli/tests/queue.rs index 15b5128ae9..9db3e7377c 100644 --- a/codex-rs/cli/tests/queue.rs +++ b/codex-rs/cli/tests/queue.rs @@ -652,6 +652,36 @@ fn remote_exec_rejects_conflicting_endpoints() -> Result<()> { Ok(()) } +#[test] +fn tui_session_commands_reject_conflicting_remotes_before_token_lookup() -> Result<()> { + const MISSING_TOKEN: &str = "CODEX_TEST_MISSING_TUI_REMOTE_TOKEN"; + for command in ["resume", "fork"] { + let output = std::process::Command::new(codex_utils_cargo_bin::cargo_bin("codex")?) + .env_remove(MISSING_TOKEN) + .args([ + "--remote", + "wss://a.example:443", + "--remote-auth-token-env", + MISSING_TOKEN, + command, + "--remote", + "wss://b.example:443", + THREAD_ID, + ]) + .output()?; + let stderr = String::from_utf8(output.stderr)?; + assert!(!output.status.success()); + assert!( + stderr.contains(&format!( + "`codex {command}` received conflicting remote server endpoints" + )), + "{command}: {stderr}" + ); + assert!(!stderr.contains(MISSING_TOKEN), "{command}: {stderr}"); + } + Ok(()) +} + #[test] fn remote_session_commands_validate_config() -> Result<()> { let cases: &[(&[&str], &str, &str)] = &[