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
feat(web_chat): add parallel turn guards for concurrent chat turns
Steven Enamakelde0bb48dbcce
diff --git a/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs b/crates/openhuman-core/src/web_chat/ops/parallel_turn.rsindex 23f8eebb0e..3a38f8f7ad 100644--- a/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs+++ b/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs@@ -67,7 +67,7 @@ pub(crate) async fn spawn_parallel_turn(task_cancel_token,origin.clone(),approval_ctx,- |host_env| {+ |host_env, activity| {run_chat_task(&client_id_task,&thread_id_task,@@ -81,6 +81,7 @@ pub(crate) async fn spawn_parallel_turn(origin.clone(),/* fork */ true,host_env,+ activity,)},)diff --git a/crates/openhuman-core/src/web_chat/ops/start_chat.rs b/crates/openhuman-core/src/web_chat/ops/start_chat.rsindex 6a202a8bd2..d5ebc1a689 100644--- a/crates/openhuman-core/src/web_chat/ops/start_chat.rs+++ b/crates/openhuman-core/src/web_chat/ops/start_chat.rs@@ -510,7 +510,7 @@ async fn start_chat_inner(task_cancel_token,origin.clone(),approval_ctx,- |host_env| {+ |host_env, activity| {run_chat_task(&client_id_task,&thread_id_task,@@ -524,6 +524,7 @@ async fn start_chat_inner(origin.clone(),/* fork */ false,host_env,+ activity,)},)diff --git a/crates/openhuman-core/src/web_chat/ops/turn_guards.rs b/crates/openhuman-core/src/web_chat/ops/turn_guards.rsindex 94223a5c30..38f1ed8e3a 100644--- a/crates/openhuman-core/src/web_chat/ops/turn_guards.rs+++ b/crates/openhuman-core/src/web_chat/ops/turn_guards.rs@@ -93,20 +93,22 @@ enum Stop {/// The idle guard fires first for a turn that stopped emitting progress with/// nothing in flight; the backstop bounds a turn that stays busy past the/// harness wind-down. `run_chat_task` taps the turn's progress stream into the-/// [`TurnActivity`] scoped here.-async fn drive_turn_with_deadline<F>(+/// [`TurnActivity`] built here and handed to the turn.+async fn drive_turn_with_deadline<B, F>(deadline: Option<Duration>,idle_window: Option<Duration>,ids: Option<TurnIds>,- fut: F,+ build: B,) -> Result<WebChatTaskResult, String>where+ B: FnOnce(TurnActivity) -> F,F: std::future::Future<Output = Result<WebChatTaskResult, String>>,{let activity = TurnActivity::new();// The turn future is boxed: it is very large, and the scope wrappers would- // otherwise hold it inline again and overflow the worker stack.- let turn = turn_activity::with_turn_activity(activity.clone(), Box::pin(fut));+ // otherwise hold it inline again and overflow the worker stack. It receives+ // the activity record explicitly.+ let turn = Box::pin(build(activity.clone()));let bounded_turn = async move {let Some(d) = deadline else {return Ok(turn.await);@@ -214,7 +216,8 @@ fn turn_host_env(/// `run_chat_task` when building `fut`.////// `build_turn` receives the turn's [`HostTurnEnv`] -- its own cancellation-/// token (fired by a Stop) and the backstop's [`TurnDeadline`] -- and returns+/// token (fired by a Stop) and the backstop's [`TurnDeadline`] -- and the guard's+/// [`TurnActivity`] record, and returns/// the turn future, which hands the env to the session it runs. The env is an/// explicit value, so it reaches the turn's whole run tree without any ambient/// scope.@@ -227,7 +230,7 @@ pub(crate) async fn run_turn_under_cancel_and_deadline<B, F>(build_turn: B,) -> Option<Result<WebChatTaskResult, String>>where- B: FnOnce(HostTurnEnv) -> F,+ B: FnOnce(HostTurnEnv, TurnActivity) -> F,F: std::future::Future<Output = Result<WebChatTaskResult, String>>,{let ids = approval_ctx.request_id.clone().map(|request_id| TurnIds {@@ -236,7 +239,6 @@ where});let deadline = resolve_web_turn_deadline().await;let (env, turn_cancellation) = turn_host_env(deadline);- let fut = build_turn(env);run_until_stopped(cancel_token,turn_cancellation,@@ -244,10 +246,13 @@ wheredeadline,turn_activity::configured_idle_window(),ids,- crate::agent::turn_origin::with_origin(- origin,- crate::security::approval::APPROVAL_CHAT_CONTEXT.scope(approval_ctx, fut),- ),+ |activity| {+ crate::agent::turn_origin::with_origin(+ origin,+ crate::security::approval::APPROVAL_CHAT_CONTEXT+ .scope(approval_ctx, build_turn(env, activity)),+ )+ },),).awaitdiff --git a/crates/openhuman-core/src/web_chat/run_task.rs b/crates/openhuman-core/src/web_chat/run_task.rsindex 7a122edd0b..f437321ffc 100644--- a/crates/openhuman-core/src/web_chat/run_task.rs+++ b/crates/openhuman-core/src/web_chat/run_task.rs@@ -44,6 +44,9 @@ pub(crate) async fn run_chat_task(// This turn's host environment (its Stop cancellation, the wall-clock// backstop's deadline), handed to the session so the whole run tree sees it.host_env: crate::agent::turn_env::HostTurnEnv,+ // The web-turn guard's activity record for this turn, passed explicitly by+ // `run_turn_under_cancel_and_deadline`.+ activity: super::turn_activity::TurnActivity,) -> Result<WebChatTaskResult, String> {// One turn at a time per profile: wait behind its relay and cron turns.let _turn = crate::profiles::turn_gate::acquire().await;@@ -124,9 +127,7 @@ pub(crate) async fn run_chat_task(}// A guard that stops this turn closes it from this turn's own config and// reply language, not from whatever profile is active when it fires.- if let Some(activity) = super::turn_activity::current() {- activity.remember_turn(std::sync::Arc::new(config.clone()), locale.clone());- }+ activity.remember_turn(std::sync::Arc::new(config.clone()), locale.clone());let model_override = normalize_model_override(model_override);// The cached session (or a cold-boot resumed one) is the thread's single// live history; every turn on the thread checks it out through this path.@@ -158,10 +159,7 @@ pub(crate) async fn run_chat_task(let (progress_tx, progress_rx) = tokio::sync::mpsc::channel(256);// Under the web-turn guard, fold every event into the turn's activity// record (the idle guard's input) on its way to the bridge.- let progress_rx = match super::turn_activity::current() {- Some(activity) => super::turn_activity::tap(activity, progress_rx, 256),- None => progress_rx,- };+ let progress_rx = super::turn_activity::tap(activity, progress_rx, 256);// The channel is fresh here. Record the user input even if the turn fails// before a committed reply can emit its final TurnContent event.let _ = progress_tx.try_send(AgentProgress::TurnContent {