Computer use CLI for AI agents
Rust accessibilityaccessibility-apiai-agentsautomationclicomputer-usecomputer-use-agentcomputer-use-agentsdesktop-automationmacosmcprust
fix: preserve host deadlines and private startup
Lahfircdc59b94744e
diff --git a/crates/macos/src/system/appkit_bridge.m b/crates/macos/src/system/appkit_bridge.mindex 9d291d5d4..1fd0cee33 100644--- a/crates/macos/src/system/appkit_bridge.m+++ b/crates/macos/src/system/appkit_bridge.m@@ -121,7 +121,6 @@ AgentDesktopBytesResult agent_desktop_copy_workspace_snapshot_json(void) {@try {@autoreleasepool {NSWorkspace *workspace = [NSWorkspace sharedWorkspace];- // A command host must consume AppKit launch/exit notifications between requests.CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.001, false);NSArray<NSRunningApplication *> *running = workspace.runningApplications;if (running == nil || running.count > 8192) {diff --git a/src/batch/execution.rs b/src/batch/execution.rsindex 4f742aec5..4188f82ab 100644--- a/src/batch/execution.rs+++ b/src/batch/execution.rs@@ -141,7 +141,7 @@ pub(super) fn execute(entry,results_bytes,);- completed += 1;+ completed += usize::from(entry["execution"] == "completed");push_small_entry(&mut results, &mut results_bytes, entry);if oversized {diff --git a/src/batch/execution_tests.rs b/src/batch/execution_tests.rsindex fc205056c..be25a61b3 100644--- a/src/batch/execution_tests.rs+++ b/src/batch/execution_tests.rs@@ -105,6 +105,8 @@ fn dispatches_each_entry_at_most_once() {assert_eq!(output["semantics"]["batch_retries"], false);assert_eq!(output["semantics"]["atomic"], false);assert_eq!(output["results"][0]["execution"], "completed");+ assert_eq!(output["completed_entries"], 1);+ assert_eq!(output["not_started_entries"], 0);}#[test]@@ -120,6 +122,8 @@ fn expired_batch_never_starts_the_entry() {assert_eq!(adapter.clears.load(Ordering::SeqCst), 0);assert_eq!(output["results"][0]["execution"], "not_started");+ assert_eq!(output["completed_entries"], 0);+ assert_eq!(output["not_started_entries"], output["total_entries"]);assert_eq!(output["results"][0]["not_started_reason"], "deadline");assert_eq!(output["results"][0]["error"]["disposition"]["delivery"],@@ -151,6 +155,8 @@ fn baseline_failure_prevents_the_producer_action() {assert_eq!(adapter.clears.load(Ordering::SeqCst), 0);assert_eq!(output["results"][0]["execution"], "not_started");+ assert_eq!(output["completed_entries"], 0);+ assert_eq!(output["not_started_entries"], output["total_entries"]);assert_eq!(output["results"][0]["not_started_reason"],"pre_action_baseline_failed"diff --git a/src/command_host_budget.rs b/src/command_host_budget.rsindex c88ac5f75..6a3338595 100644--- a/src/command_host_budget.rs+++ b/src/command_host_budget.rs@@ -69,6 +69,18 @@ fn budget_ms(cli: &crate::Cli, command: &crate::Commands) -> Result<u64, AppErro.ok_or_else(|| AppError::invalid_input("Wait is too long"))?,None => a.timeout,},+ Commands::Snapshot(a) => a+ .timeout_ms+ .unwrap_or(agent_desktop_core::DEFAULT_OPERATION_TIMEOUT_MS),+ Commands::Find(a) => a+ .timeout_ms+ .unwrap_or(agent_desktop_core::DEFAULT_OPERATION_TIMEOUT_MS),+ Commands::ListApps(a) => a+ .timeout_ms+ .unwrap_or(agent_desktop_core::DEFAULT_OPERATION_TIMEOUT_MS),+ Commands::ListWindows(a) => a+ .timeout_ms+ .unwrap_or(agent_desktop_core::DEFAULT_OPERATION_TIMEOUT_MS),Commands::Launch(a) => a.timeout,Commands::Batch(a) => a.timeout_ms,_ => agent_desktop_core::DEFAULT_OPERATION_TIMEOUT_MS,@@ -112,6 +124,10 @@ mod tests {vec!["ad", "wait", "--text", "absent", "--timeout", "250"],250,),+ (vec!["ad", "snapshot", "--timeout-ms", "30000"], 30000),+ (vec!["ad", "find", "--timeout-ms", "30000"], 30000),+ (vec!["ad", "list-apps", "--timeout-ms", "30000"], 30000),+ (vec!["ad", "list-windows", "--timeout-ms", "30000"], 30000),(vec!["ad", "wait", "100"], 5100),(vec!["ad", "batch", "[]", "--timeout-ms", "80000"], 80000),(diff --git a/src/main.rs b/src/main.rsindex 4c371e29c..a602c642c 100644--- a/src/main.rs+++ b/src/main.rs@@ -33,6 +33,10 @@ use std::process::ExitCode;const EXIT_INVALID_ARGS: u8 = 2;fn main() -> ExitCode {+ #[cfg(target_os = "macos")]+ if command_host::is_host_process() {+ unsafe { libc::umask(0o077) };+ }if let Some(result) = build_adapter().run_cursor_overlay_child() {return if result.is_ok() {ExitCode::SUCCESSdiff --git a/src/tests/command_host_process.rs b/src/tests/command_host_process.rsindex 59ecb62e5..c712b3ff9 100644--- a/src/tests/command_host_process.rs+++ b/src/tests/command_host_process.rs@@ -162,6 +162,7 @@ fn socket_host_survives_a_bad_client_and_serves_separate_invocations() {#[test]fn default_cli_starts_and_reuses_its_host() {use std::hash::{Hash, Hasher};+ use std::os::unix::{fs::PermissionsExt, process::CommandExt};let directory = std::env::temp_dir().join(format!("ad-host-client-{}", std::process::id()));let mut hash = std::collections::hash_map::DefaultHasher::new();host_identity(&directory).hash(&mut hash);@@ -171,7 +172,14 @@ fn default_cli_starts_and_reuses_its_host() {hash.finish(),));let invoke = || {- Command::new(env!("CARGO_BIN_EXE_agent-desktop"))+ let mut command = Command::new(env!("CARGO_BIN_EXE_agent-desktop"));+ unsafe {+ command.pre_exec(|| {+ libc::umask(0);+ Ok(())+ });+ }+ command.arg("version").env_remove("AGENT_DESKTOP_INTERNAL_HOST_CLIENT").env_remove("AGENT_DESKTOP_INTERNAL_COMMAND_HOST")@@ -184,6 +192,20 @@ fn default_cli_starts_and_reuses_its_host() {};let first = invoke();let concurrent = invoke();+ let deadline = Instant::now() + Duration::from_secs(10);+ loop {+ match std::fs::metadata(&socket) {+ Ok(metadata) => {+ assert_eq!(metadata.permissions().mode() & 0o077, 0);+ break;+ }+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => {+ assert!(Instant::now() < deadline, "host did not bind");+ std::thread::yield_now();+ }+ Err(error) => panic!("socket metadata failed: {error}"),+ }+ }let first = first.wait_with_output().unwrap();let concurrent = concurrent.wait_with_output().unwrap();let stream = UnixStream::connect(&socket).unwrap();