mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
chore: merge trusted project config gating into hooks trust precedence
Co-authored-by: Codex noreply@openai.com
This commit is contained in:
@@ -81,17 +81,6 @@ impl ClaudeHooksEngine {
|
||||
};
|
||||
}
|
||||
|
||||
if cfg!(windows) {
|
||||
return Self {
|
||||
handlers: Vec::new(),
|
||||
warnings: vec![
|
||||
"Disabled `codex_hooks` for this session because `hooks.json` lifecycle hooks are not supported on Windows yet."
|
||||
.to_string(),
|
||||
],
|
||||
shell,
|
||||
};
|
||||
}
|
||||
|
||||
let _ = schema_loader::generated_hook_schemas();
|
||||
let discovered = discovery::discover_handlers(config_layer_stack);
|
||||
Self {
|
||||
|
||||
@@ -3,6 +3,7 @@ use codex_protocol::protocol::HookEventName;
|
||||
use codex_protocol::protocol::HookOutputEntry;
|
||||
use codex_protocol::protocol::HookOutputEntryKind;
|
||||
use codex_protocol::protocol::HookRunStatus;
|
||||
use codex_protocol::protocol::HookRunSummary;
|
||||
|
||||
use crate::engine::ConfiguredHandler;
|
||||
use crate::engine::dispatcher;
|
||||
@@ -69,6 +70,31 @@ pub(crate) fn serialization_failure_hook_events(
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) fn serialization_failure_hook_events_for_tool_use(
|
||||
handlers: Vec<ConfiguredHandler>,
|
||||
turn_id: Option<String>,
|
||||
error_message: String,
|
||||
tool_use_id: &str,
|
||||
) -> Vec<HookCompletedEvent> {
|
||||
serialization_failure_hook_events(handlers, turn_id, error_message)
|
||||
.into_iter()
|
||||
.map(|event| hook_completed_for_tool_use(event, tool_use_id))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) fn hook_completed_for_tool_use(
|
||||
mut event: HookCompletedEvent,
|
||||
tool_use_id: &str,
|
||||
) -> HookCompletedEvent {
|
||||
event.run = hook_run_for_tool_use(event.run, tool_use_id);
|
||||
event
|
||||
}
|
||||
|
||||
pub(crate) fn hook_run_for_tool_use(mut run: HookRunSummary, tool_use_id: &str) -> HookRunSummary {
|
||||
run.id = format!("{}:{tool_use_id}", run.id);
|
||||
run
|
||||
}
|
||||
|
||||
pub(crate) fn matcher_pattern_for_event(
|
||||
event_name: HookEventName,
|
||||
matcher: Option<&str>,
|
||||
|
||||
@@ -59,7 +59,9 @@ pub(crate) fn preview(
|
||||
Some(&request.tool_name),
|
||||
)
|
||||
.into_iter()
|
||||
.map(|handler| dispatcher::running_summary(&handler))
|
||||
.map(|handler| {
|
||||
common::hook_run_for_tool_use(dispatcher::running_summary(&handler), &request.tool_use_id)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -100,11 +102,13 @@ pub(crate) async fn run(
|
||||
}) {
|
||||
Ok(input_json) => input_json,
|
||||
Err(error) => {
|
||||
return serialization_failure_outcome(common::serialization_failure_hook_events(
|
||||
let hook_events = common::serialization_failure_hook_events_for_tool_use(
|
||||
matched,
|
||||
Some(request.turn_id),
|
||||
Some(request.turn_id.clone()),
|
||||
format!("failed to serialize post tool use hook input: {error}"),
|
||||
));
|
||||
&request.tool_use_id,
|
||||
);
|
||||
return serialization_failure_outcome(hook_events);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -113,7 +117,7 @@ pub(crate) async fn run(
|
||||
matched,
|
||||
input_json,
|
||||
request.cwd.as_path(),
|
||||
Some(request.turn_id),
|
||||
Some(request.turn_id.clone()),
|
||||
parse_completed,
|
||||
)
|
||||
.await;
|
||||
@@ -135,7 +139,12 @@ pub(crate) async fn run(
|
||||
);
|
||||
|
||||
PostToolUseOutcome {
|
||||
hook_events: results.into_iter().map(|result| result.completed).collect(),
|
||||
hook_events: results
|
||||
.into_iter()
|
||||
.map(|result| {
|
||||
common::hook_completed_for_tool_use(result.completed, &request.tool_use_id)
|
||||
})
|
||||
.collect(),
|
||||
should_stop,
|
||||
stop_reason,
|
||||
additional_contexts,
|
||||
@@ -295,16 +304,20 @@ fn serialization_failure_outcome(hook_events: Vec<HookCompletedEvent>) -> PostTo
|
||||
mod tests {
|
||||
use std::path::PathBuf;
|
||||
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::protocol::HookEventName;
|
||||
use codex_protocol::protocol::HookOutputEntry;
|
||||
use codex_protocol::protocol::HookOutputEntryKind;
|
||||
use codex_protocol::protocol::HookRunStatus;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
|
||||
use super::PostToolUseHandlerData;
|
||||
use super::parse_completed;
|
||||
use super::preview;
|
||||
use crate::engine::ConfiguredHandler;
|
||||
use crate::engine::command_runner::CommandRunResult;
|
||||
use crate::events::common;
|
||||
|
||||
#[test]
|
||||
fn block_decision_stops_normal_processing() {
|
||||
@@ -463,6 +476,40 @@ mod tests {
|
||||
assert_eq!(parsed.completed.run.entries, Vec::<HookOutputEntry>::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preview_and_completed_run_ids_include_tool_use_id() {
|
||||
let request = request_for_tool_use("tool-call-456");
|
||||
let runs = preview(&[handler()], &request);
|
||||
|
||||
assert_eq!(runs.len(), 1);
|
||||
assert_eq!(runs[0].id, "post-tool-use:0:/tmp/hooks.json:tool-call-456");
|
||||
|
||||
let parsed = parse_completed(
|
||||
&handler(),
|
||||
run_result(Some(0), "", ""),
|
||||
Some("turn-1".to_string()),
|
||||
);
|
||||
let completed = common::hook_completed_for_tool_use(parsed.completed, &request.tool_use_id);
|
||||
|
||||
assert_eq!(completed.run.id, runs[0].id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialization_failure_run_ids_include_tool_use_id() {
|
||||
let request = request_for_tool_use("tool-call-456");
|
||||
let runs = preview(&[handler()], &request);
|
||||
|
||||
let completed = common::serialization_failure_hook_events_for_tool_use(
|
||||
vec![handler()],
|
||||
Some(request.turn_id.clone()),
|
||||
"serialize failed".into(),
|
||||
&request.tool_use_id,
|
||||
);
|
||||
|
||||
assert_eq!(completed.len(), 1);
|
||||
assert_eq!(completed[0].run.id, runs[0].id);
|
||||
}
|
||||
|
||||
fn handler() -> ConfiguredHandler {
|
||||
ConfiguredHandler {
|
||||
event_name: HookEventName::PostToolUse,
|
||||
@@ -487,4 +534,19 @@ mod tests {
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn request_for_tool_use(tool_use_id: &str) -> super::PostToolUseRequest {
|
||||
super::PostToolUseRequest {
|
||||
session_id: ThreadId::new(),
|
||||
turn_id: "turn-1".to_string(),
|
||||
cwd: PathBuf::from("/tmp"),
|
||||
transcript_path: None,
|
||||
model: "gpt-test".to_string(),
|
||||
permission_mode: "default".to_string(),
|
||||
tool_name: "Bash".to_string(),
|
||||
tool_use_id: tool_use_id.to_string(),
|
||||
command: "echo hello".to_string(),
|
||||
tool_response: json!({"ok": true}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,9 @@ pub(crate) fn preview(
|
||||
Some(&request.tool_name),
|
||||
)
|
||||
.into_iter()
|
||||
.map(|handler| dispatcher::running_summary(&handler))
|
||||
.map(|handler| {
|
||||
common::hook_run_for_tool_use(dispatcher::running_summary(&handler), &request.tool_use_id)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -90,11 +92,13 @@ pub(crate) async fn run(
|
||||
}) {
|
||||
Ok(input_json) => input_json,
|
||||
Err(error) => {
|
||||
return serialization_failure_outcome(common::serialization_failure_hook_events(
|
||||
let hook_events = common::serialization_failure_hook_events_for_tool_use(
|
||||
matched,
|
||||
Some(request.turn_id),
|
||||
Some(request.turn_id.clone()),
|
||||
format!("failed to serialize pre tool use hook input: {error}"),
|
||||
));
|
||||
&request.tool_use_id,
|
||||
);
|
||||
return serialization_failure_outcome(hook_events);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -142,7 +146,12 @@ pub(crate) async fn run(
|
||||
.find_map(|result| result.data.block_reason.clone());
|
||||
|
||||
PreToolUseOutcome {
|
||||
hook_events: results.into_iter().map(|result| result.completed).collect(),
|
||||
hook_events: results
|
||||
.into_iter()
|
||||
.map(|result| {
|
||||
common::hook_completed_for_tool_use(result.completed, &request.tool_use_id)
|
||||
})
|
||||
.collect(),
|
||||
should_block,
|
||||
block_reason,
|
||||
}
|
||||
@@ -270,9 +279,11 @@ mod tests {
|
||||
use super::PreToolUseHandlerData;
|
||||
use super::PreToolUseRequest;
|
||||
use super::parse_completed;
|
||||
use super::preview;
|
||||
use crate::engine::CommandShell;
|
||||
use crate::engine::ConfiguredHandler;
|
||||
use crate::engine::command_runner::CommandRunResult;
|
||||
use crate::events::common;
|
||||
|
||||
#[test]
|
||||
fn permission_decision_deny_blocks_processing() {
|
||||
@@ -564,6 +575,40 @@ mod tests {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preview_and_completed_run_ids_include_tool_use_id() {
|
||||
let request = request_for_tool_use("tool-call-123");
|
||||
let runs = preview(&[handler()], &request);
|
||||
|
||||
assert_eq!(runs.len(), 1);
|
||||
assert_eq!(runs[0].id, "pre-tool-use:0:/tmp/hooks.json:tool-call-123");
|
||||
|
||||
let parsed = parse_completed(
|
||||
&handler(),
|
||||
run_result(Some(0), "", ""),
|
||||
Some("turn-1".to_string()),
|
||||
);
|
||||
let completed = common::hook_completed_for_tool_use(parsed.completed, &request.tool_use_id);
|
||||
|
||||
assert_eq!(completed.run.id, runs[0].id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialization_failure_run_ids_include_tool_use_id() {
|
||||
let request = request_for_tool_use("tool-call-123");
|
||||
let runs = preview(&[handler()], &request);
|
||||
|
||||
let completed = common::serialization_failure_hook_events_for_tool_use(
|
||||
vec![handler()],
|
||||
Some(request.turn_id.clone()),
|
||||
"serialize failed".into(),
|
||||
&request.tool_use_id,
|
||||
);
|
||||
|
||||
assert_eq!(completed.len(), 1);
|
||||
assert_eq!(completed[0].run.id, runs[0].id);
|
||||
}
|
||||
|
||||
fn handler() -> ConfiguredHandler {
|
||||
ConfiguredHandler {
|
||||
event_name: HookEventName::PreToolUse,
|
||||
@@ -588,4 +633,18 @@ mod tests {
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn request_for_tool_use(tool_use_id: &str) -> super::PreToolUseRequest {
|
||||
super::PreToolUseRequest {
|
||||
session_id: ThreadId::new(),
|
||||
turn_id: "turn-1".to_string(),
|
||||
cwd: PathBuf::from("/tmp"),
|
||||
transcript_path: None,
|
||||
model: "gpt-test".to_string(),
|
||||
permission_mode: "default".to_string(),
|
||||
tool_name: "Bash".to_string(),
|
||||
tool_use_id: tool_use_id.to_string(),
|
||||
command: "echo hello".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ use crate::schema::SessionStartCommandInput;
|
||||
pub enum SessionStartSource {
|
||||
Startup,
|
||||
Resume,
|
||||
Clear,
|
||||
}
|
||||
|
||||
impl SessionStartSource {
|
||||
@@ -27,6 +28,7 @@ impl SessionStartSource {
|
||||
match self {
|
||||
Self::Startup => "startup",
|
||||
Self::Resume => "resume",
|
||||
Self::Clear => "clear",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user