Unify turn input submission and routing (#38275)

## What changed

- Add `TurnInputRequest` and typed submission results for atomically starting a turn, steering the active turn, or declining input with a specific reason.
- Expose `start_or_steer_turn`, `start_turn_if_idle`, and `steer_turn` on `CodexThread`, and migrate Core consumers to these APIs.
- Make app-server `turn/start` steer an active regular turn and return that turn's ID. Reject incompatible output schemas and non-steerable turns without applying settings or enqueueing input.

## Testing

- Cover concurrent start-or-steer submissions, accepted and rejected settings updates, output-schema compatibility, idle-start rejection, and app-server steering.

GitOrigin-RevId: dd9b5528d76ec650c019e97af420bc13190ea86a
This commit is contained in:
Owen Lin
2026-08-13 00:00:16 +00:00
committed by copyberry
parent 4b07886d59
commit cbb7e82a8b
137 changed files with 5217 additions and 6812 deletions
@@ -688,7 +688,7 @@ async fn turn_start_jsonrpc_span_parents_core_turn_spans() -> Result<()> {
&& span_attr(span, "rpc.method") == Some("turn/start")
&& span.span_context.trace_id() == remote_trace_id
}) && spans.iter().any(|span| {
span_attr(span, "codex.op") == Some("user_input")
span_attr(span, "codex.op") == Some("turn_input")
&& span.span_context.trace_id() == remote_trace_id
})
})
@@ -697,8 +697,8 @@ async fn turn_start_jsonrpc_span_parents_core_turn_spans() -> Result<()> {
let server_request_span =
find_rpc_span_with_trace(&spans, SpanKind::Server, "turn/start", remote_trace_id);
let core_turn_span =
find_span_with_trace(&spans, remote_trace_id, "codex.op=user_input", |span| {
span_attr(span, "codex.op") == Some("user_input")
find_span_with_trace(&spans, remote_trace_id, "codex.op=turn_input", |span| {
span_attr(span, "codex.op") == Some("turn_input")
});
assert_eq!(server_request_span.parent_span_id, remote_parent_span_id);
@@ -333,12 +333,17 @@ use codex_core::CodexThreadSettingsOverrides;
use codex_core::ForkSnapshot;
use codex_core::McpManager;
use codex_core::NewThread;
use codex_core::NotSubmittedReason;
#[cfg(test)]
use codex_core::SessionMeta;
use codex_core::StartThreadOptions;
use codex_core::SteerInputError;
use codex_core::SteerSubmission;
use codex_core::ThreadConfigSnapshot;
use codex_core::ThreadManager;
use codex_core::TurnInput;
use codex_core::TurnInputRequest;
use codex_core::TurnInputSubmission;
use codex_core::TurnStartOptions;
use codex_core::config::Config;
use codex_core::config::ConfigOverrides;
use codex_core::config::NetworkProxyAuditMetadata;
@@ -554,28 +554,38 @@ impl TurnRequestProcessor {
.map(PermissionProfile::from_legacy_sandbox_policy)
});
// Start the turn by submitting the user input. Return its submission id as turn_id.
let turn_op = Op::UserInput {
items: mapped_items,
final_output_json_schema: params.output_schema,
responsesapi_client_metadata: params.responsesapi_client_metadata,
additional_context,
thread_settings,
};
let turn_id = thread
.submit_user_input_with_client_user_message_id(
turn_op,
self.request_trace_context(&request_id).await,
client_user_message_id,
let submission = thread
.start_or_steer_turn(
TurnInputRequest::new(TurnInput::UserInput {
content: mapped_items,
client_id: client_user_message_id,
})
.with_thread_settings(thread_settings)
.on_start(TurnStartOptions {
final_output_json_schema: params.output_schema,
..Default::default()
})
.with_additional_context(additional_context)
.with_responses_metadata(params.responsesapi_client_metadata)
.with_trace(self.request_trace_context(&request_id).await),
)
.await
.map_err(|err| {
let error = internal_error(format!("failed to start turn: {err}"));
let error = internal_error(format!("failed to submit turn input: {err}"));
self.track_error_response(&request_id, &error, /*error_type*/ None);
error
})?;
let (turn_id, started) = match submission {
TurnInputSubmission::Started { turn_id } => (turn_id, true),
TurnInputSubmission::Steered { turn_id } => (turn_id, false),
TurnInputSubmission::NotSubmitted { reason } => {
let error = internal_error(format!("failed to submit turn input: {reason:?}"));
self.track_error_response(&request_id, &error, /*error_type*/ None);
return Err(error);
}
};
if turn_has_input {
if turn_has_input && started {
let config_snapshot = thread.config_snapshot().await;
let parent_permission_profile =
parent_permission_profile_override.unwrap_or(config_snapshot.permission_profile);
@@ -943,32 +953,41 @@ impl TurnRequestProcessor {
.collect();
let additional_context = map_additional_context(params.additional_context);
let turn_id = thread
.steer_input(
mapped_items,
additional_context,
Some(&params.expected_turn_id),
params.client_user_message_id,
params.responsesapi_client_metadata,
let submission = thread
.steer_turn(
TurnInputRequest::new(TurnInput::UserInput {
content: mapped_items,
client_id: params.client_user_message_id,
})
.with_additional_context(additional_context)
.with_responses_metadata(params.responsesapi_client_metadata),
params.expected_turn_id,
)
.await
.map_err(|err| {
let (message, data, error_type) = match err {
SteerInputError::NoActiveTurn(_) => (
let error = internal_error(format!("failed to steer turn: {err}"));
self.track_error_response(request_id, &error, /*error_type*/ None);
error
})?;
let turn_id = match submission {
SteerSubmission::Steered { turn_id } => turn_id,
SteerSubmission::NotSubmitted { reason } => {
let (message, data, error_type) = match reason {
NotSubmittedReason::NoActiveTurn | NotSubmittedReason::NotIdle => (
"no active turn to steer".to_string(),
None,
Some(AnalyticsJsonRpcError::TurnSteer(
TurnSteerRequestError::NoActiveTurn,
)),
),
SteerInputError::ExpectedTurnMismatch { expected, actual } => (
NotSubmittedReason::ExpectedTurnMismatch { expected, actual } => (
format!("expected active turn id `{expected}` but found `{actual}`"),
None,
Some(AnalyticsJsonRpcError::TurnSteer(
TurnSteerRequestError::ExpectedTurnMismatch,
)),
),
SteerInputError::ActiveTurnNotSteerable { turn_kind } => {
NotSubmittedReason::ActiveTurnNotSteerable { turn_kind } => {
let (message, turn_steer_error) = match turn_kind {
codex_protocol::protocol::NonSteerableTurnKind::Review => (
"cannot steer a review turn".to_string(),
@@ -1002,17 +1021,30 @@ impl TurnRequestProcessor {
Some(AnalyticsJsonRpcError::TurnSteer(turn_steer_error)),
)
}
SteerInputError::EmptyInput => (
NotSubmittedReason::EmptyInput => (
"input must not be empty".to_string(),
None,
Some(AnalyticsJsonRpcError::Input(InputError::Empty)),
),
NotSubmittedReason::ActiveTurnOutputSchemaMismatch => (
"active turn uses a different output schema".to_string(),
None,
None,
),
NotSubmittedReason::PendingTriggerTurn | NotSubmittedReason::PlanMode => (
"no active turn to steer".to_string(),
None,
Some(AnalyticsJsonRpcError::TurnSteer(
TurnSteerRequestError::NoActiveTurn,
)),
),
};
let mut error = invalid_request(message);
error.data = data;
self.track_error_response(request_id, &error, error_type);
error
})?;
return Err(error);
}
};
Ok(TurnSteerResponse { turn_id })
}