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(cli): unselect a direct cua do target that stops answering (#4868)
injaneity97ef165a7b50
diff --git a/libs/cua/crates/cua-cli/src/do_cmd.rs b/libs/cua/crates/cua-cli/src/do_cmd.rsindex d0734be718..06fea7da89 100644--- a/libs/cua/crates/cua-cli/src/do_cmd.rs+++ b/libs/cua/crates/cua-cli/src/do_cmd.rs@@ -16,6 +16,7 @@ use crate::{};use clap::{Args, Subcommand};use cua_sdk::{Cua, CuaError, SandboxCreateOptions};+use cua_spaces::registry::Registry;use serde::{Deserialize, Serialize};use std::{io::Write, path::PathBuf, sync::Arc};@@ -574,7 +575,9 @@ pub async fn run(cua: &Arc<Cua>, args: DoArgs, out: &mut dyn Write) -> Result<i3Err(Exit(code, msg)) => {if !msg.is_empty() {eprintln!("❌ {msg}");- util::line(out, r.state.context());+ if r.state.provider.is_some() {+ util::line(out, r.state.context());+ }}Ok(code)}@@ -737,10 +740,47 @@ impl Run<'_> {Ok(0)}- async fn computer(&self) -> Result<Computer, CuaError> {- Ok(Computer::new(- crate::sandbox::env_of(self.cua, &self.state.name).await?,- ))+ async fn computer(&mut self) -> Result<Computer, CuaError> {+ let name = self.state.name.clone();+ let err = match crate::sandbox::env_of(self.cua, &name).await {+ Ok(env) => return Ok(Computer::new(env)),+ Err(e @ CuaError::SpacesdNotAvailable(_))+ if crate::sandbox::relay_space_id(&name).is_none() =>+ {+ e+ }+ Err(e) => return Err(e),+ };+ let Some(url) = self+ .cua+ .sandboxes()+ .get(name.clone())+ .await+ .ok()+ .filter(|i| i.location == "direct")+ .and_then(|i| i.endpoints.get("env").cloned())+ else {+ return Err(err);+ };+ if State::load().name == name {+ std::fs::remove_file(state_path()).map_err(internal)?;+ self.state.provider = None;+ }+ let next = match Registry::new(util::cua_home())+ .list()+ .unwrap_or_default()+ .into_iter()+ .find_map(|s| crate::sandbox::relay_space_id(&s.id))+ {+ Some(id) => format!("Registered relay Space: cua do switch {id}"),+ None => "List Spaces: cua spaces ls".into(),+ };+ Err(CuaError::SpacesdNotAvailable(format!(+ "{name} ({url}) did not answer; it may have moved or gone offline. Unselected it.\n \+ Select it again once it is back: cua do switch {name}\n \+ Forget it: cua sandbox rm {name}\n \+ {next}"+ )))}async fn zoom_ref(&self, c: &Computer) -> Option<cua_spacesd_client::pb::WindowRef> {diff --git a/libs/cua/crates/cua-cli/tests/sandbox_do.rs b/libs/cua/crates/cua-cli/tests/sandbox_do.rsindex b921ed06b9..cc4149e8f0 100644--- a/libs/cua/crates/cua-cli/tests/sandbox_do.rs+++ b/libs/cua/crates/cua-cli/tests/sandbox_do.rs@@ -495,6 +495,79 @@ async fn host_target_needs_consent() {h.run(&["--embedded", "do", "key", "tab"]).await.ok();}+fn closed_port() -> u16 {+ let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();+ let port = listener.local_addr().unwrap().port();+ drop(listener);+ port+}++async fn select_closed(h: &Home) -> String {+ let url = format!("127.0.0.1:{}", closed_port());+ h.run(&[+ "--embedded",+ "do",+ "switch",+ "url",+ &url,+ "--token",+ "t",+ "--as",+ "gone",+ ])+ .await+ .ok();+ url+}++#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn do_unselects_a_direct_target_that_stops_answering() {+ let h = Home::new();+ let url = select_closed(&h).await;+ let o = h.run(&["--embedded", "do", "screenshot"]).await;+ assert_eq!(o.code, 1, "{o:?}");+ assert!(o.stderr.contains(&format!("gone (http://{url})")), "{o:?}");+ assert!(o.stderr.contains("cua do switch gone"), "{o:?}");+ assert!(o.stderr.contains("cua sandbox rm gone"), "{o:?}");+ assert!(o.stderr.contains("List Spaces: cua spaces ls"), "{o:?}");+ assert!(!o.stderr.contains("retry in a few seconds"), "{o:?}");+ assert!(!o.stdout.contains('💻'), "{o:?}");+ let o = h.run(&["--embedded", "do", "status"]).await;+ assert_eq!(o.code, 1, "{o:?}");+ assert!(o.stderr.contains("No target selected"), "{o:?}");+ let listed = h.run(&["--embedded", "do", "ls", "direct"]).await;+ listed.ok();+ assert!(+ listed.stdout.contains(" gone [") && listed.stdout.contains("direct"),+ "{listed:?}"+ );+ assert_eq!(+ read_json(&h.cua_home().join("env-tokens.json"))["gone"],+ "t"+ );+ h.run(&["--embedded", "do", "switch", "gone"]).await.ok();+}++#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn do_names_a_registered_relay_space_without_touching_the_registry() {+ let h = Home::new();+ std::fs::create_dir_all(h.cua_home()).unwrap();+ let spaces = h.cua_home().join("spaces.json");+ let body = r#"[{"id":"direct:10.9.8.7:3211","name":"other","features":[]},{"id":"relay:a30ad1ef1e4fea09","name":"desk","features":[]}]"#;+ std::fs::write(&spaces, body).unwrap();+ select_closed(&h).await;+ let before = std::fs::read(&spaces).unwrap();+ let o = h.run(&["--embedded", "do", "screenshot"]).await;+ assert_eq!(o.code, 1, "{o:?}");+ assert!(+ o.stderr.contains("cua do switch relay:a30ad1ef1e4fea09"),+ "{o:?}"+ );+ assert!(!o.stderr.contains("direct:10.9.8.7:3211"), "{o:?}");+ assert!(!o.stderr.contains("List Spaces:"), "{o:?}");+ assert_eq!(std::fs::read(&spaces).unwrap(), before);+}+/// cua-spacesd's `/mcp` requires the Space token: `sb mcp` against a/// direct sandbox sends the one registered with `--token` (it used to send/// none and got 401).@@ -886,6 +959,21 @@ async fn do_drives_a_relay_space() {o.ok();assert!(o.stdout.starts_with("✅ relay-ok"), "{o:?}");+ let target = std::fs::read(h.cua_home().join("do_target.json")).unwrap();+ relay.tunnel(MACHINE, &format!("http://127.0.0.1:{}", closed_port()));+ let o = h.run(&["--embedded", "do", "screenshot"]).await;+ assert_eq!(o.code, 1, "{o:?}");+ assert!(+ o.stderr.contains("has no cua-spacesd") && o.stderr.contains("502 Bad Gateway"),+ "{o:?}"+ );+ assert!(!o.stderr.contains("Unselected"), "{o:?}");+ assert!(o.stdout.contains("💻 relay:"), "{o:?}");+ assert_eq!(+ std::fs::read(h.cua_home().join("do_target.json")).unwrap(),+ target+ );+cua_host::RelayClient::new(&relay.url).unwrap().stop_sharing("relay-token", MACHINE)