Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(llm): treat a throttled or overloaded token endpoint as transient

Rudy Mizrahi Celekli1e136d836be7
diff --git a/crates/ai-memory-llm/src/copilot.rs b/crates/ai-memory-llm/src/copilot.rs
index 1e991876..6558e1e8 100644
--- a/crates/ai-memory-llm/src/copilot.rs
+++ b/crates/ai-memory-llm/src/copilot.rs
@@ -658,6 +658,16 @@ async fn exchange_copilot_token(
let status = resp.status();
if !status.is_success() {
let body = provider_error_body(resp).await;
+ // GitHub rate-limits this endpoint per account. A 429/5xx here says
+ // nothing about the credential, and `LlmError::Auth` is not transient,
+ // so classifying it as auth stops the fallback chain and tells the
+ // operator to fix a token that is fine.
+ if LlmError::status_is_transient(status.as_u16()) {
+ return Err(LlmError::Provider {
+ status: status.as_u16(),
+ body,
+ });
+ }
return Err(LlmError::Auth(format!(
"copilot token exchange failed ({status}): {body}"
)));
diff --git a/crates/ai-memory-llm/src/error.rs b/crates/ai-memory-llm/src/error.rs
index 5c13a9dc..2dc3b90a 100644
--- a/crates/ai-memory-llm/src/error.rs
+++ b/crates/ai-memory-llm/src/error.rs
@@ -104,12 +104,21 @@ impl LlmError {
#[must_use]
pub fn is_transient(&self) -> bool {
match self {
- Self::Provider { status, .. } => *status == 429 || (500..=599).contains(status),
+ Self::Provider { status, .. } => Self::status_is_transient(*status),
Self::Http(e) => e.is_timeout() || e.is_connect(),
_ => false,
}
}
+ /// The status half of [`Self::is_transient`], for a call site that must
+ /// pick an error variant before one exists — a token endpoint answering
+ /// `429`/`5xx` has not rejected the credential, so it must not become an
+ /// `Auth` error, which is deterministic.
+ #[must_use]
+ pub fn status_is_transient(status: u16) -> bool {
+ status == 429 || (500..=599).contains(&status)
+ }
+
/// HTTP status captured from this error, when the failure carries one.
#[must_use]
pub fn http_status(&self) -> Option<u16> {
@@ -297,4 +306,30 @@ mod tests {
assert!(!err.class().contains("secret upstream body"));
}
}
+
+ #[test]
+ fn status_is_transient_matches_is_transient() {
+ for status in [429u16, 500, 503, 529, 599] {
+ assert!(LlmError::status_is_transient(status), "{status}");
+ assert!(
+ LlmError::Provider {
+ status,
+ body: String::new()
+ }
+ .is_transient(),
+ "{status} must agree with is_transient"
+ );
+ }
+ for status in [400u16, 401, 403, 404, 422, 600] {
+ assert!(!LlmError::status_is_transient(status), "{status}");
+ assert!(
+ !LlmError::Provider {
+ status,
+ body: String::new()
+ }
+ .is_transient(),
+ "{status} must agree with is_transient"
+ );
+ }
+ }
}
diff --git a/crates/ai-memory-llm/src/stored_token.rs b/crates/ai-memory-llm/src/stored_token.rs
index 8b097702..e36176bf 100644
--- a/crates/ai-memory-llm/src/stored_token.rs
+++ b/crates/ai-memory-llm/src/stored_token.rs
@@ -177,6 +177,15 @@ where
let status = resp.status();
if !status.is_success() {
let body = provider_error_body(resp).await;
+ // A rate-limited or overloaded token endpoint has not rejected the
+ // grant, so it must stay transient and must not print a re-login hint
+ // for a refresh token that is still valid.
+ if LlmError::status_is_transient(status.as_u16()) {
+ return Err(LlmError::Provider {
+ status: status.as_u16(),
+ body,
+ });
+ }
return Err(LlmError::Auth(format!(
"{failure_label} ({status}): {body}. {relogin_hint}"
)));
@@ -237,6 +246,39 @@ mod tests {
assert_eq!(parsed.access_token, "fresh-access");
}
+ #[tokio::test]
+ async fn refresh_grant_treats_a_rate_limited_endpoint_as_transient() {
+ for (wire, code) in [
+ ("429 Too Many Requests", 429u16),
+ ("503 Service Unavailable", 503),
+ ] {
+ let endpoint = serve_once(wire, r#"{"error":"slow_down"}"#).await;
+ let endpoint = credential_endpoint("test token endpoint", &endpoint).unwrap();
+ let client = reqwest::Client::new();
+
+ let err = refresh_grant::<TestResponse>(
+ &client,
+ &endpoint,
+ "refresh-x",
+ "client-y",
+ "test refresh failed",
+ "Run `test login` again.",
+ )
+ .await
+ .expect_err("a throttled token endpoint must fail");
+
+ assert!(
+ err.is_transient(),
+ "{wire} has not rejected the grant: {err:?}"
+ );
+ assert_eq!(err.http_status(), Some(code), "{wire}: {err:?}");
+ assert!(
+ !err.to_string().contains("Run `test login` again."),
+ "{wire} must not tell the operator to re-login: {err}"
+ );
+ }
+ }
+
#[tokio::test]
async fn refresh_grant_surfaces_status_body_and_relogin_hint() {
let endpoint = serve_once("401 Unauthorized", r#"{"error":"invalid_grant"}"#).await;