diff --git a/codex-rs/mxc-sandbox/src/lib.rs b/codex-rs/mxc-sandbox/src/lib.rs index ae7a8e8901..4e03ce9fc0 100644 --- a/codex-rs/mxc-sandbox/src/lib.rs +++ b/codex-rs/mxc-sandbox/src/lib.rs @@ -166,6 +166,7 @@ pub fn run_main() -> ! { match windows::run() { Ok(exit_code) => std::process::exit(exit_code), Err(error) => { + windows::log_launch(format_args!("launch failed: {error:#}")); eprintln!("MXC launcher: {error:#}"); std::process::exit(1); } diff --git a/codex-rs/mxc-sandbox/src/native.rs b/codex-rs/mxc-sandbox/src/native.rs index fb51ff2c60..ed682bb69e 100644 --- a/codex-rs/mxc-sandbox/src/native.rs +++ b/codex-rs/mxc-sandbox/src/native.rs @@ -1,5 +1,8 @@ //! Run an already prepared MXC request with inherited stdio and job ownership. +use std::time::Instant; + +use crate::windows::log_launch; use anyhow::Context; use anyhow::Result; use mxc_sdk::v1; @@ -12,8 +15,17 @@ unsafe extern "system" {} pub(super) fn launch(request: v1::ContainerRequest) -> Result { let mut options = v1::SpawnExtendedOptions::default(); options.stdio = v1::SpawnStdio::Inherit; + let spawning = Instant::now(); let mut child = v1::spawn_with_extended_options(request, options)?; - match child.wait().context("waiting for the MXC command")? { + let spawn_ms = spawning.elapsed().as_millis(); + log_launch(format_args!("phase=wait spawn_ms={spawn_ms}")); + let waiting = Instant::now(); + let outcome = child.wait().context("waiting for the MXC command")?; + let wait_ms = waiting.elapsed().as_millis(); + log_launch(format_args!( + "phase=finished outcome={outcome:?} wait_ms={wait_ms}" + )); + match outcome { v1::WaitResult::Exited(code) => Ok(code), v1::WaitResult::TimedOut => anyhow::bail!("MXC command timed out"), } diff --git a/codex-rs/mxc-sandbox/src/policy.rs b/codex-rs/mxc-sandbox/src/policy.rs index 0a102b5335..82ca625d20 100644 --- a/codex-rs/mxc-sandbox/src/policy.rs +++ b/codex-rs/mxc-sandbox/src/policy.rs @@ -136,6 +136,8 @@ pub(super) fn build_request( } let policy = materialize_temporary_paths(permissions.file_system_sandbox_policy(), &env)?; #[cfg(windows)] + crate::windows::log_launch("phase=prepare_deny_parents"); + #[cfg(windows)] for denied in policy.get_unreadable_roots_with_cwd(cwd) { let Some(parent) = denied.as_path().parent() else { continue; @@ -185,6 +187,8 @@ pub(super) fn build_request( } } let full_disk_write = policy.has_full_disk_write_access(); + #[cfg(windows)] + crate::windows::log_launch("phase=resolve_filesystem_policy"); let volumes = volume_roots .iter() .map(PathUri::from_host_native_path) @@ -199,6 +203,8 @@ pub(super) fn build_request( let (roots, readable_roots, denied_paths) = if fs == policy { let resolved = fs.resolve_for_enforcement(cwd, FileSystemPolicyProjection::PreserveMutableRoots)?; + #[cfg(windows)] + crate::windows::log_launch("phase=resolve_deny_paths"); let denies = expand_windows_deny_read_paths(&resolved, &absolute_cwd); (resolved.writable_roots, resolved.readable_roots, denies) } else { @@ -209,9 +215,12 @@ pub(super) fn build_request( } else { fs.get_writable_roots_with_cwd_preserving_mutable_paths(cwd) }; + let readable_roots = fs.get_readable_roots_with_cwd(cwd); + #[cfg(windows)] + crate::windows::log_launch("phase=resolve_deny_paths"); ( roots, - fs.get_readable_roots_with_cwd(cwd), + readable_roots, resolve_windows_deny_read_paths(&policy, &absolute_cwd), ) }; @@ -266,6 +275,8 @@ pub(super) fn build_request( .iter() .map(PathUri::from_host_native_path) .collect::>>()?; + #[cfg(windows)] + crate::windows::log_launch("phase=expand_volume_grants"); prune_unavailable_volume_roots(&mut write, &volume_roots); prune_unavailable_volume_roots(&mut read, &volume_roots); // BaseContainer owns alias precedence for the emitted paths. Do not call @@ -326,6 +337,13 @@ pub(super) fn build_request( Ok((key.to_owned(), value.to_owned())) }) .collect::, PolicyError>>()?; + #[cfg(windows)] + crate::windows::log_launch(format_args!( + "filesystem grants read={} write={} deny={}", + read.len(), + write.len(), + deny.len() + )); Ok(ContainerRequest { working_directory: Some( command_cwd diff --git a/codex-rs/mxc-sandbox/src/windows.rs b/codex-rs/mxc-sandbox/src/windows.rs index 0e5b3de59f..4150ef97a2 100644 --- a/codex-rs/mxc-sandbox/src/windows.rs +++ b/codex-rs/mxc-sandbox/src/windows.rs @@ -4,6 +4,7 @@ use std::collections::HashMap; use std::os::windows::ffi::OsStrExt; use std::path::Path; use std::path::PathBuf; +use std::time::Instant; use anyhow::Result; use anyhow::ensure; @@ -30,10 +31,17 @@ impl Drop for ComUninitializeGuard { } } +pub(super) fn log_launch(event: impl std::fmt::Display) { + let pid = std::process::id(); + codex_windows_sandbox::log_runtime_note(&format!("MXC pid={pid} {event}")); +} + pub(super) fn run() -> Result { if let Some(codex_home) = std::env::args_os().nth(2) { codex_windows_sandbox::init_runtime_log(Path::new(&codex_home)); } + log_launch("phase=prepare_request"); + let preparing = Instant::now(); ensure!( crate::is_available(), "native MXC is unavailable on this Windows build" @@ -54,10 +62,20 @@ pub(super) fn run() -> Result { // Serde errors can quote arbitrary payload values; do not print them to stderr. let command = crate::transport::decode(&mut env) .map_err(|_| anyhow::anyhow!("invalid MXC launcher request"))?; - // The transport itself proves this is an explicit child environment; it - // may intentionally contain no variables once launcher state is removed. + let kind = match command.command.as_slice() { + [_, arg] if arg == "--codex-run-as-fs-helper" => "fs_helper", + _ => "command", + }; + log_launch(format_args!("phase=prepare_volumes kind={kind}")); let command_cwd = std::env::current_dir()?; let volumes = volume_roots(&command.sandbox_policy_cwd, &command_cwd)?; + let volume_count = volumes.len(); + log_launch(format_args!( + "phase=prepare_known_folders volumes={volume_count}" + )); + let platform_roots = platform_read_roots()?; + // The transport itself proves this is an explicit child environment; it + // may intentionally contain no variables once launcher state is removed. let request = crate::policy::build_request( &command, &command_cwd, @@ -65,8 +83,10 @@ pub(super) fn run() -> Result { .map(|(key, value)| format!("{key}={value}")) .collect(), &volumes, - &platform_read_roots()?, + &platform_roots, )?; + let prep_ms = preparing.elapsed().as_millis(); + log_launch(format_args!("phase=spawn prep_ms={prep_ms}")); crate::native::launch(request) } diff --git a/codex-rs/windows-sandbox-rs/src/logging.rs b/codex-rs/windows-sandbox-rs/src/logging.rs index 076d06ad9f..cb3d2c2a2e 100644 --- a/codex-rs/windows-sandbox-rs/src/logging.rs +++ b/codex-rs/windows-sandbox-rs/src/logging.rs @@ -20,12 +20,16 @@ pub const MAX_LOG_FILES: usize = 90; // Service provisioning never appends to a caller-controlled existing log entry. // An initialized but empty slot also prevents falling back after an open failure. static SETUP_LOG: OnceLock>> = OnceLock::new(); -static RUNTIME_LOG_DIR: OnceLock = OnceLock::new(); +static RUNTIME_LOG: OnceLock<(PathBuf, Mutex>)> = OnceLock::new(); /// Set the default sandbox log directory for this launcher process. pub fn init_runtime_log(codex_home: &Path) { if codex_home.is_absolute() { - let _ = RUNTIME_LOG_DIR.set(crate::sandbox_dir(codex_home)); + RUNTIME_LOG.get_or_init(|| { + let dir = crate::sandbox_dir(codex_home); + let _ = std::fs::create_dir_all(&dir); + (dir, Mutex::new(None)) + }); } } @@ -117,7 +121,18 @@ fn append_line(line: &str, base_dir: Option<&Path>) { } return; } - if let Some(dir) = base_dir + if let Some((dir, cache)) = RUNTIME_LOG.get() + && base_dir == Some(dir.as_path()) + { + if let Ok(mut cached) = cache.lock() { + if cached.is_none() { + *cached = log_writer(dir); + } + if let Some(writer) = cached.as_mut() { + let _ = writer.write_all(format!("{line}\n").as_bytes()); + } + } + } else if let Some(dir) = base_dir && let Some(mut f) = log_writer(dir) { let _ = writeln!(f, "{line}"); @@ -155,7 +170,7 @@ pub fn log_note(msg: &str, base_dir: Option<&Path>) { /// Log a note in a launcher process configured with a sandbox log directory. pub fn log_runtime_note(msg: &str) { - if let Some(dir) = RUNTIME_LOG_DIR.get() { + if let Some((dir, _)) = RUNTIME_LOG.get() { log_note(msg, Some(dir)); } }