Skip to content

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

test(saas): cover per-user isolation of chat event streams

Steven Enamakelbe0d35f9127a
diff --git a/tests/saas_mode_e2e.rs b/tests/saas_mode_e2e.rs
index 89d3256fd7..d42dc2f802 100644
--- a/tests/saas_mode_e2e.rs
+++ b/tests/saas_mode_e2e.rs
@@ -509,3 +509,125 @@ fn each_user_sees_only_their_own_threads() {
assert!(body.get("error").is_some(), "{body}");
drop(server);
}
+
+/// Open `/events?client_id=` for `user` and forward each SSE `data:` line.
+fn user_events(base: &str, user: &str, client_id: &str) -> std::sync::mpsc::Receiver<String> {
+ use openhuman_core::user_agents::gateway::{sign, USER_HEADER, USER_SIG_HEADER};
+ use std::io::BufRead;
+ let (tx, rx) = std::sync::mpsc::channel();
+ let url = format!("{base}/events?client_id={client_id}");
+ let user = user.to_string();
+ std::thread::spawn(move || {
+ let client = reqwest::blocking::Client::builder()
+ .timeout(None)
+ .build()
+ .unwrap();
+ let Ok(response) = client
+ .get(&url)
+ .bearer_auth(BEARER)
+ .header(USER_HEADER, &user)
+ .header(USER_SIG_HEADER, sign(BEARER, &user, now()))
+ .send()
+ else {
+ return;
+ };
+ let _ = tx.send(format!("status:{}", response.status().as_u16()));
+ for line in std::io::BufReader::new(response).lines() {
+ let Ok(line) = line else { break };
+ if let Some(data) = line.strip_prefix("data:") {
+ if tx.send(data.trim().to_string()).is_err() {
+ break;
+ }
+ }
+ }
+ });
+ rx
+}
+
+#[test]
+fn chat_events_reach_only_the_user_whose_turn_produced_them() {
+ let d = deployment(true);
+ // Point the backend at a closed port so the turn fails fast — the failure
+ // is itself an event on the owner's stream, without any real inference.
+ let port = free_port();
+ let child = core_command(&d, &["--port", &port.to_string()])
+ .env("BACKEND_URL", "http://127.0.0.1:9")
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
+ .spawn()
+ .expect("spawn openhuman-core");
+ let server = Server(child);
+ let base = format!("http://127.0.0.1:{port}");
+ let client = reqwest::blocking::Client::builder()
+ .timeout(Duration::from_secs(10))
+ .build()
+ .unwrap();
+ let deadline = Instant::now() + Duration::from_secs(120);
+ while !client
+ .get(format!("{base}/health"))
+ .send()
+ .is_ok_and(|r| r.status().is_success())
+ {
+ assert!(Instant::now() < deadline, "SaaS core never became healthy");
+ std::thread::sleep(Duration::from_millis(250));
+ }
+ provision(&client, &base, "alice");
+ provision(&client, &base, "bob");
+
+ // The operator has no chat stream.
+ let status = client
+ .get(format!("{base}/events?client_id=c1"))
+ .bearer_auth(BEARER)
+ .send()
+ .unwrap()
+ .status()
+ .as_u16();
+ assert_eq!(status, 404);
+
+ // Both users listen on the same client id.
+ let alice_events = user_events(&base, "alice", "c1");
+ let bob_events = user_events(&base, "bob", "c1");
+ for (who, rx) in [("alice", &alice_events), ("bob", &bob_events)] {
+ let first = rx.recv_timeout(Duration::from_secs(10)).unwrap();
+ assert_eq!(first, "status:200", "{who}'s stream opens");
+ }
+
+ let (status, body) = user_rpc_with(
+ &client,
+ &base,
+ BEARER,
+ "alice",
+ None,
+ "openhuman.channel_web_chat",
+ json!({ "client_id": "c1", "thread_id": "chat-1", "message": "hello" }),
+ );
+ assert_eq!(status, 200, "{body}");
+
+ let mut alice_got = Vec::new();
+ let until = Instant::now() + Duration::from_secs(30);
+ while Instant::now() < until {
+ match alice_events.recv_timeout(Duration::from_millis(500)) {
+ Ok(data) if data.contains("chat-1") => {
+ alice_got.push(data);
+ if alice_got.iter().any(|e| e.contains("error") || e.contains("done")) {
+ break;
+ }
+ }
+ Ok(_) | Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {}
+ Err(e) => panic!("alice's stream closed: {e}"),
+ }
+ }
+ assert!(!alice_got.is_empty(), "alice receives her turn's events");
+ assert!(
+ alice_got.iter().all(|e| !e.contains("\"agent\"")),
+ "the routing stamp is not on the wire: {alice_got:?}"
+ );
+
+ std::thread::sleep(Duration::from_secs(1));
+ let leaked: Vec<String> = bob_events.try_iter().collect();
+ assert!(
+ leaked.is_empty(),
+ "bob shares the client id but must see none of alice's events: {leaked:?}"
+ );
+ drop(server);
+}