From 3e0931e07973b782736bb90dd8f918b6c3d8cf47 Mon Sep 17 00:00:00 2001 From: David Wiesen Date: Thu, 30 Apr 2026 10:00:43 -0700 Subject: [PATCH] fix(windows-sandbox): disable private desktop in headless sessions --- codex-rs/core/src/windows_sandbox.rs | 17 ++++++++++++++++- codex-rs/core/src/windows_sandbox_tests.rs | 22 +++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/codex-rs/core/src/windows_sandbox.rs b/codex-rs/core/src/windows_sandbox.rs index a094545bab..bfcfe01bca 100644 --- a/codex-rs/core/src/windows_sandbox.rs +++ b/codex-rs/core/src/windows_sandbox.rs @@ -10,6 +10,7 @@ use codex_login::default_client::originator; use codex_otel::sanitize_metric_tag_value; use codex_protocol::config_types::WindowsSandboxLevel; use codex_protocol::protocol::SandboxPolicy; +use codex_utils_path::env::is_headless_environment; use std::collections::BTreeMap; use std::collections::HashMap; use std::path::Path; @@ -85,7 +86,21 @@ pub fn resolve_windows_sandbox_private_desktop(cfg: &ConfigToml, profile: &Confi .as_ref() .and_then(|windows| windows.sandbox_private_desktop) }) - .unwrap_or(true) + .unwrap_or_else(default_windows_sandbox_private_desktop) +} + +fn default_windows_sandbox_private_desktop() -> bool { + default_windows_sandbox_private_desktop_for_environment( + cfg!(target_os = "windows"), + is_headless_environment(), + ) +} + +fn default_windows_sandbox_private_desktop_for_environment( + is_windows: bool, + is_headless: bool, +) -> bool { + !is_windows || !is_headless } fn legacy_windows_sandbox_keys_present(features: Option<&FeaturesToml>) -> bool { diff --git a/codex-rs/core/src/windows_sandbox_tests.rs b/codex-rs/core/src/windows_sandbox_tests.rs index 27612c640a..04eb14257e 100644 --- a/codex-rs/core/src/windows_sandbox_tests.rs +++ b/codex-rs/core/src/windows_sandbox_tests.rs @@ -166,9 +166,9 @@ fn resolve_windows_sandbox_private_desktop_prefers_profile_windows() { #[test] fn resolve_windows_sandbox_private_desktop_defaults_to_true() { - assert!(resolve_windows_sandbox_private_desktop( - &ConfigToml::default(), - &ConfigProfile::default() + assert!(default_windows_sandbox_private_desktop_for_environment( + /*is_windows*/ true, + /*is_headless*/ false )); } @@ -187,3 +187,19 @@ fn resolve_windows_sandbox_private_desktop_respects_explicit_cfg_value() { &ConfigProfile::default() )); } + +#[test] +fn resolve_windows_sandbox_private_desktop_defaults_to_false_for_headless_windows() { + assert!(!default_windows_sandbox_private_desktop_for_environment( + /*is_windows*/ true, + /*is_headless*/ true + )); +} + +#[test] +fn resolve_windows_sandbox_private_desktop_non_windows_default_stays_true() { + assert!(default_windows_sandbox_private_desktop_for_environment( + /*is_windows*/ false, + /*is_headless*/ true + )); +}