fix(core): emit hooks for apply_patch edits#18391
Open
fcoury-oai wants to merge 1 commit intomainfrom
Open
Conversation
Teach apply_patch to provide PreToolUse and PostToolUse payloads for hook dispatch, covering both JSON and freeform patch calls. Pass handler-supplied hook names through runtime serialization so existing Bash matchers keep working and apply_patch can be matched directly.
6a53c79 to
7e25c99
Compare
Contributor
Author
|
@codex review |
Contributor
|
Codex Review: Didn't find any major issues. Keep it up! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
canvrno-oai
approved these changes
Apr 17, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #16732.
Why
apply_patchis Codex's primary file edit path, but it was not emittingPreToolUseorPostToolUsehook events. That meant hook-based policy, auditing, and write coordination could observe shell commands while missing the actual file mutation performed byapply_patch.The issue also exposed that the hook runtime serialized command hook payloads with
tool_name: "Bash"unconditionally. Even ifapply_patchsupplied hook payloads, hooks would either fail to match it directly or receive misleading stdin that identified the edit as a Bash tool call.What Changed
PreToolUseandPostToolUsepayload support toApplyPatchHandler.tool_input.commandfor both JSON/function and freeformapply_patchcalls.tool_name.Bashfor shell-like tools.tool_nameinto hook stdin instead of hardcodingBash.tool_namecan represent tools other thanBash.Verification
Added focused handler coverage for:
apply_patchcalls producing aPreToolUsepayload.apply_patchcalls producing aPreToolUsepayload.apply_patchoutput producing aPostToolUsepayload.exec_commandhandlers continuing to exposeBash.Added end-to-end hook coverage for:
PreToolUsehook matching^apply_patch$blocking the patch before the target file is created.PostToolUsehook matching^apply_patch$receiving the patch input and tool response, then adding context to the follow-up model request.PreToolUse/PostToolUsehook events.Also validated manually with a live
codex execsmoke test using an isolated temp workspace and tempCODEX_HOME. The smoke test confirmed that a realapply_patchedit emitsPreToolUse/PostToolUsewithtool_name: "apply_patch", a shell command still emitstool_name: "Bash", and a denyingPreToolUsehook prevents the blocked patch file from being created.