Skip to content

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

refactor(runtime): read tenant from ambient dispatch context

Steven Enamakel2d05701ad8dc
diff --git a/crates/openhuman-core/src/agent/session_store/workspace.rs b/crates/openhuman-core/src/agent/session_store/workspace.rs
index e2e7d55903..f13e33c117 100644
--- a/crates/openhuman-core/src/agent/session_store/workspace.rs
+++ b/crates/openhuman-core/src/agent/session_store/workspace.rs
@@ -111,7 +111,7 @@ fn memory_fallback(workspace_dir: &Path) -> Arc<dyn SessionStoreProvider> {
/// The key the calling tenant's stores are under, or `None` for a SaaS task
/// with no scope (which must not act).
fn tenant_agent_key() -> Option<String> {
- match crate::core::runtime::current_tenant() {
+ match crate::core::runtime::DispatchCtx::ambient().tenant() {
Ok(tenant) => Some(crate::core::runtime::session_key(&tenant)),
Err(no_tenant) => {
log::warn!("[session_store] {no_tenant}");
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 a3dbc916bc..84e0833f1c 100644
--- a/crates/openhuman-core/src/agent/session_store/workspace_tests.rs
+++ b/crates/openhuman-core/src/agent/session_store/workspace_tests.rs
@@ -118,10 +118,16 @@ fn releasing_defaults_lets_the_next_call_open_afresh() {
let ws = scratch();
let before = fallback_provider(ws.path());
let cached = fallback_provider(ws.path());
- assert!(Arc::ptr_eq(&before, &cached), "a repeat call reuses the provider");
+ assert!(
+ Arc::ptr_eq(&before, &cached),
+ "a repeat call reuses the provider"
+ );
release_workspace_defaults();
let after = fallback_provider(ws.path());
- assert!(!Arc::ptr_eq(&before, &after), "release drops the cached provider");
+ assert!(
+ !Arc::ptr_eq(&before, &after),
+ "release drops the cached provider"
+ );
}
#[test]
diff --git a/crates/openhuman-core/src/integrations/composio/connected_integrations/cache.rs b/crates/openhuman-core/src/integrations/composio/connected_integrations/cache.rs
index 97165318b8..2cc155f9ee 100644
--- a/crates/openhuman-core/src/integrations/composio/connected_integrations/cache.rs
+++ b/crates/openhuman-core/src/integrations/composio/connected_integrations/cache.rs
@@ -53,7 +53,7 @@ pub(crate) fn cache_generation(key: &str) -> Arc<AtomicU64> {
/// Resolve the catalog's profile/operator identity, refusing missing SaaS scope.
fn current_cache_scope_prefix(operation: &'static str) -> Option<String> {
- let tenant = match crate::core::runtime::current_tenant() {
+ let tenant = match crate::core::runtime::DispatchCtx::ambient().tenant() {
Ok(tenant) => tenant,
Err(error) => {
// NoTenant is a fixed, content-free error; do not attach identities.
diff --git a/crates/openhuman-core/src/profiles/capabilities.rs b/crates/openhuman-core/src/profiles/capabilities.rs
index 4f5ea05403..6b7a501d62 100644
--- a/crates/openhuman-core/src/profiles/capabilities.rs
+++ b/crates/openhuman-core/src/profiles/capabilities.rs
@@ -259,7 +259,9 @@ impl ToolHook for ProfilePermission {
Ok(())
}
async fn before_tool_decision(&self, context: &ToolHookContext) -> ToolHookDecision {
- let tenant = crate::core::runtime::current_tenant()
+ // The hook runs inside the turn's own task, so its tenant is the ambient one.
+ let tenant = crate::core::runtime::DispatchCtx::ambient()
+ .tenant()
.ok()
.and_then(|tenant| tenant.profile);
if tenant.as_deref() != Some(self.profile.as_str())
diff --git a/crates/openhuman-core/src/skills/ops_discover/api.rs b/crates/openhuman-core/src/skills/ops_discover/api.rs
index 7709fd1e7b..2ba8536b5c 100644
--- a/crates/openhuman-core/src/skills/ops_discover/api.rs
+++ b/crates/openhuman-core/src/skills/ops_discover/api.rs
@@ -100,7 +100,7 @@ pub fn load_workflow_metadata(workspace_dir: &Path) -> Vec<Workflow> {
workspace_dir.to_path_buf(),
home.clone(),
trusted,
- crate::core::runtime::current_tenant().ok(),
+ crate::core::runtime::DispatchCtx::ambient().tenant().ok(),
);
if let Ok(cache) = METADATA_CACHE.read() {
if let Some(workflows) = cache.get(&key) {
diff --git a/crates/openhuman-core/src/skills/write_root.rs b/crates/openhuman-core/src/skills/write_root.rs
index 709c83f2ed..35407c746e 100644
--- a/crates/openhuman-core/src/skills/write_root.rs
+++ b/crates/openhuman-core/src/skills/write_root.rs
@@ -25,7 +25,7 @@ use crate::core::runtime::Tenant;
/// with no scope.
#[must_use]
pub fn agent_skill_home(workspace_dir: &Path) -> Option<PathBuf> {
- let tenant = crate::core::runtime::current_tenant().ok()?;
+ let tenant = crate::core::runtime::DispatchCtx::ambient().tenant().ok()?;
skill_home_for(&tenant, workspace_dir)
}
diff --git a/crates/openhuman-core/src/storage/agents.rs b/crates/openhuman-core/src/storage/agents.rs
index 7aee80764d..afad2e78b1 100644
--- a/crates/openhuman-core/src/storage/agents.rs
+++ b/crates/openhuman-core/src/storage/agents.rs
@@ -178,7 +178,8 @@ pub fn context_for(agent: &str) -> Option<Arc<CoreContext>> {
/// A SaaS profile (or a SaaS task with no scope) never switches to the
/// process default context, which is the operator's.
async fn in_local<F: Future>(fut: F) -> F::Output {
- let acting = crate::core::runtime::current_tenant()
+ let acting = crate::core::runtime::DispatchCtx::ambient()
+ .tenant()
.is_ok_and(|tenant| tenant.profile.is_none() && tenant.agent.is_some());
match CoreContext::default_context() {
Some(default) if acting => CoreContext::scope(default, fut).await,
diff --git a/crates/openhuman-core/src/storage/mod.rs b/crates/openhuman-core/src/storage/mod.rs
index a5f19f5f62..3d64e5112b 100644
--- a/crates/openhuman-core/src/storage/mod.rs
+++ b/crates/openhuman-core/src/storage/mod.rs
@@ -213,7 +213,7 @@ pub fn clear() -> bool {
/// In SaaS mode, when the current task serves no profile.
pub fn current_scope() -> Result<Scope, StorageError> {
let saas = crate::core::runtime::mode::is_saas();
- match crate::core::runtime::current_tenant() {
+ match crate::core::runtime::DispatchCtx::ambient().tenant() {
Ok(tenant) => scope_from(tenant.profile.as_deref(), tenant.agent.as_deref(), saas),
Err(no_tenant) => Err(StorageError::invalid_input(no_tenant.to_string())),
}