Skip to content

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

chore: files changed crates/openhuman-core/src/flows/ops/resume.rs,crates/openhuman-core/src/flows/o

Steven Enamakela65164ebc258
diff --git a/crates/openhuman-core/src/flows/ops/resume.rs b/crates/openhuman-core/src/flows/ops/resume.rs
index f7abc42ee3..ca59833f34 100644
--- a/crates/openhuman-core/src/flows/ops/resume.rs
+++ b/crates/openhuman-core/src/flows/ops/resume.rs
@@ -125,32 +125,21 @@ pub async fn flows_resume(
//
// A is refused either way: its own view of the graph is stale,
// so it must never proceed regardless of who owns the row.
- let observed = current_persisted_steps(config, thread_id);
- let settled_by_us = finish_flow_run_row(
+ let settled_by_us = super::run_management::settle_parked_run(
config,
thread_id,
flow_id,
"cancelled",
- &observed,
- &[],
- Some(GRAPH_CHANGED_SINCE_PARK_ERROR),
- None,
- );
+ GRAPH_CHANGED_SINCE_PARK_ERROR,
+ )
+ .await;
if settled_by_us {
- if let Err(e) = store::record_run(config, flow_id, "cancelled") {
- tracing::warn!(
- target: "flows",
- flow_id = %flow_id,
- %thread_id,
- error = %e,
- "[flows] flows_resume: failed to record run summary (stale-approval refusal)"
- );
- }
- // The checkpoint is for a graph that no longer exists as
- // approved; drop it rather than leave it resumable against
- // a future graph edit that happens to hash back to the
- // same value.
- drop_checkpoint(config, thread_id).await;
+ tracing::debug!(
+ target: "flows",
+ flow_id = %flow_id,
+ %thread_id,
+ "[flows] flows_resume: stale-approval refusal settled the parked run"
+ );
} else {
tracing::info!(
target: "flows",
@@ -178,26 +167,22 @@ pub async fn flows_resume(
// A pending checkpoint may have been created before this compatibility
// gate shipped, so resume is an independent authoritative boundary.
if let Err(error) = ensure_config_aware_engine_compatible(config, &flow.graph) {
- if let Err(rec_err) = store::record_run(config, flow_id, "failed") {
- tracing::warn!(
+ let settled_by_us = super::run_management::settle_parked_run(
+ config,
+ thread_id,
+ flow_id,
+ "failed",
+ &error,
+ )
+ .await;
+ if !settled_by_us {
+ tracing::info!(
target: "flows",
flow_id = %flow_id,
%thread_id,
- error = %rec_err,
- "[flows] flows_resume: failed to record compatibility rejection"
+ "[flows] flows_resume: compatibility refusal lost the parked row to another lifecycle operation"
);
}
- let observed = current_persisted_steps(config, thread_id);
- finish_flow_run_row(
- config,
- thread_id,
- flow_id,
- "failed",
- &observed,
- &[],
- Some(&error),
- None,
- );
tracing::warn!(
target: "flows",
flow_id = %flow_id,
diff --git a/crates/openhuman-core/src/flows/ops/run_management.rs b/crates/openhuman-core/src/flows/ops/run_management.rs
index 6f0a17b2f6..5176f48e14 100644
--- a/crates/openhuman-core/src/flows/ops/run_management.rs
+++ b/crates/openhuman-core/src/flows/ops/run_management.rs
@@ -62,17 +62,34 @@ pub async fn flows_cancel_run(config: &Config, run_id: &str) -> Result<Outcome<V
// write FIRST and treat it as the authority: it only matches a still-live
// row, so `false` means the run settled underneath us. Only once it has
// won do we record the flow summary and drop the checkpoint.
- let observed = current_persisted_steps(config, run_id);
- let settled_by_us = finish_flow_run_row(
- config,
- run_id,
- &run.flow_id,
- "cancelled",
- &observed,
- &[],
- Some("run cancelled"),
- None,
- );
+ let settled_by_us = if run.status == "pending_approval" {
+ // Use the status observed at entry to choose the atomic predicate.
+ // A resume may claim this parked row after the initial read and after
+ // `cancel` found no registry entry; the parked-only CAS then loses
+ // cleanly instead of tearing down the starting resume.
+ settle_parked_run(config, run_id, &run.flow_id, "cancelled", "run cancelled").await
+ } else {
+ // A stale `running` row cannot be newly claimed by ResumeRun, so the
+ // general live-row settle remains available for explicit cleanup.
+ let observed = current_persisted_steps(config, run_id);
+ let settled = finish_flow_run_row(
+ config,
+ run_id,
+ &run.flow_id,
+ "cancelled",
+ &observed,
+ &[],
+ Some("run cancelled"),
+ None,
+ );
+ if settled {
+ if let Err(e) = store::record_run(config, &run.flow_id, "cancelled") {
+ tracing::warn!(target: "flows", run_id, flow_id = %run.flow_id, error = %e, "[flows] flows_cancel_run: failed to record cancelled status on flow summary");
+ }
+ drop_checkpoint(config, run_id).await;
+ }
+ settled
+ };
if !settled_by_us {
tracing::info!(
target: "flows",
@@ -83,17 +100,49 @@ pub async fn flows_cancel_run(config: &Config, run_id: &str) -> Result<Outcome<V
);
return Err(rm::settled_concurrently_error(run_id));
}
- if let Err(e) = store::record_run(config, &run.flow_id, "cancelled") {
- tracing::warn!(target: "flows", run_id, flow_id = %run.flow_id, error = %e, "[flows] flows_cancel_run: failed to record cancelled status on flow summary");
- }
- drop_checkpoint(config, run_id).await;
-
Ok(Outcome::single_log(
json!({ "run_id": run_id, "cancelled": true, "was_in_flight": false }),
format!("flow run {run_id} cancelled"),
))
}
+/// Atomically settles a run that was observed parked for approval, then
+/// updates the flow summary and removes its checkpoint only if that write
+/// wins. The status check in the driver CAS protects a resume that claims the
+/// row after the caller's initial read.
+pub(super) async fn settle_parked_run(
+ config: &Config,
+ run_id: &str,
+ flow_id: &str,
+ status: &str,
+ error: &str,
+) -> bool {
+ let observed = current_persisted_steps(config, run_id);
+ if !finish_parked_flow_run_row(
+ config,
+ run_id,
+ flow_id,
+ status,
+ &observed,
+ &[],
+ Some(error),
+ None,
+ ) {
+ return false;
+ }
+ if let Err(e) = store::record_run(config, flow_id, status) {
+ tracing::warn!(
+ target: "flows",
+ run_id,
+ flow_id,
+ error = %e,
+ "[flows] failed to record settled parked run status on flow summary"
+ );
+ }
+ drop_checkpoint(config, run_id).await;
+ true
+}
+
/// Best-effort drop of a run's durable tinyagents checkpoint thread, so a
/// cancelled (or expired) run can never be resumed from its persisted interrupt
/// boundary. Logged, never fatal — the `flow_runs` row's terminal status is the
diff --git a/crates/openhuman-core/src/flows/ops/run_rows.rs b/crates/openhuman-core/src/flows/ops/run_rows.rs
index 0b7e6bdde2..02a5020a2c 100644
--- a/crates/openhuman-core/src/flows/ops/run_rows.rs
+++ b/crates/openhuman-core/src/flows/ops/run_rows.rs
@@ -56,7 +56,7 @@ impl Drop for RunRowFinalizer {
);
// Preserve whatever steps the live observer already persisted.
let observed = current_persisted_steps(&self.config, &self.thread_id);
- finish_flow_run_row(
+ let settled = finish_flow_run_row(
&self.config,
&self.thread_id,
&self.flow_id,
@@ -66,6 +66,9 @@ impl Drop for RunRowFinalizer {
Some(INTERRUPTED_DROP_REASON),
None,
);
+ if !settled {
+ return;
+ }
// Keep the flow-definition summary in step with the row, exactly as the
// success/failure/cancel arms and the boot sweep do — otherwise the
// runs list keeps advertising the *previous* run's `last_status` /
@@ -134,9 +137,69 @@ pub(super) fn finish_flow_run_row(
pending_approvals: &[String],
error: Option<&str>,
graph_hash: Option<&str>,
+) -> bool {
+ finish_flow_run_row_with_writer(
+ config,
+ thread_id,
+ flow_id,
+ status,
+ steps,
+ pending_approvals,
+ error,
+ graph_hash,
+ store::finish_flow_run,
+ )
+}
+
+/// Settles a row only if it remains parked for approval. This is the guarded
+/// write for paths that began from a parked-run observation and must leave a
+/// concurrently claimed resume untouched.
+pub(super) fn finish_parked_flow_run_row(
+ config: &Config,
+ thread_id: &str,
+ flow_id: &str,
+ status: &str,
+ steps: &[FlowRunStep],
+ pending_approvals: &[String],
+ error: Option<&str>,
+ graph_hash: Option<&str>,
+) -> bool {
+ finish_flow_run_row_with_writer(
+ config,
+ thread_id,
+ flow_id,
+ status,
+ steps,
+ pending_approvals,
+ error,
+ graph_hash,
+ store::finish_parked_flow_run,
+ )
+}
+
+#[allow(clippy::too_many_arguments)]
+fn finish_flow_run_row_with_writer(
+ config: &Config,
+ thread_id: &str,
+ flow_id: &str,
+ status: &str,
+ steps: &[FlowRunStep],
+ pending_approvals: &[String],
+ error: Option<&str>,
+ graph_hash: Option<&str>,
+ writer: fn(
+ &Config,
+ &str,
+ &str,
+ &str,
+ &[FlowRunStep],
+ &[String],
+ Option<&str>,
+ Option<&str>,
+ ) -> anyhow::Result<bool>,
) -> bool {
let finished_at = Utc::now().to_rfc3339();
- match store::finish_flow_run(
+ match writer(
config,
thread_id,
status,
diff --git a/crates/openhuman-core/src/flows/ops_builder_trail_off_tests.rs b/crates/openhuman-core/src/flows/ops_builder_trail_off_tests.rs
index ea340d28c9..5ed90fac69 100644
--- a/crates/openhuman-core/src/flows/ops_builder_trail_off_tests.rs
+++ b/crates/openhuman-core/src/flows/ops_builder_trail_off_tests.rs
@@ -440,6 +440,38 @@ fn run_row_finalizer_disarm_leaves_a_settled_row_untouched() {
assert!(row.error.is_none());
}
+#[test]
+fn run_row_finalizer_does_not_record_interrupted_for_an_already_terminal_row() {
+ let tmp = TempDir::new().unwrap();
+ let (config, flow_id, run_id) = seed_running_run(&tmp);
+ let now = Utc::now().to_rfc3339();
+ assert!(store::finish_flow_run(
+ &config,
+ &run_id,
+ "completed",
+ &now,
+ &[],
+ &[],
+ None,
+ None,
+ )
+ .unwrap());
+ store::record_run(&config, &flow_id, "completed").unwrap();
+
+ // A late drop must not rewrite the flow summary when its guarded row
+ // update lost to a terminal outcome.
+ drop(RunRowFinalizer::new(
+ Arc::new(config.clone()),
+ &run_id,
+ &flow_id,
+ ));
+
+ let row = store::get_flow_run(&config, &run_id).unwrap().unwrap();
+ assert_eq!(row.status, "completed");
+ let flow = store::get_flow(&config, &flow_id).unwrap().unwrap();
+ assert_eq!(flow.last_status.as_deref(), Some("completed"));
+}
+
#[tokio::test]
async fn boot_sweep_reconciles_orphaned_running_run_to_interrupted() {
let tmp = TempDir::new().unwrap();
diff --git a/crates/openhuman-core/src/flows/ops_run_lifecycle_tests.rs b/crates/openhuman-core/src/flows/ops_run_lifecycle_tests.rs
index a24aee156c..ec0d181920 100644
--- a/crates/openhuman-core/src/flows/ops_run_lifecycle_tests.rs
+++ b/crates/openhuman-core/src/flows/ops_run_lifecycle_tests.rs
@@ -34,7 +34,7 @@ async fn stale_graph_refusal_leaves_a_resume_claimed_run_and_summary_untouched()
// A winning resume claims the row between the stale graph check's read
// and its attempted refusal write.
assert!(store::mark_run_resuming(&config, run_id).unwrap());
- assert!(!settle_parked_run(
+ assert!(!run_management::settle_parked_run(
&config,
run_id,
&flow.id,
@@ -67,7 +67,14 @@ async fn cancel_observed_parked_does_not_tear_down_a_resume_starting() {
// Cancel already read `pending_approval` and found no registry entry;
// resume then registers and claims the row before cancel's CAS.
assert!(store::mark_run_resuming(&config, run_id).unwrap());
- assert!(!settle_parked_run(&config, run_id, &flow.id, "cancelled", "run cancelled").await);
+ assert!(!run_management::settle_parked_run(
+ &config,
+ run_id,
+ &flow.id,
+ "cancelled",
+ "run cancelled"
+ )
+ .await);
let row = store::get_flow_run(&config, run_id).unwrap().unwrap();
assert_eq!(row.status, "running");
diff --git a/crates/openhuman-core/src/flows/store.rs b/crates/openhuman-core/src/flows/store.rs
index 7a4773732f..f612caf446 100644
--- a/crates/openhuman-core/src/flows/store.rs
+++ b/crates/openhuman-core/src/flows/store.rs
@@ -267,6 +267,40 @@ pub fn finish_flow_run(
})
}
+/// The catalog's parked-only `finish_parked_flow_run` compare-and-swap.
+#[inline]
+pub fn finish_parked_flow_run(
+ config: &Config,
+ id: &str,
+ status: &str,
+ finished_at: &str,
+ steps: &[FlowRunStep],
+ pending_approvals: &[String],
+ error: Option<&str>,
+ graph_hash: Option<&str>,
+) -> Result<bool> {
+ let docs = documents(config)?;
+ let id = id.to_string();
+ let status = status.to_string();
+ let finished_at = finished_at.to_string();
+ let steps = steps.to_vec();
+ let pending_approvals = pending_approvals.to_vec();
+ let error = error.map(str::to_string);
+ let graph_hash = graph_hash.map(str::to_string);
+ run(async move {
+ docs.finish_parked_flow_run(
+ &id,
+ &status,
+ &finished_at,
+ &steps,
+ &pending_approvals,
+ error.as_deref(),
+ graph_hash.as_deref(),
+ )
+ .await
+ })
+}
+
/// The catalog's `upsert_flow_run_step` for this call.
#[inline]
pub fn upsert_flow_run_step(config: &Config, run_id: &str, step: &FlowRunStep) -> Result<()> {