Skip to content

Respect blocking PostToolUse hooks in code mode#28365

Merged
abhinav-oai merged 5 commits into
mainfrom
audit/hooks-code-mode-20260615
Jun 15, 2026
Merged

Respect blocking PostToolUse hooks in code mode#28365
abhinav-oai merged 5 commits into
mainfrom
audit/hooks-code-mode-20260615

Conversation

@abhinav-oai

@abhinav-oai abhinav-oai commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Summary

Make blocking hook behavior reliable for tools invoked from code mode.

Previously, a PostToolUse hook could block a completed tool result, but code mode would still return the original typed result to JavaScript. The hook appeared blocked in hook telemetry while the running script continued with the result.

This change:

  • rejects the nested JavaScript tool promise when PostToolUse blocks
  • normalizes decision: "block" and exit code 2 to the same blocking behavior
  • surfaces the hook feedback as the rejected promise's error
  • adds end-to-end coverage for the relevant PreToolUse and PostToolUse interactions

Hook semantics in code mode

Hook behavior Code-mode result
PreToolUse block Reject the promise before the tool executes
PreToolUse updatedInput Execute the rewritten invocation and return its result
PostToolUse decision: "block" Execute the tool, then reject the promise with the hook reason
PostToolUse exit code 2 Same behavior as decision: "block"
PostToolUse continue: false Preserve the existing feedback-only behavior; do not reject the promise

Test coverage

Added or strengthened end-to-end coverage proving that:

  • a PreToolUse block rejects the JavaScript promise before execution
  • a PreToolUse input rewrite executes only the rewritten command
  • JavaScript receives the rewritten command's result
  • PostToolUse decision: "block" rejects after the command executes
  • PostToolUse exit code 2 has the same behavior
  • the hook observes the original completed tool response
  • the blocked original result does not reach JavaScript
  • existing direct-mode replacement behavior remains intact
  • continue: false without a reason produces deterministic fallback feedback

@abhinav-oai abhinav-oai changed the title [codex] Honor blocking PostToolUse hooks in code mode Respect blocking PostToolUse hooks in code mode Jun 15, 2026
@abhinav-oai abhinav-oai marked this pull request as ready for review June 15, 2026 18:20
@abhinav-oai abhinav-oai requested a review from a team as a code owner June 15, 2026 18:20
pub stop_reason: Option<String>,
pub should_block: bool,
pub additional_contexts: Vec<String>,
pub feedback_message: Option<String>,

@abhinav-oai abhinav-oai Jun 15, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feedback_message remains the single message channel for both direct and code mode

should_block only determines whether code mode rejects with that message

Comment thread codex-rs/core/src/tools/registry.rs Outdated
let code_mode_post_tool_use_error =
if matches!(&invocation.source, ToolCallSource::CodeMode { .. }) {
post_tool_use_outcome.as_ref().and_then(|outcome| {
outcome.should_block.then(|| {

@abhinav-oai abhinav-oai Jun 15, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continue:false remains feedback-only in code mode just like in direct mode

Only explicit PostToolUse blocking signals (decision:block or exit code 2) set should_block and reject the promise

Comment thread codex-rs/core/tests/suite/hooks.rs Outdated
}

#[derive(Clone, Copy)]
enum CodeModePostToolUseBlockMode {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you delete this?

@abhinav-oai abhinav-oai merged commit d7f298f into main Jun 15, 2026
45 of 47 checks passed
@abhinav-oai abhinav-oai deleted the audit/hooks-code-mode-20260615 branch June 15, 2026 22:12
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants