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 cancel_all_turns to stop every live turn

Steven Enamakel5db819f720e4
diff --git a/crates/openhuman-core/src/web_chat/mod.rs b/crates/openhuman-core/src/web_chat/mod.rs
index dca219e6be..1bcc8a0e41 100644
--- a/crates/openhuman-core/src/web_chat/mod.rs
+++ b/crates/openhuman-core/src/web_chat/mod.rs
@@ -77,12 +77,14 @@ pub use ops::drain_queued_turns_for_test;
#[cfg(any(test, debug_assertions))]
pub use ops::parallel_in_flight_entries_for_test;
pub use ops::{
- cancel_chat, cancel_chat_scoped, cancel_should_target, channel_web_cancel, channel_web_chat,
+ cancel_all_turns, cancel_chat, cancel_chat_scoped, cancel_should_target, channel_web_cancel, channel_web_chat,
channel_web_queue_clear, channel_web_queue_remove, channel_web_queue_status,
in_flight_entries_for_test, invalidate_thread_sessions, run_system_turn_on_thread, start_chat,
StartChatError, SESSION_CHECKOUT_FAILURE, SYSTEM_CLIENT_ID,
};
pub use types::ChatRequestMetadata;
+#[cfg(test)]
+pub(crate) use ops::track_parallel_turn_for_test;
// Public API — schemas / controllers
pub use schemas::{
diff --git a/crates/openhuman-core/src/web_chat/ops.rs b/crates/openhuman-core/src/web_chat/ops.rs
index 74df8a8ddb..5477e6e4c4 100644
--- a/crates/openhuman-core/src/web_chat/ops.rs
+++ b/crates/openhuman-core/src/web_chat/ops.rs
@@ -19,7 +19,7 @@ pub(super) use budget_correlation::{
};
pub use channel_ops::{
- cancel_chat, cancel_chat_scoped, channel_web_cancel, channel_web_chat, channel_web_queue_clear,
+ cancel_all_turns, cancel_chat, cancel_chat_scoped, channel_web_cancel, channel_web_chat, channel_web_queue_clear,
channel_web_queue_remove, channel_web_queue_status,
};
@@ -34,6 +34,8 @@ pub(crate) use state::in_flight;
pub use state::parallel_in_flight_entries_for_test;
#[cfg(test)]
pub(crate) use state::scoped_key;
+#[cfg(test)]
+pub(crate) use state::track_parallel_turn_for_test;
pub(super) use state::thread_sessions;
pub use state::{cancel_should_target, in_flight_entries_for_test, invalidate_thread_sessions};
pub(crate) use state::{event_session_id_for, key_for};
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 003afaf8d9..56c3f2ac3d 100644
--- a/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
+++ b/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
@@ -41,6 +41,29 @@ pub async fn cancel_chat_scoped(
.request_id)
}
+/// Stop every turn running in the calling context — what a profile whose
+/// lease was lost does before it closes. Each thread is torn down as an
+/// unscoped stop would (primary, parallel turns and detached sub-agents).
+/// Returns how many threads had something to stop.
+pub async fn cancel_all_turns() -> usize {
+ let threads = super::state::live_thread_ids().await;
+ let mut stopped = 0;
+ for thread_id in &threads {
+ match cancel_chat_inner("profile-fence", thread_id, None).await {
+ Ok(outcome) if outcome.request_id.is_some() => stopped += 1,
+ Ok(_) => {}
+ Err(error) => {
+ log::warn!("[web-channel] stopping thread_id={thread_id} failed: {error}");
+ }
+ }
+ }
+ log::info!(
+ "[web-channel] cancel_all_turns threads={} stopped={stopped}",
+ threads.len()
+ );
+ stopped
+}
+
/// What one cancel tore down.
struct CancelOutcome {
/// The primary or parallel turn that was cancelled, if any.
diff --git a/crates/openhuman-core/src/web_chat/ops/state.rs b/crates/openhuman-core/src/web_chat/ops/state.rs
index 4f827c9042..c8381fa0cd 100644
--- a/crates/openhuman-core/src/web_chat/ops/state.rs
+++ b/crates/openhuman-core/src/web_chat/ops/state.rs
@@ -156,6 +156,47 @@ pub async fn invalidate_thread_sessions(thread_id: &str) {
}
}
+/// 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() -> Vec<String> {
+ let mut threads: Vec<String> = in_flight()
+ .lock_owned()
+ .await
+ .keys()
+ .map(|key| unscope(key))
+ .collect();
+ threads.extend(
+ parallel_in_flight()
+ .lock_owned()
+ .await
+ .values()
+ .map(|entry| entry.thread_id.clone()),
+ );
+ threads.sort();
+ threads.dedup();
+ threads
+}
+
+/// Test seam: track `cancel` as a parallel turn on `thread_id` in the
+/// calling context's tables, so callers can watch a teardown reach it.
+#[cfg(test)]
+pub(crate) async fn track_parallel_turn_for_test(
+ thread_id: &str,
+ request_id: &str,
+ cancel: tokio_util::sync::CancellationToken,
+) {
+ let watched = cancel.clone();
+ let handle = tokio::spawn(async move { watched.cancelled().await });
+ parallel_in_flight().lock_owned().await.insert(
+ request_id.to_string(),
+ ParallelEntry {
+ thread_id: thread_id.to_string(),
+ handle,
+ cancel_token: cancel,
+ },
+ );
+}
+
pub async fn in_flight_entries_for_test() -> Vec<(String, String)> {
let guard = in_flight().lock_owned().await;
guard