Skip to content

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

fix(storage): restrict agent context fallback in saas mode

Steven Enamakel099446a8dcf0
diff --git a/crates/openhuman-core/src/flows/bus/trigger.rs b/crates/openhuman-core/src/flows/bus/trigger.rs
index 6c4cbaffa2..595b7191b4 100644
--- a/crates/openhuman-core/src/flows/bus/trigger.rs
+++ b/crates/openhuman-core/src/flows/bus/trigger.rs
@@ -265,7 +265,7 @@ impl EventHandler<DomainEvent> for FlowTriggerSubscriber {
payload,
..
} => {
- crate::storage::agents::for_each_scope("flows app_event", || {
+ crate::storage::agents::for_each_live_scope("flows app_event", || {
self.handle_app_event(toolkit, trigger, payload)
})
.await;
diff --git a/crates/openhuman-core/src/integrations/task_sources/bus.rs b/crates/openhuman-core/src/integrations/task_sources/bus.rs
index 7e7435b98c..b396f56cdd 100644
--- a/crates/openhuman-core/src/integrations/task_sources/bus.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/bus.rs
@@ -62,7 +62,7 @@ impl EventHandler<DomainEvent> for TaskSourcesConnectionSubscriber {
// Every scope may hold sources for this toolkit (`crate::storage`):
// fire each scope's under its own agent.
- crate::storage::agents::for_each_scope("task_sources connection", || {
+ crate::storage::agents::for_each_live_scope("task_sources connection", || {
fire_for_connection(&config, provider, toolkit, connection_id)
})
.await;
diff --git a/crates/openhuman-core/src/integrations/task_sources/periodic.rs b/crates/openhuman-core/src/integrations/task_sources/periodic.rs
index f5464fa82e..d38cc0e530 100644
--- a/crates/openhuman-core/src/integrations/task_sources/periodic.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/periodic.rs
@@ -101,7 +101,7 @@ async fn run_loop() {
// `local`, then every agent that keeps its sources in its own storage
// scope (`crate::storage::agents`).
for (agent, result) in
- crate::storage::agents::for_each_scope("task_sources", run_one_tick).await
+ crate::storage::agents::for_each_live_scope("task_sources", run_one_tick).await
{
if let Err(e) = result {
tracing::warn!(error = %e, agent = agent.as_deref().unwrap_or("local"), "[task_sources:periodic] tick failed (continuing)");
diff --git a/crates/openhuman-core/src/storage/agents.rs b/crates/openhuman-core/src/storage/agents.rs
index b96b31ffa9..80cb1a5e4f 100644
--- a/crates/openhuman-core/src/storage/agents.rs
+++ b/crates/openhuman-core/src/storage/agents.rs
@@ -159,10 +159,17 @@ fn contexts_in(
}
/// The context to act for `agent` under: its live context when one exists,
-/// else the current context acting for it (`CoreContext::for_agent`).
+/// else — outside SaaS mode — the current context acting for it
+/// (`CoreContext::for_agent`).
pub fn context_for(agent: &str) -> Option<Arc<CoreContext>> {
- AgentContextRegistry::get(agent)
- .or_else(|| CoreContext::current().map(|current| current.for_agent(agent)))
+ AgentContextRegistry::get(agent).or_else(|| {
+ // SaaS acts only through a user's own live context: a copy of the
+ // operator's would carry the wrong configuration.
+ if crate::core::runtime::mode::is_saas() {
+ return None;
+ }
+ CoreContext::current().map(|current| current.for_agent(agent))
+ })
}
/// Runs `fut` acting for `agent` when there is one — background work that
@@ -225,10 +232,46 @@ where
if installed().is_none() {
return Vec::new();
}
+ visit(label, contexts(), step).await
+}
+
+/// [`for_each_scope`], visiting only live agents (`AgentContextRegistry`):
+/// for work that acts as the agent — runs its flows, fetches with its
+/// connections — and so needs the agent's own configuration and tools, which
+/// a recorded agent's stand-in context (`CoreContext::for_agent`) lacks.
+pub async fn for_each_live_scope<T, F, Fut>(label: &str, step: F) -> Vec<(Option<String>, T)>
+where
+ F: Fn() -> Fut,
+ Fut: Future<Output = T>,
+{
+ let mut results = Vec::new();
+ if !crate::core::runtime::mode::is_saas() {
+ results.push((None, step().await));
+ }
+ if installed().is_none() {
+ return results;
+ }
+ for (agent, value) in visit(label, AgentContextRegistry::live(), step).await {
+ results.push((Some(agent), value));
+ }
+ results
+}
+
+/// Runs `step` under each of `contexts`, building each step inside its
+/// agent's scope, so anything it reads while being set up is the agent's.
+async fn visit<T, F, Fut>(
+ label: &str,
+ contexts: Vec<(String, Arc<CoreContext>)>,
+ step: F,
+) -> Vec<(String, T)>
+where
+ F: Fn() -> Fut,
+ Fut: Future<Output = T>,
+{
let mut results = Vec::new();
- for (agent, context) in contexts() {
+ for (agent, context) in contexts {
tracing::trace!(%agent, label, "[storage::agents] visiting agent scope");
- let value = CoreContext::scope(context, step()).await;
+ let value = CoreContext::scope(context, async { step().await }).await;
results.push((agent, value));
}
results