mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Add subagent identity to hook inputs (#22882)
# What When a normal hook fires inside a thread-spawned subagent, Codex now includes these optional top-level fields in the hook input: - `agent_id`: the child thread id - `agent_type`: the subagent role Root-agent hook inputs omit these fields. `SubagentStart` and `SubagentStop` keep their existing required `agent_id` and `agent_type` fields because those events are inherently subagent-scoped. This does not change matcher behavior. Tool hooks still match on tool name, compact hooks still match on trigger, and `UserPromptSubmit` still ignores matchers. Only `SubagentStart` and `SubagentStop` match on `agent_type`.
This commit is contained in:
1 parent
58be470d15
commit
16d85e2708
17 files changed
+302
-14
No files matched your search
@@ -12,6 +12,8 @@ use serde_json::Value;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::events::common::SubagentHookContext;
|
||||
|
||||
const GENERATED_DIR: &str = "generated";
|
||||
const POST_TOOL_USE_INPUT_FIXTURE: &str = "post-tool-use.command.input.schema.json";
|
||||
const POST_TOOL_USE_OUTPUT_FIXTURE: &str = "post-tool-use.command.output.schema.json";
|
||||
@@ -61,6 +63,24 @@ impl JsonSchema for NullableString {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub(crate) struct SubagentCommandInputFields {
|
||||
pub agent_id: Option<String>,
|
||||
pub agent_type: Option<String>,
|
||||
}
|
||||
|
||||
impl From<Option<&SubagentHookContext>> for SubagentCommandInputFields {
|
||||
fn from(value: Option<&SubagentHookContext>) -> Self {
|
||||
match value {
|
||||
Some(context) => Self {
|
||||
agent_id: Some(context.agent_id.clone()),
|
||||
agent_type: Some(context.agent_type.clone()),
|
||||
},
|
||||
None => Self::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[serde(deny_unknown_fields)]
|
||||
@@ -251,6 +271,10 @@ pub(crate) struct PreToolUseCommandInput {
|
||||
pub session_id: String,
|
||||
/// Codex extension: expose the active turn id to internal turn-scoped hooks.
|
||||
pub turn_id: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub agent_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub agent_type: Option<String>,
|
||||
pub transcript_path: NullableString,
|
||||
pub cwd: String,
|
||||
#[schemars(schema_with = "pre_tool_use_hook_event_name_schema")]
|
||||
@@ -270,6 +294,10 @@ pub(crate) struct PermissionRequestCommandInput {
|
||||
pub session_id: String,
|
||||
/// Codex extension: expose the active turn id to internal turn-scoped hooks.
|
||||
pub turn_id: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub agent_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub agent_type: Option<String>,
|
||||
pub transcript_path: NullableString,
|
||||
pub cwd: String,
|
||||
#[schemars(schema_with = "permission_request_hook_event_name_schema")]
|
||||
@@ -288,6 +316,10 @@ pub(crate) struct PostToolUseCommandInput {
|
||||
pub session_id: String,
|
||||
/// Codex extension: expose the active turn id to internal turn-scoped hooks.
|
||||
pub turn_id: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub agent_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub agent_type: Option<String>,
|
||||
pub transcript_path: NullableString,
|
||||
pub cwd: String,
|
||||
#[schemars(schema_with = "post_tool_use_hook_event_name_schema")]
|
||||
@@ -308,6 +340,10 @@ pub(crate) struct PreCompactCommandInput {
|
||||
pub session_id: String,
|
||||
/// Codex extension: expose the active turn id to internal turn-scoped hooks.
|
||||
pub turn_id: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub agent_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub agent_type: Option<String>,
|
||||
pub transcript_path: NullableString,
|
||||
pub cwd: String,
|
||||
#[schemars(schema_with = "pre_compact_hook_event_name_schema")]
|
||||
@@ -324,6 +360,10 @@ pub(crate) struct PostCompactCommandInput {
|
||||
pub session_id: String,
|
||||
/// Codex extension: expose the active turn id to internal turn-scoped hooks.
|
||||
pub turn_id: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub agent_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub agent_type: Option<String>,
|
||||
pub transcript_path: NullableString,
|
||||
pub cwd: String,
|
||||
#[schemars(schema_with = "post_compact_hook_event_name_schema")]
|
||||
@@ -486,6 +526,10 @@ pub(crate) struct UserPromptSubmitCommandInput {
|
||||
pub session_id: String,
|
||||
/// Codex extension: expose the active turn id to internal turn-scoped hooks.
|
||||
pub turn_id: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub agent_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub agent_type: Option<String>,
|
||||
pub transcript_path: NullableString,
|
||||
pub cwd: String,
|
||||
#[schemars(schema_with = "user_prompt_submit_hook_event_name_schema")]
|
||||
@@ -761,6 +805,7 @@ fn default_continue() -> bool {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::NullableString;
|
||||
use super::PERMISSION_REQUEST_INPUT_FIXTURE;
|
||||
use super::PERMISSION_REQUEST_OUTPUT_FIXTURE;
|
||||
use super::POST_COMPACT_INPUT_FIXTURE;
|
||||
@@ -785,6 +830,7 @@ mod tests {
|
||||
use super::SUBAGENT_STOP_INPUT_FIXTURE;
|
||||
use super::SUBAGENT_STOP_OUTPUT_FIXTURE;
|
||||
use super::StopCommandInput;
|
||||
use super::SubagentCommandInputFields;
|
||||
use super::SubagentStartCommandInput;
|
||||
use super::SubagentStopCommandInput;
|
||||
use super::USER_PROMPT_SUBMIT_INPUT_FIXTURE;
|
||||
@@ -792,8 +838,10 @@ mod tests {
|
||||
use super::UserPromptSubmitCommandInput;
|
||||
use super::schema_json;
|
||||
use super::write_schema_fixtures;
|
||||
use crate::events::common::SubagentHookContext;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::Value;
|
||||
use serde_json::json;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn expected_fixture(name: &str) -> &'static str {
|
||||
@@ -968,4 +1016,87 @@ mod tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subagent_context_fields_are_optional_for_hooks_that_run_inside_subagents() {
|
||||
let schemas = [
|
||||
schema_json::<PreToolUseCommandInput>().expect("serialize pre tool use input schema"),
|
||||
schema_json::<PermissionRequestCommandInput>()
|
||||
.expect("serialize permission request input schema"),
|
||||
schema_json::<PostToolUseCommandInput>().expect("serialize post tool use input schema"),
|
||||
schema_json::<PreCompactCommandInput>().expect("serialize pre compact input schema"),
|
||||
schema_json::<PostCompactCommandInput>().expect("serialize post compact input schema"),
|
||||
schema_json::<UserPromptSubmitCommandInput>()
|
||||
.expect("serialize user prompt submit input schema"),
|
||||
];
|
||||
|
||||
for schema in schemas {
|
||||
let schema: Value = serde_json::from_slice(&schema).expect("parse hook input schema");
|
||||
assert_eq!(schema["properties"]["agent_id"]["type"], "string");
|
||||
assert_eq!(schema["properties"]["agent_type"]["type"], "string");
|
||||
let required = schema["required"]
|
||||
.as_array()
|
||||
.expect("schema required fields");
|
||||
assert!(!required.contains(&Value::String("agent_id".to_string())));
|
||||
assert!(!required.contains(&Value::String("agent_type".to_string())));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subagent_context_fields_serialize_flat_and_omit_when_absent() {
|
||||
let subagent = SubagentCommandInputFields::from(Some(&SubagentHookContext {
|
||||
agent_id: "agent-1".to_string(),
|
||||
agent_type: "worker".to_string(),
|
||||
}));
|
||||
let input = PreToolUseCommandInput {
|
||||
session_id: "session-1".to_string(),
|
||||
turn_id: "turn-1".to_string(),
|
||||
agent_id: subagent.agent_id,
|
||||
agent_type: subagent.agent_type,
|
||||
transcript_path: NullableString::from_path(/*path*/ None),
|
||||
cwd: "/tmp".to_string(),
|
||||
hook_event_name: "PreToolUse".to_string(),
|
||||
model: "gpt-test".to_string(),
|
||||
permission_mode: "default".to_string(),
|
||||
tool_name: "Bash".to_string(),
|
||||
tool_input: json!({ "command": "echo hello" }),
|
||||
tool_use_id: "tool-1".to_string(),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
serde_json::to_value(input).expect("serialize subagent hook input"),
|
||||
json!({
|
||||
"session_id": "session-1",
|
||||
"turn_id": "turn-1",
|
||||
"agent_id": "agent-1",
|
||||
"agent_type": "worker",
|
||||
"transcript_path": null,
|
||||
"cwd": "/tmp",
|
||||
"hook_event_name": "PreToolUse",
|
||||
"model": "gpt-test",
|
||||
"permission_mode": "default",
|
||||
"tool_name": "Bash",
|
||||
"tool_input": { "command": "echo hello" },
|
||||
"tool_use_id": "tool-1",
|
||||
})
|
||||
);
|
||||
|
||||
let root_input = PreToolUseCommandInput {
|
||||
session_id: "session-1".to_string(),
|
||||
turn_id: "turn-1".to_string(),
|
||||
agent_id: None,
|
||||
agent_type: None,
|
||||
transcript_path: NullableString::from_path(/*path*/ None),
|
||||
cwd: "/tmp".to_string(),
|
||||
hook_event_name: "PreToolUse".to_string(),
|
||||
model: "gpt-test".to_string(),
|
||||
permission_mode: "default".to_string(),
|
||||
tool_name: "Bash".to_string(),
|
||||
tool_input: json!({ "command": "echo hello" }),
|
||||
tool_use_id: "tool-1".to_string(),
|
||||
};
|
||||
let root_input = serde_json::to_value(root_input).expect("serialize root hook input");
|
||||
assert_eq!(root_input.get("agent_id"), None);
|
||||
assert_eq!(root_input.get("agent_type"), None);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user