Move workspace roots onto thread/session state and stop using active permission profile modifications as an overlay for writable roots. Existing app-server threads now preserve their persisted PermissionProfile value across resume, fork, and turn updates; permissions requests on existing threads only update the active named profile after validating it exists. Workspace roots can be updated independently, and SandboxPolicy::WorkspaceWrite no longer stores its own writable_roots.

This commit is contained in:
Michael Bolin
2026-05-11 19:06:28 -07:00
parent 9ab7f4e6ac
commit d824faf0dc
150 changed files with 3300 additions and 2653 deletions
+1 -1
View File
@@ -11,9 +11,9 @@ workspace = true
codex-core = { workspace = true }
codex-model-provider-info = { workspace = true }
codex-protocol = { workspace = true }
codex-utils-absolute-path = { workspace = true }
[dev-dependencies]
codex-utils-absolute-path = { workspace = true }
pretty_assertions = { workspace = true }
[lib]
@@ -1,7 +1,8 @@
use codex_core::config::Config;
use codex_model_provider_info::WireApi;
use codex_utils_absolute_path::AbsolutePathBuf;
use crate::sandbox_summary::summarize_sandbox_policy;
use crate::sandbox_summary::summarize_permission_profile_with_hidden_writable_roots;
/// Build a list of key/value pairs summarizing the effective configuration.
pub fn create_config_summary_entries(config: &Config, model: &str) -> Vec<(&'static str, String)> {
@@ -13,14 +14,7 @@ pub fn create_config_summary_entries(config: &Config, model: &str) -> Vec<(&'sta
"approval",
config.permissions.approval_policy.value().to_string(),
),
(
"sandbox",
summarize_sandbox_policy(
&config
.permissions
.legacy_sandbox_policy(config.cwd.as_path()),
),
),
("sandbox", summarize_config_permission_profile(config)),
];
if config.model_provider.wire_api == WireApi::Responses {
let reasoning_effort = config
@@ -41,3 +35,17 @@ pub fn create_config_summary_entries(config: &Config, model: &str) -> Vec<(&'sta
entries
}
pub fn summarize_config_permission_profile(config: &Config) -> String {
let hidden_writable_roots = hidden_writable_roots(config);
summarize_permission_profile_with_hidden_writable_roots(
config.permissions.permission_profile.get(),
&config.cwd,
&config.workspace_roots,
&hidden_writable_roots,
)
}
fn hidden_writable_roots(config: &Config) -> [AbsolutePathBuf; 1] {
[config.codex_home.join("memories")]
}
+1 -1
View File
@@ -2,5 +2,5 @@ mod config_summary;
mod sandbox_summary;
pub use config_summary::create_config_summary_entries;
pub use config_summary::summarize_config_permission_profile;
pub use sandbox_summary::summarize_permission_profile;
pub use sandbox_summary::summarize_sandbox_policy;
@@ -1,115 +1,283 @@
use codex_protocol::models::PermissionProfile;
use codex_protocol::protocol::NetworkAccess;
use codex_protocol::protocol::SandboxPolicy;
use codex_protocol::permissions::NetworkSandboxPolicy;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_absolute_path::canonicalize_preserving_symlinks;
use std::path::Path;
pub fn summarize_sandbox_policy(sandbox_policy: &SandboxPolicy) -> String {
match sandbox_policy {
SandboxPolicy::DangerFullAccess => "danger-full-access".to_string(),
SandboxPolicy::ReadOnly { network_access, .. } => {
let mut summary = "read-only".to_string();
if *network_access {
summary.push_str(" (network access enabled)");
}
summary
}
SandboxPolicy::ExternalSandbox { network_access } => {
pub fn summarize_permission_profile(
permission_profile: &PermissionProfile,
cwd: &AbsolutePathBuf,
workspace_roots: &[AbsolutePathBuf],
) -> String {
summarize_permission_profile_with_hidden_writable_roots(
permission_profile,
cwd,
workspace_roots,
&[],
)
}
pub(crate) fn summarize_permission_profile_with_hidden_writable_roots(
permission_profile: &PermissionProfile,
cwd: &AbsolutePathBuf,
workspace_roots: &[AbsolutePathBuf],
hidden_writable_roots: &[AbsolutePathBuf],
) -> String {
match permission_profile {
PermissionProfile::Disabled => "danger-full-access".to_string(),
PermissionProfile::External { network } => {
let mut summary = "external-sandbox".to_string();
if matches!(network_access, NetworkAccess::Enabled) {
summary.push_str(" (network access enabled)");
}
append_network_summary(&mut summary, *network);
summary
}
SandboxPolicy::WorkspaceWrite {
writable_roots,
network_access,
exclude_tmpdir_env_var,
exclude_slash_tmp,
} => {
PermissionProfile::Managed { .. } => {
let materialized_profile =
permission_profile.materialize_project_roots_with_workspace_roots(workspace_roots);
let file_system_policy = materialized_profile.file_system_sandbox_policy();
let network_policy = materialized_profile.network_sandbox_policy();
if file_system_policy.has_full_disk_write_access() {
return if network_policy.is_enabled() {
"danger-full-access".to_string()
} else {
"external-sandbox".to_string()
};
}
let writable_roots = file_system_policy
.get_writable_roots_with_cwd(cwd.as_path())
.into_iter()
.filter(|root| {
!hidden_writable_roots.iter().any(|hidden_root| {
paths_match_after_canonicalization(
root.root.as_path(),
hidden_root.as_path(),
)
})
})
.collect::<Vec<_>>();
if writable_roots.is_empty() {
let mut summary = "read-only".to_string();
append_network_summary(&mut summary, network_policy);
return summary;
}
let mut summary = "workspace-write".to_string();
let mut writable_entries = Vec::<String>::new();
writable_entries.push("workdir".to_string());
if !*exclude_slash_tmp {
writable_entries.push("/tmp".to_string());
}
if !*exclude_tmpdir_env_var {
writable_entries.push("$TMPDIR".to_string());
}
writable_entries.extend(
writable_roots
.iter()
.map(|p| p.to_string_lossy().to_string()),
);
let writable_entries = writable_roots
.iter()
.map(|root| writable_root_label(root.root.as_path(), cwd.as_path()))
.collect::<Vec<_>>();
summary.push_str(&format!(" [{}]", writable_entries.join(", ")));
if *network_access {
summary.push_str(" (network access enabled)");
}
append_network_summary(&mut summary, network_policy);
summary
}
}
}
pub fn summarize_permission_profile(permission_profile: &PermissionProfile, cwd: &Path) -> String {
match permission_profile.to_legacy_sandbox_policy(cwd) {
Ok(policy) => summarize_sandbox_policy(&policy),
Err(_) => {
if permission_profile.network_sandbox_policy().is_enabled() {
"custom permissions (network access enabled)".to_string()
} else {
"custom permissions".to_string()
}
}
fn append_network_summary(summary: &mut String, network_policy: NetworkSandboxPolicy) {
if network_policy.is_enabled() {
summary.push_str(" (network access enabled)");
}
}
fn writable_root_label(root: &Path, cwd: &Path) -> String {
if paths_match_after_canonicalization(root, cwd) {
return "workdir".to_string();
}
if paths_match_after_canonicalization(root, Path::new("/tmp")) {
return "/tmp".to_string();
}
if std::env::var_os("TMPDIR")
.filter(|tmpdir| !tmpdir.is_empty())
.is_some_and(|tmpdir| paths_match_after_canonicalization(root, Path::new(&tmpdir)))
{
return "$TMPDIR".to_string();
}
display_path_label(root)
}
fn paths_match_after_canonicalization(left: &Path, right: &Path) -> bool {
match (
canonicalize_preserving_symlinks(left),
canonicalize_preserving_symlinks(right),
) {
(Ok(left), Ok(right)) if left == right => true,
_ => display_path_label(left) == display_path_label(right),
}
}
fn display_path_label(path: &Path) -> String {
path.strip_prefix("/private/tmp")
.ok()
.map(|suffix| Path::new("/tmp").join(suffix))
.unwrap_or_else(|| path.to_path_buf())
.to_string_lossy()
.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_protocol::permissions::FileSystemAccessMode;
use codex_protocol::permissions::FileSystemPath;
use codex_protocol::permissions::FileSystemSandboxEntry;
use codex_protocol::permissions::FileSystemSandboxPolicy;
use codex_utils_absolute_path::test_support::PathBufExt;
use codex_utils_absolute_path::test_support::test_path_buf;
use pretty_assertions::assert_eq;
#[test]
fn summarizes_disabled_permission_profile_as_danger_full_access() {
let cwd = test_path_buf("/tmp/project").abs();
let summary = summarize_permission_profile(&PermissionProfile::Disabled, &cwd, &[]);
assert_eq!(summary, "danger-full-access");
}
#[test]
fn summarizes_external_sandbox_without_network_access_suffix() {
let summary = summarize_sandbox_policy(&SandboxPolicy::ExternalSandbox {
network_access: NetworkAccess::Restricted,
});
let cwd = test_path_buf("/tmp/project").abs();
let summary = summarize_permission_profile(
&PermissionProfile::External {
network: NetworkSandboxPolicy::Restricted,
},
&cwd,
&[],
);
assert_eq!(summary, "external-sandbox");
}
#[test]
fn summarizes_external_sandbox_with_enabled_network() {
let summary = summarize_sandbox_policy(&SandboxPolicy::ExternalSandbox {
network_access: NetworkAccess::Enabled,
});
let cwd = test_path_buf("/tmp/project").abs();
let summary = summarize_permission_profile(
&PermissionProfile::External {
network: NetworkSandboxPolicy::Enabled,
},
&cwd,
&[],
);
assert_eq!(summary, "external-sandbox (network access enabled)");
}
#[test]
fn summarizes_read_only_with_enabled_network() {
let summary = summarize_sandbox_policy(&SandboxPolicy::ReadOnly {
network_access: true,
});
let cwd = test_path_buf("/tmp/project").abs();
let profile = PermissionProfile::from_runtime_permissions(
&FileSystemSandboxPolicy::restricted(Vec::new()),
NetworkSandboxPolicy::Enabled,
);
let summary = summarize_permission_profile(&profile, &cwd, &[]);
assert_eq!(summary, "read-only (network access enabled)");
}
#[test]
fn workspace_write_summary_still_includes_network_access() {
let root = if cfg!(windows) { "C:\\repo" } else { "/repo" };
let writable_root = AbsolutePathBuf::try_from(root).unwrap();
let summary = summarize_sandbox_policy(&SandboxPolicy::WorkspaceWrite {
writable_roots: vec![writable_root.clone()],
network_access: true,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
});
fn summarizes_unrestricted_managed_profile_with_enabled_network_as_danger_full_access() {
let cwd = test_path_buf("/tmp/project").abs();
let profile = PermissionProfile::from_runtime_permissions(
&FileSystemSandboxPolicy::unrestricted(),
NetworkSandboxPolicy::Enabled,
);
let summary = summarize_permission_profile(&profile, &cwd, &[]);
assert_eq!(summary, "danger-full-access");
}
#[test]
fn summarizes_unrestricted_managed_profile_without_network_as_external_sandbox() {
let cwd = test_path_buf("/tmp/project").abs();
let profile = PermissionProfile::from_runtime_permissions(
&FileSystemSandboxPolicy::unrestricted(),
NetworkSandboxPolicy::Restricted,
);
let summary = summarize_permission_profile(&profile, &cwd, &[]);
assert_eq!(summary, "external-sandbox");
}
#[test]
fn workspace_write_summary_includes_workspace_roots_and_network_access() {
let cwd = test_path_buf("/tmp/project").abs();
let cache_root = test_path_buf("/tmp/cache").abs();
let profile = PermissionProfile::from_runtime_permissions(
&FileSystemSandboxPolicy::restricted(vec![FileSystemSandboxEntry {
path: FileSystemPath::Special {
value: codex_protocol::permissions::FileSystemSpecialPath::ProjectRoots {
subpath: None,
},
},
access: FileSystemAccessMode::Write,
}]),
NetworkSandboxPolicy::Enabled,
);
let summary =
summarize_permission_profile(&profile, &cwd, &[cwd.clone(), cache_root.clone()]);
assert_eq!(
summary,
format!(
"workspace-write [workdir, {}] (network access enabled)",
writable_root.to_string_lossy()
cache_root.display()
)
);
}
#[test]
fn workspace_write_summary_does_not_treat_cwd_as_root_unless_listed() {
let cwd = test_path_buf("/tmp/project").abs();
let cache_root = test_path_buf("/tmp/cache").abs();
let profile = PermissionProfile::from_runtime_permissions(
&FileSystemSandboxPolicy::restricted(vec![FileSystemSandboxEntry {
path: FileSystemPath::Special {
value: codex_protocol::permissions::FileSystemSpecialPath::ProjectRoots {
subpath: None,
},
},
access: FileSystemAccessMode::Write,
}]),
NetworkSandboxPolicy::Restricted,
);
let summary =
summarize_permission_profile(&profile, &cwd, std::slice::from_ref(&cache_root));
assert_eq!(
summary,
format!("workspace-write [{}]", cache_root.display())
);
}
#[test]
fn workspace_write_summary_hides_internal_writable_roots() {
let cwd = test_path_buf("/tmp/project").abs();
let memories_root = test_path_buf("/tmp/codex-home/memories").abs();
let profile = PermissionProfile::from_runtime_permissions(
&FileSystemSandboxPolicy::restricted(vec![
FileSystemSandboxEntry {
path: FileSystemPath::Special {
value: codex_protocol::permissions::FileSystemSpecialPath::ProjectRoots {
subpath: None,
},
},
access: FileSystemAccessMode::Write,
},
FileSystemSandboxEntry {
path: FileSystemPath::Path {
path: memories_root.clone(),
},
access: FileSystemAccessMode::Write,
},
]),
NetworkSandboxPolicy::Restricted,
);
let summary = summarize_permission_profile_with_hidden_writable_roots(
&profile,
&cwd,
std::slice::from_ref(&cwd),
std::slice::from_ref(&memories_root),
);
assert_eq!(summary, "workspace-write [workdir]");
}
#[test]
fn distinct_missing_paths_do_not_match_after_canonicalization() {
assert!(!paths_match_after_canonicalization(
test_path_buf("/tmp/codex-missing-left").as_path(),
test_path_buf("/tmp/codex-missing-right").as_path(),
));
}
}