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
// SPDX-License-Identifier: FSL-1.1-MIT
// Copyright (c) 2026 Cua AI, Inc.
//! Devices: this device as a client of the signed-in account on the relay
//! (`cua devices` in the app). The page, the enroll sheet and the approval
//! sheet are the app core's (`devices.*`, shared with the SwiftUI app); the
//! commands here only move data to and from the relay through `cua-host`.
//!
//! Approving another device widens who can reach the account's machines,
//! so [`DevicesService::approve`] asks for presence first: Touch ID or the
//! login password on macOS (the app's existing biometric gate), elsewhere
//! the Keyvault passphrase, checked by the broker. Nothing else prompts:
//! unattended agents keep their device session without the user.
use std::collections::HashMap;
use std::sync::Arc;
use async_trait::async_trait;
use cua_host::{AccountTokens, DeviceAuth, DeviceState, PairingCodeRequest, RelayClient};
use cua_spaces_app_core::devices::{AuditInput, DeviceInput, DevicesInput, MachineInput};
use serde::Serialize;
/// Newest audit events the page reads.
pub const AUDIT_LIMIT: usize = 50;
/// The reason the presence prompt shows.
pub const PRESENCE_REASON: &str = "approve a device for your Cua account";
/// Asks the person at this device to confirm (Touch ID, the login password
/// or a passphrase). `Ok` only when they did.
#[async_trait]
pub trait PresenceGate: Send + Sync {
/// Confirms `reason`; `passphrase` is what the person typed, if the
/// gate needs one.
async fn confirm(&self, reason: &str, passphrase: Option<String>) -> Result<(), String>;
}
/// A one-time code that adds a machine to the account ("Add a machine" by
/// link or QR code), as the page reads it (the cua SDK's `PairingCode`).
/// `code`, `url` and the install commands are only there when just made.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PairingCodeView {
pub id: String,
pub code: Option<String>,
pub url: Option<String>,
pub install_sh: Option<String>,
pub install_powershell: Option<String>,
/// `active`, `used`, `revoked` or `expired`.
pub state: String,
pub team: Option<String>,
/// Unix seconds.
pub created_at: u64,
pub expires_at: u64,
pub used_at: Option<u64>,
/// The relay machine id it added.
pub machine: Option<String>,
}
impl PairingCodeView {
/// The relay's code, with the link and commands for `relay`.
pub fn new(p: cua_host::PairingCode, relay: &str) -> Self {
let made = |os: Option<&str>| {
p.code.as_deref().map(|c| match os {
None => cua_host::add_machine_url(c, relay),
Some(os) => cua_host::install_command(c, relay, os),
})
};
Self {
url: made(None),
install_sh: made(Some("linux")),
install_powershell: made(Some("windows")),
id: p.id,
code: p.code,
state: p.state,
team: p.team,
created_at: p.created_at,
expires_at: p.expires_at,
used_at: p.used_at,
machine: p.machine,
}
}
}
/// `devices_enroll`'s result.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EnrollResult {
/// Enrolled at once (a fresh sign-in on the account's first device).
pub enrolled: bool,
/// The one-time code to approve from an enrolled device otherwise.
pub code: Option<String>,
}
fn state_word(s: DeviceState) -> &'static str {
match s {
DeviceState::Pending => "pending",
DeviceState::Enrolled => "enrolled",
DeviceState::Expired => "expired",
DeviceState::Revoked => "revoked",
}
}
fn err(e: cua_host::Error) -> String {
e.to_string()
}
/// The relay calls behind the `devices_*` commands.
pub struct DevicesService {
auth: Arc<DeviceAuth>,
tokens: Arc<dyn AccountTokens>,
}
impl DevicesService {
/// Over this device's `auth` and the account's `tokens`.
pub fn new(auth: Arc<DeviceAuth>, tokens: Arc<dyn AccountTokens>) -> Self {
Self { auth, tokens }
}
/// The account's devices, the grace period, the newest audit events and
/// the machines' names, as the core's `DevicesInput`. Only the device
/// list must succeed.
pub async fn snapshot(&self) -> Result<DevicesInput, String> {
let listing = self.auth.listing().await.map_err(err)?;
let audit = self.auth.audit(AUDIT_LIMIT).await.unwrap_or_default();
let mut machine_names = HashMap::new();
let mut machines_input = Vec::new();
if let Some(session) = self.auth.try_session().await {
if let (Ok(token), Ok(relay)) = (
self.tokens.access_token().await,
RelayClient::new(self.auth.relay_url()),
) {
if let Ok(machines) = relay
.with_device_session(Some(session))
.machines(&token)
.await
{
machines_input = machines
.iter()
.map(|m| MachineInput {
id: m.id.clone(),
name: m.name.clone(),
confirmed: m.confirmed,
})
.collect();
machine_names = machines.into_iter().map(|m| (m.id, m.name)).collect();
}
}
}
Ok(DevicesInput {
devices: listing
.devices
.into_iter()
.map(|d| DeviceInput {
state: state_word(d.state).into(),
platform: (!d.platform.is_empty()).then_some(d.platform),
id: d.id,
name: d.name,
enrolled_until: d.enrolled_until,
last_seen: d.last_seen,
current: d.current,
})
.collect(),
audit: audit
.into_iter()
.map(|e| AuditInput {
ts: e.ts,
kind: e.kind,
device: e.device,
machine: e.machine,
subject: e.subject,
detail: e.detail,
})
.collect(),
local_device_id: self.auth.device_id().map_err(err)?,
pending_code: None,
enforce_after: (listing.enforce_after > 0).then_some(listing.enforce_after),
machine_names,
machines: machines_input,
})
}
/// Registers this device: enrolled at once after a fresh sign-in on the
/// account's first device, else a one-time code.
pub async fn enroll(&self) -> Result<EnrollResult, String> {
let e = self.auth.enroll().await.map_err(err)?;
Ok(EnrollResult {
enrolled: e.device.state == DeviceState::Enrolled,
code: e.code,
})
}
/// Whether the relay lets this device open a session now, asked afresh
/// (polled while waiting for an approval).
pub async fn check_enrolled(&self) -> bool {
self.auth.reset_session().await;
self.auth.session().await.is_ok()
}
/// Approves the device showing `code` (or re-verifies `device_id`)
/// after `presence` confirms. Nothing reaches the relay otherwise.
pub async fn approve(
&self,
presence: &dyn PresenceGate,
code: Option<String>,
device_id: Option<String>,
passphrase: Option<String>,
) -> Result<(), String> {
let code = code.filter(|c| !c.trim().is_empty());
let device_id = device_id.filter(|d| !d.trim().is_empty());
if code.is_none() && device_id.is_none() {
return Err("enter the code the other device shows".into());
}
presence.confirm(PRESENCE_REASON, passphrase).await?;
self.auth
.approve(code.as_deref(), device_id.as_deref())
.await
.map(|_| ())
.map_err(err)
}
/// Renames a device (the name as the core cleans it).
pub async fn rename(&self, id: &str, name: &str) -> Result<(), String> {
let name = cua_spaces_app_core::devices::clean_name(name)
.ok_or_else(|| "enter a name".to_string())?;
self.auth.rename(id, &name).await.map(|_| ()).map_err(err)
}
/// Revokes a device (Deny on a pending one; Revoke… after its
/// confirmation).
pub async fn revoke(&self, id: &str) -> Result<(), String> {
self.auth.revoke(id).await.map(|_| ()).map_err(err)
}
/// Vouches for a machine that registered without an enrolled device's
/// proof (S5), from this enrolled device.
pub async fn confirm_machine(&self, id: &str) -> Result<(), String> {
let session = self.auth.session().await.map_err(err)?;
let token = self.tokens.access_token().await.map_err(err)?;
RelayClient::new(self.auth.relay_url())
.map_err(err)?
.with_device_session(Some(session))
.confirm(&token, id)
.await
.map(|_| ())
.map_err(err)
}
/// The relay, as this device and the account (the pairing-code calls).
async fn account_relay(&self) -> Result<(RelayClient, String), String> {
let session = self.auth.session().await.map_err(err)?;
let token = self.tokens.access_token().await.map_err(err)?;
let relay = RelayClient::new(self.auth.relay_url())
.map_err(err)?
.with_device_session(Some(session));
Ok((relay, token))
}
/// Makes a one-time code (link, QR code, install commands) that adds
/// another computer to the account, from this enrolled device. It
/// expires after 30 minutes. With `team` (an organization id) the code
/// is the same: the relay's own teams are retired, and the machine
/// becomes a team machine once it joined (the web UI shares it with
/// `team:<id>`, through the Spaces API).
pub async fn create_pairing_code(
&self,
team: Option<String>,
) -> Result<PairingCodeView, String> {
let (relay, token) = self.account_relay().await?;
let req = PairingCodeRequest {
name: None,
team: None,
ttl_secs: None,
};
let mut code = relay.create_pairing_code(&token, &req).await.map_err(err)?;
code.team = team.filter(|t| !t.trim().is_empty());
Ok(PairingCodeView::new(code, self.auth.relay_url()))
}
/// The account's Teams (the organizations of `GET /v1/me` on the Spaces
/// API), for "Add a machine" and "Share". This app keeps no current
/// team, so none is chosen.
pub async fn teams(&self) -> Result<TeamsListView, String> {
let token = self.tokens.access_token().await.map_err(err)?;
let me = cua_host::spaces_api::SpacesApiClient::current()
.map_err(err)?
.me(&token)
.await
.map_err(err)?;
Ok(TeamsListView {
teams: me
.orgs
.into_iter()
.map(|o| TeamChoiceView {
// Team machines decide what can be offered: the API's
// features when it lists them, else the plan.
plan: if o.has("team_machines") {
"teams".into()
} else {
o.plan
},
id: o.id,
alias: o.alias,
name: o.name,
role: o.role,
})
.collect(),
current_team_id: None,
})
}
/// The account's pairing codes, newest first (without the codes).
pub async fn pairing_codes(&self) -> Result<Vec<PairingCodeView>, String> {
let (relay, token) = self.account_relay().await?;
let codes = relay.pairing_codes(&token).await.map_err(err)?;
let url = self.auth.relay_url();
Ok(codes
.into_iter()
.map(|c| PairingCodeView::new(c, url))
.collect())
}
/// Revokes an unused pairing code (`pc_…`).
pub async fn revoke_pairing_code(&self, id: &str) -> Result<(), String> {
let (relay, token) = self.account_relay().await?;
relay.revoke_pairing_code(&token, id).await.map_err(err)
}
}
/// The app's presence gate: the biometric prompt on macOS, else the
/// Keyvault passphrase checked by the broker.
pub struct OsPresence<'a> {
/// The Keyvault broker client (the passphrase check off macOS).
pub keyvault: &'a crate::keyvault::KeyvaultCommands,
}
#[async_trait]
impl PresenceGate for OsPresence<'_> {
async fn confirm(&self, reason: &str, passphrase: Option<String>) -> Result<(), String> {
if cfg!(target_os = "macos") {
let reason = reason.to_string();
return tokio::task::spawn_blocking(move || crate::biometric::authorize(&reason))
.await
.map_err(|e| e.to_string())?;
}
match passphrase.filter(|p| !p.is_empty()) {
Some(p) => self
.keyvault
.unlock_with_passphrase(cua_spaces_app_core::keyvault::client::Zeroizing::new(p))
.await
.map_err(|f| format!("the Keyvault passphrase was not accepted ({})", f.message)),
None => Err(
"enter your Keyvault passphrase to approve (set one up in Keyvault first)".into(),
),
}
}
}
/// Whether presence on this system is a passphrase the page must ask for.
pub fn presence_needs_passphrase() -> bool {
!cfg!(target_os = "macos")
}
/// One of the account's Teams (`teams.list`).
#[derive(Debug, Clone, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TeamChoiceView {
pub id: String,
pub alias: String,
pub name: String,
pub role: String,
pub plan: String,
}
/// `teams.list`: the account's Teams and the current one.
#[derive(Debug, Clone, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TeamsListView {
pub teams: Vec<TeamChoiceView>,
pub current_team_id: Option<String>,
}