-
Notifications
You must be signed in to change notification settings - Fork 3
fix(codex): surface streaming fatal errors #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| //! Terminal SSE error rendering for Codex streams that fail after downstream | ||
| //! bytes have already been sent. | ||
| //! | ||
| //! This module deliberately does not classify upstream failures. The dispatcher | ||
| //! already owns that job; this file only translates a classified failure into | ||
| //! the terminal shape expected by the client-facing protocol. | ||
| //! | ||
| //! ```text | ||
| //! upstream SSE event | ||
| //! | | ||
| //! v | ||
| //! classify_codex_sse_event_failure(...) | ||
| //! | | ||
| //! v | ||
| //! CodexClassifiedUpstreamError + effective HTTP status | ||
| //! | | ||
| //! v | ||
| //! codex_stream_failure_chunks(adapter, path, status, error) | ||
| //! | | ||
| //! +-- Responses ---------> event: response.failed | ||
| //! | data: {"type":"response.failed", ...} | ||
| //! | | ||
| //! +-- ChatCompletions ---> data: {"error":{...}} | ||
| //! | data: [DONE] | ||
| //! | | ||
| //! `-- AnthropicMessages -> event: error | ||
| //! data: {"type":"error","error":{...}} | ||
| //! ``` | ||
| //! | ||
| //! The important boundary is "after downstream write started": at that point we | ||
| //! cannot change the HTTP status and we cannot safely fail over to another | ||
| //! account without mixing two upstream conversations in one client stream. | ||
| //! Returning a protocol-shaped terminal error is the least surprising contract: | ||
| //! clients see the real failure, usage still records the upstream failure, and | ||
| //! the stream does not look like a normal empty response. | ||
| //! | ||
| //! Error-body policy: | ||
| //! - Surface only the sanitized classified message/type/code to the client. | ||
| //! - Keep raw upstream error bodies in usage metadata, not in SSE frames. | ||
| //! - Leave preflight failures to the existing non-stream JSON error path, where | ||
| //! the HTTP response has not been committed yet. | ||
|
|
||
| use axum::{body::Bytes, http::StatusCode}; | ||
| use llm_access_codex::types::GatewayResponseAdapter; | ||
| use serde_json::{json, Value}; | ||
|
|
||
| use super::{ | ||
| codex_upstream_error::CodexClassifiedUpstreamError, | ||
| errors::{codex_error_type_for_status, codex_surface_error_body_with_code}, | ||
| }; | ||
|
|
||
| /// Build the final client-visible chunks for a stream that can no longer | ||
| /// return a regular HTTP error response. | ||
| /// | ||
| /// The output is intentionally tiny: one SSE event for Responses/Anthropic, or | ||
| /// an OpenAI-style error data frame plus `[DONE]` for Chat Completions. Keeping | ||
| /// this as a `Vec<Bytes>` makes the dispatcher branch explicit without adding | ||
| /// another stream abstraction for at most two frames. | ||
| pub(super) fn codex_stream_failure_chunks( | ||
| response_adapter: GatewayResponseAdapter, | ||
| original_path: &str, | ||
| effective_status: StatusCode, | ||
| error: &CodexClassifiedUpstreamError, | ||
| ) -> Vec<Bytes> { | ||
| match response_adapter { | ||
| GatewayResponseAdapter::Responses => vec![encode_sse_event( | ||
| "response.failed", | ||
| &responses_failed_payload(effective_status, error), | ||
| )], | ||
| GatewayResponseAdapter::ChatCompletions => vec![ | ||
| encode_sse_data(&codex_surface_error_body_with_code( | ||
| original_path, | ||
| effective_status, | ||
| &error.message, | ||
| error.class.surface_error_code(), | ||
| )), | ||
| Bytes::from_static(b"data: [DONE]\n\n"), | ||
| ], | ||
| GatewayResponseAdapter::AnthropicMessages => { | ||
| vec![encode_sse_event("error", &anthropic_error_payload(effective_status, error))] | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| fn responses_failed_payload( | ||
| effective_status: StatusCode, | ||
| error: &CodexClassifiedUpstreamError, | ||
| ) -> Value { | ||
| json!({ | ||
| "type": "response.failed", | ||
| "response": { | ||
| "status": "failed", | ||
| "error": stream_error_object(effective_status, error), | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| fn anthropic_error_payload( | ||
| effective_status: StatusCode, | ||
| error: &CodexClassifiedUpstreamError, | ||
| ) -> Value { | ||
| json!({ | ||
| "type": "error", | ||
| "error": stream_error_object(effective_status, error), | ||
| }) | ||
| } | ||
|
|
||
| fn stream_error_object( | ||
| effective_status: StatusCode, | ||
| error: &CodexClassifiedUpstreamError, | ||
| ) -> Value { | ||
| json!({ | ||
| "type": codex_error_type_for_status(effective_status), | ||
| "message": error.message, | ||
| "code": error.class.surface_error_code().map_or(Value::Null, Value::from), | ||
| }) | ||
| } | ||
|
|
||
| fn encode_sse_event(event: &str, payload: &Value) -> Bytes { | ||
| Bytes::from(format!("event: {event}\ndata: {}\n\n", encode_json(payload))) | ||
| } | ||
|
|
||
| fn encode_sse_data(data: &str) -> Bytes { | ||
| Bytes::from(format!("data: {data}\n\n")) | ||
| } | ||
|
|
||
| fn encode_json(value: &Value) -> String { | ||
| serde_json::to_string(value).unwrap_or_else(|_| "{}".to_string()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While serializing the JSON payload for an SSE event, any potential error is currently suppressed, and an empty JSON object
{}is returned. This could hide underlying issues with payload construction.It would be more robust to log the serialization error to aid in debugging, even if it's considered an unlikely event.
You can achieve this by using
tracing::error!. You'll also need to adduse tracing;at the top of the file.