Skip to content

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

fix(web_chat): scope parallel turn keys by agent

Steven Enamakel18a73b7e9846
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 7cf6f74c11..c6eab6c57c 100644
--- a/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs
+++ b/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs
@@ -14,7 +14,7 @@ use super::super::event_bus::publish_web_channel_event;
use super::super::run_task::run_chat_task;
use super::super::types::{ChatRequestMetadata, ParallelEntry};
use super::super::web_errors::classify_inference_error;
-use super::state::PARALLEL_IN_FLIGHT;
+use super::state::{key_for, unscope, PARALLEL_IN_FLIGHT};
use super::turn_guards::{
run_turn_under_cancel_and_deadline, sentry_suppression_reason, timeout_bound_tag,
};
@@ -42,6 +42,10 @@ pub(crate) async fn spawn_parallel_turn(
let client_id_task = client_id.to_string();
let thread_id_task = thread_id.to_string();
let request_id_task = request_id.clone();
+ // Request and thread ids are caller-chosen and only unique per agent, so
+ // the table is keyed in the caller's scope (see `key_for`).
+ let map_key = key_for(&request_id);
+ let map_key_task = map_key.clone();
let user_message = message.to_string();
// Forked turns don't participate in the steer/followup/collect queue, but
// `run_chat_task` requires a queue handle — give each its own.
@@ -193,14 +197,14 @@ pub(crate) async fn spawn_parallel_turn(
}
}
- PARALLEL_IN_FLIGHT.lock().await.remove(&request_id_task);
+ PARALLEL_IN_FLIGHT.lock().await.remove(&map_key_task);
},
));
PARALLEL_IN_FLIGHT.lock().await.insert(
- request_id,
+ map_key,
ParallelEntry {
- thread_id: thread_id.to_string(),
+ thread_id: key_for(thread_id),
handle,
cancel_token,
},
@@ -212,10 +216,11 @@ pub(crate) async fn spawn_parallel_turn(
/// tears down any concurrent forked turns, not just the primary turn.
pub(crate) async fn cancel_parallel_turns_for_thread(thread_id: &str) -> Vec<String> {
let mut cancelled = Vec::new();
+ let scoped_thread = key_for(thread_id);
let mut parallel = PARALLEL_IN_FLIGHT.lock().await;
let request_ids: Vec<String> = parallel
.iter()
- .filter(|(_, entry)| entry.thread_id == thread_id)
+ .filter(|(_, entry)| entry.thread_id == scoped_thread)
.map(|(request_id, _)| request_id.clone())
.collect();
for request_id in request_ids {
@@ -230,7 +235,7 @@ pub(crate) async fn cancel_parallel_turns_for_thread(thread_id: &str) -> Vec<Str
}
}
});
- cancelled.push(request_id);
+ cancelled.push(unscope(&request_id));
}
}
cancelled
@@ -244,15 +249,17 @@ pub(crate) async fn cancel_parallel_turn_by_request_id(
thread_id: &str,
request_id: &str,
) -> Vec<String> {
+ let map_key = key_for(request_id);
+ let scoped_thread = key_for(thread_id);
let mut parallel = PARALLEL_IN_FLIGHT.lock().await;
let matches = parallel
- .get(request_id)
- .map(|entry| entry.thread_id == thread_id)
+ .get(&map_key)
+ .map(|entry| entry.thread_id == scoped_thread)
.unwrap_or(false);
if !matches {
return Vec::new();
}
- if let Some(entry) = parallel.remove(request_id) {
+ if let Some(entry) = parallel.remove(&map_key) {
entry.cancel_token.cancel();
let mut handle = entry.handle;
tokio::spawn(async move {
diff --git a/crates/openhuman-core/src/web_chat/ops/state.rs b/crates/openhuman-core/src/web_chat/ops/state.rs
index 6f09ddf92d..a830e32fa0 100644
--- a/crates/openhuman-core/src/web_chat/ops/state.rs
+++ b/crates/openhuman-core/src/web_chat/ops/state.rs
@@ -39,6 +39,20 @@ pub(crate) fn key_for(thread_id: &str) -> String {
scoped_key(agent.as_deref(), thread_id)
}
+/// `key` with the calling scope's agent prefix removed: the id the caller
+/// chose, for handing back to it.
+pub(crate) fn unscope(key: &str) -> String {
+ let agent = crate::core::runtime::CoreContext::current()
+ .and_then(|context| context.session_agent().map(str::to_owned));
+ match agent {
+ Some(agent) => key
+ .strip_prefix(&format!("{agent}::"))
+ .unwrap_or(key)
+ .to_string(),
+ None => key.to_string(),
+ }
+}
+
pub(crate) fn scoped_key(session_agent: Option<&str>, thread_id: &str) -> String {
match session_agent {
Some(agent) => format!("{agent}::{thread_id}"),
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge.rs b/crates/openhuman-core/src/web_chat/progress_bridge.rs
index ed5e060ae9..8d24e1fc18 100644
--- a/crates/openhuman-core/src/web_chat/progress_bridge.rs
+++ b/crates/openhuman-core/src/web_chat/progress_bridge.rs
@@ -333,7 +333,9 @@ pub(crate) fn spawn_progress_bridge(
std::sync::Mutex<Option<super::turn_timing::TurnTimingSnapshot>>,
> = std::sync::Arc::new(std::sync::Mutex::new(None));
let timing_snapshot_for_task = timing_snapshot.clone();
- tokio::spawn(async move {
+ // Scoped: every event this bridge publishes must carry the agent of the
+ // turn it reports on, or a SaaS user's stream would drop it.
+ crate::core::runtime::spawn_scoped(async move {
log::debug!(
"[web_channel][bridge] spawned client_id={} thread_id={} request_id={} speak_reply={:?} source={:?} session_id={:?}",
client_id,