Skip to content

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

refactor(sessions): consolidate session storage and drop rebound module

Steven Enamakel5ff6b589d3ae
diff --git a/crates/openhuman-core/src/agent/mod.rs b/crates/openhuman-core/src/agent/mod.rs
index a9ce1b1ece..b9869396e3 100644
--- a/crates/openhuman-core/src/agent/mod.rs
+++ b/crates/openhuman-core/src/agent/mod.rs
@@ -64,6 +64,5 @@ pub mod tools;
pub mod turn_env;
#[allow(unused_imports)]
pub use session_host::{
- HostOnlyToolPolicy, HostTools, HostTurnTools, OpenHumanSessionHost, SessionHostBuilder,
- TurnContext, TurnOverrides,
+ HostTools, HostTurnTools, OpenHumanSessionHost, SessionHostBuilder, TurnContext, TurnOverrides,
};
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/dispatch_outcomes.rs b/crates/openhuman-core/src/agent/orchestration/tools/dispatch_outcomes.rs
index 5dea7efdb0..393aa28a2d 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/dispatch_outcomes.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/dispatch_outcomes.rs
@@ -54,7 +54,7 @@ pub(crate) fn incomplete_stop(
failure_cause: Option<tinyagents_harness::no_progress::ClassifiedStopCause>,
) -> Option<tinyagents_harness::turn_stop::TurnStop> {
Some(
- crate::agent::session_host::stop_summary::stop_from_incomplete_with_cause(
+ crate::agent::session_host::stop_policy::stop_from_incomplete_with_cause(
reason,
failure_cause,
),
diff --git a/crates/openhuman-core/src/agent/session_host/builder/builder_tests.rs b/crates/openhuman-core/src/agent/session_host/builder/builder_tests.rs
index 06c08b0b24..b9d5ac2878 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/builder_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/builder_tests.rs
@@ -1,8 +1,8 @@
//! Tests for the builder module — dedup_visible_tool_specs and related logic.
use super::visible_tool_specs_for_policy;
-use tinyagents_runtime::dedup_visible_tool_specs;
use serde_json::json;
+use tinyagents_runtime::dedup_visible_tool_specs;
use tinytools::ToolSpec;
fn spec(name: &str) -> ToolSpec {
diff --git a/crates/openhuman-core/src/agent/session_host/builder/builder_tests_host_only_tests.rs b/crates/openhuman-core/src/agent/session_host/builder/builder_tests_host_only_tests.rs
index 6ab180773a..a27d812af4 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/builder_tests_host_only_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/builder_tests_host_only_tests.rs
@@ -142,10 +142,7 @@ fn a_host_only_session_runs_a_definition_that_cannot_delegate_or_act() {
#[tokio::test]
async fn the_host_only_policy_admits_only_host_names() {
- let policy = tinyagents_runtime::HostOnlyToolPolicy::new(
- ["read_file".to_string()].into_iter().collect(),
- None,
- );
+ let policy = tinyagents_runtime::HostOnlyToolPolicy::new(["read_file".to_string()], None);
assert_eq!(
{
@@ -181,7 +178,7 @@ async fn the_host_only_policy_still_consults_the_hosts_own_gate() {
}
}
let policy = tinyagents_runtime::HostOnlyToolPolicy::new(
- ["read_file".to_string()].into_iter().collect(),
+ ["read_file".to_string()],
Some(Arc::new(DenyAll)),
);
diff --git a/crates/openhuman-core/src/agent/session_host/builder/host_policy.rs b/crates/openhuman-core/src/agent/session_host/builder/host_policy.rs
index 4fdaecb586..4b969bdad1 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/host_policy.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/host_policy.rs
@@ -21,8 +21,8 @@
use std::sync::Arc;
-use crate::agent_registry::host_definition::AgentDefinition;
use crate::agent::OpenHumanSessionHost;
+use crate::agent_registry::host_definition::AgentDefinition;
use crate::config::Config;
use anyhow::Result;
use tinyagents_definition::{SandboxMode, ToolScope};
@@ -30,7 +30,9 @@ use tinyagents_definition::{SandboxMode, ToolScope};
/// The visible set of a host-only belt before the host's names are merged in:
/// explicitly zero tools (an empty set would mean "no filter").
pub(super) fn empty_belt() -> std::collections::HashSet<String> {
- std::collections::HashSet::from([crate::agent_registry::host_definition::NO_TOOLS_SENTINEL.to_string()])
+ std::collections::HashSet::from([
+ crate::agent_registry::host_definition::NO_TOOLS_SENTINEL.to_string()
+ ])
}
/// The session gate: on a host-only belt, TinyAgents' `HostOnlyToolPolicy` over the
@@ -57,7 +59,8 @@ pub(super) fn session_policy(
if !host_only {
return host;
}
- let allowed = tools.iter().map(|tool| tool.name().to_string()).collect();
+ let allowed: std::collections::HashSet<String> =
+ tools.iter().map(|tool| tool.name().to_string()).collect();
Some(Arc::new(tinyagents_runtime::HostOnlyToolPolicy::new(
allowed, host,
)))
diff --git a/crates/openhuman-core/src/agent/session_host/resume_bound_session_tests.rs b/crates/openhuman-core/src/agent/session_host/resume_bound_session_tests.rs
index b17deac9f0..a19e429cc1 100644
--- a/crates/openhuman-core/src/agent/session_host/resume_bound_session_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/resume_bound_session_tests.rs
@@ -30,10 +30,7 @@ impl OpenHumanSessionHost {
request_id: None,
thread_id: self.thread_id.clone(),
stream: false,
- session: self
- .session_history_locator
- .as_ref()
- .map(|_| session),
+ session: self.session_history_locator.as_ref().map(|_| session),
resume: ResumeMode::Session,
cancellation,
run_context: context.into_tinyagents(root_config),
diff --git a/crates/openhuman-core/src/agent/session_host/runtime/accessors/tool_visibility.rs b/crates/openhuman-core/src/agent/session_host/runtime/accessors/tool_visibility.rs
index 56bdd1d111..40aecd2a60 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime/accessors/tool_visibility.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime/accessors/tool_visibility.rs
@@ -132,8 +132,7 @@ impl OpenHumanSessionHost {
&self.visible_tool_names,
&self.tool_policy_session,
);
- self.visible_tool_specs = Arc::new(tinyagents_runtime::dedup_visible_tool_specs(
- visible_specs,
- ));
+ self.visible_tool_specs =
+ Arc::new(tinyagents_runtime::dedup_visible_tool_specs(visible_specs));
}
}
diff --git a/crates/openhuman-core/src/agent/subagent_host/ops/graph/dispatch.rs b/crates/openhuman-core/src/agent/subagent_host/ops/graph/dispatch.rs
index 99b86958cc..7d4a6c0d2b 100644
--- a/crates/openhuman-core/src/agent/subagent_host/ops/graph/dispatch.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/ops/graph/dispatch.rs
@@ -542,7 +542,7 @@ pub(in super::super) async fn run_subagent_via_graph(
// host boundary, then acknowledge it on the parent stream so the linked
// sub-agent span cannot close ahead of its child record.
if let Some(terminal) = outcome.deferred_terminal.take() {
- let stop = crate::agent::session_host::stop_summary::classify_stop_with_cause(
+ let stop = crate::agent::session_host::stop_policy::classify_stop_with_cause(
outcome.breaker_halt.as_deref(),
outcome.failure_cause.clone(),
false,
diff --git a/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs b/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
index 7b0cfef344..b544380d87 100644
--- a/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
@@ -11,10 +11,9 @@ use std::time::Instant;
use crate::agent::prompts::render_subagent_system_prompt_with_format;
use crate::agent::prompts::{PromptContext, SubagentRenderOptions};
-use crate::agent::session_host::dedup_visible_tool_specs;
use crate::agent::subagent_host::definition_iter_cap;
use crate::agent::subagent_host::tool_prep::{
- filter_definition_tool_indices, subagent_prompt_protocol,
+ dedup_owned_tool_specs, filter_definition_tool_indices, subagent_prompt_protocol,
};
use crate::agent::subagent_host::types::{
SubagentMode, SubagentRunError, SubagentRunOptions, SubagentRunOutcome,
@@ -523,7 +522,7 @@ async fn run_typed_mode(
.iter()
.map(|&i| parent.all_tools[i].name().to_string())
.collect();
- let filtered_specs = dedup_visible_tool_specs(filtered_specs);
+ let filtered_specs = dedup_owned_tool_specs(filtered_specs);
tracing::debug!(
agent_id = %definition.id,
diff --git a/crates/openhuman-core/src/agent/subagent_host/tool_prep.rs b/crates/openhuman-core/src/agent/subagent_host/tool_prep.rs
index e117921fc3..625e64bf33 100644
--- a/crates/openhuman-core/src/agent/subagent_host/tool_prep.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/tool_prep.rs
@@ -7,11 +7,21 @@
use super::types::SubagentRunError;
use crate::agent::prompts::PromptContext;
use crate::agent_registry::host_definition::AgentDefinition;
+use std::sync::Arc;
use tinyagents_definition::{PromptSource, ToolScope};
use tinyagents_harness::tool::ToolSnapshot;
use tinyagents_orchestration::subagent::{restrict_tools_with_scope, SubagentRole};
use tinytools::Tool;
+/// Adapts the owned host catalogue to TinyAgents' shared Arc-backed
+/// deduplication surface and returns the owned specs expected by dispatch.
+pub(crate) fn dedup_owned_tool_specs(specs: Vec<tinytools::ToolSpec>) -> Vec<tinytools::ToolSpec> {
+ tinyagents_runtime::dedup_visible_tool_specs(specs.into_iter().map(Arc::new).collect())
+ .into_iter()
+ .map(|spec| Arc::try_unwrap(spec).expect("newly wrapped spec has one owner"))
+ .collect()
+}
+
// ── Prompt dialect selection ────────────────────────────────────────────
/// The tool-call format a sub-agent's prompt is rendered for, and the
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware_stop_cause_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware_stop_cause_tests.rs
index 1a210eb460..4ad3b37f50 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware_stop_cause_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware_stop_cause_tests.rs
@@ -4,7 +4,7 @@
//! left, the way the session driver does.
use super::*;
-use crate::agent::session_host::stop_summary::{render_stop_summary, StopReasonKind};
+use crate::agent::session_host::stop_policy::{render_stop_summary, StopReasonKind};
use crate::agent::tinyagents::middleware::repeated_failure::RepeatedToolFailureMiddleware;
use tinyagents_harness::close::CloseToolResult as CheckpointToolResult;
use tinyagents_harness::middleware::is_repeat_call_exempt;
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_run_finalize.rs b/crates/openhuman-core/src/agent/tinyagents/turn_run_finalize.rs
index bd60e759f9..252ceb1326 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_run_finalize.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_run_finalize.rs
@@ -162,7 +162,7 @@ pub(super) async fn finalize_turn_outcome(
// clean completion. No deadline wind-down on this path: only the session
// turn carries one, and it records the stop through its sidecar.
let failure_cause = run.failure_cause.clone();
- let stop = crate::agent::session_host::stop_summary::classify_stop_with_cause(
+ let stop = crate::agent::session_host::stop_policy::classify_stop_with_cause(
breaker_halt.as_deref(),
failure_cause.clone(),
false,
diff --git a/crates/openhuman-core/src/agent/turn_env.rs b/crates/openhuman-core/src/agent/turn_env.rs
index 584b53c807..e3eae357ca 100644
--- a/crates/openhuman-core/src/agent/turn_env.rs
+++ b/crates/openhuman-core/src/agent/turn_env.rs
@@ -18,8 +18,8 @@ use std::sync::Arc;
use tinyagents_harness::cancel::CancellationToken;
use tinyagents_harness::turn_deadline::TurnDeadline;
-use tinyagents_harness::cost::ModelBudget;
use crate::agent::tinyagents::host::OpenHumanRunContext;
+use tinyagents_harness::cost::ModelBudget;
/// Per-turn values an entry point hands to the session it runs.
///
diff --git a/crates/openhuman-core/src/inference/host_runtime/ops/complete_once.rs b/crates/openhuman-core/src/inference/host_runtime/ops/complete_once.rs
index f440f78bfe..8f8e943651 100644
--- a/crates/openhuman-core/src/inference/host_runtime/ops/complete_once.rs
+++ b/crates/openhuman-core/src/inference/host_runtime/ops/complete_once.rs
@@ -159,7 +159,7 @@ pub async fn complete_once_controlled(
});
if admission.is_none() {
let model: std::sync::Arc<dyn ChatModel<()>> = match (budget, observer) {
- (Some(budget), observer) => budget.wrap_with_observer(model, observer),
+ (Some(budget), observer) => budget.wrap_model_with_observer(model, observer),
(None, Some(observer)) => {
let normalized =
std::sync::Arc::new(tinyagents_harness::cost::GatewayChargeModel::new(model));
diff --git a/crates/openhuman-core/src/storage/sessions/workspace.rs b/crates/openhuman-core/src/storage/sessions/host_stores.rs
similarity index 98%
rename from crates/openhuman-core/src/storage/sessions/workspace.rs
rename to crates/openhuman-core/src/storage/sessions/host_stores.rs
index c9cd2f6d17..0790527180 100644
--- a/crates/openhuman-core/src/storage/sessions/workspace.rs
+++ b/crates/openhuman-core/src/storage/sessions/host_stores.rs
@@ -100,7 +100,7 @@ pub(super) fn fallback_provider(workspace_dir: &Path) -> Arc<dyn SessionStorePro
return existing.provider.clone();
}
}
- match TenantSessionStores::new(Arc::clone(&backend), false) {
+ match TenantSessionStores::new_workspace_local(Arc::clone(&backend), false) {
Ok(provider) => {
let provider = Arc::new(provider);
fallbacks.insert(
@@ -127,7 +127,7 @@ fn memory_fallback(workspace_dir: &Path) -> Arc<dyn SessionStoreProvider> {
.entry(workspace_dir.to_path_buf())
.or_insert_with(|| {
let backend: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());
- match TenantSessionStores::new(backend, false) {
+ match TenantSessionStores::new_workspace_local(backend, false) {
Ok(provider) => Arc::new(provider),
Err(_) => Arc::new(tinyagents_session::port::InMemorySessionStores::new()),
}
@@ -420,7 +420,7 @@ where
/// For the one host with no single backend to install: a SaaS core with no
/// storage URL, where every profile has a workspace of its own and so a
/// database of its own. Every other host installs
-/// [`ReboundSessionStores`](super::ReboundSessionStores) over its backend.
+/// [`TenantSessionStores`](super::TenantSessionStores) over its backend.
#[derive(Debug, Default, Clone, Copy)]
pub struct WorkspaceSessionStores;
@@ -486,5 +486,5 @@ pub fn session_scope(key: &str) -> tinystoragedrivers::Scope {
}
#[cfg(test)]
-#[path = "workspace_tests.rs"]
+#[path = "host_stores_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/storage/sessions/workspace_tests.rs b/crates/openhuman-core/src/storage/sessions/host_stores_tests.rs
similarity index 100%
rename from crates/openhuman-core/src/storage/sessions/workspace_tests.rs
rename to crates/openhuman-core/src/storage/sessions/host_stores_tests.rs
diff --git a/crates/openhuman-core/src/storage/sessions/mod.rs b/crates/openhuman-core/src/storage/sessions/mod.rs
index 107b75fe83..d1975c3665 100644
--- a/crates/openhuman-core/src/storage/sessions/mod.rs
+++ b/crates/openhuman-core/src/storage/sessions/mod.rs
@@ -19,19 +19,17 @@ use std::sync::{Arc, LazyLock, Mutex, PoisonError, RwLock};
use tinyagents_session::port::{AgentStores, SessionStoreProvider};
-mod rebound;
+mod host_stores;
mod tenant_router;
-mod workspace;
-pub use rebound::{rebind_all, ReboundSessionStores, StoreRegistry};
-pub use tenant_router::TenantSessionStores;
-pub use workspace::{
+pub use host_stores::{
artifact_blobs_for, artifact_scoped_for_in, attachment_stash_for, current_documents_for,
default_turn_states_for, ledger_for, provider_for_workspace, provider_for_workspace_in,
release_workspace, release_workspace_defaults, require_ledger, session_scope, status_store_for,
stores_for, stores_or_ephemeral, turn_states_for, with_ledger, with_ledger_async,
WorkspaceSessionStores,
};
+pub use tenant_router::{rebind_all, TenantSessionStores};
static PROVIDER: LazyLock<RwLock<Option<Arc<dyn SessionStoreProvider>>>> =
LazyLock::new(|| RwLock::new(None));
diff --git a/crates/openhuman-core/src/storage/sessions/rebound.rs b/crates/openhuman-core/src/storage/sessions/rebound.rs
deleted file mode 100644
index 8e3b54eea2..0000000000
--- a/crates/openhuman-core/src/storage/sessions/rebound.rs
+++ /dev/null
@@ -1,194 +0,0 @@
-//! 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 [`TenantSessionStores`] 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 super::tenant_router::TenantSessionStores;
-use tinyagents_session::port::{AgentStores, SessionStoreProvider};
-use tinystoragedrivers::{Blocking, StorageBackend, StorageError};
-
-/// 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);
-
-/// [`TenantSessionStores`] over a backend [`rebind_all`] can replace.
-pub struct ReboundSessionStores {
- inner: RwLock<Arc<TenantSessionStores>>,
- 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
- /// the provider's `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> {
- 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 = TenantSessionStores::with_bridge(backend, bridge.clone(), recover_on_open);
- let this = Arc::new(Self {
- inner: RwLock::new(Arc::new(inner)),
- bridge,
- recover_on_open,
- });
- registry.register(&this);
- Ok(this)
- }
-
- fn current(&self) -> Arc<TenantSessionStores> {
- 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 =
- TenantSessionStores::with_bridge(backend, self.bridge.clone(), 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`] of the process to `backend`;
-/// returns how many.
-pub fn rebind_all(backend: &Arc<dyn StorageBackend>) -> usize {
- PROCESS.rebind(backend)
-}
-
-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/storage/sessions/rebound_tests.rs b/crates/openhuman-core/src/storage/sessions/rebound_tests.rs
deleted file mode 100644
index cdde20b4a3..0000000000
--- a/crates/openhuman-core/src/storage/sessions/rebound_tests.rs
+++ /dev/null
@@ -1,128 +0,0 @@
-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 registry = StoreRegistry::default();
- let stores = ReboundSessionStores::new_in(&registry, Arc::clone(&first), true).unwrap();
- put_marker(&stores, &first);
- let before = stores.destination_key();
-
- // 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.
- 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 registry = StoreRegistry::default();
- let stores = ReboundSessionStores::new_in(&registry, Arc::clone(&first), false).unwrap();
- let old = stores.try_for_agent("agent").unwrap();
- registry.rebind(&second);
- let new = stores.try_for_agent("agent").unwrap();
- old.turn_states
- .put(&tinyagents_session::turn_state::TurnState::started(
- "t",
- "r",
- 4,
- "2026-01-01T00:00:00Z",
- ))
- .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 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]
-fn the_provider_surface_forwards_to_the_current_backend() {
- use tinyagents_harness::store::SchemaAdmission;
-
- let backend: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());
- 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:?}");
- assert!(rendered.contains("ReboundSessionStores"), "{rendered}");
- assert!(rendered.contains("destination"), "{rendered}");
-
- // `for_agent` and `try_for_agent` open the same agent scope: a turn state
- // written through one is read back through the other.
- let infallible = stores.for_agent("forwarded");
- let fallible = stores.try_for_agent("forwarded").unwrap();
- infallible
- .turn_states
- .put(&tinyagents_session::turn_state::TurnState::started(
- "t-fwd",
- "r",
- 4,
- "2026-01-01T00:00:00Z",
- ))
- .unwrap();
- assert!(fallible.turn_states.get("t-fwd").unwrap().is_some());
-
- assert!(stores.recover().is_ok());
- assert_eq!(
- stores.destination_key(),
- stores.current().destination_key(),
- "the destination is the current inner provider's"
- );
- stores
- .run_ledger_for_agent("forwarded")
- .expect("the ledger lookup does not fail on a fresh memory backend");
- stores
- .conversations_for_agent("forwarded")
- .expect("the conversation lookup does not fail on a fresh memory backend");
-
- // Binding an actor goes through the inner provider, with and without an
- // explicit schema admission mode; both agree on whether it binds.
- let other: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());
- let plain = stores.bind_actor(Arc::clone(&other)).unwrap().is_some();
- let admitted = stores
- .bind_actor_with_schema_admission(other, SchemaAdmission::Declare)
- .unwrap()
- .is_some();
- assert_eq!(plain, admitted);
-}
diff --git a/crates/openhuman-core/src/storage/sessions/tenant_router.rs b/crates/openhuman-core/src/storage/sessions/tenant_router.rs
index 54c197d5f9..d99202759f 100644
--- a/crates/openhuman-core/src/storage/sessions/tenant_router.rs
+++ b/crates/openhuman-core/src/storage/sessions/tenant_router.rs
@@ -1,6 +1,6 @@
-//! [`TenantSessionStores`]: one [`DriverSessionStores`] per tenant.
+//! [`TenantSessionStores`]: one upstream rebindable provider per tenant.
//!
-//! TinyAgents' `DriverSessionStores` serves one tenant and derives
+//! TinyAgents' rebindable provider serves one tenant and derives
//! `tenant.agent(id)` itself. The host keeps its flat session keys
//! ([`crate::core::runtime::session_key`]: the agent id for the desktop
//! operator, `<profile>~<agent>` for a SaaS profile), so this provider decodes
@@ -10,22 +10,64 @@
//! `u:<profile>/agent:<id>`, inside the tenant fence.
use std::collections::HashMap;
-use std::sync::{Arc, Mutex, PoisonError};
+use std::sync::{Arc, LazyLock, Mutex, PoisonError, RwLock, Weak};
use tinyagents_harness::store::SchemaAdmission;
use tinyagents_session::port::{
AgentStores, SessionStoreLease, SessionStoreLeaseError, SessionStoreProvider,
};
-use tinyagents_session::DriverSessionStores;
-use tinystoragedrivers::{Blocking, Scope, StorageBackend, StorageError};
+use tinyagents_session::{RebindableSessionStores, SessionStoreRegistry};
+use tinystoragedrivers::{Scope, StorageBackend, StorageError};
+
+#[derive(Default)]
+struct TenantStoreRegistry {
+ providers: Arc<SessionStoreRegistry>,
+ backends: Mutex<Vec<Weak<RwLock<Arc<dyn StorageBackend>>>>>,
+}
+
+impl TenantStoreRegistry {
+ fn rebind(&self, backend: &Arc<dyn StorageBackend>) -> usize {
+ let rebound = match self.providers.rebind(Arc::clone(backend)) {
+ Ok(rebound) => rebound,
+ Err(error) => {
+ tracing::error!(target: "openhuman::storage", %error, "[session_store] could not rebind session providers");
+ return 0;
+ }
+ };
+ let mut live = self.backends.lock().unwrap_or_else(PoisonError::into_inner);
+ live.retain(|weak| {
+ if let Some(current) = weak.upgrade() {
+ *current.write().unwrap_or_else(PoisonError::into_inner) = Arc::clone(backend);
+ true
+ } else {
+ false
+ }
+ });
+ rebound
+ }
+
+ fn register_backend(&self, backend: &Arc<RwLock<Arc<dyn StorageBackend>>>) {
+ let mut live = self.backends.lock().unwrap_or_else(PoisonError::into_inner);
+ live.retain(|weak| weak.strong_count() > 0);
+ live.push(Arc::downgrade(backend));
+ }
+}
+
+static PROCESS_SESSION_STORES: LazyLock<Arc<TenantStoreRegistry>> =
+ LazyLock::new(|| Arc::new(TenantStoreRegistry::default()));
+
+/// Rebinds the host's live tenant routers to `backend`.
+pub fn rebind_all(backend: &Arc<dyn StorageBackend>) -> usize {
+ PROCESS_SESSION_STORES.rebind(backend)
+}
/// Session stores over one backend, routed to a per-tenant provider.
pub struct TenantSessionStores {
- backend: Arc<dyn StorageBackend>,
- bridge: Blocking,
+ backend: Arc<RwLock<Arc<dyn StorageBackend>>>,
recover_on_open: bool,
admission: SchemaAdmission,
- tenants: Mutex<HashMap<Scope, Arc<DriverSessionStores>>>,
+ tenants: Mutex<HashMap<Scope, Arc<RebindableSessionStores>>>,
+ registry: Arc<TenantStoreRegistry>,
/// Names this router in [`SessionStoreProvider::destination_key`].
id: uuid::Uuid,
}
@@ -33,57 +75,66 @@ pub struct TenantSessionStores {
impl std::fmt::Debug for TenantSessionStores {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let tenants = self.tenants.lock().unwrap_or_else(PoisonError::into_inner);
+ let backend = self.backend.read().unwrap_or_else(PoisonError::into_inner);
f.debug_struct("TenantSessionStores")
- .field("driver", &self.backend.driver())
+ .field("driver", &backend.driver())
.field("tenants", &tenants.len())
.finish_non_exhaustive()
}
}
impl TenantSessionStores {
- /// Stores over `backend` on a shared `bridge`.
- pub fn with_bridge(
- backend: Arc<dyn StorageBackend>,
- bridge: Blocking,
- recover_on_open: bool,
- ) -> Self {
- Self::with_admission(backend, bridge, recover_on_open, SchemaAdmission::Declare)
- }
-
- fn with_admission(
+ fn with_registry(
backend: Arc<dyn StorageBackend>,
- bridge: Blocking,
recover_on_open: bool,
admission: SchemaAdmission,
+ registry: Arc<TenantStoreRegistry>,
) -> Self {
Self {
- backend,
- bridge,
+ backend: Arc::new(RwLock::new(backend)),
recover_on_open,
admission,
tenants: Mutex::new(HashMap::new()),
+ registry,
id: uuid::Uuid::new_v4(),
}
}
- /// Stores over `backend` with a bridge of their own.
+ /// Stores over `backend`, registered for workspace rebinds.
///
/// # Errors
///
- /// When the blocking bridge cannot start.
+ /// When the TinyAgents session provider cannot start.
pub fn new(
backend: Arc<dyn StorageBackend>,
recover_on_open: bool,
) -> Result<Self, StorageError> {
- Ok(Self::with_bridge(
+ let router = Self::with_registry(
+ backend,
+ recover_on_open,
+ SchemaAdmission::Declare,
+ Arc::clone(&PROCESS_SESSION_STORES),
+ );
+ router.registry.register_backend(&router.backend);
+ Ok(router)
+ }
+
+ /// Workspace fallback stores are tied to a fixed workspace backend and
+ /// therefore must not join the process-wide workspace-rebind registry.
+ pub(super) fn new_workspace_local(
+ backend: Arc<dyn StorageBackend>,
+ recover_on_open: bool,
+ ) -> Result<Self, StorageError> {
+ Ok(Self::with_registry(
backend,
- Blocking::new()?,
recover_on_open,
+ SchemaAdmission::Declare,
+ Arc::new(TenantStoreRegistry::default()),
))
}
/// The provider of the tenant `key` belongs to, and the agent id within it.
- fn route(&self, key: &str) -> (Arc<DriverSessionStores>, String) {
+ fn route(&self, key: &str) -> (Arc<RebindableSessionStores>, String) {
let (profile, mut agent) = crate::core::runtime::split_session_key(key);
if agent.is_empty() {
agent = crate::storage::scope::DEFAULT_AGENT.to_owned();
@@ -93,15 +144,30 @@ impl TenantSessionStores {
if let Some(provider) = tenants.get(&tenant) {
return (Arc::clone(provider), agent);
}
- let provider = Arc::new(
- DriverSessionStores::with_bridge_and_schema_admission(
- Arc::clone(&self.backend),
- tenant.clone(),
- self.bridge.clone(),
- self.admission,
- )
- .recover_on_open(self.recover_on_open),
- );
+ let provider = match RebindableSessionStores::new_in_tenant(
+ &self.registry.providers,
+ Arc::clone(&self.backend.read().unwrap_or_else(PoisonError::into_inner)),
+ tenant.clone(),
+ self.recover_on_open,
+ ) {
+ Ok(provider) => provider,
+ Err(error) => {
+ tracing::error!(target: "openhuman::storage", tenant = tenant.as_str(), %error, "[session_store] could not open tenant session provider");
+ // Storage is unavailable, so keep this tenant isolated in a
+ // temporary in-memory provider rather than redirecting its
+ // session data into another tenant's store.
+ return (
+ RebindableSessionStores::new_in_tenant(
+ &self.registry.providers,
+ Arc::new(crate::storage::MemoryStorage::new()),
+ tenant.clone(),
+ false,
+ )
+ .expect("in-memory session store provider must initialize"),
+ agent,
+ );
+ }
+ };
tracing::debug!(
target: "openhuman::storage",
tenant = tenant.as_str(),
@@ -111,7 +177,7 @@ impl TenantSessionStores {
(provider, agent)
}
- fn providers(&self) -> Vec<Arc<DriverSessionStores>> {
+ fn providers(&self) -> Vec<Arc<RebindableSessionStores>> {
self.tenants
.lock()
.unwrap_or_else(PoisonError::into_inner)
@@ -149,12 +215,14 @@ impl SessionStoreProvider for TenantSessionStores {
backend: Arc<dyn StorageBackend>,
admission: SchemaAdmission,
) -> Result<Option<Arc<dyn SessionStoreProvider>>, StorageError> {
- Ok(Some(Arc::new(Self::with_admission(
+ let actor = Self::with_registry(
backend,
- self.bridge.clone(),
self.recover_on_open,
admission,
- ))))
+ Arc::clone(&self.registry),
+ );
+ self.registry.register_backend(&actor.backend);
+ Ok(Some(Arc::new(actor)))
}
fn run_ledger_for_agent(
@@ -185,7 +253,14 @@ impl SessionStoreProvider for TenantSessionStores {
}
fn destination_key(&self) -> Option<String> {
- Some(format!("{}://{}", self.backend.driver(), self.id))
+ Some(format!(
+ "{}://{}",
+ self.backend
+ .read()
+ .unwrap_or_else(PoisonError::into_inner)
+ .driver(),
+ self.id
+ ))
}
}
diff --git a/crates/openhuman-core/src/storage/sessions/tenant_router_tests.rs b/crates/openhuman-core/src/storage/sessions/tenant_router_tests.rs
index 03332d31bb..e45ce6ffa2 100644
--- a/crates/openhuman-core/src/storage/sessions/tenant_router_tests.rs
+++ b/crates/openhuman-core/src/storage/sessions/tenant_router_tests.rs
@@ -2,19 +2,27 @@ use super::*;
use crate::storage::MemoryStorage;
fn stores() -> TenantSessionStores {
- TenantSessionStores::new(Arc::new(MemoryStorage::new()), false).expect("bridge")
+ TenantSessionStores::with_registry(
+ Arc::new(MemoryStorage::new()),
+ false,
+ tinyagents_harness::store::SchemaAdmission::Declare,
+ Arc::new(TenantStoreRegistry::default()),
+ )
}
#[test]
fn an_unprofiled_key_routes_to_the_local_tenant() {
let router = stores();
let (provider, agent) = router.route("asha");
- assert_eq!(provider.tenant(), &Scope::local());
- assert_eq!(agent, "asha");
- assert_eq!(
- provider.scope_for(&agent).unwrap().as_str(),
- "local/agent:asha"
+ let destination = provider.for_agent(&agent).transcripts.destination_key();
+ let expected = crate::storage::tenant_scope(None).agent("asha").unwrap();
+ assert!(
+ destination
+ .as_deref()
+ .is_some_and(|key| key.contains(expected.as_str())),
+ "{destination:?}"
);
+ assert_eq!(agent, "asha");
}
#[test]
@@ -25,10 +33,15 @@ fn a_profile_key_routes_to_the_profile_tenant() {
agent: Some("asha".into()),
});
let (provider, agent) = router.route(&key);
- assert_eq!(provider.tenant().as_str(), "u:alice");
- assert_eq!(
- provider.scope_for(&agent).unwrap().as_str(),
- "u:alice/agent:asha"
+ let destination = provider.for_agent(&agent).transcripts.destination_key();
+ let expected = crate::storage::scope_for_profile("alice")
+ .agent("asha")
+ .unwrap();
+ assert!(
+ destination
+ .as_deref()
+ .is_some_and(|key| key.contains(expected.as_str())),
+ "{destination:?}"
);
}
@@ -40,9 +53,15 @@ fn escaped_keys_round_trip_to_the_same_tenant_and_agent() {
agent: None,
});
let (provider, agent) = router.route(&key);
- assert_eq!(
- provider.tenant(),
- &crate::storage::scope_for_profile("a~b%c")
+ let destination = provider.for_agent(&agent).transcripts.destination_key();
+ let expected = crate::storage::scope_for_profile("a~b%c")
+ .agent("default")
+ .unwrap();
+ assert!(
+ destination
+ .as_deref()
+ .is_some_and(|key| key.contains(expected.as_str())),
+ "{destination:?}"
);
assert_eq!(agent, "default");
}
@@ -81,3 +100,70 @@ fn pinned_session_store_leases_keep_profile_and_agent_routing() {
bob.stores().transcripts.destination_key()
);
}
+
+#[test]
+fn upstream_rebind_keeps_tenant_routing_and_moves_only_future_leases() {
+ let registry = Arc::new(TenantStoreRegistry::default());
+ let first: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());
+ let second: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());
+ let router = TenantSessionStores::with_registry(
+ Arc::clone(&first),
+ false,
+ tinyagents_harness::store::SchemaAdmission::Declare,
+ Arc::clone(&registry),
+ );
+ registry.register_backend(&router.backend);
+ let alice_key = crate::core::runtime::session_key(&crate::core::runtime::Tenant {
+ profile: Some("alice".into()),
+ agent: Some("asha".into()),
+ });
+ let bob_key = crate::core::runtime::session_key(&crate::core::runtime::Tenant {
+ profile: Some("bob".into()),
+ agent: Some("asha".into()),
+ });
+
+ let alice_before = router.lease_for_agent(&alice_key).unwrap();
+ let bob_before = router.lease_for_agent(&bob_key).unwrap();
+ let alice_destination = alice_before.stores().transcripts.destination_key();
+ let bob_destination = bob_before.stores().transcripts.destination_key();
+ assert_ne!(
+ alice_destination, bob_destination,
+ "profiles keep separate scopes"
+ );
+
+ assert_eq!(registry.rebind(&second), 2);
+ assert!(
+ Arc::ptr_eq(&*router.backend.read().unwrap(), &second),
+ "new tenants open on the newly bound workspace backend"
+ );
+ let alice_after = router.lease_for_agent(&alice_key).unwrap();
+ let bob_after = router.lease_for_agent(&bob_key).unwrap();
+ let charlie_key = crate::core::runtime::session_key(&crate::core::runtime::Tenant {
+ profile: Some("charlie".into()),
+ agent: Some("asha".into()),
+ });
+ let charlie_after = router.lease_for_agent(&charlie_key).unwrap();
+ assert_ne!(
+ alice_destination,
+ alice_after.stores().transcripts.destination_key(),
+ "a future lease follows the upstream rebind"
+ );
+ assert_ne!(
+ bob_destination,
+ bob_after.stores().transcripts.destination_key(),
+ "each tenant provider follows the same backend rebind"
+ );
+ assert_eq!(
+ alice_destination,
+ alice_before.stores().transcripts.destination_key(),
+ "an already-issued lease remains pinned"
+ );
+ assert!(
+ charlie_after
+ .stores()
+ .transcripts
+ .destination_key()
+ .is_some(),
+ "a tenant first seen after rebind still gets a session store"
+ );
+}
diff --git a/crates/openhuman-embed/src/budget.rs b/crates/openhuman-embed/src/budget.rs
index 621b051836..5af85cc3a8 100644
--- a/crates/openhuman-embed/src/budget.rs
+++ b/crates/openhuman-embed/src/budget.rs
@@ -18,11 +18,11 @@
//! Budgeted calls currently accept text only. Input bounds use serialized-byte
//! counts conservatively; multimodal requests fail before dispatch. Choose
//! bounds that include model framing and the maximum price on allowed routes.
-pub use tinyagents_harness::cost::ModelBudget;
pub use openhuman_core::agent::tinyagents::budget::{
ControlledDispatchError, DispatchModelOptions, DispatchRefusal, DispatchRefusalReason,
PhysicalDispatchAdmission, PhysicalDispatchPermit,
};
+pub use tinyagents_harness::cost::ModelBudget;
pub use tinyinference_llm::model::budget::{
Budget, BudgetExceeded, BudgetSnapshot, CallBudget, Spend, SpendLimits,
};