Skip to content

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

feat(web_chat): add parallel turn handling for web chat ops

Steven Enamakel45fd99343659
diff --git a/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs b/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs
index 76c3a12709..a137dc9815 100644
--- a/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs
+++ b/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs
@@ -27,6 +27,7 @@ use super::turn_guards::{
/// can render it as an interleaved branch.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn spawn_parallel_turn(
+ ctx: &crate::core::runtime::DispatchCtx,
client_id: &str,
thread_id: &str,
request_id: String,
@@ -47,6 +48,7 @@ pub(crate) async fn spawn_parallel_turn(
let map_key = key_for(&request_id);
let map_key_task = map_key.clone();
let user_message = message.to_string();
+ let ctx_task = ctx.clone();
// Forked turns don't participate in the steer/followup/collect queue, but
// `run_chat_task` requires a queue handle — give each its own.
let run_queue = Arc::new(RunQueue::new());
@@ -64,11 +66,13 @@ pub(crate) async fn spawn_parallel_turn(
request_id: Some(request_id_task.clone()),
};
let result = run_turn_under_cancel_and_deadline(
+ &ctx_task,
task_cancel_token,
origin.clone(),
approval_ctx,
|host_env, activity, approval| {
run_chat_task(
+ &ctx_task,
&client_id_task,
&thread_id_task,
&request_id_task,
@@ -91,6 +95,7 @@ pub(crate) async fn spawn_parallel_turn(
match result {
Some(Ok(chat_result)) => {
crate::web_chat::presentation::deliver_response(
+ &ctx_task,
&client_id_task,
&thread_id_task,
&request_id_task,
diff --git a/crates/openhuman-core/src/web_chat/ops/start_chat.rs b/crates/openhuman-core/src/web_chat/ops/start_chat.rs
index 98fba4977e..18ad6ee658 100644
--- a/crates/openhuman-core/src/web_chat/ops/start_chat.rs
+++ b/crates/openhuman-core/src/web_chat/ops/start_chat.rs
@@ -138,6 +138,7 @@ fn prompt_guard_user_message(action: PromptEnforcementAction) -> &'static str {
#[allow(clippy::too_many_arguments)]
#[inline(never)]
pub fn start_chat<'a>(
+ ctx: &'a crate::core::runtime::DispatchCtx,
client_id: &'a str,
thread_id: &'a str,
message: &'a str,
@@ -148,6 +149,7 @@ pub fn start_chat<'a>(
metadata: ChatRequestMetadata,
) -> futures::future::BoxFuture<'a, Result<String, StartChatError>> {
Box::pin(start_chat_inner(
+ ctx,
client_id,
thread_id,
message,
@@ -161,6 +163,7 @@ pub fn start_chat<'a>(
#[allow(clippy::too_many_arguments)]
async fn start_chat_inner(
+ ctx: &crate::core::runtime::DispatchCtx,
client_id: &str,
thread_id: &str,
message: &str,
@@ -190,7 +193,8 @@ async fn start_chat_inner(
// Save originals before scanning, history, memory, or queue persistence.
// A missing config/root cannot safely accept an upload.
let mut message = if message.contains("[FILE:") || message.contains("[IMAGE:") {
- let config = crate::config::rpc::load_config_with_timeout()
+ let config = ctx
+ .load_config()
.await
.map_err(|error| {
StartChatError::Other(format!("upload configuration unavailable: {error}"))
@@ -368,6 +372,7 @@ async fn start_chat_inner(
request_id
);
spawn_parallel_turn(
+ ctx,
&client_id,
&thread_id,
request_id.clone(),
@@ -491,6 +496,7 @@ async fn start_chat_inner(
let task_cancel_token = cancel_token.clone();
let user_message = message.clone();
+ let ctx_task = ctx.clone();
let handle = tokio::spawn(crate::core::runtime::context::CoreContext::propagate(
async move {
let approval_ctx = crate::security::approval::ApprovalChatContext {
@@ -507,11 +513,13 @@ async fn start_chat_inner(
// result; the interrupting/cancelling side already emitted the
// user-facing `chat_error`, so we just unwind quietly here.
let result = run_turn_under_cancel_and_deadline(
+ &ctx_task,
task_cancel_token,
origin.clone(),
approval_ctx,
|host_env, activity, approval| {
run_chat_task(
+ &ctx_task,
&client_id_task,
&thread_id_task,
&request_id_task,
@@ -659,6 +667,7 @@ async fn start_chat_inner(
// once on another task.
if let Some(chat_result) = deferred_done {
crate::web_chat::presentation::announce_reply(
+ &ctx_task,
&client_id_task,
&thread_id_task,
&request_id_task,
@@ -694,7 +703,7 @@ async fn start_chat_inner(
.map(|f| crate::agent::queued_turn::text_preview(&f.text)),
},
);
- dispatch_followups(followups);
+ dispatch_followups(&ctx_task, followups);
}
},
));
@@ -716,11 +725,16 @@ async fn start_chat_inner(
Ok(request_id)
}
-fn dispatch_followups(followups: Vec<crate::agent::queued_turn::QueuedTurn>) {
+fn dispatch_followups(
+ ctx: &crate::core::runtime::DispatchCtx,
+ followups: Vec<crate::agent::queued_turn::QueuedTurn>,
+) {
for fup in followups {
+ let ctx = ctx.clone();
tokio::spawn(crate::core::runtime::context::CoreContext::propagate(
async move {
if let Err(err) = start_chat(
+ &ctx,
&fup.client_id,
&fup.thread_id,
&fup.text,
diff --git a/crates/openhuman-core/src/web_chat/ops/system_turn.rs b/crates/openhuman-core/src/web_chat/ops/system_turn.rs
index 1c05221f47..c2c9f20bc1 100644
--- a/crates/openhuman-core/src/web_chat/ops/system_turn.rs
+++ b/crates/openhuman-core/src/web_chat/ops/system_turn.rs
@@ -18,7 +18,6 @@ use std::sync::Arc;
use tinyagents_harness::run_queue::RunQueue;
use crate::agent::turn_origin::{with_origin, AgentTurnOrigin};
-use crate::config::rpc as config_rpc;
use super::super::run_task::turn_error_discards_session;
use super::super::session::{
@@ -43,12 +42,14 @@ pub const SESSION_CHECKOUT_FAILURE: &str = "session checkout failed: ";
/// newer user message and does not interrupt one. Callers gate on idleness
/// themselves (background delivery defers while a user turn is in flight).
pub async fn run_system_turn_on_thread(
+ ctx: &crate::core::runtime::DispatchCtx,
thread_id: &str,
run_id: &str,
prompt: &str,
origin: AgentTurnOrigin,
) -> Result<String, String> {
- let config = config_rpc::load_config_with_timeout()
+ let config = ctx
+ .load_config()
.await
.map_err(|error| format!("{SESSION_CHECKOUT_FAILURE}{error}"))?;
let CheckedOutSession {
diff --git a/crates/openhuman-core/src/web_chat/ops/turn_guards.rs b/crates/openhuman-core/src/web_chat/ops/turn_guards.rs
index 5fa2448a1a..169b377aa6 100644
--- a/crates/openhuman-core/src/web_chat/ops/turn_guards.rs
+++ b/crates/openhuman-core/src/web_chat/ops/turn_guards.rs
@@ -43,11 +43,13 @@ fn web_turn_deadline() -> Option<Duration> {
/// explicit `OPENHUMAN_WEB_TURN_TIMEOUT_SECS` override still wins; otherwise the
/// default is the local turn ceiling plus a grace period, never the hosted
/// backstop, which sits below the local harness ceilings and would pre-empt them.
-async fn resolve_web_turn_deadline() -> Option<Duration> {
+async fn resolve_web_turn_deadline(
+ ctx: &crate::core::runtime::DispatchCtx,
+) -> Option<Duration> {
if std::env::var("OPENHUMAN_WEB_TURN_TIMEOUT_SECS").is_ok() {
return web_turn_deadline();
}
- let local = match crate::config::rpc::load_config_with_timeout().await {
+ let local = match ctx.load_config().await {
Ok(config) => crate::agent::tinyagents::chat_provider_is_local(&config),
Err(error) => {
log::debug!("[web-channel] backstop locality probe skipped: {error}");
@@ -224,6 +226,7 @@ fn turn_host_env(
///
/// [`TurnDeadline`]: tinyagents_harness::turn_deadline::TurnDeadline
pub(crate) async fn run_turn_under_cancel_and_deadline<B, F>(
+ ctx: &crate::core::runtime::DispatchCtx,
cancel_token: CancellationToken,
origin: crate::agent::turn_origin::AgentTurnOrigin,
approval_ctx: crate::security::approval::ApprovalChatContext,
@@ -237,7 +240,7 @@ where
thread_id: approval_ctx.thread_id.clone(),
request_id,
});
- let deadline = resolve_web_turn_deadline().await;
+ let deadline = resolve_web_turn_deadline(ctx).await;
let (env, turn_cancellation) = turn_host_env(deadline);
run_until_stopped(
cancel_token,
diff --git a/crates/openhuman-core/src/web_chat/presentation.rs b/crates/openhuman-core/src/web_chat/presentation.rs
index ebb03f5f83..c77c3b56c5 100644
--- a/crates/openhuman-core/src/web_chat/presentation.rs
+++ b/crates/openhuman-core/src/web_chat/presentation.rs
@@ -47,6 +47,7 @@ fn usage_payload(usage: Option<&LastTurnUsage>) -> Option<TurnUsagePayload> {
/// (#6034). Pass `None` from a caller that has no workspace in scope; delivery
/// then behaves exactly as it did when the renderer was the only writer.
pub(crate) async fn deliver_response(
+ ctx: &crate::core::runtime::DispatchCtx,
client_id: &str,
thread_id: &str,
request_id: &str,
@@ -67,6 +68,7 @@ pub(crate) async fn deliver_response(
)
.await;
announce_reply(
+ ctx,
client_id,
thread_id,
request_id,
@@ -136,6 +138,7 @@ pub(crate) async fn persist_reply(
/// `chat_done`, then (for the main chat turn) the follow-up suggestions.
#[allow(clippy::too_many_arguments)]
pub(crate) fn announce_reply(
+ ctx: &crate::core::runtime::DispatchCtx,
client_id: &str,
thread_id: &str,
request_id: &str,
@@ -160,6 +163,7 @@ pub(crate) fn announce_reply(
);
if suggest_follow_ups {
super::suggestions::spawn_follow_up_suggestions(
+ ctx.clone(),
client_id.to_string(),
thread_id.to_string(),
request_id.to_string(),
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge.rs b/crates/openhuman-core/src/web_chat/progress_bridge.rs
index 878271256f..785cae1add 100644
--- a/crates/openhuman-core/src/web_chat/progress_bridge.rs
+++ b/crates/openhuman-core/src/web_chat/progress_bridge.rs
@@ -278,7 +278,7 @@ pub(crate) fn spawn_progress_bridge(
request_id: String,
turn_state_store: std::sync::Arc<dyn TurnStates>,
metadata: ChatRequestMetadata,
- config: crate::config::Config,
+ config: crate::config::Config, ctx: crate::core::runtime::DispatchCtx,
) -> ProgressBridgeHandle {
use crate::agent::progress::AgentProgress;
use std::collections::HashMap;
@@ -1164,10 +1164,7 @@ pub(crate) fn spawn_progress_bridge(
let live_spans = collector.spans().to_vec();
let journal_export = if let Some(trace_ctx) = journal_trace_ctx.take() {
super::journal_shadow::shadow_compare_journal_projection(
- &request_id,
- trace_ctx.clone(),
- parent_max_iterations,
- &live_spans,
+ &ctx, &request_id, trace_ctx.clone(), parent_max_iterations, &live_spans,
)
.await
.map(|observations| (trace_ctx, observations))
diff --git a/crates/openhuman-core/src/web_chat/run_task.rs b/crates/openhuman-core/src/web_chat/run_task.rs
index 8e37ddb187..9ca67e9a19 100644
--- a/crates/openhuman-core/src/web_chat/run_task.rs
+++ b/crates/openhuman-core/src/web_chat/run_task.rs
@@ -8,7 +8,6 @@ use std::sync::Arc;
use tinyagents_harness::run_queue::RunQueue;
use crate::agent::progress::AgentProgress;
-use crate::config::rpc as config_rpc;
use super::ops::BudgetCorrelation;
use super::progress_bridge::spawn_progress_bridge;
@@ -26,6 +25,7 @@ use tinyinference_llm::failure::is_empty_provider_response_text;
use super::ops::TEST_FORCED_RUN_CHAT_TASK_ERROR;
pub(crate) async fn run_chat_task(
+ ctx: &crate::core::runtime::DispatchCtx,
client_id: &str,
thread_id: &str,
request_id: &str,
@@ -116,7 +116,7 @@ pub(crate) async fn run_chat_task(
}
}
- let mut config = config_rpc::load_config_with_timeout().await?;
+ let mut config = ctx.load_config().await?;
// A thread started in a chosen folder acts there, not in the global
// `action_dir`. Applied before checkout so the session agent, its security
// policy (which grants `action_dir` as a trusted root) and its tools are
@@ -197,6 +197,7 @@ pub(crate) async fn run_chat_task(
turn_state_store,
bridge_metadata,
config.clone(),
+ ctx.clone(),
);
// `run_single`'s future is very large; box it so the two ambient-scope
diff --git a/crates/openhuman-core/src/web_chat/suggestions.rs b/crates/openhuman-core/src/web_chat/suggestions.rs
index d24b0b7a2c..b7a33e41bc 100644
--- a/crates/openhuman-core/src/web_chat/suggestions.rs
+++ b/crates/openhuman-core/src/web_chat/suggestions.rs
@@ -34,7 +34,6 @@ use serde::Deserialize;
use tinyinference_llm::message::Message;
use tinyinference_llm::model::ModelRequest;
-use crate::config::rpc as config_rpc;
use crate::inference::provider;
use crate::web_chat::{ChatSuggestion, WebChannelEvent};
@@ -78,6 +77,7 @@ struct RawSuggestion {
/// to be called immediately after the turn's terminal `chat_done` has been
/// published — see module docs for why this never delays that event.
pub(crate) fn spawn_follow_up_suggestions(
+ ctx: crate::core::runtime::DispatchCtx,
client_id: String,
thread_id: String,
request_id: String,
@@ -87,6 +87,7 @@ pub(crate) fn spawn_follow_up_suggestions(
tokio::spawn(crate::core::runtime::context::CoreContext::propagate(
async move {
generate_and_emit(
+ &ctx,
&client_id,
&thread_id,
&request_id,
@@ -99,6 +100,7 @@ pub(crate) fn spawn_follow_up_suggestions(
}
async fn generate_and_emit(
+ ctx: &crate::core::runtime::DispatchCtx,
client_id: &str,
thread_id: &str,
request_id: &str,
@@ -115,7 +117,7 @@ async fn generate_and_emit(
return;
}
- let config = match config_rpc::load_config_with_timeout().await {
+ let config = match ctx.load_config().await {
Ok(c) => c,
Err(err) => {
log::debug!(