mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Apply additional context when turn input is delivered (#62602)
## Why
Applying client context at submission time lets input stopped by a hook
affect the conversation. A queued prompt that is later blocked can also
supersede the context attached to an earlier accepted prompt.
## What changed
Keep each context update with its submitted input and apply updates in
delivery order after the input passes its hooks. Preserve context-only
submissions when a turn is suspended for host handoff before inference.
For experimental `additionalContext` on `turn/start` and `turn/steer`,
omission or `null` keeps the current set. A supplied map replaces it on
delivery, and `{}` clears it while preserving conversation history.
## Testing
Add regression coverage for context on prompts stopped by hooks, context
on accepted queued prompts, context-only host handoff, and context
delivery when continuing an idle thread.
GitOrigin-RevId: 160f4f90d9aebf5eb3ea4a939c67265a1c6422da
This commit is contained in:
BIN
Binary file not shown.
@@ -224,7 +224,10 @@ pub struct TurnStartParams {
|
||||
#[experimental("turn/start.responsesapiClientMetadataPatch")]
|
||||
#[ts(optional = nullable)]
|
||||
pub responsesapi_client_metadata_patch: Option<HashMap<String, String>>,
|
||||
/// Optional client-provided context fragments keyed by an opaque source identifier.
|
||||
/// Client-provided context fragments keyed by an opaque source identifier.
|
||||
/// Omit this field or pass `null` to keep the current set. Pass a map to
|
||||
/// replace the set when the input is delivered, or `{}` to clear it.
|
||||
/// Clearing the set leaves previously recorded messages in the conversation history.
|
||||
#[experimental("turn/start.additionalContext")]
|
||||
#[ts(optional = nullable)]
|
||||
pub additional_context: Option<HashMap<String, AdditionalContextEntry>>,
|
||||
@@ -346,7 +349,10 @@ pub struct TurnSteerParams {
|
||||
#[experimental("turn/steer.responsesapiClientMetadataPatch")]
|
||||
#[ts(optional = nullable)]
|
||||
pub responsesapi_client_metadata_patch: Option<HashMap<String, String>>,
|
||||
/// Optional client-provided context fragments keyed by an opaque source identifier.
|
||||
/// Client-provided context fragments keyed by an opaque source identifier.
|
||||
/// Omit this field or pass `null` to keep the current set. Pass a map to
|
||||
/// replace the set when the input is delivered, or `{}` to clear it.
|
||||
/// Clearing the set leaves previously recorded messages in the conversation history.
|
||||
#[experimental("turn/steer.additionalContext")]
|
||||
#[ts(optional = nullable)]
|
||||
pub additional_context: Option<HashMap<String, AdditionalContextEntry>>,
|
||||
|
||||
@@ -102,10 +102,9 @@ pub(crate) struct TurnRequestProcessor {
|
||||
}
|
||||
|
||||
fn map_additional_context(
|
||||
additional_context: Option<HashMap<String, AdditionalContextEntry>>,
|
||||
additional_context: HashMap<String, AdditionalContextEntry>,
|
||||
) -> BTreeMap<String, CoreAdditionalContextEntry> {
|
||||
additional_context
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|(key, entry)| {
|
||||
let entry = CoreAdditionalContextEntry {
|
||||
@@ -638,9 +637,10 @@ impl TurnRequestProcessor {
|
||||
return Err(error);
|
||||
}
|
||||
// Validate before changing the thread's live MCP policy.
|
||||
let additional_context = map_additional_context(params.additional_context);
|
||||
let additional_context = params.additional_context.map(map_additional_context);
|
||||
additional_context
|
||||
.values()
|
||||
.iter()
|
||||
.flat_map(BTreeMap::values)
|
||||
.try_for_each(CoreAdditionalContextEntry::validate)
|
||||
.map_err(invalid_request)
|
||||
.inspect_err(|error| {
|
||||
@@ -1185,7 +1185,7 @@ impl TurnRequestProcessor {
|
||||
.into_iter()
|
||||
.map(V2UserInput::into_core)
|
||||
.collect();
|
||||
let additional_context = map_additional_context(params.additional_context);
|
||||
let additional_context = params.additional_context.map(map_additional_context);
|
||||
|
||||
let submission = thread
|
||||
.steer_turn(
|
||||
|
||||
@@ -278,7 +278,7 @@ impl SampleTask {
|
||||
.with_responses_metadata(params.responsesapi_client_metadata)
|
||||
.with_trace(trace);
|
||||
if source_in_progress {
|
||||
input.additional_context.insert(
|
||||
input.additional_context.get_or_insert_default().insert(
|
||||
"thread_snapshot".to_string(),
|
||||
AdditionalContextEntry::new(
|
||||
THREAD_SNAPSHOT_NOTE.to_string(),
|
||||
|
||||
@@ -7,6 +7,7 @@ use codex_diagnostics::GaugeGuard;
|
||||
use codex_history::ResponseItemEnvelope;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::protocol::AdditionalContextEntry;
|
||||
use codex_protocol::turn_input::TurnStartOptions;
|
||||
use codex_user_input::ResolvedUserInput;
|
||||
use serde::Deserialize;
|
||||
@@ -59,6 +60,32 @@ pub enum TurnInput {
|
||||
InterAgentCommunication(crate::PendingAgentMessage),
|
||||
}
|
||||
|
||||
/// Client context attached to one submitted input.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub(crate) struct SubmittedAdditionalContext(pub(crate) BTreeMap<String, AdditionalContextEntry>);
|
||||
|
||||
/// An existing input and the optional client context submitted with it.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub(crate) struct InputQueueEntry {
|
||||
pub(crate) input: TurnInput,
|
||||
pub(crate) additional_context: Option<SubmittedAdditionalContext>,
|
||||
}
|
||||
|
||||
impl InputQueueEntry {
|
||||
pub(crate) fn input_and_context(&self) -> (&TurnInput, Option<&SubmittedAdditionalContext>) {
|
||||
(&self.input, self.additional_context.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TurnInput> for InputQueueEntry {
|
||||
fn from(input: TurnInput) -> Self {
|
||||
Self {
|
||||
input,
|
||||
additional_context: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod turn_input_response_item {
|
||||
use super::ResponseItem;
|
||||
use super::ResponseItemEnvelope;
|
||||
@@ -127,7 +154,7 @@ impl TurnInput {
|
||||
/// Turn-local pending input storage owned by the input queue flow.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct TurnInputQueue {
|
||||
items: Vec<TurnInput>,
|
||||
items: Vec<InputQueueEntry>,
|
||||
/// Activity from notifying writes. Passive response items do not wake a wait.
|
||||
notified_activity: Option<InputQueueActivity>,
|
||||
}
|
||||
@@ -250,9 +277,9 @@ impl InputQueue {
|
||||
let turn_state = turn_state.lock().await;
|
||||
turn_state.accepts_mailbox_delivery_for_current_turn()
|
||||
&& (native_mail_pending
|
||||
|| turn_state.pending_input.items.iter().any(|input| {
|
||||
|| turn_state.pending_input.items.iter().any(|entry| {
|
||||
matches!(
|
||||
input,
|
||||
&entry.input,
|
||||
TurnInput::ResponseItem(ResponseItemEnvelope {
|
||||
item: ResponseItem::AgentMessage { .. },
|
||||
..
|
||||
@@ -385,7 +412,7 @@ impl InputQueue {
|
||||
let results: Vec<_> = pending_input
|
||||
.items
|
||||
.into_iter()
|
||||
.filter_map(|input| match input {
|
||||
.filter_map(|entry| match entry.input {
|
||||
TurnInput::InterAgentCommunication(message) if message.is_terminal_result() => {
|
||||
Some(message)
|
||||
}
|
||||
@@ -420,7 +447,7 @@ impl InputQueue {
|
||||
.pending_input
|
||||
.items
|
||||
.iter()
|
||||
.any(|input| !input.is_queue_only_mail())
|
||||
.any(|entry| !entry.input.is_queue_only_mail())
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -453,10 +480,12 @@ impl InputQueue {
|
||||
pub(super) async fn extend_pending_input_and_notify_for_turn_state(
|
||||
&self,
|
||||
turn_state: &Mutex<TurnState>,
|
||||
input: Vec<TurnInput>,
|
||||
input: Vec<impl Into<InputQueueEntry>>,
|
||||
) {
|
||||
let activity = input.iter().map(TurnInput::wait_activity).max();
|
||||
let queue_only_mail = !input.is_empty() && input.iter().all(TurnInput::is_queue_only_mail);
|
||||
let input: Vec<InputQueueEntry> = input.into_iter().map(Into::into).collect();
|
||||
let activity = input.iter().map(|entry| entry.input.wait_activity()).max();
|
||||
let queue_only_mail =
|
||||
!input.is_empty() && input.iter().all(|entry| entry.input.is_queue_only_mail());
|
||||
{
|
||||
let mut turn_state = turn_state.lock().await;
|
||||
turn_state.pending_input.notified_activity =
|
||||
@@ -473,15 +502,16 @@ impl InputQueue {
|
||||
pub(crate) async fn extend_pending_input_for_turn_state(
|
||||
&self,
|
||||
turn_state: &Mutex<TurnState>,
|
||||
input: Vec<TurnInput>,
|
||||
input: Vec<impl Into<InputQueueEntry>>,
|
||||
) {
|
||||
let input = input.into_iter().map(Into::into);
|
||||
turn_state.lock().await.pending_input.items.extend(input);
|
||||
}
|
||||
|
||||
pub(crate) async fn take_pending_input_for_turn_state(
|
||||
&self,
|
||||
turn_state: &Mutex<TurnState>,
|
||||
) -> Vec<TurnInput> {
|
||||
) -> Vec<InputQueueEntry> {
|
||||
std::mem::take(&mut turn_state.lock().await.pending_input).items
|
||||
}
|
||||
|
||||
@@ -492,7 +522,7 @@ impl InputQueue {
|
||||
pub(crate) async fn get_pending_input(
|
||||
&self,
|
||||
active_turn: &Mutex<Option<ActiveTurn>>,
|
||||
) -> (Vec<TurnInput>, TurnStartOptions) {
|
||||
) -> (Vec<InputQueueEntry>, TurnStartOptions) {
|
||||
let (pending_input, accepts_mailbox_delivery) = {
|
||||
let mut active = active_turn.lock().await;
|
||||
match active.as_mut() {
|
||||
@@ -514,13 +544,9 @@ impl InputQueue {
|
||||
return (pending_input, TurnStartOptions::default());
|
||||
}
|
||||
let (mailbox_items, start_options) = self.drain_mailbox_input_items().await;
|
||||
if pending_input.is_empty() {
|
||||
(mailbox_items, start_options)
|
||||
} else {
|
||||
let mut pending_input = pending_input;
|
||||
pending_input.extend(mailbox_items);
|
||||
(pending_input, start_options)
|
||||
}
|
||||
let mut pending_input = pending_input;
|
||||
pending_input.extend(mailbox_items.into_iter().map(InputQueueEntry::from));
|
||||
(pending_input, start_options)
|
||||
}
|
||||
|
||||
#[expect(
|
||||
@@ -555,7 +581,7 @@ impl TurnInputQueue {
|
||||
pub(crate) fn has_user_input(&self) -> bool {
|
||||
self.items
|
||||
.iter()
|
||||
.any(|input| matches!(input, TurnInput::UserInput { .. }))
|
||||
.any(|entry| matches!(entry.input, TurnInput::UserInput { .. }))
|
||||
}
|
||||
|
||||
pub(crate) fn is_empty(&self) -> bool {
|
||||
@@ -568,8 +594,8 @@ impl TurnInputQueue {
|
||||
}
|
||||
self.items
|
||||
.iter()
|
||||
.filter(|input| !matches!(input, TurnInput::ResponseItem(_)))
|
||||
.map(TurnInput::wait_activity)
|
||||
.filter(|entry| !matches!(entry.input, TurnInput::ResponseItem(_)))
|
||||
.map(|entry| entry.input.wait_activity())
|
||||
.chain(self.notified_activity)
|
||||
.max()
|
||||
}
|
||||
|
||||
@@ -309,6 +309,8 @@ pub(crate) use self::environment::ThreadEnvironmentDefaults;
|
||||
use self::handlers::submission_dispatch_span;
|
||||
use self::handlers::submission_loop;
|
||||
pub(crate) use self::input_queue::InputQueueActivity;
|
||||
pub(crate) use self::input_queue::InputQueueEntry;
|
||||
pub(crate) use self::input_queue::SubmittedAdditionalContext;
|
||||
pub(crate) use self::input_queue::TurnInput;
|
||||
pub(crate) use self::input_queue::TurnInputQueue;
|
||||
pub(crate) use self::input_queue::UserInputMetadata;
|
||||
|
||||
@@ -12798,7 +12798,7 @@ async fn abort_empty_active_turn_preserves_pending_input() {
|
||||
sess.input_queue
|
||||
.take_pending_input_for_turn_state(turn_state.as_ref())
|
||||
.await,
|
||||
vec![TurnInput::ResponseItem(pending_item.into())]
|
||||
vec![TurnInput::ResponseItem(pending_item.into()).into()]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -12855,7 +12855,7 @@ async fn queue_only_mailbox_mail_waits_for_next_turn_after_answer_boundary() {
|
||||
|
||||
assert_eq!(
|
||||
(sess.input_queue.get_pending_input(&sess.active_turn).await).0,
|
||||
vec![TurnInput::InterAgentCommunication(communication.into())],
|
||||
vec![TurnInput::InterAgentCommunication(communication.into()).into()],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -12956,8 +12956,8 @@ async fn active_turn_keeps_first_root_when_mail_coalesces(inherited_root: Option
|
||||
assert_eq!(
|
||||
(sess.input_queue.get_pending_input(&sess.active_turn).await).0,
|
||||
vec![
|
||||
TurnInput::InterAgentCommunication(first.into()),
|
||||
TurnInput::InterAgentCommunication(second.into()),
|
||||
TurnInput::InterAgentCommunication(first.into()).into(),
|
||||
TurnInput::InterAgentCommunication(second.into()).into(),
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
@@ -13013,8 +13013,9 @@ async fn steered_input_reopens_mailbox_delivery_for_current_turn() {
|
||||
},
|
||||
content: vec![ResolvedUserInput::text("follow up".to_string())],
|
||||
client_id: None,
|
||||
},
|
||||
TurnInput::InterAgentCommunication(communication.into()),
|
||||
}
|
||||
.into(),
|
||||
TurnInput::InterAgentCommunication(communication.into()).into(),
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -13067,8 +13068,9 @@ async fn stale_defer_mailbox_delivery_does_not_override_steered_input() {
|
||||
},
|
||||
content: vec![ResolvedUserInput::text("follow up".to_string())],
|
||||
client_id: None,
|
||||
},
|
||||
TurnInput::InterAgentCommunication(communication.into()),
|
||||
}
|
||||
.into(),
|
||||
TurnInput::InterAgentCommunication(communication.into()).into(),
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -13129,7 +13131,7 @@ async fn tool_calls_reopen_mailbox_delivery_for_current_turn() {
|
||||
assert!(output.tool_future.is_some());
|
||||
assert_eq!(
|
||||
(sess.input_queue.get_pending_input(&sess.active_turn).await).0,
|
||||
vec![TurnInput::InterAgentCommunication(communication.into())],
|
||||
vec![TurnInput::InterAgentCommunication(communication.into()).into()],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,9 @@ use crate::responses_retry::handle_response_stream_error;
|
||||
use crate::responses_retry::is_terminal_sampling_outcome;
|
||||
use crate::rollout_budget::ResponseWork;
|
||||
use crate::rollout_budget::TurnWork;
|
||||
use crate::session::InputQueueEntry;
|
||||
use crate::session::PreviousTurnSettings;
|
||||
use crate::session::SubmittedAdditionalContext;
|
||||
use crate::session::TurnInput;
|
||||
use crate::session::daemon_recovery::RecordedTurnInput;
|
||||
use crate::session::sampling_interrupt::SamplingInterrupt;
|
||||
@@ -174,7 +176,14 @@ async fn preserve_initial_input_during<T>(
|
||||
input: &mut Vec<TurnInput>,
|
||||
prepare: impl Future<Output = T>,
|
||||
) -> CodexResult<T> {
|
||||
if input.is_empty() || sess.host_roster_reader(turn_context).is_none() {
|
||||
// A context-only submission still needs Some([]) so suspension records its context.
|
||||
if sess.host_roster_reader(turn_context).is_none()
|
||||
|| (input.is_empty()
|
||||
&& turn_context
|
||||
.extension_data
|
||||
.get::<SubmittedAdditionalContext>()
|
||||
.is_none())
|
||||
{
|
||||
return Ok(prepare.await);
|
||||
}
|
||||
|
||||
@@ -227,11 +236,12 @@ pub(crate) async fn run_turn(
|
||||
if !model_info.input_modalities.contains(&InputModality::Audio)
|
||||
&& input.iter().any(turn_input_has_uploaded_audio_file)
|
||||
{
|
||||
let result = run_hooks_and_record_inputs(
|
||||
let result = run_hooks_and_record_input_entries(
|
||||
&sess,
|
||||
&turn_context,
|
||||
&model_info,
|
||||
&input,
|
||||
Some(&input),
|
||||
/*pending_input*/ &[],
|
||||
PersistContext::TurnStart,
|
||||
)
|
||||
.await;
|
||||
@@ -274,11 +284,12 @@ pub(crate) async fn run_turn(
|
||||
.await?
|
||||
{
|
||||
// Compaction runs before the new input is recorded, so preserve it on every failure.
|
||||
let _ = run_hooks_and_record_inputs(
|
||||
let _ = run_hooks_and_record_input_entries(
|
||||
&sess,
|
||||
&turn_context,
|
||||
&turn_context.capture_current_model_info(),
|
||||
&input,
|
||||
Some(&input),
|
||||
/*pending_input*/ &[],
|
||||
PersistContext::Standard,
|
||||
)
|
||||
.await;
|
||||
@@ -323,11 +334,12 @@ pub(crate) async fn run_turn(
|
||||
{
|
||||
Ok(requirements) => requirements,
|
||||
Err(err) => {
|
||||
let _ = run_hooks_and_record_inputs(
|
||||
let _ = run_hooks_and_record_input_entries(
|
||||
&sess,
|
||||
&turn_context,
|
||||
&turn_context.capture_current_model_info(),
|
||||
&input,
|
||||
Some(&input),
|
||||
/*pending_input*/ &[],
|
||||
PersistContext::Standard,
|
||||
)
|
||||
.await;
|
||||
@@ -351,11 +363,12 @@ pub(crate) async fn run_turn(
|
||||
{
|
||||
Ok(step_context) => step_context,
|
||||
Err(err) if crate::tasks::ends_turn_as_interrupted(&err) => {
|
||||
let _ = run_hooks_and_record_inputs(
|
||||
let _ = run_hooks_and_record_input_entries(
|
||||
&sess,
|
||||
&turn_context,
|
||||
&turn_context.capture_current_model_info(),
|
||||
&input,
|
||||
Some(&input),
|
||||
/*pending_input*/ &[],
|
||||
PersistContext::Standard,
|
||||
)
|
||||
.await;
|
||||
@@ -410,6 +423,10 @@ pub(crate) async fn run_turn(
|
||||
};
|
||||
|
||||
if run_pending_session_start_hooks(&sess, &turn_context).await {
|
||||
// A queued steer can reuse this turn context after the initial input is discarded.
|
||||
turn_context
|
||||
.extension_data
|
||||
.remove::<SubmittedAdditionalContext>();
|
||||
return Ok(None);
|
||||
}
|
||||
if crate::guardian::is_basic_session_source(&turn_context.session_source)
|
||||
@@ -455,11 +472,12 @@ pub(crate) async fn run_turn(
|
||||
.await?;
|
||||
}
|
||||
let mut can_drain_pending_input = input.is_empty();
|
||||
if run_hooks_and_record_inputs(
|
||||
if run_hooks_and_record_input_entries(
|
||||
&sess,
|
||||
&turn_context,
|
||||
&first_step_context.settings.model_info,
|
||||
&input,
|
||||
Some(&input),
|
||||
/*pending_input*/ &[],
|
||||
PersistContext::TurnStart,
|
||||
)
|
||||
.await?
|
||||
@@ -569,10 +587,11 @@ pub(crate) async fn run_turn(
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
if run_hooks_and_record_inputs(
|
||||
if run_hooks_and_record_input_entries(
|
||||
&sess,
|
||||
&turn_context,
|
||||
&turn_context.capture_current_model_info(),
|
||||
/*initial_input*/ None,
|
||||
&pending_input,
|
||||
PersistContext::SteeredUserInput,
|
||||
)
|
||||
@@ -604,7 +623,8 @@ pub(crate) async fn run_turn(
|
||||
.await?
|
||||
}
|
||||
Some(_) | None => {
|
||||
let pending_user_input = input_for_extensions(&pending_input);
|
||||
let pending_user_input =
|
||||
input_for_extensions(pending_input.iter().map(|entry| &entry.input));
|
||||
if allow_plugin_mentions {
|
||||
required_plugins.extend(crate::plugins::collect_explicit_plugin_ids(
|
||||
&pending_user_input,
|
||||
@@ -1151,19 +1171,39 @@ async fn turn_diff_display_roots(step_context: &StepContext) -> Vec<(String, Pat
|
||||
display_roots
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip_all)]
|
||||
pub(crate) async fn run_hooks_and_record_inputs(
|
||||
#[instrument(name = "run_hooks_and_record_inputs", level = "trace", skip_all)]
|
||||
pub(crate) async fn run_hooks_and_record_input_entries(
|
||||
sess: &Arc<Session>,
|
||||
turn_context: &Arc<TurnContext>,
|
||||
model_info: &ModelInfo,
|
||||
input: &[TurnInput],
|
||||
initial_input: Option<&[TurnInput]>,
|
||||
pending_input: &[InputQueueEntry],
|
||||
persist_context: PersistContext,
|
||||
) -> CodexResult<bool> {
|
||||
// A started turn owns one initial submission; consume its context exactly once.
|
||||
let initial_context = turn_context
|
||||
.extension_data
|
||||
.remove::<SubmittedAdditionalContext>();
|
||||
// An empty original submission can deliver context by itself; missing input
|
||||
// means its original submission is no longer available for delivery.
|
||||
if matches!(initial_input, Some([]))
|
||||
&& let Some(context) = initial_context.as_deref()
|
||||
&& can_record_input(sess)
|
||||
{
|
||||
record_delivered_additional_context(sess, turn_context, model_info, context).await;
|
||||
}
|
||||
let initial_input = initial_input.unwrap_or_default();
|
||||
let mut blocked_input = false;
|
||||
let mut accepted_user_input = false;
|
||||
let mut input_error = None;
|
||||
for input_item in recordable_inputs(sess, model_info, input) {
|
||||
let input_item = input_item?;
|
||||
for input_item in recordable_inputs(
|
||||
sess,
|
||||
model_info,
|
||||
initial_input,
|
||||
pending_input,
|
||||
initial_context.as_deref(),
|
||||
) {
|
||||
let (input_item, additional_context) = input_item?;
|
||||
let hook_outcome = inspect_pending_input(sess, turn_context, &input_item).await;
|
||||
let realtime_handoff_id = match input_item.as_ref() {
|
||||
TurnInput::UserInput { metadata, .. } => metadata.realtime_handoff_id,
|
||||
@@ -1176,6 +1216,9 @@ pub(crate) async fn run_hooks_and_record_inputs(
|
||||
blocked_input = true;
|
||||
record_additional_contexts(sess, turn_context, hook_outcome.additional_contexts).await;
|
||||
} else {
|
||||
if let Some(context) = additional_context {
|
||||
record_delivered_additional_context(sess, turn_context, model_info, context).await;
|
||||
}
|
||||
// Tool outputs retain their durability barrier, including in mixed input batches.
|
||||
let input_persist_context = if persist_context == PersistContext::SteeredUserInput
|
||||
&& matches!(input_item.as_ref(), TurnInput::FunctionCallOutput(_))
|
||||
@@ -1204,7 +1247,7 @@ pub(crate) async fn run_hooks_and_record_inputs(
|
||||
}
|
||||
// The batch has left the input queue. Persist every accepted item before the
|
||||
// optional lookup can suspend this task long enough for an interrupt to abort it.
|
||||
if !input.is_empty() {
|
||||
if !initial_input.is_empty() || !pending_input.is_empty() {
|
||||
super::file_audio::resolve_pending(sess).await;
|
||||
}
|
||||
if let Some(error) = input_error {
|
||||
@@ -1214,28 +1257,60 @@ pub(crate) async fn run_hooks_and_record_inputs(
|
||||
Ok(blocked_input && !accepted_user_input)
|
||||
}
|
||||
|
||||
/// Yields usable input in order, then reports unsupported audio after valid siblings can be recorded.
|
||||
pub(crate) async fn record_delivered_additional_context(
|
||||
sess: &Session,
|
||||
turn_context: &TurnContext,
|
||||
model_info: &ModelInfo,
|
||||
context: &SubmittedAdditionalContext,
|
||||
) {
|
||||
let items = merge_submitted_context(sess, turn_context, context).await;
|
||||
if !items.is_empty() {
|
||||
sess.record_annotated_conversation_items(turn_context, model_info, items)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn merge_submitted_context(
|
||||
sess: &Session,
|
||||
turn_context: &TurnContext,
|
||||
context: &SubmittedAdditionalContext,
|
||||
) -> Vec<ResponseItemEnvelope> {
|
||||
let items = sess.state.lock().await.additional_context.merge(
|
||||
context.0.clone(),
|
||||
sess.additional_context_format(),
|
||||
&turn_context.session_telemetry,
|
||||
&sess.item_id_generator,
|
||||
);
|
||||
items
|
||||
.into_iter()
|
||||
.map(|item| sess.annotate_client_response_item(item))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Yields usable input with its context in order, then reports unsupported audio after valid siblings can be recorded.
|
||||
pub(crate) fn recordable_inputs<'a>(
|
||||
sess: &Session,
|
||||
model_info: &ModelInfo,
|
||||
input: &'a [TurnInput],
|
||||
) -> impl Iterator<Item = CodexResult<Cow<'a, TurnInput>>> {
|
||||
let rejected_audio = !model_info.input_modalities.contains(&InputModality::Audio)
|
||||
&& input.iter().any(turn_input_has_uploaded_audio_file);
|
||||
// Only finalized evidence may enter reusable Guardian history.
|
||||
let items: &[TurnInput] = if sess
|
||||
.services
|
||||
.thread_extension_data
|
||||
.get::<crate::guardian::PendingReviewContext>()
|
||||
.is_some()
|
||||
{
|
||||
&[]
|
||||
} else {
|
||||
input
|
||||
};
|
||||
items
|
||||
initial_input: &'a [TurnInput],
|
||||
pending_input: &'a [InputQueueEntry],
|
||||
initial_context: Option<&'a SubmittedAdditionalContext>,
|
||||
) -> impl Iterator<Item = CodexResult<(Cow<'a, TurnInput>, Option<&'a SubmittedAdditionalContext>)>>
|
||||
{
|
||||
let input = initial_input
|
||||
.iter()
|
||||
.filter_map(move |item| {
|
||||
.enumerate()
|
||||
.map(move |(index, item)| (item, initial_context.filter(|_| index == 0)))
|
||||
.chain(pending_input.iter().map(InputQueueEntry::input_and_context));
|
||||
let rejected_audio = !model_info.input_modalities.contains(&InputModality::Audio)
|
||||
&& input
|
||||
.clone()
|
||||
.any(|(item, _)| turn_input_has_uploaded_audio_file(item));
|
||||
let can_record = can_record_input(sess);
|
||||
input
|
||||
.filter_map(move |(item, context)| {
|
||||
if !can_record {
|
||||
return None;
|
||||
}
|
||||
let mut item = Cow::Borrowed(item);
|
||||
if rejected_audio
|
||||
&& turn_input_has_uploaded_audio_file(&item)
|
||||
@@ -1246,11 +1321,19 @@ pub(crate) fn recordable_inputs<'a>(
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some(Ok(item))
|
||||
Some(Ok((item, context)))
|
||||
})
|
||||
.chain(rejected_audio.then(|| Err(unsupported_uploaded_audio_error())))
|
||||
}
|
||||
|
||||
fn can_record_input(sess: &Session) -> bool {
|
||||
// Only finalized evidence may enter reusable Guardian history.
|
||||
sess.services
|
||||
.thread_extension_data
|
||||
.get::<crate::guardian::PendingReviewContext>()
|
||||
.is_none()
|
||||
}
|
||||
|
||||
fn turn_input_has_uploaded_audio_file(input: &TurnInput) -> bool {
|
||||
matches!(input, TurnInput::UserInput { content, .. } if content.iter().any(ResolvedUserInput::is_uploaded_audio_file))
|
||||
}
|
||||
@@ -1262,9 +1345,9 @@ fn unsupported_uploaded_audio_error() -> CodexErr {
|
||||
}
|
||||
|
||||
/// Extensions and telemetry consume the protocol view, including the original mention and path labels.
|
||||
fn input_for_extensions(input: &[TurnInput]) -> Vec<UserInput> {
|
||||
fn input_for_extensions<'a>(input: impl IntoIterator<Item = &'a TurnInput>) -> Vec<UserInput> {
|
||||
input
|
||||
.iter()
|
||||
.into_iter()
|
||||
.filter_map(|item| match item {
|
||||
TurnInput::UserInput { content, .. } => Some(content.as_slice()),
|
||||
TurnInput::ResponseItem(_)
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
use super::TurnInput;
|
||||
use super::UserInputMetadata;
|
||||
use super::inject::validate_injected_response_item;
|
||||
use super::input_queue::InputQueueEntry;
|
||||
use super::input_queue::SubmittedAdditionalContext;
|
||||
use super::session::Session;
|
||||
use super::session::SessionConfiguration;
|
||||
use super::session::SessionSettingsUpdate;
|
||||
@@ -322,7 +324,8 @@ async fn handle_inner(
|
||||
request
|
||||
.request
|
||||
.additional_context
|
||||
.values()
|
||||
.iter()
|
||||
.flat_map(BTreeMap::values)
|
||||
.try_for_each(AdditionalContextEntry::validate)
|
||||
.map_err(|message| CodexErr::InvalidRequest(message.to_owned()))?;
|
||||
}
|
||||
@@ -483,7 +486,7 @@ pub(super) async fn start_or_steer(
|
||||
mut input,
|
||||
thread_settings,
|
||||
start,
|
||||
additional_context,
|
||||
mut additional_context,
|
||||
responsesapi_client_metadata,
|
||||
..
|
||||
} = request;
|
||||
@@ -518,7 +521,7 @@ pub(super) async fn start_or_steer(
|
||||
.steer_input(
|
||||
&mut input,
|
||||
sender_messages.as_ref(),
|
||||
additional_context.clone(),
|
||||
&mut additional_context,
|
||||
/*expected_turn_id*/ None,
|
||||
settings.required_active_final_output_json_schema(),
|
||||
responsesapi_client_metadata.clone(),
|
||||
@@ -568,8 +571,12 @@ pub(super) async fn start_or_steer(
|
||||
session
|
||||
.maybe_emit_model_warnings_for_turn(turn_context.as_ref())
|
||||
.await;
|
||||
let mut task_input =
|
||||
merge_additional_context_input(session, &turn_context, additional_context).await;
|
||||
if let Some(context) = additional_context {
|
||||
turn_context
|
||||
.extension_data
|
||||
.insert(SubmittedAdditionalContext(context));
|
||||
}
|
||||
let mut task_input = Vec::new();
|
||||
if has_explicit_input {
|
||||
task_input.push(
|
||||
pending_turn_input(
|
||||
@@ -728,8 +735,8 @@ async fn start_if_idle(
|
||||
.maybe_emit_model_warnings_for_turn(turn_context.as_ref())
|
||||
.await;
|
||||
|
||||
let mut task_input =
|
||||
merge_additional_context_input(session, &turn_context, additional_context).await;
|
||||
let mut additional_context = additional_context.map(SubmittedAdditionalContext);
|
||||
let mut task_input = Vec::new();
|
||||
let sender_messages = capture_sender_user_messages(session, &mut input).await;
|
||||
match kind {
|
||||
TurnStartKind::User => {
|
||||
@@ -748,28 +755,31 @@ async fn start_if_idle(
|
||||
TurnStartKind::Automatic | TurnStartKind::Recovery => {
|
||||
// Empty automatic user input resumes sampling without a new message.
|
||||
if !matches!(&input, SubmittedTurnInput::UserInput { .. }) {
|
||||
task_input.push(
|
||||
pending_turn_input(
|
||||
session,
|
||||
input,
|
||||
sender_messages.as_ref(),
|
||||
&turn_context.sub_id,
|
||||
input_metadata,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
// Automatic turns drain their queued input before sampling. Keep the
|
||||
// context and the input it describes together for the first request.
|
||||
session
|
||||
.input_queue
|
||||
.extend_pending_input_for_turn_state(
|
||||
turn_state.as_ref(),
|
||||
std::mem::take(&mut task_input),
|
||||
vec![InputQueueEntry {
|
||||
input: pending_turn_input(
|
||||
session,
|
||||
input,
|
||||
sender_messages.as_ref(),
|
||||
&turn_context.sub_id,
|
||||
input_metadata,
|
||||
)
|
||||
.await,
|
||||
additional_context: additional_context.take(),
|
||||
}],
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(context) = additional_context {
|
||||
turn_context.extension_data.insert(context);
|
||||
}
|
||||
Ok(session
|
||||
.start_task(turn_context, task_input, RegularTask::new())
|
||||
.await)
|
||||
@@ -791,7 +801,7 @@ async fn steer(
|
||||
mut input,
|
||||
thread_settings,
|
||||
start,
|
||||
additional_context,
|
||||
mut additional_context,
|
||||
responsesapi_client_metadata,
|
||||
..
|
||||
} = request;
|
||||
@@ -813,7 +823,7 @@ async fn steer(
|
||||
.steer_input(
|
||||
&mut input,
|
||||
/*sender_messages*/ None,
|
||||
additional_context,
|
||||
&mut additional_context,
|
||||
Some(expected_turn_id.as_str()),
|
||||
settings.required_active_final_output_json_schema(),
|
||||
responsesapi_client_metadata,
|
||||
@@ -873,7 +883,7 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
fn additional_context_format(&self) -> AdditionalContextFormat {
|
||||
pub(super) fn additional_context_format(&self) -> AdditionalContextFormat {
|
||||
if self.enabled(Feature::UntrustedAsToolOutput) {
|
||||
AdditionalContextFormat::ToolOutputs
|
||||
} else {
|
||||
@@ -1027,7 +1037,7 @@ impl Session {
|
||||
&self,
|
||||
input: &mut SubmittedTurnInput,
|
||||
sender_messages: Option<&GuardianSenderMessages>,
|
||||
additional_context: BTreeMap<String, AdditionalContextEntry>,
|
||||
additional_context: &mut Option<BTreeMap<String, AdditionalContextEntry>>,
|
||||
expected_turn_id: Option<&str>,
|
||||
required_final_output_json_schema: Option<&Value>,
|
||||
responsesapi_client_metadata: ResponsesApiClientMetadataUpdate,
|
||||
@@ -1096,10 +1106,6 @@ impl Session {
|
||||
{
|
||||
return Err(NotSubmittedReason::ActiveTurnOutputSchemaMismatch);
|
||||
}
|
||||
let mut pending_input =
|
||||
merge_additional_context_input(self, &active_task.turn_context, additional_context)
|
||||
.await;
|
||||
|
||||
active_task
|
||||
.turn_context
|
||||
.turn_metadata_state
|
||||
@@ -1125,13 +1131,15 @@ impl Session {
|
||||
.await
|
||||
}
|
||||
};
|
||||
pending_input.push(input);
|
||||
self.bind_realtime_handoffs_to_turn(&pending_input, active_turn_id)
|
||||
self.bind_realtime_handoffs_to_turn(std::slice::from_ref(&input), active_turn_id)
|
||||
.await;
|
||||
self.input_queue
|
||||
.extend_pending_input_and_notify_for_turn_state(
|
||||
active_turn.turn_state.as_ref(),
|
||||
pending_input,
|
||||
vec![InputQueueEntry {
|
||||
input,
|
||||
additional_context: additional_context.take().map(SubmittedAdditionalContext),
|
||||
}],
|
||||
)
|
||||
.await;
|
||||
Ok((
|
||||
@@ -1141,28 +1149,6 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
async fn merge_additional_context_input(
|
||||
session: &Session,
|
||||
turn_context: &TurnContext,
|
||||
additional_context: BTreeMap<String, AdditionalContextEntry>,
|
||||
) -> Vec<TurnInput> {
|
||||
let format = session.additional_context_format();
|
||||
let additional_context_input = {
|
||||
let mut state = session.state.lock().await;
|
||||
state.additional_context.merge(
|
||||
additional_context,
|
||||
format,
|
||||
&turn_context.session_telemetry,
|
||||
&session.item_id_generator,
|
||||
)
|
||||
};
|
||||
additional_context_input
|
||||
.into_iter()
|
||||
.map(|item| session.annotate_client_response_item(item))
|
||||
.map(TurnInput::ResponseItem)
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn capture_sender_user_messages(
|
||||
session: &Session,
|
||||
input: &mut SubmittedTurnInput,
|
||||
|
||||
@@ -238,7 +238,7 @@ async fn start_only_rejects_active_turn_without_injecting() {
|
||||
submission
|
||||
);
|
||||
assert_eq!(
|
||||
Vec::<TurnInput>::new(),
|
||||
Vec::<InputQueueEntry>::new(),
|
||||
session
|
||||
.input_queue
|
||||
.get_pending_input(&session.active_turn)
|
||||
@@ -325,7 +325,7 @@ async fn recovery_rejects_active_turn_without_injecting_or_applying_settings() {
|
||||
.get_pending_input(&session.active_turn)
|
||||
.await
|
||||
.0,
|
||||
Vec::<TurnInput>::new()
|
||||
Vec::<InputQueueEntry>::new()
|
||||
);
|
||||
|
||||
session.abort_all_tasks(TurnAbortReason::Interrupted).await;
|
||||
@@ -399,7 +399,7 @@ async fn start_only_rejects_current_plan_before_validating_settings() {
|
||||
assert_eq!(session.thread_settings_snapshot().await, desired_settings);
|
||||
assert!(session.active_turn.lock().await.is_none());
|
||||
assert_eq!(
|
||||
Vec::<TurnInput>::new(),
|
||||
Vec::<InputQueueEntry>::new(),
|
||||
session
|
||||
.input_queue
|
||||
.get_pending_input(&session.active_turn)
|
||||
@@ -673,7 +673,7 @@ async fn automatic_admission_rechecks_plan_mode_without_committing_sparse_settin
|
||||
.get_pending_input(&session.active_turn)
|
||||
.await
|
||||
.0,
|
||||
Vec::<TurnInput>::new()
|
||||
Vec::<InputQueueEntry>::new()
|
||||
);
|
||||
|
||||
// The rejected candidate is valid and would have real runtime effects if
|
||||
@@ -767,7 +767,7 @@ async fn admission_revalidates_constraints_before_committing(kind: TurnStartKind
|
||||
.get_pending_input(&session.active_turn)
|
||||
.await
|
||||
.0,
|
||||
Vec::<TurnInput>::new()
|
||||
Vec::<InputQueueEntry>::new()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1129,7 +1129,10 @@ async fn steer_preserves_request_origin(
|
||||
.input_queue
|
||||
.get_pending_input(&session.active_turn)
|
||||
.await
|
||||
.0;
|
||||
.0
|
||||
.into_iter()
|
||||
.map(|entry| entry.input)
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
pending,
|
||||
vec![TurnInput::UserInput {
|
||||
@@ -1214,7 +1217,7 @@ async fn scored_prompt_keeps_its_submission_item_id() {
|
||||
content,
|
||||
client_id,
|
||||
metadata,
|
||||
} = input
|
||||
} = input.input
|
||||
else {
|
||||
panic!("expected admitted user input");
|
||||
};
|
||||
|
||||
@@ -13,10 +13,12 @@ use crate::agent::host::AgentTurnAdmission;
|
||||
use crate::agent::host::AgentTurnAdmissionRequest;
|
||||
use crate::hook_environment_executor::with_environment_hooks;
|
||||
use crate::hook_runtime::record_pending_input;
|
||||
use crate::session::SubmittedAdditionalContext;
|
||||
use crate::session::TurnInput;
|
||||
use crate::session::session::Session;
|
||||
use crate::session::turn::record_delivered_additional_context;
|
||||
use crate::session::turn::recordable_inputs;
|
||||
use crate::session::turn::run_hooks_and_record_inputs;
|
||||
use crate::session::turn::run_hooks_and_record_input_entries;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
use crate::state::TaskKind;
|
||||
use crate::state::TurnState;
|
||||
@@ -84,21 +86,21 @@ impl Session {
|
||||
input: Option<Vec<TurnInput>>,
|
||||
abort_reason: Option<&TurnAbortReason>,
|
||||
) -> Result<()> {
|
||||
let Some(mut input) = input else {
|
||||
let Some(input) = input else {
|
||||
return Ok(());
|
||||
};
|
||||
input.extend(
|
||||
self.input_queue
|
||||
.take_pending_input_for_turn_state(turn_state)
|
||||
.await,
|
||||
);
|
||||
let pending_input = self
|
||||
.input_queue
|
||||
.take_pending_input_for_turn_state(turn_state)
|
||||
.await;
|
||||
let model_info = context.capture_current_model_info();
|
||||
if abort_reason != Some(&TurnAbortReason::Interrupted) {
|
||||
run_hooks_and_record_inputs(
|
||||
run_hooks_and_record_input_entries(
|
||||
self,
|
||||
context,
|
||||
&model_info,
|
||||
&input,
|
||||
Some(&input),
|
||||
&pending_input,
|
||||
PersistContext::Standard,
|
||||
)
|
||||
.await?;
|
||||
@@ -106,8 +108,28 @@ impl Session {
|
||||
}
|
||||
|
||||
let mut input_error = None;
|
||||
for item in recordable_inputs(self, &model_info, &input) {
|
||||
let item = item?;
|
||||
if input.is_empty() {
|
||||
run_hooks_and_record_input_entries(
|
||||
self,
|
||||
context,
|
||||
&model_info,
|
||||
Some(&[]),
|
||||
/*pending_input*/ &[],
|
||||
PersistContext::Standard,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
let initial_context = context
|
||||
.extension_data
|
||||
.remove::<SubmittedAdditionalContext>();
|
||||
for item in recordable_inputs(
|
||||
self,
|
||||
&model_info,
|
||||
&input,
|
||||
&pending_input,
|
||||
initial_context.as_deref(),
|
||||
) {
|
||||
let (item, additional_context) = item?;
|
||||
// The user must resubmit a prompt that still needs a submit hook.
|
||||
if matches!(item.as_ref(), TurnInput::UserInput { .. })
|
||||
&& with_environment_hooks(
|
||||
@@ -125,6 +147,10 @@ impl Session {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(additional_context) = additional_context {
|
||||
record_delivered_additional_context(self, context, &model_info, additional_context)
|
||||
.await;
|
||||
}
|
||||
if let Err(error) = record_pending_input(
|
||||
self,
|
||||
context,
|
||||
|
||||
@@ -34,11 +34,13 @@ use crate::codex_thread::BackgroundTerminalInfo;
|
||||
use crate::config::Config;
|
||||
use crate::context::ContextualUserFragment;
|
||||
use crate::hook_runtime::run_turn_interrupt_hooks;
|
||||
use crate::session::InputQueueEntry;
|
||||
use crate::session::SamplingInterrupt;
|
||||
use crate::session::TurnInput;
|
||||
use crate::session::session::Session;
|
||||
use crate::session::turn::McpStartupRequirements;
|
||||
use crate::session::turn::run_hooks_and_record_inputs;
|
||||
use crate::session::turn::merge_submitted_context;
|
||||
use crate::session::turn::run_hooks_and_record_input_entries;
|
||||
use crate::session::turn_context::NewTurnContextOptions;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
use crate::state::ActiveTurn;
|
||||
@@ -805,7 +807,7 @@ impl Session {
|
||||
&& !self.work_budget().is_exhausted();
|
||||
let finishing = CancellationToken::new();
|
||||
let finish_recording = finishing.clone().drop_guard();
|
||||
let (turn_state, unadmitted_task, mut pending_input, outcome_handoff, sampling_interrupted) = {
|
||||
let (turn_state, unadmitted_task, initial_input, outcome_handoff, sampling_interrupted) = {
|
||||
let mut active = self.active_turn.lock().await;
|
||||
let active_turn = active.as_mut()?;
|
||||
if !Arc::ptr_eq(&active_turn.task.as_ref()?.turn_context, &turn_context) {
|
||||
@@ -830,7 +832,7 @@ impl Session {
|
||||
Arc::clone(&active_turn.turn_state),
|
||||
(task._agent_execution_guard.is_none() && task.pending_initial_input.is_some())
|
||||
.then_some(task.task),
|
||||
task.pending_initial_input.unwrap_or_default(),
|
||||
task.pending_initial_input,
|
||||
task.outcome_handoff,
|
||||
sampling_interrupted,
|
||||
)
|
||||
@@ -839,11 +841,10 @@ impl Session {
|
||||
.turn_metadata_state
|
||||
.cancel_git_enrichment_task();
|
||||
let last_agent_message = regular_output.and_then(|output| output.last_agent_message);
|
||||
pending_input.extend(
|
||||
self.input_queue
|
||||
.take_pending_input_for_turn_state(turn_state.as_ref())
|
||||
.await,
|
||||
);
|
||||
let pending_input = self
|
||||
.input_queue
|
||||
.take_pending_input_for_turn_state(turn_state.as_ref())
|
||||
.await;
|
||||
let (
|
||||
turn_had_memory_citation,
|
||||
turn_tool_calls,
|
||||
@@ -858,11 +859,11 @@ impl Session {
|
||||
std::mem::take(&mut ts.token_usage_by_model),
|
||||
)
|
||||
};
|
||||
let model_info = turn_context.capture_current_model_info();
|
||||
let record_result = run_hooks_and_record_inputs(
|
||||
let record_result = run_hooks_and_record_input_entries(
|
||||
self,
|
||||
&turn_context,
|
||||
&model_info,
|
||||
&turn_context.capture_current_model_info(),
|
||||
initial_input.as_deref(),
|
||||
&pending_input,
|
||||
PersistContext::Standard,
|
||||
)
|
||||
@@ -1283,9 +1284,15 @@ impl Session {
|
||||
.await
|
||||
{
|
||||
match input {
|
||||
TurnInput::ResponseItem(item)
|
||||
if matches!(item.item, ResponseItem::AgentMessage { .. }) =>
|
||||
{
|
||||
InputQueueEntry {
|
||||
input: TurnInput::ResponseItem(item),
|
||||
additional_context,
|
||||
} if matches!(item.item, ResponseItem::AgentMessage { .. }) => {
|
||||
if let Some(context) = additional_context {
|
||||
pending_mail.extend(
|
||||
merge_submitted_context(self, &task.turn_context, &context).await,
|
||||
);
|
||||
}
|
||||
pending_mail.push(item);
|
||||
}
|
||||
input => remaining_input.push(input),
|
||||
|
||||
@@ -15,7 +15,7 @@ use crate::session::TurnInput;
|
||||
use crate::session::session::Session;
|
||||
use crate::session::startup_prewarm::SessionStartupPrewarmResolution;
|
||||
use crate::session::turn::McpStartupRequirements;
|
||||
use crate::session::turn::run_hooks_and_record_inputs;
|
||||
use crate::session::turn::run_hooks_and_record_input_entries;
|
||||
use crate::session::turn::run_turn;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
use crate::state::TaskKind;
|
||||
@@ -93,11 +93,12 @@ impl SessionTask for RegularTask {
|
||||
.await;
|
||||
let prewarmed_client_session = match prewarmed_client_session {
|
||||
SessionStartupPrewarmResolution::Cancelled => {
|
||||
let _ = run_hooks_and_record_inputs(
|
||||
let _ = run_hooks_and_record_input_entries(
|
||||
&sess,
|
||||
&ctx,
|
||||
&ctx.capture_current_model_info(),
|
||||
&input,
|
||||
Some(&input),
|
||||
/*pending_input*/ &[],
|
||||
PersistContext::Standard,
|
||||
)
|
||||
.await;
|
||||
@@ -111,11 +112,12 @@ impl SessionTask for RegularTask {
|
||||
if let Some(resolution) = ctx.extension_data.get::<SavedProgramResolution>()
|
||||
&& let Err(error) = resolution.validate()
|
||||
{
|
||||
let _ = run_hooks_and_record_inputs(
|
||||
let _ = run_hooks_and_record_input_entries(
|
||||
&sess,
|
||||
&ctx,
|
||||
&ctx.capture_current_model_info(),
|
||||
&input,
|
||||
Some(&input),
|
||||
/*pending_input*/ &[],
|
||||
PersistContext::Standard,
|
||||
)
|
||||
.await;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
@@ -29,6 +30,8 @@ use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::openai_models::ModelsResponse;
|
||||
use codex_protocol::openai_models::ReasoningEffort;
|
||||
use codex_protocol::permissions::NetworkSandboxPolicy;
|
||||
use codex_protocol::protocol::AdditionalContextEntry;
|
||||
use codex_protocol::protocol::AdditionalContextKind;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::HookEventName;
|
||||
@@ -1462,6 +1465,93 @@ async fn session_start_hook_skips_persistence_for_non_local_thread_store() -> Re
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn session_start_stop_does_not_deliver_submitted_context() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
let server = start_mock_server().await;
|
||||
let responses = mount_sse_sequence(
|
||||
&server,
|
||||
vec![
|
||||
sse(vec![ev_completed("resp-1")]),
|
||||
sse(vec![ev_completed("resp-2")]),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
let hook_gate = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
|
||||
let port = hook_gate.local_addr()?.port();
|
||||
let test = test_codex()
|
||||
.with_pre_build_hook(move |home| {
|
||||
let script = home.join("stop_session_start.py");
|
||||
fs::write(
|
||||
&script,
|
||||
format!(
|
||||
r#"import json, socket
|
||||
with socket.create_connection(("127.0.0.1", {port})) as gate:
|
||||
gate.recv(1)
|
||||
print(json.dumps({{"continue": False}}))
|
||||
"#
|
||||
),
|
||||
)
|
||||
.expect("write hook script");
|
||||
let hooks = json!({"hooks": {"SessionStart": [{"hooks": [{
|
||||
"type": "command", "command": format!("python3 {}", script.display())
|
||||
}]}]}});
|
||||
fs::write(home.join("hooks.json"), hooks.to_string()).expect("write hook config");
|
||||
})
|
||||
.with_config(trust_discovered_hooks)
|
||||
// SessionStart must run the Python hook installed in the host test home.
|
||||
.build(&server)
|
||||
.await?;
|
||||
let context = BTreeMap::from([(
|
||||
"continuation".to_owned(),
|
||||
AdditionalContextEntry::new(
|
||||
"resume the task".to_owned(),
|
||||
AdditionalContextKind::Application,
|
||||
),
|
||||
)]);
|
||||
test.codex
|
||||
.start_or_steer_turn(
|
||||
TurnInputRequest::user_input(vec![UserInput::Text {
|
||||
text: "stopped prompt".to_owned(),
|
||||
text_elements: Vec::new(),
|
||||
}])
|
||||
.with_additional_context(context.clone()),
|
||||
)
|
||||
.await?;
|
||||
let (held_hook, _) = timeout(Duration::from_secs(10), hook_gate.accept()).await??;
|
||||
test.codex
|
||||
.start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
|
||||
text: "queued steer".to_owned(),
|
||||
text_elements: Vec::new(),
|
||||
}]))
|
||||
.await?;
|
||||
drop(held_hook);
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::TurnComplete(_))
|
||||
})
|
||||
.await;
|
||||
|
||||
test.codex
|
||||
.start_turn_if_idle(
|
||||
TurnInputRequest::user_input(Vec::new()).with_additional_context(context),
|
||||
)
|
||||
.await?;
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::TurnComplete(_))
|
||||
})
|
||||
.await;
|
||||
|
||||
let requests = responses.requests();
|
||||
assert_eq!(requests.len(), 2);
|
||||
let texts = requests[0].message_input_texts("user");
|
||||
assert!(texts.iter().any(|text| text == "queued steer"));
|
||||
assert!(!texts.iter().any(|text| text == "stopped prompt"));
|
||||
assert!(!requests[0].has_content_kinds(&["additional_content.continuation"]));
|
||||
assert!(requests[1].has_content_kinds(&["additional_content.continuation"]));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn session_end_flushes_transcript_and_ignores_control_output() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
@@ -2892,12 +2982,24 @@ async fn blocked_queued_prompt_does_not_strand_earlier_accepted_prompt() -> Resu
|
||||
// inside message completion instead, so both steers are in the same pending batch.
|
||||
timeout(Duration::from_secs(30), gate.entered.notified()).await?;
|
||||
|
||||
for text in ["accepted queued prompt", "blocked queued prompt"] {
|
||||
for (text, context) in [
|
||||
("accepted queued prompt", "accepted-context-value"),
|
||||
("blocked queued prompt", "blocked-context-value"),
|
||||
] {
|
||||
test.codex
|
||||
.start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
|
||||
text: text.to_string(),
|
||||
text_elements: Vec::new(),
|
||||
}]))
|
||||
.start_or_steer_turn(
|
||||
TurnInputRequest::user_input(vec![UserInput::Text {
|
||||
text: text.to_string(),
|
||||
text_elements: Vec::new(),
|
||||
}])
|
||||
.with_additional_context(BTreeMap::from([(
|
||||
"source".to_string(),
|
||||
AdditionalContextEntry::new(
|
||||
context.to_string(),
|
||||
AdditionalContextKind::Untrusted,
|
||||
),
|
||||
)])),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
@@ -2929,9 +3031,20 @@ async fn blocked_queued_prompt_does_not_strand_earlier_accepted_prompt() -> Resu
|
||||
});
|
||||
let rollout = fs::read_to_string(test.codex.rollout_path().expect("rollout path"))?;
|
||||
assert!(!rollout.contains("blocked queued prompt"));
|
||||
assert!(!rollout.contains("blocked-context-value"));
|
||||
assert!(rollout.contains("accepted queued prompt"));
|
||||
|
||||
let second_user_texts = requests[1].message_input_texts("user");
|
||||
let second_request = requests[1].body_json();
|
||||
assert!(
|
||||
second_request["input"]
|
||||
.as_array()
|
||||
.expect("request input")
|
||||
.iter()
|
||||
.any(|item| item["type"] == "function_call_output"
|
||||
&& item["name"] == "source"
|
||||
&& item["output"] == "accepted-context-value")
|
||||
);
|
||||
assert!(
|
||||
second_user_texts.contains(&"accepted queued prompt".to_string()),
|
||||
"second request should include the accepted queued prompt",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Host capacity is acquired by the ordinary task startup path, including on a root.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -26,6 +27,8 @@ use codex_protocol::error::Result as CodexResult;
|
||||
use codex_protocol::items::TurnItem;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::models::input_text;
|
||||
use codex_protocol::protocol::AdditionalContextEntry;
|
||||
use codex_protocol::protocol::AdditionalContextKind;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::MultiAgentVersion;
|
||||
use codex_protocol::protocol::Op;
|
||||
@@ -484,6 +487,73 @@ async fn suspending_root_during_roster_read_preserves_the_recoverable_prompt() -
|
||||
suspend_and_check_recoverable_prompt(&test, &server, turn_id, prompt).await
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn repro_16086_context_only_host_handoff() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
let server = responses::start_mock_server().await;
|
||||
let builder = test_codex().with_config(|config| {
|
||||
config
|
||||
.features
|
||||
.enable(Feature::MultiAgentV2)
|
||||
.expect("enable multi-agent V2");
|
||||
});
|
||||
let roster = Arc::new(PendingRosterRead::default());
|
||||
let (test, mut attempts) = root_with_admission(&server, builder, Some(roster.clone())).await?;
|
||||
let context = BTreeMap::from([(
|
||||
"handoff".to_owned(),
|
||||
AdditionalContextEntry::new(
|
||||
"roster case 16086".to_owned(),
|
||||
AdditionalContextKind::Application,
|
||||
),
|
||||
)]);
|
||||
let submitted = test
|
||||
.codex
|
||||
.start_or_steer_turn(
|
||||
TurnInputRequest::user_input(Vec::new()).with_additional_context(context),
|
||||
)
|
||||
.await?;
|
||||
let TurnInputSubmission::Started { turn_id, .. } = submitted else {
|
||||
anyhow::bail!("expected a new turn, got {submitted:?}");
|
||||
};
|
||||
let (request, grant) = tokio::time::timeout(Duration::from_secs(10), attempts.recv())
|
||||
.await?
|
||||
.context("admission provider dropped")?;
|
||||
assert_eq!(request.turn_id, turn_id);
|
||||
assert!(grant.send(Ok(AgentExecutionGuard::new(()))).is_ok());
|
||||
tokio::time::timeout(Duration::from_secs(10), roster.0.notified()).await?;
|
||||
|
||||
assert_eq!(
|
||||
test.codex.suspend_turn_and_shutdown().await?,
|
||||
SuspendTurnOutcome::Suspended { turn_id }
|
||||
);
|
||||
let history = test
|
||||
.thread_store
|
||||
.load_latest_model_context(LoadThreadHistoryParams {
|
||||
thread_id: test.session_configured.thread_id,
|
||||
include_archived: false,
|
||||
})
|
||||
.await?;
|
||||
let expected = vec![input_text("<handoff>roster case 16086</handoff>")];
|
||||
let saved_context_items = history
|
||||
.items
|
||||
.iter()
|
||||
.filter(|entry| {
|
||||
let RolloutItem::ResponseItem(envelope) = entry else {
|
||||
return false;
|
||||
};
|
||||
matches!(&envelope.item, ResponseItem::Message { role, content, .. }
|
||||
if role == "developer" && content == &expected)
|
||||
})
|
||||
.count();
|
||||
let requests = responses::received_responses_requests(&server).await;
|
||||
assert!(requests.is_empty(), "expected suspension before inference");
|
||||
assert_eq!(
|
||||
saved_context_items, 1,
|
||||
"context-only submission must survive host handoff"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn suspend_and_check_recoverable_prompt(
|
||||
test: &TestCodex,
|
||||
server: &MockServer,
|
||||
|
||||
@@ -25,6 +25,8 @@ use codex_protocol::config_types::CollaborationMode;
|
||||
use codex_protocol::config_types::ModeKind;
|
||||
use codex_protocol::config_types::Settings;
|
||||
use codex_protocol::items::TurnItem;
|
||||
use codex_protocol::protocol::AdditionalContextEntry;
|
||||
use codex_protocol::protocol::AdditionalContextKind;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::InterAgentCommunication;
|
||||
@@ -1175,14 +1177,20 @@ async fn continue_turn_if_idle_starts_new_turn_with_internal_input() {
|
||||
let submission = test
|
||||
.codex
|
||||
.continue_turn_if_idle(
|
||||
TurnInputRequest::new(TurnInput::ResponseItem(input.clone())).on_start(
|
||||
TurnStartOptions {
|
||||
TurnInputRequest::new(TurnInput::ResponseItem(input.clone()))
|
||||
.with_additional_context(BTreeMap::from([(
|
||||
"recovery_info".to_string(),
|
||||
AdditionalContextEntry::new(
|
||||
"active tab".to_string(),
|
||||
AdditionalContextKind::Untrusted,
|
||||
),
|
||||
)]))
|
||||
.on_start(TurnStartOptions {
|
||||
final_output_json_schema: Some(schema.clone()),
|
||||
service_tier: Some("priority".to_string()),
|
||||
root_turn_id: Some("originating-turn".to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
}),
|
||||
previous_turn_id.clone(),
|
||||
)
|
||||
.await
|
||||
@@ -1200,6 +1208,12 @@ async fn continue_turn_if_idle_starts_new_turn_with_internal_input() {
|
||||
ThreadIdle::wait(&test.codex).await;
|
||||
let request = mock.single_request();
|
||||
assert!(request.has_content_kinds(&["daemon_recovery.internal_context"]));
|
||||
assert!(
|
||||
request
|
||||
.inputs_of_type("function_call_output")
|
||||
.iter()
|
||||
.any(|item| item["name"] == "recovery_info" && item["output"] == "active tab")
|
||||
);
|
||||
let metadata: Value = serde_json::from_str(
|
||||
&request
|
||||
.header("x-codex-turn-metadata")
|
||||
|
||||
@@ -62,11 +62,10 @@ pub(crate) fn continuation_request(
|
||||
) -> TurnInputRequest {
|
||||
let steering = goal_context_input_item(continuation_prompt(goal, update_plan_enabled));
|
||||
TurnInputRequest::new(TurnInput::ResponseItem(steering)).with_additional_context(
|
||||
[(
|
||||
std::collections::BTreeMap::from([(
|
||||
GOAL_OBJECTIVE_CONTEXT.to_owned(),
|
||||
AdditionalContextEntry::new(goal.objective.clone(), AdditionalContextKind::Untrusted),
|
||||
)]
|
||||
.into(),
|
||||
)]),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ fn disabled_checklist_preserves_goal_text_that_mentions_the_tool() {
|
||||
let objective = "Inspect update_plan.\n\n## Plan tool\nThis is user task data.";
|
||||
let request =
|
||||
steering::continuation_request(&test_goal(objective), /*update_plan_enabled*/ false);
|
||||
let entry = &request.additional_context["goal_objective"];
|
||||
let entry = &request.additional_context.as_ref().expect("goal context")["goal_objective"];
|
||||
assert_eq!(entry.kind, AdditionalContextKind::Untrusted);
|
||||
assert_eq!(entry.value, objective);
|
||||
let TurnInput::ResponseItem(ResponseItem::Message { content, .. }) = request.input else {
|
||||
|
||||
@@ -117,7 +117,8 @@ pub struct TurnInputRequest<I = UserInput> {
|
||||
pub user_input_extension_data: BTreeMap<String, Value>,
|
||||
pub thread_settings: ThreadSettingsOverrides,
|
||||
pub start: TurnStartOptions,
|
||||
pub additional_context: BTreeMap<String, AdditionalContextEntry>,
|
||||
/// Replaces submitted client context when present; omission preserves it and an empty map clears it.
|
||||
pub additional_context: Option<BTreeMap<String, AdditionalContextEntry>>,
|
||||
pub responsesapi_client_metadata: ResponsesApiClientMetadataUpdate,
|
||||
pub trace: Option<W3cTraceContext>,
|
||||
}
|
||||
@@ -178,7 +179,7 @@ impl<I> TurnInputRequest<I> {
|
||||
user_input_extension_data: BTreeMap::new(),
|
||||
thread_settings: ThreadSettingsOverrides::default(),
|
||||
start: TurnStartOptions::default(),
|
||||
additional_context: BTreeMap::new(),
|
||||
additional_context: None,
|
||||
responsesapi_client_metadata: ResponsesApiClientMetadataUpdate::default(),
|
||||
trace: None,
|
||||
}
|
||||
@@ -209,12 +210,12 @@ impl<I> TurnInputRequest<I> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Context merged whether this request starts or steers.
|
||||
/// Context submitted with the input whether this request starts or steers.
|
||||
pub fn with_additional_context(
|
||||
mut self,
|
||||
additional_context: BTreeMap<String, AdditionalContextEntry>,
|
||||
additional_context: impl Into<Option<BTreeMap<String, AdditionalContextEntry>>>,
|
||||
) -> Self {
|
||||
self.additional_context = additional_context;
|
||||
self.additional_context = additional_context.into();
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user