Skip to content

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

refactor(flows): thread dispatch context through flow execution

Steven Enamakelcb9c8a840064
diff --git a/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs b/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
index f3b137b5c8..7b25b886de 100644
--- a/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
+++ b/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
@@ -184,6 +184,21 @@ impl DispatchCtx {
self.turn_origin()
.or_else(|| Self::ambient_turn().turn_origin())
}
+ /// This context with `origin` bound as its turn origin, for a run that
+ /// acts under a trusted entry point of its own (a saved flow). The base is
+ /// this context, else the calling task's turn scope, the process default,
+ /// or a bare one, the same resolution `agent::turn_origin::with_origin`
+ /// scopes under. This context is not mutated.
+ pub fn with_turn_origin(&self, origin: crate::agent::turn_origin::AgentTurnOrigin) -> Self {
+ let base = self
+ .core
+ .clone()
+ .or_else(CoreContext::scoped_for_turn)
+ .or_else(CoreContext::default_context)
+ .unwrap_or_else(CoreContext::bare);
+ Self::new(base.derive_channel_turn(None, Some(origin)))
+ }
+
/// A non-owning handle on the bound context, for state that outlives the
/// call (a session, a built turn). The tenant lease is the strong count of
/// the context ([`CoreContext::tenant_in_use`]), so a long-lived holder
diff --git a/crates/openhuman-core/src/flows/ops/execution.rs b/crates/openhuman-core/src/flows/ops/execution.rs
index 37f4b79bab..1d73c60368 100644
--- a/crates/openhuman-core/src/flows/ops/execution.rs
+++ b/crates/openhuman-core/src/flows/ops/execution.rs
@@ -235,8 +235,12 @@ async fn run_flow_body_inner(
flow_id: flow_id.to_string(),
run_id: thread_id.clone(),
};
+ // The run acts under its own trusted `Workflow` origin: its capabilities
+ // read the origin from this context, not from the calling task's scope.
+ let origin = workflow_origin(flow_id, flow.require_approval);
+ let run_ctx = ctx.with_turn_origin(origin.clone());
let caps = crate::flows::tinyflows::build_capabilities(
- &ctx,
+ &run_ctx,
config_arc.clone(),
format!("flow:{flow_id}"),
Some(flow_run),
@@ -288,7 +292,6 @@ async fn run_flow_body_inner(
);
};
- let origin = workflow_origin(flow_id, flow.require_approval);
// Per-run in-memory journal: tinyflows records every graph event as a
// durable GraphObservation under the run's tinyagents run id, which the
// post-run Langfuse export reads back. Process-local and dropped with the
diff --git a/crates/openhuman-core/src/flows/ops/resume.rs b/crates/openhuman-core/src/flows/ops/resume.rs
index 4d5a5cf5df..f7abc42ee3 100644
--- a/crates/openhuman-core/src/flows/ops/resume.rs
+++ b/crates/openhuman-core/src/flows/ops/resume.rs
@@ -209,8 +209,12 @@ pub async fn flows_resume(
}
let compiled = tinyflows::compiler::compile(&flow.graph).map_err(|e| e.to_string())?;
let config_arc = Arc::new(config.clone());
+ // The run acts under its own trusted `Workflow` origin: its capabilities
+ // read the origin from this context, not from the calling task's scope.
+ let origin = workflow_origin(flow_id, flow.require_approval);
+ let run_ctx = ctx.with_turn_origin(origin.clone());
let caps = crate::flows::tinyflows::build_capabilities(
- ctx,
+ &run_ctx,
config_arc.clone(),
format!("flow:{flow_id}"),
Some(FlowRunContext {
@@ -281,7 +285,6 @@ pub async fn flows_resume(
"[flows] flows_resume: resuming checkpointed run"
);
- let origin = workflow_origin(flow_id, flow.require_approval);
// Same per-run journal as `flows_run`: the resumed execution mints a new
// tinyagents run id, so its observation slice is read under that id.
let journal = Arc::new(tinyflows::engine::InMemoryGraphEventJournal::new());
diff --git a/crates/openhuman-core/src/flows/tinyflows/memory_adapter_tests.rs b/crates/openhuman-core/src/flows/tinyflows/memory_adapter_tests.rs
index 7c7442f071..3e23d05950 100644
--- a/crates/openhuman-core/src/flows/tinyflows/memory_adapter_tests.rs
+++ b/crates/openhuman-core/src/flows/tinyflows/memory_adapter_tests.rs
@@ -29,6 +29,7 @@ fn adapter(autonomy: AutonomyLevel) -> (TempDir, OpenHumanMemory) {
(
tmp,
OpenHumanMemory {
+ ctx: crate::core::runtime::DispatchCtx::unbound(),
config,
security: security(autonomy),
approval: Default::default(),