The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(flows): skip events when a flow or job owner lookup fails
Steven Enamakeld8b3779e7fd3
diff --git a/crates/openhuman-core/src/desktop/notifications/bus.rs b/crates/openhuman-core/src/desktop/notifications/bus.rsindex c81d7c82c2..0963e54562 100644--- a/crates/openhuman-core/src/desktop/notifications/bus.rs+++ b/crates/openhuman-core/src/desktop/notifications/bus.rs@@ -472,25 +472,35 @@ fn translate(event: &DomainEvent) -> Option<CoreNotificationEvent> {/// The agent whose storage scope a notification raised by `event` belongs/// in: the owner of the cron job a `CronJobCompleted` names, when a storage/// backend is installed and an agent's scope holds the job; `None` (`local`)-/// otherwise.-async fn event_owner(config: &crate::config::Config, event: &DomainEvent) -> Option<String> {+/// otherwise — including a job found in `local` or found nowhere.+///+/// # Errors+///+/// When no scope reported the job and a scope's lookup failed.+async fn event_owner(+ config: &crate::config::Config,+ event: &DomainEvent,+) -> Result<Option<String>, crate::storage::agents::LookupFailed> {let DomainEvent::CronJobCompleted { job_id, .. } = event else {- return None;+ return Ok(None);};// Noted by the scheduler as the job completed — the only record of a// one-shot job, which is deleted before the event is published.if let Some(agent) = crate::cron::completion_owner::take(job_id) {- return Some(agent);+ return Ok(Some(agent));+ }+ if crate::storage::installed().is_none() {+ return Ok(None);}- // No backend: every record is `local`.- crate::storage::installed()?;- // Found in `local` (`Some(None)`) and found nowhere (`None`) both mean- // the notification is stored in `local`.- crate::storage::agents::find_owner("notification owner", || async {- crate::cron::get_job(config, job_id).is_ok()+ let owner = crate::storage::agents::find_owner("notification owner", || async {+ match crate::cron::get_job(config, job_id) {+ Ok(_) => Ok(true),+ Err(error) if error.to_string().contains("not found") => Ok(false),+ Err(error) => Err(error.to_string()),+ }})- .await- .flatten()+ .await?;+ Ok(owner.flatten())}#[async_trait]@@ -518,12 +528,21 @@ impl EventHandler<DomainEvent> for NotificationBridgeSubscriber {// Stored with the record that raised it: a cron job's// completion goes to the agent the job belongs to// (`crate::storage`), `local` otherwise.- let owner = event_owner(&config, event).await;- let stored = crate::storage::agents::within_agent(owner.as_deref(), async {- super::store::insert_core_notification(&config, ¬ification)- })- .await;- match stored {+ let stored = match event_owner(&config, event).await {+ Ok(owner) => {+ crate::storage::agents::within_agent(owner.as_deref(), async {+ super::store::insert_core_notification(&config, ¬ification)+ })+ .await+ }+ Err(failed) => {+ // Unknown owner: not stored rather than stored in the+ // wrong scope. The live broadcast below still goes out.+ log::warn!("{LOG_PREFIX} notification owner unknown ({failed}); not persisted");+ None+ }+ };+ match stored.unwrap_or(Ok(false)) {Ok(true) => log::debug!("{LOG_PREFIX} persisted core notification id={}",notification.iddiff --git a/crates/openhuman-core/src/flows/bus/dedup_commit.rs b/crates/openhuman-core/src/flows/bus/dedup_commit.rsindex 9f9fc1aaee..764788b6a8 100644--- a/crates/openhuman-core/src/flows/bus/dedup_commit.rs+++ b/crates/openhuman-core/src/flows/bus/dedup_commit.rs@@ -326,9 +326,9 @@ impl EventHandler<DomainEvent> for DedupCommitSubscriber {} = event{// Settle the run as the agent its flow belongs to (`super::owner`).- let owner = super::owner::flow_owner(&self.config, flow_id).await;- crate::storage::agents::within_agent(- owner.as_deref(),+ super::owner::as_owner(+ &self.config,+ flow_id,self.handle_finished(flow_id, run_id, status),).await;diff --git a/crates/openhuman-core/src/flows/bus/owner.rs b/crates/openhuman-core/src/flows/bus/owner.rsindex fc99e20694..79d31f76c5 100644--- a/crates/openhuman-core/src/flows/bus/owner.rs+++ b/crates/openhuman-core/src/flows/bus/owner.rs@@ -11,20 +11,38 @@ use std::collections::HashMap;use std::sync::{LazyLock, Mutex};use crate::config::Config;+use crate::storage::agents::{find_owner, within_agent, LookupFailed};/// Flows whose owner this process has resolved (`None` = `local`).static OWNERS: LazyLock<Mutex<HashMap<String, Option<String>>>> = LazyLock::new(Default::default);/// The agent `flow_id` belongs to: `None` for `local`, or when no scope has/// the flow (the handler then reports the flow as unknown, as before).-pub(super) async fn flow_owner(config: &Config, flow_id: &str) -> Option<String> {+///+/// # Errors+///+/// When no scope reported the flow and a scope's lookup failed: the event+/// is then not handled, rather than handled in the wrong scope.+pub(super) async fn flow_owner(+ config: &Config,+ flow_id: &str,+) -> Result<Option<String>, LookupFailed> {// No backend: every record is `local`.- crate::storage::installed()?;+ if crate::storage::installed().is_none() {+ return Ok(None);+ }resolve(config, flow_id).await}+/// Whether the current scope has `flow_id`.+async fn has_flow(config: &Config, flow_id: &str) -> Result<bool, String> {+ crate::flows::store::get_flow(config, flow_id)+ .map(|flow| flow.is_some())+ .map_err(|error| error.to_string())+}+/// [`flow_owner`]'s cache and lookup, across whatever scopes exist.-async fn resolve(config: &Config, flow_id: &str) -> Option<String> {+async fn resolve(config: &Config, flow_id: &str) -> Result<Option<String>, LookupFailed> {let cached = OWNERS.lock().unwrap_or_else(std::sync::PoisonError::into_inner)@@ -33,20 +51,15 @@ async fn resolve(config: &Config, flow_id: &str) -> Option<String> {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;+ match within_agent(owner.as_deref(), has_flow(config, flow_id)).await {+ Some(Ok(true)) => return Ok(owner),+ Some(Err(error)) => return Err(LookupFailed { agent: owner, error }),+ Some(Ok(false)) | None => forget(flow_id),}- forget(flow_id);}- let found = crate::storage::agents::find_owner("flow owner", || async {- matches!(crate::flows::store::get_flow(config, flow_id), Ok(Some(_)))- })- .await;- let owner = found?;+ let Some(owner) = find_owner("flow owner", || has_flow(config, flow_id)).await? else {+ return Ok(None);+ };tracing::debug!(target: "flows",%flow_id,@@ -57,7 +70,26 @@ async fn resolve(config: &Config, flow_id: &str) -> Option<String> {.lock().unwrap_or_else(std::sync::PoisonError::into_inner).insert(flow_id.to_string(), owner.clone());- owner+ Ok(owner)+}++/// Runs `fut` as `flow_id`'s owner; logs and skips it when the owner is+/// unknown (a failed lookup) or no context can act for it.+pub(super) async fn as_owner<F: std::future::Future<Output = ()>>(+ config: &Config,+ flow_id: &str,+ fut: F,+) {+ match flow_owner(config, flow_id).await {+ Ok(owner) => {+ within_agent(owner.as_deref(), fut).await;+ }+ Err(failed) => tracing::warn!(+ target: "flows",+ %flow_id,+ "[flows] flow owner unknown ({failed}); event not handled"+ ),+ }}/// Forgets a cached owner (a removed flow's id may be reused).diff --git a/crates/openhuman-core/src/flows/bus/run_digest.rs b/crates/openhuman-core/src/flows/bus/run_digest.rsindex ff340fc83d..418c78a8d5 100644--- a/crates/openhuman-core/src/flows/bus/run_digest.rs+++ b/crates/openhuman-core/src/flows/bus/run_digest.rs@@ -190,9 +190,9 @@ impl EventHandler<DomainEvent> for FlowRunDigestSubscriber {} = event{// Settle the run as the agent its flow belongs to (`super::owner`).- let owner = super::owner::flow_owner(&self.config, flow_id).await;- crate::storage::agents::within_agent(- owner.as_deref(),+ super::owner::as_owner(+ &self.config,+ flow_id,self.handle_finished(flow_id, run_id, status),).await;diff --git a/crates/openhuman-core/src/flows/bus/trigger.rs b/crates/openhuman-core/src/flows/bus/trigger.rsindex 80bacb58ef..8b4b52ad7c 100644--- a/crates/openhuman-core/src/flows/bus/trigger.rs+++ b/crates/openhuman-core/src/flows/bus/trigger.rs@@ -265,12 +265,8 @@ impl EventHandler<DomainEvent> for FlowTriggerSubscriber {match event {// The flow runs as the agent it belongs to (`super::owner`).DomainEvent::FlowScheduleTick { flow_id } => {- let owner = super::owner::flow_owner(&self.config, flow_id).await;- crate::storage::agents::within_agent(- owner.as_deref(),- self.handle_schedule_tick(flow_id),- )- .await+ super::owner::as_owner(&self.config, flow_id, self.handle_schedule_tick(flow_id))+ .await}// Every scope's flows may listen for this trigger: match them in// each, so each run starts as its flow's owner.