Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
restore codex stream parity
Raine Virta8d005cb150eb
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex cbc84a6..4c2fa4d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,6 +17,11 @@- Codex WebSocket streaming responses report completed turns as `end_turn`when the gateway sends `incomplete_details: null`, preventing false outputlimit recovery loops in Claude Code.+- Codex WebSocket live streaming matches the TypeScript fallback behavior for+ retryable startup failures, completed tool-call disconnects, SSE error+ framing, buffered `Read` argument repair, and longer quiet upstream turns.+- Codex tool result translation preserves omitted-block markers for malformed+ text and image result content, keeping unsupported tool output visible.## v0.1.2 (2026-06-30)diff --git a/src/providers/codex/continuation.rs b/src/providers/codex/continuation.rsindex 0563ab1..23ea139 100644--- a/src/providers/codex/continuation.rs+++ b/src/providers/codex/continuation.rs@@ -20,6 +20,7 @@ struct ContinuationState {static STATES: Mutex<Option<HashMap<String, ContinuationState>>> = Mutex::new(None);static TOTAL_TRANSCRIPT_BYTES: Mutex<u64> = Mutex::new(0);+#[derive(Clone)]pub struct ContinuationCandidate {pub previous_response_id: Option<String>,pub input_delta: Option<Vec<ResponsesInputItem>>,diff --git a/src/providers/codex/mod.rs b/src/providers/codex/mod.rsindex 89c55a8..35b0f05 100644--- a/src/providers/codex/mod.rs+++ b/src/providers/codex/mod.rs@@ -20,19 +20,24 @@ use crate::config;use crate::monitor::usage_from_anthropic_sse;use crate::provider::{CliHandlers, Provider, RequestContext};use crate::registry;+use crate::retry::{compute_backoff_delay, sleep};use self::auth::browser_login::run_browser_login;use self::auth::device::DeviceAuthClient;use self::auth::manager::CodexAuthManager;use self::auth::token_store::file_store;use self::client::CodexHttpClient;-use self::continuation::{clear_continuation, continuation_candidate, record_continuation};+use self::continuation::{+ ContinuationCandidate, clear_continuation, continuation_candidate, record_continuation,+};use self::count_tokens::count_translated_tokens;use self::translate::accumulate::accumulate_response_with_traffic;use self::translate::live_stream::LiveStreamTranslator;use self::translate::model_allowlist::{assert_allowed_model, resolve_model_request};use self::translate::reducer::finish_metadata_from_upstream;use self::translate::request::{TranslateOptions, translate_request};++const MAX_RETRYABLE_LIVE_STREAM_RETRIES: u32 = 10;use self::translate::stream::translate_stream_bytes_with_traffic;// ---------------------------------------------------------------------------@@ -126,18 +131,15 @@ impl Provider for CodexProvider {}if want_stream && matches!(config::codex_transport(), config::CodexTransport::WebSocket) {let stream_request = translated.clone();- let upstream_events = match client- .stream_codex_websocket_events(&translated, &ctx, Some(&continuation))- .await- {- Ok(events) => events,- Err(e) => {- clear_continuation(ctx.session_id.as_deref());- return map_codex_error_to_response(&e);- }- };- return live_stream_response(upstream_events, message_id, model, ctx, stream_request)- .await;+ return live_stream_response(+ client,+ message_id,+ model,+ ctx,+ stream_request,+ continuation,+ )+ .await;}let upstream = match client@@ -275,13 +277,88 @@ fn count_sse_events(bytes: &[u8]) -> u64 {String::from_utf8_lossy(bytes).matches("event:").count() as u64}+enum LiveStreamStart {+ Response(Response),+ Retry {+ message: String,+ retry_after: Option<String>,+ },+}+async fn live_stream_response(- mut upstream_events: websocket::CodexWebSocketEventReceiver,+ client: Arc<CodexHttpClient>,message_id: String,model: &str,ctx: RequestContext,request_body: translate::request::ResponsesRequest,+ continuation: ContinuationCandidate,) -> Response {+ let model = model.to_string();+ let mut attempt = 0_u32;++ loop {+ let upstream_events = match client+ .stream_codex_websocket_events(&request_body, &ctx, Some(&continuation))+ .await+ {+ Ok(events) => events,+ Err(err) if retryable_live_start_codex_error(&err) => {+ if attempt >= MAX_RETRYABLE_LIVE_STREAM_RETRIES {+ clear_continuation(ctx.session_id.as_deref());+ return map_codex_error_to_response(&err);+ }+ let delay = compute_backoff_delay(attempt, err.retry_after.as_deref());+ if delay.exceeds_budget {+ clear_continuation(ctx.session_id.as_deref());+ return map_codex_error_to_response(&err);+ }+ attempt += 1;+ sleep(delay.wait_ms).await;+ continue;+ }+ Err(err) => {+ clear_continuation(ctx.session_id.as_deref());+ return map_codex_error_to_response(&err);+ }+ };++ match live_stream_response_once(+ upstream_events,+ message_id.clone(),+ &model,+ ctx.clone(),+ request_body.clone(),+ )+ .await+ {+ LiveStreamStart::Response(response) => return response,+ LiveStreamStart::Retry {+ message,+ retry_after,+ } => {+ if attempt >= MAX_RETRYABLE_LIVE_STREAM_RETRIES {+ clear_continuation(ctx.session_id.as_deref());+ return json_error(StatusCode::BAD_GATEWAY, "api_error", message);+ }+ let delay = compute_backoff_delay(attempt, retry_after.as_deref());+ if delay.exceeds_budget {+ clear_continuation(ctx.session_id.as_deref());+ return json_error(StatusCode::BAD_GATEWAY, "api_error", message);+ }+ attempt += 1;+ sleep(delay.wait_ms).await;+ }+ }+ }+}++async fn live_stream_response_once(+ mut upstream_events: websocket::CodexWebSocketEventReceiver,+ message_id: String,+ model: &str,+ ctx: RequestContext,+ request_body: translate::request::ResponsesRequest,+) -> LiveStreamStart {let mut translator = LiveStreamTranslator::new(message_id, model.to_string());let mut upstream_sse_body = Vec::new();@@ -289,8 +366,14 @@ async fn live_stream_response(let payload = match item {Ok(payload) => payload,Err(err) => {+ if retryable_live_start_codex_error(&err) {+ return LiveStreamStart::Retry {+ message: codex_error_message(&err).to_string(),+ retry_after: err.retry_after,+ };+ }clear_continuation(ctx.session_id.as_deref());- return map_codex_error_to_response(&err);+ return LiveStreamStart::Response(map_codex_error_to_response(&err));}};append_upstream_sse_payload(&mut upstream_sse_body, &payload);@@ -298,8 +381,18 @@ async fn live_stream_response({Ok(result) => result,Err(message) => {+ if retryable_live_start_payload(&payload, &message) {+ return LiveStreamStart::Retry {+ message,+ retry_after: retry_after_from_live_payload(&payload),+ };+ }clear_continuation(ctx.session_id.as_deref());- return json_error(StatusCode::BAD_GATEWAY, "api_error", message);+ return LiveStreamStart::Response(json_error(+ StatusCode::BAD_GATEWAY,+ "api_error",+ message,+ ));}};if !chunk.is_empty() {@@ -310,16 +403,16 @@ async fn live_stream_response(&request_body,&upstream_sse_body,);- return single_live_stream_response(chunk);+ return LiveStreamStart::Response(single_live_stream_response(chunk));}- return remaining_live_stream_response(+ return LiveStreamStart::Response(remaining_live_stream_response(upstream_events,translator,chunk,ctx,request_body,upstream_sse_body,- );+ ));}if terminal {update_continuation_from_upstream(@@ -327,16 +420,14 @@ async fn live_stream_response(&request_body,&upstream_sse_body,);- return empty_live_stream_response();+ return LiveStreamStart::Response(empty_live_stream_response());}}- clear_continuation(ctx.session_id.as_deref());- json_error(- StatusCode::BAD_GATEWAY,- "api_error",- "WebSocket connection closed before terminal Codex response event",- )+ LiveStreamStart::Retry {+ message: "WebSocket connection closed before terminal Codex response event".to_string(),+ retry_after: None,+ }}fn translate_live_stream_payload(@@ -344,8 +435,8 @@ fn translate_live_stream_payload(payload: &serde_json::Value,ctx: &RequestContext,) -> Result<(Vec<u8>, bool), String> {- let terminal = is_codex_terminal_event(payload);let chunk = translator.accept(payload, ctx.traffic.as_deref())?;+ let terminal = is_codex_terminal_event(payload) || translator.is_finished();Ok((chunk, terminal))}@@ -394,7 +485,15 @@ fn remaining_live_stream_response(Ok(result) => result,Err(message) => {clear_continuation(ctx.session_id.as_deref());- let _ = tx.send(Err(stream_error(message))).await;+ 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;}};@@ -416,18 +515,44 @@ fn remaining_live_stream_response(}Err(err) => {clear_continuation(ctx.session_id.as_deref());- let _ = tx.send(Err(stream_error(codex_error_message(&err)))).await;+ let chunk =+ translator.finish_after_closed_completed_tool_call(ctx.traffic.as_deref());+ if !chunk.is_empty() {+ record_live_stream_progress(&ctx, &chunk);+ let _ = tx.send(Ok(Bytes::from(chunk))).await;+ return;+ }+ let error_type = codex_stream_error_type(&err);+ let chunk = translator.error_chunk(+ codex_error_message(&err),+ error_type,+ ctx.traffic.as_deref(),+ );+ if !chunk.is_empty() {+ record_live_stream_progress(&ctx, &chunk);+ let _ = tx.send(Ok(Bytes::from(chunk))).await;+ }return;}}}clear_continuation(ctx.session_id.as_deref());- let _ = tx- .send(Err(stream_error(- "WebSocket connection closed before terminal Codex response event",- )))- .await;+ let chunk = translator.finish_after_closed_completed_tool_call(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 = translator.error_chunk(+ "WebSocket connection closed before terminal Codex response event",+ "api_error",+ ctx.traffic.as_deref(),+ );+ if !chunk.is_empty() {+ record_live_stream_progress(&ctx, &chunk);+ let _ = tx.send(Ok(Bytes::from(chunk))).await;+ }});let stream = futures_util::stream::unfold(rx, |mut rx| async {@@ -458,10 +583,6 @@ where(headers, Body::from_stream(stream)).into_response()}-fn stream_error(message: impl Into<String>) -> std::io::Error {- std::io::Error::other(message.into())-}-fn is_codex_terminal_event(payload: &serde_json::Value) -> bool {matches!(payload.get("type").and_then(|v| v.as_str()),@@ -474,6 +595,135 @@ fn is_codex_terminal_event(payload: &serde_json::Value) -> bool {)}+fn retryable_live_start_codex_error(err: &client::CodexError) -> bool {+ matches!(err.status, 429 | 500 | 502 | 503 | 504 | 529)+ || (err.status == 0 && retryable_live_message(codex_error_message(err)))+}++fn retryable_live_start_payload(payload: &serde_json::Value, message: &str) -> bool {+ if payload.get("type").and_then(|v| v.as_str()) == Some("codex.rate_limits")+ && payload+ .get("rate_limits")+ .and_then(|r| r.get("limit_reached"))+ .and_then(|v| v.as_bool())+ == Some(true)+ {+ return true;+ }++ let status = payload+ .get("status")+ .or_else(|| payload.get("status_code"))+ .and_then(|v| v.as_u64())+ .or_else(|| {+ payload+ .get("error")+ .and_then(|e| e.get("status"))+ .and_then(|v| v.as_u64())+ })+ .or_else(|| {+ payload+ .get("response")+ .and_then(|r| r.get("error"))+ .and_then(|e| e.get("status"))+ .and_then(|v| v.as_u64())+ });+ if matches!(status, Some(429 | 500 | 502 | 503 | 504 | 529)) {+ return true;+ }++ let code = payload+ .get("error")+ .and_then(|e| e.get("code"))+ .and_then(|v| v.as_str())+ .or_else(|| {+ payload+ .get("response")+ .and_then(|r| r.get("error"))+ .and_then(|e| e.get("code"))+ .and_then(|v| v.as_str())+ });+ let err_type = payload+ .get("error")+ .and_then(|e| e.get("type"))+ .and_then(|v| v.as_str())+ .or_else(|| {+ payload+ .get("response")+ .and_then(|r| r.get("error"))+ .and_then(|e| e.get("type"))+ .and_then(|v| v.as_str())+ });+ code == Some("overloaded_error")+ || err_type == Some("overloaded_error")+ || retryable_live_message(message)+}++fn retry_after_from_live_payload(payload: &serde_json::Value) -> Option<String> {+ payload+ .get("rate_limits")+ .and_then(|r| r.get("primary"))+ .and_then(|r| r.get("reset_after_seconds"))+ .map(json_scalar_to_string)+ .or_else(|| {+ payload+ .get("error")+ .and_then(|e| e.get("retry_after"))+ .map(json_scalar_to_string)+ })+ .or_else(|| {+ payload+ .get("error")+ .and_then(|e| e.get("retry_after_seconds"))+ .map(json_scalar_to_string)+ })+ .or_else(|| {+ payload+ .get("response")+ .and_then(|r| r.get("error"))+ .and_then(|e| e.get("retry_after_seconds"))+ .map(json_scalar_to_string)+ })+ .or_else(|| {+ payload+ .get("headers")+ .and_then(|h| h.get("retry-after"))+ .map(json_scalar_to_string)+ })+}++fn json_scalar_to_string(value: &serde_json::Value) -> String {+ value+ .as_str()+ .map(str::to_string)+ .unwrap_or_else(|| value.to_string())+}++fn retryable_live_message(message: &str) -> bool {+ let lower = message.to_lowercase();+ lower.contains("overloaded")+ || lower.contains("rate limit")+ || lower.contains("you can retry your request")+ || lower.contains("temporarily unavailable")+ || lower.contains("timed out")+ || lower.contains("connection closed")+ || lower.contains("connection reset")+}++fn codex_stream_error_type(err: &client::CodexError) -> &'static str {+ match err.status {+ 429 => "rate_limit_error",+ 529 => "overloaded_error",+ _ if codex_error_message(err)+ .to_lowercase()+ .contains("overloaded") =>+ {+ "overloaded_error"+ }+ _ => "api_error",+ }+}+fn update_continuation_from_upstream(session_id: Option<&str>,request_body: &translate::request::ResponsesRequest,@@ -762,4 +1012,29 @@ mod tests {Some("WebSocket connect error: HTTP error: 502 Bad Gateway"));}++ #[test]+ fn live_start_payload_retry_detection_covers_rate_limit_and_overload() {+ assert!(retryable_live_start_payload(+ &serde_json::json!({+ "type": "codex.rate_limits",+ "rate_limits": {"limit_reached": true}+ }),+ "rate limit reached",+ ));+ assert!(retryable_live_start_payload(+ &serde_json::json!({+ "type": "response.failed",+ "response": {"error": {"type": "overloaded_error", "message": "overloaded"}}+ }),+ "overloaded",+ ));+ assert!(!retryable_live_start_payload(+ &serde_json::json!({+ "type": "response.failed",+ "response": {"error": {"message": "bad request"}}+ }),+ "bad request",+ ));+ }}diff --git a/src/providers/codex/translate/live_stream.rs b/src/providers/codex/translate/live_stream.rsindex 7a602f8..49b8d0c 100644--- a/src/providers/codex/translate/live_stream.rs+++ b/src/providers/codex/translate/live_stream.rs@@ -7,6 +7,9 @@ use super::reducer::{CodexUsage, STOP_END_TURN, STOP_MAX_TOKENS, STOP_TOOL_USE, map_codex_usage_to_anthropic,};+const BUFFERED_READ_REPAIR_TRAILING_WHITESPACE_BYTES: usize = 1_024;+const BUFFERED_TOOL_MAX_ARGS_BYTES: usize = 5_000_000;+enum LiveBlock {Text {index: usize,@@ -63,7 +66,17 @@ impl LiveStreamTranslator {let mut out = Vec::new();match kind {- "codex.rate_limits" | "keepalive" => {}+ "codex.rate_limits" => {+ if payload+ .get("rate_limits")+ .and_then(|r| r.get("limit_reached"))+ .and_then(|v| v.as_bool())+ == Some(true)+ {+ return Err("rate limit reached".to_string());+ }+ }+ "keepalive" => {}"response.failed" | "response.error" | "error" => {return Err(error_message(payload));}@@ -94,7 +107,7 @@ impl LiveStreamTranslator {self.text_delta(payload, traffic, &mut out);}"response.function_call_arguments.delta" => {- self.tool_delta(payload, traffic, &mut out);+ self.tool_delta(payload, traffic, &mut out)?;}"response.function_call_arguments.done" => {self.tool_arguments_done(payload);@@ -111,6 +124,52 @@ impl LiveStreamTranslator {Ok(out)}+ pub fn is_finished(&self) -> bool {+ self.finished+ }++ pub fn finish_after_closed_completed_tool_call(+ &mut self,+ traffic: Option<&TrafficCapture>,+ ) -> Vec<u8> {+ let mut out = Vec::new();+ if self.finished || !self.saw_tool_use || !self.blocks_by_output_index.is_empty() {+ return out;+ }+ self.close_thinking(traffic, &mut out);+ self.ensure_message_start(traffic, &mut out);+ self.emit_finish(STOP_TOOL_USE, None, traffic, &mut out);+ out+ }++ pub fn error_chunk(+ &mut self,+ message: &str,+ error_type: &str,+ traffic: Option<&TrafficCapture>,+ ) -> Vec<u8> {+ let mut out = Vec::new();+ if self.finished {+ return out;+ }+ self.close_open_blocks(traffic, &mut out);+ self.ensure_message_start(traffic, &mut out);+ self.emit(+ traffic,+ &mut out,+ "error",+ &serde_json::json!({+ "type": "error",+ "error": {+ "type": error_type,+ "message": message,+ }+ }),+ );+ self.finished = true;+ out+ }+fn ensure_message_start(&mut self, traffic: Option<&TrafficCapture>, out: &mut Vec<u8>) {if self.message_started {return;@@ -345,20 +404,22 @@ impl LiveStreamTranslator {payload: &serde_json::Value,traffic: Option<&TrafficCapture>,out: &mut Vec<u8>,- ) {+ ) -> Result<(), String> {let Some(output_index) = payload.get("output_index").and_then(|v| v.as_u64()).map(|v| v as usize)else {- return;+ return Ok(());};let delta = payload.get("delta").and_then(|v| v.as_str()).unwrap_or("");if delta.is_empty() {- return;+ return Ok(());}+ let mut repaired_read: Option<(usize, String)> = None;let Some(LiveBlock::Tool {index,+ name,args_accum,had_delta,buffer_until_done,@@ -366,28 +427,67 @@ impl LiveStreamTranslator {..}) = self.blocks_by_output_index.get_mut(&output_index)else {- return;+ return Ok(());};args_accum.push_str(delta);*had_delta = true;if *buffer_until_done {- return;+ if args_accum.len() > BUFFERED_TOOL_MAX_ARGS_BYTES {+ return Err(format!(+ "Buffered {name} tool arguments exceeded safe limits"+ ));+ }+ if let Some(repaired) = repair_whitespace_stalled_read_args(name, args_accum) {+ *args_accum = repaired.clone();+ *emitted_args = true;+ repaired_read = Some((*index, repaired));+ }+ } else {+ *emitted_args = true;+ let index = *index;+ self.emit(+ traffic,+ out,+ "content_block_delta",+ &serde_json::json!({+ "type": "content_block_delta",+ "index": index,+ "delta": {+ "type": "input_json_delta",+ "partial_json": delta+ }+ }),+ );+ return Ok(());}- *emitted_args = true;- let index = *index;- self.emit(- traffic,- out,- "content_block_delta",- &serde_json::json!({- "type": "content_block_delta",- "index": index,- "delta": {- "type": "input_json_delta",- "partial_json": delta- }- }),- );+ if let Some((index, repaired)) = repaired_read {+ self.blocks_by_output_index.remove(&output_index);+ self.emit(+ traffic,+ out,+ "content_block_delta",+ &serde_json::json!({+ "type": "content_block_delta",+ "index": index,+ "delta": {+ "type": "input_json_delta",+ "partial_json": repaired+ }+ }),+ );+ self.emit(+ traffic,+ out,+ "content_block_stop",+ &serde_json::json!({+ "type": "content_block_stop",+ "index": index,+ }),+ );+ self.ensure_message_start(traffic, out);+ self.emit_finish(STOP_TOOL_USE, None, traffic, out);+ }+ Ok(())}fn tool_arguments_done(&mut self, payload: &serde_json::Value) {@@ -501,25 +601,7 @@ impl LiveStreamTranslator {out: &mut Vec<u8>,) {self.close_thinking(traffic, out);- let open: Vec<usize> = self.blocks_by_output_index.keys().copied().collect();- for output_index in open {- let Some(state) = self.blocks_by_output_index.remove(&output_index) else {- continue;- };- let index = match state {- LiveBlock::Text { index } => index,- LiveBlock::Tool { index, .. } => index,- };- self.emit(- traffic,- out,- "content_block_stop",- &serde_json::json!({- "type": "content_block_stop",- "index": index,- }),- );- }+ self.close_open_blocks(traffic, out);self.ensure_message_start(traffic, out);let usage = payload.get("response").map(parse_codex_usage);let incomplete = response_is_incomplete(payload);@@ -530,6 +612,16 @@ impl LiveStreamTranslator {} else {STOP_END_TURN};+ self.emit_finish(stop_reason, usage, traffic, out);+ }++ fn emit_finish(+ &mut self,+ stop_reason: &str,+ usage: Option<CodexUsage>,+ traffic: Option<&TrafficCapture>,+ out: &mut Vec<u8>,+ ) {let mapped = map_codex_usage_to_anthropic(&usage, Some(self.web_search_requests));self.emit(traffic,@@ -553,6 +645,29 @@ impl LiveStreamTranslator {self.finished = true;}+ fn close_open_blocks(&mut self, traffic: Option<&TrafficCapture>, out: &mut Vec<u8>) {+ self.close_thinking(traffic, out);+ let open: Vec<usize> = self.blocks_by_output_index.keys().copied().collect();+ for output_index in open {+ let Some(state) = self.blocks_by_output_index.remove(&output_index) else {+ continue;+ };+ let index = match state {+ LiveBlock::Text { index } => index,+ LiveBlock::Tool { index, .. } => index,+ };+ self.emit(+ traffic,+ out,+ "content_block_stop",+ &serde_json::json!({+ "type": "content_block_stop",+ "index": index,+ }),+ );+ }+ }+fn close_thinking(&mut self, traffic: Option<&TrafficCapture>, out: &mut Vec<u8>) {let Some(index) = self.thinking_index.take() else {return;@@ -634,6 +749,69 @@ fn sanitize_tool_args(name: &str, args: &str) -> String {serde_json::to_string(&sanitized).unwrap_or_else(|_| args.to_string())}+fn repair_whitespace_stalled_read_args(name: &str, args: &str) -> Option<String> {+ if name != "Read" {+ return None;+ }+ let trimmed = args.trim_end();+ let trailing_whitespace = args.len().saturating_sub(trimmed.len());+ if trailing_whitespace < BUFFERED_READ_REPAIR_TRAILING_WHITESPACE_BYTES {+ return None;+ }+ parse_read_args_candidate(trimmed).or_else(|| {+ let with_brace = format!("{trimmed}}}");+ parse_read_args_candidate(&with_brace)+ })+}++fn parse_read_args_candidate(args: &str) -> Option<String> {+ let parsed: serde_json::Value = serde_json::from_str(args).ok()?;+ if !is_valid_read_args(&parsed) {+ return None;+ }+ Some(sanitize_tool_args(+ "Read",+ &serde_json::to_string(&parsed).ok()?,+ ))+}++fn is_valid_read_args(value: &serde_json::Value) -> bool {+ let Some(obj) = value.as_object() else {+ return false;+ };+ for key in obj.keys() {+ if !matches!(key.as_str(), "file_path" | "offset" | "limit" | "pages") {+ return false;+ }+ }+ let Some(file_path) = obj.get("file_path").and_then(|v| v.as_str()) else {+ return false;+ };+ if file_path.is_empty() {+ return false;+ }+ if let Some(offset) = obj.get("offset").and_then(|v| v.as_i64())+ && offset < 0+ {+ return false;+ }+ if let Some(limit) = obj.get("limit").and_then(|v| v.as_i64())+ && limit <= 0+ {+ return false;+ }+ if obj.get("offset").is_some_and(|v| !v.is_i64()) {+ return false;+ }+ if obj.get("limit").is_some_and(|v| !v.is_i64()) {+ return false;+ }+ if obj.get("pages").is_some_and(|v| !v.is_string()) {+ return false;+ }+ true+}+fn error_message(payload: &serde_json::Value) -> String {payload.get("response")@@ -757,4 +935,91 @@ mod tests {assert!(out.contains("/tmp/a"));assert!(!out.contains("pages"));}++ #[test]+ fn repairs_whitespace_stalled_read_args_as_tool_use_finish() {+ let mut translator = LiveStreamTranslator::new("msg_1", "gpt-5.5");+ let mut out = Vec::new();+ out.extend(+ translator+ .accept(+ &json!({+ "type": "response.output_item.added",+ "output_index": 0,+ "item": {"type":"function_call","call_id":"call_1","name":"Read"}+ }),+ None,+ )+ .unwrap(),+ );+ out.extend(+ translator+ .accept(+ &json!({+ "type": "response.function_call_arguments.delta",+ "output_index": 0,+ "delta": format!("{{\"file_path\":\"/tmp/a\",\"pages\":\"\"{}", " ".repeat(1024))+ }),+ None,+ )+ .unwrap(),+ );+ let rendered = String::from_utf8(out).unwrap();+ assert!(rendered.contains(r#""partial_json":"{\"file_path\":\"/tmp/a\"}""#));+ assert!(rendered.contains(r#""stop_reason":"tool_use""#));+ assert!(rendered.contains("message_stop"));+ assert!(translator.is_finished());+ }++ #[test]+ fn finishes_after_closed_completed_tool_call() {+ let mut translator = LiveStreamTranslator::new("msg_1", "gpt-5.5");+ let mut out = Vec::new();+ for event in [+ json!({+ "type": "response.output_item.added",+ "output_index": 0,+ "item": {"type":"function_call","call_id":"call_1","name":"WebSearch"}+ }),+ json!({+ "type": "response.function_call_arguments.done",+ "output_index": 0,+ "arguments": "{\"query\":\"claude-code-proxy github\"}"+ }),+ json!({+ "type": "response.output_item.done",+ "output_index": 0,+ "item": {+ "type":"function_call",+ "call_id":"call_1",+ "name":"WebSearch",+ "arguments":"{\"query\":\"claude-code-proxy github\"}"+ }+ }),+ ] {+ out.extend(translator.accept(&event, None).unwrap());+ }+ out.extend(translator.finish_after_closed_completed_tool_call(None));+ let rendered = String::from_utf8(out).unwrap();+ assert!(rendered.contains("content_block_start"));+ assert!(rendered.contains("input_json_delta"));+ assert!(rendered.contains(r#""stop_reason":"tool_use""#));+ assert!(rendered.contains("message_stop"));+ assert!(!rendered.contains("event: error"));+ }++ #[test]+ fn rate_limit_event_returns_error() {+ let mut translator = LiveStreamTranslator::new("msg_1", "gpt-5.5");+ let err = translator+ .accept(+ &json!({+ "type": "codex.rate_limits",+ "rate_limits": {"limit_reached": true}+ }),+ None,+ )+ .unwrap_err();+ assert_eq!(err, "rate limit reached");+ }}diff --git a/src/providers/codex/translate/reducer.rs b/src/providers/codex/translate/reducer.rsindex 11a3f84..11da381 100644--- a/src/providers/codex/translate/reducer.rs+++ b/src/providers/codex/translate/reducer.rs@@ -59,6 +59,9 @@ pub const TERM_COMPLETED: &str = "response.completed";pub const TERM_INCOMPLETE: &str = "response.incomplete";pub const TERM_DONE: &str = "response.done";+const BUFFERED_READ_REPAIR_TRAILING_WHITESPACE_BYTES: usize = 1_024;+const BUFFERED_TOOL_MAX_ARGS_BYTES: usize = 5_000_000;+#[derive(Debug, Clone)]pub enum ReducerEvent {ThinkingStart {@@ -453,50 +456,70 @@ pub fn reduce_upstream_bytes(input: &[u8]) -> Result<Vec<ReducerEvent>, UpstreamSome(s) => s,None => continue,};+ let mut repaired_read: Option<(usize, String)> = None;match state {BlockState::Tool {args_accum,had_delta,buffer_until_done,emitted_args,- name: _,+ name,+ index,..} => {args_accum.push_str(delta);*had_delta = true;+ if args_accum.len() > BUFFERED_TOOL_MAX_ARGS_BYTES {+ return Err(UpstreamStreamError {+ kind: UpstreamErrorKind::Failed,+ message: format!("Buffered {name} tool arguments exceeded safe limits"),+ retry_after_seconds: None,+ diagnostics: None,+ });+ }- // Handle buffered Read tool argsif *buffer_until_done {- // Only emit progress, not delta- if let Some(idx) =- blocks_by_output_index- .get(&output_index)- .and_then(|s| match s {- BlockState::Tool { index, .. } => Some(*index),- _ => None,- })+ if let Some(repaired) =+ repair_whitespace_stalled_read_args(name, args_accum){- out.push(ReducerEvent::ToolProgress { index: idx });+ *args_accum = repaired.clone();+ *emitted_args = true;+ repaired_read = Some((*index, repaired));+ } else {+ out.push(ReducerEvent::ToolProgress { index: *index });}} else {*emitted_args = true;- if let Some(idx) =- blocks_by_output_index- .get(&output_index)- .and_then(|s| match s {- BlockState::Tool { index, .. } => Some(*index),- _ => None,- })- {- out.push(ReducerEvent::ToolDelta {- index: idx,- partial_json: delta.to_string(),- });- }+ out.push(ReducerEvent::ToolDelta {+ index: *index,+ partial_json: delta.to_string(),+ });}}_ => continue,}+ if let Some((index, repaired)) = repaired_read {+ if let Some(state) = blocks_by_output_index.remove(&output_index) {+ capture_output_item(output_index, &state, &mut output_items_by_index);+ }+ out.push(ReducerEvent::ToolDelta {+ index,+ partial_json: repaired,+ });+ out.push(ReducerEvent::ToolStop { index });+ let output_items: Vec<ResponsesInputItem> =+ output_items_by_index.into_values().collect();+ out.push(ReducerEvent::Finish {+ stop_reason: STOP_TOOL_USE,+ terminal_type: TERM_INCOMPLETE.to_string(),+ continuation_eligible: false,+ usage: None,+ web_search_requests,+ response_id: None,+ output_items,+ });+ return Ok(out);+ }continue;}@@ -768,6 +791,69 @@ fn sanitize_tool_args(name: &str, args: &str) -> String {serde_json::to_string(&sanitized).unwrap_or_else(|_| args.to_string())}+fn repair_whitespace_stalled_read_args(name: &str, args: &str) -> Option<String> {+ if name != "Read" {+ return None;+ }+ let trimmed = args.trim_end();+ let trailing_whitespace = args.len().saturating_sub(trimmed.len());+ if trailing_whitespace < BUFFERED_READ_REPAIR_TRAILING_WHITESPACE_BYTES {+ return None;+ }+ parse_read_args_candidate(trimmed).or_else(|| {+ let with_brace = format!("{trimmed}}}");+ parse_read_args_candidate(&with_brace)+ })+}++fn parse_read_args_candidate(args: &str) -> Option<String> {+ let parsed: serde_json::Value = serde_json::from_str(args).ok()?;+ if !is_valid_read_args(&parsed) {+ return None;+ }+ Some(sanitize_tool_args(+ "Read",+ &serde_json::to_string(&parsed).ok()?,+ ))+}++fn is_valid_read_args(value: &serde_json::Value) -> bool {+ let Some(obj) = value.as_object() else {+ return false;+ };+ for key in obj.keys() {+ if !matches!(key.as_str(), "file_path" | "offset" | "limit" | "pages") {+ return false;+ }+ }+ let Some(file_path) = obj.get("file_path").and_then(|v| v.as_str()) else {+ return false;+ };+ if file_path.is_empty() {+ return false;+ }+ if let Some(offset) = obj.get("offset").and_then(|v| v.as_i64())+ && offset < 0+ {+ return false;+ }+ if let Some(limit) = obj.get("limit").and_then(|v| v.as_i64())+ && limit <= 0+ {+ return false;+ }+ if obj.get("offset").is_some_and(|v| !v.is_i64()) {+ return false;+ }+ if obj.get("limit").is_some_and(|v| !v.is_i64()) {+ return false;+ }+ if obj.get("pages").is_some_and(|v| !v.is_string()) {+ return false;+ }+ true+}+fn web_search_query(item: &serde_json::Value) -> String {let action = match item.get("action") {Some(v) => v,@@ -1041,6 +1127,47 @@ mod tests {assert_eq!(result.unwrap_err().kind, UpstreamErrorKind::RateLimit);}+ #[test]+ fn reduce_repairs_whitespace_stalled_read_args() {+ let upstream = format!(+ "{}{}",+ sse(+ "response.output_item.added",+ json!({+ "output_index":0,+ "item":{"type":"function_call","call_id":"call_1","name":"Read"}+ })+ ),+ sse(+ "response.function_call_arguments.delta",+ json!({+ "output_index":0,+ "delta": format!("{{\"file_path\":\"/tmp/a\",\"pages\":\"\"{}", " ".repeat(1024))+ })+ )+ );+ let out = reduce_upstream_bytes(upstream.as_bytes()).unwrap();+ assert!(out.iter().any(|event| {+ matches!(+ event,+ ReducerEvent::ToolDelta { partial_json, .. }+ if partial_json == "{\"file_path\":\"/tmp/a\"}"+ )+ }));+ let last = out.last().unwrap();+ if let ReducerEvent::Finish {+ stop_reason,+ continuation_eligible,+ ..+ } = last+ {+ assert_eq!(*stop_reason, "tool_use");+ assert!(!continuation_eligible);+ } else {+ panic!("expected Finish");+ }+ }+#[test]fn reduce_upstream_error_event() {let upstream = sse("error", json!({"error":{"message":"upstream failure"}}));diff --git a/src/providers/codex/translate/request.rs b/src/providers/codex/translate/request.rsindex e7c1e67..1af09a4 100644--- a/src/providers/codex/translate/request.rs+++ b/src/providers/codex/translate/request.rs@@ -599,22 +599,44 @@ fn tool_result_to_string(content: &Value) -> String {let mut parts = Vec::new();for b in arr {match b.get("type").and_then(|v| v.as_str()) {- Some("text") => {- let text = b.get("text").and_then(|v| v.as_str()).unwrap_or("");- parts.push(text.to_string());- }+ Some("text") => match b.get("text").and_then(|v| v.as_str()) {+ Some(text) => parts.push(text.to_string()),+ None => parts.push(unsupported_tool_result_block_to_string(b)),+ },Some("image") => {- let source = b.get("source");- let media_type = source- .and_then(|s| s.get("media_type"))- .and_then(|v| v.as_str())- .unwrap_or("image");- parts.push(format!("[image omitted: {media_type}]"));+ if let Some(source) = b.get("source").and_then(|v| v.as_object()) {+ match source.get("type").and_then(|v| v.as_str()) {+ Some("url")+ if source.get("url").and_then(|v| v.as_str()).is_some() =>+ {+ parts.push("[image omitted: url]".to_string());+ }+ Some("base64")+ if source+ .get("media_type")+ .and_then(|v| v.as_str())+ .is_some()+ && source+ .get("data")+ .and_then(|v| v.as_str())+ .is_some() =>+ {+ let media_type = source+ .get("media_type")+ .and_then(|v| v.as_str())+ .unwrap_or("image");+ parts.push(format!("[image omitted: {media_type}]"));+ }+ _ => parts.push(unsupported_tool_result_block_to_string(b)),+ }+ } else {+ parts.push(unsupported_tool_result_block_to_string(b));+ }}Some(other) => {parts.push(format!("[unsupported content block omitted: {other}]"));}- None => {}+ None => parts.push(unsupported_tool_result_block_to_string(b)),}}parts.join("\n")@@ -623,6 +645,14 @@ fn tool_result_to_string(content: &Value) -> String {}}+fn unsupported_tool_result_block_to_string(block: &Value) -> String {+ let kind = block+ .get("type")+ .and_then(|v| v.as_str())+ .unwrap_or("unknown");+ format!("[unsupported content block omitted: {kind}]")+}+#[cfg(test)]mod tests {use super::*;@@ -808,6 +838,22 @@ mod tests {}}+ #[test]+ fn tool_result_stringifies_images_and_malformed_blocks() {+ let rendered = tool_result_to_string(&json!([+ {"type": "text", "text": "caption"},+ {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "abc"}},+ {"type": "image", "source": {"type": "url", "url": "https://example.invalid/a.png"}},+ {"type": "text"},+ {"type": "image"},+ {}+ ]));+ assert_eq!(+ rendered,+ "caption\n[image omitted: image/png]\n[image omitted: url]\n[unsupported content block omitted: text]\n[unsupported content block omitted: image]\n[unsupported content block omitted: unknown]"+ );+ }+#[test]fn translate_returns_only_expected_top_level_fields() {let req: MessagesRequest = serde_json::from_value(json!({diff --git a/src/providers/codex/websocket.rs b/src/providers/codex/websocket.rsindex f02c8d1..702b133 100644--- a/src/providers/codex/websocket.rs+++ b/src/providers/codex/websocket.rs@@ -24,7 +24,7 @@ use super::continuation::ContinuationCandidate;pub const WEBSOCKET_PROTOCOL_HEADER: &str = "responses_websockets=2026-02-06";pub const WEBSOCKET_CONNECT_TIMEOUT_MS: u64 = 15_000;-pub const WEBSOCKET_IDLE_TIMEOUT_MS: u64 = 60_000;+pub const WEBSOCKET_IDLE_TIMEOUT_MS: u64 = 300_000;pub const WEBSOCKET_RESPONSE_START_TIMEOUT_DETAIL: &str = "websocket_response_start_timeout";const POOL_IDLE_TTL_MS: u64 = 30 * 60 * 1000;