Files
codex/codex-rs/protocol/src/codex_error_info.rs
T
Alex Chang cc7a62e19c Report API spend limit errors by scope (#62329)
## Why

Generic quota errors hide whether the project or organization spend limit
blocked a request. Reporting the scope tells users which limit to check.

## What changed

Recognize API key, project, and organization spend limit errors across
HTTP, SSE, and WebSocket responses. Show a message that names the scope and
treat the error as non-retryable, even when the provider sends
`Retry-After`. Apply the existing usage-limit handling to goals and
compaction.

Expose distinct `CodexErrorInfo` values in the app-server protocol and
generated bindings so clients can identify the limit in live errors and
failed turn history.

## Testing

Extend the transport tests to cover all three scopes and verify that they
stop retries. Extend the app-server test to check that the error survives
`thread/read` and that a subsequent turn can succeed in the same thread.

GitOrigin-RevId: 03341f7a78daa7540e7baa519f1a29ec07e34509
2026-11-18 18:11:54 +00:00

93 lines
2.6 KiB
Rust

//! Decodes unknown error classifications as `Other` so enclosing records stay readable.
//! The wire definition keeps serialization exhaustive and known payload validation strict.
use crate::protocol::CodexErrorInfo;
use crate::protocol::NonSteerableTurnKind;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
use serde::de::Error as _;
use serde_json::Value;
#[derive(Serialize, Deserialize)]
#[serde(
remote = "CodexErrorInfo",
rename = "CodexErrorInfo",
rename_all = "snake_case"
)]
enum CodexErrorInfoWire {
ContextWindowExceeded,
SessionBudgetExceeded,
UsageLimitExceeded,
ApiKeySpendLimitExceeded,
ProjectSpendLimitExceeded,
OrganizationSpendLimitExceeded,
RateLimitExceeded,
FlexUnavailable,
ServerOverloaded,
ModelNotFound,
/// Capacity is unavailable on a route restricted by server policy.
PolicyCapacityUnavailable,
InferenceHookDenied,
CyberPolicy,
BioPolicy,
MisalignmentPolicyViolation,
TooManyDenials,
HttpConnectionFailed {
http_status_code: Option<u16>,
},
ResponseStreamConnectionFailed {
http_status_code: Option<u16>,
},
InternalServerError,
Unauthorized,
BadRequest,
InvalidPrompt,
SandboxError,
ResponseStreamDisconnected {
http_status_code: Option<u16>,
},
ResponseTooManyFailedAttempts {
http_status_code: Option<u16>,
},
ActiveTurnNotSteerable {
turn_kind: NonSteerableTurnKind,
},
ThreadRollbackFailed,
#[serde(other)]
Other,
}
impl Serialize for CodexErrorInfo {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
CodexErrorInfoWire::serialize(self, serializer)
}
}
impl<'de> Deserialize<'de> for CodexErrorInfo {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let value = Value::deserialize(deserializer)?;
// Probe the tag first so only unknown payloads are discarded: `serde(other)`
// recognizes unknown tags, but its unit variant rejects their payloads.
if let Value::Object(fields) = &value
&& fields.len() == 1
&& let Some(kind) = fields.keys().next()
&& kind != "other"
&& matches!(
CodexErrorInfoWire::deserialize(Value::String(kind.clone())),
Ok(Self::Other)
)
{
return Ok(Self::Other);
}
CodexErrorInfoWire::deserialize(value).map_err(D::Error::custom)
}
}