Skip to content

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

feat(runtime): add tenant-scoped event context

Steven Enamakel1147cac14e0f
diff --git a/crates/openhuman-core/src/core/runtime/event_ctx.rs b/crates/openhuman-core/src/core/runtime/event_ctx.rs
new file mode 100644
index 0000000000..49e93cc8e8
--- /dev/null
+++ b/crates/openhuman-core/src/core/runtime/event_ctx.rs
@@ -0,0 +1,74 @@
+//! The dispatch context of a bus event.
+//!
+//! A subscriber runs on the broker's task, not on the publisher's, so it
+//! inherits no task scope. An event that concerns one tenant therefore carries
+//! that tenant ([`Tenant`]), and the subscriber rebuilds its [`DispatchCtx`]
+//! from it with [`DispatchCtx::for_event`]:
+//!
+//! - the tenant's live context when it has one (a SaaS profile's, an embedded
+//! agent's), whatever other context happens to be active;
+//! - the process default for an untagged event on a single-user host;
+//! - nothing (the subscriber logs and skips) in SaaS for an untagged event, and
+//! for a tagged one whose tenant is gone: work that lost its tenant never
+//! acts as the operator.
+
+use std::sync::Arc;
+
+use super::{CoreContext, DispatchCtx, Tenant};
+
+/// The live context `tenant` acts under, if it has one.
+///
+/// A SaaS profile resolves through the process's profile host; an embedded
+/// agent through the agent context registry (outside SaaS it may also fall
+/// back to the default context acting for it). A tenant with neither profile
+/// nor agent is the host's own, which has no context of its own to find.
+pub fn context_for_tenant(tenant: &Tenant) -> Option<Arc<CoreContext>> {
+ if let Some(profile) = tenant.profile.as_deref() {
+ let id = crate::profiles::ProfileId::parse(profile).ok()?;
+ return crate::profiles::host::host()?
+ .get(&id)
+ .map(|state| Arc::clone(state.context()));
+ }
+ let agent = tenant.agent.as_deref()?;
+ crate::storage::agents::context_for(agent)
+}
+
+impl DispatchCtx {
+ /// The tenant this call acts for, to tag an event with. `None` when it has
+ /// none (SaaS work that lost its scope): the event then carries no tenant
+ /// and its subscribers fail closed.
+ pub fn event_tenant(&self) -> Option<Tenant> {
+ self.tenant().ok()
+ }
+
+ /// The context a subscriber of an event tagged with `tenant` runs under.
+ ///
+ /// `None` means the event must not be handled: SaaS with no tenant, or a
+ /// tenant (profile or agent) with no live context left. See the module
+ /// docs.
+ pub fn for_event(tenant: Option<&Tenant>) -> Option<Self> {
+ let Some(tenant) = tenant.filter(|tenant| tenant.is_scoped()) else {
+ // Untagged, or tagged with the host's own default orchestrator.
+ return if super::is_saas() {
+ None
+ } else {
+ Some(Self::process_default())
+ };
+ };
+ match context_for_tenant(tenant) {
+ Some(core) => Some(Self::new(core)),
+ None => {
+ tracing::warn!(
+ profile = ?tenant.profile,
+ agent = ?tenant.agent,
+ "[event-ctx] no live context for the event's tenant; event skipped"
+ );
+ None
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+#[path = "event_ctx_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/core/runtime/mod.rs b/crates/openhuman-core/src/core/runtime/mod.rs
index 6fcfcf7fea..7174bf6832 100644
--- a/crates/openhuman-core/src/core/runtime/mod.rs
+++ b/crates/openhuman-core/src/core/runtime/mod.rs
@@ -52,6 +52,7 @@ pub use components::{ComponentSet, COMPONENT_NAMES};
pub mod context;
pub mod dispatch_ctx;
pub mod domain_set;
+pub mod event_ctx;
mod features;
pub use features::compiled_features;
pub mod mode;
diff --git a/crates/openhuman-core/src/core/runtime/tenant.rs b/crates/openhuman-core/src/core/runtime/tenant.rs
index 58b7ea627d..7ab4db2564 100644
--- a/crates/openhuman-core/src/core/runtime/tenant.rs
+++ b/crates/openhuman-core/src/core/runtime/tenant.rs
@@ -23,7 +23,7 @@ use std::sync::Arc;
use super::CoreContext;
/// The tenant (profile) and the agent inside it that work runs for.
-#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
+#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct Tenant {
/// The SaaS profile; `None` on the desktop and for embedded agents.
pub profile: Option<String>,