Skip to content

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

refactor(browser): bound shared session pool in its own module

Steven Enamakel31ab89d56bfe
diff --git a/crates/openhuman-core/src/tools/impl/browser/browser.rs b/crates/openhuman-core/src/tools/impl/browser/browser.rs
index 952a502767..67b0744184 100644
--- a/crates/openhuman-core/src/tools/impl/browser/browser.rs
+++ b/crates/openhuman-core/src/tools/impl/browser/browser.rs
@@ -1,15 +1,26 @@
//! Agent-facing browser backed by the TinyBrowser module.
+#[path = "browser_session_pool.rs"]
+mod session_pool;
+
use crate::modules::browser::BrowserClient;
use crate::security::approval::{ApprovalGate, GateOutcome};
use crate::security::SecurityPolicy;
use async_trait::async_trait;
use serde_json::{json, Value};
+use session_pool::{
+ browser_session_fingerprint, evict_thread_sessions, requires_rebind, thread_sessions,
+ ThreadSession,
+};
+#[cfg(test)]
+use session_pool::{MAX_THREAD_SESSIONS, SESSION_IDLE_TTL};
use sha2::{Digest, Sha256};
use std::{
- collections::{BTreeMap, HashMap},
- sync::{Arc, Mutex as StdMutex, OnceLock},
- time::{Duration, Instant},
+ collections::BTreeMap,
+ sync::{Arc, Mutex as StdMutex},
+ time::Instant,
};
+#[cfg(test)]
+use std::{collections::HashMap, time::Duration};
use tinybrowser_bus::{
Action, DownloadState, DownloadWaitRequest, LocateBy, Locator, NavigateRequest, ReadRequest,
ScrollDirection, SessionId, SessionOptions, Snapshot, SnapshotRequest, Target, WaitState,
@@ -25,77 +36,6 @@ struct Pending {
token: String,
}
-const MAX_THREAD_SESSIONS: usize = 6;
-const SESSION_IDLE_TTL: Duration = Duration::from_secs(30 * 60);
-
-struct ThreadSession {
- id: SessionId,
- client: Arc<BrowserClient>,
- last_used: Instant,
- config_fingerprint: String,
- bound_origin: Option<String>,
-}
-
-static THREAD_SESSIONS: OnceLock<Mutex<HashMap<String, ThreadSession>>> = OnceLock::new();
-
-fn thread_sessions() -> &'static Mutex<HashMap<String, ThreadSession>> {
- THREAD_SESSIONS.get_or_init(|| Mutex::new(HashMap::new()))
-}
-
-fn browser_session_fingerprint(client: &BrowserClient) -> String {
- let config = client.config();
- // Include the complete browser settings so a newly assembled tool never
- // inherits a Chrome session opened with older profile, viewport, timeout,
- // download, or module settings. The shared web allowlist and override also
- // determine the module's allowed_origins at OPEN_SESSION time.
- let allow_all = matches!(
- std::env::var("OPENHUMAN_BROWSER_ALLOW_ALL").ok().as_deref(),
- Some("1" | "true" | "TRUE" | "yes" | "YES")
- );
- let bytes = serde_json::to_vec(&json!({
- "browser": &config.browser,
- "allowed_domains": &config.http_request.allowed_domains,
- "allow_all": allow_all,
- }))
- .expect("browser configuration is JSON serializable");
- format!("{:x}", Sha256::digest(bytes))
-}
-
-fn requires_rebind(bound: Option<&str>, requested: Option<&str>) -> bool {
- requested.is_some_and(|origin| bound != Some(origin))
-}
-
-fn evict_thread_sessions(
- sessions: &mut HashMap<String, ThreadSession>,
- now: Instant,
- reserve_slot: bool,
-) -> Vec<ThreadSession> {
- let mut removed = Vec::new();
- let expired = sessions
- .iter()
- .filter(|(_, value)| now.duration_since(value.last_used) >= SESSION_IDLE_TTL)
- .map(|(key, _)| key.clone())
- .collect::<Vec<_>>();
- for key in expired {
- if let Some(value) = sessions.remove(&key) {
- removed.push(value);
- }
- }
- while reserve_slot && sessions.len() >= MAX_THREAD_SESSIONS {
- let Some(oldest) = sessions
- .iter()
- .min_by_key(|(_, value)| value.last_used)
- .map(|(key, _)| key.clone())
- else {
- break;
- };
- if let Some(value) = sessions.remove(&oldest) {
- removed.push(value);
- }
- }
- removed
-}
-
impl Pending {
fn matches(&self, args: &Value) -> bool {
args["token"].as_str() == Some(self.token.as_str())
diff --git a/crates/openhuman-core/src/tools/impl/browser/browser_session_pool.rs b/crates/openhuman-core/src/tools/impl/browser/browser_session_pool.rs
new file mode 100644
index 0000000000..476221ec24
--- /dev/null
+++ b/crates/openhuman-core/src/tools/impl/browser/browser_session_pool.rs
@@ -0,0 +1,80 @@
+//! Shared browser sessions across turns in one conversation.
+
+use crate::modules::browser::BrowserClient;
+use serde_json::json;
+use sha2::{Digest, Sha256};
+use std::{
+ collections::HashMap,
+ sync::{Arc, OnceLock},
+ time::{Duration, Instant},
+};
+use tinybrowser_bus::SessionId;
+use tokio::sync::Mutex;
+
+pub(super) const MAX_THREAD_SESSIONS: usize = 6;
+pub(super) const SESSION_IDLE_TTL: Duration = Duration::from_secs(30 * 60);
+
+pub(super) struct ThreadSession {
+ pub(super) id: SessionId,
+ pub(super) client: Arc<BrowserClient>,
+ pub(super) last_used: Instant,
+ pub(super) config_fingerprint: String,
+ pub(super) bound_origin: Option<String>,
+}
+
+static THREAD_SESSIONS: OnceLock<Mutex<HashMap<String, ThreadSession>>> = OnceLock::new();
+
+pub(super) fn thread_sessions() -> &'static Mutex<HashMap<String, ThreadSession>> {
+ THREAD_SESSIONS.get_or_init(|| Mutex::new(HashMap::new()))
+}
+
+pub(super) fn browser_session_fingerprint(client: &BrowserClient) -> String {
+ let config = client.config();
+ // A session opened with older settings or website policy cannot be reused.
+ let allow_all = matches!(
+ std::env::var("OPENHUMAN_BROWSER_ALLOW_ALL").ok().as_deref(),
+ Some("1" | "true" | "TRUE" | "yes" | "YES")
+ );
+ let bytes = serde_json::to_vec(&json!({
+ "browser": &config.browser,
+ "allowed_domains": &config.http_request.allowed_domains,
+ "allow_all": allow_all,
+ }))
+ .expect("browser configuration is JSON serializable");
+ format!("{:x}", Sha256::digest(bytes))
+}
+
+pub(super) fn requires_rebind(bound: Option<&str>, requested: Option<&str>) -> bool {
+ requested.is_some_and(|origin| bound != Some(origin))
+}
+
+pub(super) fn evict_thread_sessions(
+ sessions: &mut HashMap<String, ThreadSession>,
+ now: Instant,
+ reserve_slot: bool,
+) -> Vec<ThreadSession> {
+ let mut removed = Vec::new();
+ let expired = sessions
+ .iter()
+ .filter(|(_, value)| now.duration_since(value.last_used) >= SESSION_IDLE_TTL)
+ .map(|(key, _)| key.clone())
+ .collect::<Vec<_>>();
+ for key in expired {
+ if let Some(value) = sessions.remove(&key) {
+ removed.push(value);
+ }
+ }
+ while reserve_slot && sessions.len() >= MAX_THREAD_SESSIONS {
+ let Some(oldest) = sessions
+ .iter()
+ .min_by_key(|(_, value)| value.last_used)
+ .map(|(key, _)| key.clone())
+ else {
+ break;
+ };
+ if let Some(value) = sessions.remove(&oldest) {
+ removed.push(value);
+ }
+ }
+ removed
+}
diff --git a/vendor/tinybrowser b/vendor/tinybrowser
index 005513d12d..19ee5671d7 160000
--- a/vendor/tinybrowser
+++ b/vendor/tinybrowser
@@ -1 +1 @@
-Subproject commit 005513d12df0a2c171f71cc20d71e45b471a0c3a
+Subproject commit 19ee5671d724f1cd0cff107eb0d684f146d42bd1