apply-patch: carry paths as PathUri (#28854)

## Why

Allows the model to edit files that are hosted on a different OS than
where app-server is running.

## What

* Use `PathUri` for apply_patch-internal data structures
* Limit `PathUri` -> `AbsolutePathBuf` conversion to cases where the
inferred path convention matches the host OS, allows requiring valid
paths to pass to perms check
* Adds `PathConvention::path_segments()` for iterating over path
segments regardless of OS
* Handle cross-platform relative paths in path filename parsing for
sniffing a shell
* Ensure we can apply patches in the wine e2e test
This commit is contained in:
Adam Perry @ OpenAI authored and GitHub committed 2026-06-18 19:31:19 +00:00
1 parent a52a3b5197
commit 0f89dd768c
20 files changed
+626 -370

No files matched your search

@@ -32,7 +32,6 @@ use codex_protocol::protocol::ReviewDecision;
use codex_sandboxing::SandboxType;
use codex_sandboxing::SandboxablePreference;
use codex_sandboxing::policy_transforms::effective_permission_profile;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_path_uri::PathUri;
use futures::future::BoxFuture;
use std::path::PathBuf;
@@ -41,14 +40,14 @@ use std::time::Instant;
#[derive(Clone, Debug, Eq, PartialEq, Hash, serde::Serialize)]
pub(crate) struct ApplyPatchApprovalKey {
environment_id: String,
path: AbsolutePathBuf,
path: PathUri,
}
#[derive(Debug)]
pub struct ApplyPatchRequest {
pub turn_environment: TurnEnvironment,
pub action: ApplyPatchAction,
pub file_paths: Vec<AbsolutePathBuf>,
pub file_paths: Vec<PathUri>,
pub changes: std::collections::HashMap<PathBuf, FileChange>,
pub exec_approval_requirement: ExecApprovalRequirement,
pub additional_permissions: Option<AdditionalPermissionProfile>,
@@ -81,10 +80,15 @@ impl ApplyPatchRuntime {
) -> std::io::Result<GuardianApprovalRequest> {
// TODO(anp): Remove this conversion once the guardian API supports PathUri.
let cwd = req.action.cwd.to_abs_path()?;
let files = req
.file_paths
.iter()
.map(PathUri::to_abs_path)
.collect::<std::io::Result<Vec<_>>>()?;
Ok(GuardianApprovalRequest::ApplyPatch {
id: call_id.to_string(),
cwd,
files: req.file_paths.clone(),
files,
patch: req.action.patch.clone(),
})
}
@@ -245,15 +249,9 @@ impl ToolRuntime<ApplyPatchRequest, ApplyPatchRuntimeOutput> for ApplyPatchRunti
let sandbox = Self::file_system_sandbox_context_for_attempt(req, attempt);
let mut stdout = Vec::new();
let mut stderr = Vec::new();
// TODO(anp): Teach apply_patch to operate on PathUri directly.
let cwd = req
.action
.cwd
.to_abs_path()
.map_err(|err| ToolError::Rejected(err.to_string()))?;
let result = codex_apply_patch::apply_patch(
&req.action.patch,
&cwd,
&req.action.cwd,
&mut stdout,
&mut stderr,
fs.as_ref(),
@@ -53,13 +53,14 @@ async fn guardian_review_request_includes_patch_context() {
let path = std::env::temp_dir()
.join("guardian-apply-patch-test.txt")
.abs();
let action = ApplyPatchAction::new_add_for_test(&path, "hello".to_string());
let action =
ApplyPatchAction::new_add_for_test(&PathUri::from_abs_path(&path), "hello".to_string());
let expected_cwd = action.cwd.to_abs_path().expect("native patch cwd");
let expected_patch = action.patch.clone();
let request = ApplyPatchRequest {
turn_environment: test_turn_environment(codex_exec_server::LOCAL_ENVIRONMENT_ID),
action,
file_paths: vec![path.clone()],
file_paths: vec![PathUri::from_abs_path(&path)],
changes: HashMap::from([(
path.to_path_buf(),
FileChange::Add {
@@ -82,7 +83,7 @@ async fn guardian_review_request_includes_patch_context() {
GuardianApprovalRequest::ApplyPatch {
id: "call-1".to_string(),
cwd: expected_cwd,
files: request.file_paths,
files: vec![path],
patch: expected_patch,
}
);
@@ -94,12 +95,13 @@ async fn permission_request_payload_uses_apply_patch_hook_name_and_aliases() {
let path = std::env::temp_dir()
.join("apply-patch-permission-request-payload.txt")
.abs();
let action = ApplyPatchAction::new_add_for_test(&path, "hello".to_string());
let action =
ApplyPatchAction::new_add_for_test(&PathUri::from_abs_path(&path), "hello".to_string());
let expected_patch = action.patch.clone();
let req = ApplyPatchRequest {
turn_environment: test_turn_environment(codex_exec_server::LOCAL_ENVIRONMENT_ID),
action,
file_paths: vec![path],
file_paths: vec![PathUri::from_abs_path(&path)],
changes: HashMap::new(),
exec_approval_requirement: ExecApprovalRequirement::NeedsApproval {
reason: None,
@@ -130,10 +132,11 @@ async fn approval_keys_include_environment_id() {
let path = std::env::temp_dir()
.join("apply-patch-approval-key.txt")
.abs();
let path_uri = PathUri::from_abs_path(&path);
let req = ApplyPatchRequest {
turn_environment: test_turn_environment("remote"),
action: ApplyPatchAction::new_add_for_test(&path, "hello".to_string()),
file_paths: vec![path.clone()],
action: ApplyPatchAction::new_add_for_test(&path_uri, "hello".to_string()),
file_paths: vec![path_uri.clone()],
changes: HashMap::new(),
exec_approval_requirement: ExecApprovalRequirement::Skip {
bypass_sandbox: false,
@@ -150,7 +153,7 @@ async fn approval_keys_include_environment_id() {
serde_json::json!([
{
"environment_id": "remote",
"path": path,
"path": path_uri,
}
])
);
@@ -164,8 +167,11 @@ async fn sandbox_cwd_uses_patch_action_cwd() {
.abs();
let req = ApplyPatchRequest {
turn_environment: test_turn_environment(codex_exec_server::LOCAL_ENVIRONMENT_ID),
action: ApplyPatchAction::new_add_for_test(&path, "hello".to_string()),
file_paths: vec![path.clone()],
action: ApplyPatchAction::new_add_for_test(
&PathUri::from_abs_path(&path),
"hello".to_string(),
),
file_paths: vec![PathUri::from_abs_path(&path)],
changes: HashMap::new(),
exec_approval_requirement: ExecApprovalRequirement::Skip {
bypass_sandbox: false,
@@ -192,8 +198,11 @@ async fn file_system_sandbox_context_uses_active_attempt() {
};
let req = ApplyPatchRequest {
turn_environment: test_turn_environment(codex_exec_server::LOCAL_ENVIRONMENT_ID),
action: ApplyPatchAction::new_add_for_test(&path, "hello".to_string()),
file_paths: vec![path.clone()],
action: ApplyPatchAction::new_add_for_test(
&PathUri::from_abs_path(&path),
"hello".to_string(),
),
file_paths: vec![PathUri::from_abs_path(&path)],
changes: HashMap::new(),
exec_approval_requirement: ExecApprovalRequirement::Skip {
bypass_sandbox: false,
@@ -259,8 +268,11 @@ async fn no_sandbox_attempt_has_no_file_system_context() {
.abs();
let req = ApplyPatchRequest {
turn_environment: test_turn_environment(codex_exec_server::LOCAL_ENVIRONMENT_ID),
action: ApplyPatchAction::new_add_for_test(&path, "hello".to_string()),
file_paths: vec![path.clone()],
action: ApplyPatchAction::new_add_for_test(
&PathUri::from_abs_path(&path),
"hello".to_string(),
),
file_paths: vec![PathUri::from_abs_path(&path)],
changes: HashMap::new(),
exec_approval_requirement: ExecApprovalRequirement::Skip {
bypass_sandbox: false,