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(cua-driver): refuse set_value on Finder file name cells instead of confirming a rename that never happened (#4483)

Ed-Key323af7c39876
diff --git a/docs/content/docs/cua-driver/reference/mcp-tools/values-and-clipboard.mdx b/docs/content/docs/cua-driver/reference/mcp-tools/values-and-clipboard.mdx
index fb954792be..d23cf7fc1d 100644
--- a/docs/content/docs/cua-driver/reference/mcp-tools/values-and-clipboard.mdx
+++ b/docs/content/docs/cua-driver/reference/mcp-tools/values-and-clipboard.mdx
@@ -33,6 +33,8 @@ Set a value on a UI element. Two modes depending on element role:
- **All other elements**: writes AXValue directly (sliders, steppers, date pickers, native text fields that expose settable AXValue).
+- **A file's name as Finder lists it** (a text field carrying AXFilename and a file URL, not being edited): refused with `file_name_needs_rename`, because the write changes only what Finder shows, never the file. The refusal names the keyboard route that renames it.
+
For free-form text entry into web inputs, prefer `type_text_chars` which synthesises key events: AXValue writes are ignored by WebKit.
**macOS parameters**
diff --git a/libs/cua-driver/rust/Skills/cua-driver/MACOS.md b/libs/cua-driver/rust/Skills/cua-driver/MACOS.md
index 332057df78..f813c95784 100644
--- a/libs/cua-driver/rust/Skills/cua-driver/MACOS.md
+++ b/libs/cua-driver/rust/Skills/cua-driver/MACOS.md
@@ -511,6 +511,7 @@ starting point for new browser workflows.
| `Accessibility permission not granted` | TCC not granted | Stop; tell user to grant in System Settings |
| `Screen Recording permission not granted` | TCC not granted for capture | Screenshots and pixel actions are unavailable. If the task is AX-completable, use `get_window_state({include_screenshot:false})` and `element_token` actions; otherwise stop and ask the user to run `cua-driver permissions grant` |
| `AX action failed: ... returned -25200` (or -25205, -25206) | The app may have acted anyway: some AppKit controls return these after a press they performed | Re-read with `get_window_state` before retrying; a second press on a checkbox toggles it back. A radio button or checkbox whose AXValue settles on the pressed state is reported as performed, naming the error |
+| `set_value refused (file_name_needs_rename)` | A file's name as Finder lists it: an AXValue write changes only the list, never the file | `click` the item, then with Finder frontmost send `press_key` return, `hotkey` cmd+a, `type_text` the full new name, `press_key` return, each with `scope:"desktop"`; re-read to confirm |
## Example end-to-end task (macOS)
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/ax/bindings.rs b/libs/cua-driver/rust/crates/platform-macos/src/ax/bindings.rs
index cca6190a59..3fbbf4c3e1 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/ax/bindings.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/ax/bindings.rs
@@ -183,6 +183,28 @@ pub unsafe fn copy_string_attr(element: AXUIElementRef, attr_name: &str) -> Opti
Some(s.to_string())
}
+/// Copy the `AXURL` attribute (a `CFURL`) as its absolute URL string.
+/// Returns `None` on any error or if the attribute is not a `CFURL`.
+///
+/// # Safety
+///
+/// `element` must be a valid, live `AXUIElementRef` for the duration of the call.
+pub unsafe fn copy_url_attr(element: AXUIElementRef) -> Option<String> {
+ use core_foundation::url::CFURL;
+ let attr = CFStr::new("AXURL");
+ let mut value: CFTypeRef = std::ptr::null();
+ let err = AXUIElementCopyAttributeValue(element, attr.as_concrete_TypeRef(), &mut value);
+ if err != kAXErrorSuccess || value.is_null() {
+ return None;
+ }
+ if core_foundation::base::CFGetTypeID(value) != CFURL::type_id() {
+ CFRelease(value);
+ return None;
+ }
+ let url = CFURL::wrap_under_create_rule(value as _);
+ Some(url.absolute().get_string().to_string())
+}
+
/// Copy a numeric attribute from an AX element as an `f64`. Returns `None` on
/// any error or if the attribute is not a `CFNumber`. SwiftUI sliders expose a
/// readable numeric `AXValue` even when that value is not settable — this lets
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/set_value.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/set_value.rs
index 49484e0041..13cd9fad3a 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/set_value.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/set_value.rs
@@ -21,8 +21,8 @@ use std::sync::Arc;
use crate::apps;
use crate::ax::bindings::{
- copy_children, copy_number_attr, copy_string_attr, kAXErrorSuccess, perform_action,
- set_number_attr, set_string_attr, AXUIElementRef,
+ copy_bool_attr, copy_children, copy_number_attr, copy_string_attr, copy_url_attr,
+ kAXErrorSuccess, perform_action, set_number_attr, set_string_attr, AXUIElementRef,
};
use crate::focus_guard;
use crate::window_change_detector::WindowChangeDetector;
@@ -57,6 +57,12 @@ fn def() -> &'static ToolDef {
- **All other elements**: writes AXValue directly (sliders, steppers, \
date pickers, native text fields that expose settable AXValue).\n\
\n\
+ - **A file's name as Finder lists it** (a text field carrying \
+ AXFilename and a file URL, not being edited): refused with \
+ `file_name_needs_rename`, because the write changes only what \
+ Finder shows, never the file. The refusal names the keyboard \
+ route that renames it.\n\
+ \n\
For free-form text entry into web inputs, prefer `type_text_chars` \
which synthesises key events — AXValue writes are ignored by WebKit."
.into(),
@@ -138,6 +144,17 @@ impl Tool for SetValueTool {
Err(refusal_result) => return refusal_result,
};
+ // A file's name as Finder lists it takes an AXValue write and reads it
+ // back, but the file is never renamed. Refuse before anything moves.
+ let name_guard = element_guard.clone();
+ if let Ok(true) = tokio::task::spawn_blocking(move || unsafe {
+ file_name_cell(name_guard.as_ptr() as AXUIElementRef)
+ })
+ .await
+ {
+ return file_name_needs_rename(pid, window_id);
+ }
+
let cursor_key = super::cursor_tools::resolve_cursor_key(&args);
let center_guard = element_guard.clone();
if let Ok(Some((screen_x, screen_y))) = tokio::task::spawn_blocking(move || unsafe {
@@ -215,6 +232,48 @@ impl Tool for SetValueTool {
}
}
+// ── File name cells ──────────────────────────────────────────────────────────
+
+const FILE_NAME_NEEDS_RENAME: &str = "file_name_needs_rename";
+
+/// Whether `element` is a file's name as a list shows it (Finder's list and
+/// icon views): a text field that names a file and is not being edited.
+/// Finder's inline rename editor is focused, so it stays writable.
+unsafe fn file_name_cell(element: AXUIElementRef) -> bool {
+ copy_string_attr(element, "AXRole").as_deref() == Some("AXTextField")
+ && is_file_name_cell(
+ copy_string_attr(element, "AXFilename").as_deref(),
+ copy_url_attr(element).as_deref(),
+ copy_bool_attr(element, "AXFocused"),
+ )
+}
+
+fn is_file_name_cell(filename: Option<&str>, url: Option<&str>, focused: Option<bool>) -> bool {
+ filename.is_some_and(|name| !name.is_empty())
+ && url.is_some_and(|url| url.starts_with("file://"))
+ && focused != Some(true)
+}
+
+fn file_name_needs_rename(pid: i32, window_id: u32) -> ToolResult {
+ let reason = "This is a file's name as the list shows it. Writing its AXValue changes only \
+ what the list shows, never the file, so nothing was written. To rename the \
+ file: click this element to select the item, then with Finder frontmost send \
+ press_key return, hotkey cmd+a (Finder selects the name without its \
+ extension), type_text the full new name, and press_key return, each with \
+ scope:\"desktop\". Then check the new name in a fresh get_window_state.";
+ ToolResult::error(format!(
+ "set_value refused ({FILE_NAME_NEEDS_RENAME}): {reason}"
+ ))
+ .with_structured(serde_json::json!({
+ "code": FILE_NAME_NEEDS_RENAME,
+ "effect": "refused",
+ "path": "ax",
+ "pid": pid,
+ "window_id": window_id,
+ "reason": reason,
+ }))
+}
+
// ── Blocking implementation (runs on spawn_blocking thread) ─────────────────
/// Outcome of a `set_value` write.
@@ -638,7 +697,57 @@ fn hex_digit(n: u8) -> char {
#[cfg(test)]
mod tests {
- use super::{apply_surface_trust, apply_verification_label, classify_write, SetValueOutcome};
+ use super::{
+ apply_surface_trust, apply_verification_label, classify_write, file_name_needs_rename,
+ is_file_name_cell, SetValueOutcome,
+ };
+
+ #[test]
+ fn a_listed_file_name_is_a_file_name_cell() {
+ let url = Some("file:///Users/me/lab/charlie.bin");
+ assert!(is_file_name_cell(Some("charlie.bin"), url, Some(false)));
+ assert!(is_file_name_cell(Some("charlie.bin"), url, None));
+ }
+
+ #[test]
+ fn rename_editor_and_ordinary_fields_stay_writable() {
+ let url = Some("file:///Users/me/lab/charlie.bin");
+ // Finder's inline rename editor is focused while editing.
+ assert!(!is_file_name_cell(Some("charlie.bin"), url, Some(true)));
+ // A plain text field names no file.
+ assert!(!is_file_name_cell(None, None, Some(false)));
+ assert!(!is_file_name_cell(Some(""), url, Some(false)));
+ assert!(!is_file_name_cell(Some("charlie.bin"), None, Some(false)));
+ assert!(!is_file_name_cell(
+ Some("page"),
+ Some("https://example.com/page"),
+ None
+ ));
+ }
+
+ #[test]
+ fn file_name_refusal_names_the_rename_route() {
+ let result = file_name_needs_rename(7, 42);
+ assert_eq!(result.is_error, Some(true));
+ let data = result.structured_content.unwrap();
+ assert_eq!(data["code"], "file_name_needs_rename");
+ assert_eq!(data["effect"], "refused");
+ assert_eq!(
+ (data["pid"].as_i64(), data["window_id"].as_u64()),
+ (Some(7), Some(42))
+ );
+ let reason = data["reason"].as_str().unwrap();
+ for needed in [
+ "never the file",
+ "nothing was written",
+ "return",
+ "cmd+a",
+ "type_text",
+ "desktop",
+ ] {
+ assert!(reason.contains(needed), "missing {needed:?}: {reason}");
+ }
+ }
#[test]
fn unreadable_value_reports_neither_verified_nor_changed() {
diff --git a/libs/cua/skills/cua-driver/MACOS.md b/libs/cua/skills/cua-driver/MACOS.md
index 332057df78..f813c95784 100644
--- a/libs/cua/skills/cua-driver/MACOS.md
+++ b/libs/cua/skills/cua-driver/MACOS.md
@@ -511,6 +511,7 @@ starting point for new browser workflows.
| `Accessibility permission not granted` | TCC not granted | Stop; tell user to grant in System Settings |
| `Screen Recording permission not granted` | TCC not granted for capture | Screenshots and pixel actions are unavailable. If the task is AX-completable, use `get_window_state({include_screenshot:false})` and `element_token` actions; otherwise stop and ask the user to run `cua-driver permissions grant` |
| `AX action failed: ... returned -25200` (or -25205, -25206) | The app may have acted anyway: some AppKit controls return these after a press they performed | Re-read with `get_window_state` before retrying; a second press on a checkbox toggles it back. A radio button or checkbox whose AXValue settles on the pressed state is reported as performed, naming the error |
+| `set_value refused (file_name_needs_rename)` | A file's name as Finder lists it: an AXValue write changes only the list, never the file | `click` the item, then with Finder frontmost send `press_key` return, `hotkey` cmd+a, `type_text` the full new name, `press_key` return, each with `scope:"desktop"`; re-read to confirm |
## Example end-to-end task (macOS)
diff --git a/scripts/docs-generators/cli-specs/cua-driver-mcp-macos.json b/scripts/docs-generators/cli-specs/cua-driver-mcp-macos.json
index a34de45638..595ea950d6 100644
--- a/scripts/docs-generators/cli-specs/cua-driver-mcp-macos.json
+++ b/scripts/docs-generators/cli-specs/cua-driver-mcp-macos.json
@@ -1232,7 +1232,7 @@
"read_only": false
},
{
- "description": "Set a value on a UI element. Two modes depending on element role:\n\n- **AXPopUpButton / select dropdown**: finds the child option whose title or value matches `value` (case-insensitive) and AXPresses it directly — the native macOS popup menu is never opened, so focus is never stolen. Use this for HTML <select> elements in Safari or any native NSPopUpButton.\n\n- **All other elements**: writes AXValue directly (sliders, steppers, date pickers, native text fields that expose settable AXValue).\n\nFor free-form text entry into web inputs, prefer `type_text_chars` which synthesises key events — AXValue writes are ignored by WebKit.",
+ "description": "Set a value on a UI element. Two modes depending on element role:\n\n- **AXPopUpButton / select dropdown**: finds the child option whose title or value matches `value` (case-insensitive) and AXPresses it directly — the native macOS popup menu is never opened, so focus is never stolen. Use this for HTML <select> elements in Safari or any native NSPopUpButton.\n\n- **All other elements**: writes AXValue directly (sliders, steppers, date pickers, native text fields that expose settable AXValue).\n\n- **A file's name as Finder lists it** (a text field carrying AXFilename and a file URL, not being edited): refused with `file_name_needs_rename`, because the write changes only what Finder shows, never the file. The refusal names the keyboard route that renames it.\n\nFor free-form text entry into web inputs, prefer `type_text_chars` which synthesises key events — AXValue writes are ignored by WebKit.",
"destructive": true,
"idempotent": true,
"input_schema": {