Refactor Windows sandbox setup and service helpers (#45455)

## What changed

- Extract helper copying, token-user SID queries, provisioning pipe ownership, and service runtime lifecycle into dedicated modules.
- Simplify command-runner resolution and extract setup configuration loading, payload execution, provisioning request exchange, and response handling into helpers.
- Parameterize installation-record registry access and return the saved installation record from authenticated user registration.

## Testing

Add tests for explicit setup `cwd` selection and effective workspace roots, plus valid and invalid token-user SID queries. Move existing helper-copy and freshness tests alongside the extracted copy implementation.

GitOrigin-RevId: ffb39adae7611baa95e85c89f9a31ef7a779e217
This commit is contained in:
zm-oai authored and copyberry committed 2026-09-14 16:05:40 +00:00
1 parent 7a48b95c6c
commit 1a02867bd1
19 files changed
+640 -486

No files matched your search

@@ -43,22 +43,13 @@ impl WindowsSandboxRequestProcessor {
) -> Result<(), JSONRPCErrorError> {
// Validate requirements before acknowledging setup so callers do not get a
// `started` response for a Windows sandbox mode that cannot be persisted.
let command_cwd = params
.cwd
.map(PathBuf::from)
.unwrap_or_else(|| self.config.cwd.to_path_buf());
let config = self
.config_manager
.load_for_cwd(
/*request_overrides*/ None,
ConfigOverrides {
cwd: Some(command_cwd.clone()),
..Default::default()
},
Some(command_cwd.clone()),
)
.await
.map_err(|err| config_load_error(&err))?;
let (config, command_cwd) = load_setup_config(
&self.config_manager,
self.config.cwd.as_path(),
params.cwd.map(PathBuf::from),
)
.await
.map_err(|err| config_load_error(&err))?;
let setup_mode = resolve_allowed_windows_sandbox_setup_mode(
config.config_layer_stack.requirements(),
params.mode,
@@ -181,6 +172,29 @@ impl WindowsSandboxRequestProcessor {
}
}
async fn load_setup_config(
manager: &ConfigManager,
fallback_cwd: &std::path::Path,
requested_cwd: Option<PathBuf>,
) -> std::io::Result<(Config, PathBuf)> {
let cwd = requested_cwd.unwrap_or_else(|| fallback_cwd.to_path_buf());
let config = manager
.load_for_cwd(
/*request_overrides*/ None,
ConfigOverrides {
cwd: Some(cwd.clone()),
..Default::default()
},
Some(cwd.clone()),
)
.await?;
Ok((config, cwd))
}
#[cfg(test)]
#[path = "windows_sandbox_setup_config_tests.rs"]
mod setup_config_tests;
/// Resolves the requested API mode after checking that managed requirements allow it.
fn resolve_allowed_windows_sandbox_setup_mode(
requirements: &codex_config::ConfigRequirements,
@@ -0,0 +1,23 @@
//! Exercise the setup RPC's real configuration loading without changing Windows accounts or ACLs.
use super::ConfigManager;
use super::load_setup_config;
use codex_utils_absolute_path::AbsolutePathBuf;
use pretty_assertions::assert_eq;
#[tokio::test]
async fn explicit_cwd_remains_the_setup_workspace() -> anyhow::Result<()> {
let home = tempfile::tempdir()?;
let install = tempfile::tempdir()?;
let project = tempfile::tempdir()?;
let manager = ConfigManager::without_managed_config_for_tests(home.path().to_path_buf());
let (config, command_cwd) =
load_setup_config(&manager, install.path(), Some(project.path().to_path_buf())).await?;
let expected = AbsolutePathBuf::from_absolute_path(project.path())?;
assert_eq!(
(command_cwd, config.effective_workspace_roots()),
(project.path().to_path_buf(), vec![expected]),
);
Ok(())
}