diff --git a/codex-rs/protocol/src/models.rs b/codex-rs/protocol/src/models.rs index 46aecb61b9..905947cf24 100644 --- a/codex-rs/protocol/src/models.rs +++ b/codex-rs/protocol/src/models.rs @@ -143,6 +143,30 @@ impl FileSystemPermissions { }) } + /// Maps explicit filesystem paths while preserving glob and special-path entries. + pub fn map_paths( + self, + mut map: impl FnMut(PathType) -> OutputPath, + ) -> FileSystemPermissions { + FileSystemPermissions { + entries: self + .entries + .into_iter() + .map(|entry| FileSystemSandboxEntry { + path: match entry.path { + FileSystemPath::Path { path } => FileSystemPath::Path { path: map(path) }, + FileSystemPath::GlobPattern { pattern } => { + FileSystemPath::GlobPattern { pattern } + } + FileSystemPath::Special { value } => FileSystemPath::Special { value }, + }, + access: entry.access, + }) + .collect(), + glob_scan_max_depth: self.glob_scan_max_depth, + } + } + pub fn legacy_read_write_roots(&self) -> Option> where PathType: Clone, @@ -263,16 +287,61 @@ impl NetworkPermissions { /// Partial permission overlay used for per-command requests and approved /// session/turn grants. -#[derive(Debug, Clone, Default, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, TS)] -pub struct AdditionalPermissionProfile { +#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, TS)] +#[serde(bound( + serialize = "PathType: Clone + Serialize", + deserialize = "PathType: Deserialize<'de>" +))] +pub struct AdditionalPermissionProfile { pub network: Option, - pub file_system: Option, + pub file_system: Option>, } -impl AdditionalPermissionProfile { +impl AdditionalPermissionProfile { pub fn is_empty(&self) -> bool { self.network.is_none() && self.file_system.is_none() } + + /// Maps explicit filesystem paths while preserving the rest of the overlay. + pub fn map_paths( + self, + map: impl FnMut(PathType) -> OutputPath, + ) -> AdditionalPermissionProfile { + AdditionalPermissionProfile { + network: self.network, + file_system: self + .file_system + .map(|file_system| file_system.map_paths(map)), + } + } +} + +impl Default for AdditionalPermissionProfile { + fn default() -> Self { + Self { + network: None, + file_system: None, + } + } +} + +impl From> for AdditionalPermissionProfile { + fn from(value: AdditionalPermissionProfile) -> Self { + value.map_paths(|path| PathUri::from_abs_path(&path)) + } +} + +impl TryFrom> + for AdditionalPermissionProfile +{ + type Error = io::Error; + + fn try_from(value: AdditionalPermissionProfile) -> Result { + Ok(Self { + network: value.network, + file_system: value.file_system.map(TryInto::try_into).transpose()?, + }) + } } #[derive( diff --git a/codex-rs/protocol/src/request_permissions.rs b/codex-rs/protocol/src/request_permissions.rs index 1752363ec5..bf80d6d647 100644 --- a/codex-rs/protocol/src/request_permissions.rs +++ b/codex-rs/protocol/src/request_permissions.rs @@ -15,21 +15,47 @@ pub enum PermissionGrantScope { Session, } -#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS)] +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS)] #[serde(deny_unknown_fields)] -pub struct RequestPermissionProfile { +#[serde(bound( + serialize = "PathType: Clone + Serialize", + deserialize = "PathType: Deserialize<'de>" +))] +pub struct RequestPermissionProfile { pub network: Option, - pub file_system: Option, + pub file_system: Option>, } -impl RequestPermissionProfile { +impl RequestPermissionProfile { pub fn is_empty(&self) -> bool { self.network.is_none() && self.file_system.is_none() } + + /// Maps explicit filesystem paths while preserving the rest of the request. + pub fn map_paths( + self, + map: impl FnMut(PathType) -> OutputPath, + ) -> RequestPermissionProfile { + RequestPermissionProfile { + network: self.network, + file_system: self + .file_system + .map(|file_system| file_system.map_paths(map)), + } + } } -impl From for AdditionalPermissionProfile { - fn from(value: RequestPermissionProfile) -> Self { +impl Default for RequestPermissionProfile { + fn default() -> Self { + Self { + network: None, + file_system: None, + } + } +} + +impl From> for AdditionalPermissionProfile { + fn from(value: RequestPermissionProfile) -> Self { Self { network: value.network, file_system: value.file_system, @@ -37,8 +63,8 @@ impl From for AdditionalPermissionProfile { } } -impl From for RequestPermissionProfile { - fn from(value: AdditionalPermissionProfile) -> Self { +impl From> for RequestPermissionProfile { + fn from(value: AdditionalPermissionProfile) -> Self { Self { network: value.network, file_system: value.file_system, @@ -47,7 +73,11 @@ impl From for RequestPermissionProfile { } #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS)] -pub struct RequestPermissionsArgs { +#[serde(bound( + serialize = "PathType: Clone + Serialize", + deserialize = "PathType: Deserialize<'de>" +))] +pub struct RequestPermissionsArgs { #[serde( default, rename = "environment_id", @@ -58,12 +88,16 @@ pub struct RequestPermissionsArgs { pub environment_id: Option, #[serde(skip_serializing_if = "Option::is_none")] pub reason: Option, - pub permissions: RequestPermissionProfile, + pub permissions: RequestPermissionProfile, } #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS)] -pub struct RequestPermissionsResponse { - pub permissions: RequestPermissionProfile, +#[serde(bound( + serialize = "PathType: Clone + Serialize", + deserialize = "PathType: Deserialize<'de>" +))] +pub struct RequestPermissionsResponse { + pub permissions: RequestPermissionProfile, #[serde(default)] pub scope: PermissionGrantScope, /// Review every subsequent command in this turn before normal sandboxed execution. @@ -71,6 +105,19 @@ pub struct RequestPermissionsResponse { pub strict_auto_review: bool, } +impl RequestPermissionsResponse { + pub fn map_paths( + self, + map: impl FnMut(PathType) -> OutputPath, + ) -> RequestPermissionsResponse { + RequestPermissionsResponse { + permissions: self.permissions.map_paths(map), + scope: self.scope, + strict_auto_review: self.strict_auto_review, + } + } +} + #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS)] pub struct RequestPermissionsEvent { /// Responses API call id for the associated tool call, if available.