The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
chore: files changed crates/openhuman-core/src/flows/ops.rs,crates/openhuman-core/src/flows/ops/buil
Steven Enamakela71fd309fc35
diff --git a/crates/openhuman-core/src/flows/ops.rs b/crates/openhuman-core/src/flows/ops.rsindex eaebe50f8c..669aa89bc4 100644--- a/crates/openhuman-core/src/flows/ops.rs+++ b/crates/openhuman-core/src/flows/ops.rs@@ -47,7 +47,7 @@ use crate::flows::draft_store;use crate::flows::store;use crate::security::approval::{ApprovalChatContext, ApprovalTurnContext};use crate::security::turn_origin::{with_origin, AgentTurnOrigin};-use tinyflows_catalog::build_registry;+mod build_registry;use tinyflows_catalog::types::{FlowConnection, FlowRunTrigger, FlowSuggestion, SuggestionStatus};use tokio_util::sync::CancellationToken;use {tinyflows_catalog::Flow, tinyflows_catalog::FlowRun};diff --git a/crates/openhuman-core/src/flows/ops/build_registry.rs b/crates/openhuman-core/src/flows/ops/build_registry.rsnew file mode 100644index 0000000000..4eb08d7f36--- /dev/null+++ b/crates/openhuman-core/src/flows/ops/build_registry.rs@@ -0,0 +1,114 @@+//! Tracks the OpenHuman-owned in-flight workflow-builder turns that stream+//! into a chat thread. The builder runs inline on its host task, so the bus+//! run registry does not own this cancellation lifecycle.++use std::collections::HashMap;+use std::sync::{LazyLock, Mutex};++use tokio_util::sync::CancellationToken;++/// One in-flight builder turn keyed by its chat thread.+struct BuildTurn {+ request_id: Option<String>,+ token: CancellationToken,+}++/// Current builder turn for each chat thread.+static BUILD_TURNS: LazyLock<Mutex<HashMap<String, BuildTurn>>> =+ LazyLock::new(|| Mutex::new(HashMap::new()));++/// Registers an in-flight `flows_build` turn.+///+/// A newer turn replaces the old thread entry and cancels its token so the+/// displaced model call cannot continue after losing its Stop control.+pub(super) fn register_build_turn(+ thread_id: String,+ request_id: Option<String>,+ token: CancellationToken,+) {+ tracing::debug!(+ target: "flows",+ thread_id = %thread_id,+ request_id = request_id.as_deref().unwrap_or("<none>"),+ "[flows] build turn registered"+ );+ let displaced = BUILD_TURNS+ .lock()+ .unwrap_or_else(|error| error.into_inner())+ .insert(thread_id.clone(), BuildTurn { request_id, token });+ if let Some(displaced) = displaced {+ if !displaced.token.is_cancelled() {+ tracing::debug!(+ target: "flows",+ thread_id = %thread_id,+ "[flows] cancelling displaced builder turn"+ );+ displaced.token.cancel();+ }+ }+}++/// Signals the current builder turn, optionally matching its request id.+/// A stale Stop for an earlier request cannot cancel its replacement.+pub(super) fn cancel_build_turn_scoped(thread_id: &str, request_id: Option<&str>) -> bool {+ let turns = BUILD_TURNS+ .lock()+ .unwrap_or_else(|error| error.into_inner());+ let Some(turn) = turns.get(thread_id) else {+ tracing::debug!(+ target: "flows",+ thread_id,+ "[flows] builder cancellation ignored: no turn is registered"+ );+ return false;+ };+ if let Some(request_id) = request_id {+ if turn.request_id.as_deref() != Some(request_id) {+ tracing::debug!(+ target: "flows",+ thread_id,+ request_id,+ active_request_id = turn.request_id.as_deref().unwrap_or("<none>"),+ "[flows] builder cancellation ignored: request id does not match"+ );+ return false;+ }+ }+ turn.token.cancel();+ tracing::info!(+ target: "flows",+ thread_id,+ "[flows] builder turn cancellation signalled"+ );+ true+}++/// Removes a turn only if it still belongs to this request.+///+/// An older turn's cleanup may run after a newer turn has registered on the+/// same thread; matching the request id prevents that cleanup from removing+/// the newer Stop target.+pub(super) fn unregister_build_turn(thread_id: &str, request_id: Option<&str>) {+ let mut turns = BUILD_TURNS+ .lock()+ .unwrap_or_else(|error| error.into_inner());+ if let Some(request_id) = request_id {+ match turns.get(thread_id) {+ Some(turn) if turn.request_id.as_deref() == Some(request_id) => {}+ _ => {+ tracing::debug!(+ target: "flows",+ thread_id,+ request_id,+ "[flows] builder unregister ignored: thread has a different request"+ );+ return;+ }+ }+ }+ turns.remove(thread_id);+}++#[cfg(test)]+#[path = "build_registry_tests.rs"]+mod tests;diff --git a/crates/openhuman-core/src/flows/ops/build_registry_tests.rs b/crates/openhuman-core/src/flows/ops/build_registry_tests.rsnew file mode 100644index 0000000000..0dc23e4917--- /dev/null+++ b/crates/openhuman-core/src/flows/ops/build_registry_tests.rs@@ -0,0 +1,106 @@+use super::*;++#[test]+fn cancellation_signals_the_matching_registered_turn() {+ let thread_id = "build-registry-test:thread-1";+ let token = CancellationToken::new();+ register_build_turn(+ thread_id.to_string(),+ Some("req-1".to_string()),+ token.clone(),+ );++ assert!(!token.is_cancelled());+ assert!(cancel_build_turn_scoped(thread_id, Some("req-1")));+ assert!(token.is_cancelled());++ unregister_build_turn(thread_id, Some("req-1"));+}++#[test]+fn cancellation_returns_false_when_no_turn_is_registered() {+ assert!(!cancel_build_turn_scoped(+ "build-registry-test:never-registered",+ Some("req-x")+ ));+ assert!(!cancel_build_turn_scoped(+ "build-registry-test:never-registered",+ None+ ));+}++#[test]+fn cancellation_ignores_a_mismatched_request_id() {+ let thread_id = "build-registry-test:thread-2";+ let token = CancellationToken::new();+ register_build_turn(+ thread_id.to_string(),+ Some("req-1".to_string()),+ token.clone(),+ );++ assert!(!cancel_build_turn_scoped(+ thread_id,+ Some("some-other-request")+ ));+ assert!(!token.is_cancelled());+ assert!(cancel_build_turn_scoped(thread_id, Some("req-1")));+ assert!(token.is_cancelled());++ unregister_build_turn(thread_id, Some("req-1"));+}++#[test]+fn unscoped_cancellation_targets_the_registered_turn() {+ let thread_id = "build-registry-test:thread-3";+ let token = CancellationToken::new();+ register_build_turn(+ thread_id.to_string(),+ Some("req-1".to_string()),+ token.clone(),+ );++ assert!(cancel_build_turn_scoped(thread_id, None));+ assert!(token.is_cancelled());+ unregister_build_turn(thread_id, None);+}++#[test]+fn stale_unregister_keeps_the_replacement_turn_registered() {+ let thread_id = "build-registry-test:thread-4";+ let token_a = CancellationToken::new();+ register_build_turn(+ thread_id.to_string(),+ Some("req-a".to_string()),+ token_a.clone(),+ );++ let token_b = CancellationToken::new();+ register_build_turn(+ thread_id.to_string(),+ Some("req-b".to_string()),+ token_b.clone(),+ );+ assert!(token_a.is_cancelled());+ assert!(!token_b.is_cancelled());++ unregister_build_turn(thread_id, Some("req-a"));+ assert!(cancel_build_turn_scoped(thread_id, Some("req-b")));+ assert!(token_b.is_cancelled());++ unregister_build_turn(thread_id, Some("req-b"));+ assert!(!cancel_build_turn_scoped(thread_id, None));+}++#[test]+fn unscoped_unregister_removes_the_current_turn() {+ let thread_id = "build-registry-test:thread-5";+ register_build_turn(+ thread_id.to_string(),+ Some("req-1".to_string()),+ CancellationToken::new(),+ );++ unregister_build_turn(thread_id, None);+ assert!(!cancel_build_turn_scoped(thread_id, None));+}