Skip to content

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

feat(session_store): scope rebinds to a store registry

Steven Enamakela55a7a8a2e14
diff --git a/crates/openhuman-core/src/agent/session_store/mod.rs b/crates/openhuman-core/src/agent/session_store/mod.rs
index 4e210a406d..09f1fc184c 100644
--- a/crates/openhuman-core/src/agent/session_store/mod.rs
+++ b/crates/openhuman-core/src/agent/session_store/mod.rs
@@ -23,7 +23,7 @@ pub mod ledger_sync;
mod rebound;
mod workspace;
-pub use rebound::{rebind_all, ReboundSessionStores};
+pub use rebound::{rebind_all, ReboundSessionStores, StoreRegistry};
pub use workspace::{
artifact_blobs_for, attachment_stash_for, current_documents_for, default_turn_states_for,
ledger_for, provider_for_workspace, release_workspace, release_workspace_defaults, require_ledger,
diff --git a/crates/openhuman-core/src/agent/session_store/rebound.rs b/crates/openhuman-core/src/agent/session_store/rebound.rs
index fccc01f48f..30b77019bf 100644
--- a/crates/openhuman-core/src/agent/session_store/rebound.rs
+++ b/crates/openhuman-core/src/agent/session_store/rebound.rs
@@ -17,9 +17,51 @@ 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);
+/// The live [`ReboundSessionStores`] of one owner, so a rebind reaches the one
+/// a host installed however it was installed (process-wide, per runtime, per
+/// task).
+///
+/// The process has one ([`rebind_all`] moves every store in it). A registry of
+/// its own scopes a rebind to the stores created in it, which is how a test
+/// exercises rebinding without moving the stores of tests running beside it.
+#[derive(Default)]
+pub struct StoreRegistry {
+ live: Mutex<Vec<Weak<ReboundSessionStores>>>,
+}
+
+impl StoreRegistry {
+ fn register(&self, stores: &Arc<ReboundSessionStores>) {
+ let mut live = self.live.lock().unwrap_or_else(PoisonError::into_inner);
+ live.retain(|weak| weak.strong_count() > 0);
+ live.push(Arc::downgrade(stores));
+ }
+
+ fn live(&self) -> Vec<Arc<ReboundSessionStores>> {
+ self.live
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner)
+ .iter()
+ .filter_map(Weak::upgrade)
+ .collect()
+ }
+
+ /// Rebinds every live store of this registry to `backend`; returns how many.
+ pub fn rebind(&self, backend: &Arc<dyn StorageBackend>) -> usize {
+ let live = self.live();
+ for stores in &live {
+ stores.rebind(Arc::clone(backend));
+ }
+ tracing::debug!(
+ target: "openhuman::storage",
+ rebound = live.len(),
+ "[session_store] registry rebind finished"
+ );
+ live.len()
+ }
+}
+
+/// The process's registry, the one [`ReboundSessionStores::new`] joins.
+static PROCESS: LazyLock<StoreRegistry> = LazyLock::new(StoreRegistry::default);
/// [`DriverSessionStores`] over a backend [`rebind_all`] can replace.
pub struct ReboundSessionStores {
@@ -46,6 +88,20 @@ impl ReboundSessionStores {
pub fn new(
backend: Arc<dyn StorageBackend>,
recover_on_open: bool,
+ ) -> Result<Arc<Self>, StorageError> {
+ Self::new_in(&PROCESS, backend, recover_on_open)
+ }
+
+ /// [`Self::new`], joining `registry` instead of the process's, so only a
+ /// rebind of that registry reaches it.
+ ///
+ /// # Errors
+ ///
+ /// When the blocking bridge cannot start.
+ pub fn new_in(
+ registry: &StoreRegistry,
+ backend: Arc<dyn StorageBackend>,
+ recover_on_open: bool,
) -> Result<Arc<Self>, StorageError> {
let bridge = Blocking::new()?;
let inner = DriverSessionStores::with_bridge(backend, bridge.clone())
@@ -55,9 +111,7 @@ impl ReboundSessionStores {
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));
+ registry.register(&this);
Ok(this)
}
@@ -78,18 +132,10 @@ impl ReboundSessionStores {
}
}
-/// Rebinds every live [`ReboundSessionStores`] to `backend`; returns how many.
+/// Rebinds every live [`ReboundSessionStores`] of the process 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()
+ PROCESS.rebind(backend)
}
impl SessionStoreProvider for ReboundSessionStores {
diff --git a/crates/openhuman-core/src/agent/session_store/rebound_tests.rs b/crates/openhuman-core/src/agent/session_store/rebound_tests.rs
index e43cfc44ed..cdde20b4a3 100644
--- a/crates/openhuman-core/src/agent/session_store/rebound_tests.rs
+++ b/crates/openhuman-core/src/agent/session_store/rebound_tests.rs
@@ -11,12 +11,13 @@ fn put_marker(stores: &ReboundSessionStores, backend: &Arc<dyn StorageBackend>)
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();
+ let registry = StoreRegistry::default();
+ let stores = ReboundSessionStores::new_in(&registry, Arc::clone(&first), true).unwrap();
put_marker(&stores, &first);
let before = stores.destination_key();
- // Other tests' live stores in this process are rebound too.
- assert!(rebind_all(&second) >= 1);
+ // Only the stores of this registry move; tests beside this one keep theirs.
+ assert_eq!(registry.rebind(&second), 1);
// The destination names the backend driver and a per-provider id; a
// rebound store is a different provider underneath.
@@ -28,9 +29,10 @@ fn a_rebind_moves_new_agents_to_the_new_backend() {
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 registry = StoreRegistry::default();
+ let stores = ReboundSessionStores::new_in(&registry, Arc::clone(&first), false).unwrap();
let old = stores.try_for_agent("agent").unwrap();
- rebind_all(&second);
+ registry.rebind(&second);
let new = stores.try_for_agent("agent").unwrap();
old.turn_states
.put(&tinyagents_session::turn_state::TurnState::started(
@@ -49,16 +51,28 @@ fn handles_opened_before_a_rebind_keep_their_backend() {
#[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);
+ let registry = StoreRegistry::default();
+ let kept = ReboundSessionStores::new_in(&registry, Arc::clone(&backend), false).unwrap();
+ let dropped = ReboundSessionStores::new_in(&registry, Arc::clone(&backend), false).unwrap();
+ drop(dropped);
+ assert_eq!(registry.rebind(&backend), 1, "only the live store moves");
+ drop(kept);
+ assert_eq!(registry.rebind(&backend), 0);
+}
+
+#[test]
+fn a_registry_rebind_leaves_other_registries_alone() {
+ let first: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());
+ let second: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());
+ let (mine, theirs) = (StoreRegistry::default(), StoreRegistry::default());
+ let moved = ReboundSessionStores::new_in(&mine, Arc::clone(&first), false).unwrap();
+ let untouched = ReboundSessionStores::new_in(&theirs, Arc::clone(&first), false).unwrap();
+ let before = untouched.destination_key();
+
+ assert_eq!(mine.rebind(&second), 1);
+
+ assert_eq!(untouched.destination_key(), before);
+ drop(moved);
}
#[test]
@@ -66,7 +80,8 @@ fn the_provider_surface_forwards_to_the_current_backend() {
use tinyagents_harness::store::SchemaAdmission;
let backend: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());
- let stores = ReboundSessionStores::new(Arc::clone(&backend), false).unwrap();
+ let registry = StoreRegistry::default();
+ let stores = ReboundSessionStores::new_in(&registry, Arc::clone(&backend), false).unwrap();
// Debug names the live destination rather than the wrapper's internals.
let rendered = format!("{stores:?}");
diff --git a/crates/openhuman-core/src/agent/session_store/workspace_tests.rs b/crates/openhuman-core/src/agent/session_store/workspace_tests.rs
index 0712a023fc..c5d47cf92e 100644
--- a/crates/openhuman-core/src/agent/session_store/workspace_tests.rs
+++ b/crates/openhuman-core/src/agent/session_store/workspace_tests.rs
@@ -124,6 +124,22 @@ fn releasing_defaults_lets_the_next_call_open_afresh() {
assert!(!Arc::ptr_eq(&before, &after), "release drops the cached provider");
}
+#[test]
+fn releasing_one_workspace_keeps_the_others_open() {
+ let (mine, other) = (scratch(), scratch());
+ let kept = fallback_provider(other.path());
+ let released = fallback_provider(mine.path());
+ release_workspace(mine.path());
+ assert!(
+ Arc::ptr_eq(&kept, &fallback_provider(other.path())),
+ "another workspace keeps its provider"
+ );
+ assert!(
+ !Arc::ptr_eq(&released, &fallback_provider(mine.path())),
+ "the released workspace opens afresh"
+ );
+}
+
#[test]
fn the_workspace_session_stores_are_a_workspace_default_with_nothing_to_recover() {
let stores = WorkspaceSessionStores;