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(spaces): renew rejected device sessions during discovery (#4780)
injaneity12af9135471d
diff --git a/.github/workflows/e2e-cua-sdk.yml b/.github/workflows/e2e-cua-sdk.ymlindex 9837bc4b5b..77ca4e827b 100644--- a/.github/workflows/e2e-cua-sdk.yml+++ b/.github/workflows/e2e-cua-sdk.yml@@ -58,9 +58,10 @@ jobs:name: hermetic + container (${{ github.event_name == 'workflow_dispatch' && inputs.lanes || 'hermetic,container' }})if: github.event_name != 'schedule'runs-on: ubuntu-24.04- # A cold build of the SDK, fixtures, bindings, images and runtimes alone- # took 86 minutes; the suite then ran into the old 120-minute limit.- timeout-minutes: 180+ # Run 37562962482 exhausted 180 minutes during cold browser compilation,+ # before image preparation or the suite. Allow the hosted runner's full+ # six-hour budget for the unchanged build and strict test matrix.+ timeout-minutes: 360env:# Optional: an authenticated Docker Hub keeps anonymous manifest-read# rate limits from failing the docs blocks' runtime inspection.diff --git a/libs/cua/crates/cua-host/src/device.rs b/libs/cua/crates/cua-host/src/device.rsindex 6017cd070f..ee1fc3e11d 100644--- a/libs/cua/crates/cua-host/src/device.rs+++ b/libs/cua/crates/cua-host/src/device.rs@@ -627,22 +627,21 @@ impl DeviceAuth {/// never when there was none to begin with.async fn with_optional_session_retry<T, Fut>(&self,+ relay: &RelayClient,+ token: &str,call: impl Fn(RelayClient, String) -> Fut,) -> Result<T>whereFut: std::future::Future<Output = Result<T>>,{- let token = self.tokens.access_token().await?;+ let token = token.to_owned();let session = self.try_session().await;let had_session = session.is_some();- let relay = self.relay.clone().with_device_session(session);- match call(relay, token.clone()).await {+ let client = relay.clone().with_device_session(session);+ match call(client, token.clone()).await {Err(e) if had_session && is_session_rejection(&e) => {self.reset_session().await;- let relay = self- .relay- .clone()- .with_device_session(self.try_session().await);+ let relay = relay.clone().with_device_session(self.try_session().await);call(relay, token).await}other => other,@@ -674,12 +673,26 @@ impl DeviceAuth {/// The account's devices (this one marked `current`) and the end of the/// relay's grace period.pub async fn listing(&self) -> Result<DeviceListing> {- self.with_optional_session_retry(|relay, token| async move {+ let token = self.tokens.access_token().await?;+ self.with_optional_session_retry(&self.relay, &token, |relay, token| async move {relay.device_listing(&token).await}).await}+ /// Lists machines for the caller's relay and account, carrying this device's+ /// session and renewing it once if the relay no longer recognizes it.+ pub async fn machines_for(+ &self,+ relay: &RelayClient,+ token: &str,+ ) -> Result<Vec<crate::Machine>> {+ self.with_optional_session_retry(relay, token, |relay, token| async move {+ relay.machines(&token).await+ })+ .await+ }+/// Renames a device.pub async fn rename(&self, id: &str, name: &str) -> Result<DeviceView> {let id = id.to_owned();@@ -712,7 +725,8 @@ impl DeviceAuth {/// The account's audit log, newest last.pub async fn audit(&self, limit: usize) -> Result<Vec<AuditEvent>> {- self.with_optional_session_retry(move |relay, token| async move {+ let token = self.tokens.access_token().await?;+ self.with_optional_session_retry(&self.relay, &token, move |relay, token| async move {relay.audit(&token, limit).await}).awaitdiff --git a/libs/cua/crates/cua-sdk/tests/topologies.rs b/libs/cua/crates/cua-sdk/tests/topologies.rsindex 479bca2f58..8c17809038 100644--- a/libs/cua/crates/cua-sdk/tests/topologies.rs+++ b/libs/cua/crates/cua-sdk/tests/topologies.rs@@ -873,6 +873,70 @@ async fn discovery_refusal(t: Topology) {}}+// Exercise the same public SDK route used by the native app, including a+// daemon that retains its own cached session while the relay restarts.+async fn discovery_session_recovery(t: Topology) {+ let relay = cua_host::testing::FakeRelay::start().await;+ relay.add_account("account", "owner", None);+ cua_host::RelayClient::new(&relay.url)+ .unwrap()+ .register(+ "account",+ &cua_host::relay::RegisterRequest {+ id: "sdk-roster-machine".into(),+ name: "studio".into(),+ allow: vec![],+ host: None,+ meta: Default::default(),+ },+ )+ .await+ .unwrap();+ relay.require_devices(true);+ relay.fresh_sign_in("owner");+ let tokens = Arc::new(cua_host::StaticToken("account".into()));+ let device = Arc::new(+ cua_host::DeviceAuth::new(+ &relay.url,+ tokens.clone(),+ Arc::new(cua_host::MemoryKeySlot::default()),+ "client",+ )+ .unwrap(),+ );+ device.enroll().await.unwrap();+ let (_world, cua) = world_with_relay(+ t,+ Some(cua_spaces::RelayAccount::new(&relay.url, tokens).with_device(device.clone())),+ )+ .await;+ let rows = cua.spaces().list().await.unwrap();+ assert!(rows.iter().any(|row| row.id == "relay:sdk-roster-machine"));+ let before = device.session().await.unwrap();+ relay.forget_sessions();+ let rows = cua+ .spaces()+ .list()+ .await+ .expect("same runtime recovers its roster");+ assert!(rows.iter().any(|row| row.id == "relay:sdk-roster-machine"));+ assert_ne!(device.session().await.unwrap(), before);+}++#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn discovery_session_recovery_embedded() {+ discovery_session_recovery(Topology::Embedded).await;+}+#[cfg(unix)]+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn discovery_session_recovery_daemon_socket() {+ discovery_session_recovery(Topology::DaemonSocket).await;+}+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn discovery_session_recovery_daemon_loopback() {+ discovery_session_recovery(Topology::DaemonLoopback).await;+}+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]async fn discovery_refusal_embedded() {discovery_refusal(Topology::Embedded).await;diff --git a/libs/cua/crates/cua-spaces/src/spaces.rs b/libs/cua/crates/cua-spaces/src/spaces.rsindex 2b21ab5910..4b5922710b 100644--- a/libs/cua/crates/cua-spaces/src/spaces.rs+++ b/libs/cua/crates/cua-spaces/src/spaces.rs@@ -848,7 +848,11 @@ impl Spaces {pub async fn relay_machines(&self) -> Result<Vec<crate::relay::RelayMachine>> {let relay = self.relay()?;let token = relay.tokens.access_token().await?;- let machines = relay.client().await?.machines(&token).await?;+ let client = cua_host::RelayClient::new(&relay.url)?;+ let machines = match &relay.device {+ Some(device) => device.machines_for(&client, &token).await?,+ None => client.machines(&token).await?,+ };*self.inner.relay_cache.lock().expect("relay cache") = machines.clone();Ok(machines)}diff --git a/libs/cua/crates/cua-spaces/tests/relay.rs b/libs/cua/crates/cua-spaces/tests/relay.rsindex a4185c4c16..4881903a30 100644--- a/libs/cua/crates/cua-spaces/tests/relay.rs+++ b/libs/cua/crates/cua-spaces/tests/relay.rs@@ -287,6 +287,49 @@ async fn relay_spaces_carry_the_enrolled_device_session() {.build();let machines = spaces.relay_machines().await.unwrap();assert_eq!(machines.len(), 1);+ let session_before = device.session().await.unwrap();+ let device_before = device.device_id().unwrap();+ relay.forget_sessions();+ let recovered = spaces+ .list_all()+ .await+ .expect("roster recovers after relay restart");+ assert_eq!(recovered.len(), 1);+ assert_eq!(recovered[0].id, format!("relay:{MACHINE}"));+ assert_eq!(device.device_id().unwrap(), device_before);+ let recovered_session = device.session().await.unwrap();+ assert_ne!(recovered_session, session_before);+ // The directory's authority can differ from the device-session authority.+ // Only this token works at the other directory, whose roster is empty.+ let directory = FakeRelay::start().await;+ directory.add_account("directory-token", "user-1", None);+ let other = Spaces::builder()+ .home(home.path())+ .relay(+ RelayAccount::new(+ &directory.url,+ Arc::new(StaticToken("directory-token".into())),+ )+ .with_device(device.clone()),+ )+ .build();+ assert!(other.relay_machines().await.unwrap().is_empty());+ let wrong = Spaces::builder()+ .home(home.path())+ .relay(+ RelayAccount::new(&relay.url, Arc::new(StaticToken("invalid-token".into())))+ .with_device(device.clone()),+ )+ .build();+ assert_eq!(wrong.list_all().await.unwrap_err().tag(), "unauthenticated");+ assert_eq!(device.session().await.unwrap(), recovered_session);+ // Neither retry may silently fall back to the device's own directory.+ directory.require_devices(true);+ assert_eq!(+ other.list_all().await.unwrap_err().tag(),+ "permission_denied"+ );+ assert_ne!(device.session().await.unwrap(), recovered_session);// The fake relay does not tunnel; it records what reached /m/<id>.assert!(spaces.space(&format!("relay:{MACHINE}")).await.is_err());let session = device.session().await.unwrap();@@ -299,6 +342,24 @@ async fn relay_spaces_carry_the_enrolled_device_session() {&& s.as_deref() == Some(session.as_str())),"{proxied:?}");+ // Revocation cannot be healed by renewing a session. The directory must+ // still refuse the request, including subsequent calls without a session.+ cua_host::RelayClient::new(&relay.url)+ .unwrap()+ .with_device_session(Some(session))+ .revoke_device("owner-token", device_before.as_deref().unwrap())+ .await+ .unwrap();+ assert_eq!(+ spaces.list_all().await.unwrap_err().tag(),+ "permission_denied"+ );+ assert!(device.try_session().await.is_none());+ assert_eq!(+ spaces.list_all().await.unwrap_err().tag(),+ "permission_denied"+ );+ assert_eq!(device.device_id().unwrap(), device_before);}/// A failed refresh leaves the explicit cached route available, while a