Skip to content

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

fix(flows): dedupe repeated commits and harden memory tool calls

Steven Enamakel87bd9c776583
diff --git a/crates/openhuman-core/src/flows/bus/dedup_commit.rs b/crates/openhuman-core/src/flows/bus/dedup_commit.rs
index fb127b6a5e..b0b9ffe7e9 100644
--- a/crates/openhuman-core/src/flows/bus/dedup_commit.rs
+++ b/crates/openhuman-core/src/flows/bus/dedup_commit.rs
@@ -201,12 +201,13 @@ impl DedupCommitSubscriber {
/// having this method read that snapshot instead of `store::get_flow` —
/// a schema + call-site change bigger than this PR's scope; reported as a
/// follow-up rather than attempted here.
- fn dedup_node_ids(&self, flow_id: &str) -> Vec<String> {
+ fn dedup_node_ids(
+ &self,
+ ctx: &crate::core::runtime::DispatchCtx,
+ flow_id: &str,
+ ) -> Vec<String> {
match store::get_flow(
- &super::owner::config_for_scope(
- &crate::core::runtime::DispatchCtx::ambient(),
- &self.config,
- ),
+ &super::owner::config_for_scope(ctx, &self.config),
flow_id,
) {
Ok(Some(flow)) => flow
@@ -229,11 +230,9 @@ impl DedupCommitSubscriber {
async fn handle_finished(&self, 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 node_ids = self.dedup_node_ids(flow_id);
+ 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() {
tracing::trace!(target: "flows", %flow_id, %run_id, %status, "[dedup-commit] no dedup nodes in this flow — nothing to settle");
return;
diff --git a/crates/openhuman-core/src/flows/memory_tools.rs b/crates/openhuman-core/src/flows/memory_tools.rs
index 8ffd79e339..aa3f10bf7e 100644
--- a/crates/openhuman-core/src/flows/memory_tools.rs
+++ b/crates/openhuman-core/src/flows/memory_tools.rs
@@ -348,7 +348,8 @@ impl Tool for FlowMemoryRecallTool {
.and_then(serde_json::Value::as_u64)
.map_or(5, |v| v as usize);
- let config = match DispatchCtx::ambient().load_config().await {
+ let config = match crate::agent::tinyagents::host::run_context::dispatch_ctx_for_tool(tool_context)
+ .load_config().await {
Ok(config) => config,
Err(error) => {
return Ok(ToolResult::error(format!(
@@ -497,7 +498,8 @@ impl Tool for FlowMemoryRememberTool {
));
}
- let config = match DispatchCtx::ambient().load_config().await {
+ let config = match crate::agent::tinyagents::host::run_context::dispatch_ctx_for_tool(tool_context)
+ .load_config().await {
Ok(config) => config,
Err(error) => {
return Ok(ToolResult::error(format!(
diff --git a/crates/openhuman-core/src/flows/tinyflows/caps/agent.rs b/crates/openhuman-core/src/flows/tinyflows/caps/agent.rs
index 09897eaa51..b38ca55cec 100644
--- a/crates/openhuman-core/src/flows/tinyflows/caps/agent.rs
+++ b/crates/openhuman-core/src/flows/tinyflows/caps/agent.rs
@@ -175,6 +175,8 @@ impl AgentRunner for OpenHumanAgentRunner {
request: Value,
conn: Option<&str>,
) -> Result<Value> {
+ // Entry point of a flow agent node: bind the calling task's context once.
+ let dispatch = crate::core::runtime::DispatchCtx::ambient();
// The harness definition registry must be initialised before we can
// build a named agent. Idempotent: a booted core already did this at
// startup; a bare flow run (tests, standalone) has not. A failure here
@@ -203,7 +205,7 @@ impl AgentRunner for OpenHumanAgentRunner {
"[flows] agent_runner: HOST AGENT path — running the host-registered agent \
with its own tools in its own context"
);
- self.run_via_harness(agent_ref, request, conn, None, host)
+ self.run_via_harness(&dispatch, agent_ref, request, conn, None, host)
.await
}
AgentRoute::Harness => {
@@ -215,7 +217,7 @@ impl AgentRunner for OpenHumanAgentRunner {
// A shipped/TOML harness definition has no `entry.model` — the
// definition's own `ModelSpec` (already applied by the session
// builder) is the only model pin in play here.
- self.run_via_harness(agent_ref, request, conn, None, None)
+ self.run_via_harness(&dispatch, agent_ref, request, conn, None, None)
.await
}
AgentRoute::RegistryFallback => {
@@ -256,7 +258,14 @@ impl AgentRunner for OpenHumanAgentRunner {
// comment on this PR: this previously regressed to the
// default chat model for a custom flow agent with no
// per-node override).
- self.run_via_harness(agent_ref, request, conn, entry_model.as_deref(), None)
+ self.run_via_harness(
+ &dispatch,
+ agent_ref,
+ request,
+ conn,
+ entry_model.as_deref(),
+ None,
+ )
.await
}
AgentRoute::RegistryFallback => {
@@ -266,7 +275,7 @@ impl AgentRunner for OpenHumanAgentRunner {
"[flows] agent_runner: FALLBACK path — persona-shaping single \
completion for a custom registry entry"
);
- self.run_via_registry_fallback(agent_ref, request, conn)
+ self.run_via_registry_fallback(&dispatch, agent_ref, request, conn)
.await
}
}
@@ -322,6 +331,7 @@ impl OpenHumanAgentRunner {
/// reaching for a background sub-agent from inside a single node.
async fn run_via_harness(
&self,
+ dispatch: &crate::core::runtime::DispatchCtx,
agent_ref: &str,
request: Value,
conn: Option<&str>,
@@ -376,7 +386,7 @@ impl OpenHumanAgentRunner {
let built = match &host {
Some(host) => host.session_host(effective.as_ref(), None),
None => OpenHumanSessionHost::from_config_for_agent(
- &crate::core::runtime::DispatchCtx::ambient(),
+ dispatch,
effective.as_ref(),
agent_ref,
),
@@ -510,13 +520,14 @@ impl OpenHumanAgentRunner {
/// agents don't regress.
async fn run_via_registry_fallback(
&self,
+ dispatch: &crate::core::runtime::DispatchCtx,
agent_ref: &str,
request: Value,
conn: Option<&str>,
) -> Result<Value> {
// Resolve + validate the requested agent kind against the registry.
let entry = crate::agent::registry::get_agent(
- &crate::core::runtime::DispatchCtx::ambient(),
+ dispatch,
agent_ref,
)
.await
diff --git a/crates/openhuman-core/src/integrations/composio/action_tool.rs b/crates/openhuman-core/src/integrations/composio/action_tool.rs
index aa0a3a39d4..a76437bbee 100644
--- a/crates/openhuman-core/src/integrations/composio/action_tool.rs
+++ b/crates/openhuman-core/src/integrations/composio/action_tool.rs
@@ -251,7 +251,7 @@ impl Tool for ComposioActionTool {
context: Option<&dyn ToolRunContext>,
) -> anyhow::Result<ToolResult> {
let (config, outcome) = Box::pin(self.execute_unredacted(
- &DispatchCtx::ambient(),
+ &dispatch_ctx_for_tool(context),
args,
sandbox_mode_for_tool(context),
))
diff --git a/crates/openhuman-core/src/integrations/composio/bus/trigger_subscriber.rs b/crates/openhuman-core/src/integrations/composio/bus/trigger_subscriber.rs
index d7b725369f..3fe678a010 100644
--- a/crates/openhuman-core/src/integrations/composio/bus/trigger_subscriber.rs
+++ b/crates/openhuman-core/src/integrations/composio/bus/trigger_subscriber.rs
@@ -83,7 +83,8 @@ impl EventHandler<DomainEvent> for ComposioTriggerSubscriber {
// let the event through rather than silently dropping it. The
// existing env-var / config triage flags below remain the
// backend-mode gates.
- if let Ok(config) = DispatchCtx::ambient().load_config().await {
+ let ctx = DispatchCtx::ambient();
+ if let Ok(config) = ctx.load_config().await {
if config.composio.mode == crate::config::schema::COMPOSIO_MODE_DIRECT {
tracing::info!(
toolkit = %toolkit,
@@ -95,7 +96,7 @@ impl EventHandler<DomainEvent> for ComposioTriggerSubscriber {
}
if let Some(store) = trigger_history::global() {
- match DispatchCtx::ambient().load_config().await {
+ match ctx.load_config().await {
Ok(config) => {
let event = super::super::types::ComposioTriggerEvent {
toolkit: toolkit.clone(),
@@ -130,7 +131,7 @@ impl EventHandler<DomainEvent> for ComposioTriggerSubscriber {
// remains a global emergency kill-switch that works even when the
// config file is corrupt. Fail-open on load error: if we can't read
// the config we let triage run rather than silently drop events.
- match DispatchCtx::ambient().load_config().await {
+ match ctx.load_config().await {
Ok(config) => {
if config.composio.triage_disabled {
tracing::debug!(
@@ -185,7 +186,9 @@ impl EventHandler<DomainEvent> for ComposioTriggerSubscriber {
// Spawn so the bus dispatch loop stays non-blocking — the
// triage turn is an LLM round-trip that may take seconds.
tokio::spawn(async move {
- match run_triage(&crate::core::runtime::DispatchCtx::ambient(), &envelope).await {
+ // A spawned task does not inherit the handler's task scope: bind its own.
+ let ctx = DispatchCtx::ambient();
+ match run_triage(&ctx, &envelope).await {
Ok(TriageOutcome::Decision(run)) => {
// Remote payload: a Composio trigger body is
// attacker-influenceable, so the dispatch parks rather than
@@ -195,11 +198,7 @@ impl EventHandler<DomainEvent> for ComposioTriggerSubscriber {
let origin = remote_trigger_origin(&envelope);
if let Err(e) = with_origin(
origin,
- apply_decision(
- &crate::core::runtime::DispatchCtx::ambient(),
- run,
- &envelope,
- ),
+ apply_decision(&ctx, run, &envelope),
)
.await
{