Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions codex-rs/hooks/src/events/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>,
Expand Down
74 changes: 68 additions & 6 deletions codex-rs/hooks/src/events/post_tool_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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);
}
};

Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand All @@ -486,4 +533,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}),
}
}
}
72 changes: 66 additions & 6 deletions codex-rs/hooks/src/events/pre_tool_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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);
}
};

Expand All @@ -103,7 +107,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;
Expand All @@ -114,7 +118,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,
}
Expand Down Expand Up @@ -232,6 +241,7 @@ fn serialization_failure_outcome(hook_events: Vec<HookCompletedEvent>) -> PreToo
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;
Expand All @@ -240,8 +250,10 @@ mod tests {

use super::PreToolUseHandlerData;
use super::parse_completed;
use super::preview;
use crate::engine::ConfiguredHandler;
use crate::engine::command_runner::CommandRunResult;
use crate::events::common;

#[test]
fn permission_decision_deny_blocks_processing() {
Expand Down Expand Up @@ -453,6 +465,40 @@ mod tests {
);
}

#[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,
Expand All @@ -476,4 +522,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(),
}
}
}
Loading
Loading