Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cli): fail fast on a daemon share without a terminal; test shared relay Spaces (#5017)

Francesco Bonacci5c74ac36b357
diff --git a/docs/content/docs/cua-cli/reference/cli/spaces.mdx b/docs/content/docs/cua-cli/reference/cli/spaces.mdx
index ac5d326859..b3572e039a 100644
--- a/docs/content/docs/cua-cli/reference/cli/spaces.mdx
+++ b/docs/content/docs/cua-cli/reference/cli/spaces.mdx
@@ -255,10 +255,10 @@ cua spaces share [OPTIONS] <SPACE> <WHO>
| `<SPACE>` | string | required | Space id, address or display name. |
| `<WHO>` | string | required | Their email (verified by cua.ai) or account id. |
-| Flag | Type | Default | Description |
-| --- | --- | --- | --- |
-| <span id="cua-spaces-share--role"></span>`--role` | `viewer` \| `editor` | `viewer` | `viewer`: presence and a view-only stream. `editor`: full use. |
-| <span id="cua-spaces-share--yes"></span>`--yes` | boolean | `false` | Confirm without a prompt (needed without a terminal when no daemon runs; a running daemon asks with Touch ID or your password instead). |
+| Flag | Short | Type | Default | Description |
+| --- | --- | --- | --- | --- |
+| <span id="cua-spaces-share--role"></span>`--role` | | `viewer` \| `editor` | `viewer` | `viewer`: presence and a view-only stream. `editor`: full use. |
+| <span id="cua-spaces-share--yes"></span>`--yes` | `-y` | boolean | `false` | Confirm without a terminal prompt. Without a daemon the share happens at once. A running daemon still asks with Touch ID or your password on this computer; without a terminal, `--yes` says someone will answer there (otherwise the command fails at once instead of waiting for a prompt nobody sees). |
**Examples**
@@ -266,6 +266,9 @@ cua spaces share [OPTIONS] <SPACE> <WHO>
cua spaces share local:studio [email protected]
cua spaces share local:studio [email protected] --role editor
cua spaces share relay:0123abcd4567ef89 acct-42 --role viewer
+
+# From a script or agent: confirm with Touch ID or your password here
+cua spaces share local:studio [email protected] --yes
```
## `cua spaces unshare`
diff --git a/libs/cua/crates/cua-cli/src/host.rs b/libs/cua/crates/cua-cli/src/host.rs
index e46ccb696c..07fa9dad80 100644
--- a/libs/cua/crates/cua-cli/src/host.rs
+++ b/libs/cua/crates/cua-cli/src/host.rs
@@ -365,7 +365,9 @@ pub enum SpacesCmd {
#[command(after_help = "Examples:
cua spaces share local:studio [email protected]
cua spaces share local:studio [email protected] --role editor
- cua spaces share relay:0123abcd4567ef89 acct-42 --role viewer")]
+ cua spaces share relay:0123abcd4567ef89 acct-42 --role viewer
+ # From a script or agent: confirm with Touch ID or your password here
+ cua spaces share local:studio [email protected] --yes")]
Share {
/// Space id, address or display name.
space: String,
@@ -374,10 +376,12 @@ pub enum SpacesCmd {
/// `viewer`: presence and a view-only stream. `editor`: full use.
#[arg(long, default_value = "viewer", value_parser = ["viewer", "editor"])]
role: String,
- /// Confirm without a prompt (needed without a terminal when no
- /// daemon runs; a running daemon asks with Touch ID or your
- /// password instead).
- #[arg(long)]
+ /// Confirm without a terminal prompt. Without a daemon the share
+ /// happens at once. A running daemon still asks with Touch ID or
+ /// your password on this computer; without a terminal, `--yes`
+ /// says someone will answer there (otherwise the command fails
+ /// at once instead of waiting for a prompt nobody sees).
+ #[arg(long, short = 'y')]
yes: bool,
},
/// Stop sharing a Space with one account at once, or with everyone.
@@ -430,6 +434,24 @@ pub enum SpacesCmd {
},
}
+/// Checks a `cua spaces share` before it goes to a running daemon. The
+/// daemon confirms every share with user presence (Touch ID or the login
+/// password on this computer) and `--yes` cannot skip that. Without a
+/// terminal the caller is likely a script or agent with nobody at the
+/// screen, and the OS prompt would wait about two minutes before it
+/// fails, so this fails at once unless `--yes` says someone will answer.
+pub fn daemon_share_gate(yes: bool, terminal: bool) -> std::result::Result<(), CuaError> {
+ if yes || terminal {
+ return Ok(());
+ }
+ Err(CuaError::InvalidArgument(
+ "not shared: stdin is not a terminal, and the running cua daemon confirms every share \
+ with Touch ID or your password on this computer. Pass --yes (-y) when someone there \
+ will confirm it; sharing through the daemon cannot skip that confirmation"
+ .into(),
+ ))
+}
+
/// Confirms a share on the terminal (the `cua` CLI without a daemon).
pub struct TerminalConsent {
/// `--yes`: confirmed up front.
@@ -2303,6 +2325,20 @@ mod tests {
);
}
assert!(TerminalConsent { yes: true }.confirm("x").is_ok());
+ match parse_spaces(&["share", "s", "[email protected]", "-y"]).unwrap() {
+ SpacesCmd::Share { yes, .. } => assert!(yes),
+ other => panic!("{other:?}"),
+ }
+ }
+
+ #[test]
+ fn daemon_share_fails_fast_without_a_terminal_or_yes() {
+ let err = daemon_share_gate(false, false).unwrap_err().to_string();
+ assert!(err.contains("stdin is not a terminal"), "{err}");
+ assert!(err.contains("--yes (-y)"), "{err}");
+ assert!(daemon_share_gate(true, false).is_ok());
+ assert!(daemon_share_gate(false, true).is_ok());
+ assert!(daemon_share_gate(true, true).is_ok());
}
#[test]
diff --git a/libs/cua/crates/cua-cli/src/lib.rs b/libs/cua/crates/cua-cli/src/lib.rs
index c946454d2b..ee4e2ddc83 100644
--- a/libs/cua/crates/cua-cli/src/lib.rs
+++ b/libs/cua/crates/cua-cli/src/lib.rs
@@ -1758,12 +1758,17 @@ async fn run(cli: Cli, out: &mut dyn Write) -> Result<i32, CuaError> {
// already has it; an embedded runtime gets it here), and
// an embedded runtime confirms on the terminal.
host::attach_relay_account(&cua, || host::daemon_relay_account(&util::cua_home()));
- if let Some(rt) = cua.embedded_runtime()
- && !rt.spaces().has_share_consent()
- {
- let yes = matches!(cmd, host::SpacesCmd::Share { yes: true, .. });
- rt.spaces()
- .set_share_consent(Some(Arc::new(host::TerminalConsent { yes })));
+ let yes = matches!(cmd, host::SpacesCmd::Share { yes: true, .. });
+ if let Some(rt) = cua.embedded_runtime() {
+ if !rt.spaces().has_share_consent() {
+ rt.spaces()
+ .set_share_consent(Some(Arc::new(host::TerminalConsent { yes })));
+ }
+ } else if matches!(cmd, host::SpacesCmd::Share { .. }) {
+ // The daemon asks for user presence on this computer.
+ use std::io::IsTerminal as _;
+ host::daemon_share_gate(yes, std::io::stdin().is_terminal())?;
+ eprintln!("Confirm the share on this computer (Touch ID or your password).");
}
}
return host::run_spaces_sdk(&cua, cmd, json, out).await;
diff --git a/libs/cua/crates/cua-cli/tests/mcp_skills_images.rs b/libs/cua/crates/cua-cli/tests/mcp_skills_images.rs
index 658c857f00..dc7feacb62 100644
--- a/libs/cua/crates/cua-cli/tests/mcp_skills_images.rs
+++ b/libs/cua/crates/cua-cli/tests/mcp_skills_images.rs
@@ -628,3 +628,99 @@ async fn fleet_image_resources() {
let o = h.run(&["image", "info", "desktop"]).await;
assert_eq!(o.code, 3, "{o:?}");
}
+
+/// A Space another account shared through the relay (CUA-1418): the guest
+/// drives `relay:<id>` with the `computer_*` MCP tools and `cua do`, which
+/// resolve it through the Spaces API, not the sandboxes API ("not supported
+/// by the Direct provider" before).
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn a_shared_relay_space_is_driven_by_the_guest() {
+ use cua_host::testing::FakeRelay;
+ const MACHINE: &str = "5a7ed0e1a5bce111";
+ let relay = FakeRelay::start().await;
+ relay.add_account("owner-token", "owner-1", Some("[email protected]"));
+ relay.add_account("guest-token", "guest-1", Some("[email protected]"));
+ let owner = cua_host::RelayClient::new(&relay.url).unwrap();
+ owner
+ .register(
+ "owner-token",
+ &cua_host::relay::RegisterRequest {
+ id: MACHINE.into(),
+ name: "Owner desk".into(),
+ ..Default::default()
+ },
+ )
+ .await
+ .unwrap();
+ owner
+ .patch(
+ "owner-token",
+ MACHINE,
+ &cua_host::relay::MachinePatch {
+ allow: Some(vec!["[email protected]".into()]),
+ ..Default::default()
+ },
+ )
+ .await
+ .unwrap();
+ let env = fixtures::start_env(None, None).await;
+ relay.tunnel(MACHINE, &env.url);
+ relay.set_online(MACHINE, true, "0.0.0-mock");
+
+ // Signed in as the guest.
+ let mut h = Home::new();
+ h.set("CUA_RELAY_URL", &relay.url);
+ h.set("DOCKER_HOST", "unix:///nonexistent/cua-test-no-docker.sock");
+ std::fs::create_dir_all(h.cua_home()).unwrap();
+ std::fs::write(
+ h.cua_home().join("credentials.json"),
+ json!({
+ "access_token": "guest-token",
+ "expires_at": "2999-01-01T00:00:00Z",
+ "token_type": "Bearer"
+ })
+ .to_string(),
+ )
+ .unwrap();
+ let id = format!("relay:{MACHINE}");
+
+ let mut m = Mcp::start(&h, &["--embedded", "mcp"]);
+ m.call("initialize", json!({"protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "t", "version": "0"}})).await;
+ m.notify("notifications/initialized").await;
+ let r = m.tool("computer_screenshot", json!({"sandbox": id})).await;
+ assert_eq!(r["content"][0]["type"], "image", "{r}");
+ let r = m
+ .tool("computer_click", json!({"sandbox": id, "x": 600, "y": 300}))
+ .await;
+ assert_eq!(r["isError"], false, "{r}");
+ let ptr = env
+ .mock
+ .state
+ .observed
+ .pointer
+ .lock()
+ .unwrap()
+ .last()
+ .cloned()
+ .unwrap();
+ assert!(ptr.contains("x: 640.0, y: 320.0"), "{ptr}");
+ let r = m
+ .tool(
+ "computer_type",
+ json!({"sandbox": id, "text": "guest-typed"}),
+ )
+ .await;
+ assert_eq!(r["isError"], false, "{r}");
+ let kb = env.mock.state.observed.keyboard.lock().unwrap().clone();
+ assert!(kb.iter().any(|k| k.contains("guest-typed")), "{kb:?}");
+ m.close().await;
+
+ let o = h.run(&["--embedded", "do", "switch", &id]).await;
+ o.ok();
+ assert!(o.stdout.contains(&format!("✅ Switched to {id}")), "{o:?}");
+ let o = h
+ .run(&["--embedded", "do", "shell", "echo", "guest-ok"])
+ .await;
+ o.ok();
+ assert!(o.stdout.starts_with("✅ guest-ok"), "{o:?}");
+}
diff --git a/libs/cua/crates/cua-cli/tests/spaces_share_daemon.rs b/libs/cua/crates/cua-cli/tests/spaces_share_daemon.rs
new file mode 100644
index 0000000000..f5e6b5d05f
--- /dev/null
+++ b/libs/cua/crates/cua-cli/tests/spaces_share_daemon.rs
@@ -0,0 +1,39 @@
+//! `cua spaces share` through a running daemon without a terminal fails at
+//! once (CUA-1419). The daemon confirms every share with Touch ID or the
+//! login password; from a script or agent nobody may be there, and the OS
+//! prompt used to hold the command for about two minutes before it failed.
+//! Everything runs in a temp home; the only daemon is this test's own.
+
+mod common;
+
+use common::Home;
+use std::time::{Duration, Instant};
+
+/// Unix: socket only. Windows has no Unix-socket listener, so the daemon
+/// needs its loopback listener there.
+const LOOPBACK: &str = if cfg!(unix) { "off" } else { "127.0.0.1:0" };
+
+#[tokio::test]
+async fn share_through_the_daemon_without_a_terminal_needs_yes() {
+ let mut h = Home::new();
+ h.set("CUA_DAEMON_NO_RELAY", "1");
+ h.run(&["daemon", "start", "--loopback", LOOPBACK])
+ .await
+ .ok();
+
+ // `Home::run` gives the CLI no terminal on stdin.
+ let started = Instant::now();
+ let o = h
+ .run(&["spaces", "share", "local:studio", "[email protected]"])
+ .await;
+ let took = started.elapsed();
+ h.run(&["daemon", "stop"]).await.ok();
+
+ assert_eq!(o.code, 2, "{o:?}");
+ assert!(o.stderr.contains("stdin is not a terminal"), "{o:?}");
+ assert!(o.stderr.contains("--yes (-y)"), "{o:?}");
+ // Refused before the daemon was asked (it would say the Space is
+ // unknown) and without waiting on a prompt.
+ assert!(!o.stderr.contains("not found"), "{o:?}");
+ assert!(took < Duration::from_secs(20), "took {took:?}");
+}
diff --git a/scripts/docs-generators/cli-specs/cua.json b/scripts/docs-generators/cli-specs/cua.json
index f1def3834b..cc83361662 100644
--- a/scripts/docs-generators/cli-specs/cua.json
+++ b/scripts/docs-generators/cli-specs/cua.json
@@ -10352,7 +10352,7 @@
},
{
"abstract": "Let another cua.ai account watch (viewer) or use (editor) a Space, through the relay. They open it as `relay:<machine>`",
- "after_help": "Examples:\n cua spaces share local:studio [email protected]\n cua spaces share local:studio [email protected] --role editor\n cua spaces share relay:0123abcd4567ef89 acct-42 --role viewer",
+ "after_help": "Examples:\n cua spaces share local:studio [email protected]\n cua spaces share local:studio [email protected] --role editor\n cua spaces share relay:0123abcd4567ef89 acct-42 --role viewer\n # From a script or agent: confirm with Touch ID or your password here\n cua spaces share local:studio [email protected] --yes",
"aliases": [],
"arguments": [
{
@@ -10381,11 +10381,11 @@
"aliases": [],
"default_value": false,
"env": null,
- "help": "Confirm without a prompt (needed without a terminal when no daemon runs; a running daemon asks with Touch ID or your password instead)",
+ "help": "Confirm without a terminal prompt. Without a daemon the share happens at once. A running daemon still asks with Touch ID or your password on this computer; without a terminal, `--yes` says someone will answer there (otherwise the command fails at once instead of waiting for a prompt nobody sees)",
"hidden": false,
"name": "yes",
"repeatable": false,
- "short_name": null,
+ "short_name": "y",
"takes_value": false
}
],