Fix permission request hook precedence and retry context

This commit is contained in:
Abhinav Vedmala
2026-04-12 13:17:13 -07:00
parent 99458d2929
commit f6517fa6a2
6 changed files with 236 additions and 6 deletions
+7
View File
@@ -11,6 +11,7 @@ use codex_hooks::PreToolUseRequest;
use codex_hooks::SessionStartOutcome;
use codex_hooks::UserPromptSubmitOutcome;
use codex_hooks::UserPromptSubmitRequest;
use codex_protocol::approvals::NetworkApprovalContext;
use codex_protocol::items::TurnItem;
use codex_protocol::models::DeveloperInstructions;
use codex_protocol::models::ResponseInputItem;
@@ -154,6 +155,9 @@ pub(crate) async fn run_permission_request_hooks(
turn_context: &Arc<TurnContext>,
run_id_suffix: String,
payload: PermissionRequestPayload,
approval_attempt: String,
retry_reason: Option<String>,
network_approval_context: Option<NetworkApprovalContext>,
) -> Option<PermissionRequestDecision> {
let request = PermissionRequestRequest {
session_id: sess.conversation_id,
@@ -168,6 +172,9 @@ pub(crate) async fn run_permission_request_hooks(
sandbox_permissions: payload.sandbox_permissions,
additional_permissions: payload.additional_permissions,
justification: payload.justification,
approval_attempt,
retry_reason,
network_approval_context,
};
let preview_runs = sess.hooks().preview_permission_request(&request);
emit_hook_started_events(sess, turn_context, preview_runs).await;
+12 -3
View File
@@ -415,15 +415,24 @@ impl ToolOrchestrator {
T: ToolRuntime<Rq, Out>,
{
if let Some(permission_request) = tool.permission_request_payload(req) {
let run_id_suffix = match approval_attempt {
ApprovalAttempt::Initial => format!("{}:initial", approval_ctx.call_id),
ApprovalAttempt::Retry => format!("{}:retry", approval_ctx.call_id),
let (approval_attempt_label, run_id_suffix) = match approval_attempt {
ApprovalAttempt::Initial => (
"initial".to_string(),
format!("{}:initial", approval_ctx.call_id),
),
ApprovalAttempt::Retry => (
"retry".to_string(),
format!("{}:retry", approval_ctx.call_id),
),
};
match run_permission_request_hooks(
approval_ctx.session,
approval_ctx.turn,
run_id_suffix,
permission_request,
approval_attempt_label,
approval_ctx.retry_reason.clone(),
approval_ctx.network_approval_context.clone(),
)
.await
{
+89
View File
@@ -1165,6 +1165,9 @@ async fn permission_request_hook_allows_shell_command_without_user_approval() ->
"sandbox_permissions": "use_default",
"additional_permissions": null,
"justification": null,
"approval_attempt": "initial",
"retry_reason": null,
"network_approval_context": null,
})
);
assert!(
@@ -1180,6 +1183,92 @@ async fn permission_request_hook_allows_shell_command_without_user_approval() ->
Ok(())
}
#[cfg(not(target_os = "linux"))]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn permission_request_hook_sees_retry_context_after_sandbox_denial() -> Result<()> {
skip_if_no_network!(Ok(()));
let server = start_mock_server().await;
let call_id = "permissionrequest-retry-shell-command";
let marker = "permissionrequest_retry_marker.txt";
let command = format!("printf retry > {marker}");
let args = serde_json::json!({ "command": command });
let responses = mount_sse_sequence(
&server,
vec![
sse(vec![
ev_response_created("resp-1"),
core_test_support::responses::ev_function_call(
call_id,
"shell_command",
&serde_json::to_string(&args)?,
),
ev_completed("resp-1"),
]),
sse(vec![
ev_response_created("resp-2"),
ev_assistant_message("msg-1", "permission request hook allowed retry"),
ev_completed("resp-2"),
]),
],
)
.await;
let mut builder = test_codex()
.with_pre_build_hook(|home| {
if let Err(error) = write_permission_request_hook(
home,
Some("^Bash$"),
"allow",
"should not be used for allow",
) {
panic!("failed to write permission request hook test fixture: {error}");
}
})
.with_config(|config| {
config
.features
.enable(Feature::CodexHooks)
.expect("test config should allow feature update");
});
let test = builder.build(&server).await?;
let marker_path = test.workspace_path(marker);
let _ = fs::remove_file(&marker_path);
test.submit_turn_with_policies(
"retry the shell command after sandbox denial",
AskForApproval::OnFailure,
codex_protocol::protocol::SandboxPolicy::new_read_only_policy(),
)
.await?;
let requests = responses.requests();
assert_eq!(requests.len(), 2);
requests[1].function_call_output(call_id);
assert_eq!(
fs::read_to_string(&marker_path).context("read retry marker")?,
"retry"
);
let hook_inputs = read_permission_request_hook_inputs(test.codex_home_path())?;
assert_eq!(hook_inputs.len(), 1);
assert_eq!(hook_inputs[0]["hook_event_name"], "PermissionRequest");
assert_eq!(hook_inputs[0]["tool_input"]["command"], command);
assert_eq!(
hook_inputs[0]["approval_context"],
serde_json::json!({
"sandbox_permissions": "use_default",
"additional_permissions": null,
"justification": null,
"approval_attempt": "retry",
"retry_reason": "command failed; retry without sandbox?",
"network_approval_context": null,
})
);
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn pre_tool_use_blocks_shell_command_before_execution() -> Result<()> {
skip_if_no_network!(Ok(()));