Batch sandboxed capability root discovery (#40443)

## What changed

- Run capability discovery for roots with the same sandbox context in one sandbox helper invocation.
- Keep roots with different sandbox permissions isolated, and fall back to per-root discovery if the batched invocation fails.

## Testing

- Add coverage for single-launch batching, fallback after a sandbox launch failure, and roots with different permission profiles.

GitOrigin-RevId: a6e185a8edfd028fc3639b7c6c3cc7da342b6fe3
This commit is contained in:
jif
2026-08-24 17:39:16 +00:00
committed by copyberry
parent a12c4f4c01
commit eb15245d82
7 changed files with 373 additions and 62 deletions
@@ -3,6 +3,7 @@ use std::io;
use base64::Engine as _;
use base64::engine::general_purpose::STANDARD;
use codex_exec_server_protocol::JSONRPCErrorError;
use codex_protocol::config_types::WindowsSandboxLevel;
use crate::CapabilityRootsDiscoverParams;
use crate::CapabilityRootsDiscoverResponse;
@@ -74,6 +75,42 @@ impl FileSystemHandler {
&self,
params: CapabilityRootsDiscoverParams,
) -> Result<CapabilityRootsDiscoverResponse, JSONRPCErrorError> {
let sandbox = params
.roots
.first()
.and_then(|root| root.sandbox.as_ref())
.filter(|sandbox| {
sandbox.should_run_in_sandbox()
&& (!cfg!(target_os = "windows")
|| sandbox.windows_sandbox_level != WindowsSandboxLevel::Disabled)
&& params
.roots
.iter()
.all(|root| root.sandbox.as_ref() == Some(*sandbox))
})
.cloned();
if let Some(sandbox) = sandbox {
let mut batched_params = params.clone();
for root in &mut batched_params.roots {
root.sandbox = None;
}
let result = match self.file_system.sandboxed() {
Ok(file_system) => {
file_system
.discover_capability_roots(batched_params, &sandbox)
.await
}
Err(error) => Err(error),
};
match result {
Ok(response) => return Ok(response),
Err(error) => {
tracing::warn!(%error, "batched capability discovery failed; retrying roots separately");
}
}
}
crate::discover_capability_roots(&self.file_system, params)
.await
.map_err(|error| invalid_request(error.to_string()))