Skip to content

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

fix(session): prevent stale credential cache from delaying logout

Steven Enamakel697e660bb81e
diff --git a/app/src/services/coreStateApi.ts b/app/src/services/coreStateApi.ts
index c505a2f157..224b4269b1 100644
--- a/app/src/services/coreStateApi.ts
+++ b/app/src/services/coreStateApi.ts
@@ -152,6 +152,7 @@ export const fetchCoreAppSnapshot = async (): Promise<AppStateSnapshotResult> =>
}
if (!result.currentUser && result.auth?.user) {
result.currentUser = result.auth.user as User;
+ result.currentUserStale = true;
}
return result;
};
diff --git a/app/src/services/session/sessionOwner.ts b/app/src/services/session/sessionOwner.ts
index 3e8094d7b5..1b4dff0ad5 100644
--- a/app/src/services/session/sessionOwner.ts
+++ b/app/src/services/session/sessionOwner.ts
@@ -215,7 +215,6 @@ const browserCurrentUser = async (force: boolean): Promise<SessionCurrentUser> =
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
if (message.startsWith('REJECTED:')) {
- browserCurrentUserCache = null;
// A login may have published a newer token while this request was in
// flight. Never clear that newer browser-owned session on an old
// request's rejection.
@@ -223,12 +222,13 @@ const browserCurrentUser = async (force: boolean): Promise<SessionCurrentUser> =
method: 'openhuman.auth_get_session_token',
}).catch(() => null);
if (latest?.result.token === token) {
+ browserCurrentUserCache = null;
await callCoreRpc({
method: 'openhuman.auth_clear_credential',
params: { kind: 'session' },
}).catch(() => undefined);
} else {
- return { user: core.user, stale: true, staleSeconds: null };
+ return browserCurrentUser(false);
}
// Same confirmed-expiry signal the shell owner raises through
// `auth://expired`: the credential is gone, so the core-state layer
@@ -280,7 +280,13 @@ export const logoutSession = async (): Promise<void> => {
return;
}
browserCurrentUserCache = null;
- await callCoreRpc({ method: 'openhuman.auth_clear_credential', params: { kind: 'session' } });
+ const state = await callCoreRpc<{
+ result: { credential?: 'session' | 'local' | 'api-key' | null };
+ }>({ method: 'openhuman.auth_get_state' });
+ const kind = state.result.credential;
+ if (kind === 'session' || kind === 'local') {
+ await callCoreRpc({ method: 'openhuman.auth_clear_credential', params: { kind } });
+ }
};
/**
diff --git a/crates/openhuman-session/src/cache.rs b/crates/openhuman-session/src/cache.rs
index 570c1bf065..ea5c46e757 100644
--- a/crates/openhuman-session/src/cache.rs
+++ b/crates/openhuman-session/src/cache.rs
@@ -205,6 +205,11 @@ 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");
@@ -249,12 +254,7 @@ impl CurrentUserCache {
let generation = self.lock().generation;
if !force {
- // A background rejection is authoritative. Surface it before a
- // stale positive entry so the owning manager can clear the token.
if let Some((error, consecutive, retry_in)) = self.suppressed(&key) {
- 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/test_support.rs b/crates/openhuman-session/src/test_support.rs
index 8e62bfc6a8..7f24683e6c 100644
--- a/crates/openhuman-session/src/test_support.rs
+++ b/crates/openhuman-session/src/test_support.rs
@@ -289,7 +289,9 @@ impl CoreLink for FakeCore {
}
if kind == "api-key" {
*self.api_key.lock().unwrap() = Some(token);
+ *self.session.lock().unwrap() = None;
} else {
+ *self.api_key.lock().unwrap() = None;
*self.session.lock().unwrap() = Some(StoredCredential {
kind,
token,
diff --git a/gitbooks/features/cloud-deploy.md b/gitbooks/features/cloud-deploy.md
index b48d50295d..80c04b75e3 100644
--- a/gitbooks/features/cloud-deploy.md
+++ b/gitbooks/features/cloud-deploy.md
@@ -437,7 +437,7 @@ elsewhere and hand it to the core at boot:
Either installs the credential only when the store has none of that kind; to
rotate, clear first (`openhuman-core auth clear_credential --kind api-key`).
-The same operation is available over RPC as `openhuman.auth_set_credential`.
+The same operation is available over RPC as `openhuman.auth_clear_credential`.
---