Serialize tests that share Windows sandbox accounts (#49389)

## Why

Elevated sandbox tests share machine-local Windows accounts. Concurrent setup can rotate account passwords between another test's setup and logon.

## What changed

- Add a shared file-lock guard for elevated sandbox tests in `codex-core`, `codex-exec-server`, and `codex-windows-sandbox`, held through sandbox commands and cleanup.
- Queue these tests in a single-threaded Nextest group before their timeouts start, while retaining the file lock to coordinate other test processes.
- Limit account locking to tests that use elevated accounts and remove the sandbox crate's blanket Bazel `exclusive-if-local` tag.

GitOrigin-RevId: 26d4606cb1fdcac65926d29835bed4eac04c19c0
This commit is contained in:
jgershen-oai
2026-09-29 22:50:07 +00:00
committed by copyberry
parent d6571322d2
commit 9212b3eca8
13 changed files with 125 additions and 23 deletions
+20
View File
@@ -43,6 +43,9 @@ max-threads = 1
[test-groups.windows_sandbox_legacy_sessions]
max-threads = 1
[test-groups.windows_sandbox_accounts]
max-threads = 1
[test-groups.windows_process_heavy]
max-threads = 2
@@ -82,6 +85,23 @@ test-group = 'app_server_integration_local'
filter = 'package(codex-core) & kind(test) & test(apply_patch_cli)'
test-group = 'core_apply_patch_cli_integration'
[[profile.default.overrides]]
# These cases share the runner's elevated sandbox accounts. Queue them before
# starting their timeout; their file lock still coordinates other test processes.
platform = 'cfg(windows)'
filter = '''
(package(codex-core) & (
test(windows_sandbox_cli_preserves_managed_deny_reads_across_launches) |
test(windows_elevated_does_not_create_missing_workspace_metadata) |
test(windows_elevated_enforces_deny_read_and_protects_setup_marker) |
test(windows_elevated_unified_exec_enforces_large_recursive_deny_reads) |
test(windows_elevated_approved_git_pull_preserves_deny_read)
)) |
(package(codex-exec-server) & test(file_system_elevated_relative_read_denial_uses_policy_cwd)) |
(package(codex-windows-sandbox) & test(elevated_non_tty_cmd_forwards_env_output_and_exit))
'''
test-group = 'windows_sandbox_accounts'
[[profile.default.overrides]]
# These tests create restricted-token Windows child processes and private desktops.
# Serialize them to avoid exhausting Windows session/global desktop resources in CI.
+10
View File
@@ -2976,6 +2976,7 @@ dependencies = [
"codex-utils-string",
"codex-web-search-extension",
"codex-windows-sandbox",
"codex-windows-sandbox-test-support",
"core_test_support",
"ctor 0.6.3",
"dirs",
@@ -3213,6 +3214,7 @@ dependencies = [
"codex-utils-rustls-provider",
"codex-websocket-auth",
"codex-websocket-client",
"codex-windows-sandbox-test-support",
"ctor 0.6.3",
"dirs",
"futures",
@@ -5282,6 +5284,7 @@ dependencies = [
"codex-utils-process",
"codex-utils-pty",
"codex-utils-string",
"codex-windows-sandbox-test-support",
"dirs-next",
"dunce",
"glob",
@@ -5313,6 +5316,13 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "codex-windows-sandbox-test-support"
version = "0.0.0"
dependencies = [
"dirs",
]
[[package]]
name = "codex-workload-identity"
version = "0.0.0"
+2
View File
@@ -112,6 +112,7 @@ members = [
"v8-poc",
"websocket-auth",
"websocket-client",
"windows-sandbox-rs/tests/support",
"windows-sandbox-service",
"worktree",
"workload-identity",
@@ -316,6 +317,7 @@ codex-user-verification = { path = "user-verification" }
codex-v8-poc = { path = "v8-poc" }
codex-workload-identity = { path = "workload-identity" }
codex-windows-sandbox = { path = "windows-sandbox-rs" }
codex-windows-sandbox-test-support = { path = "windows-sandbox-rs/tests/support" }
core_test_support = { path = "core/tests/common" }
# External
+1
View File
@@ -151,6 +151,7 @@ codex-test-binary-support = { workspace = true }
codex-utils-cargo-bin = { workspace = true }
codex-utils-redacted-string = { workspace = true }
codex-web-search-extension = { workspace = true }
codex-windows-sandbox-test-support = { workspace = true }
core_test_support = { workspace = true }
ctor = { workspace = true }
insta = { workspace = true }
+13 -19
View File
@@ -25,6 +25,7 @@ use codex_protocol::protocol::EventMsg;
use codex_protocol::protocol::Op;
use codex_protocol::protocol::ReviewDecision;
use codex_protocol::user_input::UserInput;
use codex_windows_sandbox_test_support::WindowsSandboxAccountTestGuard;
use core_test_support::PathExt;
use core_test_support::responses::ev_assistant_message;
use core_test_support::responses::ev_completed;
@@ -41,7 +42,6 @@ use serde_json::json;
use serial_test::serial;
use std::collections::HashMap;
use std::ffi::OsString;
use std::fs::File;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
@@ -75,32 +75,21 @@ impl Drop for EnvVarGuard {
}
enum TestCodexHome {
Persistent(PathBuf, File),
Temporary(TempDir, File),
Persistent(PathBuf),
Temporary(TempDir),
}
impl TestCodexHome {
fn path(&self) -> &Path {
match self {
Self::Persistent(path, _account_lock) => path.as_path(),
Self::Temporary(temp_dir, _account_lock) => temp_dir.path(),
Self::Persistent(path) => path.as_path(),
Self::Temporary(temp_dir) => temp_dir.path(),
}
}
}
fn codex_home_for_windows_sandbox_test(name: &str) -> anyhow::Result<TestCodexHome> {
// Bazel shards and nextest processes share Windows accounts. Hold this lock
// through the test so they cannot rotate each other's credentials mid-launch.
let lock_path = dirs::data_local_dir()
.context("resolve local app data for the Windows sandbox test lock")?
.join("codex-windows-sandbox-integration-test.lock");
let lock = File::options()
.read(true)
.append(true)
.create(true)
.open(lock_path)?;
lock.lock().context("lock Windows sandbox test accounts")?;
// Only tests that use the elevated accounts also take the shared account guard.
if let Some(test_tmpdir) = std::env::var_os("TEST_TMPDIR") {
// The elevated backend provisions machine-local sandbox users. Bazel
// retries run in the same Windows VM, so keep CODEX_HOME stable within
@@ -108,10 +97,10 @@ fn codex_home_for_windows_sandbox_test(name: &str) -> anyhow::Result<TestCodexHo
let codex_home = PathBuf::from(test_tmpdir).join(name);
std::fs::create_dir_all(&codex_home)
.with_context(|| format!("create stable test CODEX_HOME {}", codex_home.display()))?;
return Ok(TestCodexHome::Persistent(codex_home, lock));
return Ok(TestCodexHome::Persistent(codex_home));
}
Ok(TestCodexHome::Temporary(TempDir::new()?, lock))
Ok(TestCodexHome::Temporary(TempDir::new()?))
}
fn stage_windows_sandbox_helpers() -> anyhow::Result<()> {
@@ -205,6 +194,7 @@ fn assert_managed_deny_probe(output: &std::process::Output, launch: usize) -> an
#[test]
#[serial(codex_home)]
fn windows_sandbox_cli_preserves_managed_deny_reads_across_launches() -> anyhow::Result<()> {
let _account_guard = WindowsSandboxAccountTestGuard::acquire()?;
let codex_home =
codex_home_for_windows_sandbox_test("windows-cli-managed-deny-read-codex-home")?;
@@ -415,6 +405,7 @@ async fn windows_restricted_token_rejects_exact_and_glob_deny_read_policy() -> a
#[tokio::test]
#[serial(codex_home)]
async fn windows_elevated_does_not_create_missing_workspace_metadata() -> anyhow::Result<()> {
let _account_guard = WindowsSandboxAccountTestGuard::acquire()?;
let codex_home =
codex_home_for_windows_sandbox_test("windows-elevated-missing-metadata-codex-home")?;
let _codex_home_guard = EnvVarGuard::set("CODEX_HOME", codex_home.path().as_os_str());
@@ -540,6 +531,7 @@ $rules = foreach ($name in @('codex_sandbox_offline_block_inbound', 'codex_sandb
#[tokio::test]
#[serial(codex_home)]
async fn windows_elevated_enforces_deny_read_and_protects_setup_marker() -> anyhow::Result<()> {
let _account_guard = WindowsSandboxAccountTestGuard::acquire()?;
let codex_home = codex_home_for_windows_sandbox_test("windows-elevated-deny-read-codex-home")?;
let _codex_home_guard = EnvVarGuard::set("CODEX_HOME", codex_home.path().as_os_str());
stage_windows_sandbox_helpers()?;
@@ -680,6 +672,7 @@ async fn windows_elevated_enforces_deny_read_and_protects_setup_marker() -> anyh
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[serial(codex_home)]
async fn windows_elevated_unified_exec_enforces_large_recursive_deny_reads() -> anyhow::Result<()> {
let _account_guard = WindowsSandboxAccountTestGuard::acquire()?;
let codex_home =
codex_home_for_windows_sandbox_test("windows-elevated-tool-runtime-deny-read-codex-home")?;
let _codex_home_guard = EnvVarGuard::set("CODEX_HOME", codex_home.path().as_os_str());
@@ -857,6 +850,7 @@ async fn windows_elevated_approved_git_pull_preserves_deny_read() -> anyhow::Res
use EventMsg::ExecCommandEnd;
use EventMsg::TurnComplete;
let _account_guard = WindowsSandboxAccountTestGuard::acquire()?;
let codex_home = codex_home_for_windows_sandbox_test("windows-elevated-git-pull-deny-read")?;
let _codex_home_guard = EnvVarGuard::set("CODEX_HOME", codex_home.path().as_os_str());
stage_windows_sandbox_helpers()?;
+1
View File
@@ -95,6 +95,7 @@ anyhow = { workspace = true }
codex-exec-server-test-support = { workspace = true }
codex-test-binary-support = { workspace = true }
codex-utils-cargo-bin = { workspace = true }
codex-windows-sandbox-test-support = { workspace = true }
ctor = { workspace = true }
http = { workspace = true }
opentelemetry_sdk = { workspace = true }
@@ -33,6 +33,7 @@ use codex_protocol::permissions::NetworkSandboxPolicy;
use codex_protocol::protocol::SandboxPolicy;
use codex_sandboxing::SandboxType;
use codex_utils_path_uri::PathUri;
use codex_windows_sandbox_test_support::WindowsSandboxAccountTestGuard;
use futures::TryStreamExt;
use pretty_assertions::assert_eq;
use test_case::test_case;
@@ -456,6 +457,7 @@ async fn file_system_remote_fs_helper_respects_windows_sandbox_write_policy(
async fn file_system_elevated_relative_read_denial_uses_policy_cwd(
implementation: FileSystemImplementation,
) -> Result<()> {
let _account_guard = WindowsSandboxAccountTestGuard::acquire()?;
// Both implementations re-enter this test binary; the elevated backend finds its helpers
// next to that binary, while Cargo and Bazel provide them separately.
let test_exe = std::env::current_exe()?;
-4
View File
@@ -55,8 +55,4 @@ codex_rust_crate(
":codex-command-runner",
":codex-windows-sandbox-setup",
],
# Setup rotates machine-wide account passwords. The process-local test lock
# cannot prevent exec-server's elevated filesystem tests from rotating them
# between this crate's setup and logon. Windows tests execute locally in CI.
test_tags = ["exclusive-if-local"],
)
+1
View File
@@ -102,4 +102,5 @@ features = [
version = "0.52"
[dev-dependencies]
codex-windows-sandbox-test-support = { workspace = true }
pretty_assertions = { workspace = true }
@@ -16,6 +16,7 @@ use codex_protocol::models::PermissionProfile;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_pty::ProcessDriver;
use codex_utils_pty::ProcessSignal;
use codex_windows_sandbox_test_support::WindowsSandboxAccountTestGuard;
use pretty_assertions::assert_eq;
use std::collections::HashMap;
use std::fs;
@@ -302,6 +303,8 @@ fn legacy_non_tty_cmd_emits_output() {
#[test]
fn elevated_non_tty_cmd_forwards_env_output_and_exit() {
let _account_guard =
WindowsSandboxAccountTestGuard::acquire().expect("lock Windows sandbox test accounts");
let _guard = legacy_process_test_guard();
let runtime = current_thread_runtime();
runtime.block_on(async move {
@@ -0,0 +1,8 @@
load("//:defs.bzl", "codex_rust_crate")
codex_rust_crate(
name = "support",
crate_name = "codex_windows_sandbox_test_support",
# This support library has no tests of its own.
test_tags = ["manual"],
)
@@ -0,0 +1,15 @@
[package]
name = "codex-windows-sandbox-test-support"
version.workspace = true
edition.workspace = true
license.workspace = true
[lib]
test = false
doctest = false
[lints]
workspace = true
[target.'cfg(windows)'.dependencies]
dirs = { workspace = true }
@@ -0,0 +1,49 @@
//! Coordinates tests that share the runner's Windows sandbox accounts across processes.
#![cfg(windows)]
use std::fs::File;
use std::fs::OpenOptions;
use std::io;
/// Serializes tests that provision or use the runner's shared Windows sandbox accounts.
/// Hold this from before setup until all sandbox commands and cleanup have finished.
#[must_use = "the account lock is released when the guard is dropped"]
pub struct WindowsSandboxAccountTestGuard {
_file: File,
}
impl WindowsSandboxAccountTestGuard {
/// Acquires the same file lock across all test processes run by this Windows user.
pub fn acquire() -> io::Result<Self> {
let directory = dirs::data_local_dir().ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
"Windows local application data directory is unavailable",
)
})?;
// Bazel uses per-test temporary directories, so the lock lives in the runner user's
// Windows known folder instead. It is separate from the production setup lock.
let path = directory.join("codex-windows-sandbox-integration-test.lock");
// Leave the file in place: deleting it would let processes lock different files at this path.
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(path)
.map_err(|error| {
io::Error::new(
error.kind(),
format!("open Windows sandbox test lock: {error}"),
)
})?;
file.lock().map_err(|error| {
io::Error::new(
error.kind(),
format!("lock Windows sandbox test accounts: {error}"),
)
})?;
Ok(Self { _file: file })
}
}