Skip to content

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

feat(flows): deduplicate bus commits and track run digests

Steven Enamakel21365daaff28
diff --git a/crates/openhuman-core/src/flows/bus/dedup_commit.rs b/crates/openhuman-core/src/flows/bus/dedup_commit.rs
index 7f64f6d32d..0884075f1d 100644
--- a/crates/openhuman-core/src/flows/bus/dedup_commit.rs
+++ b/crates/openhuman-core/src/flows/bus/dedup_commit.rs
@@ -228,9 +228,14 @@ impl DedupCommitSubscriber {
}
}
- async fn handle_finished(&self, flow_id: &str, run_id: &str, status: &str) {
+ async fn handle_finished(
+ &self,
+ ctx: crate::core::runtime::DispatchCtx,
+ flow_id: &str,
+ run_id: &str,
+ status: &str,
+ ) {
// As the flow's owner: its own configuration (`super::owner`).
- let ctx = crate::core::runtime::DispatchCtx::ambient();
let config = super::owner::config_for_scope(&ctx, &self.config);
let node_ids = self.dedup_node_ids(&ctx, flow_id);
if node_ids.is_empty() {
@@ -336,11 +341,9 @@ impl EventHandler<DomainEvent> for DedupCommitSubscriber {
} = event
{
// Settle the run as the agent its flow belongs to (`super::owner`).
- super::owner::as_owner(
- &self.config,
- flow_id,
- self.handle_finished(flow_id, run_id, status),
- )
+ super::owner::as_owner(&self.config, flow_id, |ctx| {
+ self.handle_finished(ctx, 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.rs
index fb661d59a7..5438e79e46 100644
--- a/crates/openhuman-core/src/flows/bus/owner.rs
+++ b/crates/openhuman-core/src/flows/bus/owner.rs
@@ -11,7 +11,8 @@ use std::collections::HashMap;
use std::sync::{LazyLock, Mutex};
use crate::config::Config;
-use crate::storage::agents::{find_owner, within_agent, LookupFailed};
+use crate::core::runtime::DispatchCtx;
+use crate::storage::agents::{find_owner, within_agent, within_agent_ctx, LookupFailed};
/// Flows whose owner this process has resolved (`None` = `local`).
static OWNERS: LazyLock<Mutex<HashMap<String, Option<String>>>> = LazyLock::new(Default::default);
@@ -78,16 +79,16 @@ async fn resolve(config: &Config, flow_id: &str) -> Result<Option<String>, Looku
Ok(owner)
}
-/// Runs `fut` as `flow_id`'s owner; logs and skips it when the owner is
+/// Runs `step`, handed the owner's context, 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,
-) {
+pub(super) async fn as_owner<F, Fut>(config: &Config, flow_id: &str, step: F)
+where
+ F: FnOnce(DispatchCtx) -> Fut,
+ Fut: std::future::Future<Output = ()>,
+{
match flow_owner(config, flow_id).await {
Ok(owner) => {
- within_agent(owner.as_deref(), fut).await;
+ within_agent_ctx(owner.as_deref(), step).await;
}
Err(failed) => tracing::warn!(
target: "flows",
diff --git a/crates/openhuman-core/src/flows/bus/run_digest.rs b/crates/openhuman-core/src/flows/bus/run_digest.rs
index d5697b4f6c..eec57ef230 100644
--- a/crates/openhuman-core/src/flows/bus/run_digest.rs
+++ b/crates/openhuman-core/src/flows/bus/run_digest.rs
@@ -59,12 +59,15 @@ impl FlowRunDigestSubscriber {
Self { config }
}
- async fn handle_finished(&self, flow_id: &str, run_id: &str, status: &str) {
+ async fn handle_finished(
+ &self,
+ ctx: crate::core::runtime::DispatchCtx,
+ flow_id: &str,
+ run_id: &str,
+ status: &str,
+ ) {
// As the flow's owner: its own configuration (`super::owner`).
- let config = super::owner::config_for_scope(
- &crate::core::runtime::DispatchCtx::ambient(),
- &self.config,
- );
+ let config = super::owner::config_for_scope(&ctx, &self.config);
if status != "completed" && status != "completed_with_warnings" {
tracing::trace!(target: "flows", %flow_id, %run_id, %status, "[flows] digest: ignoring non-success terminal status");
return;
@@ -194,11 +197,9 @@ impl EventHandler<DomainEvent> for FlowRunDigestSubscriber {
} = event
{
// Settle the run as the agent its flow belongs to (`super::owner`).
- super::owner::as_owner(
- &self.config,
- flow_id,
- self.handle_finished(flow_id, run_id, status),
- )
+ super::owner::as_owner(&self.config, flow_id, |ctx| {
+ self.handle_finished(ctx, flow_id, run_id, status)
+ })
.await;
}
}
diff --git a/crates/openhuman-core/src/storage/agents.rs b/crates/openhuman-core/src/storage/agents.rs
index 014d9690a8..0247ca6ef7 100644
--- a/crates/openhuman-core/src/storage/agents.rs
+++ b/crates/openhuman-core/src/storage/agents.rs
@@ -197,11 +197,27 @@ async fn in_local<F: Future>(fut: F) -> F::Output {
/// context can act for it (SaaS with the agent gone, or no booted core):
/// running it anywhere else would act outside the agent's scope.
pub async fn within_agent<F: Future>(agent: Option<&str>, fut: F) -> Option<F::Output> {
+ within_agent_ctx(agent, |_| fut).await
+}
+
+/// [`within_agent`] for work that takes the context it acts under: `step` is
+/// handed the dispatch context of the agent's scope (the host's own for
+/// `None`, which in SaaS is unbound: work that lost its tenant never acts as
+/// the operator).
+pub async fn within_agent_ctx<F, Fut>(agent: Option<&str>, step: F) -> Option<Fut::Output>
+where
+ F: FnOnce(DispatchCtx) -> Fut,
+ Fut: Future,
+{
let Some(agent) = agent else {
- return Some(in_local(fut).await);
+ let local = local_context().unwrap_or_else(DispatchCtx::host_default);
+ return Some(in_local(step(local)).await);
};
match context_for(agent) {
- Some(context) => Some(CoreContext::scope(context, fut).await),
+ Some(context) => {
+ let dispatch = DispatchCtx::new(Arc::clone(&context));
+ Some(CoreContext::scope(context, step(dispatch)).await)
+ }
None => {
tracing::warn!(%agent, "[storage::agents] no context can act for the agent; skipped");
None