Skip to content

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

test(flows): split resume checkpoint tests into focused modules

Steven Enamakelf6251294e170
diff --git a/crates/openhuman-core/src/flows/ops_resume_checkpoint_tests.rs b/crates/openhuman-core/src/flows/ops_resume_checkpoint_tests.rs
index c28e534a92..f41c526a93 100644
--- a/crates/openhuman-core/src/flows/ops_resume_checkpoint_tests.rs
+++ b/crates/openhuman-core/src/flows/ops_resume_checkpoint_tests.rs
@@ -324,478 +324,3 @@ async fn flows_resume_allows_a_legacy_row_with_null_graph_hash() {
assert_eq!(resumed.value["pending_approvals"], json!([]));
}
-#[tokio::test]
-async fn flows_resume_marks_an_incompatible_legacy_checkpoint_failed() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
- .await
- .unwrap();
- let run = flows_run(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- json!({ "x": 1 }),
- serde_json::Map::new(),
- FlowRunTrigger::Rpc,
- )
- .await
- .unwrap();
- let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
- let pending: Vec<String> =
- serde_json::from_value(run.value["pending_approvals"].clone()).unwrap();
-
- // Model a legacy parked checkpoint whose current saved graph is unsafe.
- // The module owns compatibility refusal and should settle this run as
- // failed when ResumeRun is attempted.
- let legacy_graph = structurally_valid_graph(nested_conditional_fan_in_graph());
- store::update_flow_graph(
- &config,
- &created.value.id,
- created.value.name.clone(),
- legacy_graph.clone(),
- created.value.require_approval,
- None,
- false,
- None,
- )
- .unwrap();
- // Keep the compatibility refusal under test distinct from the stale-pin
- // refusal: a pre-pin parked row has no graph hash to compare.
- update_parked_run_document_for_test(&config, &thread_id, |document| {
- document
- .as_object_mut()
- .expect("run document is an object")
- .remove("graph_hash");
- });
- let error = flows_resume(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- &thread_id,
- pending,
- vec![],
- )
- .await
- .expect_err("an incompatible checkpoint cannot be resumed safely");
- assert!(
- error.contains(UNSUPPORTED_NESTED_CONDITIONAL_FAN_IN),
- "{error}"
- );
-
- let run_row = flows_get_run(&config, &thread_id).await.unwrap().value;
- assert_eq!(run_row.status, "failed");
- assert!(run_row.pending_approvals.is_empty());
- assert!(
- run_row
- .error
- .as_deref()
- .is_some_and(|value| value.contains(UNSUPPORTED_NESTED_CONDITIONAL_FAN_IN)),
- "the terminal run row should retain the rejection reason: {run_row:?}"
- );
- let flow = flows_get(&config, &created.value.id).await.unwrap().value;
- assert_eq!(flow.last_status.as_deref(), Some("failed"));
-}
-
-#[tokio::test]
-async fn flows_resume_marks_a_checkpoint_with_an_incompatible_saved_child_failed() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
- .await
- .unwrap();
- let run = flows_run(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- json!({ "x": 1 }),
- serde_json::Map::new(),
- FlowRunTrigger::Rpc,
- )
- .await
- .unwrap();
- let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
- let pending: Vec<String> =
- serde_json::from_value(run.value["pending_approvals"].clone()).unwrap();
- let child = store::create_flow(
- &config,
- "legacy unsafe child".to_string(),
- structurally_valid_graph(nested_conditional_fan_in_graph()),
- false,
- false,
- )
- .unwrap();
- let legacy_graph = structurally_valid_graph(referenced_child_graph(&child.id));
- store::update_flow_graph(
- &config,
- &created.value.id,
- created.value.name.clone(),
- legacy_graph.clone(),
- created.value.require_approval,
- None,
- false,
- None,
- )
- .unwrap();
- // Keep the compatibility refusal under test distinct from the stale-pin
- // refusal: a pre-pin parked row has no graph hash to compare.
- update_parked_run_document_for_test(&config, &thread_id, |document| {
- document
- .as_object_mut()
- .expect("run document is an object")
- .remove("graph_hash");
- });
- let error = flows_resume(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- &thread_id,
- pending,
- vec![],
- )
- .await
- .expect_err("an incompatible saved child cannot be resumed safely");
- assert!(
- error.contains(UNSUPPORTED_NESTED_CONDITIONAL_FAN_IN),
- "{error}"
- );
- assert!(error.contains(&child.id), "{error}");
-
- let run_row = flows_get_run(&config, &thread_id).await.unwrap().value;
- assert_eq!(run_row.status, "failed");
- assert!(run_row.pending_approvals.is_empty());
- assert!(
- run_row
- .error
- .as_deref()
- .is_some_and(|value| value.contains(UNSUPPORTED_NESTED_CONDITIONAL_FAN_IN))
- );
- let flow = flows_get(&config, &created.value.id).await.unwrap().value;
- assert_eq!(flow.last_status.as_deref(), Some("failed"));
-}
-
-#[tokio::test]
-async fn flows_resume_missing_flow_errors() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let err = flows_resume(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- "missing",
- "thread-1",
- vec![],
- vec![],
- )
- .await
- .expect_err("must error");
- assert!(err.contains("not found"));
-}
-
-// ── flows_resume host-side approval guard (issue B2 finding #3) ──────────
-//
-// tinyflows 0.2's `resume_with_checkpointer` treats the resume call itself
-// as approval of whatever gate paused the run — its `approvals` argument is
-// advisory, not enforced by the crate. Live testing confirmed
-// `flows_resume(..., approvals: [])` on a paused run still completed it.
-// These tests exercise the host-side guard added in `flows::ops::flows_resume`
-// that requires `approvals` to actually name a currently-pending gate,
-// straight from the persisted `flow_runs` row, before ever calling into the
-// engine.
-
-#[tokio::test]
-async fn flows_resume_with_empty_approvals_is_rejected_and_does_not_complete_the_run() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
- .await
- .unwrap();
-
- let run = flows_run(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- json!({ "x": 1 }),
- serde_json::Map::new(),
- FlowRunTrigger::Rpc,
- )
- .await
- .unwrap();
- let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
-
- let err = flows_resume(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- &thread_id,
- vec![],
- vec![],
- )
- .await
- .expect_err("an empty approvals list must not silently approve the pending gate");
- assert!(
- err.contains("no pending approval matches"),
- "expected a clear approval-mismatch error, got: {err}"
- );
-
- // The run must still be sitting at pending_approval, not completed.
- let run_row = flows_get_run(&config, &thread_id).await.unwrap();
- assert_eq!(run_row.value.status, "pending_approval");
- assert_eq!(run_row.value.pending_approvals, vec!["gate".to_string()]);
-
- let reloaded = flows_get(&config, &created.value.id).await.unwrap();
- assert_eq!(
- reloaded.value.last_status.as_deref(),
- Some("pending_approval"),
- "a rejected resume attempt must not overwrite the flow's last_status as completed"
- );
-}
-
-#[tokio::test]
-async fn flows_resume_with_mismatched_approvals_is_rejected() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
- .await
- .unwrap();
-
- let run = flows_run(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- json!({ "x": 1 }),
- serde_json::Map::new(),
- FlowRunTrigger::Rpc,
- )
- .await
- .unwrap();
- let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
-
- // Names a node id that is not actually pending for this run.
- let err = flows_resume(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- &thread_id,
- vec!["not-a-real-gate".to_string()],
- vec![],
- )
- .await
- .expect_err("approvals naming no actually-pending gate must be rejected");
- assert!(err.contains("no pending approval matches"));
-}
-
-#[tokio::test]
-async fn flows_resume_with_the_correct_gate_completes_and_runs_downstream() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
- .await
- .unwrap();
-
- let run = flows_run(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- json!({ "x": 1 }),
- serde_json::Map::new(),
- FlowRunTrigger::Rpc,
- )
- .await
- .unwrap();
- let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
-
- let resumed = flows_resume(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- &thread_id,
- vec!["gate".to_string()],
- vec![],
- )
- .await
- .unwrap();
- assert_eq!(resumed.value["pending_approvals"], json!([]));
- assert!(
- !resumed.value["output"]["nodes"]["downstream"]["items"].is_null(),
- "downstream should run once the correct gate is named in approvals"
- );
-
- let reloaded = flows_get(&config, &created.value.id).await.unwrap();
- assert_eq!(reloaded.value.last_status.as_deref(), Some("completed"));
-}
-
-#[tokio::test]
-async fn flows_resume_denying_a_gate_routes_to_its_error_port() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let created = flows_create(
- &config,
- "gated-deny".to_string(),
- approval_gated_graph_with_error_port(),
- false,
- )
- .await
- .unwrap();
-
- let run = flows_run(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- json!({ "x": 1 }),
- serde_json::Map::new(),
- FlowRunTrigger::Rpc,
- )
- .await
- .unwrap();
- let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
-
- // Deny the gate: no approvals, `gate` in rejections.
- let resumed = flows_resume(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- &thread_id,
- vec![],
- vec!["gate".to_string()],
- )
- .await
- .unwrap();
-
- assert_eq!(resumed.value["pending_approvals"], json!([]));
- assert_eq!(
- resumed.value["output"]["nodes"]["recover"]["items"][0]["json"]["error"]["node"],
- json!("gate"),
- "a denied gate must route its error item to the `error`-port recovery node"
- );
- assert!(
- resumed.value["output"]["nodes"]["downstream"].is_null(),
- "the main branch must not run when the gate is denied"
- );
-
- let reloaded = flows_get(&config, &created.value.id).await.unwrap();
- assert_eq!(reloaded.value.last_status.as_deref(), Some("completed"));
-
- let run_row = flows_get_run(&config, &thread_id).await.unwrap();
- assert_eq!(run_row.value.status, "completed");
- assert!(run_row.value.pending_approvals.is_empty());
-}
-
-#[tokio::test]
-async fn flows_resume_returns_failed_step_output_when_denial_has_no_error_port() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- // `approval_gated_graph()` has only a `main` edge out of the gate — no
- // `error` port to route a denial to, so the whole run must fail.
- let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
- .await
- .unwrap();
-
- let run = flows_run(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- json!({ "x": 1 }),
- serde_json::Map::new(),
- FlowRunTrigger::Rpc,
- )
- .await
- .unwrap();
- let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
-
- let resumed = flows_resume(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- &thread_id,
- vec![],
- vec!["gate".to_string()],
- )
- .await
- .expect("step-level failure still has a successful engine outcome");
- assert_eq!(resumed.value["pending_approvals"], json!([]));
- assert_eq!(resumed.value["thread_id"], json!(thread_id));
- assert!(
- resumed.value["output"].is_object(),
- "a failed step outcome must preserve the engine output in the legacy RPC response"
- );
-
- let reloaded = flows_get(&config, &created.value.id).await.unwrap();
- assert_eq!(reloaded.value.last_status.as_deref(), Some("failed"));
- let run_row = flows_get_run(&config, &thread_id).await.unwrap();
- assert_eq!(run_row.value.status, "failed");
-}
-
-#[tokio::test]
-async fn flows_resume_rejects_a_gate_named_in_both_approvals_and_rejections() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
- .await
- .unwrap();
-
- let run = flows_run(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- json!({}),
- serde_json::Map::new(),
- FlowRunTrigger::Rpc,
- )
- .await
- .unwrap();
- let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
-
- let err = flows_resume(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- &thread_id,
- vec!["gate".to_string()],
- vec!["gate".to_string()],
- )
- .await
- .expect_err("a gate cannot be both approved and rejected");
- assert!(err.contains("cannot be both approved and rejected"));
-
- // The run must be untouched (still pending), never half-resumed.
- let run_row = flows_get_run(&config, &thread_id).await.unwrap();
- assert_eq!(run_row.value.status, "pending_approval");
-}
-
-#[tokio::test]
-async fn flows_resume_of_a_non_paused_run_errors_clearly() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let created = flows_create(&config, "demo".to_string(), trigger_only_graph(), false)
- .await
- .unwrap();
-
- // This run completes outright (no approval gate) — its recorded status
- // is "completed", not "pending_approval".
- let run = flows_run(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- json!({}),
- serde_json::Map::new(),
- FlowRunTrigger::Rpc,
- )
- .await
- .unwrap();
- let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
-
- let err = flows_resume(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &created.value.id,
- &thread_id,
- vec![],
- vec![],
- )
- .await
- .expect_err("resuming an already-completed run must be a clear error, not a silent no-op");
- assert!(
- err.contains("not pending approval") || err.contains("no paused run"),
- "expected a clear non-paused-run error, got: {err}"
- );
-}
diff --git a/crates/openhuman-core/src/flows/ops_resume_compatibility_tests.rs b/crates/openhuman-core/src/flows/ops_resume_compatibility_tests.rs
new file mode 100644
index 0000000000..784db1aa6c
--- /dev/null
+++ b/crates/openhuman-core/src/flows/ops_resume_compatibility_tests.rs
@@ -0,0 +1,170 @@
+use super::*;
+use super::resume_checkpoint_tests::update_parked_run_document_for_test;
+
+#[tokio::test]
+async fn flows_resume_marks_an_incompatible_legacy_checkpoint_failed() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
+ .await
+ .unwrap();
+ let run = flows_run(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ json!({ "x": 1 }),
+ serde_json::Map::new(),
+ FlowRunTrigger::Rpc,
+ )
+ .await
+ .unwrap();
+ let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
+ let pending: Vec<String> =
+ serde_json::from_value(run.value["pending_approvals"].clone()).unwrap();
+
+ // Model a legacy parked checkpoint whose current saved graph is unsafe.
+ // The module owns compatibility refusal and should settle this run as
+ // failed when ResumeRun is attempted.
+ let legacy_graph = structurally_valid_graph(nested_conditional_fan_in_graph());
+ store::update_flow_graph(
+ &config,
+ &created.value.id,
+ created.value.name.clone(),
+ legacy_graph.clone(),
+ created.value.require_approval,
+ None,
+ false,
+ None,
+ )
+ .unwrap();
+ // Keep the compatibility refusal under test distinct from the stale-pin
+ // refusal: a pre-pin parked row has no graph hash to compare.
+ update_parked_run_document_for_test(&config, &thread_id, |document| {
+ document
+ .as_object_mut()
+ .expect("run document is an object")
+ .remove("graph_hash");
+ });
+ let error = flows_resume(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ &thread_id,
+ pending,
+ vec![],
+ )
+ .await
+ .expect_err("an incompatible checkpoint cannot be resumed safely");
+ assert!(
+ error.contains(UNSUPPORTED_NESTED_CONDITIONAL_FAN_IN),
+ "{error}"
+ );
+
+ let run_row = flows_get_run(&config, &thread_id).await.unwrap().value;
+ assert_eq!(run_row.status, "failed");
+ assert!(run_row.pending_approvals.is_empty());
+ assert!(
+ run_row
+ .error
+ .as_deref()
+ .is_some_and(|value| value.contains(UNSUPPORTED_NESTED_CONDITIONAL_FAN_IN)),
+ "the terminal run row should retain the rejection reason: {run_row:?}"
+ );
+ let flow = flows_get(&config, &created.value.id).await.unwrap().value;
+ assert_eq!(flow.last_status.as_deref(), Some("failed"));
+}
+
+#[tokio::test]
+async fn flows_resume_marks_a_checkpoint_with_an_incompatible_saved_child_failed() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
+ .await
+ .unwrap();
+ let run = flows_run(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ json!({ "x": 1 }),
+ serde_json::Map::new(),
+ FlowRunTrigger::Rpc,
+ )
+ .await
+ .unwrap();
+ let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
+ let pending: Vec<String> =
+ serde_json::from_value(run.value["pending_approvals"].clone()).unwrap();
+ let child = store::create_flow(
+ &config,
+ "legacy unsafe child".to_string(),
+ structurally_valid_graph(nested_conditional_fan_in_graph()),
+ false,
+ false,
+ )
+ .unwrap();
+ let legacy_graph = structurally_valid_graph(referenced_child_graph(&child.id));
+ store::update_flow_graph(
+ &config,
+ &created.value.id,
+ created.value.name.clone(),
+ legacy_graph.clone(),
+ created.value.require_approval,
+ None,
+ false,
+ None,
+ )
+ .unwrap();
+ // Keep the compatibility refusal under test distinct from the stale-pin
+ // refusal: a pre-pin parked row has no graph hash to compare.
+ update_parked_run_document_for_test(&config, &thread_id, |document| {
+ document
+ .as_object_mut()
+ .expect("run document is an object")
+ .remove("graph_hash");
+ });
+ let error = flows_resume(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ &thread_id,
+ pending,
+ vec![],
+ )
+ .await
+ .expect_err("an incompatible saved child cannot be resumed safely");
+ assert!(
+ error.contains(UNSUPPORTED_NESTED_CONDITIONAL_FAN_IN),
+ "{error}"
+ );
+ assert!(error.contains(&child.id), "{error}");
+
+ let run_row = flows_get_run(&config, &thread_id).await.unwrap().value;
+ assert_eq!(run_row.status, "failed");
+ assert!(run_row.pending_approvals.is_empty());
+ assert!(
+ run_row
+ .error
+ .as_deref()
+ .is_some_and(|value| value.contains(UNSUPPORTED_NESTED_CONDITIONAL_FAN_IN))
+ );
+ let flow = flows_get(&config, &created.value.id).await.unwrap().value;
+ assert_eq!(flow.last_status.as_deref(), Some("failed"));
+}
+
+#[tokio::test]
+async fn flows_resume_missing_flow_errors() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ let err = flows_resume(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ "missing",
+ "thread-1",
+ vec![],
+ vec![],
+ )
+ .await
+ .expect_err("must error");
+ assert!(err.contains("not found"));
+}
+
diff --git a/crates/openhuman-core/src/flows/ops_resume_decision_tests.rs b/crates/openhuman-core/src/flows/ops_resume_decision_tests.rs
new file mode 100644
index 0000000000..6cd5f25f67
--- /dev/null
+++ b/crates/openhuman-core/src/flows/ops_resume_decision_tests.rs
@@ -0,0 +1,310 @@
+use super::*;
+
+// ── flows_resume host-side approval guard (issue B2 finding #3) ──────────
+//
+// tinyflows 0.2's `resume_with_checkpointer` treats the resume call itself
+// as approval of whatever gate paused the run — its `approvals` argument is
+// advisory, not enforced by the crate. Live testing confirmed
+// `flows_resume(..., approvals: [])` on a paused run still completed it.
+// These tests exercise the host-side guard added in `flows::ops::flows_resume`
+// that requires `approvals` to actually name a currently-pending gate,
+// straight from the persisted `flow_runs` row, before ever calling into the
+// engine.
+
+#[tokio::test]
+async fn flows_resume_with_empty_approvals_is_rejected_and_does_not_complete_the_run() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
+ .await
+ .unwrap();
+
+ let run = flows_run(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ json!({ "x": 1 }),
+ serde_json::Map::new(),
+ FlowRunTrigger::Rpc,
+ )
+ .await
+ .unwrap();
+ let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
+
+ let err = flows_resume(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ &thread_id,
+ vec![],
+ vec![],
+ )
+ .await
+ .expect_err("an empty approvals list must not silently approve the pending gate");
+ assert!(
+ err.contains("no pending approval matches"),
+ "expected a clear approval-mismatch error, got: {err}"
+ );
+
+ // The run must still be sitting at pending_approval, not completed.
+ let run_row = flows_get_run(&config, &thread_id).await.unwrap();
+ assert_eq!(run_row.value.status, "pending_approval");
+ assert_eq!(run_row.value.pending_approvals, vec!["gate".to_string()]);
+
+ let reloaded = flows_get(&config, &created.value.id).await.unwrap();
+ assert_eq!(
+ reloaded.value.last_status.as_deref(),
+ Some("pending_approval"),
+ "a rejected resume attempt must not overwrite the flow's last_status as completed"
+ );
+}
+
+#[tokio::test]
+async fn flows_resume_with_mismatched_approvals_is_rejected() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
+ .await
+ .unwrap();
+
+ let run = flows_run(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ json!({ "x": 1 }),
+ serde_json::Map::new(),
+ FlowRunTrigger::Rpc,
+ )
+ .await
+ .unwrap();
+ let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
+
+ // Names a node id that is not actually pending for this run.
+ let err = flows_resume(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ &thread_id,
+ vec!["not-a-real-gate".to_string()],
+ vec![],
+ )
+ .await
+ .expect_err("approvals naming no actually-pending gate must be rejected");
+ assert!(err.contains("no pending approval matches"));
+}
+
+#[tokio::test]
+async fn flows_resume_with_the_correct_gate_completes_and_runs_downstream() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
+ .await
+ .unwrap();
+
+ let run = flows_run(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ json!({ "x": 1 }),
+ serde_json::Map::new(),
+ FlowRunTrigger::Rpc,
+ )
+ .await
+ .unwrap();
+ let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
+
+ let resumed = flows_resume(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ &thread_id,
+ vec!["gate".to_string()],
+ vec![],
+ )
+ .await
+ .unwrap();
+ assert_eq!(resumed.value["pending_approvals"], json!([]));
+ assert!(
+ !resumed.value["output"]["nodes"]["downstream"]["items"].is_null(),
+ "downstream should run once the correct gate is named in approvals"
+ );
+
+ let reloaded = flows_get(&config, &created.value.id).await.unwrap();
+ assert_eq!(reloaded.value.last_status.as_deref(), Some("completed"));
+}
+
+#[tokio::test]
+async fn flows_resume_denying_a_gate_routes_to_its_error_port() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ let created = flows_create(
+ &config,
+ "gated-deny".to_string(),
+ approval_gated_graph_with_error_port(),
+ false,
+ )
+ .await
+ .unwrap();
+
+ let run = flows_run(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ json!({ "x": 1 }),
+ serde_json::Map::new(),
+ FlowRunTrigger::Rpc,
+ )
+ .await
+ .unwrap();
+ let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
+
+ // Deny the gate: no approvals, `gate` in rejections.
+ let resumed = flows_resume(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ &thread_id,
+ vec![],
+ vec!["gate".to_string()],
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(resumed.value["pending_approvals"], json!([]));
+ assert_eq!(
+ resumed.value["output"]["nodes"]["recover"]["items"][0]["json"]["error"]["node"],
+ json!("gate"),
+ "a denied gate must route its error item to the `error`-port recovery node"
+ );
+ assert!(
+ resumed.value["output"]["nodes"]["downstream"].is_null(),
+ "the main branch must not run when the gate is denied"
+ );
+
+ let reloaded = flows_get(&config, &created.value.id).await.unwrap();
+ assert_eq!(reloaded.value.last_status.as_deref(), Some("completed"));
+
+ let run_row = flows_get_run(&config, &thread_id).await.unwrap();
+ assert_eq!(run_row.value.status, "completed");
+ assert!(run_row.value.pending_approvals.is_empty());
+}
+
+#[tokio::test]
+async fn flows_resume_returns_failed_step_output_when_denial_has_no_error_port() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ // `approval_gated_graph()` has only a `main` edge out of the gate — no
+ // `error` port to route a denial to, so the whole run must fail.
+ let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
+ .await
+ .unwrap();
+
+ let run = flows_run(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ json!({ "x": 1 }),
+ serde_json::Map::new(),
+ FlowRunTrigger::Rpc,
+ )
+ .await
+ .unwrap();
+ let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
+
+ let resumed = flows_resume(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ &thread_id,
+ vec![],
+ vec!["gate".to_string()],
+ )
+ .await
+ .expect("step-level failure still has a successful engine outcome");
+ assert_eq!(resumed.value["pending_approvals"], json!([]));
+ assert_eq!(resumed.value["thread_id"], json!(thread_id));
+ assert!(
+ resumed.value["output"].is_object(),
+ "a failed step outcome must preserve the engine output in the legacy RPC response"
+ );
+
+ let reloaded = flows_get(&config, &created.value.id).await.unwrap();
+ assert_eq!(reloaded.value.last_status.as_deref(), Some("failed"));
+ let run_row = flows_get_run(&config, &thread_id).await.unwrap();
+ assert_eq!(run_row.value.status, "failed");
+}
+
+#[tokio::test]
+async fn flows_resume_rejects_a_gate_named_in_both_approvals_and_rejections() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ let created = flows_create(&config, "gated".to_string(), approval_gated_graph(), false)
+ .await
+ .unwrap();
+
+ let run = flows_run(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ json!({}),
+ serde_json::Map::new(),
+ FlowRunTrigger::Rpc,
+ )
+ .await
+ .unwrap();
+ let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
+
+ let err = flows_resume(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ &thread_id,
+ vec!["gate".to_string()],
+ vec!["gate".to_string()],
+ )
+ .await
+ .expect_err("a gate cannot be both approved and rejected");
+ assert!(err.contains("cannot be both approved and rejected"));
+
+ // The run must be untouched (still pending), never half-resumed.
+ let run_row = flows_get_run(&config, &thread_id).await.unwrap();
+ assert_eq!(run_row.value.status, "pending_approval");
+}
+
+#[tokio::test]
+async fn flows_resume_of_a_non_paused_run_errors_clearly() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ let created = flows_create(&config, "demo".to_string(), trigger_only_graph(), false)
+ .await
+ .unwrap();
+
+ // This run completes outright (no approval gate) — its recorded status
+ // is "completed", not "pending_approval".
+ let run = flows_run(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ json!({}),
+ serde_json::Map::new(),
+ FlowRunTrigger::Rpc,
+ )
+ .await
+ .unwrap();
+ let thread_id = run.value["thread_id"].as_str().unwrap().to_string();
+
+ let err = flows_resume(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &created.value.id,
+ &thread_id,
+ vec![],
+ vec![],
+ )
+ .await
+ .expect_err("resuming an already-completed run must be a clear error, not a silent no-op");
+ assert!(
+ err.contains("not pending approval") || err.contains("no paused run"),
+ "expected a clear non-paused-run error, got: {err}"
+ );
+}