Centralize app-server turn error conversion (#62096)

## What changed

Add a shared `From<ErrorEvent>` implementation for `TurnError` and reuse it in thread history, rollout projection, and live event handling. Preserve the existing field mappings and keep `additional_details` unset.

Parameterize the compaction replay test by error code and recoverability, retaining the existing `server_error` case, and document retry expectations for WebSocket closes.

GitOrigin-RevId: 3afcc23e9fdc314477febe6b4a4aeb680efafd5a
This commit is contained in:
adi-oai
2026-11-17 18:56:05 +00:00
committed by copyberry
parent cb64621bf3
commit 0122b120cd
6 changed files with 47 additions and 65 deletions
@@ -1427,12 +1427,7 @@ impl ThreadHistoryBuilder {
let tracking_changes = self.is_tracking_changes();
let changed_turn = if let Some(turn) = self.current_turn.as_mut() {
turn.status = TurnStatus::Failed;
turn.error = Some(V2TurnError {
misalignment: payload.misalignment.clone().map(Into::into),
message: payload.message.clone(),
codex_error_info: payload.codex_error_info.clone().map(Into::into),
additional_details: None,
});
turn.error = Some(V2TurnError::from(payload.clone()));
tracking_changes.then(|| ThreadHistoryTurnMetadata::from_pending_turn(turn))
} else {
None
@@ -1443,12 +1438,7 @@ impl ThreadHistoryBuilder {
}
fn handle_turn_aborted(&mut self, payload: &TurnAbortedEvent) {
let terminal_error = payload.error.as_ref().map(|error| V2TurnError {
message: error.message.clone(),
codex_error_info: error.codex_error_info.clone().map(Into::into),
misalignment: error.misalignment.clone().map(Into::into),
additional_details: None,
});
let terminal_error = payload.error.clone().map(V2TurnError::from);
let apply_abort = |turn: &mut PendingTurn| {
turn.status = TurnStatus::Interrupted;
if let Some(error) = terminal_error.as_ref() {
@@ -1497,12 +1487,7 @@ impl ThreadHistoryBuilder {
}
fn handle_turn_complete(&mut self, payload: &TurnCompleteEvent) {
let terminal_error = payload.error.as_ref().map(|error| V2TurnError {
misalignment: error.misalignment.clone().map(Into::into),
message: error.message.clone(),
codex_error_info: error.codex_error_info.clone().map(Into::into),
additional_details: None,
});
let terminal_error = payload.error.clone().map(V2TurnError::from);
let apply_completion = |turn: &mut PendingTurn| {
if let Some(error) = terminal_error.as_ref() {
turn.status = TurnStatus::Failed;
@@ -41,12 +41,7 @@ pub fn project_rollout_line(line: &RolloutLine) -> ThreadHistoryChangeSet {
} else {
TurnStatus::Completed
},
error: event.error.as_ref().map(|error| TurnError {
misalignment: error.misalignment.clone().map(Into::into),
message: error.message.clone(),
codex_error_info: error.codex_error_info.clone().map(Into::into),
additional_details: None,
}),
error: event.error.clone().map(TurnError::from),
started_at: event.started_at,
completed_at: event.completed_at,
duration_ms: event.duration_ms,
@@ -62,12 +57,7 @@ pub fn project_rollout_line(line: &RolloutLine) -> ThreadHistoryChangeSet {
turn_id: turn_id.clone(),
root_turn_id: event.root_turn_id.clone(),
status: TurnStatus::Interrupted,
error: event.error.as_ref().map(|error| TurnError {
message: error.message.clone(),
codex_error_info: error.codex_error_info.clone().map(Into::into),
misalignment: error.misalignment.clone().map(Into::into),
additional_details: None,
}),
error: event.error.clone().map(TurnError::from),
started_at: event.started_at,
completed_at: event.completed_at,
duration_ms: event.duration_ms,
@@ -451,6 +451,18 @@ pub struct TurnError {
pub misalignment: Option<MisalignmentErrorDetails>,
}
impl From<codex_protocol::protocol::ErrorEvent> for TurnError {
fn from(error: codex_protocol::protocol::ErrorEvent) -> Self {
let additional_details = None;
Self {
message: error.message,
codex_error_info: error.codex_error_info.map(Into::into),
additional_details,
misalignment: error.misalignment.map(Into::into),
}
}
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
@@ -1093,12 +1093,7 @@ pub(crate) async fn apply_bespoke_event_handling(
return;
}
let turn_error = TurnError {
misalignment: ev.misalignment.map(Into::into),
message: ev.message,
codex_error_info: ev.codex_error_info.map(V2CodexErrorInfo::from),
additional_details: None,
};
let turn_error = TurnError::from(ev);
handle_error_notification(
conversation_id,
&event_turn_id,
@@ -1239,12 +1234,7 @@ pub(crate) async fn apply_bespoke_event_handling(
TurnCompletionMetadata {
status: TurnStatus::Interrupted,
root_turn_id: aborted.root_turn_id,
error: aborted.error.map(|error| TurnError {
message: error.message,
codex_error_info: error.codex_error_info.map(Into::into),
misalignment: error.misalignment.map(Into::into),
additional_details: None,
}),
error: aborted.error.map(TurnError::from),
last_agent_message: None,
started_at: aborted.started_at,
completed_at: aborted.completed_at,
@@ -1635,12 +1625,7 @@ async fn handle_turn_interrupted(
TurnCompletionMetadata {
status: TurnStatus::Interrupted,
root_turn_id: turn_aborted_event.root_turn_id,
error: turn_aborted_event.error.map(|error| TurnError {
message: error.message,
codex_error_info: error.codex_error_info.map(Into::into),
misalignment: error.misalignment.map(Into::into),
additional_details: None,
}),
error: turn_aborted_event.error.map(TurnError::from),
last_agent_message: None,
started_at: turn_summary.started_at,
completed_at: turn_aborted_event.completed_at,
+26 -17
View File
@@ -3658,8 +3658,10 @@ async fn pre_sampling_compact_falls_back_after_previous_model_invalid_request_on
);
}
/// Only a recoverable compaction error can replay on the same or a different model.
#[test_case::test_case("server_error", true; "transient")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn pre_sampling_compact_falls_back_after_previous_model_stream_retries_are_exhausted() {
async fn pre_sampling_compact_replays_only_recoverable_errors(code: &str, recoverable: bool) {
skip_if_no_network!();
let server = MockServer::start().await;
@@ -3676,19 +3678,17 @@ async fn pre_sampling_compact_falls_back_after_previous_model_stream_retries_are
)
.await;
let compaction_failure = sse_failed(
"compact-failure",
"server_error",
"previous-model compaction failed",
);
let request_log = mount_sse_sequence(
&server,
vec![
sse(vec![
ev_assistant_message("m1", "before switch"),
ev_completed_with_tokens("r1", /*total_tokens*/ 120_000),
]),
compaction_failure.clone(),
let compaction_failure =
sse_failed("compact-failure", code, "previous-model compaction failed");
let mut responses = vec![
sse(vec![
ev_assistant_message("m1", "before switch"),
ev_completed_with_tokens("r1", /*total_tokens*/ 120_000),
]),
compaction_failure.clone(),
];
if recoverable {
responses.extend([
compaction_failure.clone(),
compaction_failure,
remote_v2_compaction_response(),
@@ -3696,9 +3696,9 @@ async fn pre_sampling_compact_falls_back_after_previous_model_stream_retries_are
ev_assistant_message("m3", "after switch"),
ev_completed_with_tokens("r3", /*total_tokens*/ 100),
]),
],
)
.await;
]);
}
let request_log = mount_sse_sequence(&server, responses).await;
let mut model_provider = openai_model_provider(&server);
model_provider.stream_max_retries = Some(2);
@@ -3732,6 +3732,15 @@ async fn pre_sampling_compact_falls_back_after_previous_model_stream_retries_are
))
.await
.expect("submit selected-model turn");
if !recoverable {
wait_for_event(&test.codex, |event| matches!(event, EventMsg::Error(_))).await;
wait_for_event(&test.codex, |event| {
matches!(event, EventMsg::TurnComplete(_))
})
.await;
assert_eq!(request_log.requests().len(), 2);
return;
}
assert_compaction_uses_turn_lifecycle_id(&test.codex).await;
let actual_models = request_log
@@ -35,6 +35,7 @@ enum Rejection {
GoingAway,
}
/// Transient closes can retry. A classifier rejection ends the turn and the next turn uses WebSocket.
#[test_case::test_case(Rejection::AdvertisedLimit, 0, 1; "advertised_limit_skips_websocket_upload")]
#[test_case::test_case(Rejection::Oversized, 1, 1; "size_rejection_before_any_text")]
#[test_case::test_case(Rejection::OversizedWarmup, 0, 1; "size_rejection_during_prewarm")]