diff --git a/codex-rs/core/src/hook_runtime.rs b/codex-rs/core/src/hook_runtime.rs index f6319084b2..ed3758cfbd 100644 --- a/codex-rs/core/src/hook_runtime.rs +++ b/codex-rs/core/src/hook_runtime.rs @@ -150,6 +150,9 @@ pub(crate) async fn run_pre_tool_use_hooks( if should_block { block_reason } else { None } } +// PermissionRequest hooks share the same preview/start/completed event flow as +// other hook types, but they return an optional decision instead of mutating +// tool input or post-run state. pub(crate) async fn run_permission_request_hooks( sess: &Arc, turn_context: &Arc, diff --git a/codex-rs/core/src/tools/orchestrator.rs b/codex-rs/core/src/tools/orchestrator.rs index b1273e5248..b7fc708e5b 100644 --- a/codex-rs/core/src/tools/orchestrator.rs +++ b/codex-rs/core/src/tools/orchestrator.rs @@ -399,10 +399,9 @@ impl ToolOrchestrator { } } - // PermissionRequest hooks get the first chance to answer approval prompts - // for tools that expose a hook payload. Today this is Bash-only; if no - // matching hook returns a decision, fall back to the normal user or guardian - // approval path. + // PermissionRequest hooks take top precedence for answering approval + // prompts. If no matching hook returns a decision, fall back to the + // normal guardian or user approval path. async fn request_approval( tool: &mut T, req: &Rq, diff --git a/codex-rs/core/src/tools/runtimes/shell.rs b/codex-rs/core/src/tools/runtimes/shell.rs index 735c83385a..6b94249d31 100644 --- a/codex-rs/core/src/tools/runtimes/shell.rs +++ b/codex-rs/core/src/tools/runtimes/shell.rs @@ -200,12 +200,13 @@ impl Approvable for ShellRuntime { } fn permission_request_payload(&self, req: &ShellRequest) -> Option { - Some(PermissionRequestPayload::bash( - req.hook_command.clone(), - req.sandbox_permissions, - req.additional_permissions.clone(), - req.justification.clone(), - )) + Some(PermissionRequestPayload { + tool_name: "Bash".to_string(), + command: req.hook_command.clone(), + sandbox_permissions: req.sandbox_permissions, + additional_permissions: req.additional_permissions.clone(), + justification: req.justification.clone(), + }) } fn sandbox_mode_for_first_attempt(&self, req: &ShellRequest) -> SandboxOverride { diff --git a/codex-rs/core/src/tools/runtimes/unified_exec.rs b/codex-rs/core/src/tools/runtimes/unified_exec.rs index c5998c6e75..71614cf333 100644 --- a/codex-rs/core/src/tools/runtimes/unified_exec.rs +++ b/codex-rs/core/src/tools/runtimes/unified_exec.rs @@ -183,12 +183,13 @@ impl Approvable for UnifiedExecRuntime<'_> { &self, req: &UnifiedExecRequest, ) -> Option { - Some(PermissionRequestPayload::bash( - req.hook_command.clone(), - req.sandbox_permissions, - req.additional_permissions.clone(), - req.justification.clone(), - )) + Some(PermissionRequestPayload { + tool_name: "Bash".to_string(), + command: req.hook_command.clone(), + sandbox_permissions: req.sandbox_permissions, + additional_permissions: req.additional_permissions.clone(), + justification: req.justification.clone(), + }) } fn sandbox_mode_for_first_attempt(&self, req: &UnifiedExecRequest) -> SandboxOverride { diff --git a/codex-rs/core/src/tools/sandboxing.rs b/codex-rs/core/src/tools/sandboxing.rs index c5d0392219..64dd01a84d 100644 --- a/codex-rs/core/src/tools/sandboxing.rs +++ b/codex-rs/core/src/tools/sandboxing.rs @@ -141,23 +141,6 @@ pub(crate) struct PermissionRequestPayload { pub justification: Option, } -impl PermissionRequestPayload { - pub(crate) fn bash( - command: String, - sandbox_permissions: SandboxPermissions, - additional_permissions: Option, - justification: Option, - ) -> Self { - Self { - tool_name: "Bash".to_string(), - command, - sandbox_permissions, - additional_permissions, - justification, - } - } -} - // Specifies what tool orchestrator should do with a given tool call. #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) enum ExecApprovalRequirement { @@ -300,6 +283,8 @@ pub(crate) trait Approvable { None } + /// Return hook input for approval-time policy hooks when this runtime wants + /// hook evaluation to run before guardian or user approval. fn permission_request_payload(&self, _req: &Req) -> Option { None } diff --git a/codex-rs/hooks/src/events/permission_request.rs b/codex-rs/hooks/src/events/permission_request.rs index 591a57413a..248e6d11a1 100644 --- a/codex-rs/hooks/src/events/permission_request.rs +++ b/codex-rs/hooks/src/events/permission_request.rs @@ -1,3 +1,18 @@ +//! Permission-request hook execution. +//! +//! This event runs in the approval path, before guardian or user approval UI is +//! shown. Unlike `pre_tool_use`, handlers do not rewrite tool input or block by +//! stopping execution outright; instead they can return a concrete allow/deny +//! decision, or decline to decide and let the normal approval flow continue. +//! +//! The event also mirrors the rest of the hook system's lifecycle: +//! +//! 1. Preview matching handlers so the UI can render pending hook rows. +//! 2. Execute every matching handler in precedence order. +//! 3. Parse each handler into transcript-visible output plus an optional +//! decision. +//! 4. Fold the decisions conservatively: any deny wins, otherwise the last +//! allow wins, otherwise there is no hook verdict. use std::path::PathBuf; use codex_protocol::ThreadId; @@ -119,6 +134,8 @@ pub(crate) async fn run( ) .await; + // Preserve the most specific matching allow, but treat any deny as final so + // broader policy layers cannot accidentally overrule a more specific block. let decision = resolve_permission_request_decision( results .iter() @@ -296,7 +313,7 @@ mod tests { #[test] fn permission_request_deny_overrides_earlier_allow() { - let decisions = vec![ + let decisions = [ PermissionRequestDecision::Allow, PermissionRequestDecision::Deny { message: "repo deny".to_string(), @@ -313,7 +330,7 @@ mod tests { #[test] fn permission_request_returns_allow_when_no_handler_denies() { - let decisions = vec![ + let decisions = [ PermissionRequestDecision::Allow, PermissionRequestDecision::Allow, ];