diff --git a/crates/openhuman-core/src/modules/computer_config.rs b/crates/openhuman-core/src/modules/computer_config.rs index 83d42bc8e7b..b19ceff8c56 100644 --- a/crates/openhuman-core/src/modules/computer_config.rs +++ b/crates/openhuman-core/src/modules/computer_config.rs @@ -169,6 +169,15 @@ fn create_trace_dir(dir: &Path) -> std::io::Result<()> { Ok(()) } +/// The absolute profile folder a persistent `[browser]` keeps, if any. +fn persistent_profile(config: &Config) -> Option<&str> { + let browser = &config.browser; + (browser.profile_mode == "persistent") + .then_some(browser.profile_path.as_deref()) + .flatten() + .filter(|path| Path::new(path).is_absolute()) +} + /// Build the module configuration for `config`. #[must_use] pub async fn module_config(config: &Config) -> Value { @@ -183,8 +192,18 @@ pub async fn module_config(config: &Config) -> Value { if let Some(captcha) = captcha(config).await { out.insert("captcha".into(), captcha); } + let mut browser = Map::new(); if let Some(executable) = config.browser.chrome_path.as_ref() { - out.insert("browser".into(), json!({ "executable": executable })); + browser.insert("executable".into(), json!(executable)); + } + // The persistent profile folder is host control, like the executable: + // TinyComputer's egress-enforced launcher lets a session name this + // folder and no other, so cookies and sign-ins survive between sessions. + if let Some(profile) = persistent_profile(config) { + browser.insert("profile".into(), json!(profile)); + } + if !browser.is_empty() { + out.insert("browser".into(), Value::Object(browser)); } let traced = tracing_enabled(config); if traced { @@ -201,6 +220,7 @@ pub async fn module_config(config: &Config) -> Value { decision_model = config.computer.decision_model.as_str(), jev = out.contains_key("jev"), planner = out.contains_key("planner"), + profile = persistent_profile(config).is_some(), captcha = out.contains_key("captcha"), hosted = hosted.is_some(), traced, diff --git a/crates/openhuman-core/src/modules/computer_config_tests.rs b/crates/openhuman-core/src/modules/computer_config_tests.rs index 092854f31f8..20f2ac1286c 100644 --- a/crates/openhuman-core/src/modules/computer_config_tests.rs +++ b/crates/openhuman-core/src/modules/computer_config_tests.rs @@ -141,6 +141,44 @@ async fn chrome_path_is_the_module_browser_executable() { ); } +/// A persistent profile reaches the module as the host's own profile folder, +/// the only one TinyComputer's egress-enforced launcher lets a session name, +/// so cookies and sign-ins survive between sessions. +#[tokio::test] +async fn a_persistent_profile_is_the_module_browser_profile() { + let dir = tempfile::tempdir().unwrap(); + let mut config = config_in(dir.path()); + config.browser.chrome_path = Some("/usr/bin/chromium".into()); + config.browser.profile_mode = "persistent".into(); + config.browser.profile_path = Some("/srv/teeny/state/browser/profile".into()); + let value = module_config(&config).await; + assert_eq!( + value["browser"], + json!({ + "executable": "/usr/bin/chromium", + "profile": "/srv/teeny/state/browser/profile" + }) + ); +} + +/// A fresh profile names no folder, and neither does a persistent mode whose +/// path is unset or relative, which `open_session` refuses anyway. +#[tokio::test] +async fn a_fresh_or_unusable_profile_sends_no_module_profile() { + let dir = tempfile::tempdir().unwrap(); + for (mode, path) in [ + ("fresh", Some("/srv/teeny/state/browser/profile")), + ("persistent", None), + ("persistent", Some("relative/profile")), + ] { + let mut config = config_in(dir.path()); + config.browser.profile_mode = mode.into(); + config.browser.profile_path = path.map(str::to_owned); + let value = module_config(&config).await; + assert!(value.get("browser").is_none(), "{mode} {path:?}"); + } +} + #[tokio::test] async fn a_trace_directory_that_cannot_be_made_still_leaves_the_module_configured() { let dir = tempfile::tempdir().unwrap();