mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Add hooks for interrupted turns (#40511)
## What changed - Add an `Interrupt` hook event that runs for an active top-level turn before its interrupted abort event is emitted. - Flush the turn transcript before invoking the hook and provide the session, turn, transcript, working directory, model, and permission mode in its input. - Support command and MCP handlers, including asynchronous commands, with a one-second default timeout and a three-second maximum. - Expose the event through hook configuration, managed requirements, app-server notifications, generated schemas, analytics, and the TUI hook views. ## Testing - Cover handler discovery, timeout normalization, output parsing, protocol compatibility, TUI rendering, and interrupt execution ordering. GitOrigin-RevId: 163fa7c098d94ac2775f6d137f8e916f8ea9b6eb
This commit is contained in:
1 parent
9b2ef38f54
commit
cbfd999db7
49 files changed
+1393
-48
No files matched your search
@@ -36,6 +36,8 @@ const SUBAGENT_STOP_INPUT_FIXTURE: &str = "subagent-stop.command.input.schema.js
|
||||
const SUBAGENT_STOP_OUTPUT_FIXTURE: &str = "subagent-stop.command.output.schema.json";
|
||||
const STOP_INPUT_FIXTURE: &str = "stop.command.input.schema.json";
|
||||
const STOP_OUTPUT_FIXTURE: &str = "stop.command.output.schema.json";
|
||||
const INTERRUPT_INPUT_FIXTURE: &str = "interrupt.command.input.schema.json";
|
||||
const INTERRUPT_OUTPUT_FIXTURE: &str = "interrupt.command.output.schema.json";
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(transparent)]
|
||||
@@ -118,6 +120,8 @@ pub(crate) enum HookEventNameWire {
|
||||
SubagentStop,
|
||||
#[serde(rename = "Stop")]
|
||||
Stop,
|
||||
#[serde(rename = "Interrupt")]
|
||||
Interrupt,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
|
||||
@@ -474,6 +478,15 @@ pub(crate) struct SubagentStopCommandOutputWire {
|
||||
pub reason: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[serde(deny_unknown_fields)]
|
||||
#[schemars(rename = "interrupt.command.output")]
|
||||
pub(crate) struct InterruptCommandOutputWire {
|
||||
#[serde(default)]
|
||||
pub system_message: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
|
||||
pub(crate) enum BlockDecisionWire {
|
||||
#[serde(rename = "block")]
|
||||
@@ -608,6 +621,22 @@ pub(crate) struct SubagentStopCommandInput {
|
||||
pub last_assistant_message: NullableString,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, JsonSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
#[schemars(rename = "interrupt.command.input")]
|
||||
pub(crate) struct InterruptCommandInput {
|
||||
pub session_id: String,
|
||||
/// Codex extension: expose the active turn id to internal turn-scoped hooks.
|
||||
pub turn_id: String,
|
||||
pub transcript_path: NullableString,
|
||||
pub cwd: String,
|
||||
#[schemars(schema_with = "interrupt_hook_event_name_schema")]
|
||||
pub hook_event_name: String,
|
||||
pub model: String,
|
||||
#[schemars(schema_with = "permission_mode_schema")]
|
||||
pub permission_mode: String,
|
||||
}
|
||||
|
||||
pub fn write_schema_fixtures(schema_root: &Path) -> anyhow::Result<()> {
|
||||
let generated_dir = schema_root.join(GENERATED_DIR);
|
||||
ensure_empty_dir(&generated_dir)?;
|
||||
@@ -696,6 +725,14 @@ pub fn write_schema_fixtures(schema_root: &Path) -> anyhow::Result<()> {
|
||||
&generated_dir.join(STOP_OUTPUT_FIXTURE),
|
||||
schema_json::<StopCommandOutputWire>()?,
|
||||
)?;
|
||||
write_schema(
|
||||
&generated_dir.join(INTERRUPT_INPUT_FIXTURE),
|
||||
schema_json::<InterruptCommandInput>()?,
|
||||
)?;
|
||||
write_schema(
|
||||
&generated_dir.join(INTERRUPT_OUTPUT_FIXTURE),
|
||||
schema_json::<InterruptCommandOutputWire>()?,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -799,6 +836,10 @@ fn stop_hook_event_name_schema(_gen: &mut SchemaGenerator) -> Schema {
|
||||
string_const_schema("Stop")
|
||||
}
|
||||
|
||||
fn interrupt_hook_event_name_schema(_gen: &mut SchemaGenerator) -> Schema {
|
||||
string_const_schema("Interrupt")
|
||||
}
|
||||
|
||||
fn permission_mode_schema(_gen: &mut SchemaGenerator) -> Schema {
|
||||
string_enum_schema(&[
|
||||
"default",
|
||||
@@ -846,6 +887,9 @@ fn default_continue() -> bool {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::INTERRUPT_INPUT_FIXTURE;
|
||||
use super::INTERRUPT_OUTPUT_FIXTURE;
|
||||
use super::InterruptCommandInput;
|
||||
use super::NullableString;
|
||||
use super::PERMISSION_REQUEST_INPUT_FIXTURE;
|
||||
use super::PERMISSION_REQUEST_OUTPUT_FIXTURE;
|
||||
@@ -958,6 +1002,12 @@ mod tests {
|
||||
STOP_OUTPUT_FIXTURE => {
|
||||
include_str!("../schema/generated/stop.command.output.schema.json")
|
||||
}
|
||||
INTERRUPT_INPUT_FIXTURE => {
|
||||
include_str!("../schema/generated/interrupt.command.input.schema.json")
|
||||
}
|
||||
INTERRUPT_OUTPUT_FIXTURE => {
|
||||
include_str!("../schema/generated/interrupt.command.output.schema.json")
|
||||
}
|
||||
_ => panic!("unexpected fixture name: {name}"),
|
||||
}
|
||||
}
|
||||
@@ -1008,6 +1058,8 @@ mod tests {
|
||||
SUBAGENT_STOP_OUTPUT_FIXTURE,
|
||||
STOP_INPUT_FIXTURE,
|
||||
STOP_OUTPUT_FIXTURE,
|
||||
INTERRUPT_INPUT_FIXTURE,
|
||||
INTERRUPT_OUTPUT_FIXTURE,
|
||||
] {
|
||||
let expected = normalize_newlines(expected_fixture(fixture));
|
||||
let actual = std::fs::read_to_string(schema_root.join("generated").join(fixture))
|
||||
@@ -1090,6 +1142,10 @@ mod tests {
|
||||
&schema_json::<StopCommandInput>().expect("serialize stop input schema"),
|
||||
)
|
||||
.expect("parse stop input schema");
|
||||
let interrupt: Value = serde_json::from_slice(
|
||||
&schema_json::<InterruptCommandInput>().expect("serialize interrupt input schema"),
|
||||
)
|
||||
.expect("parse interrupt input schema");
|
||||
|
||||
for schema in [
|
||||
&pre_tool_use,
|
||||
@@ -1101,6 +1157,7 @@ mod tests {
|
||||
&subagent_start,
|
||||
&subagent_stop,
|
||||
&stop,
|
||||
&interrupt,
|
||||
] {
|
||||
assert_eq!(schema["properties"]["turn_id"]["type"], "string");
|
||||
assert!(
|
||||
|
||||
Reference in new issue
Block a user