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:
@@ -33,7 +33,7 @@ impl LocalAgentRuntime {
|
||||
};
|
||||
if !matches!(
|
||||
(namespace.as_str(), name.as_str()),
|
||||
("codex_app" | "akode_tui", "send_message_to_thread")
|
||||
("codex_app" | "codex_tui", "send_message_to_thread")
|
||||
| ("codex_app", "create_thread")
|
||||
| ("cloud_threads", "send_message")
|
||||
) {
|
||||
|
||||
@@ -531,7 +531,7 @@ async fn responses_request_includes_internal_metadata_for_provider_grant_or_firs
|
||||
);
|
||||
let ws_request = akode_api::ResponseCreateWsRequest {
|
||||
client_metadata: Some(ws_client_metadata),
|
||||
..codex_api::ResponseCreateWsRequest::from(&request)
|
||||
..akode_api::ResponseCreateWsRequest::from(&request)
|
||||
};
|
||||
let ws_request = serde_json::to_value(ws_request)?;
|
||||
assert_eq!(ws_request["input"], expected_wire_input);
|
||||
|
||||
@@ -1131,7 +1131,7 @@ async fn build_guardian_prompt_items_explains_network_access_review_scope() -> a
|
||||
settings.set_prepend_module_to_snapshot(false);
|
||||
settings.bind(|| {
|
||||
assert_snapshot!(
|
||||
"codex_core__guardian__tests__network_access_guardian_prompt_layout",
|
||||
"akode_core__guardian__tests__network_access_guardian_prompt_layout",
|
||||
normalize_guardian_snapshot_paths(text)
|
||||
);
|
||||
});
|
||||
@@ -1844,7 +1844,7 @@ async fn guardian_review_request_layout_matches_model_visible_request_snapshot()
|
||||
settings.set_prepend_module_to_snapshot(false);
|
||||
settings.bind(|| {
|
||||
assert_snapshot!(
|
||||
"codex_core__guardian__tests__guardian_review_request_layout",
|
||||
"akode_core__guardian__tests__guardian_review_request_layout",
|
||||
normalize_guardian_snapshot_paths(format_guardian_requests_snapshot(
|
||||
"Guardian review request layout",
|
||||
&[("Guardian Review Request", &request)],
|
||||
@@ -2297,7 +2297,7 @@ async fn guardian_reuses_prompt_cache_key_and_appends_prior_reviews() -> anyhow:
|
||||
settings.set_prepend_module_to_snapshot(false);
|
||||
settings.bind(|| {
|
||||
assert_snapshot!(
|
||||
"codex_core__guardian__tests__guardian_followup_review_request_layout",
|
||||
"akode_core__guardian__tests__guardian_followup_review_request_layout",
|
||||
format!(
|
||||
"{}\n\nshared_prompt_cache_key: {}\nfollowup_contains_first_rationale: {}",
|
||||
normalize_guardian_snapshot_paths(format_guardian_requests_snapshot(
|
||||
|
||||
@@ -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)"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1906,7 +1906,7 @@ async fn reload_user_config_layer_refreshes_hooks() -> anyhow::Result<()> {
|
||||
.with_user_config(&config_toml_path, user_config.clone())
|
||||
.expect("hook user config should be valid"),
|
||||
),
|
||||
..codex_hooks::HooksConfig::default()
|
||||
..akode_hooks::HooksConfig::default()
|
||||
});
|
||||
assert_eq!(hook_list.hooks.len(), 1);
|
||||
assert_eq!(
|
||||
@@ -4083,7 +4083,7 @@ async fn fork_startup_context_then_first_turn_diff_snapshot() -> anyhow::Result<
|
||||
.expect("rollout path");
|
||||
|
||||
initial
|
||||
.akode
|
||||
.codex
|
||||
.start_or_steer_turn(ExternalTurnInputRequest::user_input(vec![
|
||||
UserInput::Text {
|
||||
text: "fork seed".into(),
|
||||
@@ -4096,7 +4096,7 @@ async fn fork_startup_context_then_first_turn_diff_snapshot() -> anyhow::Result<
|
||||
// before snapshotting from it.
|
||||
initial.codex.ensure_rollout_materialized().await;
|
||||
initial
|
||||
.akode
|
||||
.codex
|
||||
.flush_rollout()
|
||||
.await
|
||||
.expect("source rollout should flush before fork");
|
||||
@@ -4149,7 +4149,7 @@ async fn fork_startup_context_then_first_turn_diff_snapshot() -> anyhow::Result<
|
||||
settings.set_prepend_module_to_snapshot(false);
|
||||
settings.bind(|| {
|
||||
insta::assert_snapshot!(
|
||||
"codex_core__codex_tests__fork_startup_context_then_first_turn_diff",
|
||||
"akode_core__codex_tests__fork_startup_context_then_first_turn_diff",
|
||||
snapshot
|
||||
);
|
||||
});
|
||||
@@ -12953,7 +12953,7 @@ async fn session_start_hooks_only_load_from_trusted_project_layers() -> std::io:
|
||||
let hook_list = akode_hooks::list_hooks(akode_hooks::HooksConfig {
|
||||
feature_enabled: true,
|
||||
config_layer_stack: Some(config.config_layer_stack.clone()),
|
||||
..codex_hooks::HooksConfig::default()
|
||||
..akode_hooks::HooksConfig::default()
|
||||
});
|
||||
let expected_source_path = akode_utils_absolute_path::AbsolutePathBuf::from_absolute_path(
|
||||
nested_dot_codex.join("hooks.json"),
|
||||
@@ -13013,7 +13013,7 @@ async fn session_start_hooks_require_project_trust_without_config_toml() -> std:
|
||||
let hook_list = akode_hooks::list_hooks(akode_hooks::HooksConfig {
|
||||
feature_enabled: true,
|
||||
config_layer_stack: Some(config.config_layer_stack.clone()),
|
||||
..codex_hooks::HooksConfig::default()
|
||||
..akode_hooks::HooksConfig::default()
|
||||
});
|
||||
assert_eq!(
|
||||
hook_list.hooks.len(),
|
||||
|
||||
Reference in New Issue
Block a user