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
refactor(session_store): extract rebound logic into its own module
Steven Enamakel9d0cdcb805f1
diff --git a/crates/openhuman-core/src/agent/session_store/rebound.rs b/crates/openhuman-core/src/agent/session_store/rebound.rsnew file mode 100644index 0000000000..fccc01f48f--- /dev/null+++ b/crates/openhuman-core/src/agent/session_store/rebound.rs@@ -0,0 +1,149 @@+//! A driver-backed session store whose backend can be swapped in place.+//!+//! The default storage backend lives in the workspace+//! (`sqlite:<workspace>/storage`), and the desktop rebinds its workspace when a+//! different user signs in. The session store is installed once, process-wide+//! (a detached turn still writing across a rebind must keep landing in a+//! store, not in a no-provider fallback), so it cannot be replaced by a new+//! provider object; it forwards to a [`DriverSessionStores`] that+//! [`rebind_all`] points at the new workspace's backend.+//!+//! Handles already handed out keep the backend they were opened on, so a turn+//! in flight when the user switches finishes against the old workspace.++use std::sync::{Arc, LazyLock, Mutex, PoisonError, RwLock, Weak};++use tinyagents_session::port::{AgentStores, SessionStoreProvider};+use tinyagents_session::DriverSessionStores;+use tinystoragedrivers::{Blocking, StorageBackend, StorageError};++/// Every live [`ReboundSessionStores`], so a rebind reaches the one a host+/// installed however it was installed (process-wide, per runtime, per task).+static LIVE: LazyLock<Mutex<Vec<Weak<ReboundSessionStores>>>> = LazyLock::new(Mutex::default);++/// [`DriverSessionStores`] over a backend [`rebind_all`] can replace.+pub struct ReboundSessionStores {+ inner: RwLock<Arc<DriverSessionStores>>,+ bridge: Blocking,+ recover_on_open: bool,+}++impl std::fmt::Debug for ReboundSessionStores {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("ReboundSessionStores")+ .field("destination", &self.current().destination_key())+ .finish()+ }+}++impl ReboundSessionStores {+ /// Session stores over `backend`; `recover_on_open` is+ /// [`DriverSessionStores::recover_on_open`].+ ///+ /// # Errors+ ///+ /// When the blocking bridge cannot start.+ pub fn new(+ backend: Arc<dyn StorageBackend>,+ recover_on_open: bool,+ ) -> Result<Arc<Self>, StorageError> {+ let bridge = Blocking::new()?;+ let inner = DriverSessionStores::with_bridge(backend, bridge.clone())+ .recover_on_open(recover_on_open);+ let this = Arc::new(Self {+ inner: RwLock::new(Arc::new(inner)),+ bridge,+ recover_on_open,+ });+ let mut live = LIVE.lock().unwrap_or_else(PoisonError::into_inner);+ live.retain(|weak| weak.strong_count() > 0);+ live.push(Arc::downgrade(&this));+ Ok(this)+ }++ fn current(&self) -> Arc<DriverSessionStores> {+ Arc::clone(&self.inner.read().unwrap_or_else(PoisonError::into_inner))+ }++ /// Points the store at `backend`: agents opened from now on live there.+ pub fn rebind(&self, backend: Arc<dyn StorageBackend>) {+ let next = DriverSessionStores::with_bridge(backend, self.bridge.clone())+ .recover_on_open(self.recover_on_open);+ *self.inner.write().unwrap_or_else(PoisonError::into_inner) = Arc::new(next);+ tracing::info!(+ target: "openhuman::storage",+ destination = ?self.current().destination_key(),+ "[session_store] rebound to a new storage backend"+ );+ }+}++/// Rebinds every live [`ReboundSessionStores`] to `backend`; returns how many.+pub fn rebind_all(backend: &Arc<dyn StorageBackend>) -> usize {+ let live: Vec<Arc<ReboundSessionStores>> = LIVE+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ .iter()+ .filter_map(Weak::upgrade)+ .collect();+ for stores in &live {+ stores.rebind(Arc::clone(backend));+ }+ live.len()+}++impl SessionStoreProvider for ReboundSessionStores {+ fn for_agent(&self, agent_id: &str) -> AgentStores {+ self.current().for_agent(agent_id)+ }++ fn try_for_agent(&self, agent_id: &str) -> Result<AgentStores, StorageError> {+ SessionStoreProvider::try_for_agent(self.current().as_ref(), agent_id)+ }++ fn bind_actor(+ &self,+ backend: Arc<dyn StorageBackend>,+ ) -> Result<Option<Arc<dyn SessionStoreProvider>>, StorageError> {+ self.current().bind_actor(backend)+ }++ fn bind_actor_with_schema_admission(+ &self,+ backend: Arc<dyn StorageBackend>,+ admission: tinyagents_harness::store::SchemaAdmission,+ ) -> Result<Option<Arc<dyn SessionStoreProvider>>, StorageError> {+ self.current()+ .bind_actor_with_schema_admission(backend, admission)+ }++ fn run_ledger_for_agent(+ &self,+ agent_id: &str,+ ) -> tinyagents_session::run_ledger::LedgerResult<+ Option<Arc<dyn tinyagents_session::run_ledger::RunLedgerStore>>,+ > {+ self.current().run_ledger_for_agent(agent_id)+ }++ fn conversations_for_agent(+ &self,+ agent_id: &str,+ ) -> tinyagents_session::threads::ConversationResult<+ Option<Arc<dyn tinyagents_session::threads::ConversationPersistence>>,+ > {+ self.current().conversations_for_agent(agent_id)+ }++ fn recover(&self) -> anyhow::Result<()> {+ self.current().recover()+ }++ fn destination_key(&self) -> Option<String> {+ self.current().destination_key()+ }+}++#[cfg(test)]+#[path = "rebound_tests.rs"]+mod tests;diff --git a/crates/openhuman-core/src/agent/session_store/rebound_tests.rs b/crates/openhuman-core/src/agent/session_store/rebound_tests.rsnew file mode 100644index 0000000000..243227ee8b--- /dev/null+++ b/crates/openhuman-core/src/agent/session_store/rebound_tests.rs@@ -0,0 +1,56 @@+use super::*;+use tinystoragedrivers::{MemoryStorage, Scope};++fn put_marker(stores: &ReboundSessionStores, backend: &Arc<dyn StorageBackend>) {+ // Opening an agent's stores declares its collections in the backend.+ let _ = stores.try_for_agent("probe").unwrap();+ backend.for_scope(&Scope::local()).unwrap();+}++#[test]+fn a_rebind_moves_new_agents_to_the_new_backend() {+ let first: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());+ let second: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());+ let stores = ReboundSessionStores::new(Arc::clone(&first), true).unwrap();+ put_marker(&stores, &first);+ let before = stores.destination_key();++ assert_eq!(rebind_all(&second), 1);++ // The destination names the backend driver and a per-provider id; a+ // rebound store is a different provider underneath.+ assert_ne!(stores.destination_key(), before);+ stores.try_for_agent("probe").unwrap();+}++#[test]+fn handles_opened_before_a_rebind_keep_their_backend() {+ let first: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());+ let second: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());+ let stores = ReboundSessionStores::new(Arc::clone(&first), false).unwrap();+ let old = stores.try_for_agent("agent").unwrap();+ rebind_all(&second);+ let new = stores.try_for_agent("agent").unwrap();+ old.turn_states+ .put(&tinyagents_session::turn_state::TurnState::default())+ .ok();+ assert!(+ !Arc::ptr_eq(&old.turn_states, &new.turn_states),+ "a store opened after the rebind is not the one opened before it"+ );+}++#[test]+fn dropped_stores_are_not_rebound() {+ let backend: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());+ let stores = ReboundSessionStores::new(Arc::clone(&backend), false).unwrap();+ drop(stores);+ let live_before = LIVE+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ .iter()+ .filter(|weak| weak.strong_count() > 0)+ .count();+ let rebound = rebind_all(&backend);+ assert!(rebound <= live_before);+}