Log Windows sandbox availability and backend selection (#51974)

## What changed

Record local MXC availability and the selected Windows sandbox backend at app-server and session startup. Log the backend again when an accepted session settings update changes the Windows sandbox level, using a blocking task for the log write.

Selection entries distinguish `mxc`, `disabled`, `unelevated`, and `elevated` in the sandbox logs under `.sandbox` in Codex home.

## Testing

Update the remote thread persistence assertion to allow the `.sandbox` directory on Windows, since startup logs are not thread persistence artifacts.

GitOrigin-RevId: 4130b59ac7761223775ac9b6afb096abb7fb4615
This commit is contained in:
iceweasel-oai
2026-10-08 06:31:56 +00:00
committed by copyberry
parent 14c8b7771a
commit ea27864f99
4 changed files with 80 additions and 2 deletions
@@ -19,6 +19,7 @@ impl WindowsSandboxRequestProcessor {
config: Arc<Config>,
config_manager: ConfigManager,
) -> Self {
codex_core::windows_sandbox::log_windows_sandbox_startup(&config);
Self {
outgoing,
config,
@@ -711,9 +711,12 @@ fn assert_no_local_persistence_artifacts(codex_home: &Path) -> Result<()> {
"non-local thread persistence should not create sqlite artifacts: {sqlite_artifacts:?}"
);
let mut entries = codex_home_entries(codex_home)?;
// Host startup may leave sandbox migration markers, and Bazel test runs may
// initialize shell snapshot storage. Neither is thread persistence.
// Host startup may leave sandbox migration markers or Windows sandbox logs,
// and Bazel test runs may initialize shell snapshot storage. None is thread
// persistence.
entries.remove(".sandbox_migration");
#[cfg(windows)]
entries.remove(".sandbox");
entries.remove("shell_snapshots");
assert_eq!(
entries,
+11
View File
@@ -903,6 +903,7 @@ impl Session {
session_configuration
.validate(&environment_selections)
.map_err(|err| CodexErr::InvalidRequest(err.to_string()))?;
crate::windows_sandbox::log_windows_sandbox_startup(&config);
// Generate a unique ID for the lifetime of this session.
let session_source_clone = session_configuration.session_source.clone();
@@ -1933,6 +1934,7 @@ impl Session {
should_commit: impl FnOnce(&SessionConfiguration, &SessionConfiguration) -> bool + Send,
) -> ConstraintResult<Option<SessionSettingsCommit>> {
let notify_config_contributors = !self.services.extensions.config_contributors().is_empty();
let windows_sandbox_changed;
let (commit, previous_config, new_config, permission_profile_changed, mcp_inputs_changed) = {
let mut state = self.state.lock().await;
let updated = match self.apply_session_settings(&state.session_configuration, &updates)
@@ -1962,6 +1964,8 @@ impl Session {
self.mark_mcp_runtime_dirty();
}
// Save new environment defaults for future turns. The running turn keeps its own.
windows_sandbox_changed =
state.session_configuration.windows_sandbox_level != updated.windows_sandbox_level;
state.session_configuration = updated;
if root_service_tier_changed {
self.services.agent_control.propagate_config_update(
@@ -1990,6 +1994,13 @@ impl Session {
mcp_inputs_changed,
)
};
if windows_sandbox_changed {
crate::windows_sandbox::log_windows_sandbox_change(
commit.configuration.codex_home.as_path(),
commit.configuration.windows_sandbox_type,
commit.configuration.windows_sandbox_level,
);
}
self.emit_config_changed_contributors(previous_config.as_ref(), new_config.as_ref());
if permission_profile_changed {
self.refresh_managed_network_proxy_for_current_permission_profile()
+63
View File
@@ -1,3 +1,5 @@
//! Windows sandbox configuration, runtime policy, and lifecycle diagnostics.
use crate::config::Config;
use crate::config::edit::ConfigEditsBuilder;
use codex_config::config_toml::ConfigToml;
@@ -18,6 +20,67 @@ use std::path::Path;
use std::path::PathBuf;
use std::time::Instant;
/// Records local MXC availability and the selected backend at startup.
pub fn log_windows_sandbox_startup(config: &Config) {
#[cfg(windows)]
{
let sandbox_dir = codex_windows_sandbox::sandbox_dir(config.codex_home.as_path());
let _ = std::fs::create_dir_all(&sandbox_dir);
let available = codex_sandboxing::windows_mxc_available();
codex_windows_sandbox::log_note(
&format!("MXC availability: {available}"),
Some(&sandbox_dir),
);
log_windows_sandbox_selection(
config.codex_home.as_path(),
config.effective_local_windows_sandbox_type(),
WindowsSandboxLevel::from_config(config),
);
}
#[cfg(not(windows))]
let _ = config;
}
/// Records the local backend after an accepted selection change.
pub(crate) fn log_windows_sandbox_change(
codex_home: &Path,
sandbox_type: SandboxType,
sandbox_level: WindowsSandboxLevel,
) {
#[cfg(windows)]
{
let codex_home = codex_home.to_owned();
tokio::task::spawn_blocking(move || {
log_windows_sandbox_selection(&codex_home, sandbox_type, sandbox_level);
});
}
#[cfg(not(windows))]
let _ = (codex_home, sandbox_type, sandbox_level);
}
#[cfg(windows)]
fn log_windows_sandbox_selection(
codex_home: &Path,
sandbox_type: SandboxType,
sandbox_level: WindowsSandboxLevel,
) {
let backend = if sandbox_type == SandboxType::WindowsMxc {
"mxc"
} else {
match sandbox_level {
WindowsSandboxLevel::Disabled => "disabled",
WindowsSandboxLevel::RestrictedToken => "unelevated",
WindowsSandboxLevel::Elevated => "elevated",
}
};
let sandbox_dir = codex_windows_sandbox::sandbox_dir(codex_home);
let _ = std::fs::create_dir_all(&sandbox_dir);
codex_windows_sandbox::log_note(
&format!("Windows sandbox selection: {backend}"),
Some(&sandbox_dir),
);
}
/// Selects local binding policy from the sandbox and executor OS.
pub fn local_binding_policy_for_sandbox(
sandbox_type: SandboxType,