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
refactor(channels): unify turn guard acquisition across runners
Steven Enamakele868693dc886
diff --git a/crates/openhuman-core/src/channels/providers/relay/ops.rs b/crates/openhuman-core/src/channels/providers/relay/ops.rsindex 7fcd1c0494..cbeec9d1c4 100644--- a/crates/openhuman-core/src/channels/providers/relay/ops.rs+++ b/crates/openhuman-core/src/channels/providers/relay/ops.rs@@ -105,17 +105,11 @@ pub(crate) async fn run_relay_turn(thread_id: String,request_id: String,) -> Vec<String> {- // One turn at a time per profile: wait behind its chat and cron turns.- let _turn = crate::profiles::turn_gate::acquire(&dispatch_ctx).await;let workspace_dir = config.workspace_dir.clone();// The thread is busy for the whole turn, so a background result due on it// waits instead of landing mid-turn. The pipeline's session id does not// name the thread, so the guard is taken on the thread itself; this task// runs in the caller's scope, which keys it to the caller's profile.- let busy = crate::agent::orchestration::busy_guard::TurnBusy::start_on_thread(- &dispatch_ctx,- &thread_id,- );let channel = Arc::new(RelayChannel::new(params.channel.clone(),params.client_id(),@@ -315,7 +309,6 @@ pub(crate) async fn run_relay_turn(record(&dispatch_ctx, &workspace_dir, ¶ms, &thread_id, &replies).await;// Idle again: deliver anything that finished while the turn ran. No// `AgentTurnCompleted` maps to this thread, so nothing else would.- drop(busy);crate::agent::orchestration::background_delivery::kick_delivery(&dispatch_ctx, &thread_id);tracing::info!(channel = %params.channel,diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/native_runner.rs b/crates/openhuman-core/src/channels/runtime/dispatch/native_runner.rsindex 05c73d68c9..9d6a5504a0 100644--- a/crates/openhuman-core/src/channels/runtime/dispatch/native_runner.rs+++ b/crates/openhuman-core/src/channels/runtime/dispatch/native_runner.rs@@ -36,6 +36,8 @@ type Runner = TurnRunner<#[derive(Default)]struct NativeSlot {lease: Option<SessionLease>,+ profile_turn: Option<crate::profiles::turn_gate::TurnGuard>,+ busy: Option<crate::agent::orchestration::busy_guard::TurnBusy>,native_reply: Option<Arc<NativeProgressiveReply>>,reply: Option<String>,progress_failed: bool,@@ -70,6 +72,8 @@ impl NativeShared {slot.delivery_succeeded = Some(delivered);// The lease spans model execution and terminal provider delivery.slot.lease = None;+ slot.profile_turn = None;+ slot.busy = None;if !slot.waiting_for_delivery {slots.remove(request_id);}@@ -243,6 +247,21 @@ impl NativeHost {).await.map_err(|()| "channel session lease was cancelled".to_owned())?;+ // Match WebHost's lock order exactly: session lease before the+ // profile-wide chat/cron gate. Both acquisitions happen in checkout,+ // after TurnRunner admission, and cancellation drops either future.+ let profile_turn = crate::profiles::turn_gate::acquire(dispatch).await;+ let busy = match trusted.relay_thread.as_deref() {+ Some(thread_id) => Some(+ crate::agent::orchestration::busy_guard::TurnBusy::start_on_thread(+ dispatch, thread_id,+ ),+ ),+ None => Some(crate::agent::orchestration::busy_guard::TurnBusy::start(+ dispatch,+ &session_id,+ )),+ };let native_reply = match trusted.native_target.as_ref() {Some(target) => Some(target@@ -254,6 +273,8 @@ impl NativeHost {};self.shared.with(&request.request_id, |slot| {slot.lease = Some(lease);+ slot.profile_turn = profile_turn;+ slot.busy = busy;slot.native_reply = native_reply;});Ok(TurnSession {diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/native_runner_tests.rs b/crates/openhuman-core/src/channels/runtime/dispatch/native_runner_tests.rsindex 316266fd52..72dcc7711a 100644--- a/crates/openhuman-core/src/channels/runtime/dispatch/native_runner_tests.rs+++ b/crates/openhuman-core/src/channels/runtime/dispatch/native_runner_tests.rs@@ -31,3 +31,43 @@ async fn active_terminal_delivery_is_returned_to_its_waiting_caller() {.unwrap_or_else(|poisoned| poisoned.into_inner()).is_empty());}++#[tokio::test]+async fn terminal_delivery_releases_session_profile_and_busy_guards() {+ let dispatch = crate::core::runtime::test_support::dispatch_ctx();+ let profile = format!("native-runner-test-{}", uuid::Uuid::new_v4());+ let session = format!("native-session-{}", uuid::Uuid::new_v4());+ let lease = crate::agent::session_lease::SessionLease::acquire(+ crate::agent::session_lease::SessionLeaseKey::new("native-test-tenant", "agent", &session),+ None,+ )+ .await+ .expect("session lease");+ let profile_turn = crate::profiles::turn_gate::acquire_profile(&profile).await;+ let busy = crate::agent::orchestration::busy_guard::TurnBusy::start(&dispatch, &session);+ let shared = NativeShared::default();+ shared.begin_delivery_wait("guarded-request");+ shared.with("guarded-request", |slot| {+ slot.lease = Some(lease);+ slot.profile_turn = Some(profile_turn);+ slot.busy = Some(busy);+ });++ assert!(crate::agent::orchestration::busy_guard::is_busy(&dispatch, &session));+ assert!(tokio::time::timeout(+ std::time::Duration::from_millis(5),+ crate::profiles::turn_gate::acquire_profile(&profile),+ )+ .await+ .is_err());++ shared.record_delivery("guarded-request", true);++ assert!(!crate::agent::orchestration::busy_guard::is_busy(&dispatch, &session));+ tokio::time::timeout(+ std::time::Duration::from_secs(1),+ crate::profiles::turn_gate::acquire_profile(&profile),+ )+ .await+ .expect("profile gate is released after delivery");+}diff --git a/crates/openhuman-core/src/web_chat/runner.rs b/crates/openhuman-core/src/web_chat/runner.rsindex 6ac4f08e1c..edfbc51f18 100644--- a/crates/openhuman-core/src/web_chat/runner.rs+++ b/crates/openhuman-core/src/web_chat/runner.rs@@ -83,6 +83,11 @@ pub(super) struct TurnSlot {/// Cross-surface lease survives TinyAgents' early `checkin` until the/// terminal projection has finished delivering this turn.pub(super) session_lease: Option<SessionLease>,+ /// Profile-wide serialization is acquired after the session lease (the+ /// same order used by native channel turns) and held through terminal+ /// projection. Acquiring these in the opposite order can deadlock when+ /// two Runner instances target one session.+ pub(super) profile_turn: Option<crate::profiles::turn_gate::TurnGuard>,pub(super) fork: bool,pub(super) config: Option<Arc<crate::config::Config>>,pub(super) thread_dir: Option<std::path::PathBuf>,diff --git a/crates/openhuman-core/src/web_chat/turn_events.rs b/crates/openhuman-core/src/web_chat/turn_events.rsindex 0491093d0e..5f04c1a24f 100644--- a/crates/openhuman-core/src/web_chat/turn_events.rs+++ b/crates/openhuman-core/src/web_chat/turn_events.rs@@ -426,14 +426,15 @@ impl EventSink<QueuedPayload> for WebEvents {// A message that waited in a run queue and never started was never// announced as a turn, so its being dropped is silent too.let never_started = self.shared.was_queued(&request.request_id);- let (projection, done, note, _session_lease) = match slot {+ let (projection, done, note, _session_lease, _profile_turn) = match slot {Some(slot) => (slot.projection,slot.result,slot.cancel,slot.session_lease,+ slot.profile_turn,),- None => (None, None, None, None),+ None => (None, None, None, None, None),};if never_started && !matches!(event, TerminalEvent::Completed { .. }) {log::debug!(diff --git a/crates/openhuman-core/src/web_chat/turn_host.rs b/crates/openhuman-core/src/web_chat/turn_host.rsindex b5e449e629..f56b928f75 100644--- a/crates/openhuman-core/src/web_chat/turn_host.rs+++ b/crates/openhuman-core/src/web_chat/turn_host.rs@@ -118,8 +118,15 @@ impl WebHost {).await.map_err(|()| "agent session lease was cancelled".to_owned())?;+ // Every runner acquires locks in this order: durable session first,+ // then the profile-wide chat/cron gate. This keeps native, relay and+ // web runner instances from forming an inverse-lock cycle.+ let profile_turn = crate::profiles::turn_gate::acquire(&ctx).await;self.shared- .with(id, |slot| slot.session_lease = Some(lease));+ .with(id, |slot| {+ slot.session_lease = Some(lease);+ slot.profile_turn = profile_turn;+ });// A follow-up the runner dispatches after its predecessor finished was// admitted when it was queued; one started directly still has its// admission to answer.@@ -223,9 +230,6 @@ impl WebHost {let id = request.request_id.as_str();let thread_id = request.thread_id.as_str();let payload = &request.payload;- // One turn at a time per profile: wait behind its relay and cron turns.- let _turn = crate::profiles::turn_gate::acquire(&ctx).await;-#[cfg(any(test, debug_assertions))]if let Some(outcome) = ops::run_test_hooks(&self.shared, request).await {return outcome;