Skip to content

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

refactor(modules): move Chrome profile locking into the module

Steven Enamakel3e4b747b2184
diff --git a/crates/openhuman-core/src/modules/README.md b/crates/openhuman-core/src/modules/README.md
index 8fe4639fea..3abebf9b76 100644
--- a/crates/openhuman-core/src/modules/README.md
+++ b/crates/openhuman-core/src/modules/README.md
@@ -304,7 +304,7 @@ download, its `dlopen`, or its resident memory.
| [`computer.rs`](computer.rs) | `tinycomputer` | `status` for the Computer settings page: lifecycle state, configured routes, and optionally what the module reports through `Describe`. |
| [`browser.rs`](browser.rs) | `tinycomputer` | `BrowserClient` for the `Browser*` members, over the desktop proxy, plus the shared website policy: "Allow all" (the list's `*`) lets the browser open any public site, private and local addresses still refused. |
| [`browser_task.rs`](browser_task.rs) | `tinycomputer` | Browser tasks over `StartTask` / `AwaitTask` / `ContinueTask`: `start`, `wait`, `resume`, `cancel`, `report`. The host keeps the policy (surfaces, origins, action budget, approvals) and sends Browser Control's Chrome path, and in persistent mode its profile folder, with every task, read from the settings each time. A refusal or failure that says Chrome is missing, or that its path names no Chrome, tells the agent where the user sets that path. |
-| [`browser_profile.rs`](browser_profile.rs) | `tinycomputer` | Whether a running Chrome holds the persistent profile folder, by the lock Chrome keeps there (`SingletonLock`, or `lockfile` on Windows). Chrome lets one browser at a time use a profile, so a task waits up to 5 s for one closing on it, then runs in a fresh profile. A browser of this process claims the folder before it starts, as Chrome locks it only once started, so two starting together never both take it. |
+| [`profile_claim.rs`](profile_claim.rs) | `tinycomputer` | Host adapter over `BrowserClaimProfile` / `BrowserReleaseProfile`: sends the configured persistent profile folder with the wait (5 s for a task, 1 s for a session) and applies the outcome, a claimed folder or a fresh profile. Chrome's lock detection, the wait and the claim bookkeeping live in the module. |
| [`browser_task_report.rs`](browser_task_report.rs) | `tinycomputer` | What is kept when a task stops: a content-free log summary, and with tracing on the full report under `<workspace>/state/computer/tasks/`. |
| [`browser_sites.rs`](browser_sites.rs), [`browser_sites/store.rs`](browser_sites/store.rs) | `tinycomputer` | Per-site memory of plans and elements learned by finished tasks, in `<workspace>/state/computer/sites/<site>.json`, with 30-day expiry. Fed back on `StartTask`; never reaches the agent's memory or prompts. |
| [`connectors.rs`](connectors.rs) | `tinyconnectors` | `proxy`, `call`, `call_stateless`, `call_bare`, `reconcile_route_if_loaded`. Builds the route configuration (signed-in backend or the user's own Composio key) and reconciles it on every call so sign-out reaches the module. |
diff --git a/crates/openhuman-core/src/modules/browser_profile.rs b/crates/openhuman-core/src/modules/browser_profile.rs
deleted file mode 100644
index 58413b6abf..0000000000
--- a/crates/openhuman-core/src/modules/browser_profile.rs
+++ /dev/null
@@ -1,131 +0,0 @@
-//! Whether a running Chrome holds a profile folder, and claiming one for a
-//! browser about to start.
-//!
-//! Chrome lets one browser at a time use a profile folder: a second one
-//! started on it hands its window to the first and exits, so a browser task
-//! or session given the persistent profile while another Chrome holds it
-//! could not start its browser. A task waits up to [`PROFILE_WAIT`] for a
-//! Chrome that is closing (the one the task before it closes as it ends), a
-//! session up to [`SESSION_PROFILE_WAIT`], and otherwise each runs in a fresh
-//! profile.
-//!
-//! Chrome takes the folder's lock only once it has started, so two browsers
-//! starting together would each find the folder free. A browser of this
-//! process therefore claims the folder first ([`claim_within`]), and the
-//! claim stands until its Chrome holds the folder itself. Claims are this
-//! process's own; between processes, Chrome's lock is the only arbiter.
-
-use std::collections::HashMap;
-use std::path::{Path, PathBuf};
-use std::sync::{LazyLock, Mutex, PoisonError};
-use std::time::{Duration, Instant};
-
-/// How long a task waits for the Chrome holding its persistent profile to
-/// let it go before it runs in a fresh profile instead.
-pub(crate) const PROFILE_WAIT: Duration = Duration::from_secs(5);
-
-/// How long a browser session waits for the same, before it opens in a
-/// fresh profile: shorter than a task's, as a person waits on it, and the
-/// Chrome holding the profile is most often another conversation's, which
-/// does not close.
-pub(crate) const SESSION_PROFILE_WAIT: Duration = Duration::from_secs(1);
-
-/// How often the profile is looked at while waiting.
-const PROFILE_POLL: Duration = Duration::from_millis(250);
-
-/// How long a claim stands while its Chrome has not taken the folder's lock:
-/// well past the time a browser takes to start. A session's Chrome starts
-/// before its open returns, a task's as the task starts, beside its planning.
-const CLAIM_GRACE: Duration = Duration::from_secs(30);
-
-/// The profile folders browsers of this process may still be starting on,
-/// with when each was claimed.
-static CLAIMS: LazyLock<Mutex<HashMap<PathBuf, Instant>>> = LazyLock::new(Mutex::default);
-
-/// Whether a running Chrome holds the profile folder `path`, by the lock it
-/// keeps there: on macOS and Linux a `SingletonLock` link naming the host and
-/// the process holding it, on Windows a `lockfile` held open. A link naming
-/// another host is taken as held, as Chrome takes it; one naming a process
-/// that is gone is a lock left by a crash, and free.
-pub(crate) fn in_use(path: &Path) -> bool {
- #[cfg(windows)]
- {
- let lock = path.join("lockfile");
- lock.exists() && std::fs::OpenOptions::new().write(true).open(&lock).is_err()
- }
- #[cfg(not(windows))]
- {
- let Ok(target) = std::fs::read_link(path.join("SingletonLock")) else {
- return false;
- };
- held_by(&target.to_string_lossy())
- }
-}
-
-/// Whether a `SingletonLock` link's target, `<host>-<pid>`, names a live
-/// process on this host, or another host.
-#[cfg(not(windows))]
-fn held_by(target: &str) -> bool {
- let Some((host, pid)) = target.rsplit_once('-') else {
- return false;
- };
- let Ok(pid) = pid.parse::<u32>() else {
- return false;
- };
- if sysinfo::System::host_name().is_some_and(|here| here != host) {
- return true;
- }
- let pid = sysinfo::Pid::from_u32(pid);
- let mut system = sysinfo::System::new();
- system.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true);
- system.process(pid).is_some()
-}
-
-/// Claims the profile folder `path` for a browser about to start, waiting up
-/// to `wait` for no Chrome, and no other browser of this process starting on
-/// it, to hold it; whether it was claimed. The claim stands until that
-/// browser's Chrome holds the folder, [`release`], or [`CLAIM_GRACE`] passes.
-pub(crate) async fn claim_within(path: &Path, wait: Duration) -> bool {
- let deadline = tokio::time::Instant::now() + wait;
- loop {
- if claim(path) {
- return true;
- }
- if tokio::time::Instant::now() >= deadline {
- return false;
- }
- tokio::time::sleep(PROFILE_POLL).await;
- }
-}
-
-/// Claims `path` when neither a Chrome nor a claim holds it; whether it did.
-fn claim(path: &Path) -> bool {
- let mut claims = CLAIMS.lock().unwrap_or_else(PoisonError::into_inner);
- if in_use(path) {
- // Chrome's own lock guards the folder now, so a claim made for that
- // Chrome has done its work.
- claims.remove(path);
- return false;
- }
- if claims
- .get(path)
- .is_some_and(|claimed| claimed.elapsed() < CLAIM_GRACE)
- {
- return false;
- }
- claims.insert(path.to_path_buf(), Instant::now());
- true
-}
-
-/// Gives up the claim on `path`: its browser did not start, or has started
-/// and holds the folder itself.
-pub(crate) fn release(path: &Path) {
- CLAIMS
- .lock()
- .unwrap_or_else(PoisonError::into_inner)
- .remove(path);
-}
-
-#[cfg(test)]
-#[path = "browser_profile_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-core/src/modules/browser_profile_tests.rs b/crates/openhuman-core/src/modules/browser_profile_tests.rs
deleted file mode 100644
index 2d44da12f0..0000000000
--- a/crates/openhuman-core/src/modules/browser_profile_tests.rs
+++ /dev/null
@@ -1,123 +0,0 @@
-use super::*;
-
-/// A fresh profile folder under the system temporary directory.
-fn folder(name: &str) -> std::path::PathBuf {
- let path = std::env::temp_dir().join(format!(
- "openhuman-browser-profile-{}-{name}",
- std::process::id()
- ));
- let _ = std::fs::remove_dir_all(&path);
- std::fs::create_dir_all(&path).unwrap();
- path
-}
-
-#[test]
-fn a_folder_no_chrome_has_locked_is_free() {
- let path = folder("unlocked");
- assert!(!in_use(&path));
- assert!(!in_use(&path.join("missing")));
- let _ = std::fs::remove_dir_all(&path);
-}
-
-#[cfg(not(windows))]
-#[test]
-fn a_lock_naming_a_live_process_here_holds_the_folder() {
- let here = sysinfo::System::host_name().unwrap_or_default();
- let path = folder("live");
- std::os::unix::fs::symlink(
- format!("{here}-{}", std::process::id()),
- path.join("SingletonLock"),
- )
- .unwrap();
- assert!(in_use(&path));
- let _ = std::fs::remove_dir_all(&path);
-}
-
-#[cfg(not(windows))]
-#[test]
-fn a_lock_a_crashed_chrome_left_behind_is_free_and_another_hosts_is_held() {
- let here = sysinfo::System::host_name().unwrap_or_default();
- // No process has this id: the lock outlived its Chrome.
- let path = folder("stale");
- std::os::unix::fs::symlink(format!("{here}-4294967"), path.join("SingletonLock")).unwrap();
- assert!(!in_use(&path));
- let _ = std::fs::remove_dir_all(&path);
-
- let path = folder("elsewhere");
- std::os::unix::fs::symlink(
- format!("not-{here}-{}", std::process::id()),
- path.join("SingletonLock"),
- )
- .unwrap();
- assert!(in_use(&path), "a profile another computer holds is held");
- let _ = std::fs::remove_dir_all(&path);
-
- // A link with no process in it is no lock Chrome wrote.
- assert!(!held_by("no-number-here"));
- assert!(!held_by("nodash"));
-}
-
-#[cfg(not(windows))]
-#[tokio::test]
-async fn waiting_ends_as_soon_as_the_folder_is_let_go() {
- let here = sysinfo::System::host_name().unwrap_or_default();
- let path = folder("closing");
- let lock = path.join("SingletonLock");
- std::os::unix::fs::symlink(format!("{here}-{}", std::process::id()), &lock).unwrap();
- assert!(!claim_within(&path, Duration::from_millis(300)).await);
- let letting_go = {
- let lock = lock.clone();
- tokio::spawn(async move {
- tokio::time::sleep(Duration::from_millis(300)).await;
- std::fs::remove_file(lock).unwrap();
- })
- };
- assert!(claim_within(&path, PROFILE_WAIT).await);
- letting_go.await.unwrap();
- release(&path);
- let _ = std::fs::remove_dir_all(&path);
-}
-
-#[tokio::test]
-async fn a_folder_claimed_for_a_starting_browser_is_held_until_released() {
- let path = folder("claimed");
- assert!(claim_within(&path, Duration::ZERO).await);
- assert!(
- !claim_within(&path, Duration::from_millis(50)).await,
- "a browser still starting on it holds it"
- );
- release(&path);
- assert!(claim_within(&path, Duration::ZERO).await);
- release(&path);
- let _ = std::fs::remove_dir_all(&path);
-}
-
-#[tokio::test]
-async fn of_two_browsers_starting_together_one_claims_the_folder() {
- // Both look before either Chrome has locked the folder.
- let path = folder("together");
- let (first, second) = tokio::join!(
- claim_within(&path, Duration::from_millis(50)),
- claim_within(&path, Duration::from_millis(50)),
- );
- assert!(first ^ second, "first {first}, second {second}");
- release(&path);
- let _ = std::fs::remove_dir_all(&path);
-}
-
-#[cfg(not(windows))]
-#[tokio::test]
-async fn a_claim_ends_when_its_chrome_holds_the_folder() {
- let here = sysinfo::System::host_name().unwrap_or_default();
- let path = folder("taken-up");
- assert!(claim_within(&path, Duration::ZERO).await);
- // Its Chrome has started and locked the folder.
- let lock = path.join("SingletonLock");
- std::os::unix::fs::symlink(format!("{here}-{}", std::process::id()), &lock).unwrap();
- assert!(!claim_within(&path, Duration::ZERO).await);
- // That Chrome has closed: nothing of the claim is left to hold the folder.
- std::fs::remove_file(&lock).unwrap();
- assert!(claim_within(&path, Duration::ZERO).await);
- release(&path);
- let _ = std::fs::remove_dir_all(&path);
-}
diff --git a/crates/openhuman-core/src/modules/mod.rs b/crates/openhuman-core/src/modules/mod.rs
index cf63fe6fec..7a5cdc2de2 100644
--- a/crates/openhuman-core/src/modules/mod.rs
+++ b/crates/openhuman-core/src/modules/mod.rs
@@ -31,8 +31,8 @@
//! - [`browser`] and [`browser_task`] — TinyComputer browser members and tasks
//! without linking the browser engine into the core; [`browser_sites`] keeps
//! what finished tasks learned about each site for the next task there, and
-//! [`browser_profile`] tells whether a Chrome holds the persistent profile
-//! and claims it for a browser about to start.
+//! [`profile_claim`] asks the module to claim the persistent Chrome profile
+//! for a browser about to start.
//! - [`wallet`] — the host half of the `tinywallet` module, which builds and
//! assembles transactions while the signing key stays in this process.
//! - [`linked`] — the registry of modules linked into the executable (local development).
@@ -47,7 +47,7 @@ pub mod boot;
#[cfg(feature = "modules")]
pub mod browser;
#[cfg(feature = "modules")]
-pub(crate) mod browser_profile;
+pub(crate) mod profile_claim;
#[cfg(feature = "modules")]
pub(crate) mod browser_sites;
#[cfg(feature = "modules")]
diff --git a/scripts/__tests__/storage-bypass.test.mjs b/scripts/__tests__/storage-bypass.test.mjs
index ec7fee3d6f..b04dbadd25 100644
--- a/scripts/__tests__/storage-bypass.test.mjs
+++ b/scripts/__tests__/storage-bypass.test.mjs
@@ -229,13 +229,3 @@ test("a verbatim copy of an allowlisted line is still a finding", () => {
);
});
-test("Chrome's Windows lock probe is not persisted state; additional writes still fail", () => {
- const file = `${SRC}/modules/browser_profile.rs`;
- const probe =
- "lock.exists() && std::fs::OpenOptions::new().write(true).open(&lock).is_err()";
- const { status, out } = run({ [file]: probe }, []);
- assert.equal(status, 0, out);
- const extra = run({ [file]: `${probe}\nstd::fs::write(path, data);` }, []);
- assert.equal(extra.status, 1, extra.out);
- assert.match(extra.out, /New storage-bypass sites/);
-});
diff --git a/scripts/ci/check-storage-bypass.mjs b/scripts/ci/check-storage-bypass.mjs
index 7182f6fd53..85541fe0a8 100755
--- a/scripts/ci/check-storage-bypass.mjs
+++ b/scripts/ci/check-storage-bypass.mjs
@@ -67,12 +67,6 @@ const LEGACY_READER =
*/
export const ALLOW = new Map(
[
- [
- "modules/browser_profile.rs",
- "json-write",
- "lock.exists() && std::fs::OpenOptions::new().write(true).open(&lock).is_err()",
- "probes Chrome's machine-local Windows profile lock for sharing violations; opens an existing file without creating, truncating or writing persisted state",
- ],
[
"security/approval/store_import.rs",
"sqlite-open",