Run patch hooks for intercepted apply_patch commands (#65077)

## Why

Codex can intercept an `apply_patch` heredoc passed to `exec_command`
and apply the patch directly. That path ran shell hooks but bypassed
`apply_patch` hooks, so patch-specific rules could miss the same edit
when it was submitted through the shell tool.

## What changed

- Run `apply_patch` `PreToolUse` after shell hooks and before file
  verification. Hooks can block or rewrite the patch, and the final
  patch is checked against the selected environment's permissions.
- Report the executed patch through `apply_patch` `PostToolUse`. Link
  the patch hook events to the outer shell call with
  `parent_tool_use_id` and provide working-directory and filesystem
  metadata for hooks that inspect patch targets.
- Apply the same block and rewrite rules in recorded Guardian reviews.
  Mark those hook requests with `review_only` and review the resulting
  patch without executing it or publishing hook-provided context.

## Testing

Add coverage for hook blocks and rewrites, effective working directories,
environment permissions, and cancellation before and after a patch write.
Cover recorded reviews of rewritten patches and reviews stopped by a hook.

GitOrigin-RevId: 2115f26261a20d4a512669bc9e277a764b8f42f4
This commit is contained in:
Aismit
2026-12-01 20:18:13 +00:00
committed by copyberry
parent b9cbe0d977
commit 624fdeecee
42 changed files with 2730 additions and 316 deletions
+22 -4
View File
@@ -36,6 +36,7 @@ enum ApplyPatchShell {
#[derive(Debug, PartialEq)]
pub enum MaybeApplyPatch {
Body(ApplyPatchArgs),
ImplicitInvocation,
ShellParseError(ExtractHeredocError),
PatchParseError(ParseError),
NotApplyPatch,
@@ -118,6 +119,10 @@ fn is_implicit_patch(argv: &[String], cwd: &PathUri) -> bool {
// TODO: make private once we remove tests in lib.rs
/// `cwd` supplies the path convention used to interpret the shell executable in `argv`.
pub fn maybe_parse_apply_patch(argv: &[String], cwd: &PathUri) -> MaybeApplyPatch {
if is_implicit_patch(argv, cwd) {
return MaybeApplyPatch::ImplicitInvocation;
}
match argv {
// Direct invocation: apply_patch <patch>
[cmd, body] if APPLY_PATCH_COMMANDS.contains(&cmd.as_str()) => match parse_patch(body) {
@@ -152,12 +157,11 @@ pub async fn maybe_parse_apply_patch_verified(
fs: &dyn ExecutorFileSystem,
sandbox: Option<&codex_exec_server::FileSystemSandboxContext>,
) -> MaybeApplyPatchVerified {
if is_implicit_patch(argv, cwd) {
return MaybeApplyPatchVerified::CorrectnessError(ApplyPatchError::ImplicitInvocation);
}
match maybe_parse_apply_patch(argv, cwd) {
MaybeApplyPatch::Body(args) => verify_apply_patch_args(args, cwd, fs, sandbox).await,
MaybeApplyPatch::ImplicitInvocation => {
MaybeApplyPatchVerified::CorrectnessError(ApplyPatchError::ImplicitInvocation)
}
MaybeApplyPatch::ShellParseError(e) => MaybeApplyPatchVerified::ShellParseError(e),
MaybeApplyPatch::PatchParseError(e) => MaybeApplyPatchVerified::CorrectnessError(e.into()),
MaybeApplyPatch::NotApplyPatch => MaybeApplyPatchVerified::NotApplyPatch,
@@ -508,6 +512,13 @@ mod tests {
let patch = "*** Begin Patch\n*** Add File: foo\n+hi\n*** End Patch".to_string();
let args = vec![patch];
let dir = tempdir().unwrap();
assert_matches!(
maybe_parse_apply_patch(
&args,
&PathUri::from_host_native_path(dir.path()).expect("absolute test path"),
),
MaybeApplyPatch::ImplicitInvocation
);
assert_matches!(
maybe_parse_apply_patch_verified(
&args,
@@ -525,6 +536,13 @@ mod tests {
let script = "*** Begin Patch\n*** Add File: foo\n+hi\n*** End Patch";
let args = args_bash(script);
let dir = tempdir().unwrap();
assert_matches!(
maybe_parse_apply_patch(
&args,
&PathUri::from_host_native_path(dir.path()).expect("absolute test path"),
),
MaybeApplyPatch::ImplicitInvocation
);
assert_matches!(
maybe_parse_apply_patch_verified(
&args,