mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
Inspect turn hooks before runtime preparation
This commit is contained in:
@@ -48,6 +48,7 @@ use crate::session::turn_context::TurnContext;
|
||||
use crate::tools::hook_names::HookToolName;
|
||||
use crate::tools::sandboxing::PermissionRequestPayload;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct HookRuntimeOutcome {
|
||||
pub should_stop: bool,
|
||||
pub additional_contexts: Vec<String>,
|
||||
@@ -103,7 +104,8 @@ impl From<UserPromptSubmitOutcome> for ContextInjectingHookOutcome {
|
||||
pub(crate) async fn run_pending_session_start_hooks(
|
||||
sess: &Arc<Session>,
|
||||
turn_context: &Arc<TurnContext>,
|
||||
) -> bool {
|
||||
) -> HookRuntimeOutcome {
|
||||
let mut combined_outcome = HookRuntimeOutcome::default();
|
||||
while let Some(session_start_source) = sess.take_pending_session_start_source().await {
|
||||
// Pending session-start hooks are reused to dispatch thread-spawn subagent
|
||||
// starts. Other subagent sessions are internal/system work and do not run
|
||||
@@ -122,7 +124,7 @@ pub(crate) async fn run_pending_session_start_hooks(
|
||||
agent_type: context.agent_type,
|
||||
}
|
||||
}
|
||||
SessionSource::SubAgent(_) => return false,
|
||||
SessionSource::SubAgent(_) => return combined_outcome,
|
||||
_ => StartHookTarget::SessionStart {
|
||||
source: session_start_source,
|
||||
},
|
||||
@@ -138,21 +140,23 @@ pub(crate) async fn run_pending_session_start_hooks(
|
||||
};
|
||||
let hooks = sess.hooks();
|
||||
let preview_runs = hooks.preview_session_start(&request);
|
||||
if run_context_injecting_hook(
|
||||
let outcome = run_context_injecting_hook(
|
||||
sess,
|
||||
turn_context,
|
||||
preview_runs,
|
||||
hooks.run_session_start(request, Some(turn_context.sub_id.clone())),
|
||||
)
|
||||
.await
|
||||
.record_additional_contexts(sess, turn_context)
|
||||
.await
|
||||
{
|
||||
return true;
|
||||
.await;
|
||||
combined_outcome
|
||||
.additional_contexts
|
||||
.extend(outcome.additional_contexts);
|
||||
if outcome.should_stop {
|
||||
combined_outcome.should_stop = true;
|
||||
return combined_outcome;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
combined_outcome
|
||||
}
|
||||
|
||||
/// Runs matching `PreToolUse` hooks before a tool executes.
|
||||
@@ -580,18 +584,6 @@ where
|
||||
outcome.outcome
|
||||
}
|
||||
|
||||
impl HookRuntimeOutcome {
|
||||
async fn record_additional_contexts(
|
||||
self,
|
||||
sess: &Arc<Session>,
|
||||
turn_context: &Arc<TurnContext>,
|
||||
) -> bool {
|
||||
record_additional_contexts(sess, turn_context, self.additional_contexts).await;
|
||||
|
||||
self.should_stop
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn record_additional_contexts(
|
||||
sess: &Arc<Session>,
|
||||
turn_context: &Arc<TurnContext>,
|
||||
|
||||
@@ -18,6 +18,7 @@ use crate::compact_remote_v2::run_inline_remote_auto_compact_task as run_inline_
|
||||
use crate::connectors;
|
||||
use crate::context::ContextualUserFragment;
|
||||
use crate::feedback_tags;
|
||||
use crate::hook_runtime::HookRuntimeOutcome;
|
||||
use crate::hook_runtime::inspect_pending_input;
|
||||
use crate::hook_runtime::record_additional_contexts;
|
||||
use crate::hook_runtime::record_pending_input;
|
||||
@@ -164,24 +165,52 @@ pub(crate) async fn run_turn(
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let session_start_outcome = run_pending_session_start_hooks(&sess, &turn_context).await;
|
||||
if session_start_outcome.should_stop {
|
||||
sess.record_context_updates_and_set_reference_context_item(turn_context.as_ref())
|
||||
.await;
|
||||
record_additional_contexts(
|
||||
&sess,
|
||||
&turn_context,
|
||||
session_start_outcome.additional_contexts,
|
||||
)
|
||||
.await;
|
||||
return Ok(None);
|
||||
}
|
||||
let (inspected_input, blocked_input) = inspect_inputs(&sess, &turn_context, &input).await;
|
||||
if blocked_input {
|
||||
sess.record_context_updates_and_set_reference_context_item(turn_context.as_ref())
|
||||
.await;
|
||||
record_additional_contexts(
|
||||
&sess,
|
||||
&turn_context,
|
||||
session_start_outcome.additional_contexts,
|
||||
)
|
||||
.await;
|
||||
record_inspected_inputs(&sess, &turn_context, inspected_input).await;
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// Keep the exact model-visible state used by this turn and its inline compactions.
|
||||
let mut world_state = sess
|
||||
.record_context_updates_and_set_reference_context_item(turn_context.as_ref())
|
||||
.await;
|
||||
record_additional_contexts(
|
||||
&sess,
|
||||
&turn_context,
|
||||
session_start_outcome.additional_contexts,
|
||||
)
|
||||
.await;
|
||||
|
||||
let Some((injection_items, explicitly_enabled_connectors)) =
|
||||
build_skills_and_plugins(&sess, turn_context.as_ref(), &input, &cancellation_token).await
|
||||
else {
|
||||
record_inspected_inputs(&sess, &turn_context, inspected_input).await;
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
if run_pending_session_start_hooks(&sess, &turn_context).await {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut can_drain_pending_input = input.is_empty();
|
||||
if run_hooks_and_record_inputs(&sess, &turn_context, &input).await {
|
||||
return Ok(None);
|
||||
}
|
||||
record_inspected_inputs(&sess, &turn_context, inspected_input).await;
|
||||
|
||||
sess.merge_connector_selection(explicitly_enabled_connectors.clone())
|
||||
.await;
|
||||
@@ -472,17 +501,41 @@ async fn run_hooks_and_record_inputs(
|
||||
turn_context: &Arc<TurnContext>,
|
||||
input: &[TurnInput],
|
||||
) -> bool {
|
||||
let (inspected_input, blocked_input) = inspect_inputs(sess, turn_context, input).await;
|
||||
record_inspected_inputs(sess, turn_context, inspected_input).await;
|
||||
blocked_input
|
||||
}
|
||||
|
||||
async fn inspect_inputs<'a>(
|
||||
sess: &Arc<Session>,
|
||||
turn_context: &Arc<TurnContext>,
|
||||
input: &'a [TurnInput],
|
||||
) -> (Vec<(&'a TurnInput, HookRuntimeOutcome)>, bool) {
|
||||
let mut blocked_input = false;
|
||||
let mut accepted_user_input = false;
|
||||
let mut inspected_input = Vec::with_capacity(input.len());
|
||||
for input_item in input {
|
||||
let hook_outcome = inspect_pending_input(sess, turn_context, input_item).await;
|
||||
if hook_outcome.should_stop {
|
||||
blocked_input = true;
|
||||
} else if matches!(input_item, TurnInput::UserInput { content, .. } if !content.is_empty())
|
||||
{
|
||||
accepted_user_input = true;
|
||||
}
|
||||
inspected_input.push((input_item, hook_outcome));
|
||||
}
|
||||
(inspected_input, blocked_input && !accepted_user_input)
|
||||
}
|
||||
|
||||
async fn record_inspected_inputs(
|
||||
sess: &Arc<Session>,
|
||||
turn_context: &Arc<TurnContext>,
|
||||
inspected_input: Vec<(&TurnInput, HookRuntimeOutcome)>,
|
||||
) {
|
||||
for (input_item, hook_outcome) in inspected_input {
|
||||
if hook_outcome.should_stop {
|
||||
record_additional_contexts(sess, turn_context, hook_outcome.additional_contexts).await;
|
||||
} else {
|
||||
if matches!(input_item, TurnInput::UserInput { content, .. } if !content.is_empty()) {
|
||||
accepted_user_input = true;
|
||||
}
|
||||
record_pending_input(
|
||||
sess,
|
||||
turn_context,
|
||||
@@ -492,7 +545,6 @@ async fn run_hooks_and_record_inputs(
|
||||
.await;
|
||||
}
|
||||
}
|
||||
blocked_input && !accepted_user_input
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip_all)]
|
||||
|
||||
Reference in New Issue
Block a user