The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(browser): add profile claim support for browser tasks
Steven Enamakel5d4ae3c51b82
diff --git a/crates/openhuman-core/src/modules/browser.rs b/crates/openhuman-core/src/modules/browser.rsindex 3141163f99..eebdc3ded0 100644--- a/crates/openhuman-core/src/modules/browser.rs+++ b/crates/openhuman-core/src/modules/browser.rs@@ -205,7 +205,7 @@ impl BrowserClient {None};let claimed =- claim_profile(&mut options, super::browser_profile::SESSION_PROFILE_WAIT).await;+ claim_profile(&self.config, &mut options, super::profile_claim::SESSION_WAIT_MS).await;options.download_dir = browser.download_dir.clone();options.default_timeout_ms = browser.task_timeout_secs.saturating_mul(1000);options.allowed_origins =@@ -214,7 +214,7 @@ impl BrowserClient {// An open session's Chrome holds the profile itself; one that did// not open holds nothing.if let Some(profile) = claimed {- super::browser_profile::release(&profile);+ super::profile_claim::release(&self.config, &profile).await;}opened}@@ -495,18 +495,21 @@ fn parsed_browser_url(raw: &str) -> Result<url::Url, BrowserCallError> {/// a session in one conversation found the profile held by another/// conversation's browser, or a task's.async fn claim_profile(+ config: &Config,options: &mut SessionOptions,- wait: std::time::Duration,-) -> Option<std::path::PathBuf> {- let profile = std::path::PathBuf::from(options.user_data_dir.as_ref()?);- if super::browser_profile::claim_within(&profile, wait).await {- return Some(profile);- }- tracing::warn!(- "[browser] another browser holds the persistent profile; this session uses a fresh profile"- );- options.user_data_dir = None;- None+ wait_ms: u64,+) -> Option<String> {+ let profile = options.user_data_dir.clone()?;+ match super::profile_claim::claim(config, &profile, wait_ms).await {+ super::profile_claim::Claim::Claimed(path) => Some(path),+ super::profile_claim::Claim::Fresh => {+ tracing::warn!(+ "[browser] another browser holds the persistent profile; this session uses a fresh profile"+ );+ options.user_data_dir = None;+ None+ }+ }}fn browser_allow_all() -> bool {diff --git a/crates/openhuman-core/src/modules/browser_task.rs b/crates/openhuman-core/src/modules/browser_task.rsindex 943be21f95..ee9c716402 100644--- a/crates/openhuman-core/src/modules/browser_task.rs+++ b/crates/openhuman-core/src/modules/browser_task.rs@@ -119,7 +119,7 @@ pub(crate) async fn begin<'a>(return Err("a browser task needs at least one allowed origin".to_owned());}let mut request = start_request(config, task);- let claimed = claim_profile(&mut request, super::browser_profile::PROFILE_WAIT).await;+ let claimed = claim_profile(config, &mut request, super::profile_claim::TASK_WAIT_MS).await;let mut following = super::browser_sites::apply(config, task, &mut request).await;tracing::debug!(origins = task.origins.len(),@@ -146,7 +146,7 @@ pub(crate) async fn begin<'a>(Err(error) => {// A task that did not start starts no browser on the profile.if let Some(profile) = claimed {- super::browser_profile::release(&profile);+ super::profile_claim::release(config, &profile).await;}return Err(error);}@@ -303,16 +303,22 @@ fn refused_flow(error: &str) -> bool {/// folder, so a task waits a moment for one closing on it (the browser of the/// task before it, which closes as it ends), and otherwise runs in a fresh/// profile rather than fail to start its browser.-async fn claim_profile(request: &mut StartTaskRequest, wait: Duration) -> Option<PathBuf> {- let profile = PathBuf::from(request.constraints.browser_profile.as_ref()?);- if super::browser_profile::claim_within(&profile, wait).await {- return Some(profile);+async fn claim_profile(+ config: &Config,+ request: &mut StartTaskRequest,+ wait_ms: u64,+) -> Option<String> {+ let profile = request.constraints.browser_profile.clone()?;+ match super::profile_claim::claim(config, &profile, wait_ms).await {+ super::profile_claim::Claim::Claimed(path) => Some(path),+ super::profile_claim::Claim::Fresh => {+ tracing::warn!(+ "[browser-task] another browser holds the persistent profile; this task runs in a fresh profile"+ );+ request.constraints.browser_profile = None;+ None+ }}- tracing::warn!(- "[browser-task] another browser holds the persistent profile; this task runs in a fresh profile"- );- request.constraints.browser_profile = None;- None}/// A module reply as its data, or as the error the tool reports: its code,diff --git a/crates/openhuman-core/src/modules/profile_claim.rs b/crates/openhuman-core/src/modules/profile_claim.rsnew file mode 100644index 0000000000..0a4dd2c793--- /dev/null+++ b/crates/openhuman-core/src/modules/profile_claim.rs@@ -0,0 +1,96 @@+//! Claims the persistent Chrome profile folder for a browser about to start.+//!+//! Which profile to use (persistent or fresh) and its folder are host policy,+//! read from Browser Control settings and sent with each claim. How Chrome+//! locks a folder, the wait for a closing browser and the claim bookkeeping+//! belong to TinyComputer (`BrowserClaimProfile` / `BrowserReleaseProfile`);+//! this adapter only asks and reports which outcome the module reached.++use serde_json::Value;+use tinycomputer_bus::browser::{+ names::methods, ClaimProfileRequest, ClaimProfileResponse, ProfileClaim, ReleaseProfileRequest,+};+use tinycomputer_bus::DesktopResponse;++use crate::config::Config;++/// How long a task waits, in milliseconds, for the Chrome holding its+/// persistent profile to let it go before it runs in a fresh profile.+pub(crate) const TASK_WAIT_MS: u64 = 5_000;++/// How long a browser session waits for the same: shorter than a task's, as+/// a person waits on it.+pub(crate) const SESSION_WAIT_MS: u64 = 1_000;++/// What a claim came to, for the caller to apply to its browser launch.+#[derive(Debug, Clone, PartialEq, Eq)]+pub(crate) enum Claim {+ /// The folder is claimed: start the browser on it and release the claim+ /// if the browser does not start.+ Claimed(String),+ /// The folder is held by another browser (or the module could not be+ /// asked): start with a fresh profile.+ Fresh,+}++/// The request for `path`. The host runs a fresh profile by leaving the path+/// out, so the module is never asked to make a folder it would leak.+pub(crate) fn claim_request(path: &str, wait_ms: u64) -> ClaimProfileRequest {+ ClaimProfileRequest {+ path: path.to_owned(),+ wait_ms,+ fall_back_to_fresh: false,+ }+}++/// Reads the module's answer to a claim of `requested`.+pub(crate) fn settle(requested: &str, response: &ClaimProfileResponse) -> Claim {+ match (response.outcome, response.path.as_deref()) {+ (ProfileClaim::Claimed, path) => Claim::Claimed(path.unwrap_or(requested).to_owned()),+ (ProfileClaim::Busy | ProfileClaim::FellBackToFresh, _) => Claim::Fresh,+ }+}++/// Claims `path`, waiting up to `wait_ms` for a Chrome that is closing on it.+/// A module that cannot be asked leaves the folder unclaimed and the browser+/// on a fresh profile, as a held folder does.+pub(crate) async fn claim(config: &Config, path: &str, wait_ms: u64) -> Claim {+ let reply = call(config, methods::CLAIM_PROFILE, &claim_request(path, wait_ms)).await;+ let Some(data) = reply else {+ tracing::warn!("[profile-claim] the module could not claim the profile; using a fresh one");+ return Claim::Fresh;+ };+ match serde_json::from_value::<ClaimProfileResponse>(data) {+ Ok(response) => {+ let claim = settle(path, &response);+ tracing::debug!(outcome = ?response.outcome, wait_ms, "[profile-claim] claimed");+ claim+ }+ Err(error) => {+ tracing::warn!(%error, "[profile-claim] unreadable claim reply; using a fresh profile");+ Claim::Fresh+ }+ }+}++/// Gives up a claim: the browser did not start, or holds the folder itself.+pub(crate) async fn release(config: &Config, path: &str) {+ let request = ReleaseProfileRequest {+ path: path.to_owned(),+ };+ let _ = call(config, methods::RELEASE_PROFILE, &request).await;+}++async fn call<A: serde::Serialize + Sync>(config: &Config, member: &str, request: &A) -> Option<Value> {+ let proxy = super::desktop::proxy(config).await.ok()?;+ let response: DesktopResponse = proxy.call(member, (request,)).await.ok()?;+ if !response.ok {+ tracing::debug!(member, "[profile-claim] the module refused the call");+ return None;+ }+ Some(response.data.unwrap_or(Value::Null))+}++#[cfg(test)]+#[path = "profile_claim_tests.rs"]+mod tests;diff --git a/crates/openhuman-core/src/modules/profile_claim_tests.rs b/crates/openhuman-core/src/modules/profile_claim_tests.rsnew file mode 100644index 0000000000..1f9878e98e--- /dev/null+++ b/crates/openhuman-core/src/modules/profile_claim_tests.rs@@ -0,0 +1,39 @@+use super::*;++fn answer(outcome: ProfileClaim, path: Option<&str>) -> ClaimProfileResponse {+ ClaimProfileResponse {+ outcome,+ path: path.map(str::to_owned),+ }+}++#[test]+fn waits_match_the_task_and_session_policy() {+ assert_eq!(TASK_WAIT_MS, 5_000);+ assert_eq!(SESSION_WAIT_MS, 1_000);+}++#[test]+fn the_request_carries_the_configured_path_and_never_asks_for_a_fresh_folder() {+ let request = claim_request("/home/asha/.openhuman/chrome", TASK_WAIT_MS);+ assert_eq!(request.path, "/home/asha/.openhuman/chrome");+ assert_eq!(request.wait_ms, 5_000);+ assert!(!request.fall_back_to_fresh);+}++#[test]+fn a_claimed_folder_is_kept_and_a_held_one_means_a_fresh_profile() {+ assert_eq!(+ settle("/p", &answer(ProfileClaim::Claimed, Some("/p"))),+ Claim::Claimed("/p".into())+ );+ assert_eq!(+ settle("/p", &answer(ProfileClaim::Claimed, None)),+ Claim::Claimed("/p".into())+ );+ assert_eq!(settle("/p", &answer(ProfileClaim::Busy, None)), Claim::Fresh);+ assert_eq!(+ settle("/p", &answer(ProfileClaim::FellBackToFresh, Some("/tmp/x"))),+ Claim::Fresh+ );+}