diff --git a/codex-rs/app-server-daemon/src/backend/pid_start.rs b/codex-rs/app-server-daemon/src/backend/pid_start.rs index 11fdd3495b..1c286a8b14 100644 --- a/codex-rs/app-server-daemon/src/backend/pid_start.rs +++ b/codex-rs/app-server-daemon/src/backend/pid_start.rs @@ -235,7 +235,11 @@ impl PidBackend { } } - let child = match command.spawn() { + #[cfg(windows)] + let child = super::super::windows::spawn_without_inheriting_stdio(&mut command); + #[cfg(not(windows))] + let child = command.spawn().map_err(anyhow::Error::from); + let child = match child { Ok(child) => child, Err(err) => { if replacement.is_none() { @@ -258,10 +262,6 @@ impl PidBackend { .id() .context("spawned app-server process has no pid")?; let record = match async { - #[cfg(windows)] - super::super::windows::Process::open(pid)? - .context("daemon exited during launch")? - .ensure_detached()?; let process_start_time = read_process_start_time(pid).await?; #[cfg(any(target_os = "linux", target_os = "macos"))] let process_identity = super::identity::read_process_details(pid) diff --git a/codex-rs/app-server-daemon/src/backend/windows.rs b/codex-rs/app-server-daemon/src/backend/windows.rs index 157bf3343a..3b8b91ae47 100644 --- a/codex-rs/app-server-daemon/src/backend/windows.rs +++ b/codex-rs/app-server-daemon/src/backend/windows.rs @@ -2,6 +2,7 @@ //! so PID reuse can never redirect forced termination to a different process. //! Managed servers must not elevate ordinary clients sharing the account's socket. //! Installer jobs contain extraction processes when an update is cancelled. +//! Detached launches stop the launcher's original stdio handles from propagating. use std::io; use std::os::windows::io::AsRawHandle; @@ -14,9 +15,13 @@ use std::process::Stdio; use anyhow::Context; use anyhow::Result; +use windows_sys::Win32::Foundation::ERROR_INVALID_HANDLE; use windows_sys::Win32::Foundation::ERROR_INVALID_PARAMETER; use windows_sys::Win32::Foundation::ERROR_LOCK_VIOLATION; use windows_sys::Win32::Foundation::FILETIME; +use windows_sys::Win32::Foundation::HANDLE_FLAG_INHERIT; +use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE; +use windows_sys::Win32::Foundation::SetHandleInformation; use windows_sys::Win32::Foundation::WAIT_OBJECT_0; use windows_sys::Win32::Foundation::WAIT_TIMEOUT; use windows_sys::Win32::Security::GetTokenInformation; @@ -28,7 +33,6 @@ use windows_sys::Win32::Storage::FileSystem::LOCKFILE_FAIL_IMMEDIATELY; use windows_sys::Win32::Storage::FileSystem::LockFileEx; use windows_sys::Win32::System::JobObjects::AssignProcessToJobObject; use windows_sys::Win32::System::JobObjects::CreateJobObjectW; -use windows_sys::Win32::System::JobObjects::IsProcessInJob; use windows_sys::Win32::System::JobObjects::JOB_OBJECT_LIMIT_BREAKAWAY_OK; use windows_sys::Win32::System::JobObjects::JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; use windows_sys::Win32::System::JobObjects::JOBOBJECT_EXTENDED_LIMIT_INFORMATION; @@ -49,6 +53,38 @@ use windows_sys::Win32::System::Threading::PROCESS_TERMINATE; use windows_sys::Win32::System::Threading::TerminateProcess; use windows_sys::Win32::System::Threading::WaitForSingleObject; +pub(super) fn spawn_without_inheriting_stdio( + command: &mut tokio::process::Command, +) -> Result { + // The daemon must not inherit the launcher's output pipes: callers wait for + // them to close after the launcher exits. Leave the flags cleared so concurrent + // launches cannot inherit them either; Rust duplicates the child's chosen stdio. + for (name, handle) in [ + ("stdin", io::stdin().as_raw_handle()), + ("stdout", io::stdout().as_raw_handle()), + ("stderr", io::stderr().as_raw_handle()), + ] { + if handle.is_null() || handle as isize == INVALID_HANDLE_VALUE { + continue; + } + // SAFETY: these are borrowed standard handles; changing the inherit + // flag neither closes them nor changes their read/write access. + if unsafe { + SetHandleInformation(handle as _, HANDLE_FLAG_INHERIT, /*dwflags*/ 0) + } == 0 + { + let error = io::Error::last_os_error(); + // An already-closed handle cannot be inherited. + if error.raw_os_error() == Some(ERROR_INVALID_HANDLE as i32) { + continue; + } + return Err(error) + .with_context(|| format!("failed to clear launcher {name} inheritance")); + } + } + Ok(command.spawn()?) +} + pub(crate) fn ensure_not_elevated() -> Result<()> { let mut token = 0; if unsafe { OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) } == 0 { @@ -77,8 +113,9 @@ pub(crate) fn ensure_not_elevated() -> Result<()> { Ok(()) } -// Probe the actual child association: escaping an inner job can leave an outer -// job attached. Suspend the image so no application code runs before cleanup. +// Check that breakaway launch is permitted before stopping an existing daemon. +// An outer system job may remain attached; membership alone does not establish +// whether it will terminate the daemon. Suspend the probe before cleanup. pub(crate) fn ensure_detached_launch(executable: &Path) -> Result<()> { let mut child = Command::new(executable) .creation_flags(CREATE_SUSPENDED | DETACHED_PROCESS | CREATE_BREAKAWAY_FROM_JOB) @@ -87,30 +124,13 @@ pub(crate) fn ensure_detached_launch(executable: &Path) -> Result<()> { .stderr(Stdio::null()) .spawn() .context("cannot launch detached daemon; existing daemon was not stopped")?; - let mut in_job = 0; - let result = if unsafe { - IsProcessInJob( - child.as_raw_handle() as _, - /*jobhandle*/ 0, - &mut in_job, - ) - } == 0 - { - Err(io::Error::last_os_error()).context("failed to verify daemon launch capability") - } else if in_job != 0 { - Err(anyhow::anyhow!( - "host Job Object prevents daemon detachment; start from a host that allows breakaway" - )) - } else { - Ok(()) - }; child .kill() .context("failed to terminate suspended launch probe")?; child .wait() .context("failed to reap suspended launch probe")?; - result + Ok(()) } pub(super) struct Process(OwnedHandle); @@ -170,29 +190,6 @@ impl Process { } } - pub(super) fn ensure_detached(&self) -> Result<()> { - let mut in_job = 0; - if unsafe { - IsProcessInJob( - self.0.as_raw_handle() as _, - /*jobhandle*/ 0, - &mut in_job, - ) - } == 0 - { - let error = io::Error::last_os_error(); - self.terminate()?; - return Err(error).context("failed to verify daemon detachment"); - } - if in_job != 0 { - self.terminate()?; - anyhow::bail!( - "host Job Object prevents daemon detachment; start from a host that allows breakaway" - ); - } - Ok(()) - } - pub(super) fn terminate(&self) -> Result<()> { if !self.is_running()? { return Ok(()); diff --git a/codex-rs/app-server-daemon/src/backend/windows_tests.rs b/codex-rs/app-server-daemon/src/backend/windows_tests.rs index 7ce59b332b..ed9044bd18 100644 --- a/codex-rs/app-server-daemon/src/backend/windows_tests.rs +++ b/codex-rs/app-server-daemon/src/backend/windows_tests.rs @@ -1,10 +1,136 @@ use super::Process; use pretty_assertions::assert_eq; use std::os::windows::io::AsRawHandle; +use std::os::windows::io::FromRawHandle; +use std::os::windows::io::OwnedHandle; +use std::os::windows::process::CommandExt; +use std::process::Stdio; +use std::time::Duration; +use tokio::io::AsyncReadExt; use windows_sys::Win32::Foundation::ERROR_ACCESS_DENIED; +use windows_sys::Win32::Foundation::HANDLE_FLAG_INHERIT; +use windows_sys::Win32::Foundation::SetHandleInformation; +use windows_sys::Win32::System::JobObjects::IsProcessInJob; use windows_sys::Win32::System::Threading::CREATE_NO_WINDOW; +use windows_sys::Win32::System::Threading::DETACHED_PROCESS; use windows_sys::Win32::System::Threading::TerminateProcess; +#[tokio::test] +async fn captured_stdio_closes_while_child_is_alive() { + const TEST: &str = "backend::windows::tests::captured_stdio_closes_while_child_is_alive"; + const ROLE: &str = "CODEX_TEST_STDIO_ROLE"; + const HOME: &str = "CODEX_TEST_STDIO_HOME"; + let executable = std::env::current_exe().expect("test executable"); + + if let Ok(role) = std::env::var(ROLE) { + let home = std::path::PathBuf::from(std::env::var_os(HOME).expect("test home")); + if role == "child" { + eprintln!("child stderr"); + std::fs::write(home.join("ready"), []).unwrap(); + tokio::time::sleep(Duration::from_secs(30)).await; + return; + } + + let handles = [ + std::io::stdout().as_raw_handle() as _, + std::io::stderr().as_raw_handle() as _, + ]; + for handle in handles { + assert_ne!( + unsafe { SetHandleInformation(handle, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT) }, + 0 + ); + } + let mut command = tokio::process::Command::new(executable); + command + .args(["--exact", TEST, "--nocapture"]) + .env(ROLE, "child") + // The CI job may forbid breakaway; it does not affect stdio inheritance. + .creation_flags(DETACHED_PROCESS) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(std::fs::File::create(home.join("child.log")).unwrap()); + let child = super::spawn_without_inheriting_stdio(&mut command).unwrap(); + let pid = child.id().unwrap(); + let started = Process::open(pid).unwrap().unwrap().start_time().unwrap(); + std::fs::write( + home.join("pid"), + serde_json::to_vec(&(pid, started)).unwrap(), + ) + .unwrap(); + println!("launcher stdout"); + eprintln!("launcher stderr"); + return; + } + + let home = tempfile::tempdir().unwrap(); + let mut launcher = tokio::process::Command::new(executable) + .args(["--exact", TEST, "--nocapture"]) + .env(ROLE, "launcher") + .env(HOME, home.path()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .kill_on_drop(true) + .spawn() + .unwrap(); + let status = tokio::time::timeout(Duration::from_secs(10), launcher.wait()) + .await + .expect("launcher exit") + .unwrap(); + let (pid, started): (u32, String) = + serde_json::from_slice(&std::fs::read(home.path().join("pid")).unwrap()).unwrap(); + let child = Process::open(pid).unwrap().expect("live child"); + assert_eq!( + child.start_time().unwrap(), + started, + "fixture PID was reused" + ); + let mut stdout = String::new(); + let mut stderr = String::new(); + let captured = tokio::time::timeout(Duration::from_secs(5), async { + let ready = home.path().join("ready"); + while !ready.exists() { + tokio::time::sleep(Duration::from_millis(25)).await; + } + tokio::try_join!( + launcher + .stdout + .as_mut() + .unwrap() + .read_to_string(&mut stdout), + launcher + .stderr + .as_mut() + .unwrap() + .read_to_string(&mut stderr), + ) + }) + .await; + let still_running = child.is_running(); + child.terminate().expect("cleanup child"); + tokio::time::timeout(Duration::from_secs(5), async { + while child.is_running().unwrap() { + tokio::time::sleep(Duration::from_millis(25)).await; + } + }) + .await + .expect("child exit after cleanup"); + + captured.expect("launcher output must close").unwrap(); + assert!(status.success(), "{stderr}"); + assert!( + still_running.unwrap(), + "child exited before its launcher output closed" + ); + assert!(stdout.contains("launcher stdout")); + assert!(stderr.contains("launcher stderr")); + assert!( + std::fs::read_to_string(home.path().join("child.log")) + .unwrap() + .contains("child stderr") + ); +} + #[test] fn detached_launch_preflight_rejects_restrictive_job() { const CHILD: &str = "CODEX_TEST_RESTRICTIVE_LAUNCH_JOB"; @@ -45,6 +171,154 @@ fn detached_launch_preflight_rejects_restrictive_job() { assert!(String::from_utf8_lossy(&output.stdout).contains("1 passed")); } +#[test] +fn detached_launch_preflight_allows_residual_job() { + const CHILD: &str = "CODEX_TEST_RESIDUAL_LAUNCH_JOB"; + let executable = std::env::current_exe().expect("test executable"); + if std::env::var_os(CHILD).is_some() { + let mut jobs = Vec::new(); + for inner in [false, true] { + let job = unsafe { super::CreateJobObjectW(std::ptr::null(), std::ptr::null()) }; + assert_ne!(job, 0); + let job = unsafe { OwnedHandle::from_raw_handle(job as _) }; + if inner { + let mut limits: super::JOBOBJECT_EXTENDED_LIMIT_INFORMATION = + unsafe { std::mem::zeroed() }; + limits.BasicLimitInformation.LimitFlags = super::JOB_OBJECT_LIMIT_BREAKAWAY_OK; + assert_ne!( + unsafe { + super::SetInformationJobObject( + job.as_raw_handle() as _, + super::JobObjectExtendedLimitInformation, + (&limits as *const super::JOBOBJECT_EXTENDED_LIMIT_INFORMATION).cast(), + std::mem::size_of_val(&limits) as u32, + ) + }, + 0 + ); + } + assert_ne!( + unsafe { + super::AssignProcessToJobObject( + job.as_raw_handle() as _, + super::GetCurrentProcess(), + ) + }, + 0 + ); + jobs.push(job); + } + + let mut probe = std::process::Command::new(&executable) + .creation_flags( + super::CREATE_SUSPENDED + | super::DETACHED_PROCESS + | super::CREATE_BREAKAWAY_FROM_JOB, + ) + .spawn() + .expect("breakaway probe"); + let mut in_outer = 0; + let mut in_inner = 0; + let outer_checked = unsafe { + IsProcessInJob( + probe.as_raw_handle() as _, + jobs[0].as_raw_handle() as _, + &mut in_outer, + ) + }; + let inner_checked = unsafe { + IsProcessInJob( + probe.as_raw_handle() as _, + jobs[1].as_raw_handle() as _, + &mut in_inner, + ) + }; + probe.kill().expect("terminate probe"); + probe.wait().expect("reap probe"); + assert_ne!(outer_checked, 0); + assert_ne!(inner_checked, 0); + assert_ne!(in_outer, 0, "probe must retain its outer job"); + assert_eq!(in_inner, 0, "probe must leave its inner job"); + super::ensure_detached_launch(&executable).expect("residual job must be accepted"); + + if let Err(err) = super::ensure_not_elevated() { + assert!(err.to_string().contains("non-elevated terminal")); + return; + } + let temp = tempfile::tempdir().expect("temp dir"); + let script = temp.path().join("codex.cmd"); + std::fs::write( + &script, + "@echo off\r\nif \"%3\"==\"--help\" exit /b 1\r\necho ready > \"%~dp0ready\"\r\nfor /l %%i in (1,1,10000000) do @rem\r\n", + ) + .expect("write daemon fixture"); + let backend = crate::backend::pid::PidBackend::new( + script, + temp.path().join("state").join("daemon.pid"), + /*remote_control_enabled*/ false, + ); + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .expect("runtime"); + let pid = runtime + .block_on(backend.start()) + .expect("daemon launch") + .expect("new daemon pid"); + let process = Process::open(pid) + .expect("open daemon") + .expect("live daemon"); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + while !temp.path().join("ready").exists() && std::time::Instant::now() < deadline { + std::thread::sleep(std::time::Duration::from_millis(50)); + } + let ready = temp.path().join("ready").exists(); + let outer_checked = unsafe { + IsProcessInJob( + process.0.as_raw_handle() as _, + jobs[0].as_raw_handle() as _, + &mut in_outer, + ) + }; + let inner_checked = unsafe { + IsProcessInJob( + process.0.as_raw_handle() as _, + jobs[1].as_raw_handle() as _, + &mut in_inner, + ) + }; + process.terminate().expect("terminate daemon fixture"); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + while process.is_running().expect("daemon liveness") && std::time::Instant::now() < deadline + { + std::thread::sleep(std::time::Duration::from_millis(50)); + } + assert!(!process.is_running().expect("daemon liveness")); + assert!(ready, "daemon fixture did not start"); + assert_ne!(outer_checked, 0); + assert_ne!(inner_checked, 0); + assert_ne!(in_outer, 0, "daemon must retain its outer job"); + assert_eq!(in_inner, 0, "daemon must leave its inner job"); + return; + } + let output = std::process::Command::new(executable) + .args([ + "--exact", + "backend::windows::tests::detached_launch_preflight_allows_residual_job", + "--nocapture", + ]) + .env(CHILD, "1") + .output() + .expect("isolated job test"); + assert!( + output.status.success(), + "{}\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + assert!(String::from_utf8_lossy(&output.stdout).contains("1 passed")); +} + #[tokio::test] async fn identity_queries_do_not_require_termination_access() { let mut child = tokio::process::Command::new("powershell.exe") diff --git a/codex-rs/app-server-protocol/schema/precomputed/app-server-exports-stable.json.zst b/codex-rs/app-server-protocol/schema/precomputed/app-server-exports-stable.json.zst index e9b07c3ca4..38be2c6312 100644 Binary files a/codex-rs/app-server-protocol/schema/precomputed/app-server-exports-stable.json.zst and b/codex-rs/app-server-protocol/schema/precomputed/app-server-exports-stable.json.zst differ diff --git a/codex-rs/code-mode/src/remote_session/connection.rs b/codex-rs/code-mode/src/remote_session/connection.rs index eecf9c64ee..a900bb2332 100644 --- a/codex-rs/code-mode/src/remote_session/connection.rs +++ b/codex-rs/code-mode/src/remote_session/connection.rs @@ -157,6 +157,8 @@ impl Connection { let mut command = Command::new(host_program); #[cfg(unix)] command.process_group(0); + #[cfg(windows)] + command.creation_flags(/*flags*/ 0x0800_0000); // CREATE_NO_WINDOW command .stdin(Stdio::piped()) .stdout(Stdio::piped()) diff --git a/codex-rs/core/src/agent/control_tests.rs b/codex-rs/core/src/agent/control_tests.rs index ee643af259..144f0b30ac 100644 --- a/codex-rs/core/src/agent/control_tests.rs +++ b/codex-rs/core/src/agent/control_tests.rs @@ -59,6 +59,7 @@ use codex_protocol::items::TurnItem; use codex_protocol::items::UserMessageItem; use codex_protocol::mcp::ClientMcpExtensions; use codex_protocol::mcp::McpAttribution; +use codex_protocol::mcp::McpAttributionErrorReason; use codex_protocol::mcp::McpAttributionSource; use codex_protocol::mcp::McpAttributionStatus; use codex_protocol::mcp::OPENAI_FORM_EXTENSION_ID; @@ -3159,8 +3160,12 @@ async fn spawn_agent_full_fork_legacy_compaction_rebuilds_child_instructions_onc } } +#[test_case::test_case(false; "complete")] +#[test_case::test_case(true; "error_reason")] #[tokio::test] -async fn resume_and_cold_fork_restore_mcp_attribution_in_constructed_requests() { +async fn resume_and_cold_fork_restore_mcp_attribution_in_constructed_requests( + record_invalid_source: bool, +) { let harness = AgentControlHarness::new().await; let (source_thread_id, source_thread) = harness.start_thread().await; let turn_context = source_thread.session.new_default_turn().await; @@ -3184,6 +3189,16 @@ async fn resume_and_cold_fork_restore_mcp_attribution_in_constructed_requests() .services .executed_tool_calls .record_mcp_source(source.clone()); + if record_invalid_source { + source_thread + .session + .services + .executed_tool_calls + .record_mcp_source(McpAttributionSource { + tool_name: String::new(), + ..source.clone() + }); + } source_thread .session .record_conversation_items( @@ -3232,7 +3247,12 @@ async fn resume_and_cold_fork_restore_mcp_attribution_in_constructed_requests() .await .expect("resume source thread"); let expected = McpAttribution { - status: McpAttributionStatus::Complete, + status: if record_invalid_source { + McpAttributionStatus::AttributionError + } else { + McpAttributionStatus::Complete + }, + error_reason: record_invalid_source.then_some(McpAttributionErrorReason::SourceInvalid), sources: vec![source], }; assert_eq!( @@ -3475,6 +3495,7 @@ async fn spawn_agent_fork_last_n_turns_keeps_only_recent_turns() { mcp_attribution_in_constructed_request(&child_thread).await, McpAttribution { status: McpAttributionStatus::Complete, + error_reason: None, sources: vec![source], } ); diff --git a/codex-rs/core/src/client_tests.rs b/codex-rs/core/src/client_tests.rs index 0f62891632..69bdaf81fa 100644 --- a/codex-rs/core/src/client_tests.rs +++ b/codex-rs/core/src/client_tests.rs @@ -499,6 +499,7 @@ fn responses_request_limits_internal_metadata_to_resolved_first_party_https_endp let without_raw_metadata = output_with_tool_result_metadata(ToolResultMetadata::default()); let attribution = McpAttribution { status: McpAttributionStatus::Complete, + error_reason: None, sources: vec![McpAttributionSource { connector_id: Some("connector_example".to_string()), plugin_id: Some("example@openai-bundled".to_string()), @@ -585,6 +586,36 @@ fn responses_request_limits_internal_metadata_to_resolved_first_party_https_endp assert_eq!(prompt.input, vec![output.clone()]); } } + responses_metadata.mcp_attribution = Some(McpAttribution { + status: McpAttributionStatus::AttributionError, + error_reason: Some( + codex_protocol::mcp::McpAttributionErrorReason::HistoryMissingCheckpoint, + ), + sources: attribution.sources.clone(), + }); + let request = client.build_responses_request( + &prompt, + &test_model_info(), + /*effort*/ None, + codex_protocol::config_types::ReasoningSummary::None, + /*service_tier*/ None, + &responses_metadata, + /*include_internal*/ true, + )?; + assert_eq!( + request + .client_metadata + .as_ref() + .and_then(|metadata| metadata.get(MCP_ATTRIBUTION_CLIENT_METADATA_KEY)) + .map(|value| serde_json::from_str::(value)) + .transpose()?, + Some(json!({ + "status": "attribution_error", + "sources": attribution.sources, + "error_reason": "history_missing_checkpoint", + })), + ); + let mut oversized_attribution = attribution; oversized_attribution.sources[0].tool_name = "a".repeat(MAX_MCP_ATTRIBUTION_BYTES); responses_metadata.mcp_attribution = Some(oversized_attribution); @@ -603,7 +634,7 @@ fn responses_request_limits_internal_metadata_to_resolved_first_party_https_endp .get(MCP_ATTRIBUTION_CLIENT_METADATA_KEY) .map(String::as_str) }), - Some(r#"{"status":"attribution_error"}"#), + Some(r#"{"status":"attribution_error","error_reason":"payload_too_large"}"#), ); Ok(()) } diff --git a/codex-rs/core/src/responses_metadata.rs b/codex-rs/core/src/responses_metadata.rs index dcc6f5e407..bfdeb3b40f 100644 --- a/codex-rs/core/src/responses_metadata.rs +++ b/codex-rs/core/src/responses_metadata.rs @@ -1,5 +1,6 @@ use std::collections::BTreeMap; use std::collections::HashMap; +use std::io::ErrorKind; use codex_analytics::CompactionImplementation; use codex_analytics::CompactionPhase; @@ -359,7 +360,15 @@ impl CodexResponsesMetadata { } if include_internal && let Some(attribution) = &self.mcp_attribution { let serialized = to_json_string_bounded(attribution, MAX_MCP_ATTRIBUTION_BYTES) - .unwrap_or_else(|_| r#"{"status":"attribution_error"}"#.to_string()); + .unwrap_or_else(|error| { + if error.io_error_kind() == Some(ErrorKind::WriteZero) { + r#"{"status":"attribution_error","error_reason":"payload_too_large"}"# + .to_string() + } else { + r#"{"status":"attribution_error","error_reason":"serialization_failed"}"# + .to_string() + } + }); client_metadata.insert(MCP_ATTRIBUTION_CLIENT_METADATA_KEY.to_string(), serialized); } client_metadata diff --git a/codex-rs/core/src/session/tests.rs b/codex-rs/core/src/session/tests.rs index 1a14903b62..9a61d17de3 100644 --- a/codex-rs/core/src/session/tests.rs +++ b/codex-rs/core/src/session/tests.rs @@ -5632,6 +5632,7 @@ async fn mcp_attribution_checkpoints_cover_batch_prefixes_compaction_and_restore .record_mcp_source(source.clone()); let expected = McpAttribution { status: McpAttributionStatus::Complete, + error_reason: None, sources: vec![source], }; session @@ -6288,6 +6289,7 @@ async fn response_metadata_builders_capture_fresh_mcp_attribution() { .await; let expected = Some(McpAttribution { status: McpAttributionStatus::Complete, + error_reason: None, sources: vec![source], }); assert_eq!(before.mcp_attribution, Some(McpAttribution::default())); diff --git a/codex-rs/core/src/tools/executed_tool_calls/mcp_attribution.rs b/codex-rs/core/src/tools/executed_tool_calls/mcp_attribution.rs index fce3f8f36d..86b03077c7 100644 --- a/codex-rs/core/src/tools/executed_tool_calls/mcp_attribution.rs +++ b/codex-rs/core/src/tools/executed_tool_calls/mcp_attribution.rs @@ -7,6 +7,7 @@ use std::sync::MutexGuard; use codex_history::InitialHistory; use codex_history::RolloutItem; use codex_protocol::mcp::McpAttribution; +use codex_protocol::mcp::McpAttributionErrorReason; use codex_protocol::mcp::McpAttributionSource; use codex_protocol::mcp::McpAttributionStatus; @@ -56,25 +57,24 @@ impl McpAttributionRecorder { for item in history.get_rollout_items() { match item { RolloutItem::ResponseItem(envelope) => { - if let Some(checkpoint) = envelope - .metadata - .as_ref() - .and_then(|metadata| metadata.mcp_attribution.as_ref()) + if let Some(metadata) = envelope.metadata.as_ref() + && let Some(checkpoint) = metadata.mcp_attribution.as_ref() { found_checkpoint = true; state.merge_checkpoint(checkpoint); } } RolloutItem::Compacted(compacted) => { - for checkpoint in compacted + for metadata in compacted .replacement_history .iter() .flatten() .filter_map(|envelope| envelope.metadata.as_ref()) - .filter_map(|metadata| metadata.mcp_attribution.as_ref()) { - found_checkpoint = true; - state.merge_checkpoint(checkpoint); + if let Some(checkpoint) = metadata.mcp_attribution.as_ref() { + found_checkpoint = true; + state.merge_checkpoint(checkpoint); + } } } _ => {} @@ -82,7 +82,7 @@ impl McpAttributionRecorder { } if !found_checkpoint { // Pre-attribution history cannot establish that earlier context was MCP-free. - state.mark_error(); + state.mark_error(McpAttributionErrorReason::HistoryMissingCheckpoint); } Self(Arc::new(Mutex::new(state))) } @@ -90,7 +90,7 @@ impl McpAttributionRecorder { fn lock(&self) -> MutexGuard<'_, State> { self.0.lock().unwrap_or_else(|poisoned| { let mut state = poisoned.into_inner(); - state.mark_error(); + state.mark_error(McpAttributionErrorReason::RecorderPoisoned); state }) } @@ -116,20 +116,27 @@ impl McpAttributionRecorder { } impl State { - fn mark_error(&mut self) { + fn mark_error(&mut self, reason: McpAttributionErrorReason) { if self.attribution.status != McpAttributionStatus::AttributionError { self.attribution.status = McpAttributionStatus::AttributionError; + self.attribution.error_reason = Some(reason); self.revision += 1; } } fn merge_checkpoint(&mut self, checkpoint: &McpAttribution) { - if checkpoint.status == McpAttributionStatus::AttributionError - || (checkpoint.status == McpAttributionStatus::None && !checkpoint.sources.is_empty()) + if checkpoint.status == McpAttributionStatus::AttributionError { + self.mark_error( + checkpoint + .error_reason + .unwrap_or(McpAttributionErrorReason::RestoredErrorUnknown), + ); + } else if (checkpoint.status == McpAttributionStatus::None + && !checkpoint.sources.is_empty()) || (checkpoint.status == McpAttributionStatus::Complete && checkpoint.sources.is_empty()) { - self.mark_error(); + self.mark_error(McpAttributionErrorReason::CheckpointInvalid); } for source in &checkpoint.sources { self.record(source.clone(), /*restoring*/ true); @@ -145,7 +152,7 @@ impl State { .find(|existing| SourceIdentity::from(*existing) == identity) { if restoring && existing.first_turn_id != source.first_turn_id { - self.mark_error(); + self.mark_error(McpAttributionErrorReason::CheckpointSourceConflict); } return; } @@ -153,7 +160,11 @@ impl State { || source.tool_name.is_empty() || source.first_turn_id.is_empty() { - self.mark_error(); + self.mark_error(if restoring { + McpAttributionErrorReason::CheckpointInvalid + } else { + McpAttributionErrorReason::SourceInvalid + }); return; } if self.attribution.status == McpAttributionStatus::None { diff --git a/codex-rs/core/src/tools/executed_tool_calls/mcp_attribution_tests.rs b/codex-rs/core/src/tools/executed_tool_calls/mcp_attribution_tests.rs index a7c9dbb391..8d90927ddc 100644 --- a/codex-rs/core/src/tools/executed_tool_calls/mcp_attribution_tests.rs +++ b/codex-rs/core/src/tools/executed_tool_calls/mcp_attribution_tests.rs @@ -44,6 +44,7 @@ fn records_the_first_turn_for_each_unique_source() { recorder.snapshot(), McpAttribution { status: McpAttributionStatus::Complete, + error_reason: None, sources: vec![source("search", "turn_1"), source("fetch", "turn_2")], } ); @@ -53,10 +54,12 @@ fn records_the_first_turn_for_each_unique_source() { fn restores_cumulative_item_and_compaction_checkpoints() { let initial = McpAttribution { status: McpAttributionStatus::Complete, + error_reason: None, sources: vec![source("search", "turn_1")], }; let cumulative = McpAttribution { status: McpAttributionStatus::Complete, + error_reason: None, sources: vec![source("search", "turn_1"), source("fetch", "turn_2")], }; let history = InitialHistory::Forked(vec![ @@ -89,6 +92,7 @@ fn legacy_or_conflicting_history_is_not_complete() { McpAttributionRecorder::new(&legacy).snapshot(), McpAttribution { status: McpAttributionStatus::AttributionError, + error_reason: Some(McpAttributionErrorReason::HistoryMissingCheckpoint), sources: Vec::new(), } ); @@ -98,6 +102,7 @@ fn legacy_or_conflicting_history_is_not_complete() { .map(|turn_id| { RolloutItem::ResponseItem(envelope(Some(McpAttribution { status: McpAttributionStatus::Complete, + error_reason: None, sources: vec![source("search", turn_id)], }))) }) @@ -107,11 +112,116 @@ fn legacy_or_conflicting_history_is_not_complete() { McpAttributionRecorder::new(&history).snapshot(), McpAttribution { status: McpAttributionStatus::AttributionError, + error_reason: Some(McpAttributionErrorReason::CheckpointSourceConflict), sources: vec![source("search", "turn_1")], } ); } +#[test] +fn first_error_reason_survives_checkpoints_and_later_sources() { + let recorder = McpAttributionRecorder::default(); + recorder.record(source("", "turn_1")); + recorder.record(source("search", "turn_2")); + let (snapshot, _) = recorder + .checkpoint(/*force*/ true) + .expect("error checkpoint"); + let checkpoint = envelope(Some(snapshot)); + let mut compacted: CompactedItem = + serde_json::from_value(serde_json::json!({"message": "summary"})) + .expect("compaction checkpoint"); + compacted.replacement_history = Some(vec![checkpoint.clone()]); + for item in [ + RolloutItem::ResponseItem(checkpoint), + RolloutItem::Compacted(compacted), + ] { + let restored = McpAttributionRecorder::new(&InitialHistory::Forked(vec![item])); + restored.record(source("", "turn_3")); + + assert_eq!( + restored.snapshot(), + McpAttribution { + status: McpAttributionStatus::AttributionError, + error_reason: Some(McpAttributionErrorReason::SourceInvalid), + sources: vec![source("search", "turn_2")], + }, + ); + } +} + +#[test] +fn poisoned_recorder_reports_a_bounded_reason() { + let recorder = McpAttributionRecorder::default(); + let poisoned = recorder.clone(); + assert!( + std::thread::spawn(move || { + let _state = poisoned.0.lock().expect("unpoisoned recorder"); + panic!("poison recorder"); + }) + .join() + .is_err() + ); + assert_eq!( + recorder.snapshot(), + McpAttribution { + status: McpAttributionStatus::AttributionError, + error_reason: Some(McpAttributionErrorReason::RecorderPoisoned), + sources: Vec::new(), + } + ); +} + +#[test] +fn restored_diagnostics_do_not_change_attribution_state() { + let checkpoint = envelope(Some(McpAttribution { + status: McpAttributionStatus::Complete, + error_reason: Some(McpAttributionErrorReason::SourceInvalid), + sources: vec![source("search", "turn_1")], + })); + assert_eq!( + McpAttributionRecorder::new(&InitialHistory::Forked(vec![RolloutItem::ResponseItem( + checkpoint + ),])) + .snapshot(), + McpAttribution { + status: McpAttributionStatus::Complete, + error_reason: None, + sources: vec![source("search", "turn_1")], + } + ); + + for (attribution, expected_reason) in [ + ( + McpAttribution { + status: McpAttributionStatus::AttributionError, + error_reason: None, + sources: Vec::new(), + }, + McpAttributionErrorReason::RestoredErrorUnknown, + ), + ( + McpAttribution { + status: McpAttributionStatus::Complete, + error_reason: None, + sources: Vec::new(), + }, + McpAttributionErrorReason::CheckpointInvalid, + ), + ] { + assert_eq!( + McpAttributionRecorder::new(&InitialHistory::Forked(vec![RolloutItem::ResponseItem( + envelope(Some(attribution)) + ),])) + .snapshot(), + McpAttribution { + status: McpAttributionStatus::AttributionError, + error_reason: Some(expected_reason), + sources: Vec::new(), + } + ); + } +} + #[test] fn acknowledging_an_older_checkpoint_does_not_clear_newer_changes() { let recorder = McpAttributionRecorder::default(); diff --git a/codex-rs/core/tests/suite/mcp_optional_startup_grace.rs b/codex-rs/core/tests/suite/mcp_optional_startup_grace.rs index f5ef343ae4..d366b75f41 100644 --- a/codex-rs/core/tests/suite/mcp_optional_startup_grace.rs +++ b/codex-rs/core/tests/suite/mcp_optional_startup_grace.rs @@ -14,6 +14,9 @@ use test_case::test_case; const SERVER_NAME: &str = "optional_startup"; const TOOL_NAMESPACE: &str = "mcp__optional_startup"; const TOOL_NAME: &str = "calendar_create_event"; +// Allow the whole turn to finish on slow runners without reaching the server's own timeout. +const TURN_TIMEOUT: Duration = Duration::from_secs(5); +const PENDING_SERVER_TIMEOUT: Duration = Duration::from_secs(30); #[derive(Clone, Copy)] enum StartupGraceScenario { @@ -46,9 +49,9 @@ async fn optional_mcp_startup_grace_controls_initial_turn_tool_catalog( | StartupGraceScenario::DisabledGraceRespectsStartupTimeout => Duration::ZERO, }; let startup_timeout = match scenario { + StartupGraceScenario::ShortGraceOmitsPending => PENDING_SERVER_TIMEOUT, StartupGraceScenario::DisabledGraceRespectsStartupTimeout => Duration::from_millis(250), - StartupGraceScenario::ShortGraceOmitsPending - | StartupGraceScenario::CustomGraceIncludesReady + StartupGraceScenario::CustomGraceIncludesReady | StartupGraceScenario::DisabledGraceWaitsForStartup => Duration::from_secs(1), }; let responses_server = responses::start_mock_server().await; @@ -101,7 +104,7 @@ async fn optional_mcp_startup_grace_controls_initial_turn_tool_catalog( let mut turn = Box::pin(fixture.submit_turn("show optional MCP tools")); match scenario { StartupGraceScenario::ShortGraceOmitsPending => { - tokio::time::timeout(Duration::from_millis(500), &mut turn) + tokio::time::timeout(TURN_TIMEOUT, &mut turn) .await .context("the configured grace should omit the pending server")??; release_startup @@ -206,7 +209,7 @@ async fn running_thread_uses_refreshed_optional_mcp_startup_grace( "url": server_url, "http_headers": { "Authorization": "Bearer synthetic-test-token" }, "enabled_tools": [TOOL_NAME], - "startup_timeout_sec": 5, + "startup_timeout_sec": PENDING_SERVER_TIMEOUT.as_secs(), })) .expect("synthetic optional MCP server configuration"), ); @@ -226,8 +229,10 @@ async fn running_thread_uses_refreshed_optional_mcp_startup_grace( .await .context("optional MCP initialization should begin before the initial turn")?; + // Allow for turn setup on remote workers while still finishing before the + // pending MCP startup timeout. The server remains gated throughout. tokio::time::timeout( - Duration::from_millis(500), + TURN_TIMEOUT, fixture.submit_turn("show initial optional MCP tools"), ) .await diff --git a/codex-rs/history/src/lib.rs b/codex-rs/history/src/lib.rs index a0cd404cb6..9da17e4cc7 100644 --- a/codex-rs/history/src/lib.rs +++ b/codex-rs/history/src/lib.rs @@ -113,6 +113,7 @@ where Ok(Some(serde_json::from_value(value).unwrap_or_else(|_| { McpAttribution { status: McpAttributionStatus::AttributionError, + error_reason: None, sources: Vec::new(), } }))) diff --git a/codex-rs/history/src/tests.rs b/codex-rs/history/src/tests.rs index c414bacb77..96733b2041 100644 --- a/codex-rs/history/src/tests.rs +++ b/codex-rs/history/src/tests.rs @@ -181,12 +181,54 @@ fn mcp_checkpoint_handles_missing_or_unknown_identity() -> Result<()> { restored.mcp_attribution, Some(McpAttribution { status: McpAttributionStatus::AttributionError, + error_reason: None, sources: Vec::new(), }) ); Ok(()) } +#[test] +fn mcp_error_diagnostics_do_not_invalidate_checkpoints() -> Result<()> { + let metadata = CodexHarnessMetadata { + mcp_attribution: Some(McpAttribution { + status: McpAttributionStatus::AttributionError, + error_reason: Some( + codex_protocol::mcp::McpAttributionErrorReason::HistoryMissingCheckpoint, + ), + sources: Vec::new(), + }), + ..Default::default() + }; + for (reason, expected_reason) in [ + (None, None), + ( + Some(json!("future_reason")), + Some(codex_protocol::mcp::McpAttributionErrorReason::Unknown), + ), + (Some(json!({"invalid": true})), None), + ] { + let mut serialized = serde_json::to_value(&metadata)?; + let attribution = serialized["mcp_attribution"] + .as_object_mut() + .expect("attribution object"); + if let Some(reason) = reason { + attribution.insert("error_reason".to_string(), reason); + } else { + attribution.remove("error_reason"); + } + let restored: CodexHarnessMetadata = serde_json::from_value(serialized)?; + let mut expected = metadata.clone(); + expected + .mcp_attribution + .as_mut() + .expect("attribution checkpoint") + .error_reason = expected_reason; + assert_eq!(restored, expected); + } + Ok(()) +} + #[test] /// Keeps legacy response-item rollout lines readable and byte-shape compatible. fn response_item_rollout_line_preserves_shape() -> Result<()> { diff --git a/codex-rs/protocol/src/mcp.rs b/codex-rs/protocol/src/mcp.rs index c898c2af4e..2b2dddc703 100644 --- a/codex-rs/protocol/src/mcp.rs +++ b/codex-rs/protocol/src/mcp.rs @@ -5,6 +5,7 @@ //! `schemars`) so they can be embedded in Codex's own protocol structures. use schemars::JsonSchema; use serde::Deserialize; +use serde::Deserializer; use serde::Serialize; use std::collections::HashMap; use ts_rs::TS; @@ -62,6 +63,27 @@ pub enum McpAttributionStatus { AttributionError, } +/// Bounded diagnostics for an attribution error, never a policy decision. +/// +/// Recorder reasons identify the first observed error and survive checkpoints. +/// Payload reasons describe a request-serialization fallback and are not checkpointed. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] +#[serde(rename_all = "snake_case")] +#[non_exhaustive] +pub enum McpAttributionErrorReason { + HistoryMissingCheckpoint, + CheckpointInvalid, + CheckpointSourceConflict, + SourceInvalid, + RecorderPoisoned, + /// An earlier error checkpoint did not retain its cause. + RestoredErrorUnknown, + PayloadTooLarge, + SerializationFailed, + #[serde(other)] + Unknown, +} + /// One source and the first runtime turn in which Codex recorded it. /// /// Server and tool names are retained when no stable identifier is available; @@ -84,10 +106,27 @@ pub struct McpAttributionSource { #[serde(deny_unknown_fields)] pub struct McpAttribution { pub status: McpAttributionStatus, + /// First observed attribution error, absent for non-error states. + #[serde( + default, + skip_serializing_if = "Option::is_none", + deserialize_with = "deserialize_mcp_attribution_error_reason" + )] + pub error_reason: Option, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub sources: Vec, } +fn deserialize_mcp_attribution_error_reason<'de, D>( + deserializer: D, +) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + let value = serde_json::Value::deserialize(deserializer)?; + Ok(serde_json::from_value(value).ok()) +} + /// Bounded app-resource provenance retained across a compaction checkpoint. #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] pub struct McpResourceOriginCheckpoint { diff --git a/codex-rs/rmcp-client/src/bin/test_stdio_server.rs b/codex-rs/rmcp-client/src/bin/test_stdio_server.rs index 79a2e1c066..8bf0845d1a 100644 --- a/codex-rs/rmcp-client/src/bin/test_stdio_server.rs +++ b/codex-rs/rmcp-client/src/bin/test_stdio_server.rs @@ -1003,6 +1003,15 @@ async fn main() -> Result<(), Box> { std::fs::write(pid_file, std::process::id().to_string())?; } #[cfg(windows)] + if let Ok(console_state_file) = std::env::var("MCP_TEST_CONSOLE_STATE_FILE") { + #[link(name = "kernel32")] + unsafe extern "system" { + fn GetConsoleWindow() -> *mut std::ffi::c_void; + } + let has_console = unsafe { !GetConsoleWindow().is_null() }; + std::fs::write(console_state_file, has_console.to_string())?; + } + #[cfg(windows)] if let Ok(marker_file) = std::env::var("MCP_TEST_BREAKAWAY_DENIED_FILE") { use std::os::windows::process::CommandExt; diff --git a/codex-rs/rmcp-client/src/stdio_server_launcher.rs b/codex-rs/rmcp-client/src/stdio_server_launcher.rs index 65ba1f0c5a..05071b5800 100644 --- a/codex-rs/rmcp-client/src/stdio_server_launcher.rs +++ b/codex-rs/rmcp-client/src/stdio_server_launcher.rs @@ -214,6 +214,11 @@ impl StdioServerLauncher for LocalStdioServerLauncher { // Local private implementation. +#[cfg(windows)] +const CREATE_NO_WINDOW: u32 = 0x0800_0000; +#[cfg(windows)] +const CREATE_SUSPENDED: u32 = 0x0000_0004; + #[cfg(unix)] const PROCESS_GROUP_TERM_GRACE_PERIOD: Duration = Duration::from_secs(2); @@ -288,6 +293,8 @@ impl LocalStdioServerLauncher { // handles and needs a handle allowlist in the shared spawn backend. #[cfg(unix)] command.descriptor_policy(DescriptorPolicy::StdioOnly); + #[cfg(windows)] + command.creation_flags(CREATE_NO_WINDOW); command }; #[cfg(windows)] @@ -298,6 +305,8 @@ impl LocalStdioServerLauncher { let job = match codex_utils_pty::JobObject::create_without_breakaway() { Ok(job) => { command.prepare_suspended_spawn(&job); + // The helper replaces creation flags; retain suspension and suppress the console. + command.creation_flags(CREATE_SUSPENDED | CREATE_NO_WINDOW); Some(job) } Err(error) => { diff --git a/codex-rs/rmcp-client/tests/stdio_message_limits.rs b/codex-rs/rmcp-client/tests/stdio_message_limits.rs index 7c8ec6102b..cbfeceae4b 100644 --- a/codex-rs/rmcp-client/tests/stdio_message_limits.rs +++ b/codex-rs/rmcp-client/tests/stdio_message_limits.rs @@ -134,6 +134,90 @@ async fn stdio_message_limits_preserve_legacy_local_compatibility() -> anyhow::R Ok(()) } +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn local_stdio_initializes_without_a_console_directly_and_through_cmd() -> anyhow::Result<()> +{ + let server = codex_utils_cargo_bin::cargo_bin("test_stdio_server")?; + let temp_dir = tempfile::tempdir()?; + let console_state_file = temp_dir.path().join("console-state"); + let server_pid_file = temp_dir.path().join("server.pid"); + std::fs::write( + temp_dir.path().join("launch.cmd"), + "@echo off\r\n\"%MCP_TEST_SERVER_BINARY%\"\r\n", + )?; + let env = HashMap::from([ + ( + OsString::from("MCP_TEST_CONSOLE_STATE_FILE"), + console_state_file.clone().into_os_string(), + ), + ( + OsString::from("MCP_TEST_PID_FILE"), + server_pid_file.clone().into_os_string(), + ), + ( + OsString::from("MCP_TEST_SERVER_BINARY"), + server.clone().into(), + ), + ]); + + for (program, args) in [ + (server.into(), Vec::new()), + ( + OsString::from("cmd.exe"), + ["/d", "/s", "/c", "call", ".\\launch.cmd"] + .map(OsString::from) + .to_vec(), + ), + ] { + let client = RmcpClient::new_stdio_client( + program, + args, + Some(env.clone()), + &[], + Some(temp_dir.path().to_string_lossy().into_owned()), + Arc::new(LocalStdioServerLauncher::new(temp_dir.path().to_path_buf())), + ) + .await?; + client + .initialize( + InitializeRequestParams::new( + ClientCapabilities::default(), + Implementation::new("stdio-console-test", "1.0.0"), + ) + .with_protocol_version(ProtocolVersion::V_2025_06_18), + Some(Duration::from_secs(10)), + Box::new(|_, _| { + async { + Ok(ElicitationResponse { + action: ElicitationAction::Decline, + content: None, + meta: None, + }) + } + .boxed() + }), + ) + .await?; + let console_state = std::fs::read_to_string(&console_state_file)?; + let server_pid = std::fs::read_to_string(&server_pid_file)? + .trim() + .parse::()?; + let server_process = open_process_for_wait(server_pid)?; + let tools = client + .list_tools(/*params*/ None, Some(Duration::from_secs(10))) + .await; + client.shutdown().await; + wait_for_process_exit(&server_process)?; + + pretty_assertions::assert_eq!(console_state, "false"); + assert!(!tools?.tools.is_empty()); + std::fs::remove_file(&console_state_file)?; + std::fs::remove_file(&server_pid_file)?; + } + Ok(()) +} + #[cfg(windows)] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn local_stdio_shutdown_terminates_descendants_after_server_exit() -> anyhow::Result<()> { diff --git a/codex-rs/utils/pty/src/child_command.rs b/codex-rs/utils/pty/src/child_command.rs index 89c890ddfa..06f274ce0d 100644 --- a/codex-rs/utils/pty/src/child_command.rs +++ b/codex-rs/utils/pty/src/child_command.rs @@ -130,6 +130,12 @@ impl Command { self } + /// Set the Windows process creation flags, replacing any previously selected flags. + #[cfg(windows)] + pub fn creation_flags(&mut self, flags: u32) -> &mut Self { + self.inner.creation_flags(flags); + self + } /// Preserve Job Object assignment before the child begins executing on Windows. #[cfg(windows)] pub fn prepare_suspended_spawn(&mut self, job: &crate::JobObject) {