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:
andrea.difonzo
2026-11-11 11:41:54 +01:00
parent 86e7c72e5f
commit 8b73331037
130 changed files with 1204 additions and 410 deletions

View File

@@ -23,6 +23,38 @@ use tracing::warn;
const INITIAL_CONNECTION_RETRY_DELAY: Duration = Duration::from_secs(5);
const MAX_CONNECTION_RETRY_DELAY: Duration = Duration::from_secs(60);
/// Provider rate limits (HTTP 429) get more attempts than other stream failures.
const RATE_LIMIT_MIN_RETRIES: u64 = 8;
/// First local wait for a 429 without server advice; doubles on each attempt.
const RATE_LIMIT_INITIAL_DELAY: Duration = Duration::from_secs(2);
/// Longest single wait for a 429, even when the provider advises a later time.
const MAX_RATE_LIMIT_WAIT: Duration = Duration::from_secs(60);
/// Server advice beyond this is a long-window (e.g. daily) limit: retrying in the turn is futile.
const LONG_WINDOW_RATE_LIMIT: Duration = Duration::from_secs(15 * 60);
/// Retry budget for provider rate limits: twice the stream budget, at least
/// [`RATE_LIMIT_MIN_RETRIES`].
pub(crate) fn rate_limit_max_retries(max_retries: u64) -> u64 {
max_retries.saturating_mul(2).max(RATE_LIMIT_MIN_RETRIES)
}
/// Wait before retry `retry_count` (1-based) of a rate-limited request: the provider's advice
/// when present, otherwise an exponential backoff from [`RATE_LIMIT_INITIAL_DELAY`]; never
/// more than [`MAX_RATE_LIMIT_WAIT`].
pub(crate) fn rate_limit_delay(advised: Option<Duration>, retry_count: u64) -> Duration {
let local = || {
let exponent = u32::try_from(retry_count.saturating_sub(1)).unwrap_or(u32::MAX);
RATE_LIMIT_INITIAL_DELAY.saturating_mul(2u32.saturating_pow(exponent))
};
advised.unwrap_or_else(local).min(MAX_RATE_LIMIT_WAIT)
}
/// Message shown in the UI while waiting out a provider rate limit.
pub(crate) fn rate_limit_wait_message(delay: Duration, retry_count: u64, max_retries: u64) -> String {
let seconds = delay.as_secs_f64().ceil().max(1.0) as u64;
format!("Limite del provider raggiunto, riprovo tra {seconds} s ({retry_count}/{max_retries})")
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum ResponsesStreamRequest {
Sampling,
@@ -86,6 +118,36 @@ pub(crate) async fn handle_response_stream_error(
ResponsesStreamRequest::RemoteCompactionV2 => RetryOperation::RemoteCompactionV2,
};
let retry_count = retry_state.retries.saturating_add(1);
if let CodexErrorDetails::RateLimitExceeded(message) = err.details() {
let advised = err.server_retry_delay();
if advised.is_some_and(|delay| delay > LONG_WINDOW_RATE_LIMIT) {
return Err(CodexErr::new(
CodexErrorDetails::ProviderDailyLimitExhausted(message.clone()),
));
}
let max_rate_limit_retries = rate_limit_max_retries(max_retries);
if retry_state.retries >= max_rate_limit_retries {
sess.services
.thread_extension_data
.insert(ExhaustedResponseRetry {
turn_id: turn_context.sub_id.clone(),
retry_at: err.retry_after().map(RetryAfter::deadline),
});
return Err(err);
}
let delay = rate_limit_delay(advised, retry_count);
retry_state.retries = retry_count;
log_retry(request, turn_context, &err, retry_count, max_rate_limit_retries, delay);
sess.notify_stream_error(
turn_context,
rate_limit_wait_message(delay, retry_count, max_rate_limit_retries),
err,
)
.await;
akode_client::record_retry!(retry_count, delay, operation);
tokio::time::sleep(delay).await;
return Ok(());
}
let Some(delay) = err.retry_delay(retry_count) else {
return Err(err);
};
@@ -208,3 +270,40 @@ fn log_retry(
#[cfg(test)]
#[path = "responses_retry_tests.rs"]
mod tests;
#[cfg(test)]
mod rate_limit_tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn rate_limits_get_a_larger_budget() {
assert_eq!(rate_limit_max_retries(0), RATE_LIMIT_MIN_RETRIES);
assert_eq!(rate_limit_max_retries(5), 10);
assert!(rate_limit_max_retries(5) > 5);
}
#[test]
fn rate_limit_waits_follow_advice_and_are_capped() {
assert_eq!(rate_limit_delay(Some(Duration::from_secs(7)), 1), Duration::from_secs(7));
assert_eq!(
rate_limit_delay(Some(Duration::from_secs(600)), 1),
MAX_RATE_LIMIT_WAIT
);
assert_eq!(rate_limit_delay(None, 1), Duration::from_secs(2));
assert_eq!(rate_limit_delay(None, 3), Duration::from_secs(8));
assert_eq!(rate_limit_delay(None, 20), MAX_RATE_LIMIT_WAIT);
}
#[test]
fn rate_limit_message_is_italian_and_rounds_up() {
assert_eq!(
rate_limit_wait_message(Duration::from_millis(1500), 1, 8),
"Limite del provider raggiunto, riprovo tra 2 s (1/8)"
);
assert_eq!(
rate_limit_wait_message(Duration::ZERO, 2, 8),
"Limite del provider raggiunto, riprovo tra 1 s (2/8)"
);
}
}