Skip to content

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

fix(flows): revalidate cached flow owners and scope trigger config

Steven Enamakelec48a6936c8d
diff --git a/crates/openhuman-core/src/flows/bus/owner.rs b/crates/openhuman-core/src/flows/bus/owner.rs
index 34dfe1f83e..ea695c029a 100644
--- a/crates/openhuman-core/src/flows/bus/owner.rs
+++ b/crates/openhuman-core/src/flows/bus/owner.rs
@@ -20,13 +20,22 @@ static OWNERS: LazyLock<Mutex<HashMap<String, Option<String>>>> = LazyLock::new(
pub(super) async fn flow_owner(config: &Config, flow_id: &str) -> Option<String> {
// No backend: every record is `local`.
crate::storage::installed()?;
- if let Some(owner) = OWNERS
+ let cached = OWNERS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.get(flow_id)
- .cloned()
- {
- return owner;
+ .cloned();
+ if let Some(owner) = cached {
+ // Still there? A removed flow's id may be reused, or the flow moved:
+ // re-check the cached scope and resolve again when it lost the flow.
+ let still_there = crate::storage::agents::within_agent(owner.as_deref(), async {
+ matches!(crate::flows::store::get_flow(config, flow_id), Ok(Some(_)))
+ })
+ .await;
+ if still_there {
+ return owner;
+ }
+ forget(flow_id);
}
let found = crate::storage::agents::find_owner("flow owner", || async {
matches!(crate::flows::store::get_flow(config, flow_id), Ok(Some(_)))
@@ -47,7 +56,6 @@ pub(super) async fn flow_owner(config: &Config, flow_id: &str) -> Option<String>
}
/// Forgets a cached owner (a removed flow's id may be reused).
-#[cfg_attr(not(test), allow(dead_code))]
pub(super) fn forget(flow_id: &str) {
OWNERS
.lock()
diff --git a/crates/openhuman-core/src/flows/bus/trigger.rs b/crates/openhuman-core/src/flows/bus/trigger.rs
index 595b7191b4..80bacb58ef 100644
--- a/crates/openhuman-core/src/flows/bus/trigger.rs
+++ b/crates/openhuman-core/src/flows/bus/trigger.rs
@@ -111,12 +111,26 @@ impl FlowTriggerSubscriber {
})
}
+ /// The configuration to handle an event under: the acting agent's own
+ /// when the handler runs as one (`super::owner`, `crate::storage::agents`)
+ /// — its provider, access policy and action directory — else the one this
+ /// subscriber was registered with.
+ pub(super) fn config_for_scope(&self) -> Arc<Config> {
+ let acting = crate::core::runtime::CoreContext::current()
+ .is_some_and(|context| context.session_agent().is_some());
+ match acting.then(crate::core::runtime::CoreContext::current_embedder_config) {
+ Some(Some(config)) => Arc::new(config),
+ _ => Arc::clone(&self.config),
+ }
+ }
+
/// `DomainEvent::FlowScheduleTick` — a `flow`-type cron job fired. Loads
/// the one named flow, checks it is still enabled with a `schedule`
/// trigger (it may have been disabled/edited since the job was
/// registered), and dispatches it with an empty trigger payload.
async fn handle_schedule_tick(&self, flow_id: &str) {
- let flow = match store::get_flow(&self.config, flow_id) {
+ let config = self.config_for_scope();
+ let flow = match store::get_flow(&config, flow_id) {
Ok(Some(flow)) => flow,
Ok(None) => {
tracing::debug!(target: "flows", %flow_id, "[flows] schedule tick for unknown/removed flow — ignoring");
@@ -149,7 +163,8 @@ impl FlowTriggerSubscriber {
/// dispatches each match with the event payload as the run input
/// (seeded into `run.trigger`, per the node-catalog contract).
async fn handle_app_event(&self, toolkit: &str, trigger_slug: &str, payload: &Value) {
- let (flows, skipped) = match store::list_enabled_flows(&self.config) {
+ let config = self.config_for_scope();
+ let (flows, skipped) = match store::list_enabled_flows(&config) {
Ok(result) => result,
Err(e) => {
tracing::warn!(target: "flows", %toolkit, %trigger_slug, error = %e, "[flows] failed to list enabled flows for app_event dispatch");
@@ -198,7 +213,7 @@ impl FlowTriggerSubscriber {
return;
};
- let config = self.config.clone();
+ let config = self.config_for_scope();
// Scoped: the run keeps the owner's context the handler entered.
crate::core::runtime::spawn_scoped(async move {
// Held for the lifetime of the run; released on drop (including