Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(cache): retain rejected credentials until the owner observes them

Steven Enamakele23664546f20
diff --git a/crates/openhuman-session/src/cache.rs b/crates/openhuman-session/src/cache.rs
index 7c0b39037a..a75afd0c1a 100644
--- a/crates/openhuman-session/src/cache.rs
+++ b/crates/openhuman-session/src/cache.rs
@@ -205,11 +205,6 @@ impl CurrentUserCache {
}
fn record_failure(&self, generation: u64, key: &Key, error: FetchMeError) {
- // Credential rejection is authoritative and must be handed to the
- // owner immediately; caching it delays logout behind backoff.
- if matches!(error, FetchMeError::Rejected(_)) {
- return;
- }
let mut state = self.lock();
if state.generation != generation {
log::debug!("{LOG_PREFIX} discarding failure that raced sign-out");
@@ -255,6 +250,12 @@ impl CurrentUserCache {
if !force {
if let Some((error, consecutive, retry_in)) = self.suppressed(&key) {
+ // Rejections are authoritative. A stale-while-revalidate
+ // caller cannot observe the background result directly, so
+ // retain it until the next read can hand it to the owner.
+ if matches!(error, FetchMeError::Rejected(_)) {
+ return Err(error);
+ }
return Err(FetchMeError::Suppressed {
message: error.message().to_string(),
consecutive,
diff --git a/crates/openhuman-session/src/cache_tests.rs b/crates/openhuman-session/src/cache_tests.rs
index 0ec2326376..7ea44139e2 100644
--- a/crates/openhuman-session/src/cache_tests.rs
+++ b/crates/openhuman-session/src/cache_tests.rs
@@ -141,7 +141,7 @@ async fn availability_failure_opens_a_backoff_window() {
}
#[tokio::test]
-async fn rejection_is_never_cached() {
+async fn rejection_is_retained_until_the_owner_observes_it() {
let backend = Backend::start(vec![MeAnswer::Status(401)]).await;
let cache = CurrentUserCache::new();
let cred = Credential::session(LIVE_JWT.as_str());
@@ -152,7 +152,7 @@ async fn rejection_is_never_cached() {
Err(FetchMeError::Rejected(_))
));
}
- assert_eq!(backend.me_calls(), 2);
+ assert_eq!(backend.me_calls(), 1);
}
#[tokio::test]
diff --git a/crates/openhuman-session/src/manager.rs b/crates/openhuman-session/src/manager.rs
index 7cb95f0fcd..07fc27671c 100644
--- a/crates/openhuman-session/src/manager.rs
+++ b/crates/openhuman-session/src/manager.rs
@@ -347,10 +347,14 @@ impl<L: CoreLink> SessionManager<L> {
log::warn!(
"{LOG_PREFIX} pending session rejected by backend; clearing: {reason}"
);
- manager
+ if manager
.clear_session_credential("pending-revalidation")
- .await;
- return;
+ .await
+ {
+ return;
+ }
+ drop(guard);
+ delay = (delay * 2).min(REVALIDATION_MAX_DELAY);
}
Err(error) => {
drop(guard);
@@ -379,9 +383,14 @@ impl<L: CoreLink> SessionManager<L> {
self.emit(SessionEvent::Expired {
source: source.to_string(),
});
- // The credential is gone, so the signed-out state is known without
- // asking the core again (and `state()` would recurse into here).
- self.emit(SessionEvent::Changed(SessionState::default()));
+ // Re-read the core after clearing the session. An API key profile may
+ // still be active and must not be reported as a signed-out state.
+ match self.state().await {
+ Ok(state) => self.emit(SessionEvent::Changed(state)),
+ Err(error) => log::warn!(
+ "{LOG_PREFIX} cleared rejected session but could not read the post-clear state: {error}"
+ ),
+ }
true
}