Propagate temporary directories into sandbox policy contexts (#40660)

## Why

Filesystem policies containing `:tmpdir` need executor-local directory bindings,
including when execution occurs in a remote environment.

## What changed

- Cache temporary directories reported by remote executors and discover the
  equivalent directories for local environments.
- Preserve the cached directories across inherited environment selection and
  include them in `FileSystemSandboxContext`.
- Add `FileSystemSandboxPolicyContext` and a context accessor so filesystem
  policy entries can be resolved with the executor-owned current directory,
  workspace roots, and temporary directories.

## Testing

Extend environment-selection tests to cover local, remote, and inherited
temporary-directory policy context.

GitOrigin-RevId: 36335af3465c529f024bf69293af288803dd582d
This commit is contained in:
iceweasel-oai authored and copyberry committed 2026-08-25 18:04:01 +00:00
1 parent 2f4b105514
commit ba9567daf3
10 files changed
+126 -12

No files matched your search

+64 -10
View File
@@ -8,6 +8,7 @@ use arc_swap::ArcSwap;
use async_channel::Sender;
use codex_exec_server::Environment;
use codex_exec_server::EnvironmentConnectionState;
use codex_exec_server::EnvironmentInfo;
use codex_exec_server::EnvironmentManager;
use codex_exec_server::ExecServerError;
use codex_exec_server::ExecutorFileSystem;
@@ -136,6 +137,7 @@ type PendingConfigurationResult = Result<EnvironmentConfig, String>;
struct ResolvedEnvironment {
environment: Arc<Environment>,
shell: Option<Shell>,
temporary_directories: Option<Vec<PathUri>>,
shell_snapshot: ShellSnapshotTask,
shell_snapshot_v2_supported: bool,
installed_config: Option<EnvironmentConfig>,
@@ -239,6 +241,7 @@ impl ThreadEnvironments {
futures::future::ready(Ok(ResolvedEnvironment {
environment: environment.environment,
shell: environment.shell,
temporary_directories: environment.temporary_directories,
shell_snapshot: environment.shell_snapshot,
shell_snapshot_v2_supported: environment.shell_snapshot_v2_supported,
installed_config: None,
@@ -604,10 +607,13 @@ impl ThreadEnvironments {
};
// Resolve the attachment only after both prerequisites are ready.
let ((), installed_config) = tokio::try_join!(connection_ready, configuration_ready)?;
let (shell, shell_snapshot_v2_supported) = if environment.is_remote() {
let (shell, temporary_directories, shell_snapshot_v2_supported) = if environment.is_remote()
{
match environment.info().await {
Ok(info) => (
match Shell::from_environment_shell_info(info.shell) {
Ok(info) => {
let temporary_directories = info.temporary_directories;
let shell_snapshot_v2_supported = info.capabilities.shell_snapshot_v2;
let shell = match Shell::from_environment_shell_info(info.shell) {
Ok(shell) => Some(shell),
Err(err) => {
tracing::warn!(
@@ -615,16 +621,20 @@ impl ThreadEnvironments {
);
None
}
},
info.capabilities.shell_snapshot_v2,
),
};
(shell, temporary_directories, shell_snapshot_v2_supported)
}
Err(err) => {
tracing::warn!("failed to get info for environment `{environment_id}`: {err}");
(None, false)
(None, None, false)
}
}
} else {
(Some(local_shell), cfg!(unix))
(
Some(local_shell),
Some(EnvironmentInfo::local_temporary_directories()),
cfg!(unix),
)
};
let task = shell_snapshot
.build(Arc::clone(&environment), selection.cwd, shell.clone())
@@ -636,6 +646,7 @@ impl ThreadEnvironments {
Ok(ResolvedEnvironment {
environment,
shell,
temporary_directories,
shell_snapshot: task,
shell_snapshot_v2_supported,
installed_config,
@@ -715,6 +726,7 @@ impl TurnEnvironmentState {
turn_environment.shell_snapshot = environment.shell_snapshot;
turn_environment.shell_snapshot_v2_supported =
environment.shell_snapshot_v2_supported;
turn_environment.temporary_directories = environment.temporary_directories;
Some(Self::Ready(turn_environment))
}
Some(Err(err)) => {
@@ -881,6 +893,7 @@ mod tests {
use codex_protocol::config_types::WindowsSandboxLevel;
use codex_protocol::models::ActivePermissionProfile;
use codex_protocol::models::PermissionProfile;
use codex_protocol::permissions::FileSystemSandboxPolicyContext;
use codex_protocol::protocol::TurnEnvironmentSelection;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_path_uri::PathUri;
@@ -986,7 +999,10 @@ mod tests {
.send(Message::Text(
serde_json::json!({
"id": info["id"],
"result": { "shell": { "name": "zsh", "path": "/bin/zsh" } }
"result": {
"shell": { "name": "zsh", "path": "/bin/zsh" },
"temporaryDirectories": ["file:///tmp/remote"],
}
})
.to_string()
.into(),
@@ -1111,9 +1127,21 @@ url = "ws://127.0.0.1:8765"
let snapshot = turn_environments.snapshot().await;
let environment = snapshot.primary().expect("local environment");
let expected_temporary_directories = EnvironmentInfo::local_temporary_directories();
assert_eq!(environment.shell.as_ref(), Some(&local_shell));
assert_eq!(environment.config(), &expected_config);
assert_eq!(
environment
.sandbox_context(/*additional_permissions*/ None)
.policy_context()
.expect("selected environment sandbox context has cwd"),
FileSystemSandboxPolicyContext {
cwd: environment.cwd(),
workspace_roots: &[],
temporary_directories: Some(expected_temporary_directories.as_slice()),
}
);
}
#[tokio::test]
@@ -1424,6 +1452,20 @@ url = "ws://127.0.0.1:8765"
vec![expected_config.clone(), expected_config]
);
assert_eq!(attached.to_selections(), vec![remote, local]);
let environment = attached.primary().expect("remote environment");
let expected_temporary_directories =
[PathUri::parse("file:///tmp/remote").expect("remote temporary directory")];
assert_eq!(
environment
.sandbox_context(/*additional_permissions*/ None)
.policy_context()
.expect("selected environment sandbox context has cwd"),
FileSystemSandboxPolicyContext {
cwd: environment.cwd(),
workspace_roots: &[],
temporary_directories: Some(expected_temporary_directories.as_slice()),
}
);
assert_eq!(
next_starting
.refresh_readiness()
@@ -1637,7 +1679,7 @@ url = "ws://127.0.0.1:8765"
Environment::create_for_tests(Some("ws://127.0.0.1:8765".to_string()))
.expect("inherited environment"),
);
let inherited = TurnEnvironment::new(
let mut inherited = TurnEnvironment::new(
TurnEnvironmentSelection {
config: EnvironmentConfigState::Ready(test_environment_config()),
..selection.clone()
@@ -1646,6 +1688,9 @@ url = "ws://127.0.0.1:8765"
Arc::clone(&inherited_environment),
/*shell*/ None,
);
inherited.temporary_directories = Some(vec![
PathUri::parse("file:///tmp/inherited").expect("temporary directory"),
]);
let manager = Arc::new(environment_manager_without_environments());
manager
.upsert_environment(
@@ -1687,6 +1732,15 @@ url = "ws://127.0.0.1:8765"
let inherited = snapshot.primary().expect("inherited environment");
assert!(Arc::ptr_eq(&inherited.environment, &inherited_environment));
assert_eq!(inherited.config(), &child_config);
assert_eq!(
inherited
.sandbox_context(/*additional_permissions*/ None)
.temporary_directories
.as_deref(),
Some(
[PathUri::parse("file:///tmp/inherited").expect("temporary directory")].as_slice()
)
);
}
#[tokio::test]