Skip to content

Computer use CLI for AI agents

fix: ancestor-path cycle detection + CGEvent click fallback

Lahfir198d7d7d2716
diff --git a/.gitignore b/.gitignore
index ae25984b6..24c23ada4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -64,4 +64,5 @@ dist/
# Compound Engineering
docs/
-todos/
\ No newline at end of file
+todos/
+.cursor/
\ No newline at end of file
diff --git a/crates/macos/src/actions.rs b/crates/macos/src/actions.rs
index fdb3b4c68..7cb6b380b 100644
--- a/crates/macos/src/actions.rs
+++ b/crates/macos/src/actions.rs
@@ -1,5 +1,5 @@
use agent_desktop_core::{
- action::{Action, ActionResult},
+ action::{Action, ActionResult, MouseButton, MouseEvent, MouseEventKind, Point},
error::{AdapterError, ErrorCode},
};
@@ -18,34 +18,70 @@ mod imp {
string::CFString,
};
+ fn try_ax_action(el: &AXElement, name: &str) -> bool {
+ let action = CFString::new(name);
+ let err = unsafe { AXUIElementPerformAction(el.0, action.as_concrete_TypeRef()) };
+ err == kAXErrorSuccess
+ }
+
+ fn click_via_bounds(
+ el: &AXElement,
+ button: MouseButton,
+ count: u32,
+ ) -> Result<(), AdapterError> {
+ let bounds = crate::tree::read_bounds(el).ok_or_else(|| {
+ AdapterError::new(ErrorCode::ActionFailed, "Element has no readable bounds")
+ .with_suggestion("AX action failed and CGEvent fallback unavailable")
+ })?;
+ if bounds.width <= 0.0 || bounds.height <= 0.0 {
+ return Err(AdapterError::new(
+ ErrorCode::ActionFailed,
+ "Element has zero-size bounds",
+ ));
+ }
+ let center = Point {
+ x: bounds.x + bounds.width / 2.0,
+ y: bounds.y + bounds.height / 2.0,
+ };
+ tracing::debug!(
+ ?button,
+ count,
+ x = center.x,
+ y = center.y,
+ "AX action failed, falling back to CGEvent click"
+ );
+ crate::mouse::synthesize_mouse(MouseEvent {
+ kind: MouseEventKind::Click { count },
+ point: center,
+ button,
+ })
+ }
+
pub fn perform_action(el: &AXElement, action: &Action) -> Result<ActionResult, AdapterError> {
let label = action_label(action);
match action {
Action::Click => {
- ax_press_or_fail(el, "click")?;
+ if !try_ax_action(el, kAXPressAction) && !try_ax_action(el, "AXConfirm") {
+ click_via_bounds(el, MouseButton::Left, 1)?;
+ }
}
Action::DoubleClick => {
- let open_action = CFString::new("AXOpen");
- let err =
- unsafe { AXUIElementPerformAction(el.0, open_action.as_concrete_TypeRef()) };
- if err != kAXErrorSuccess {
- ax_press_or_fail(el, "double-click (first press)")?;
- std::thread::sleep(std::time::Duration::from_millis(50));
- ax_press_or_fail(el, "double-click (second press)")?;
+ if !try_ax_action(el, "AXOpen") {
+ if try_ax_action(el, kAXPressAction) {
+ std::thread::sleep(std::time::Duration::from_millis(50));
+ if !try_ax_action(el, kAXPressAction) {
+ click_via_bounds(el, MouseButton::Left, 2)?;
+ }
+ } else {
+ click_via_bounds(el, MouseButton::Left, 2)?;
+ }
}
}
Action::RightClick => {
- let ax_action = CFString::new("AXShowMenu");
- let err =
- unsafe { AXUIElementPerformAction(el.0, ax_action.as_concrete_TypeRef()) };
- if err != kAXErrorSuccess {
- return Err(AdapterError::new(
- ErrorCode::ActionFailed,
- format!("AXShowMenu failed (err={err})"),
- )
- .with_suggestion("Element may not support context menus"));
+ if !try_ax_action(el, "AXShowMenu") {
+ click_via_bounds(el, MouseButton::Right, 1)?;
}
}
@@ -71,7 +107,9 @@ mod imp {
"Toggle works on checkboxes, switches, and radio buttons. Use 'click' for other elements.",
));
}
- ax_press_or_fail(el, "toggle")?;
+ if !try_ax_action(el, kAXPressAction) && !try_ax_action(el, "AXConfirm") {
+ click_via_bounds(el, MouseButton::Left, 1)?;
+ }
}
Action::SetValue(val) => {
@@ -166,11 +204,19 @@ mod imp {
}
Action::TripleClick => {
- ax_press_or_fail(el, "triple-click (1st)")?;
- std::thread::sleep(std::time::Duration::from_millis(30));
- ax_press_or_fail(el, "triple-click (2nd)")?;
- std::thread::sleep(std::time::Duration::from_millis(30));
- ax_press_or_fail(el, "triple-click (3rd)")?;
+ if !try_ax_action(el, kAXPressAction) {
+ click_via_bounds(el, MouseButton::Left, 3)?;
+ } else {
+ std::thread::sleep(std::time::Duration::from_millis(30));
+ if !try_ax_action(el, kAXPressAction) {
+ click_via_bounds(el, MouseButton::Left, 3)?;
+ } else {
+ std::thread::sleep(std::time::Duration::from_millis(30));
+ if !try_ax_action(el, kAXPressAction) {
+ click_via_bounds(el, MouseButton::Left, 3)?;
+ }
+ }
+ }
}
Action::ScrollTo => {
@@ -284,7 +330,10 @@ mod imp {
if is_checked == want_checked {
return Ok(());
}
- ax_press_or_fail(el, if want_checked { "check" } else { "uncheck" })
+ if !try_ax_action(el, kAXPressAction) {
+ click_via_bounds(el, MouseButton::Left, 1)?;
+ }
+ Ok(())
}
}
diff --git a/crates/macos/src/adapter.rs b/crates/macos/src/adapter.rs
index 719544037..d77716627 100644
--- a/crates/macos/src/adapter.rs
+++ b/crates/macos/src/adapter.rs
@@ -209,12 +209,13 @@ fn find_element_recursive(
entry: &RefEntry,
depth: u8,
max_depth: u8,
- visited: &mut FxHashSet<usize>,
+ ancestors: &mut FxHashSet<usize>,
) -> Result<NativeHandle, AdapterError> {
use accessibility_sys::kAXRoleAttribute;
use core_foundation::base::{CFRetain, CFTypeRef};
- if !visited.insert(el.0 as usize) {
+ let ptr_key = el.0 as usize;
+ if !ancestors.insert(ptr_key) {
return Err(AdapterError::element_not_found("element"));
}
@@ -232,12 +233,14 @@ fn find_element_recursive(
_ => false,
};
if name_match {
+ ancestors.remove(&ptr_key);
unsafe { CFRetain(el.0 as CFTypeRef) };
return Ok(NativeHandle::from_ptr(el.0 as *const _));
}
}
if depth >= max_depth {
+ ancestors.remove(&ptr_key);
return Err(AdapterError::element_not_found("element"));
}
@@ -252,11 +255,13 @@ fn find_element_recursive(
.unwrap_or_default();
for child in &children {
- if let Ok(handle) = find_element_recursive(child, entry, depth + 1, max_depth, visited) {
+ if let Ok(handle) = find_element_recursive(child, entry, depth + 1, max_depth, ancestors) {
+ ancestors.remove(&ptr_key);
return Ok(handle);
}
}
+ ancestors.remove(&ptr_key);
Err(AdapterError::element_not_found("element"))
}
diff --git a/crates/macos/src/tree.rs b/crates/macos/src/tree.rs
index 2d5e355b9..66634588b 100644
--- a/crates/macos/src/tree.rs
+++ b/crates/macos/src/tree.rs
@@ -187,12 +187,13 @@ mod imp {
depth: u8,
max_depth: u8,
include_bounds: bool,
- visited: &mut FxHashSet<usize>,
+ ancestors: &mut FxHashSet<usize>,
) -> Option<AccessibilityNode> {
if depth > max_depth || depth >= ABSOLUTE_MAX_DEPTH {
return None;
}
- if !visited.insert(el.0 as usize) {
+ let ptr_key = el.0 as usize;
+ if !ancestors.insert(ptr_key) {
return None;
}
@@ -233,10 +234,12 @@ mod imp {
let children = children_raw
.into_iter()
.filter_map(|child| {
- build_subtree(&child, depth + 1, max_depth, include_bounds, visited)
+ build_subtree(&child, depth + 1, max_depth, include_bounds, ancestors)
})
.collect();
+ ancestors.remove(&ptr_key);
+
Some(AccessibilityNode {
ref_id: None,
role,