Files
codex/codex-rs/config/src/hook_config.rs
T
Ryan Taylor a1afa12273 Let required hooks block continuation on failure (#62831)
## Why

A required hook check can fail to run or return a usable result. Continuing
after that failure can let an action proceed without a completed check.

## What changed

Add per-handler `onFailure` for command and MCP tool hooks. The default
`continue` preserves existing behavior. On supported synchronous hooks,
`block` applies the event's blocking behavior and reports the reason while
preserving failure diagnostics. For example, a failed `PreToolUse` check
prevents the tool from running, while a failed `PostToolUse` check withholds
the result of a tool that already ran.

Expose the policy in the app-server's `configRequirements/read` response.

## Testing

Add tests for command and MCP failures under both policies, and scenario
coverage for blocked tool execution, withheld results, and completion after
a `Stop` hook recovers.

GitOrigin-RevId: 20d6ebda0094382fe052bbf34338edb994cc7bd3
2026-11-20 20:24:34 +00:00

79 lines
2.3 KiB
Rust

use std::collections::BTreeMap;
use std::path::Path;
use std::path::PathBuf;
pub use codex_protocol::hooks::HookEventsToml;
pub use codex_protocol::hooks::HookFailurePolicy;
pub use codex_protocol::hooks::HookHandlerConfig;
pub use codex_protocol::hooks::MatcherGroup;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct HooksFile {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default)]
pub hooks: HookEventsToml,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct HooksToml {
#[serde(flatten)]
pub events: HookEventsToml,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub state: BTreeMap<String, HookStateToml>,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct HookStateToml {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub trusted_hash: Option<String>,
/// Approved plugin contents; kept separate for declaration-only clients.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub trusted_content_hash: Option<String>,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ManagedHooksRequirementsToml {
pub managed_dir: Option<PathBuf>,
pub windows_managed_dir: Option<PathBuf>,
#[serde(flatten)]
pub hooks: Box<HookEventsToml>,
}
impl ManagedHooksRequirementsToml {
pub fn is_empty(&self) -> bool {
let Self {
managed_dir,
windows_managed_dir,
hooks,
} = self;
managed_dir.is_none() && windows_managed_dir.is_none() && hooks.is_empty()
}
pub fn handler_count(&self) -> usize {
self.hooks.handler_count()
}
pub fn managed_dir_for_current_platform(&self) -> Option<&Path> {
#[cfg(windows)]
{
self.windows_managed_dir.as_deref()
}
#[cfg(not(windows))]
{
self.managed_dir.as_deref()
}
}
}
#[cfg(test)]
#[path = "hooks_tests.rs"]
mod tests;