Support asynchronous command hooks (#37533)

## Why

Hook configurations can mark command handlers as asynchronous, but Codex previously skipped those handlers outside `SessionEnd`.

## What changed

- Run asynchronous command hooks in the background with a per-session concurrency limit, while keeping `SessionEnd` hooks synchronous.
- Prevent asynchronous hooks from blocking, stopping, rewriting, or otherwise controlling the operation that launched them.
- Deliver warnings and additional context at safe turn boundaries: inject results into an active turn after sampling, or buffer them ahead of the next user prompt when the session is idle.
- Preserve in-flight hooks across configuration reloads, scope spilled output to the thread, and abort outstanding work during session shutdown.

## Testing

Add unit and integration coverage for background scheduling, concurrency, output parsing, active and idle result delivery, configuration reloads, and shutdown cleanup.

GitOrigin-RevId: 8094552e4afe7b47b09a61bb575bb20f4e491d8d
This commit is contained in:
Abhinav
2026-08-08 04:24:12 +00:00
committed by copyberry
parent 61a3dd4387
commit 6f647caa9b
29 changed files with 1514 additions and 429 deletions
+21 -8
View File
@@ -61,12 +61,6 @@ pub(crate) struct StopOutput {
pub invalid_block_reason: Option<String>,
}
#[derive(Debug, Clone)]
pub(crate) struct PreCompactOutput {
pub universal: UniversalOutput,
pub invalid_reason: Option<String>,
}
#[derive(Debug, Clone)]
pub(crate) struct StatelessHookOutput {
pub universal: UniversalOutput,
@@ -238,10 +232,10 @@ pub(crate) fn parse_post_tool_use(stdout: &str) -> Option<PostToolUseOutput> {
})
}
pub(crate) fn parse_pre_compact(stdout: &str) -> Option<PreCompactOutput> {
pub(crate) fn parse_pre_compact(stdout: &str) -> Option<StatelessHookOutput> {
let wire: PreCompactCommandOutputWire = parse_json(stdout)?;
let universal = UniversalOutput::from(wire.universal);
Some(PreCompactOutput {
Some(StatelessHookOutput {
universal,
invalid_reason: None,
})
@@ -519,6 +513,25 @@ mod tests {
use serde_json::json;
use super::parse_permission_request;
use super::parse_user_prompt_submit;
#[test]
fn structured_output_rejects_invalid_shapes_and_types() {
for stdout in [
"[]",
r#"{"systemMessage":123}"#,
r#"{"hookSpecificOutput":{"hookEventName":"UserPromptSubmit","additionalContext":123}}"#,
r#"{"hookSpecificOutput":{"additionalContext":"missing event name"}}"#,
r#"{"hookSpecificOutput":{"hookEventName":"UserPromptSubmit","updatedInput":{}}}"#,
r#"{"unexpectedField":true}"#,
"{",
] {
assert!(
parse_user_prompt_submit(stdout).is_none(),
"invalid structured output should fail: {stdout}",
);
}
}
#[test]
fn permission_request_rejects_reserved_updated_input_field() {