Skip to content

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

fix(desktop): restore snapshot state after failed load

Steven Enamakel171eb28cdb2f
diff --git a/crates/openhuman-core/src/desktop/app_state/ops/snapshot.rs b/crates/openhuman-core/src/desktop/app_state/ops/snapshot.rs
index 9d92e04cdc..12fc23d640 100644
--- a/crates/openhuman-core/src/desktop/app_state/ops/snapshot.rs
+++ b/crates/openhuman-core/src/desktop/app_state/ops/snapshot.rs
@@ -1,36 +1,26 @@
//! The `app_state_snapshot` and `update_local_state` RPC handlers: the
-//! polled entry point that assembles auth, current-user, runtime and local
-//! on-disk state into one response for the frontend.
+//! polled entry point that assembles auth, runtime and local on-disk state
+//! into one response for the frontend.
+//!
+//! The core reports the credential it holds and the user payload the host
+//! handed over with `auth.set_credential` (the host's own `/auth/me`
+//! answer). The *live* current user is the host's business — the desktop
+//! shell's `openhuman-session` cache — so this snapshot never talks to the
+//! backend.
-use super::auth_timeout::auth_fetch_timeout;
-use super::current_user::{
- current_user_api_base, fetch_current_user_cached, note_current_user_timeout,
- CurrentUserFetchError,
-};
-use super::current_user_fetch::sanitize_snapshot_user;
-use super::current_user_generation::{current_user_generation, CURRENT_USER_SESSION_MUTATION_LOCK};
-use super::pending_session::{
- clear_deferred_session_after_backend_rejection, clear_pending_backend_validation_flag,
- pending_session_user_id_for_cleanup, persist_revalidated_session_user, same_config_state_dir,
- snapshot_user_pending_backend_validation,
-};
use super::runtime_snapshot::{
build_runtime_snapshot, degraded_runtime_snapshot, RUNTIME_SNAPSHOT_TIMEOUT,
};
-use super::staleness::current_user_staleness;
use super::state_file::{
load_stored_app_state, load_stored_app_state_unlocked, save_stored_app_state_unlocked,
APP_STATE_FILE_LOCK,
};
use super::types::{AppStateSnapshot, StoredAppState, StoredAppStatePatch};
use super::LOG_PREFIX;
-use crate::api::rest::user_id_from_profile_payload;
use crate::config::rpc as config_rpc;
-use crate::config::Config;
use crate::rpc::RpcOutcome;
use crate::security::credentials::session_support::{
- is_local_session_token, load_app_session_profile, session_state_from_profile,
- session_token_from_profile,
+ load_app_session_profile, session_state_from_profile, session_token_from_profile,
};
use log::{debug, warn};
use serde_json::Value;
@@ -40,24 +30,14 @@ use std::time::Instant;
/// Correlates a snapshot call's own timing/debug log lines across the poll.
pub(super) static SNAPSHOT_REQ_COUNTER: AtomicU64 = AtomicU64::new(0);
-#[derive(Debug, Clone)]
-pub(super) enum SnapshotCurrentUser {
- User(Option<Value>),
- DeferredSessionRejected,
-}
-
-impl SnapshotCurrentUser {
- fn user(user: Option<Value>) -> Self {
- Self::User(user)
+fn sanitize_snapshot_user(user: Option<Value>) -> Option<Value> {
+ match user {
+ Some(Value::Object(map)) if map.is_empty() => None,
+ Some(Value::Null) => None,
+ other => other,
}
}
-pub(super) type SnapshotCurrentUserResult = (SnapshotCurrentUser, Option<Box<Config>>);
-
-pub(super) fn snapshot_current_user_result(user: Option<Value>) -> SnapshotCurrentUserResult {
- (SnapshotCurrentUser::user(user), None)
-}
-
pub async fn snapshot() -> Result<RpcOutcome<AppStateSnapshot>, String> {
let req_id = SNAPSHOT_REQ_COUNTER.fetch_add(1, Ordering::Relaxed);
let t_total = Instant::now();
@@ -74,308 +54,62 @@ pub async fn snapshot() -> Result<RpcOutcome<AppStateSnapshot>, String> {
let config_ms = t_config.elapsed().as_millis();
let t_auth = Instant::now();
- // Load the `app-session` auth profile exactly once and derive both
- // the session-state view and the raw token from it. The previous
- // implementation called `build_session_state` + `get_session_token`
- // separately, which acquired the auth-profile file lock twice per
- // snapshot. On Windows this doubled the surface area for the
- // "Timed out waiting for auth profile lock" failure reported in
- // Sentry against `openhuman.app_state_snapshot`.
+ // Load the `app-session` auth profile exactly once and derive both the
+ // session-state view and the raw token from it — one auth-profile file
+ // lock per snapshot (the "Timed out waiting for auth profile lock" Sentry
+ // failure on Windows came from taking it twice).
//
- // `load_app_session_profile` calls `acquire_lock()`, which busy-waits
- // with `thread::sleep` for up to ~35s when the lock is contended. Calling
- // it directly on a tokio worker thread blocks that thread for the entire
- // wait, exhausting the thread pool under concurrent snapshot calls and
- // triggering `ERR_CONNECTION_TIMED_OUT` on all RPC connections.
- // Read the sign-out generation BEFORE the profile load, not before the
- // refresh. The token this snapshot is about is the one that load returns, so
- // the generation has to be the one in force when that token was read.
- // Capturing it later leaves a window — sign-out lands between the load and the
- // refresh, the refresh reads the *new* generation, and then publishes an answer
- // fetched with the *old* token, passing its own staleness check.
- // `load_app_session_profile` busy-waits up to ~35s on a contended lock, so that
- // window is not a narrow one.
- let session_mutation_lock = CURRENT_USER_SESSION_MUTATION_LOCK.lock().await;
- let generation = current_user_generation();
+ // `load_app_session_profile` busy-waits with `thread::sleep` for up to
+ // ~35s when the lock is contended, so it runs on the blocking pool rather
+ // than a tokio worker thread.
let config_for_profile = config.clone();
let session_profile =
tokio::task::spawn_blocking(move || load_app_session_profile(&config_for_profile))
.await
.unwrap_or_else(|e| Err(format!("[app_state] auth profile load task panicked: {e}")))?;
- drop(session_mutation_lock);
let mut auth = session_state_from_profile(session_profile.as_ref());
- let mut session_token = session_token_from_profile(session_profile.as_ref());
- let stored_user = sanitize_snapshot_user(auth.user.clone());
- let pending_backend_validation = snapshot_user_pending_backend_validation(stored_user.as_ref());
- let session_metadata = session_profile
- .as_ref()
- .map(|profile| profile.metadata.clone())
- .unwrap_or_default();
- let pending_session_user_id = pending_backend_validation
- .then(|| pending_session_user_id_for_cleanup(stored_user.as_ref(), &session_metadata))
- .flatten();
+ let session_token = session_token_from_profile(session_profile.as_ref());
+ let current_user = sanitize_snapshot_user(auth.user.clone());
+ auth.user = current_user.clone();
let auth_ms = t_auth.elapsed().as_millis();
- // Resolve the live current-user refresh and the runtime snapshot
- // CONCURRENTLY. Both touch the backend and both already fall back to local
- // data (stored_user / degraded runtime), so running them in parallel rather
- // than serially halves the worst-case bootstrap latency when the backend is
- // unreachable. Together with the fast auth-profile lock reclaim this keeps
- // the first `app_state_snapshot` from stranding the UI on "Initializing
- // OpenHuman" (the FE clears `isBootstrapping` on this call). `tokio::join!`
- // polls both on the current task — no extra threads.
- let t_enrich = Instant::now();
- let current_user_future = Box::pin(async {
- let Some(token) = session_token.clone().filter(|t| !t.trim().is_empty()) else {
- return snapshot_current_user_result(stored_user.clone());
- };
- if is_local_session_token(&token) {
- return snapshot_current_user_result(stored_user.clone());
- }
- match tokio::time::timeout(
- auth_fetch_timeout(),
- fetch_current_user_cached(&config, &token, !pending_backend_validation, generation),
- )
- .await
- {
- Ok(Ok(Some(fresh_user))) => {
- if pending_backend_validation && user_id_from_profile_payload(&fresh_user).is_none()
- {
- warn!(
- "{LOG_PREFIX} pending current user refresh returned a user without an id; keeping stored pending session for retry"
- );
- return snapshot_current_user_result(stored_user.clone());
- }
- let fresh_user = clear_pending_backend_validation_flag(fresh_user);
- if pending_backend_validation {
- let snapshot_config = match persist_revalidated_session_user(
- &config,
- &token,
- session_metadata.clone(),
- fresh_user.clone(),
- generation,
- )
- .await
- {
- Ok(snapshot_config) => {
- debug!(
- "{LOG_PREFIX} cleared pending backend validation after successful current user refresh"
- );
- snapshot_config
- }
- Err(error) => {
- warn!(
- "{LOG_PREFIX} failed to persist cleared pending backend validation: {error}"
- );
- return snapshot_current_user_result(stored_user.clone());
- }
- };
- return (
- SnapshotCurrentUser::user(Some(fresh_user)),
- Some(snapshot_config),
- );
- }
- snapshot_current_user_result(Some(fresh_user))
- }
- Ok(Ok(None)) if pending_backend_validation => {
- warn!(
- "{LOG_PREFIX} backend returned empty user for pending session revalidation; clearing stored app session"
- );
- if let Err(error) = clear_deferred_session_after_backend_rejection(
- &config,
- pending_session_user_id.as_deref(),
- )
- .await
- {
- warn!("{LOG_PREFIX} failed to clear rejected pending session: {error}");
- }
- (SnapshotCurrentUser::DeferredSessionRejected, None)
- }
- Ok(Ok(None)) => snapshot_current_user_result(stored_user.clone()),
- Ok(Err(CurrentUserFetchError::Rejected(error))) if pending_backend_validation => {
- warn!(
- "{LOG_PREFIX} pending current user refresh was rejected; clearing stored app session: {error}"
- );
- if let Err(clear_error) = clear_deferred_session_after_backend_rejection(
- &config,
- pending_session_user_id.as_deref(),
- )
- .await
- {
- warn!("{LOG_PREFIX} failed to clear rejected pending session: {clear_error}");
- }
- (SnapshotCurrentUser::DeferredSessionRejected, None)
- }
- Ok(Err(CurrentUserFetchError::FetchFailed(error))) if pending_backend_validation => {
- warn!(
- "{LOG_PREFIX} pending current user refresh failed before a backend response; keeping stored pending session for retry: {error}"
- );
- snapshot_current_user_result(stored_user.clone())
- }
- Ok(Err(CurrentUserFetchError::TransientResponse(error)))
- if pending_backend_validation =>
- {
- warn!(
- "{LOG_PREFIX} pending current user refresh received transient backend response; keeping stored pending session: {error}"
- );
- snapshot_current_user_result(stored_user.clone())
- }
- Ok(Err(CurrentUserFetchError::Suppressed {
- inner,
- consecutive,
- retry_in,
- })) => {
- // Not a failure of *this* poll: the backoff window opened by an
- // earlier failure is still running, so no request was made and
- // this arm cost microseconds. Logging it at `warn` with the
- // same wording as a live failure is what made a working backoff
- // look like a hammering loop in #5930's own log evidence.
- debug!(
- "{LOG_PREFIX} current user refresh suppressed; backend unavailable or unhealthy \
- {consecutive}x, next live attempt in {}s, using stored snapshot fallback: {}",
- retry_in.as_secs(),
- inner.message()
- );
- snapshot_current_user_result(stored_user.clone())
- }
- Ok(Err(error)) => {
- warn!(
- "{LOG_PREFIX} current user refresh failed; using stored snapshot fallback: {}",
- error.message()
- );
- snapshot_current_user_result(stored_user.clone())
- }
- Err(_) if pending_backend_validation => {
- warn!(
- "{LOG_PREFIX} pending current user fetch timed out after {}s; keeping stored pending session for retry",
- auth_fetch_timeout().as_secs()
- );
- note_current_user_timeout(generation, &config, &token);
- snapshot_current_user_result(stored_user.clone())
- }
- Err(_) => {
- warn!(
- "{LOG_PREFIX} current user fetch timed out after {}s; using stored snapshot fallback",
- auth_fetch_timeout().as_secs()
- );
- note_current_user_timeout(generation, &config, &token);
- snapshot_current_user_result(stored_user.clone())
- }
- }
- });
- let runtime_future = Box::pin(async {
- match tokio::time::timeout(
- RUNTIME_SNAPSHOT_TIMEOUT,
- build_runtime_snapshot(&config, req_id),
- )
- .await
- {
- Ok(snapshot) => snapshot,
- Err(_) => {
- warn!(
- "{LOG_PREFIX} build_runtime_snapshot timed out after {}s req_id={}; returning degraded runtime snapshot",
- RUNTIME_SNAPSHOT_TIMEOUT.as_secs(),
- req_id
- );
- degraded_runtime_snapshot(&config)
- }
- }
- });
- let (current_user_result, runtime) = tokio::join!(current_user_future, runtime_future);
- let enrich_ms = t_enrich.elapsed().as_millis();
- let (current_user, revalidated_config) = current_user_result;
- // Read before the `DeferredSessionRejected` arm below clears
- // `session_token`, and keyed off the same `config` the fetch used so the
- // answer describes this identity's backend, not a previous one's.
- //
- // A rejected session is excluded outright: that arm is about to clear
- // `auth`, `session_token` and `current_user`, and a signed-out snapshot
- // carrying `currentUserStale: true` would describe a user it no longer
- // reports.
- //
- // The token is matched to the fetch above, which filters on the *trimmed*
- // token being non-empty but keys the failure record on the raw one.
- // Trimming here instead would look up a key that was never written, and
- // staleness would read as `false` for every token with surrounding
- // whitespace.
- let session_rejected = matches!(current_user, SnapshotCurrentUser::DeferredSessionRejected);
- let (current_user_stale, current_user_stale_seconds) = match session_token
- .as_deref()
- .filter(|_| !session_rejected)
- .filter(|&token| !token.trim().is_empty() && !is_local_session_token(token))
+ let t_runtime = Instant::now();
+ let runtime = match tokio::time::timeout(
+ RUNTIME_SNAPSHOT_TIMEOUT,
+ build_runtime_snapshot(&config, req_id),
+ )
+ .await
{
- Some(token) => current_user_staleness(&current_user_api_base(&config), token),
- // Signed out, rejected, or a local-only token that never talks to the
- // backend: there is nothing for the stored snapshot to be stale
- // relative to.
- None => (false, None),
- };
- let mut snapshot_config = config.clone();
- if let Some(revalidated_config) = revalidated_config {
- snapshot_config = *revalidated_config;
- }
- let current_user = match current_user {
- SnapshotCurrentUser::User(current_user) => {
- if pending_backend_validation {
- if let Some(user_id) = current_user.as_ref().and_then(user_id_from_profile_payload)
- {
- auth.user_id = Some(user_id);
- }
- }
- auth.user = current_user.clone();
- current_user
- }
- SnapshotCurrentUser::DeferredSessionRejected => {
- auth.is_authenticated = false;
- auth.user_id = None;
- auth.user = None;
- auth.profile_id = None;
- session_token = None;
- None
- }
- };
- let runtime = if same_config_state_dir(&config, &snapshot_config) {
- runtime
- } else {
- warn!(
- "{LOG_PREFIX} pending session revalidation changed config scope; rebuilding runtime snapshot with activated user config"
- );
- match tokio::time::timeout(
- RUNTIME_SNAPSHOT_TIMEOUT,
- build_runtime_snapshot(&snapshot_config, req_id),
- )
- .await
- {
- Ok(snapshot) => snapshot,
- Err(_) => {
- warn!(
- "{LOG_PREFIX} activated-config runtime snapshot timed out after {}s req_id={}; returning degraded runtime snapshot",
- RUNTIME_SNAPSHOT_TIMEOUT.as_secs(),
- req_id
- );
- degraded_runtime_snapshot(&snapshot_config)
- }
+ Ok(snapshot) => snapshot,
+ Err(_) => {
+ warn!(
+ "{LOG_PREFIX} build_runtime_snapshot timed out after {}s req_id={}; returning degraded runtime snapshot",
+ RUNTIME_SNAPSHOT_TIMEOUT.as_secs(),
+ req_id
+ );
+ degraded_runtime_snapshot(&config)
}
};
+ let runtime_ms = t_runtime.elapsed().as_millis();
let t_local_state = Instant::now();
- let local_state = load_stored_app_state(&snapshot_config)?;
+ let local_state = load_stored_app_state(&config)?;
crate::security::keyring_consent::policy::initialize(local_state.keyring_consent.clone());
let local_state_ms = t_local_state.elapsed().as_millis();
let total_ms = t_total.elapsed().as_millis();
debug!(
- "{LOG_PREFIX} snapshot timings req_id={} config_ms={} auth_ms={} enrich_ms={} local_state_ms={} total_ms={}",
- req_id, config_ms, auth_ms, enrich_ms, local_state_ms, total_ms
+ "{LOG_PREFIX} snapshot timings req_id={} config_ms={} auth_ms={} runtime_ms={} local_state_ms={} total_ms={}",
+ req_id, config_ms, auth_ms, runtime_ms, local_state_ms, total_ms
);
debug!(
"{LOG_PREFIX} snapshot req_id={} auth={} onboarding={} chat_onboarding={} analytics={} local_ai_state={} service_state={:?}",
req_id,
auth.is_authenticated,
- snapshot_config.onboarding_completed,
- snapshot_config.chat_onboarding_completed,
- snapshot_config.observability.analytics_enabled,
+ config.onboarding_completed,
+ config.chat_onboarding_completed,
+ config.observability.analytics_enabled,
runtime.local_ai.state,
runtime.service.state
);
@@ -388,16 +122,14 @@ pub async fn snapshot() -> Result<RpcOutcome<AppStateSnapshot>, String> {
auth,
session_token,
current_user,
- onboarding_completed: snapshot_config.onboarding_completed,
- chat_onboarding_completed: snapshot_config.chat_onboarding_completed,
- analytics_enabled: snapshot_config.observability.analytics_enabled,
+ onboarding_completed: config.onboarding_completed,
+ chat_onboarding_completed: config.chat_onboarding_completed,
+ analytics_enabled: config.observability.analytics_enabled,
local_state,
keyring_status,
runtime,
health,
config_recovered: crate::desktop::app_state::config_recovered_this_session(),
- current_user_stale,
- current_user_stale_seconds,
},
vec!["core app state snapshot fetched".to_string()],
))