Skip to content

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

fix(web_chat): make scoped session keys injective

Steven Enamakel5b3a843e01b6
diff --git a/crates/openhuman-core/src/web_chat/ops/state.rs b/crates/openhuman-core/src/web_chat/ops/state.rs
index 6f09ddf92d..76b6e32e69 100644
--- a/crates/openhuman-core/src/web_chat/ops/state.rs
+++ b/crates/openhuman-core/src/web_chat/ops/state.rs
@@ -39,13 +39,33 @@ pub(crate) fn key_for(thread_id: &str) -> String {
scoped_key(agent.as_deref(), thread_id)
}
+/// Injective encoding of `(session_agent, thread_id)`. Both parts are caller
+/// controlled, so a plain `agent::thread` join would let `("a", "b::c")` and
+/// `("a::b", "c")` (or an unscoped `"a::b"`) share one slot. A scoped key is
+/// `\x1f<agent byte length>:<agent><thread>`; an unscoped key is the bare
+/// thread id, except that an id starting with `\x1f` gets a second `\x1f`
+/// prepended so it can never look like a scoped key.
pub(crate) fn scoped_key(session_agent: Option<&str>, thread_id: &str) -> String {
match session_agent {
- Some(agent) => format!("{agent}::{thread_id}"),
+ Some(agent) => format!("\u{1f}{}:{agent}{thread_id}", agent.len()),
+ None if thread_id.starts_with('\u{1f}') => format!("\u{1f}{thread_id}"),
None => thread_id.to_string(),
}
}
+/// The thread id a [`scoped_key`] was built from, ignoring its agent scope.
+fn thread_id_of_key(key: &str) -> Option<&str> {
+ let Some(rest) = key.strip_prefix('\u{1f}') else {
+ return Some(key);
+ };
+ if rest.starts_with('\u{1f}') {
+ return Some(rest);
+ }
+ let (len, tail) = rest.split_once(':')?;
+ let len: usize = len.parse().ok()?;
+ tail.get(len..)
+}
+
pub(crate) fn event_session_id_for(client_id: &str, thread_id: &str) -> String {
json!({
"client_id": client_id,
@@ -83,7 +103,7 @@ pub async fn invalidate_thread_sessions(thread_id: &str) {
let mut sessions = THREAD_SESSIONS.lock().await;
let keys_to_remove: Vec<String> = sessions
.keys()
- .filter(|k| k.as_str() == thread_id || k.ends_with(&format!("::{thread_id}")))
+ .filter(|k| thread_id_of_key(k) == Some(thread_id))
.cloned()
.collect();
for key in &keys_to_remove {
diff --git a/crates/openhuman-core/src/web_chat/session_checkout_tests.rs b/crates/openhuman-core/src/web_chat/session_checkout_tests.rs
index 068c8c88a6..ff24fa6d94 100644
--- a/crates/openhuman-core/src/web_chat/session_checkout_tests.rs
+++ b/crates/openhuman-core/src/web_chat/session_checkout_tests.rs
@@ -567,8 +567,14 @@ async fn two_agents_with_the_same_thread_id_get_their_own_cache_slots() {
};
let key_a = CoreContext::scope(agent_ctx("asha"), async { key_for(&thread_id) }).await;
let key_b = CoreContext::scope(agent_ctx("ravi"), async { key_for(&thread_id) }).await;
- assert_eq!(key_a, format!("asha::{thread_id}"));
- assert_eq!(key_b, format!("ravi::{thread_id}"));
+ assert_ne!(key_a, key_b);
+ assert_ne!(key_a, thread_id);
+ // Delimiter-looking input cannot forge another scope's key.
+ use crate::web_chat::ops::scoped_key;
+ assert_ne!(scoped_key(Some("a"), "b::c"), scoped_key(Some("a::b"), "c"));
+ assert_ne!(scoped_key(None, "a::b"), scoped_key(Some("a"), "b"));
+ assert_ne!(scoped_key(None, "\u{1f}1:ab"), scoped_key(Some("a"), "b"));
+ assert_ne!(scoped_key(Some("a"), "bc"), scoped_key(Some("ab"), "c"));
// Evicting the thread clears every agent's slot for it.
{