Files
codex/codex-rs/core/src/context/environment_context.rs
T
Charlie Marsh 42c320991e Add compact filesystem context for workspace-write profiles (#64164)
## Why

The filesystem context sent to the model repeats the same generated
read-only metadata entries under each writable path.

## What changed

Add the opt-in `features.compact_filesystem_context` flag. For matching
workspace-write profiles with multiple writable paths, list workspace
roots and other writable paths separately, then state the shared
read-only rule for `.git`, `.agents`, `.codex`, and `.aws` once.
Keep temporary-directory write grants explicit and use the full listing
for profiles with rules the summary cannot represent.

## Testing

Cover compact rendering and fallback for explicit metadata reads or
Windows paths with different casing. Snapshot shared and per-environment
context across two turns to verify that unchanged permissions carry
forward.

GitOrigin-RevId: 4e9f36a054a96f06c9e5d0b3882148b132233e4f
2026-11-26 15:09:58 +00:00

365 lines
12 KiB
Rust

use codex_protocol::models::ManagedFileSystemPermissions;
use codex_protocol::models::PermissionProfile;
use codex_protocol::permissions::FileSystemAccessMode;
use codex_protocol::permissions::FileSystemPath;
use codex_protocol::permissions::FileSystemSandboxEntry;
use codex_protocol::permissions::FileSystemSpecialPath;
use codex_protocol::permissions::PROTECTED_METADATA_PATH_NAMES;
use codex_utils_path_uri::PathUri;
use std::collections::HashSet;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum FileSystemFormat {
Full,
Compact,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct FileSystemContext {
format: FileSystemFormat,
workspace_roots: Vec<String>,
permission_profile: FileSystemPermissionProfileContext,
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum FileSystemPermissionProfileContext {
Managed(ManagedFileSystemContext),
Disabled,
External,
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum ManagedFileSystemContext {
Restricted {
entries: Vec<FileSystemSandboxEntry>,
glob_scan_max_depth: Option<usize>,
},
Unrestricted,
}
impl FileSystemContext {
pub(super) fn from_permission_profile(
permission_profile: &PermissionProfile,
workspace_roots: &[PathUri],
format: FileSystemFormat,
) -> Self {
let permission_profile = permission_profile
.clone()
.materialize_project_roots_with_path_uris(workspace_roots);
let workspace_roots = workspace_roots
.iter()
.map(PathUri::inferred_native_path_string)
.collect();
let permission_profile = match permission_profile {
PermissionProfile::Managed { file_system, .. } => {
FileSystemPermissionProfileContext::Managed(ManagedFileSystemContext::from(
file_system,
))
}
PermissionProfile::Disabled => FileSystemPermissionProfileContext::Disabled,
PermissionProfile::External { .. } => FileSystemPermissionProfileContext::External,
};
Self {
format,
workspace_roots,
permission_profile,
}
}
pub(super) fn render(&self) -> String {
if self.format == FileSystemFormat::Compact
&& let FileSystemPermissionProfileContext::Managed(
ManagedFileSystemContext::Restricted {
entries,
glob_scan_max_depth: None,
},
) = &self.permission_profile
&& let Some(compact) = render_compact_filesystem(&self.workspace_roots, entries)
{
return compact;
}
let mut rendered = "<filesystem>".to_string();
if !self.workspace_roots.is_empty() {
rendered.push_str("<workspace_roots>");
for root in &self.workspace_roots {
push_text_element(&mut rendered, "root", root);
}
rendered.push_str("</workspace_roots>");
}
self.permission_profile.render(&mut rendered);
rendered.push_str("</filesystem>");
rendered
}
}
impl From<ManagedFileSystemPermissions> for ManagedFileSystemContext {
fn from(file_system: ManagedFileSystemPermissions) -> Self {
match file_system {
ManagedFileSystemPermissions::Restricted {
mut entries,
glob_scan_max_depth,
} => {
dedupe_file_system_entries(&mut entries);
Self::Restricted {
entries,
glob_scan_max_depth: glob_scan_max_depth.map(usize::from),
}
}
ManagedFileSystemPermissions::Unrestricted => Self::Unrestricted,
}
}
}
impl FileSystemPermissionProfileContext {
fn render(&self, rendered: &mut String) {
match self {
Self::Managed(file_system) => {
rendered.push_str("<permission_profile type=\"managed\">");
file_system.render(rendered);
rendered.push_str("</permission_profile>");
}
Self::Disabled => {
rendered.push_str(
"<permission_profile type=\"disabled\"><file_system type=\"unrestricted\" /></permission_profile>",
);
}
Self::External => {
rendered.push_str(
"<permission_profile type=\"external\"><file_system type=\"external\" /></permission_profile>",
);
}
}
}
}
impl ManagedFileSystemContext {
fn render(&self, rendered: &mut String) {
match self {
Self::Restricted {
entries,
glob_scan_max_depth,
} => {
if entries.is_empty() && glob_scan_max_depth.is_none() {
rendered.push_str("<file_system type=\"restricted\" />");
return;
}
rendered.push_str("<file_system type=\"restricted\"");
if let Some(glob_scan_max_depth) = glob_scan_max_depth {
rendered.push_str(&format!(" glob_scan_max_depth=\"{glob_scan_max_depth}\""));
}
rendered.push('>');
for entry in entries {
render_file_system_entry(rendered, entry);
}
rendered.push_str("</file_system>");
}
Self::Unrestricted => {
rendered.push_str("<file_system type=\"unrestricted\" />");
}
}
}
}
/// Describe a workspace-write profile only if every literal write path has exactly
/// the same generated read-only children. Any other rule needs the full listing.
fn render_compact_filesystem(
workspace_roots: &[String],
entries: &[FileSystemSandboxEntry],
) -> Option<String> {
if workspace_roots.is_empty() || entries.len() < 2 * (1 + PROTECTED_METADATA_PATH_NAMES.len()) {
return None;
}
let mut read_root = false;
let mut temp_grants = Vec::new();
let mut writes = Vec::new();
let mut reads = HashSet::new();
for entry in entries {
match (&entry.path, entry.access) {
(
FileSystemPath::Special {
value: FileSystemSpecialPath::Root,
},
FileSystemAccessMode::Read,
) => read_root = true,
(FileSystemPath::Special { value }, FileSystemAccessMode::Write)
if matches!(
value,
FileSystemSpecialPath::SlashTmp | FileSystemSpecialPath::Tmpdir
) =>
{
temp_grants.push(render_special_path(value));
}
(FileSystemPath::Path { path }, FileSystemAccessMode::Write) => writes.push(path),
(FileSystemPath::Path { path }, FileSystemAccessMode::Read) => {
if !entry.skips_missing_path() || !reads.insert(path) {
return None;
}
}
(_, FileSystemAccessMode::Deny)
| (FileSystemPath::Special { .. } | FileSystemPath::GlobPattern { .. }, _) => {
return None;
}
}
}
if !read_root
|| writes.len() < 2
|| reads.len() != writes.len() * PROTECTED_METADATA_PATH_NAMES.len()
|| writes.iter().any(|path| reads.contains(path))
{
return None;
}
let mut remaining_roots: HashSet<_> = workspace_roots.iter().collect();
let mut other_writes = Vec::new();
for path in writes {
for name in PROTECTED_METADATA_PATH_NAMES {
let child = path.join_descendant(name).ok()?;
// PathUri equality ignores Windows case. Preserve the spelling the model sees.
if reads.take(&child)?.encoded_path() != child.encoded_path() {
return None;
}
}
let path = path.inferred_native_path_string();
if !remaining_roots.remove(&path) {
other_writes.push(path);
}
}
if !remaining_roots.is_empty() {
return None;
}
let mut rendered = String::from("<filesystem>\nManaged, restricted. Read access to :root.");
for (label, paths) in [
("Writable workspace roots", workspace_roots),
("Other writable paths", other_writes.as_slice()),
] {
if !paths.is_empty() {
// Quote path boundaries and control characters as JSON inside XML text.
let paths = serde_json::to_string(paths)
.ok()?
.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;");
rendered.push_str(&format!("\n{label}: {paths}"));
}
}
let (last, rest) = PROTECTED_METADATA_PATH_NAMES.split_last()?;
rendered.push_str(&format!(
"\nDirectly under each listed path, {} and {last} have read-only entries.",
rest.join(", ")
));
if !temp_grants.is_empty() {
rendered.push_str(&format!(
"\nSeparate write grants: {}",
temp_grants.join(", ")
));
}
rendered.push_str("\n</filesystem>");
Some(rendered)
}
fn render_file_system_entry(rendered: &mut String, entry: &FileSystemSandboxEntry) {
rendered.push_str("<entry access=\"");
let access = entry.access.to_string();
rendered.push_str(&access);
if entry.access == FileSystemAccessMode::Deny {
rendered.push_str("\" escalatable=\"false");
}
rendered.push_str("\">");
match &entry.path {
FileSystemPath::Path { path } => {
push_text_element(rendered, "path", &path.inferred_native_path_string());
}
FileSystemPath::GlobPattern { pattern } => {
push_text_element(rendered, "glob", pattern);
}
FileSystemPath::Special { value } => {
let value = render_special_path(value);
push_text_element(rendered, "special", &value);
}
}
rendered.push_str("</entry>");
}
fn render_special_path(value: &FileSystemSpecialPath) -> String {
match value {
FileSystemSpecialPath::Root => ":root".to_string(),
FileSystemSpecialPath::Minimal => ":minimal".to_string(),
FileSystemSpecialPath::ProjectRoots { subpath } => {
render_special_path_with_subpath(":workspace_roots", subpath)
}
FileSystemSpecialPath::Tmpdir => ":tmpdir".to_string(),
FileSystemSpecialPath::SlashTmp => ":slash_tmp".to_string(),
FileSystemSpecialPath::Unknown { path, subpath } => {
render_special_path_with_subpath(path, subpath)
}
}
}
fn render_special_path_with_subpath(base: &str, subpath: &Option<String>) -> String {
match subpath {
Some(subpath) => format!("{base}/{subpath}"),
None => base.to_string(),
}
}
fn dedupe_file_system_entries(entries: &mut Vec<FileSystemSandboxEntry>) {
let mut seen = HashSet::new();
entries.retain(|entry| seen.insert(entry.clone()));
}
fn push_text_element(rendered: &mut String, name: &str, value: &str) {
rendered.push_str(&format!("<{name}>"));
push_xml_escaped_text(rendered, value);
rendered.push_str(&format!("</{name}>"));
}
pub(crate) fn push_xml_escaped_text(rendered: &mut String, value: &str) {
for ch in value.chars() {
match ch {
'&' => rendered.push_str("&amp;"),
'<' => rendered.push_str("&lt;"),
'>' => rendered.push_str("&gt;"),
'"' => rendered.push_str("&quot;"),
'\'' => rendered.push_str("&apos;"),
_ => rendered.push(ch),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) struct NetworkContext {
allowed_domains: Vec<String>,
denied_domains: Vec<String>,
}
impl NetworkContext {
pub(crate) fn new(allowed_domains: Vec<String>, denied_domains: Vec<String>) -> Self {
Self {
allowed_domains,
denied_domains,
}
}
pub(super) fn render(&self) -> String {
let mut rendered = "<network enabled=\"true\">".to_string();
Self::push_rendered_domain_element(&mut rendered, "allowed", &self.allowed_domains);
Self::push_rendered_domain_element(&mut rendered, "denied", &self.denied_domains);
rendered.push_str("</network>");
rendered
}
fn push_rendered_domain_element(rendered_network: &mut String, name: &str, domains: &[String]) {
if domains.is_empty() {
return;
}
rendered_network.push_str(&format!("<{name}>"));
rendered_network.push_str(&domains.join(","));
rendered_network.push_str(&format!("</{name}>"));
}
}