Skip to content

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

refactor(workflow_runs): extract run lifecycle handling into its own module

Steven Enamakelc496394b86b5
diff --git a/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/lifecycle.rs b/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/lifecycle.rs
index a70318cdf5..9205252c4d 100644
--- a/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/lifecycle.rs
+++ b/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/lifecycle.rs
@@ -6,6 +6,7 @@ use serde_json::Value;
use std::sync::Arc;
use tinyagents_orchestration::workflow::{reset_running_phases, WorkflowEngine};
+use crate::agent::harness::ParentExecutionContext;
use crate::agent::orchestration::parent_context::with_root_parent;
use crate::config::Config;
use tinyagents_session::run_ledger::{WorkflowRun, WorkflowRunStatus, WorkflowRunUpsert};
@@ -48,6 +49,7 @@ pub async fn start_workflow_run(
&run_id,
None,
safety_tier,
+ None,
)),
);
let run = initial_engine
@@ -70,7 +72,7 @@ pub async fn start_workflow_run(
Ok(task_config) => {
crate::agent::turn_origin::with_inherited_origin(
inherited_origin,
- run_engine_loop(&task_config, &task_run_id, definition, cancel),
+ run_engine_loop(&task_config, &task_run_id, definition, cancel, None),
)
.await;
}
@@ -209,7 +211,7 @@ pub async fn resume_workflow_run(config: &Config, id: &str) -> Result<WorkflowRu
Ok(task_config) => {
crate::agent::turn_origin::with_inherited_origin(
inherited_origin,
- run_engine_loop(&task_config, &task_run_id, definition, cancel),
+ run_engine_loop(&task_config, &task_run_id, definition, cancel, None),
)
.await;
}
@@ -237,6 +239,7 @@ pub(crate) async fn run_engine_loop(
run_id: &str,
definition: WorkflowDefinition,
cancel: WorkflowCancelSignal,
+ parent_override: Option<ParentExecutionContext>,
) {
let model_override = get_workflow_run(config, run_id)
.ok()
@@ -282,24 +285,31 @@ pub(crate) async fn run_engine_loop(
return;
}
};
- let engine = WorkflowEngine::new(
- store,
- Arc::new(super::super::host::OpenHumanWorkflowExecutor::new(
- run_id,
- model_override,
- safety_tier,
- )),
+ let outcome = with_root_parent(
+ parent_override,
+ config,
+ "workflow_engine",
+ "workflow",
+ "workflow",
+ |parent| async move {
+ let engine = WorkflowEngine::new(
+ store,
+ Arc::new(super::super::host::OpenHumanWorkflowExecutor::new(
+ run_id,
+ model_override,
+ safety_tier,
+ Some(parent),
+ )),
+ )
+ .with_event_sink(Arc::new(tinyagents_graph::GraphTracingSink::new(format!(
+ "workflow:{run_id}"
+ ))));
+ engine
+ .drive(run_id, &definition, cancel.token.clone())
+ .await
+ .map_err(anyhow::Error::msg)
+ },
)
- .with_event_sink(Arc::new(tinyagents_graph::GraphTracingSink::new(format!(
- "workflow:{run_id}"
- ))));
-
- let outcome = with_root_parent(config, "workflow_engine", "workflow", "workflow", async {
- engine
- .drive(run_id, &definition, cancel.token.clone())
- .await
- .map_err(anyhow::Error::msg)
- })
.await
// Flatten: outer Err = root-parent build failure, inner = drive_phases result.
.unwrap_or_else(Err);
diff --git a/crates/openhuman-core/src/agent/orchestration/workflow_runs/host.rs b/crates/openhuman-core/src/agent/orchestration/workflow_runs/host.rs
index 5b45323532..c07af627d1 100644
--- a/crates/openhuman-core/src/agent/orchestration/workflow_runs/host.rs
+++ b/crates/openhuman-core/src/agent/orchestration/workflow_runs/host.rs
@@ -12,6 +12,7 @@ use tinyagents_orchestration::workflow::{
};
use crate::agent::harness::host_definition::AgentDefinitionRegistry;
+use crate::agent::harness::ParentExecutionContext;
use tinyagents_definition::{SandboxMode, ToolScope};
use {
crate::agent::orchestration::AgentOrchestrationSession,
@@ -53,6 +54,10 @@ pub(super) struct OpenHumanWorkflowExecutor {
session: AgentOrchestrationSession,
model_override: Option<String>,
safety_tier: WorkflowSafetyTier,
+ /// The root parent execution context the children are spawned under.
+ /// `None` for an executor that only persists the initial run row and never
+ /// spawns (`start_workflow_run`'s `initialise`).
+ parent: Option<ParentExecutionContext>,
}
impl OpenHumanWorkflowExecutor {
@@ -60,11 +65,13 @@ impl OpenHumanWorkflowExecutor {
run_id: &str,
model_override: Option<String>,
safety_tier: WorkflowSafetyTier,
+ parent: Option<ParentExecutionContext>,
) -> Self {
Self {
session: AgentOrchestrationSession::new(format!("workflow-engine-{run_id}")),
model_override,
safety_tier,
+ parent,
}
}
@@ -115,14 +122,20 @@ impl WorkflowExecutor for OpenHumanWorkflowExecutor {
"workflow cancelled before child spawn".to_owned(),
));
}
+ let parent = self.parent.clone().ok_or_else(|| {
+ OrchestrationError("workflow child spawn requires a parent execution context".to_owned())
+ })?;
let response = self
.session
- .spawn_agent(SpawnAgentRequest {
- agent_id: request.agent_id.clone(),
- prompt: request.prompt,
- model: self.model_override.clone(),
- ..Default::default()
- })
+ .spawn_agent(
+ parent,
+ SpawnAgentRequest {
+ agent_id: request.agent_id.clone(),
+ prompt: request.prompt,
+ model: self.model_override.clone(),
+ ..Default::default()
+ },
+ )
.await
.map_err(|error| OrchestrationError(error.to_string()))?;