Skip to content

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

fix(flows): sweep and reconcile runs across all agent storage scopes

Steven Enamakeld7d7d37e0388
diff --git a/crates/openhuman-core/src/flows/ops/run_management.rs b/crates/openhuman-core/src/flows/ops/run_management.rs
index 10be3a787c..684da8c634 100644
--- a/crates/openhuman-core/src/flows/ops/run_management.rs
+++ b/crates/openhuman-core/src/flows/ops/run_management.rs
@@ -270,6 +270,19 @@ pub async fn sweep_expired_parked_runs(config: &Config) -> usize {
/// resumable — only `pending_approval` is). Best-effort by construction: a store
/// error is logged and the sweep returns what it managed.
pub async fn sweep_orphaned_running_runs_on_boot(config: &Config) -> usize {
+ // `local`, then every agent that keeps its runs in its own storage scope
+ // (`crate::storage::agents`).
+ crate::storage::agents::for_each_scope("flows boot sweep", || {
+ sweep_orphaned_running_runs_in_scope(config)
+ })
+ .await
+ .into_iter()
+ .map(|(_, swept)| swept)
+ .sum()
+}
+
+/// [`sweep_orphaned_running_runs_on_boot`] for the current storage scope.
+async fn sweep_orphaned_running_runs_in_scope(config: &Config) -> usize {
let now_str = Utc::now().to_rfc3339();
const REASON: &str =
"Run interrupted by an app restart — no live run was executing this row after boot.";
diff --git a/crates/openhuman-core/src/flows/ops/triggers.rs b/crates/openhuman-core/src/flows/ops/triggers.rs
index 93a1212c77..c7b48941c8 100644
--- a/crates/openhuman-core/src/flows/ops/triggers.rs
+++ b/crates/openhuman-core/src/flows/ops/triggers.rs
@@ -244,6 +244,28 @@ fn log_webhook_trigger_deferred(flow: &Flow, enabled: bool) {
/// was lost some other way) gets its schedule re-registered on the next
/// boot without the user having to toggle it off and on.
pub async fn reconcile_schedule_triggers_on_boot(config: &Config) -> Result<(), String> {
+ // `local`, then every agent that keeps its flows in its own storage scope
+ // (`crate::storage::agents`); each scope's flows get their cron jobs in
+ // that scope, where the scheduler visits them.
+ let mut errors = Vec::new();
+ for (agent, result) in crate::storage::agents::for_each_scope("flows schedule reconcile", || {
+ reconcile_schedule_triggers_in_scope(config)
+ })
+ .await
+ {
+ if let Err(error) = result {
+ errors.push(format!("{}: {error}", agent.as_deref().unwrap_or("local")));
+ }
+ }
+ if errors.is_empty() {
+ Ok(())
+ } else {
+ Err(errors.join("; "))
+ }
+}
+
+/// [`reconcile_schedule_triggers_on_boot`] for the current storage scope.
+async fn reconcile_schedule_triggers_in_scope(config: &Config) -> Result<(), String> {
let (flows, skipped) = store::list_enabled_flows(config).map_err(|e| e.to_string())?;
if skipped > 0 {
// R-M4: a corrupt/unmigratable row must not abort boot reconciliation