Skip to content

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

feat(runtime): add agent scope for per-agent runtime isolation

Steven Enamakel3039cc72bc9d
diff --git a/crates/openhuman-core/src/core/runtime/agent_scope.rs b/crates/openhuman-core/src/core/runtime/agent_scope.rs
index bd4753d171..467beb182e 100644
--- a/crates/openhuman-core/src/core/runtime/agent_scope.rs
+++ b/crates/openhuman-core/src/core/runtime/agent_scope.rs
@@ -72,18 +72,17 @@ impl std::fmt::Debug for AgentScopedState {
/// Slots used when no context exists at all.
static UNSCOPED: LazyLock<AgentScopedState> = LazyLock::new(AgentScopedState::default);
-/// The ambient context's slot of type `T`, or the unscoped one when no
-/// context exists.
+/// The slot of type `T` in the context `ctx` resolves to, or the unscoped
+/// one when it resolves to none.
///
-/// In SaaS only the task's own scope counts, and a task without one gets a
-/// throwaway slot: never the shared unscoped one (which every user's lost
-/// work would meet in) and never the operator's.
-pub fn current_slot<T: Default + Send + Sync + 'static>() -> Arc<T> {
- let saas = super::is_saas();
- slot_in::<T>(saas, super::tenant::context_in(saas).as_deref())
+/// In SaaS a call with no bound context gets a throwaway slot: never the
+/// shared unscoped one (which every user's lost work would meet in) and never
+/// the operator's.
+pub fn slot_for<T: Default + Send + Sync + 'static>(ctx: &super::DispatchCtx) -> Arc<T> {
+ slot_in::<T>(super::is_saas(), ctx.resolved().as_deref())
}
-/// [`current_slot`] as a function of the mode and the resolved context.
+/// [`slot_for`] as a function of the mode and the resolved context.
pub fn slot_in<T: Default + Send + Sync + 'static>(
saas: bool,
ctx: Option<&CoreContext>,
diff --git a/crates/openhuman-core/src/core/runtime/mod.rs b/crates/openhuman-core/src/core/runtime/mod.rs
index 5196260f5b..6fcfcf7fea 100644
--- a/crates/openhuman-core/src/core/runtime/mod.rs
+++ b/crates/openhuman-core/src/core/runtime/mod.rs
@@ -61,7 +61,7 @@ pub mod spawn;
pub(crate) mod subscribers;
pub mod tenant;
-pub use agent_scope::{agent_scope_dir, current_slot, AgentContextRegistry, AgentScopedState};
+pub use agent_scope::{agent_scope_dir, slot_for, AgentContextRegistry, AgentScopedState};
pub use builder::{CoreBuilder, CoreRuntime, DomainSet, ServiceSet, TokenSource};
pub use context::{ContextOverlay, CoreContext};
pub use dispatch_ctx::DispatchCtx;
diff --git a/crates/openhuman-core/src/core/runtime/tenant.rs b/crates/openhuman-core/src/core/runtime/tenant.rs
index 0e59338d9c..58b7ea627d 100644
--- a/crates/openhuman-core/src/core/runtime/tenant.rs
+++ b/crates/openhuman-core/src/core/runtime/tenant.rs
@@ -106,7 +106,7 @@ pub fn tenant_in(saas: bool, ctx: Option<&CoreContext>) -> Result<Tenant, NoTena
/// [`current_tenant`], or for a SaaS task with no scope a tenant no other
/// work can share: a fresh, random profile. For infallible key builders whose
/// tables are already per-context (an unscoped SaaS task gets throwaway state
-/// slots, see [`current_slot`](super::current_slot)), so the key can only miss.
+/// slots, see [`slot_for`](super::slot_for)), so the key can only miss.
pub fn current_tenant_or_isolated(site: &str) -> Tenant {
isolated_unless_tenant(current_tenant(), site)
}