diff --git a/codex-rs/mxc-sandbox/src/lib.rs b/codex-rs/mxc-sandbox/src/lib.rs index d42edba8ba..ae7a8e8901 100644 --- a/codex-rs/mxc-sandbox/src/lib.rs +++ b/codex-rs/mxc-sandbox/src/lib.rs @@ -20,6 +20,9 @@ use anyhow::Result; use anyhow::ensure; use codex_network_proxy::ManagedNetworkSandboxContext; use codex_protocol::models::PermissionProfile; +use codex_utils_absolute_path::AbsolutePathBuf; +#[cfg(windows)] +use codex_utils_absolute_path::AbsolutePathBufGuard; use codex_windows_sandbox::environment_transport; use serde::Deserialize; use serde::Serialize; @@ -91,7 +94,21 @@ pub fn create_command_args(args: CreateMxcCommandArgsParams<'_>) -> Result std::fs::canonicalize(home) + .ok() + .filter(|home| home.is_dir()), + _ => AbsolutePathBufGuard::home_directory().map(|home| home.join(".codex")), + } + .and_then(|home| AbsolutePathBuf::from_absolute_path(home).ok()); + #[cfg(not(windows))] + let codex_home: Option = None; + Ok(std::iter::once(CODEX_WINDOWS_MXC_ARG1) + .chain(codex_home.as_deref().and_then(Path::to_str)) + .map(str::to_owned) + .collect()) } /// Whether the executor can create a native MXC process security environment. @@ -113,7 +130,7 @@ pub fn is_available() -> bool { pub fn prepare_file_system_policy( policy: codex_protocol::permissions::FileSystemSandboxPolicy, env: &HashMap, - policy_cwd: &codex_utils_absolute_path::AbsolutePathBuf, + policy_cwd: &AbsolutePathBuf, command_cwd: &Path, ) -> Result { use codex_utils_path_uri::PathUri; diff --git a/codex-rs/mxc-sandbox/src/policy_tests.rs b/codex-rs/mxc-sandbox/src/policy_tests.rs index 95ebeefc82..d7b13d6340 100644 --- a/codex-rs/mxc-sandbox/src/policy_tests.rs +++ b/codex-rs/mxc-sandbox/src/policy_tests.rs @@ -189,7 +189,8 @@ fn wrapper_preserves_exact_argv_and_separate_command_cwd() -> Result<()> { managed_network: None, env: &mut env, })?; - assert_eq!(wrapped, vec![crate::CODEX_WINDOWS_MXC_ARG1]); + assert_eq!(wrapped[0], crate::CODEX_WINDOWS_MXC_ARG1); + assert!(wrapped.len() == 1 || (cfg!(windows) && wrapped.len() == 2)); let parsed = crate::transport::decode(&mut env)?; assert!(parsed.default_child_env); assert!(env.is_empty()); diff --git a/codex-rs/mxc-sandbox/src/windows.rs b/codex-rs/mxc-sandbox/src/windows.rs index c475066d31..c0c9d8b10b 100644 --- a/codex-rs/mxc-sandbox/src/windows.rs +++ b/codex-rs/mxc-sandbox/src/windows.rs @@ -31,6 +31,9 @@ impl Drop for ComUninitializeGuard { } 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)); + } ensure!( crate::is_available(), "native MXC is unavailable on this Windows build" diff --git a/codex-rs/windows-sandbox-rs/src/deny_read_resolver.rs b/codex-rs/windows-sandbox-rs/src/deny_read_resolver.rs index 24e5c16884..0acef6a164 100644 --- a/codex-rs/windows-sandbox-rs/src/deny_read_resolver.rs +++ b/codex-rs/windows-sandbox-rs/src/deny_read_resolver.rs @@ -10,6 +10,8 @@ use ignore::overrides::OverrideBuilder; use std::collections::HashSet; use std::path::Path; use std::path::PathBuf; +#[cfg(windows)] +use std::time::Instant; #[derive(Debug, Eq, PartialEq)] struct GlobScanPlan { @@ -68,6 +70,8 @@ fn expand_deny_read_paths( return Ok(paths); } + #[cfg(windows)] + let started = Instant::now(); let glob_policy = FileSystemSandboxPolicy::restricted( unreadable_globs .iter() @@ -86,6 +90,8 @@ fn expand_deny_read_paths( }; let scan_plans = glob_scan_plans(unreadable_globs, glob_scan_max_depth)?; + #[cfg(windows)] + let roots = scan_plans.len(); for scan_plan in scan_plans { if !scan_plan.root.exists() { @@ -141,6 +147,14 @@ fn expand_deny_read_paths( } } + #[cfg(windows)] + { + let elapsed_ms = started.elapsed().as_millis(); + let patterns = unreadable_globs.len(); + crate::log_runtime_note(&format!( + "MXC deny glob expansion elapsed_ms={elapsed_ms} patterns={patterns} roots={roots} glob_scan_max_depth={glob_scan_max_depth:?}" + )); + } Ok(paths) } diff --git a/codex-rs/windows-sandbox-rs/src/lib.rs b/codex-rs/windows-sandbox-rs/src/lib.rs index 987f765342..35b1b797ff 100644 --- a/codex-rs/windows-sandbox-rs/src/lib.rs +++ b/codex-rs/windows-sandbox-rs/src/lib.rs @@ -331,17 +331,17 @@ pub use ipc_framed::read_frame; #[cfg(target_os = "windows")] pub use ipc_framed::write_frame; #[cfg(target_os = "windows")] -pub use logging::current_log_file_path; -#[cfg(target_os = "windows")] -pub use logging::current_log_file_path_for_codex_home; -#[cfg(target_os = "windows")] -pub use logging::log_file_path_for_utc_date; -#[cfg(target_os = "windows")] -pub use logging::log_note; -#[cfg(target_os = "windows")] -pub use logging::log_writer; -#[cfg(target_os = "windows")] -pub use logging::setup_log_writer; +#[rustfmt::skip] +pub use logging::{ + current_log_file_path, + current_log_file_path_for_codex_home, + init_runtime_log, + log_file_path_for_utc_date, + log_note, + log_runtime_note, + log_writer, + setup_log_writer, +}; #[cfg(target_os = "windows")] pub use no_reparse_dir::DirectoryOpenDisposition; #[cfg(target_os = "windows")] diff --git a/codex-rs/windows-sandbox-rs/src/logging.rs b/codex-rs/windows-sandbox-rs/src/logging.rs index 8bbd721b8a..076d06ad9f 100644 --- a/codex-rs/windows-sandbox-rs/src/logging.rs +++ b/codex-rs/windows-sandbox-rs/src/logging.rs @@ -20,6 +20,14 @@ 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(); + +/// 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)); + } +} fn exe_label() -> &'static str { static LABEL: OnceLock = OnceLock::new(); @@ -145,6 +153,13 @@ pub fn log_note(msg: &str, base_dir: Option<&Path>) { append_line(&format!("[{ts} {}] {}", exe_label(), msg), base_dir); } +/// 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() { + log_note(msg, Some(dir)); + } +} + #[cfg(test)] mod tests { use super::*;