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 cached sessions by agent and workspace

Steven Enamakel8878f5298fd3
diff --git a/crates/openhuman-core/src/web_chat/ops/state.rs b/crates/openhuman-core/src/web_chat/ops/state.rs
index 3f055a8e23..6f09ddf92d 100644
--- a/crates/openhuman-core/src/web_chat/ops/state.rs
+++ b/crates/openhuman-core/src/web_chat/ops/state.rs
@@ -24,8 +24,26 @@ pub(crate) static IN_FLIGHT: Lazy<Mutex<HashMap<String, InFlightEntry>>> =
pub(crate) static PARALLEL_IN_FLIGHT: Lazy<Mutex<HashMap<String, ParallelEntry>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
+/// The map key for `thread_id` in the calling scope.
+///
+/// Thread ids are chosen by callers and are only unique per agent, so a turn
+/// running for an embedded agent ([`CoreContext::session_agent`]) is keyed
+/// `<agent>::<thread>`. Two agents that pick the same thread id then get two
+/// cache entries and two in-flight slots instead of sharing one live session.
+/// Outside an agent scope the key stays the bare thread id.
+///
+/// [`CoreContext::session_agent`]: crate::core::runtime::CoreContext::session_agent
pub(crate) fn key_for(thread_id: &str) -> String {
- thread_id.to_string()
+ let agent = crate::core::runtime::CoreContext::current()
+ .and_then(|context| context.session_agent().map(str::to_owned));
+ scoped_key(agent.as_deref(), thread_id)
+}
+
+pub(crate) fn scoped_key(session_agent: Option<&str>, thread_id: &str) -> String {
+ match session_agent {
+ Some(agent) => format!("{agent}::{thread_id}"),
+ None => thread_id.to_string(),
+ }
}
pub(crate) fn event_session_id_for(client_id: &str, thread_id: &str) -> String {
diff --git a/crates/openhuman-core/src/web_chat/session.rs b/crates/openhuman-core/src/web_chat/session.rs
index 57e637fcf1..114ee9cb20 100644
--- a/crates/openhuman-core/src/web_chat/session.rs
+++ b/crates/openhuman-core/src/web_chat/session.rs
@@ -241,6 +241,13 @@ pub(super) fn fingerprint_diff(
&next.autonomy_signature,
));
}
+ if prior.workspace_dir != next.workspace_dir {
+ diff.push(format!(
+ "workspace_dir: {} -> {}",
+ prior.workspace_dir.display(),
+ next.workspace_dir.display()
+ ));
+ }
if prior.model_registry_signature != next.model_registry_signature {
diff.push(describe_signature_change(
"model_registry_signature",
@@ -265,6 +272,7 @@ pub(super) fn build_session_fingerprint(
target_agent_id,
autonomy_signature: autonomy_signature(config),
model_registry_signature: model_registry_signature(config),
+ workspace_dir: config.workspace_dir.clone(),
}
}
@@ -335,8 +343,12 @@ pub(crate) async fn checkout_session_agent(
};
let (agent, fingerprint) = match prior {
+ // `AdoptCached` takes the thread's agent whatever settings it was
+ // built with, but never one built for another workspace.
Some(entry)
- if entry.fingerprint == fingerprint || policy == CheckoutPolicy::AdoptCached =>
+ if entry.fingerprint == fingerprint
+ || (policy == CheckoutPolicy::AdoptCached
+ && entry.fingerprint.workspace_dir == fingerprint.workspace_dir) =>
{
log::info!(
"[web-channel] reusing cached session agent id={} for client={} thread={}",
diff --git a/crates/openhuman-core/src/web_chat/types.rs b/crates/openhuman-core/src/web_chat/types.rs
index 14d5fe0515..e8996c19ba 100644
--- a/crates/openhuman-core/src/web_chat/types.rs
+++ b/crates/openhuman-core/src/web_chat/types.rs
@@ -69,6 +69,12 @@ pub(crate) struct SessionCacheFingerprint {
/// change) — without this the stale session would be reused. Mirrors
/// [`Self::autonomy_signature`].
pub(super) model_registry_signature: String,
+ /// The workspace the agent was built against. A cached agent carries its
+ /// workspace's transcripts, memory binding and identity; reusing it after
+ /// the workspace changed (a different user signed in, or another embedded
+ /// agent asked for the same thread id) would hand one owner another's
+ /// conversation.
+ pub(super) workspace_dir: std::path::PathBuf,
}
pub(super) struct SessionEntry {