Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(cua): open a direct Space added by address with its registered token (#5077)
injaneityc54378228377
diff --git a/libs/cua/crates/cua-cli/src/sandbox.rs b/libs/cua/crates/cua-cli/src/sandbox.rsindex 1f6d04861d..b1d931ef1e 100644--- a/libs/cua/crates/cua-cli/src/sandbox.rs+++ b/libs/cua/crates/cua-cli/src/sandbox.rs@@ -2118,6 +2118,14 @@ fn direct_token(info: &SandboxInfo) -> Option<String> {.iter().find_map(|k| tokens.get(k.as_str()).and_then(|v| v.as_str())).map(str::to_string)+ .or_else(|| {+ cua_spaces::registry::Registry::new(util::cua_home())+ .credential(&info.id)+ .ok()+ .flatten()+ .filter(|c| c.service_urls.is_empty())+ .and_then(|c| c.token)+ }).or_else(|| {std::env::var("CUA_ENV_TOKEN").ok()diff --git a/libs/cua/crates/cua-cli/tests/sandbox_do.rs b/libs/cua/crates/cua-cli/tests/sandbox_do.rsindex 73c286bb4a..62450db6c3 100644--- a/libs/cua/crates/cua-cli/tests/sandbox_do.rs+++ b/libs/cua/crates/cua-cli/tests/sandbox_do.rs@@ -603,6 +603,114 @@ async fn sandbox_mcp_sends_the_direct_token() {assert!(o.stderr.contains("401"), "{o:?}");}+const SPACE_TOKEN: &str = "registry-only-5073-tok";++async fn spacesd_with_token() -> (cua_spacesd_client::testing::MockServer, String) {+ use cua_spacesd_client::testing::{MockAuth, MockServer};+ let server = MockServer::start(MockAuth {+ token: Some(SPACE_TOKEN.into()),+ ..Default::default()+ })+ .await;+ let id = format!("direct:{}", server.url().trim_start_matches("http://"));+ (server, id)+}++fn add_space(h: &Home, id: &str, credential: cua_spaces::registry::Credential) {+ let space = cua_proto::daemon::v1::Space {+ id: id.into(),+ ..Default::default()+ };+ cua_spaces::registry::Registry::new(h.cua_home())+ .upsert(space, credential)+ .unwrap();+}++async fn view(h: &Home, id: &str) -> Out {+ h.run(&["--embedded", "--json", "sb", "view", id, "--no-open"])+ .await+}++fn files(dir: &std::path::Path) -> Vec<std::path::PathBuf> {+ std::fs::read_dir(dir)+ .unwrap()+ .flat_map(|e| {+ let p = e.unwrap().path();+ if p.is_dir() { files(&p) } else { vec![p] }+ })+ .collect()+}++#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn sandbox_view_sends_the_spacesd_token_of_a_space_added_by_address() {+ use cua_spaces::registry::Credential;+ let (server, id) = spacesd_with_token().await;+ let mut h = Home::new();+ add_space(+ &h,+ &id,+ Credential {+ url: Some(server.url()),+ token: Some(SPACE_TOKEN.into()),+ ..Default::default()+ },+ );+ let o = view(&h, &id).await;+ o.ok();+ assert!(+ o.json()["url"]+ .as_str()+ .unwrap()+ .contains("#ticket=mock-viewer"),+ "{o:?}"+ );+ h.set("CUA_ENV_TOKEN", "wrong");+ view(&h, &id).await.ok();++ let creds = h.cua_home().join("spaces-credentials.json");+ for f in files(&h.cua_home()) {+ if f != creds {+ let body = String::from_utf8_lossy(&std::fs::read(&f).unwrap()).into_owned();+ assert!(!body.contains(SPACE_TOKEN), "{}", f.display());+ }+ }+ #[cfg(unix)]+ {+ use std::os::unix::fs::PermissionsExt;+ let mode = std::fs::metadata(&creds).unwrap().permissions().mode();+ assert_eq!(mode & 0o777, 0o600);+ }+ h.env.remove("CUA_ENV_TOKEN");++ let (server, _) = spacesd_with_token().await;+ let url = server.url().trim_start_matches("http://").to_string();+ direct_with(&h, &url, "own-token", SPACE_TOKEN).await;+ add_space(+ &h,+ &format!("direct:{url}"),+ Credential {+ token: Some("wrong".into()),+ ..Default::default()+ },+ );+ view(&h, "own-token").await.ok();++ let (mcp_server, mcp_id) = spacesd_with_token().await;+ add_space(+ &h,+ &mcp_id,+ Credential {+ token: Some(SPACE_TOKEN.into()),+ service_urls: [("mcp".into(), format!("{}/mcp", mcp_server.url()))].into(),+ ..Default::default()+ },+ );+ assert_ne!(view(&h, &mcp_id).await.code, 0);++ let (_unregistered, unregistered_id) = spacesd_with_token().await;+ assert_ne!(view(&h, &unregistered_id).await.code, 0);+}+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]async fn sandbox_mcp_tools_call_and_content() {use cua_sandbox_core::testing::{McpTestServer, expected_result, png_bytes};diff --git a/libs/cua/crates/cua-daemon/src/server.rs b/libs/cua/crates/cua-daemon/src/server.rsindex e149f59058..a8b6928100 100644--- a/libs/cua/crates/cua-daemon/src/server.rs+++ b/libs/cua/crates/cua-daemon/src/server.rs@@ -649,6 +649,8 @@ impl Svc {let spaces = self.rt().spaces();let is_space = name.starts_with("space://")|| name.starts_with("relay:")+ || (name.starts_with("direct:")+ && matches!(spaces.registry().get(name), Ok(Some(_))))|| (self.rt().record(name).await.is_err() && spaces.resolve(name).is_ok());if is_space {let space = spaces.space(name).await?;diff --git a/libs/cua/crates/cua-daemon/tests/direct_space_media_bridge.rs b/libs/cua/crates/cua-daemon/tests/direct_space_media_bridge.rsnew file mode 100644index 0000000000..7811b770a4--- /dev/null+++ b/libs/cua/crates/cua-daemon/tests/direct_space_media_bridge.rs@@ -0,0 +1,71 @@+use cua_daemon::client::{DaemonAddress, DaemonClient};+use cua_daemon::server::{ServerConfig, start};+use cua_daemon::{Runtime as DaemonRuntime, RuntimeConfig};+use cua_spacesd_client::testing::{MockAuth, MockServer};+use std::path::{Path, PathBuf};+use std::time::Duration;++const TOKEN: &str = "direct-space-5073-tok";++fn files(dir: &Path) -> Vec<PathBuf> {+ std::fs::read_dir(dir)+ .unwrap()+ .flat_map(|e| {+ let p = e.unwrap().path();+ if p.is_dir() { files(&p) } else { vec![p] }+ })+ .collect()+}++#[tokio::test]+async fn a_media_bridge_to_a_direct_space_sends_its_registered_token() {+ let dir = tempfile::tempdir().unwrap();+ let runtime = DaemonRuntime::new(RuntimeConfig {+ state_dir: Some(dir.path().join("sandboxes")),+ spaces_home: Some(dir.path().to_path_buf()),+ ..Default::default()+ })+ .unwrap();+ let handle = start(+ runtime.clone(),+ ServerConfig {+ socket_path: None,+ loopback: Some(([127, 0, 0, 1], 0).into()),+ token: "t0k3n".into(),+ discovery_path: None,+ bridge_ticket_ttl: Duration::from_secs(60),+ },+ )+ .await+ .unwrap();+ let client = DaemonClient::new(DaemonAddress::Url {+ url: handle.loopback_url.clone().unwrap(),+ token: handle.token.clone(),+ })+ .unwrap();+ let env = MockServer::start(MockAuth {+ token: Some(TOKEN.into()),+ ..Default::default()+ })+ .await;+ let space = runtime+ .spaces()+ .add(&env.url(), Some(TOKEN.into()), None)+ .await+ .unwrap();+ assert!(space.id.starts_with("direct:"), "{}", space.id);++ let bridge = client.open_media_bridge(&space.id, None).await.unwrap();+ assert!(bridge.ws_url.starts_with("ws://"), "{}", bridge.ws_url);++ for f in files(dir.path()) {+ if f.file_name().unwrap() != "spaces-credentials.json" {+ let body = std::fs::read(&f).unwrap();+ assert!(+ !String::from_utf8_lossy(&body).contains(TOKEN),+ "{}",+ f.display()+ );+ }+ }+}