mirror of
https://github.com/openai/codex.git
synced 2026-09-20 12:47:38 +00:00
## Why Patch targets can use a different path convention from the Codex host, so host-native path conversion can misclassify writable roots and requested permissions. ## What changed - Evaluate patch targets as `PathUri` values with the active filesystem policy context, including workspace roots and the executor's path convention. - Distinguish executor-managed sandboxing from local platform sandboxing when deciding whether a patch can be auto-approved and how to normalize additional write permissions. - Make full-disk and special-path policy checks honor the selected executor's Windows or POSIX convention. ## Testing Add coverage for Windows executor URIs, full-disk policy aliases, remote patch permission requests, sandbox availability, and owner-provided workspace roots. GitOrigin-RevId: 1a054ea443efd342623c67432762f85c53d20c15
98 lines
3.5 KiB
Rust
98 lines
3.5 KiB
Rust
use crate::function_tool::FunctionCallError;
|
|
use crate::safety::PatchSandboxRoute;
|
|
use crate::safety::SafetyCheck;
|
|
use crate::safety::assess_patch_safety;
|
|
use crate::session::step_context::StepContext;
|
|
use crate::session::turn_context::TurnEnvironment;
|
|
use crate::tools::sandboxing::ExecApprovalRequirement;
|
|
use codex_apply_patch::ApplyPatchAction;
|
|
use codex_apply_patch::ApplyPatchFileChange;
|
|
use codex_protocol::permissions::FileSystemSandboxPolicyContext;
|
|
use codex_protocol::protocol::FileChange;
|
|
use codex_protocol::protocol::FileSystemSandboxPolicy;
|
|
use codex_utils_path_uri::PathUri;
|
|
use std::collections::HashMap;
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Debug)]
|
|
pub(crate) struct ApplyPatchRuntimeInvocation {
|
|
pub(crate) action: ApplyPatchAction,
|
|
pub(crate) auto_approved: bool,
|
|
pub(crate) exec_approval_requirement: ExecApprovalRequirement,
|
|
}
|
|
|
|
pub(crate) fn prepare_apply_patch(
|
|
step_context: &StepContext,
|
|
turn_environment: &TurnEnvironment,
|
|
file_system_sandbox_policy: &FileSystemSandboxPolicy,
|
|
context: &FileSystemSandboxPolicyContext<'_>,
|
|
sandbox_route: PatchSandboxRoute,
|
|
action: ApplyPatchAction,
|
|
) -> Result<ApplyPatchRuntimeInvocation, FunctionCallError> {
|
|
match assess_patch_safety(
|
|
&action,
|
|
step_context.settings.approval_policy(),
|
|
turn_environment.permission_profile(),
|
|
file_system_sandbox_policy,
|
|
context,
|
|
sandbox_route,
|
|
) {
|
|
SafetyCheck::AutoApprove => Ok(ApplyPatchRuntimeInvocation {
|
|
action,
|
|
auto_approved: true,
|
|
exec_approval_requirement: ExecApprovalRequirement::Skip {
|
|
bypass_sandbox: false,
|
|
proposed_execpolicy_amendment: None,
|
|
},
|
|
}),
|
|
SafetyCheck::AskUser => {
|
|
// Delegate the approval prompt (including cached approvals) to the
|
|
// tool runtime, consistent with how shell/unified_exec approvals
|
|
// are orchestrator-driven.
|
|
Ok(ApplyPatchRuntimeInvocation {
|
|
action,
|
|
auto_approved: false,
|
|
exec_approval_requirement: ExecApprovalRequirement::NeedsApproval {
|
|
reason: None,
|
|
proposed_execpolicy_amendment: None,
|
|
},
|
|
})
|
|
}
|
|
SafetyCheck::Reject { reason } => Err(FunctionCallError::RespondToModel(format!(
|
|
"patch rejected: {reason}"
|
|
))),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn convert_apply_patch_to_protocol(
|
|
action: &ApplyPatchAction,
|
|
) -> HashMap<PathBuf, FileChange> {
|
|
let mut result = HashMap::with_capacity(action.changes().len());
|
|
for (path, change) in action.changes() {
|
|
let protocol_change = match change {
|
|
ApplyPatchFileChange::Add { content, .. } => FileChange::Add {
|
|
content: content.clone(),
|
|
},
|
|
ApplyPatchFileChange::Delete { content } => FileChange::Delete {
|
|
content: content.clone(),
|
|
},
|
|
ApplyPatchFileChange::Update {
|
|
unified_diff,
|
|
move_path,
|
|
new_content: _new_content,
|
|
} => FileChange::Update {
|
|
unified_diff: unified_diff.clone(),
|
|
move_path: move_path.as_ref().map(PathUri::to_path_buf),
|
|
},
|
|
};
|
|
// TODO(anp): Carry PathUri through patch protocol events once app-server and rollout
|
|
// compatibility no longer require path-flavored strings.
|
|
result.insert(path.to_path_buf(), protocol_change);
|
|
}
|
|
result
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "apply_patch_tests.rs"]
|
|
mod tests;
|