Skip to content

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

refactor(web_chat): split channel and turn ops into dedicated modules

Steven Enamakel3732e3219c90
diff --git a/crates/openhuman-core/src/web_chat/ops/channel_ops.rs b/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
index 93ae12e1f9..b68e6fbe93 100644
--- a/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
+++ b/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
@@ -94,7 +94,7 @@ async fn cancel_chat_inner(
let mut removed_client_id: Option<String> = None;
{
- let mut in_flight = in_flight().lock_owned().await;
+ let mut in_flight = in_flight(ctx).lock_owned().await;
// #4760: only tear down the primary turn when the cancel is unscoped OR
// targets exactly the request that is running. A stale cancel for an
// already-superseded request must be a no-op so the newer turn lives.
@@ -230,11 +230,11 @@ async fn cancel_chat_inner(
}
/// Is a primary or parallel turn still running on `thread_id`?
-async fn thread_has_live_turn(thread_id: &str, map_key: &str) -> bool {
- if super::state::in_flight().lock().await.contains_key(map_key) {
+async fn thread_has_live_turn(ctx: &DispatchCtx, thread_id: &str, map_key: &str) -> bool {
+ if super::state::in_flight(ctx).lock().await.contains_key(map_key) {
return true;
}
- super::state::parallel_in_flight()
+ super::state::parallel_in_flight(ctx)
.lock()
.await
.values()
@@ -265,7 +265,7 @@ pub async fn channel_web_chat(
if let Some(run_mode) = run_mode.as_deref() {
match crate::agent::tinyagents::run_mode::parse_mode_label(run_mode) {
Some(mode) => {
- crate::agent::tinyagents::run_mode::set_mode(thread_id, mode);
+ crate::agent::tinyagents::run_mode::set_mode(ctx, thread_id, mode);
}
None => log::warn!(
"[web_chat] channel_web_chat thread_id={thread_id} ignoring unrecognized run_mode={run_mode}"
@@ -319,7 +319,7 @@ pub async fn channel_web_queue_status(
thread_id: &str,
) -> Result<Outcome<Value>, String> {
let map_key = key_for(ctx, thread_id);
- let in_flight = in_flight().lock_owned().await;
+ let in_flight = in_flight(ctx).lock_owned().await;
if let Some(entry) = in_flight.get(&map_key) {
let status = entry.run_queue.status().await;
let items: Vec<Value> = entry
@@ -376,7 +376,7 @@ pub async fn channel_web_queue_remove(
return Err("item_id is required".to_string());
}
let map_key = key_for(ctx, thread_id);
- let in_flight = in_flight().lock_owned().await;
+ let in_flight = in_flight(ctx).lock_owned().await;
let Some(entry) = in_flight.get(&map_key) else {
return Ok(Outcome::single_log(
json!({
@@ -424,7 +424,7 @@ pub async fn channel_web_queue_clear(
thread_id: &str,
) -> Result<Outcome<Value>, String> {
let map_key = key_for(ctx, thread_id);
- let in_flight = in_flight().lock_owned().await;
+ let in_flight = in_flight(ctx).lock_owned().await;
if let Some(entry) = in_flight.get(&map_key) {
let dropped = entry.run_queue.clear().await;
log::info!(
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 2005b5f3a9..620921cabd 100644
--- a/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs
+++ b/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs
@@ -213,14 +213,14 @@ pub(crate) async fn spawn_parallel_turn(
}
}
- parallel_in_flight()
+ parallel_in_flight(ctx)
.lock_owned()
.await
.remove(&map_key_task);
},
));
- parallel_in_flight().lock_owned().await.insert(
+ parallel_in_flight(ctx).lock_owned().await.insert(
map_key,
ParallelEntry {
thread_id: key_for(ctx, thread_id),
@@ -240,7 +240,7 @@ pub(crate) async fn cancel_parallel_turns_for_thread(
) -> Vec<(String, String)> {
let mut cancelled = Vec::new();
let scoped_thread = key_for(ctx, thread_id);
- let mut parallel = parallel_in_flight().lock_owned().await;
+ let mut parallel = parallel_in_flight(ctx).lock_owned().await;
let request_ids: Vec<String> = parallel
.iter()
.filter(|(_, entry)| entry.thread_id == scoped_thread)
@@ -275,7 +275,7 @@ pub(crate) async fn cancel_parallel_turn_by_request_id(
) -> Vec<(String, String)> {
let map_key = key_for(ctx, request_id);
let scoped_thread = key_for(ctx, thread_id);
- let mut parallel = parallel_in_flight().lock_owned().await;
+ let mut parallel = parallel_in_flight(ctx).lock_owned().await;
let matches = parallel
.get(&map_key)
.map(|entry| entry.thread_id == scoped_thread)
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 95d8cff89f..7a0af548e4 100644
--- a/crates/openhuman-core/src/web_chat/ops/start_chat.rs
+++ b/crates/openhuman-core/src/web_chat/ops/start_chat.rs
@@ -386,7 +386,7 @@ async fn start_chat_inner(
// Non-interrupt modes: push into the running turn's queue and return.
if !matches!(parsed_mode, QueueMode::Interrupt) {
- let in_flight = in_flight().lock_owned().await;
+ let in_flight = in_flight(ctx).lock_owned().await;
if let Some(existing) = in_flight.get(&map_key) {
let item_id = uuid::Uuid::new_v4().to_string();
let text_preview = crate::agent::queued_turn::text_preview(&message);
@@ -440,7 +440,7 @@ async fn start_chat_inner(
}
{
- let mut in_flight = in_flight().lock_owned().await;
+ let mut in_flight = in_flight(ctx).lock_owned().await;
if let Some(existing) = in_flight.remove(&map_key) {
let cancelled_id = cancel_in_flight_gracefully(existing);
@@ -555,7 +555,7 @@ async fn start_chat_inner(
// Release any in-flight slot we still own and stop. The
// `request_id` guard below prevents clobbering a newer turn that
// replaced us on the interrupt path.
- let mut in_flight = in_flight().lock_owned().await;
+ let mut in_flight = in_flight(ctx).lock_owned().await;
if let Some(current) = in_flight.get(&map_key_task) {
if current.request_id == request_id_task {
in_flight.remove(&map_key_task);
@@ -652,7 +652,7 @@ async fn start_chat_inner(
// Drain followup messages queued during this turn.
let followups = {
- let mut in_flight = in_flight().lock_owned().await;
+ let mut in_flight = in_flight(ctx).lock_owned().await;
let followups = if let Some(current) = in_flight.get(&map_key_task) {
if current.request_id == request_id_task {
let fups = current.run_queue.drain(QueueLane::Followup).await;
@@ -714,7 +714,7 @@ async fn start_chat_inner(
));
{
- let mut in_flight = in_flight().lock_owned().await;
+ let mut in_flight = in_flight(ctx).lock_owned().await;
in_flight.insert(
map_key,
InFlightEntry {
diff --git a/crates/openhuman-core/src/web_chat/ops/state.rs b/crates/openhuman-core/src/web_chat/ops/state.rs
index b5f2df5ff1..7037eb4bf2 100644
--- a/crates/openhuman-core/src/web_chat/ops/state.rs
+++ b/crates/openhuman-core/src/web_chat/ops/state.rs
@@ -132,7 +132,7 @@ pub async fn invalidate_thread_sessions(ctx: &DispatchCtx, thread_id: &str) {
// Under an embedded agent or a profile only that tenant's entry goes.
// Outside both every entry for the thread in the current table goes.
let tenant = tenant_of(ctx);
- let mut sessions = thread_sessions().lock_owned().await;
+ let mut sessions = thread_sessions(ctx).lock_owned().await;
let keys_to_remove: Vec<String> = match tenant.agent.as_ref().or(tenant.profile.as_ref()) {
Some(_) => {
let key = key_for(ctx, thread_id);
@@ -184,21 +184,21 @@ pub(crate) async fn invalidate_thread_sessions_for_tenant(
/// Hold the real session table while exercising callback cancellation.
#[cfg(test)]
-pub(crate) async fn lock_thread_sessions_for_test() -> impl Send + 'static {
- thread_sessions().lock_owned().await
+pub(crate) async fn lock_thread_sessions_for_test(ctx: &DispatchCtx) -> impl Send + 'static {
+ thread_sessions(ctx).lock_owned().await
}
/// The thread ids (as the caller chose them) with a primary or parallel turn
/// in the calling context's tables.
pub(crate) async fn live_thread_ids(ctx: &DispatchCtx) -> Vec<String> {
- let mut threads: Vec<String> = in_flight()
+ let mut threads: Vec<String> = in_flight(ctx)
.lock_owned()
.await
.keys()
.map(|key| unscope(ctx, key))
.collect();
threads.extend(
- parallel_in_flight()
+ parallel_in_flight(ctx)
.lock_owned()
.await
.values()
@@ -213,14 +213,14 @@ pub(crate) async fn live_thread_ids(ctx: &DispatchCtx) -> Vec<String> {
/// calling context's tables, so callers can watch a teardown reach it.
#[cfg(test)]
pub(crate) async fn track_parallel_turn_for_test(
+ ctx: &DispatchCtx,
thread_id: &str,
request_id: &str,
cancel: tokio_util::sync::CancellationToken,
) {
- let ctx = &DispatchCtx::ambient();
let watched = cancel.clone();
let handle = crate::core::runtime::spawn_scoped(async move { watched.cancelled().await });
- parallel_in_flight().lock_owned().await.insert(
+ parallel_in_flight(ctx).lock_owned().await.insert(
key_for(ctx, request_id),
ParallelEntry {
thread_id: key_for(ctx, thread_id),
@@ -231,8 +231,8 @@ pub(crate) async fn track_parallel_turn_for_test(
);
}
-pub async fn in_flight_entries_for_test() -> Vec<(String, String)> {
- let guard = in_flight().lock_owned().await;
+pub async fn in_flight_entries_for_test(ctx: &DispatchCtx) -> Vec<(String, String)> {
+ let guard = in_flight(ctx).lock_owned().await;
guard
.iter()
.map(|(k, v)| (k.clone(), v.request_id.clone()))
@@ -245,11 +245,11 @@ pub async fn in_flight_entries_for_test() -> Vec<(String, String)> {
/// status and terminal dispatch paths.
#[cfg(test)]
pub async fn drain_queued_turns_for_test(
+ ctx: &DispatchCtx,
thread_id: &str,
lane: tinyagents_harness::run_queue::QueueLane,
) -> Vec<crate::agent::queued_turn::QueuedTurn> {
- let ctx = &DispatchCtx::ambient();
- let guard = in_flight().lock_owned().await;
+ let guard = in_flight(ctx).lock_owned().await;
match guard.get(&key_for(ctx, thread_id)) {
Some(entry) => entry.run_queue.drain(lane).await,
None => Vec::new(),
@@ -258,8 +258,8 @@ pub async fn drain_queued_turns_for_test(
/// Test accessor: `(request_id, thread_id)` for every in-flight parallel turn.
#[cfg(any(test, debug_assertions))]
-pub async fn parallel_in_flight_entries_for_test() -> Vec<(String, String)> {
- let guard = parallel_in_flight().lock_owned().await;
+pub async fn parallel_in_flight_entries_for_test(ctx: &DispatchCtx) -> Vec<(String, String)> {
+ let guard = parallel_in_flight(ctx).lock_owned().await;
guard
.iter()
.map(|(request_id, entry)| (request_id.clone(), entry.thread_id.clone()))
diff --git a/crates/openhuman-core/src/web_chat/session.rs b/crates/openhuman-core/src/web_chat/session.rs
index 8ef81a7d0d..dc3667c111 100644
--- a/crates/openhuman-core/src/web_chat/session.rs
+++ b/crates/openhuman-core/src/web_chat/session.rs
@@ -404,7 +404,7 @@ pub(crate) async fn checkout_session_agent(
let prior = if policy == CheckoutPolicy::Fork {
None
} else {
- let mut sessions = super::ops::thread_sessions().lock_owned().await;
+ let mut sessions = super::ops::thread_sessions(ctx).lock_owned().await;
sessions.remove(&map_key)
};
@@ -520,7 +520,7 @@ pub(crate) async fn checkin_session_agent(
agent: OpenHumanSessionHost,
fingerprint: SessionCacheFingerprint,
) {
- let mut sessions = super::ops::thread_sessions().lock_owned().await;
+ let mut sessions = super::ops::thread_sessions(ctx).lock_owned().await;
sessions.insert(
super::ops::key_for(ctx, thread_id),
SessionEntry { agent, fingerprint },
@@ -538,7 +538,7 @@ pub(crate) async fn checkin_session_agent_if_vacant(
agent: OpenHumanSessionHost,
fingerprint: SessionCacheFingerprint,
) -> bool {
- let mut sessions = super::ops::thread_sessions().lock_owned().await;
+ let mut sessions = super::ops::thread_sessions(ctx).lock_owned().await;
match sessions.entry(super::ops::key_for(ctx, thread_id)) {
std::collections::hash_map::Entry::Occupied(_) => {
log::info!(