Skip to content

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

Rust

test(llm): cover the Copilot token exchange's transient classification

AkitaOnRails74a399f23869
diff --git a/crates/ai-memory-llm/src/copilot.rs b/crates/ai-memory-llm/src/copilot.rs
index 6558e1e8..4c82afd4 100644
--- a/crates/ai-memory-llm/src/copilot.rs
+++ b/crates/ai-memory-llm/src/copilot.rs
@@ -255,7 +255,13 @@ impl CopilotAuthState {
})?;
info!("copilot API token expired or missing, exchanging GitHub token");
- let exchanged = exchange_copilot_token(&self.client, self.timeout, &github).await?;
+ let exchanged = exchange_copilot_token(
+ &self.client,
+ self.timeout,
+ &github,
+ GITHUB_COPILOT_TOKEN_URL,
+ )
+ .await?;
guard.copilot_access = Some(exchanged.access.clone());
guard.copilot_expires_at_ms = Some(exchanged.expires_at_ms);
guard.api_base_url = Some(resolve_copilot_api_base_url(
@@ -646,9 +652,10 @@ async fn exchange_copilot_token(
client: &reqwest::Client,
timeout: Duration,
github_token: &SecretString,
+ token_url: &str,
) -> LlmResult<CopilotApiToken> {
let resp = client
- .get(GITHUB_COPILOT_TOKEN_URL)
+ .get(token_url)
.timeout(timeout)
.bearer_auth(github_token.expose_secret())
.headers(copilot_token_exchange_headers())
@@ -665,7 +672,7 @@ async fn exchange_copilot_token(
if LlmError::status_is_transient(status.as_u16()) {
return Err(LlmError::Provider {
status: status.as_u16(),
- body,
+ body: format!("copilot token exchange: {body}"),
});
}
return Err(LlmError::Auth(format!(
@@ -1144,6 +1151,40 @@ mod tests {
use super::*;
+ #[tokio::test]
+ async fn token_exchange_treats_a_throttled_endpoint_as_transient() {
+ use wiremock::matchers::method;
+ use wiremock::{Mock, MockServer, ResponseTemplate};
+
+ for (code, transient) in [(429u16, true), (503, true), (401, false)] {
+ let server = MockServer::start().await;
+ Mock::given(method("GET"))
+ .respond_with(ResponseTemplate::new(code).set_body_string("{}"))
+ .expect(1)
+ .mount(&server)
+ .await;
+
+ let Err(err) = exchange_copilot_token(
+ &reqwest::Client::new(),
+ Duration::from_secs(5),
+ &SecretString::from("gho_test"),
+ &server.uri(),
+ )
+ .await
+ else {
+ panic!("{code} from the token endpoint must fail");
+ };
+
+ assert_eq!(err.is_transient(), transient, "{code}: {err:?}");
+ if transient {
+ assert_eq!(err.http_status(), Some(code), "{code}: {err:?}");
+ assert!(err.to_string().contains("copilot token exchange"), "{err}");
+ } else {
+ assert!(matches!(err, LlmError::Auth(_)), "{code}: {err:?}");
+ }
+ }
+ }
+
#[test]
fn token_file_round_trips_copilot_entry() {
let dir = tempfile::tempdir().unwrap();
diff --git a/crates/ai-memory-llm/src/stored_token.rs b/crates/ai-memory-llm/src/stored_token.rs
index e36176bf..cfc8afc7 100644
--- a/crates/ai-memory-llm/src/stored_token.rs
+++ b/crates/ai-memory-llm/src/stored_token.rs
@@ -183,7 +183,7 @@ where
if LlmError::status_is_transient(status.as_u16()) {
return Err(LlmError::Provider {
status: status.as_u16(),
- body,
+ body: format!("{failure_label}: {body}"),
});
}
return Err(LlmError::Auth(format!(
@@ -272,6 +272,10 @@ mod tests {
"{wire} has not rejected the grant: {err:?}"
);
assert_eq!(err.http_status(), Some(code), "{wire}: {err:?}");
+ assert!(
+ err.to_string().contains("test refresh failed"),
+ "{wire} must name the failing exchange: {err}"
+ );
assert!(
!err.to_string().contains("Run `test login` again."),
"{wire} must not tell the operator to re-login: {err}"
diff --git a/docs/llm-provider-fallback.md b/docs/llm-provider-fallback.md
index 10da84db..108e9987 100644
--- a/docs/llm-provider-fallback.md
+++ b/docs/llm-provider-fallback.md
@@ -113,6 +113,11 @@ the wrapper, while callers still receive `Option<Arc<dyn LlmProvider>>`.
| 400 / 401 / 403 / 404 / 422 | No | Usually request, capability, model, or credential configuration. |
| schema / response-shape / deserialize error | No | Same input would deterministically fail again. |
+The same rule covers the credential exchanges in front of a provider: the
+Copilot token exchange and the `openai-oauth`/OIDC refresh. A 429 or 5xx from
+those token endpoints is transient, because the grant was not rejected; any
+other non-2xx stays an auth failure with its re-login hint.
+
Before a candidate is called, the wrapper checks an in-memory circuit keyed by
`(provider, model)`. A transient error opens that candidate's circuit for a
bounded cooldown; other candidates remain eligible. A successful response