aKode fase 5: 429 con Retry-After/X-RateLimit-Reset e messaggi in italiano, filtro modelli nel primo avvio, correzioni della rinomina
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -256,10 +256,7 @@ fn map_api_error_details(err: ApiError) -> CodexErr {
|
||||
}
|
||||
}
|
||||
|
||||
CodexErr::RetryLimit(RetryLimitReachedError {
|
||||
status,
|
||||
request_id: extract_request_tracking_id(headers.as_ref()),
|
||||
})
|
||||
provider_rate_limit_error(&body_text)
|
||||
} else {
|
||||
CodexErr::UnexpectedStatus(UnexpectedResponseError {
|
||||
status,
|
||||
@@ -294,6 +291,40 @@ fn map_api_error_details(err: ApiError) -> CodexErr {
|
||||
}
|
||||
}
|
||||
|
||||
/// Maps a plain HTTP 429 from a provider to a retryable rate-limit error, or to a terminal
|
||||
/// error when the provider says its daily allowance is used up.
|
||||
fn provider_rate_limit_error(body_text: &str) -> CodexErr {
|
||||
let message = serde_json::from_str::<Value>(body_text)
|
||||
.ok()
|
||||
.and_then(|body| {
|
||||
body.get("error")
|
||||
.and_then(|error| error.get("message").or(Some(error)))
|
||||
.or_else(|| body.get("message"))
|
||||
.and_then(Value::as_str)
|
||||
.map(str::to_string)
|
||||
})
|
||||
.unwrap_or_else(|| body_text.trim().to_string());
|
||||
let message: String = message.chars().take(300).collect();
|
||||
if is_daily_limit_message(&message) || is_daily_limit_message(body_text) {
|
||||
CodexErr::new(CodexErrorDetails::ProviderDailyLimitExhausted(message))
|
||||
} else {
|
||||
CodexErr::new(CodexErrorDetails::RateLimitExceeded(message))
|
||||
}
|
||||
}
|
||||
|
||||
fn is_daily_limit_message(text: &str) -> bool {
|
||||
let text = text.to_ascii_lowercase();
|
||||
["per-day", "per day", "per_day", "daily", "requests per day", "rpd"]
|
||||
.iter()
|
||||
.any(|needle| {
|
||||
text.match_indices(needle).any(|(start, matched)| {
|
||||
let before = text[..start].chars().next_back();
|
||||
let after = text[start + matched.len()..].chars().next();
|
||||
!before.is_some_and(char::is_alphanumeric) && !after.is_some_and(char::is_alphanumeric)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const ACTIVE_LIMIT_HEADER: &str = "x-codex-active-limit";
|
||||
const REQUEST_ID_HEADER: &str = "x-request-id";
|
||||
const OAI_REQUEST_ID_HEADER: &str = "x-oai-request-id";
|
||||
@@ -317,10 +348,6 @@ const CLOUDFLARE_BLOCKED_MESSAGE: &str =
|
||||
#[path = "api_bridge_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
fn extract_request_tracking_id(headers: Option<&HeaderMap>) -> Option<String> {
|
||||
extract_request_id(headers).or_else(|| extract_header(headers, CF_RAY_HEADER))
|
||||
}
|
||||
|
||||
fn api_error_user_message(status: http::StatusCode, body: &str) -> Option<String> {
|
||||
if status == http::StatusCode::FORBIDDEN
|
||||
&& body.contains("Cloudflare")
|
||||
|
||||
@@ -152,11 +152,9 @@ async fn http_retry_deadline_survives_mapping_and_respects_terminal_errors() {
|
||||
error.to_string()
|
||||
),
|
||||
(
|
||||
CodexErrorInfo::ResponseTooManyFailedAttempts {
|
||||
http_status_code: Some(429),
|
||||
},
|
||||
Some(429),
|
||||
"exceeded retry limit, last status: 429 Too Many Requests".to_string(),
|
||||
CodexErrorInfo::RateLimitExceeded,
|
||||
None,
|
||||
"rate limit exceeded: sensitive upstream detail".to_string(),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -575,9 +573,7 @@ fn map_api_error_distinguishes_http_quota_errors_from_rate_limits() {
|
||||
serde_json::json!({"type": "rate_limit_error", "code": "slow_down"}),
|
||||
] {
|
||||
let expected = if error["type"] == "rate_limit_error" {
|
||||
CodexErrorInfo::ResponseTooManyFailedAttempts {
|
||||
http_status_code: Some(429),
|
||||
}
|
||||
CodexErrorInfo::RateLimitExceeded
|
||||
} else {
|
||||
CodexErrorInfo::UsageLimitExceeded
|
||||
};
|
||||
@@ -893,3 +889,38 @@ fn runtime_auth_denials_stop_retries_without_losing_http_diagnostics() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A plain provider 429 is a retryable rate limit; a daily allowance that is used up is terminal.
|
||||
#[test]
|
||||
fn map_api_error_classifies_provider_rate_limits() {
|
||||
let map = |body: &str| {
|
||||
map_api_error(ApiError::Transport(TransportError::Http {
|
||||
retry_after: None,
|
||||
status: http::StatusCode::TOO_MANY_REQUESTS,
|
||||
url: None,
|
||||
headers: None,
|
||||
body: Some(body.to_string()),
|
||||
}))
|
||||
};
|
||||
|
||||
let err = map(r#"{"error":{"message":"Rate limit exceeded: limit_rpm/google/gemma","code":429}}"#);
|
||||
assert_eq!(err.to_codex_protocol_error(), CodexErrorInfo::RateLimitExceeded);
|
||||
assert!(err.retry_delay(/*retry_count*/ 1).is_some());
|
||||
|
||||
for body in [
|
||||
r#"{"error":{"message":"Rate limit exceeded: free-models-per-day. Add 10 credits to unlock 1000 free model requests per day","code":429}}"#,
|
||||
"daily request quota reached",
|
||||
] {
|
||||
let err = map(body);
|
||||
assert_eq!(
|
||||
err.to_codex_protocol_error(),
|
||||
CodexErrorInfo::UsageLimitExceeded,
|
||||
"{body}"
|
||||
);
|
||||
assert_eq!(err.retry_delay(/*retry_count*/ 1), None, "{body}");
|
||||
assert!(
|
||||
err.to_string().starts_with("Limite giornaliero del provider esaurito."),
|
||||
"{err}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user