Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 98 additions & 29 deletions src/providers/codex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::sync::Arc;

use crate::anthropic::error::json_error;
use crate::anthropic::schema::{CountTokensResponse, MessagesRequest};
use crate::anthropic::sse::parse_sse_events;
use crate::config;
use crate::logging::create_logger;
use crate::monitor::usage_from_anthropic_sse;
Expand Down Expand Up @@ -567,6 +568,9 @@ async fn live_stream_response_once(
) -> LiveStreamStart {
let mut translator = LiveStreamTranslator::new(message_id, model.to_string());
let mut upstream_sse_body = Vec::new();
// Keep protocol framing private until real output makes a transparent retry unsafe.
// Every branch that consumes pending_chunk returns, so it is never flushed twice.
let mut pending_chunk = Vec::new();
let mut generation_started = false;

while let Some(item) = upstream_events.recv().await {
Expand All @@ -592,7 +596,7 @@ async fn live_stream_response_once(
generation_started = true;
}
append_upstream_sse_payload(&mut upstream_sse_body, &payload);
let (chunk, terminal) = match translate_live_stream_payload(&mut translator, &payload, &ctx)
let (chunk, terminal) = match translate_live_stream_payload(&mut translator, &payload, None)
{
Ok(result) => result,
Err(message) => {
Expand Down Expand Up @@ -638,8 +642,18 @@ async fn live_stream_response_once(
return LiveStreamStart::Response(map_codex_failure_to_response(&message));
}
};
if !chunk.is_empty() {
record_live_stream_progress(&ctx, &chunk);
pending_chunk.extend_from_slice(&chunk);
if terminal
&& is_codex_success_terminal_event(&payload)
&& !translator.has_semantic_output()
{
return LiveStreamStart::Retry {
error: empty_live_completion_error(),
};
}
if translator.has_semantic_output() && !pending_chunk.is_empty() {
record_live_stream_downstream_capture(&ctx, &pending_chunk);
record_live_stream_progress(&ctx, &pending_chunk);
if terminal {
update_continuation_from_upstream(
ctx.session_id.as_deref(),
Expand All @@ -648,12 +662,12 @@ async fn live_stream_response_once(
&upstream_sse_body,
compact_boundary,
);
return LiveStreamStart::Response(single_live_stream_response(chunk));
return LiveStreamStart::Response(single_live_stream_response(pending_chunk));
}
return LiveStreamStart::Response(remaining_live_stream_response(
upstream_events,
translator,
chunk,
pending_chunk,
ctx,
turn_id,
request_body,
Expand All @@ -669,7 +683,12 @@ async fn live_stream_response_once(
&upstream_sse_body,
compact_boundary,
);
return LiveStreamStart::Response(empty_live_stream_response());
if pending_chunk.is_empty() {
return LiveStreamStart::Response(empty_live_stream_response());
}
record_live_stream_downstream_capture(&ctx, &pending_chunk);
record_live_stream_progress(&ctx, &pending_chunk);
return LiveStreamStart::Response(single_live_stream_response(pending_chunk));
}
}

Expand All @@ -684,6 +703,16 @@ async fn live_stream_response_once(
}
}

fn empty_live_completion_error() -> client::CodexError {
client::CodexError {
status: 503,
message: "Codex completed without producing output".to_string(),
detail: Some(EMPTY_CODEX_COMPLETION_DETAIL.to_string()),
retry_after: None,
origin: client::CodexErrorOrigin::WebSocket,
}
}

fn codex_generation_event(payload: &serde_json::Value) -> bool {
!matches!(
payload.get("type").and_then(|value| value.as_str()),
Expand All @@ -694,13 +723,31 @@ fn codex_generation_event(payload: &serde_json::Value) -> bool {
fn translate_live_stream_payload(
translator: &mut LiveStreamTranslator,
payload: &serde_json::Value,
ctx: &RequestContext,
traffic: Option<&crate::traffic::TrafficCapture>,
) -> Result<(Vec<u8>, bool), String> {
let chunk = translator.accept(payload, ctx.traffic.as_deref())?;
let chunk = translator.accept(payload, traffic)?;
let terminal = is_codex_terminal_event(payload) || translator.is_finished();
Ok((chunk, terminal))
}

fn record_live_stream_downstream_capture(ctx: &RequestContext, chunk: &[u8]) {
let Some(traffic) = ctx.traffic.as_ref() else {
return;
};
for event in parse_sse_events(chunk) {
let Ok(data) = serde_json::from_str::<serde_json::Value>(&event.data) else {
continue;
};
traffic.write_json_event(
"050-downstream-event",
&serde_json::json!({
"event": event.event.as_deref().unwrap_or("message"),
"data": data,
}),
);
}
}

fn record_live_stream_progress(ctx: &RequestContext, chunk: &[u8]) {
if let Some(monitor) = ctx.monitor.as_ref() {
let (input_tokens, output_tokens) = usage_from_anthropic_sse(chunk);
Expand Down Expand Up @@ -750,28 +797,31 @@ fn remaining_live_stream_response(
match item {
Ok(payload) => {
append_upstream_sse_payload(&mut upstream_sse_body, &payload);
let (chunk, terminal) =
match translate_live_stream_payload(&mut translator, &payload, &ctx) {
Ok(result) => result,
Err(message) => {
abort_request_state(
ctx.session_id.as_deref(),
turn_id,
compact_boundary,
&request_body,
);
let chunk = translator.error_chunk(
&message,
"api_error",
ctx.traffic.as_deref(),
);
if !chunk.is_empty() {
record_live_stream_progress(&ctx, &chunk);
let _ = tx.send(Ok(Bytes::from(chunk))).await;
}
return;
let (chunk, terminal) = match translate_live_stream_payload(
&mut translator,
&payload,
ctx.traffic.as_deref(),
) {
Ok(result) => result,
Err(message) => {
abort_request_state(
ctx.session_id.as_deref(),
turn_id,
compact_boundary,
&request_body,
);
let chunk = translator.error_chunk(
&message,
"api_error",
ctx.traffic.as_deref(),
);
if !chunk.is_empty() {
record_live_stream_progress(&ctx, &chunk);
let _ = tx.send(Ok(Bytes::from(chunk))).await;
}
};
return;
}
};
if !chunk.is_empty() {
record_live_stream_progress(&ctx, &chunk);
if tx.send(Ok(Bytes::from(chunk))).await.is_err() {
Expand Down Expand Up @@ -926,6 +976,13 @@ fn is_codex_terminal_event(payload: &serde_json::Value) -> bool {
)
}

fn is_codex_success_terminal_event(payload: &serde_json::Value) -> bool {
matches!(
payload.get("type").and_then(|v| v.as_str()),
Some("response.completed") | Some("response.done")
)
}

fn retryable_live_start_codex_error(err: &client::CodexError) -> bool {
if err.origin == client::CodexErrorOrigin::WebSocketHandshake {
return err.status == 0 || matches!(err.status, 429 | 500 | 502 | 503 | 504 | 529);
Expand Down Expand Up @@ -1481,6 +1538,18 @@ mod tests {
);
}

#[tokio::test]
async fn empty_live_completion_maps_to_explicit_service_unavailable() {
let err = empty_live_completion_error();

assert_eq!(err.status, 503);
assert_eq!(err.detail.as_deref(), Some(EMPTY_CODEX_COMPLETION_DETAIL));
assert_eq!(
map_codex_error_to_response(&err).status(),
StatusCode::SERVICE_UNAVAILABLE
);
}

#[test]
fn live_start_statusless_websocket_handshake_error_is_retryable() {
let err = client::CodexError {
Expand Down
Loading