Skip to content

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

refactor(workflow_runs): remove start gate from workflow drive scheduling

Steven Enamakel2c17cadd0a46
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 2d887f7f76..338a85f9ff 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
@@ -73,7 +73,7 @@ fn schedule_workflow_drive(
run_id: String,
definition: WorkflowDefinition,
resume: bool,
-) -> (WorkflowRunHandle, tokio::sync::oneshot::Sender<()>) {
+) -> WorkflowRunHandle {
let supervisor = workflow_supervisor().clone();
let task_ctx = ctx.clone();
let handle_slot = Arc::new(OnceLock::<WorkflowRunHandle>::new());
@@ -160,22 +160,16 @@ fn schedule_workflow_drive(
};
let spawn_ctx = task_ctx;
let spawn_handle_slot = Arc::clone(&handle_slot);
- let (start_tx, start_rx) = tokio::sync::oneshot::channel();
let spawn = move |handle, future| {
let _ = spawn_handle_slot.set(handle);
- spawn_ctx.spawn(async move {
- if start_rx.await.is_ok() {
- future.await;
- }
- });
+ spawn_ctx.spawn(future);
};
- let handle = if resume {
+ if resume {
supervisor.resume_scoped_with_handle(run_id, make_future, root_parent, origin, spawn)
} else {
supervisor.start_scoped_with_handle(run_id, make_future, root_parent, origin, spawn)
- };
- (handle, start_tx)
+ }
}
/// Start a new workflow run and return immediately.
@@ -214,20 +208,7 @@ pub async fn start_workflow_run(
.initialise(run_id.clone(), &definition, input.clone(), parent_thread_id)
.context("persist initial workflow run")?;
- // Install the generation before scheduling can race with a stop. The drive
- // remains gated until the durable row is still Running and this exact
- // generation is current.
- let (handle, start_gate) = schedule_workflow_drive(ctx, run_id.clone(), definition, false);
- let current = get_workflow_run(config, &run_id)?;
- if !matches!(current, Some(ref current) if current.status == WorkflowRunStatus::Running)
- || !workflow_supervisor().is_current(&handle)
- {
- let _ = workflow_supervisor().stop_generation(&handle);
- return Err(anyhow!(
- "workflow run {run_id} was interrupted before its drive started"
- ));
- }
- let _ = start_gate.send(());
+ let _handle = schedule_workflow_drive(ctx, run_id.clone(), definition, false);
log::debug!(
target: LOG_TARGET,
@@ -325,10 +306,6 @@ pub async fn resume_workflow_run(
.ok_or_else(|| anyhow!("definition {} no longer exists", run.definition_id))?;
let _safety_tier = super::host::admit_workflow(&definition)?;
- // Register the successor before the durable transition so the prior
- // generation cannot project a stale failure into the resumed row.
- let (handle, start_gate) =
- schedule_workflow_drive(ctx, id.to_string(), definition.clone(), true);
let resumed = compare_and_swap_workflow_run_lifecycle(
config,
WorkflowRunUpsert {
@@ -345,49 +322,12 @@ pub async fn resume_workflow_run(
},
run.revision,
)
- .context("persist workflow run resume");
- let resumed = match resumed {
- Ok(Some(resumed)) => resumed,
- Ok(None) => {
- // Another resume may have committed the same Running transition
- // after this generation replaced it. In that case this is the
- // only current generation and can drive the winner's row. A stop
- // or any other transition leaves a non-Running row and cancels
- // this exact generation instead.
- let current = get_workflow_run(config, id)?;
- if workflow_supervisor().is_current(&handle)
- && current
- .as_ref()
- .is_some_and(|current| current.status == WorkflowRunStatus::Running)
- {
- let _ = start_gate.send(());
- return Ok(current.expect("running row was checked above"));
- }
- let _ = workflow_supervisor().stop_generation(&handle);
- return Err(anyhow!(
- "workflow run {id} changed while resume was being applied; reload and retry"
- ));
- }
- Err(error) => {
- let _ = workflow_supervisor().stop_generation(&handle);
- return Err(error);
- }
- };
- let current = match get_workflow_run(config, id) {
- Ok(current) => current,
- Err(error) => {
- let _ = workflow_supervisor().stop_generation(&handle);
- return Err(error);
- }
- };
- let still_running = current.is_some_and(|current| current.status == WorkflowRunStatus::Running);
- if !still_running || !workflow_supervisor().is_current(&handle) {
- let _ = workflow_supervisor().stop_generation(&handle);
- return Err(anyhow!(
- "workflow run {id} changed before its drive started"
- ));
- }
- let _ = start_gate.send(());
+ .context("persist workflow run resume")?;
+ let resumed = resumed.ok_or_else(|| {
+ anyhow!("workflow run {id} changed while resume was being applied; reload and retry")
+ })?;
+
+ let _handle = schedule_workflow_drive(ctx, id.to_string(), definition, true);
log::debug!(target: LOG_TARGET, "[workflow_run_engine] resume.spawned run={id}");
Ok(resumed)