The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
merge main into pr/5822, resolving conflicts in ops_part_02.rs, ops_tests.rs, ops_current_user_backoff_tests.rs
Steven Enamakel6c544f07f069
diff --cc src/openhuman/desktop/app_state/ops_current_user_backoff_tests.rsindex d439628b31,82e923b322..9e57c5bb07--- a/src/openhuman/desktop/app_state/ops_current_user_backoff_tests.rs+++ b/src/openhuman/desktop/app_state/ops_current_user_backoff_tests.rs@@@ -26,6 -25,9 +25,10 @@@ use once_cell::sync::Lazy as TestLazy/// whole point of that test is that `fetch_current_user_cached` consults the/// record. Kept distinct from `APP_STATE_CACHE_TEST_LOCK` because the two guard/// different globals and nothing here writes the positive cache.++///+ /// `pub(super)` because `ops_tests.rs` needs it too: a successful fetch calls+ /// `clear_current_user_failure`, which wipes this global, so a test that lets a+ /// real fetch complete has to serialise against the tests seeding outages here.pub(super) static CURRENT_USER_FAILURE_TEST_LOCK: TestLazy<tokio::sync::Mutex<()>> =TestLazy::new(|| tokio::sync::Mutex::new(()));diff --cc src/openhuman/desktop/app_state/ops_part_02.rsindex 02c934c1f7,e6ba68c237..e082f818cd--- a/src/openhuman/desktop/app_state/ops_part_02.rs+++ b/src/openhuman/desktop/app_state/ops_part_02.rs@@@ -183,20 -184,39 +183,39 @@@ async fn fetch_current_user_cachedlet api_base = current_user_api_base(config);if allow_cache {- {- let cache = CURRENT_USER_CACHE.lock();- if let Some(entry) = cache.as_ref() {- if entry.api_base == api_base- && entry.token == token- && entry.fetched_at.elapsed() < CURRENT_USER_REFRESH_TTL- {- debug!(- "{LOG_PREFIX} using cached current user age_ms={}",- entry.fetched_at.elapsed().as_millis()- );- return Ok(Some(entry.user.clone()));- }+ if let Some((user, age)) = cached_current_user(&api_base, token) {+ if age < CURRENT_USER_REFRESH_TTL {+ debug!(+ "{LOG_PREFIX} using cached current user age_ms={}",+ age.as_millis()+ );+ return Ok(Some(user));}+ // Stale-while-revalidate. The entry has expired, but we already+ // know who this is — serve that and re-confirm it behind the poll+ // instead of making the shell wait on a WAN round trip to be told+ // the same thing. Blocking here is what put a floor of one round+ // trip under every `app_state_snapshot` (#6180: 732 calls in 3.5h,+ // not one of them under 500ms).+ //+ // The refresh cadence is unchanged by this: `refresh_current_user_now`+ // stamps `fetched_at` when the request goes out, not when it lands,+ // so the round trip is not folded into the next TTL window and the+ // poll after this one expires on the same schedule it always did.+ // Stamping at completion would have quietly halved the cadence —+ // see the note there (#6190 review). The snapshot keeps reporting+ // the true age of the data it is serving in+ // `current_user_stale_seconds`.+ //+ // Only the expired-entry path revalidates in the background. With+ // no entry at all the shell has no identity to render, so that+ // first fetch after login still blocks, below.- spawn_current_user_refresh(config, token);++ spawn_current_user_refresh(config, token, generation);+ debug!(+ "{LOG_PREFIX} serving expired current user age_ms={} while refreshing behind the poll",+ age.as_millis()+ );+ return Ok(Some(user));}// Nothing fresh to serve, so this poll would normally go to the network@@@ -227,12 -247,133 +246,136 @@@}}- // `generation` belongs to the caller and was read when the TOKEN was read.- // Everything after this point is publishing an answer about an identity that- // sign-out may have dropped in the meantime; if it has, the answer is stale by- // definition and goes nowhere near the caches. The caller still gets it — it- // asked before the sign-out, and suppressing the reply is a different change- // from suppressing the cache.- refresh_current_user_now(config, token, RefreshOrigin::Blocking).await++refresh_current_user_now(config, token, generation, RefreshOrigin::Blocking).await+ }++ /// The cached user for this identity and how old it is, if the cache holds one.+ ///+ /// Reads under one lock and hands back an owned copy, so no caller holds+ /// `CURRENT_USER_CACHE` across a decision — the freshness test and the+ /// stale-while-revalidate branch below both need the same read.+ fn cached_current_user(api_base: &str, token: &str) -> Option<(Value, Duration)> {+ let cache = CURRENT_USER_CACHE.lock();+ let entry = cache.as_ref()?;+ (entry.api_base == api_base && entry.token == token)+ .then(|| (entry.user.clone(), entry.fetched_at.elapsed()))+ }++ /// Single-flight gate for the background refresh.+ ///+ /// An async mutex held by the spawned task for its lifetime, taken with+ /// `try_lock` so a poll that finds a refresh already running simply declines to+ /// start a second one rather than queueing behind it. Same gate, same reason as+ /// [`RUNTIME_SNAPSHOT_REBUILD`]: without it every overlapping poll launches its+ /// own fetch. Using a guard rather than a flag means a panicking refresh+ /// releases the gate instead of wedging it shut for the life of the process.+ static CURRENT_USER_REFRESH_INFLIGHT: Lazy<tokio::sync::Mutex<()>> =+ Lazy::new(|| tokio::sync::Mutex::new(()));++ /// Refresh the cached current user without making the caller wait for it.+ ///+ /// Declines to start when the backoff window from an earlier failure is still+ /// open — a background refresh would re-pay exactly the timeout that window+ /// exists to avoid (#5624) — and when a previous poll's refresh is still in+ /// flight.-fn spawn_current_user_refresh(config: &Config, token: &str) {++fn spawn_current_user_refresh(config: &Config, token: &str, generation: u64) {+ if let Some((error, consecutive, remaining)) =+ suppressed_current_user_failure(¤t_user_api_base(config), token)+ {+ // Logged here because the stale-while-revalidate return above means an+ // outage no longer reaches the replay branch in+ // `fetch_current_user_cached` — without this line a backend that has+ // been down for minutes leaves no trace at all in the snapshot logs.+ // `debug!`, not `warn!`: no request was made and nothing waited on it+ // (#5930).+ debug!(+ "{LOG_PREFIX} not refreshing current user behind the poll; backend failed \+ {consecutive}x, retrying in {}ms: {}",+ remaining.as_millis(),+ error.message()+ );+ return;+ }+ let gate: &'static tokio::sync::Mutex<()> = &CURRENT_USER_REFRESH_INFLIGHT;+ let Ok(guard) = gate.try_lock() else {+ return;+ };++ let config = config.clone();+ let token = token.to_string();+ tokio::spawn(async move {+ let _guard = guard;+ // Bounded by the same budget the blocking path spends, so a hung+ // backend cannot leave the gate closed for longer than one window.+ match tokio::time::timeout(+ auth_fetch_timeout(),- refresh_current_user_now(&config, &token, RefreshOrigin::Background),++ refresh_current_user_now(&config, &token, generation, RefreshOrigin::Background),+ )+ .await+ {+ Ok(Ok(_)) => {}+ // The stale entry stands and the failure is recorded, so the next+ // poll takes the backoff path. Not a failure of any user-visible+ // operation — nothing was waiting on this.+ Ok(Err(error)) => debug!(+ "{LOG_PREFIX} background current user refresh failed; serving stale entry: {}",+ error.message()+ ),+ Err(_) => {+ debug!(+ "{LOG_PREFIX} background current user refresh timed out after {}s; serving stale entry",+ auth_fetch_timeout().as_secs()+ );- note_current_user_timeout(&config, &token);++ // The generation is captured from when the refresh was launched and++ // guards the timeout record against sign-out racing the timeout.++ note_current_user_timeout(generation, &config, &token);+ }+ }+ });+ }++ /// Where a refresh was started from, which decides whether its answer may still+ /// be committed by the time it lands.+ #[derive(Clone, Copy, PartialEq, Eq, Debug)]+ enum RefreshOrigin {+ /// The caller is still awaiting this refresh and still holds the identity+ /// it asked for, so the answer is authoritative by construction.+ Blocking,+ /// Detached from the poll that started it, and therefore able to outlive+ /// the identity it was started for — a logout and re-login, or an+ /// environment switch, can land in between.+ Background,+ }++ /// Go to the backend, then reconcile the caches and freshness stamps with what+ /// came back. The blocking half of [`fetch_current_user_cached`], split out so+ /// the background refresh runs exactly the same path rather than a parallel+ /// copy of it that could drift.+ async fn refresh_current_user_now(+ config: &Config,+ token: &str,++ generation: u64,+ origin: RefreshOrigin,+ ) -> Result<Option<Value>, CurrentUserFetchError> {+ let api_base = current_user_api_base(config);+ // The TTL clock starts when the request goes out, not when it lands.+ //+ // `fetched_at` is what `CURRENT_USER_REFRESH_TTL` is measured against, and+ // the poll loop schedules itself from the previous *response*. Stamping at+ // completion would therefore fold the round trip into the next window: a+ // refresh taking `L` leaves the following poll only `TTL - L` from expiry,+ // it reads the entry as fresh, and the refresh after that is skipped+ // entirely — halving the cadence as a side effect of not blocking (#6190+ // review). Stamping at initiation keeps the wall-clock refresh cadence+ // exactly what it was before this path became non-blocking; the only thing+ // that changed is who waits for it.+ //+ // It also errs the safe way. The data itself arrives at `started_at + L`,+ // so calling it `started_at` slightly *overstates* its age and expires the+ // entry sooner — never later. `note_current_user_success` below is the+ // stamp that answers "how old is the data we are showing", and it stays at+ // completion, because that is when the data actually arrived.+ let started_at = Instant::now();let fetched = match fetch_current_user(config, token).await {Ok(user) => sanitize_snapshot_user(user),Err(error) => {@@@ -247,8 -381,73 +390,92 @@@return Err(error);}};- if !publish_current_user_unless_stale(generation, &api_base, token, fetched.clone()) {- debug!("{LOG_PREFIX} discarding current user refresh that raced sign-out");-- // A detached refresh can land after the app has moved to another identity,++// A detached refresh can land after the app has moved to another identity,+ // and every write below is process-global. Committing then would regress+ // the cache to the previous user — and `peek_cached_current_user_identity`+ // reads that slot WITHOUT a key check (#926), so the regressed entry would+ // be embedded in the agent's prompts as the current user. The keyed reads+ // in `fetch_current_user_cached` would merely miss; that one would be+ // wrong.+ //+ // The check and the commit share ONE lock acquisition, and nothing between+ // them can suspend or release it. Validating through a separate read would+ // leave a window in which a blocking refresh for a newer identity commits+ // after this one has already decided it is current — narrow, but the+ // runtime is multi-threaded, so "narrow" is not "impossible".+ //++ // The generation check (against sign-out) and the identity check (against++ // a subsequent login the background refresh missed) are applied under the++ // same lock. Sign-out bumps the generation before clearing the caches, so++ // a refresh that read the old token before sign-out will read the old++ // generation and fail the check — the cache it would restore was already++ // cleared.++ //+ // The failure and freshness stamps stay OUTSIDE this scope: they take their+ // own locks, and this module's rule is that `LAST_CURRENT_USER_SUCCESS` is+ // never nested inside `CURRENT_USER_CACHE`. They therefore run *after* the+ // commit rather than before it, which is also what lets a discarded refresh+ // leave the newer identity's `CURRENT_USER_FAILURE` record alone —+ // `clear_current_user_failure` is unkeyed and would otherwise wipe it. The+ // stamp is only ever read as an age in seconds, so moving it to the far+ // side of the commit cannot change an observable answer.++ //++ // When sign-out wins the generation race, the failure and success records++ // are untouched — they were already cleared by `forget_current_user_caches`,++ // and this refresh has no business touching them either.+ let committed = {+ let mut cache = CURRENT_USER_CACHE.lock();- let moved_on = cache- .as_ref()- .is_some_and(|entry| entry.api_base != api_base || entry.token != token);- if origin == RefreshOrigin::Background && moved_on {++ let generation_mismatch = current_user_generation() != generation;++ if generation_mismatch {++ debug!(++ "{LOG_PREFIX} discarding current user refresh that raced sign-out; \++ generation bumped while in flight"++ );+ false+ } else {- match fetched.clone() {- Some(user) => {- debug!("{LOG_PREFIX} refreshed current user from backend");- *cache = Some(CachedCurrentUser {- api_base: api_base.clone(),- token: token.to_string(),- fetched_at: started_at,- user,- });- }- None => {- debug!("{LOG_PREFIX} backend returned empty current user; clearing cache");- *cache = None;++ let moved_on = cache++ .as_ref()++ .is_some_and(|entry| entry.api_base != api_base || entry.token != token);++ if origin == RefreshOrigin::Background && moved_on {++ false++ } else {++ match fetched.clone() {++ Some(user) => {++ debug!("{LOG_PREFIX} refreshed current user from backend");++ *cache = Some(CachedCurrentUser {++ api_base: api_base.clone(),++ token: token.to_string(),++ fetched_at: started_at,++ user,++ });++ }++ None => {++ debug!("{LOG_PREFIX} backend returned empty current user; clearing cache");++ *cache = None;++ }+ }++ true+ }- true+ }+ };++ if !committed {+ debug!(+ "{LOG_PREFIX} discarding background current user refresh; the cache moved to \+ another identity while it was in flight"+ );+ return Ok(fetched);+ }++ clear_current_user_failure();+ // Only a *refreshed user* makes the displayed data fresh. The backend can+ // answer 200 with no user at all, and the snapshot caller then falls back+ // to `stored_user` — so stamping success here would report an age of ~0s+ // for data that was never replaced. `clear_current_user_failure` still runs+ // either way: an empty answer is the backend being healthy, just not+ // useful, and it should not keep the backoff window open.+ if fetched.is_some() {+ note_current_user_success(&api_base, token);}Ok(fetched)diff --cc src/openhuman/desktop/app_state/ops_tests.rsindex f2aa1670b1,bf5c0ba442..c98d167990--- a/src/openhuman/desktop/app_state/ops_tests.rs+++ b/src/openhuman/desktop/app_state/ops_tests.rs@@@ -487,5 -495,116 +495,119 @@@ async fn current_user_fetch_carries_thecrate::api::product::reset_product_identity_for_test();}+#[path = "ops_signout_cache_tests.rs"]+mod signout_cache_tests;+++ // Serialises the `OPENHUMAN_WORKSPACE` env mutations below so two of these tests+ // can't race each other on the process-global var.+ static WORKSPACE_ENV_TEST_LOCK: TestLazy<TestMutex<()>> = TestLazy::new(|| TestMutex::new(()));++ /// RAII guard for `OPENHUMAN_WORKSPACE`. Captures the prior value on+ /// construction and restores it (set or remove) on drop, so a test that panics+ /// between the mutation and the end of the test can't leak the override into a+ /// sibling test. Must be constructed while holding `WORKSPACE_ENV_TEST_LOCK`:+ /// mutating a process env var while another thread reads it is unsafe, and the+ /// lock serialises every test in this group.+ struct WorkspaceEnvGuard {+ prior: Option<std::ffi::OsString>,+ }++ impl WorkspaceEnvGuard {+ fn set(value: &std::path::Path) -> Self {+ let prior = std::env::var_os("OPENHUMAN_WORKSPACE");+ std::env::set_var("OPENHUMAN_WORKSPACE", value);+ Self { prior }+ }++ fn set_empty() -> Self {+ let prior = std::env::var_os("OPENHUMAN_WORKSPACE");+ std::env::set_var("OPENHUMAN_WORKSPACE", "");+ Self { prior }+ }++ fn unset() -> Self {+ let prior = std::env::var_os("OPENHUMAN_WORKSPACE");+ std::env::remove_var("OPENHUMAN_WORKSPACE");+ Self { prior }+ }+ }++ impl Drop for WorkspaceEnvGuard {+ fn drop(&mut self) {+ match &self.prior {+ Some(value) => std::env::set_var("OPENHUMAN_WORKSPACE", value),+ None => std::env::remove_var("OPENHUMAN_WORKSPACE"),+ }+ }+ }++ /// The #6079 twin: `config_dir_for_workspace_env` must resolve the modern+ /// `<root>/.openhuman/workspace` layout to its parent `<root>/.openhuman` (the+ /// real config dir), NOT the doubled `<root>/.openhuman/.openhuman`. The private+ /// reimplementation this replaced produced the doubled path, so+ /// `config_is_workspace_env_scoped` disagreed with the loader and mis-scoped+ /// credentials on session revalidation. Delegating to the shared+ /// `resolve_config_dir_for_workspace` keeps the two in lockstep.+ ///+ /// The workspace root is named `default_root_dir_name()` (`.openhuman` /+ /// `.openhuman-staging`) so the modern-layout arm — which keys on that name —+ /// fires regardless of the ambient `OPENHUMAN_APP_ENV`, and a temp dir isolates+ /// it from any real `.openhuman` on the host.+ #[test]+ fn config_dir_for_workspace_env_modern_layout_does_not_double_openhuman() {+ let _g = WORKSPACE_ENV_TEST_LOCK.lock();+ let tmp = tempdir().unwrap();+ let root = tmp+ .path()+ .join(crate::openhuman::config::default_root_dir_name());+ let workspace = root.join("workspace");+ let _env = WorkspaceEnvGuard::set(&workspace);++ let resolved = config_dir_for_workspace_env();++ assert_eq!(resolved, Some(root.clone()));+ assert_ne!(+ resolved,+ Some(root.join(crate::openhuman::config::default_root_dir_name())),+ "must never return the doubled .openhuman/.openhuman path"+ );+ }++ /// A fresh legacy layout (`<proj>/workspace` with no sibling `.openhuman` on+ /// disk) must resolve to the sibling `<proj>/.openhuman`, matching the loader —+ /// not nest the workspace inside itself. Guards the same seam as the+ /// `dirs.rs` fresh-legacy test, one level up through the app_state resolver.+ #[test]+ fn config_dir_for_workspace_env_fresh_legacy_resolves_to_sibling() {+ let _g = WORKSPACE_ENV_TEST_LOCK.lock();+ let tmp = tempdir().unwrap();+ let project = tmp.path().join("some-project");+ let workspace = project.join("workspace");+ let _env = WorkspaceEnvGuard::set(&workspace);++ let resolved = config_dir_for_workspace_env();++ assert_eq!(+ resolved,+ Some(project.join(".openhuman")),+ "a fresh legacy workspace must resolve to its sibling .openhuman"+ );+ }++ /// An unset / empty `OPENHUMAN_WORKSPACE` yields `None` so the caller falls back+ /// to user-scoped resolution rather than treating the empty string as a path.+ #[test]+ fn config_dir_for_workspace_env_none_when_unset_or_empty() {+ let _g = WORKSPACE_ENV_TEST_LOCK.lock();+ {+ let _env = WorkspaceEnvGuard::unset();+ assert_eq!(config_dir_for_workspace_env(), None);+ }++ let _env = WorkspaceEnvGuard::set_empty();+ assert_eq!(+ config_dir_for_workspace_env(),+ None,+ "an empty override must not be treated as a path"+ );+ }