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 tenant-scoped thread session invalidation

Steven Enamakel6e21a76f9e90
diff --git a/crates/openhuman-core/src/web_chat/mod.rs b/crates/openhuman-core/src/web_chat/mod.rs
index 9ab75a5ae0..0e59e09b90 100644
--- a/crates/openhuman-core/src/web_chat/mod.rs
+++ b/crates/openhuman-core/src/web_chat/mod.rs
@@ -84,6 +84,7 @@ pub use ops::{
in_flight_entries_for_test, invalidate_thread_sessions, run_system_turn_on_thread, start_chat,
StartChatError, SESSION_CHECKOUT_FAILURE, SYSTEM_CLIENT_ID,
};
+pub(crate) use ops::invalidate_thread_sessions_for_tenant;
pub use types::ChatRequestMetadata;
// Public API — schemas / controllers
diff --git a/crates/openhuman-core/src/web_chat/ops.rs b/crates/openhuman-core/src/web_chat/ops.rs
index a2ac56ab7f..110ec2b9c7 100644
--- a/crates/openhuman-core/src/web_chat/ops.rs
+++ b/crates/openhuman-core/src/web_chat/ops.rs
@@ -38,6 +38,7 @@ pub(super) use state::thread_sessions;
#[cfg(test)]
pub(crate) use state::track_parallel_turn_for_test;
pub use state::{cancel_should_target, in_flight_entries_for_test, invalidate_thread_sessions};
+pub(crate) use state::invalidate_thread_sessions_for_tenant;
pub(crate) use state::{event_session_id_for, key_for};
#[cfg(test)]
pub(crate) use state::{key_in, unscope_in};
diff --git a/crates/openhuman-core/src/web_chat/ops/state.rs b/crates/openhuman-core/src/web_chat/ops/state.rs
index b8f8f09994..a2d694dc18 100644
--- a/crates/openhuman-core/src/web_chat/ops/state.rs
+++ b/crates/openhuman-core/src/web_chat/ops/state.rs
@@ -156,6 +156,28 @@ pub async fn invalidate_thread_sessions(thread_id: &str) {
}
}
+/// Invalidate only the captured tenant, unless its channel session ended.
+///
+/// Call from the captured core context so the original session table is used.
+/// Cancellation while waiting for that table never mutates a replacement session.
+pub(crate) async fn invalidate_thread_sessions_for_tenant(
+ tenant: &crate::core::runtime::Tenant,
+ thread_id: &str,
+ cancellation: &tokio_util::sync::CancellationToken,
+) -> bool {
+ let table = thread_sessions();
+ let mut sessions = tokio::select! {
+ biased;
+ _ = cancellation.cancelled() => return false,
+ sessions = table.lock_owned() => sessions,
+ };
+ if cancellation.is_cancelled() {
+ return false;
+ }
+ sessions.remove(&key_in(tenant, thread_id));
+ true
+}
+
/// 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> {