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 unused cancellation registry

Steven Enamakel4db87591bd8d
diff --git a/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/cancel.rs b/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/cancel.rs
deleted file mode 100644
index be306a636c..0000000000
--- a/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/cancel.rs
+++ /dev/null
@@ -1,108 +0,0 @@
-//! Process-wide cancellation registry for running workflow engine loops.
-//!
-//! A stop/resume hand-off replaces the per-run signal. Every loop retains its
-//! exact generation, so late cleanup from a fenced loop cannot erase or cancel
-//! the signal now owned by its replacement.
-
-use std::collections::HashMap;
-use std::sync::atomic::{AtomicBool, Ordering};
-use std::sync::{Arc, Mutex, OnceLock};
-
-use tinyagents_harness::CancellationToken;
-
-/// One generation of a run's legacy poll flag and crate-native token.
-#[derive(Clone)]
-pub(super) struct WorkflowCancelSignal {
- generation: u64,
- pub(super) flag: Arc<AtomicBool>,
- pub(super) token: CancellationToken,
-}
-
-#[derive(Default)]
-struct CancelRegistry {
- next_generation: u64,
- signals: HashMap<String, WorkflowCancelSignal>,
-}
-
-fn cancel_registry() -> &'static Mutex<CancelRegistry> {
- static REGISTRY: OnceLock<Mutex<CancelRegistry>> = OnceLock::new();
- REGISTRY.get_or_init(|| Mutex::new(CancelRegistry::default()))
-}
-
-fn fresh_signal(registry: &mut CancelRegistry) -> WorkflowCancelSignal {
- registry.next_generation = registry.next_generation.wrapping_add(1);
- WorkflowCancelSignal {
- generation: registry.next_generation,
- flag: Arc::new(AtomicBool::new(false)),
- token: CancellationToken::new(),
- }
-}
-
-/// Register a signal for a new loop, preserving a stop intent that was
-/// recorded before that loop was scheduled.
-pub(super) fn register_cancel_signal(run_id: &str) -> WorkflowCancelSignal {
- let mut registry = cancel_registry().lock().expect("cancel registry poisoned");
- if let Some(signal) = registry.signals.get(run_id) {
- return signal.clone();
- }
- let signal = fresh_signal(&mut registry);
- registry.signals.insert(run_id.to_owned(), signal.clone());
- signal
-}
-
-/// Replace a completed or fenced loop's signal with a new generation. Resume
-/// calls this only after its durable lifecycle CAS has won.
-pub(super) fn replace_cancel_signal(run_id: &str) -> WorkflowCancelSignal {
- let mut registry = cancel_registry().lock().expect("cancel registry poisoned");
- let signal = fresh_signal(&mut registry);
- registry.signals.insert(run_id.to_owned(), signal.clone());
- signal
-}
-
-/// Look up the actual current signal for a run, if any.
-pub(super) fn lookup_cancel_signal(run_id: &str) -> Option<WorkflowCancelSignal> {
- cancel_registry()
- .lock()
- .expect("cancel registry poisoned")
- .signals
- .get(run_id)
- .cloned()
-}
-
-/// Cancel this exact generation only while it remains current. Lifecycle
-/// callers retain the signal they observed before their durable CAS, so a
-/// losing stop cannot cancel a successor installed by resume.
-pub(super) fn cancel_signal_if_current(run_id: &str, signal: &WorkflowCancelSignal) -> bool {
- let current = lookup_cancel_signal(run_id);
- if current
- .as_ref()
- .is_none_or(|current| current.generation != signal.generation)
- {
- return false;
- }
- signal.flag.store(true, Ordering::SeqCst);
- signal.token.cancel();
- true
-}
-
-/// Whether this loop still owns the registry slot it was created with.
-pub(super) fn is_current_cancel_signal(run_id: &str, signal: &WorkflowCancelSignal) -> bool {
- lookup_cancel_signal(run_id).is_some_and(|current| current.generation == signal.generation)
-}
-
-/// Drop a loop's signal only if no stop/resume hand-off has installed a newer
-/// generation in the meantime.
-pub(super) fn clear_cancel_signal(run_id: &str, signal: &WorkflowCancelSignal) {
- let mut registry = cancel_registry().lock().expect("cancel registry poisoned");
- if registry
- .signals
- .get(run_id)
- .is_some_and(|current| current.generation == signal.generation)
- {
- registry.signals.remove(run_id);
- }
-}
-
-#[cfg(test)]
-#[path = "cancel_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/cancel_tests.rs b/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/cancel_tests.rs
deleted file mode 100644
index 6a6d060b48..0000000000
--- a/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/cancel_tests.rs
+++ /dev/null
@@ -1,19 +0,0 @@
-use super::*;
-
-#[test]
-fn resume_token_survives_old_cleanup_and_stop_cancels_the_successor() {
- let run_id = format!("workflow-cancel-race-{}", uuid::Uuid::new_v4());
- let old = register_cancel_signal(&run_id);
- let successor = replace_cancel_signal(&run_id);
-
- clear_cancel_signal(&run_id, &old);
- assert!(is_current_cancel_signal(&run_id, &successor));
- assert!(!successor.token.is_cancelled());
-
- assert!(cancel_signal_if_current(&run_id, &successor));
- assert!(successor.token.is_cancelled());
- assert!(successor.flag.load(Ordering::SeqCst));
- assert!(!old.token.is_cancelled());
-
- clear_cancel_signal(&run_id, &successor);
-}
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 eb09b0d062..a82badcb52 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
@@ -82,7 +82,6 @@ fn schedule_workflow_drive(
let drive_handle_slot = Arc::clone(&handle_slot);
let drive_config_slot = Arc::clone(&config_slot);
let drive_parent_slot = Arc::clone(&parent_slot);
- let drive_ctx = task_ctx.clone();
let drive_run_id = run_id.clone();
let drive_definition = definition.clone();
let root_ctx = task_ctx.clone();
@@ -112,7 +111,7 @@ fn schedule_workflow_drive(
drive.await;
},
)
- .await;
+ .await
{
if let Some(handle) = handle_slot.get() {
record_failed_if_current(
@@ -136,7 +135,6 @@ fn schedule_workflow_drive(
crate::security::turn_origin::with_inherited_origin(inherited_origin, future)
};
let make_future = move |cancel_token| {
- let ctx = drive_ctx.clone();
let run_id = drive_run_id.clone();
let definition = drive_definition.clone();
let handle_slot = Arc::clone(&drive_handle_slot);
diff --git a/crates/openhuman-core/src/agent_runs/workflow_runs/mod.rs b/crates/openhuman-core/src/agent_runs/workflow_runs/mod.rs
index 24221445f3..6f35f9d5d6 100644
--- a/crates/openhuman-core/src/agent_runs/workflow_runs/mod.rs
+++ b/crates/openhuman-core/src/agent_runs/workflow_runs/mod.rs
@@ -1,7 +1,5 @@
//! Durable dynamic workflow run RPC surface.
-#[path = "../../agent/orchestration/workflow_runs/engine/cancel.rs"]
-mod cancel;
#[path = "../../agent/orchestration/workflow_runs/host.rs"]
mod host;
#[path = "../../agent/orchestration/workflow_runs/engine/lifecycle.rs"]