-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[hooks] add non-streaming (non-stdin style) shell-only PreToolUse support #15211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3be4afd
[hooks] add bash-only PreToolUse support
eternal-openai bf1c38a
codex: add pre-tool-use turn ids
eternal-openai 78b6865
codex: fix CI failure on PR #15211
eternal-openai 22cf90d
codex: materialize pre-tool-use transcript path
eternal-openai 62f797a
Align pre-tool-use hook parsing
eternal-openai 95dc181
Fix pretooluse PR CI regressions
eternal-openai ce73acc
Surface blocked bash command in hook errors
eternal-openai 69ac9a0
Align hook matchers with Claude spec
eternal-openai 1c0903c
codex: clarify pre-tool-use snapshot names
eternal-openai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1155,6 +1155,7 @@ | |
| }, | ||
| "HookEventName": { | ||
| "enum": [ | ||
| "preToolUse", | ||
| "sessionStart", | ||
| "userPromptSubmit", | ||
| "stop" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7949,6 +7949,7 @@ | |
| }, | ||
| "HookEventName": { | ||
| "enum": [ | ||
| "preToolUse", | ||
| "sessionStart", | ||
| "userPromptSubmit", | ||
| "stop" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4673,6 +4673,7 @@ | |
| }, | ||
| "HookEventName": { | ||
| "enum": [ | ||
| "preToolUse", | ||
| "sessionStart", | ||
| "userPromptSubmit", | ||
| "stop" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ use std::time::Instant; | |
|
|
||
| use crate::client_common::tools::ToolSpec; | ||
| use crate::function_tool::FunctionCallError; | ||
| use crate::hook_runtime::run_pre_tool_use_hooks; | ||
| use crate::memories::usage::emit_metric_for_tool_read; | ||
| use crate::protocol::SandboxPolicy; | ||
| use crate::sandbox_tags::sandbox_tag; | ||
|
|
@@ -20,7 +21,10 @@ use codex_hooks::HookToolInput; | |
| use codex_hooks::HookToolInputLocalShell; | ||
| use codex_hooks::HookToolKind; | ||
| use codex_protocol::models::ResponseInputItem; | ||
| use codex_protocol::models::ShellCommandToolCallParams; | ||
| use codex_protocol::models::ShellToolCallParams; | ||
| use codex_utils_readiness::Readiness; | ||
| use serde::Deserialize; | ||
| use tracing::warn; | ||
|
|
||
| #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
|
|
@@ -243,6 +247,20 @@ impl ToolRegistry { | |
| return Err(FunctionCallError::Fatal(message)); | ||
| } | ||
|
|
||
| if let Some(command) = pre_tool_use_command(tool_name.as_ref(), &invocation.payload) | ||
| && let Some(reason) = run_pre_tool_use_hooks( | ||
| &invocation.session, | ||
| &invocation.turn, | ||
| invocation.call_id.clone(), | ||
| command.clone(), | ||
| ) | ||
| .await | ||
| { | ||
| return Err(FunctionCallError::RespondToModel(format!( | ||
| "Bash command blocked by hook: {reason}. Command: {command}" | ||
| ))); | ||
| } | ||
|
|
||
| let is_mutating = handler.is_mutating(&invocation).await; | ||
| let response_cell = tokio::sync::Mutex::new(None); | ||
| let invocation_for_tool = invocation.clone(); | ||
|
|
@@ -413,6 +431,35 @@ fn sandbox_policy_tag(policy: &SandboxPolicy) -> &'static str { | |
| } | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct PreToolUseExecCommandArgs { | ||
| cmd: String, | ||
| } | ||
|
|
||
| fn pre_tool_use_command(tool_name: &str, payload: &ToolPayload) -> Option<String> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I quite dislike this hardcoding and arg re-parsing here.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about individual tool handlers invoking hooks? |
||
| match (tool_name, payload) { | ||
| ("shell" | "container.exec", ToolPayload::Function { arguments }) => { | ||
| serde_json::from_str::<ShellToolCallParams>(arguments) | ||
| .ok() | ||
| .map(|params| codex_shell_command::parse_command::shlex_join(¶ms.command)) | ||
| } | ||
| ("local_shell", ToolPayload::LocalShell { params }) => Some( | ||
| codex_shell_command::parse_command::shlex_join(¶ms.command), | ||
| ), | ||
| ("shell_command", ToolPayload::Function { arguments }) => { | ||
| serde_json::from_str::<ShellCommandToolCallParams>(arguments) | ||
| .ok() | ||
| .map(|params| params.command) | ||
| } | ||
| ("exec_command", ToolPayload::Function { arguments }) => { | ||
| serde_json::from_str::<PreToolUseExecCommandArgs>(arguments) | ||
| .ok() | ||
| .map(|params| params.cmd) | ||
| } | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| // Hooks use a separate wire-facing input type so hook payload JSON stays stable | ||
| // and decoupled from core's internal tool runtime representation. | ||
| impl From<&ToolPayload> for HookToolInput { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this message too specific (bash)?