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.

103 KB
Raw Blame
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Cua AI, Inc.

//! click tool — matches the Swift reference ClickTool.swift.
//!
//! Two addressing modes:
//!
//! * **AX path** (`element_token`): performs AXAction on the cached
//!   element. Fires via AX RPC — the target app never needs to be frontmost.
//!   Extra behaviors vs. the naive dispatch:
//!   - AXTextField / AXTextArea inside web content (AXWebArea): 800 ms post-click delay
//!     for DOM focus settle. Native text inputs return without it.
//!   - AXPopUpButton: appends the list of available options and redirects to set_value.
//!   - Advertised-action warning if the element didn't list the requested action.
//!
//! * **Pixel path** (`x`, `y`): synthesises CGEvent mouse clicks and posts them to
//!   the target pid.  `from_zoom=true` translates zoom-crop pixel coordinates back
//!   to full-window space using the most recent `zoom` context stored per-pid.

use async_trait::async_trait;
use cua_driver_contract::ClickButton;
use cua_driver_core::{
    protocol::ToolResult,
    tool::{Tool, ToolDef},
    tool_args::parse_legacy_click_input,
};
use serde_json::Value;
use std::sync::Arc;

use crate::apps;
use crate::ax::bindings::{
    copy_action_names, copy_bool_attr, copy_children, copy_element_attr, copy_string_attr,
    element_at_screen_position, element_screen_rect, kAXErrorSuccess, AXUIElementPerformAction,
    AXUIElementRef,
};
use crate::focus_guard;
use crate::window_change_detector::WindowChangeDetector;
use core_foundation::base::{CFRelease, CFTypeRef, TCFType};

use super::pixel_route::PixelClickRoute;
use super::ToolState;

pub struct ClickTool {
    state: Arc<ToolState>,
}

impl ClickTool {
    pub fn new(state: Arc<ToolState>) -> Self {
        Self { state }
    }
}

static DEF: std::sync::OnceLock<ToolDef> = std::sync::OnceLock::new();

/// Focus posture for the raw pixel transport after AX hit-testing has failed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum PixelActivationPolicy {
    /// Standard background delivery: suppress activation of the target.
    SuppressTarget,
    /// Left-click with a concrete window: intentionally make the target
    /// AppKit-active without raising it, while suppressing every other app.
    AllowTargetWithoutRaise,
    /// Explicit foreground rung owns its brief activation and restoration.
    ForegroundAssist,
}

#[derive(Clone, Copy, Debug)]
struct SelectionPixelTarget {
    screen_x: f64,
    screen_y: f64,
    window_x: f64,
    window_y: f64,
}

fn selection_pixel_target(
    screen_center: (f64, f64),
    window_origin: (f64, f64),
) -> SelectionPixelTarget {
    SelectionPixelTarget {
        screen_x: screen_center.0,
        screen_y: screen_center.1,
        window_x: screen_center.0 - window_origin.0,
        window_y: screen_center.1 - window_origin.1,
    }
}

/// Resolve the selectable row/item rather than an actionable child such as a
/// selectable NSTextField. A modified click on the child can be consumed as a
/// text interaction without ever reaching the collection's selection model.
fn nearest_selectable_container_center(element_ptr: usize) -> Option<(f64, f64)> {
    let mut current = element_ptr as AXUIElementRef;
    let mut owns_current = false;

    for _ in 0..8 {
        let role = unsafe { copy_string_attr(current, "AXRole") }.unwrap_or_default();
        if matches!(role.as_str(), "AXRow" | "AXCell" | "AXListItem" | "AXImage")
            && unsafe { copy_bool_attr(current, "AXSelected") }.is_some()
        {
            let center = unsafe { element_screen_rect(current) }
                .map(|rect| (rect[0] + rect[2] / 2.0, rect[1] + rect[3] / 2.0));
            if owns_current {
                unsafe { CFRelease(current as CFTypeRef) };
            }
            return center;
        }

        let parent = unsafe { copy_element_attr(current, "AXParent") };
        if owns_current {
            unsafe { CFRelease(current as CFTypeRef) };
        }
        current = parent?;
        owns_current = true;
    }

    if owns_current {
        unsafe { CFRelease(current as CFTypeRef) };
    }
    None
}

fn selection_readback_confirms(
    before: bool,
    after: bool,
    has_modifiers: bool,
    prior_selected_peers_preserved: bool,
) -> bool {
    if has_modifiers {
        after != before && prior_selected_peers_preserved
    } else {
        after
    }
}

const SELECTION_READBACK_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(500);
const SELECTION_READBACK_POLL: std::time::Duration = std::time::Duration::from_millis(25);
const SELECTION_READBACK_SETTLE: std::time::Duration = std::time::Duration::from_millis(200);
const SELECTION_READBACK_STABILITY: std::time::Duration = std::time::Duration::from_millis(250);

/// AXPress on a radio button or checkbox that advertises it: a press whose
/// effect the control's own AXValue shows.
fn is_toggle_press(ax_action: &str, role: &str, advertised: &[String]) -> bool {
    ax_action == "AXPress"
        && matches!(role, "AXRadioButton" | "AXCheckBox")
        && advertised.iter().any(|action| action == "AXPress")
}

/// AX errors that AppKit apps return from a press they performed (#3835).
/// Errors that mean the request never reached the app (illegal argument,
/// invalid element, cannot complete, API disabled) are not among them.
fn press_error_may_have_acted(err: crate::ax::bindings::AXError) -> bool {
    use crate::ax::bindings::{
        kAXErrorActionUnsupported, kAXErrorAttributeUnsupported, kAXErrorFailure,
    };
    err == kAXErrorFailure
        || err == kAXErrorAttributeUnsupported
        || err == kAXErrorActionUnsupported
}

/// Whether a toggle's value moving from `before` to `now` is what pressing it
/// does: a checkbox changes state; a radio button becomes selected (a radio
/// turning off was another radio's press).
fn toggle_press_shows(role: &str, before: &str, now: &str) -> bool {
    now != before && (role != "AXRadioButton" || now == "1")
}

/// The value `read` returns when, before `timeout`, it satisfies `shows` and
/// a second read `stability` later agrees on it. Some apps (Finder's toolbar
/// view switcher) apply a press and still return an AX error.
fn settled_value(
    shows: impl Fn(&str) -> bool,
    mut read: impl FnMut() -> Option<String>,
    timeout: std::time::Duration,
    poll: std::time::Duration,
    stability: std::time::Duration,
) -> Option<String> {
    let deadline = std::time::Instant::now() + timeout;
    loop {
        if let Some(now) = read().filter(|now| shows(now)) {
            std::thread::sleep(stability);
            if read().as_deref() == Some(now.as_str()) {
                return Some(now);
            }
        }
        if std::time::Instant::now() >= deadline {
            return None;
        }
        std::thread::sleep(poll);
    }
}

fn pixel_activation_policy(
    button: &str,
    effective_foreground: bool,
    has_window: bool,
) -> PixelActivationPolicy {
    if effective_foreground {
        PixelActivationPolicy::ForegroundAssist
    } else if button == "left" && has_window {
        PixelActivationPolicy::AllowTargetWithoutRaise
    } else {
        PixelActivationPolicy::SuppressTarget
    }
}

/// Return the prior foreground pid that should be restored after a raw
/// background pixel click.
///
/// This decision deliberately depends on observed application state rather
/// than the private focus recipe's return value. The recipe can be unavailable
/// or partially fail while the raw click still makes the target AppKit-active;
/// in that case the allow-target suppression lease will not restore it for us.
fn background_pixel_restore_pid(
    activation_policy: PixelActivationPolicy,
    prior_front: Option<i32>,
    target_pid: i32,
    observed_front: Option<i32>,
) -> Option<i32> {
    if activation_policy == PixelActivationPolicy::AllowTargetWithoutRaise
        && prior_front != Some(target_pid)
        && observed_front == Some(target_pid)
    {
        prior_front
    } else {
        None
    }
}

/// Hand the user's frontmost app and key window back after a raw background
/// pixel action that ran the focus-without-raise prologue (click, drag).
///
/// The no-raise record can make NSWorkspace report the target as active even
/// though its window never moved in z-order. Once the input has been queued,
/// restore the prior app if the target is still reported frontmost. Base this
/// on observed state, not `focus_without_raise`: the private recipe can report
/// failure after partially activating the target, and the raw input can
/// self-activate even when that recipe is unavailable. Do not overwrite a
/// different app here; the wildcard suppression lease handles genuine side
/// effects.
pub(super) async fn restore_user_focus_after_background_pixel(
    undo: crate::focus_arbiter::UndoWatch,
    pid: i32,
    prior_front: Option<i32>,
    prior_key_wid: Option<u32>,
    window_id: Option<u32>,
    focus_without_raise: bool,
) {
    if prior_front == Some(pid) {
        return;
    }
    tokio::time::sleep(std::time::Duration::from_millis(50)).await;
    let reactivated = if let Some(previous_pid) = background_pixel_restore_pid(
        PixelActivationPolicy::AllowTargetWithoutRaise,
        prior_front,
        pid,
        apps::frontmost_pid(),
    )
    .filter(|_| undo.decide() == crate::focus_arbiter::Decision::Undo)
    {
        let _ = apps::reactivate_app(previous_pid);
        wait_until_frontmost(previous_pid, std::time::Duration::from_millis(300)).await;
        true
    } else {
        false
    };
    // The prologue leaves the user's window key, but the input can still make
    // the target self-activate, and re-activating the prior app does not by
    // itself make its window key again. Post the focus record to the user's
    // window only then, or when WindowServer's key focus left the user's app.
    // Posting it while their window is still key re-activates their app
    // (AppKit sends it didBecomeActive), which Bench v2 counted as a
    // disturbance after every background pixel click.
    if let (Some(previous_pid), Some(wid)) = (prior_front, window_id) {
        let key_moved = user_focus_needs_restore(
            reactivated,
            focus_without_raise,
            crate::input::skylight::key_focus_pid(),
            previous_pid,
        );
        if key_moved && apps::frontmost_pid() == Some(previous_pid) {
            let _ = tokio::task::spawn_blocking(move || {
                crate::input::skylight::restore_focus_after_without_raise(
                    previous_pid,
                    prior_key_wid,
                    pid,
                    wid,
                )
            })
            .await;
        }
    }
}

/// Whether the user's window needs the focus record back after a background
/// pixel action: their app was re-activated, or WindowServer's key focus
/// (`key_focus`) left it. Not when it never moved, since the record then makes
/// their app become active again.
fn user_focus_needs_restore(
    reactivated: bool,
    focus_without_raise: bool,
    key_focus: Option<i32>,
    previous_pid: i32,
) -> bool {
    reactivated || (focus_without_raise && key_focus.is_some_and(|pid| pid != previous_pid))
}

/// Poll until `pid` is the frontmost application or `budget` runs out.
async fn wait_until_frontmost(pid: i32, budget: std::time::Duration) -> bool {
    let deadline = std::time::Instant::now() + budget;
    loop {
        if apps::frontmost_pid() == Some(pid) {
            return true;
        }
        if std::time::Instant::now() >= deadline {
            return false;
        }
        tokio::time::sleep(std::time::Duration::from_millis(15)).await;
    }
}

fn def() -> &'static ToolDef {
    DEF.get_or_init(|| ToolDef {
        name: "click".into(),
        description: "Click a target pid. Prefer `element_token` (from get_window_state): it identifies one snapshot element, works on backgrounded, minimized or off-Space windows, and moves no cursor or focus. Use `x, y` only for canvas / video / WebGL / custom-drawn surfaces missing from the AX tree.\n\
            \n\
            - element_token: AX action path. Tokens are scoped to (pid, window_id) and stale once the window is snapshotted again.\n\
            - x, y: window-local pixels of the get_window_state screenshot, sent as CGEvent mouse events. Needs a visible window. `modifier` takes cmd/shift/option/ctrl.\n\
            \n\
            `button` defaults to left; on the AX path \"right\" maps to AXShowMenu and \"middle\" falls back to a pixel click at the element center. `action`: press (default), show_menu, pick, confirm, cancel, open, show_alternate_ui, show_default_ui; an element listing showalternateui reveals its hover-only controls with show_alternate_ui, in the background and without the pointer. `from_zoom:true` translates zoom-image pixels after a `zoom` call. Pressing a popup button opens its menu and leaves it open until an item is chosen or it is dismissed; in the background the user's front window keeps key focus meanwhile. To choose an option in one call use `set_value`. In the background a web `<select>` (Chrome, Electron) is not opened, because its menu would raise the browser window: the click is refused with `select_menu_would_raise_window`; use `set_value`.".into(),
        input_schema: serde_json::json!({
            "type": "object",
            // `pid` is conditionally required — needed for window/element clicks
            // but omitted for windowless `scope:"desktop"` clicks — so it is NOT
            // in `required`; the code validates it with a clear error when needed.
            // (Keeps the contract consistent across platforms; see
            // cua_driver_core::tool_schema.)
            "required": [],
            "properties": {
                "session": { "type": "string", "description": "For multi-call work, prefer a short public session label and repeat it on every call that accepts it." },
                "pid":           { "type": "integer", "description": "Target process ID. Optional with element_token (it carries the pid)." },
                "window_id":     { "type": "integer", "description": "Target window ID. Omit with element_token." },
                "element_token": cua_driver_core::tool_schema::element_token_schema(),
                "capture_id": { "type": "string", "description": "Optional capture ID from get_window_state or get_desktop_state. With x,y it is consumed atomically; stale or mismatched captures are refused." },
                "x":             { "type": "number",  "description": "X in screenshot pixels (get_window_state PNG, or get_desktop_state PNG for a desktop target)." },
                "y":             { "type": "number",  "description": "Y in screenshot pixels from the image selected by target." },
                "action":        { "type": "string",  "description": "AX action: press (default), show_menu, pick, confirm, cancel, open, show_alternate_ui, show_default_ui. show_alternate_ui reveals hover-only controls on an element that lists it, without the pointer." },
                "button":        {
                    "type": "string",
                    "enum": ["left", "right", "middle"],
                    "description": "Mouse button. Default left."
                },
                "count":         { "type": "integer", "description": "Click count (pixel path only). Default 1." },
                "modifier": {
                    "type": "array",
                    "items": { "type": "string" },
                    "description": "Modifier keys: cmd, shift, option/alt, ctrl."
                },
                "from_zoom": {
                    "type": "boolean",
                    "description": "x and y are in the last zoom image for this pid."
                },
                "debug_image_out": {
                    "type": "string",
                    "description": "Write a screenshot with a crosshair at (x, y) to this path. Needs window_id; not with from_zoom."
                },
                "delivery_mode": {
                    "type": "string",
                    "enum": ["background", "foreground"],
                    "description": "Default \"background\" (no fronting). \"foreground\" briefly fronts the window, acts, then restores the prior app; needs window_id and is required for modified clicks. Escalate in order: AX element_token, background pixel, foreground, checking state between rungs; a click has no read-back except list-row selection."
                },
                "scope": {
                    "type": "string",
                    "enum": ["window", "desktop"],
                    "description": "Default \"window\". \"desktop\": x,y with no pid/window_id are true screen pixels from get_desktop_state."
                }
            },
            "additionalProperties": false
        }),
        read_only:   false,
        destructive: true,
        idempotent:  false,
        open_world:  true,
    })
}

#[async_trait]
impl Tool for ClickTool {
    fn def(&self) -> &ToolDef {
        def()
    }

    async fn invoke(&self, args: Value) -> ToolResult {
        // Fast path (CUA-3102): the guards below leave their check summary
        // here; it lands in the result under `fast_path`. Off by default.
        let mut fast_path = None;
        let mut result = self.invoke_guarded(args, &mut fast_path).await;
        crate::fast_path_guard::annotate(&mut result, fast_path);
        result
    }
}

impl ClickTool {
    async fn invoke_guarded(&self, args: Value, fast_path: &mut Option<Value>) -> ToolResult {
        use cua_driver_core::tool_args::ArgsExt;

        // ── Window-less screen-absolute branch (scope="desktop") ──────
        // x,y given with NO pid and NO window_id → the coordinates are TRUE
        // SCREEN pixels. This is the foreground, vision-driven desktop-scope
        // path, the macOS peer of the Windows WindowFromPoint click. Gate on the
        // effective scope: under "window" return a structured
        // `desktop_scope_disabled` error (same contract as Windows) rather than
        // silently treating window-local pixels as screen pixels.
        let has_pid = args.get("pid").map(|v| !v.is_null()).unwrap_or(false);
        let has_window_id = args.get("window_id").map(|v| !v.is_null()).unwrap_or(false);
        let has_xy = args.get("x").map(|v| v.is_number()).unwrap_or(false)
            && args.get("y").map(|v| v.is_number()).unwrap_or(false);
        let capture_id = args.opt_str("capture_id");
        if capture_id.is_some() && !has_xy {
            return ToolResult::error("click.capture_id requires pixel coordinates x and y.")
                .with_structured(serde_json::json!({ "code": "invalid_arguments" }));
        }
        if has_xy && !has_pid && !has_window_id {
            // `scope` is a per-call param now (default "window"); pass
            // scope="desktop" to enable screen-absolute clicks.
            let scope = args.str_or("scope", "window");
            if scope != "desktop" {
                return ToolResult::error(
                    "click: x,y given with no pid/window_id, but scope is \"window\". \
                     Screen-absolute clicks require desktop scope. Pass scope=\"desktop\" \
                     (and use get_desktop_state with scope=\"desktop\" to read true \
                     screen pixels) first."
                        .to_string(),
                )
                .with_structured(serde_json::json!({
                    "code": "desktop_scope_disabled",
                    "scope": scope,
                    "suggestion": "pass scope=\"desktop\"",
                }));
            }
            let input = match parse_legacy_click_input(&args) {
                Ok(input) => input,
                Err(result) => return result,
            };
            let sx_shot = input.x;
            let sy_shot = input.y;
            // ── Desktop-screenshot pixels → logical screen points ──────────────
            // The vision invariant: the pixel an agent reads off the screenshot it
            // was handed is the pixel that gets clicked. `get_desktop_state`
            // returns the display at NATIVE pixels (e.g. 3024×1964 on a 2× Retina
            // display whose logical size is 1512×982), but everything below — the
            // window-under-point hit test (logical CGWindow bounds), the cursor
            // warp, and the CGEvent post — operates in LOGICAL screen points. So
            // x,y arrive in desktop-SCREENSHOT space (what the agent reads off the
            // PNG) and must be divided by the screenshot↔logical ratio, or a
            // center-pixel pick warps to the corner (off by the backing scale).
            let (sx, sy) = if let Some(ref capture_id) = capture_id {
                match self
                    .state
                    .capture_bindings
                    .admit_desktop_click(capture_id, &args, sx_shot, sy_shot)
                {
                    Ok(point) => point,
                    Err(refusal) => return refusal,
                }
            } else {
                super::desktop_screenshot_point(sx_shot, sy_shot)
            };
            let button = match input.button.unwrap_or(ClickButton::Left) {
                ClickButton::Left => "left",
                ClickButton::Right => "right",
                ClickButton::Middle => "middle",
            }
            .to_owned();
            let count = input.count.unwrap_or(1) as usize;
            if count == 0 {
                return ToolResult::error("click.count must be at least 1.")
                    .with_structured(serde_json::json!({ "code": "invalid_arguments" }));
            }
            if let Some(refusal) =
                super::desktop_pointer_guard::check("click", &args, vec![(sx, sy)]).await
            {
                return refusal;
            }
            // Glide the session's agent cursor to the screen point for visibility.
            let cursor_key = super::cursor_tools::resolve_cursor_key(&args);
            crate::cursor::overlay::animate_cursor_to(cursor_key.clone(), sx, sy).await;
            self.state
                .cursor_registry
                .update_position(&cursor_key, sx, sy);
            // Press edge for the agent-cursor overlay (renders a click pulse on
            // viewers via the cursor hook).
            self.state.cursor_registry.note_press(&cursor_key, sx, sy);

            let btn = button.clone();
            let desktop_modifiers: Vec<String> = args.str_array("modifier");
            let result = tokio::task::spawn_blocking(move || -> anyhow::Result<()> {
                // Desktop scope is explicitly foreground and vision-driven: post
                // at the global HID tap so WindowServer delivers to the window
                // actually visible at this point. PID-posting here would silently
                // turn the foreground contract back into background delivery.
                let modifier_refs: Vec<&str> =
                    desktop_modifiers.iter().map(String::as_str).collect();
                crate::input::mouse::click_at_xy_desktop_with_modifiers(
                    sx,
                    sy,
                    count,
                    &btn,
                    &modifier_refs,
                )
            })
            .await;
            let button_label = match button.as_str() {
                "right" => "right-click",
                "middle" => "middle-click",
                _ => "click",
            };
            return match result {
                Ok(Ok(())) => ToolResult::text(format!(
                    "✅ Sent screen-absolute {button_label} at desktop-pixel \
                     ({sx_shot:.0},{sy_shot:.0}) → screen-point ({sx:.0},{sy:.0}) \
                     (desktop scope; not driver-verified)."
                ))
                .with_structured(serde_json::json!({ "path": "cgevent_hid", "verified": false, "effect": "unverifiable" })),
                Ok(Err(e)) => ToolResult::error(format!("desktop-scope click failed: {e}")),
                Err(e) => ToolResult::error(format!("task error: {e}")),
            };
        }

        let pid = match super::target_pid(&self.state, &args) {
            Ok(v) => v,
            Err(e) => return e,
        };
        // Resolve this action's cursor key so its click-pulse / glide land on
        // the calling session's cursor, not the shared "default" one.
        let cursor_key = super::cursor_tools::resolve_cursor_key(&args);

        let window_id_arg = args.opt_u64("window_id");
        let resolved = match self.state.snapshots.resolve(pid, &args) {
            Ok(r) => r,
            Err(e) => return e,
        };
        let (element_index, window_id, element_guard) = resolved.into_parts(window_id_arg);
        if capture_id.is_some() && element_guard.is_some() {
            return ToolResult::error(
                "click.capture_id is valid only for the pixel x,y path, not element actions.",
            )
            .with_structured(serde_json::json!({ "code": "invalid_arguments" }));
        }
        let window_id = match super::native_window_id(window_id) {
            Ok(window_id) => window_id,
            Err(error) => return error,
        };
        let x = args
            .opt_f64("x")
            .or_else(|| args.opt_i64("x").map(|i| i as f64));
        let y = args
            .opt_f64("y")
            .or_else(|| args.opt_i64("y").map(|i| i as f64));
        let action = args.str_or("action", "press");
        if let Err(error) = validate_click_action(&action, element_guard.is_some()) {
            return ToolResult::error(format!("click: {error}"));
        }
        // Surface 5: optional `button` arg, default "left" preserves legacy behaviour.
        // Pixel path: routes to left/right/middle CGEvent primitives.
        // AX path: "right" delegates to AXShowMenu (same surface as right_click);
        // "middle" has no AX equivalent and falls back to a pixel middle-click
        // at the element's screen-space center.
        let button_str = args.str_or("button", "left").to_lowercase();
        // delivery_mode: per-call ladder rung. Foreground briefly activates the
        // target for both AX and pixel paths, then restores the prior app.
        let delivery_mode = super::DeliveryMode::parse(args.opt_str("delivery_mode").as_deref());
        // Reject unknown buttons explicitly so silent left-click fall-through can't
        // mask a typo. Keep "" → default left for old clients that never sent the field.
        if !matches!(button_str.as_str(), "" | "left" | "right" | "middle") {
            return ToolResult::error(format!(
                "click: unknown button \"{button_str}\" — expected one of left, right, middle."
            ));
        }
        let button_str = if button_str.is_empty() {
            "left".to_string()
        } else {
            button_str
        };
        let count = args.u64_or("count", 1) as usize;
        let from_zoom = args.bool_or("from_zoom", false);
        let debug_image_out = args.opt_str("debug_image_out");
        let modifiers: Vec<String> = args.str_array("modifier");

        // PID-routed key transitions can look correct for one AX poll and then
        // collapse to a plain click once AppKit resolves the gesture. Refuse
        // that false-success path. The explicit foreground rung uses physical
        // HID modifier transitions under an exact-window activation guard.
        if !modifiers.is_empty() && (!delivery_mode.is_foreground() || window_id.is_none()) {
            return ToolResult::error(
                "click modifiers require delivery_mode:\"foreground\" and window_id on macOS; \
                 background PID-routed events cannot preserve live modifier-key state",
            )
            .with_structured(serde_json::json!({
                "code": "background_unavailable",
                "effect": "refused",
                "escalation": {
                    "recommended": "foreground",
                    "reason": "macOS modifier clicks require exact-window HID delivery so the target observes live modifier state"
                }
            }));
        }

        if let (Some(idx), Some(wid), Some(element_guard)) =
            (element_index, window_id, element_guard)
        {
            let element_ptr = element_guard.as_ptr();

            // ── Exact-target background gate (macOS background input v1) ──
            // The element branch is semantic AX delivery, except button=middle
            // which falls back to a routed pixel click at the element's center
            // and is therefore held to the stricter WindowPointer rung. Gate
            // BEFORE any cursor/dispatch work so a stale or sibling-owned
            // target refuses instead of acting on the wrong window.
            let _mutation_lease = if !delivery_mode.is_foreground() {
                let gate_action = if button_str == "middle" {
                    cua_driver_core::background_input::BackgroundAction::WindowPointer
                } else {
                    cua_driver_core::background_input::BackgroundAction::AxSemantic
                };
                match super::gate_background_window_action(pid, wid, Some(element_ptr), gate_action)
                    .await
                {
                    Ok(lease) => Some(lease),
                    Err(refusal_result) => return refusal_result,
                }
            } else {
                None
            };

            // Fast path: check the cached element against what the agent saw.
            match crate::fast_path_guard::guard_element(
                "click",
                pid,
                wid,
                &args,
                element_ptr,
                button_str == "middle",
            )
            .await
            {
                crate::fast_path_guard::Outcome::Refuse(refusal) => return refusal,
                crate::fast_path_guard::Outcome::Proceed { summary, .. } => *fast_path = summary,
            }

            // Surface 5: button=right on the AX path → AXShowMenu (the same surface
            // the dedicated `right_click` tool dispatches). Threads through the
            // identical perform_ax_click code path with the action remapped.
            let effective_action = if button_str == "right" && action == "press" {
                "show_menu".to_string()
            } else {
                action.clone()
            };

            // A background press or show-menu on a Chromium <select> would
            // open its menu and raise the browser window (CUA-1795): steer to
            // set_value before anything is sent.
            if !delivery_mode.is_foreground()
                && matches!(effective_action.as_str(), "press" | "show_menu")
            {
                let select_guard = element_guard.clone();
                let refusal = tokio::task::spawn_blocking(move || {
                    let element = select_guard.as_ptr() as AXUIElementRef;
                    super::set_value::is_chromium_web_select(pid, wid, element).then(|| {
                        super::set_value::chromium_select_open_refusal(pid, wid, Some(idx), element)
                    })
                })
                .await
                .ok()
                .flatten();
                if let Some(refusal) = refusal {
                    return refusal;
                }
            }

            // Animate cursor to element center BEFORE firing AX action,
            // mirroring Swift's `performElementClick` → `animateAndWait(to:)`.
            let center_guard = element_guard.clone();
            // The element's screen rect rides along on the glide so motion
            // styles can time by target size and highlight the target.
            let (center, target_rect) = tokio::task::spawn_blocking(move || unsafe {
                let el = center_guard.as_ptr() as AXUIElementRef;
                (
                    crate::ax::bindings::element_screen_center(el),
                    element_screen_rect(el),
                )
            })
            .await
            .unwrap_or((None, None));

            // Surface 5: button=middle on the AX path has no AX equivalent.
            // Fall back to a pixel middle-click at the element's screen-space center
            // so the request still produces a real middle-button event (browser tab
            // close, autoscroll, etc.). If we can't resolve a center, error rather
            // than silently degrade to AXPress.
            if button_str == "middle" {
                if let Some(refusal) = super::refuse_if_disabled(
                    pid,
                    wid,
                    &element_guard,
                    "middle_click",
                    delivery_mode.is_foreground(),
                )
                .await
                {
                    return refusal;
                }
                let (cx, cy) = match center {
                    Some(c) => c,
                    None => {
                        return ToolResult::error(
                            "click(button=middle) on element_token: could not resolve element \
                         center for the pixel-middle-click fallback. Pass x, y directly.",
                        )
                    }
                };
                crate::cursor::overlay::send_command(
                    cursor_key.clone(),
                    cursor_overlay::OverlayCommand::PinAbove(wid as u64),
                );
                crate::cursor::overlay::animate_cursor_to_target(
                    cursor_key.clone(),
                    cx,
                    cy,
                    target_rect,
                )
                .await;
                self.state
                    .cursor_registry
                    .update_position(&cursor_key, cx, cy);
                self.state.cursor_registry.note_press(&cursor_key, cx, cy);

                let mods_owned = modifiers.clone();
                let foreground = delivery_mode.is_foreground();
                let middle_guard = element_guard.clone();
                let result = tokio::task::spawn_blocking(move || {
                    let m: Vec<&str> = mods_owned.iter().map(String::as_str).collect();
                    // Re-read after any activation: the early read skips a
                    // foreground call into a background app.
                    let ensure_enabled = || {
                        crate::input::ax_actions::ensure_ax_action_enabled(
                            middle_guard.as_ptr(),
                            "middle_click",
                        )
                    };
                    if foreground && !m.is_empty() {
                        crate::input::skylight::with_foreground_hid_activation(
                            pid as libc::pid_t,
                            wid,
                            || {
                                ensure_enabled()?;
                                crate::input::mouse::click_at_xy_desktop_with_modifiers_preserving_cursor(
                                    cx, cy, 1, "middle", &m,
                                )
                            },
                        )
                    } else if foreground {
                        crate::input::skylight::with_foreground_assist(
                            pid as libc::pid_t,
                            wid,
                            || {
                                ensure_enabled()?;
                                crate::input::mouse::middle_click_at_xy(pid, cx, cy, &m)
                            },
                        )
                        .map(|_| ())
                    } else {
                        ensure_enabled()?;
                        crate::input::mouse::middle_click_at_xy(pid, cx, cy, &m)
                    }
                })
                .await;
                return match result {
                    Ok(Ok(())) => ToolResult::text(format!(
                        "✅ Posted middle-click to pid {pid} at element [{idx}] center \
                         (background CGEvent; not driver-verified — confirm via screenshot)."
                    ))
                    .with_structured(serde_json::json!({ "path": "cgevent", "verified": false, "effect": "unverifiable" })),
                    Ok(Err(e)) => super::element_disabled_error(pid, wid, &e, foreground)
                        .unwrap_or_else(|| ToolResult::error(format!("Middle-click failed: {e}"))),
                    Err(e)     => ToolResult::error(format!("Task error: {e}")),
                };
            }

            if let Some((cx, cy)) = center {
                // Pin overlay above target window first.
                crate::cursor::overlay::send_command(
                    cursor_key.clone(),
                    cursor_overlay::OverlayCommand::PinAbove(wid as u64),
                );
                crate::cursor::overlay::animate_cursor_to_target(
                    cursor_key.clone(),
                    cx,
                    cy,
                    target_rect,
                )
                .await;
                // Keep the registry in sync with the overlay so
                // get_agent_cursor_state reports a truthful position even when
                // the click was dispatched via the AX path (no pixel coords).
                self.state
                    .cursor_registry
                    .update_position(&cursor_key, cx, cy);
                self.state.cursor_registry.note_press(&cursor_key, cx, cy);
            }

            // Finder icon/list items can expose a readable AXSelected state
            // while refusing both AXSelected writes and AXPress. Resolve a
            // verified coordinate frame only for those collection-like
            // elements so perform_ax_click can cross that one failed semantic
            // rung internally and confirm the result by AX read-back.
            let selection_center = if effective_action == "press" {
                let selection_guard = element_guard.clone();
                tokio::task::spawn_blocking(move || {
                    crate::input::ax_actions::nearest_container_selection_state(
                        selection_guard.as_ptr(),
                    )
                    .and_then(|_| nearest_selectable_container_center(selection_guard.as_ptr()))
                })
                .await
                .unwrap_or(None)
            } else {
                None
            };
            let mut selection_pixel = if let Some(screen_center) = selection_center {
                super::px_frame::resolve_or_refuse(wid)
                    .await
                    .ok()
                    .map(|frame| {
                        selection_pixel_target(screen_center, (frame.bounds.x, frame.bounds.y))
                    })
            } else {
                None
            };
            // The selection fallback delivers a routed window-local pixel
            // click — a stricter (WindowPointer) rung than the semantic gate
            // above. In background, drop the fallback rather than silently
            // escalate when the pointer rung would refuse (e.g. a
            // minimized/hidden target); the semantic path still runs.
            if selection_pixel.is_some()
                && !delivery_mode.is_foreground()
                && _mutation_lease
                    .as_ref()
                    .expect("background element actions hold the per-pid lease")
                    .gate_again(
                        wid,
                        Some(element_ptr),
                        cua_driver_core::background_input::BackgroundAction::WindowPointer,
                    )
                    .await
                    .is_err()
            {
                selection_pixel = None;
            }

            // ── Focus-suppression wrap (Swift WindowChangeDetector + FocusGuard) ──
            // Capture prior frontmost, arm the wildcard suppressor in the
            // snapshot, then arm a targeted suppressor across the AX action
            // itself via FocusGuard. After the action returns, detect any
            // new-window / foreground side-effects and append a one-liner
            // suffix matching Swift's wording.
            let prior_front = apps::frontmost_pid();
            let foreground = delivery_mode.is_foreground();
            let snapshot = if foreground {
                WindowChangeDetector::snapshot_without_suppression(prior_front)
            } else {
                WindowChangeDetector::snapshot(prior_front)
            };

            // Run AX work on a blocking thread (can't block async executor).
            // Use `effective_action` so button=right rewrites press → show_menu.
            let action_clone = effective_action.clone();
            // Thread the resolved session cursor key into the blocking AX path
            // so its ShowFocusRect + ClickPulse land on THIS session's cursor,
            // not the shared "default" one (which would light the wrong cursor
            // and stomp default for a non-default session).
            let ck = cursor_key.clone();
            let selection_modifiers = modifiers.clone();
            let result = focus_guard::with_focus_suppressed(
                if foreground { None } else { Some(pid) },
                prior_front,
                "click.AXPress",
                || async move {
                    tokio::task::spawn_blocking(move || {
                        let element_ptr = element_guard.as_ptr();
                        if foreground {
                            let mut outcome = None;
                            let has_modifiers = !selection_modifiers.is_empty();
                            let action = || {
                                outcome = Some(perform_ax_click(
                                    (element_ptr, idx),
                                    (pid, wid),
                                    &action_clone,
                                    &ck,
                                    selection_pixel,
                                    &selection_modifiers,
                                    foreground,
                                )?);
                                std::thread::sleep(std::time::Duration::from_millis(150));
                                Ok(())
                            };
                            let fronted = if has_modifiers {
                                crate::input::skylight::with_foreground_hid_activation(
                                    pid as libc::pid_t,
                                    wid,
                                    action,
                                )?;
                                true
                            } else {
                                crate::input::skylight::with_foreground_assist(
                                    pid as libc::pid_t,
                                    wid,
                                    action,
                                )?
                            };
                            let outcome = outcome.ok_or_else(|| {
                                anyhow::anyhow!("foreground AX click did not execute")
                            })?;
                            Ok((outcome, fronted))
                        } else {
                            perform_ax_click(
                                (element_ptr, idx),
                                (pid, wid),
                                &action_clone,
                                &ck,
                                selection_pixel,
                                &selection_modifiers,
                                false,
                            )
                            .map(|outcome| (outcome, false))
                        }
                    })
                    .await
                },
            )
            .await;

            // Drop the wildcard lease + detect window/foreground side-effects.
            let acted_at = std::time::Instant::now();
            let changes = super::finish_window_observation(snapshot).await;

            match result {
                Ok(Ok((
                    (
                        mut msg,
                        needs_webkit_delay,
                        suspected_noop,
                        selection_verified,
                        selection_via_pixel,
                    ),
                    fronted,
                ))) => {
                    // For text inputs, wait 800ms for WebKit DOM focus to settle
                    // before returning — matches the Swift reference behaviour.
                    // The window watch above already used part of it.
                    if needs_webkit_delay {
                        let settle = std::time::Duration::from_millis(800)
                            .saturating_sub(acted_at.elapsed());
                        tokio::time::sleep(settle).await;
                    }
                    msg.push_str(&changes.result_suffix());
                    // AX dispatch went through, but AXPerformAction returning
                    // success does not confirm the on-screen effect (many elements
                    // no-op silently). A click is never driver-verifiable (no
                    // read-back) → verified:false stays for back-compat. The
                    // tri-state `effect` is the richer signal:
                    //   * suspected_noop — the element didn't advertise the action,
                    //     so the press likely did nothing → cross to vision/pixel.
                    //   * unverifiable — dispatched fine, driver just can't confirm;
                    //     the caller verifies via screenshot.
                    let mut structured = serde_json::json!({
                        "path": if selection_via_pixel {
                            if fronted { "cgevent_fg" } else { "cgevent" }
                        } else if fronted {
                            "ax_fg"
                        } else {
                            "ax"
                        },
                        "verified": selection_verified,
                        "effect": if selection_verified {
                            "confirmed"
                        } else if suspected_noop {
                            "suspected_noop"
                        } else {
                            "unverifiable"
                        },
                    });
                    if selection_verified {
                        structured["evidence"] = serde_json::json!([
                            { "kind": "accessibility_readback" }
                        ]);
                    }
                    if suspected_noop {
                        structured["escalation"] = serde_json::json!({
                            "recommended": "px",
                            "reason": "element does not advertise this action — the \
                                       AX press likely no-op'd. Do an element px \
                                       action: click by pixel (x,y) off the \
                                       screenshot from get_window_state."
                        });
                    }
                    ToolResult::text(msg).with_structured(structured)
                }
                Ok(Err(e)) => match e.downcast_ref::<crate::input::ax_actions::ElementDisabled>() {
                    Some(disabled) => {
                        super::element_disabled_result(pid, wid, disabled, foreground)
                    }
                    None => ToolResult::error(format!("AX action failed: {e}")),
                },
                Err(e) => ToolResult::error(format!("Task error: {e}")),
            }
        } else if let (Some(mut cx), Some(mut cy)) = (x, y) {
            // ── Pixel path ─────────────────────────────────────────────────

            if capture_id.is_some() && (from_zoom || debug_image_out.is_some()) {
                return ToolResult::error(
                    "click.capture_id is incompatible with from_zoom and debug_image_out; \
                     pass coordinates from the exact source capture directly.",
                )
                .with_structured(serde_json::json!({ "code": "invalid_arguments" }));
            }

            // debug_image_out: capture fresh screenshot, overlay crosshair BEFORE
            // any coordinate translation (so it shows received coords in the same
            // space the caller was reasoning in).
            if let Some(ref dbg_path) = debug_image_out {
                if from_zoom {
                    return ToolResult::error(
                        "debug_image_out is incompatible with from_zoom — \
                         received (x, y) would be in zoom-crop space, not window-local.",
                    );
                }
                match window_id {
                    None => return ToolResult::error("debug_image_out requires window_id."),
                    Some(wid) => {
                        // Session-effective max dimension so debug_image_out
                        // matches the resize the calling session sees in
                        // get_window_state (precedence: session override > global).
                        let max_dim = self.state.session_config.effective_max_image_dimension(
                            args.opt_str("_session_id").as_deref(),
                            &self.state.config.read().unwrap(),
                        );
                        let dbg_path_c = dbg_path.clone();
                        let dbg_result = tokio::task::spawn_blocking(move || {
                            let png = crate::capture::screenshot_window_bytes(wid)?;
                            let png = crate::capture::resize_png_if_needed(&png, max_dim)?;
                            crate::capture::write_crosshair_png(&png, cx, cy, &dbg_path_c)
                        })
                        .await;
                        match dbg_result {
                            Err(e) => {
                                return ToolResult::error(format!(
                                    "debug_image_out task failed: {e}. Not dispatching click."
                                ))
                            }
                            Ok(Err(e)) => {
                                return ToolResult::error(format!(
                                    "debug_image_out write failed: {e}. Not dispatching click."
                                ))
                            }
                            Ok(Ok(())) => {}
                        }
                    }
                }
            }

            if let Some(ref capture_id) = capture_id {
                let wid = match window_id {
                    Some(window_id) => window_id,
                    None => {
                        return ToolResult::error(
                            "window capture_id requires the matching pid and window_id.",
                        )
                        .with_structured(serde_json::json!({ "code": "invalid_arguments" }));
                    }
                };
                match self
                    .state
                    .capture_bindings
                    .admit_window_click(capture_id, &args, pid, wid, cx, cy)
                {
                    Ok((action_x, action_y)) => {
                        cx = action_x;
                        cy = action_y;
                    }
                    Err(refusal) => return refusal,
                }
            } else if from_zoom {
                match super::zoom_context(&self.state, &args, pid, window_id) {
                    Ok(ctx) => {
                        let (wx, wy) = ctx.zoom_to_window(cx, cy);
                        cx = wx;
                        cy = wy;
                    }
                    Err(refusal) => return refusal,
                }
            } else {
                let ratio = match super::screenshot_scale(&self.state, &args, pid, window_id) {
                    Ok(ratio) => ratio,
                    Err(refusal) => return refusal,
                };
                // Coordinates are in the downscaled image space; scale back to native pixels.
                cx *= ratio;
                cy *= ratio;
            }

            // ── Window-local → screen coordinate translation ──────────────────
            // `click_at_xy` accepts screen-space coordinates (top-left origin).
            // Callers supply window-local screenshot pixels; `px_frame` adds the
            // window's screen-origin (and divides out the Retina backing scale)
            // to produce the final screen position, or refuses when the window
            // has no live frame — see px_frame's module docs for why there is
            // no screen-absolute fallback.
            //
            // win_local_x/y: window-local logical-pixel coords used when the
            // Chromium recipe falls back to public PID posting. Its SkyLight
            // route uses the translated screen coordinates instead.
            let (screen_x, screen_y, win_local_x, win_local_y) = if let Some(wid) = window_id {
                match super::px_frame::resolve_or_refuse(wid).await {
                    Ok(frame) => {
                        let (sx, sy, lx, ly) = frame.to_screen(cx, cy);
                        // A window-local point outside the live frame would
                        // dispatch onto whatever occupies that screen point —
                        // the same wrong-surface misclick class as #2237.
                        // Refuse in background, where the caller cannot see
                        // what is actually under the translated point.
                        if !delivery_mode.is_foreground()
                            && (lx < 0.0
                                || ly < 0.0
                                || lx > frame.bounds.width
                                || ly > frame.bounds.height)
                        {
                            return ToolResult::error(format!(
                                "click: window-local point ({lx:.1}, {ly:.1}) pt lies outside \
                                 window {wid}'s {:.0}×{:.0} pt frame; background delivery \
                                 refused. Re-read coordinates from a fresh get_window_state \
                                 screenshot.",
                                frame.bounds.width, frame.bounds.height
                            ));
                        }
                        (sx, sy, lx, ly)
                    }
                    Err(refusal) => return refusal,
                }
            } else {
                // No window_id → treat x,y as screen coordinates (legacy behaviour).
                (cx, cy, cx, cy)
            };

            // ── Exact-target background gate (macOS background input v1) ──
            // A window-addressed background pixel action targets coordinates,
            // which only mean something while the exact window is current and
            // not minimized/hidden: a stale target would let the pid-scoped
            // hit-test or routed events land on a same-process sibling. Gate
            // BEFORE the AX hit-test backend and any cursor/dispatch work.
            // delivery_mode:"foreground" stays the explicit last resort.
            let mutation_lease_held = crate::background_mutation::held_by_current_task(pid);
            let _mutation_lease = if !delivery_mode.is_foreground() && !mutation_lease_held {
                if let Some(wid) = window_id {
                    match super::gate_background_window_action(
                        pid,
                        wid,
                        None,
                        cua_driver_core::background_input::BackgroundAction::WindowPointer,
                    )
                    .await
                    {
                        Ok(lease) => Some(lease),
                        Err(refusal_result) => return refusal_result,
                    }
                } else {
                    None
                }
            } else {
                None
            };

            // Fast path: window guards, then the AX or pixel check at the
            // point, before the cursor moves (so hover cannot change the crop).
            let (screen_x, screen_y, win_local_x, win_local_y) = if let Some(wid) = window_id {
                match crate::fast_path_guard::guard_pixel(
                    "click",
                    pid,
                    wid,
                    (win_local_x, win_local_y),
                    (screen_x, screen_y),
                    delivery_mode.is_foreground(),
                )
                .await
                {
                    crate::fast_path_guard::Outcome::Refuse(refusal) => return refusal,
                    crate::fast_path_guard::Outcome::Proceed { summary, relocate } => {
                        *fast_path = summary;
                        match relocate {
                            Some((dx, dy)) => (
                                screen_x + dx,
                                screen_y + dy,
                                win_local_x + dx,
                                win_local_y + dy,
                            ),
                            None => (screen_x, screen_y, win_local_x, win_local_y),
                        }
                    }
                }
            } else {
                (screen_x, screen_y, win_local_x, win_local_y)
            };

            // Pin the overlay above the target window BEFORE animating so
            // the cursor is already sandwiched correctly while it glides in.
            // Both PX deliveries below (AX hit-test and routed events) share
            // this glide, so the cursor shows whichever one lands the click.
            if let Some(wid) = window_id {
                crate::cursor::overlay::send_command(
                    cursor_key.clone(),
                    cursor_overlay::OverlayCommand::PinAbove(wid as u64),
                );
            }
            // Animate the visual cursor to the click point and wait for it to
            // arrive — mirrors Swift's `AgentCursor.shared.animateAndWait(to:)`.
            // A background pixel click on a Chromium <select> would open its
            // menu and raise the browser window (CUA-1795).
            if let Some(select_wid) = window_id.filter(|_| !delivery_mode.is_foreground()) {
                let refusal = tokio::task::spawn_blocking(move || unsafe {
                    let element = element_at_screen_position(pid, screen_x, screen_y)?;
                    let refusal = (crate::ax::exact_target::element_window_id(element)
                        .is_some_and(|window| window.is(select_wid))
                        && super::set_value::is_chromium_web_select(pid, select_wid, element))
                    .then(|| {
                        super::set_value::chromium_select_open_refusal(
                            pid, select_wid, None, element,
                        )
                    });
                    CFRelease(element as _);
                    refusal
                })
                .await
                .ok()
                .flatten();
                if let Some(refusal) = refusal {
                    return refusal;
                }
            }
            crate::cursor::overlay::animate_cursor_to(cursor_key.clone(), screen_x, screen_y).await;
            // Keep the registry in sync with the overlay (see AX path above).
            self.state
                .cursor_registry
                .update_position(&cursor_key, screen_x, screen_y);
            self.state
                .cursor_registry
                .note_press(&cursor_key, screen_x, screen_y);

            // A background PX action can still use an accessibility delivery
            // backend after resolving the requested screen point. This keeps
            // targeting (PX) orthogonal to delivery (AX) and avoids making a
            // Chromium/AppKit window key merely to satisfy first-mouse rules.
            if let Some(hit_test_wid) = window_id.filter(|_| {
                !delivery_mode.is_foreground()
                    && button_str == "left"
                    && count == 1
                    && modifiers.is_empty()
            }) {
                let focus_only = action == "focus";
                let ax_result = tokio::task::spawn_blocking(move || unsafe {
                    let Some(element) = element_at_screen_position(pid, screen_x, screen_y) else {
                        return Ok::<bool, anyhow::Error>(false);
                    };
                    // The pid-scoped hit-test can resolve an element from a
                    // same-process sibling overlapping the requested point.
                    // Require proven ancestry in the requested window before
                    // acting; otherwise fall through to the routed pixel path
                    // (already gated for this exact window).
                    if !crate::ax::exact_target::element_window_id(element)
                        .is_some_and(|window| window.is(hit_test_wid))
                    {
                        CFRelease(element as _);
                        return Ok(false);
                    }
                    // An element that does not advertise AXPress can still
                    // accept it as a no-op. On a toolkit that drops routed
                    // mouse events (LibreOffice's Calc cells) that no-op used
                    // to come back as a pressed click; fall through so the
                    // pixel route refuses instead.
                    if !focus_only
                        && !crate::ax::bindings::copy_action_names(element)
                            .iter()
                            .any(|action| action == "AXPress")
                        && crate::input::pointer_toolkit::detect(pid)
                            == Some(crate::input::pointer_toolkit::PointerReadingToolkit::Vcl)
                    {
                        CFRelease(element as _);
                        return Ok(false);
                    }
                    let delivered = if focus_only {
                        crate::input::ax_actions::focus_element(element as usize).is_ok()
                    } else {
                        let press = core_foundation::string::CFString::new("AXPress");
                        AXUIElementPerformAction(element, press.as_concrete_TypeRef())
                            == kAXErrorSuccess
                    };
                    CFRelease(element as _);
                    Ok(delivered)
                })
                .await;
                match ax_result {
                    Ok(Ok(true)) => {
                        crate::cursor::overlay::send_command(
                            cursor_key.clone(),
                            cursor_overlay::OverlayCommand::ClickPulse {
                                x: screen_x,
                                y: screen_y,
                            },
                        );
                        let label = if focus_only { "focused" } else { "pressed" };
                        return ToolResult::text(format!(
                            "✅ PX hit-test {label} the background element via AX."
                        ))
                        .with_structured(serde_json::json!({
                            "path": "ax",
                            "verified": false,
                            "effect": "unverifiable"
                        }));
                    }
                    Ok(Ok(false)) if focus_only => {
                        return ToolResult::error(
                            "Background PX focus is unavailable at the requested point.".to_owned(),
                        )
                        .with_structured(serde_json::json!({
                            "code": "background_unavailable"
                        }));
                    }
                    Ok(Err(error)) if focus_only => {
                        return ToolResult::error(format!("Background PX focus failed: {error}"))
                            .with_structured(serde_json::json!({
                                "code": "background_unavailable"
                            }));
                    }
                    _ => {}
                }
            }

            // Resolve the effective delivery posture before observation. A
            // requested foreground click without a window id still degrades to
            // background, matching the existing contract and result label.
            let fg = delivery_mode.is_foreground() && window_id.is_some();
            // Background delivery cannot satisfy a toolkit that reads the
            // hardware pointer; refuse before any activation or dispatch.
            let route = match super::pixel_route::resolve(pid, fg, window_id, "mouse_click").await {
                Ok(route) => route,
                Err(refusal) => return refusal,
            };
            let activation_policy = pixel_activation_policy(&button_str, fg, window_id.is_some());

            // ── Focus-suppression wrap (Swift WindowChangeDetector + FocusGuard) ──
            // A pixel click can land on a "Sign In" button that opens a sheet
            // or a Safari link that activates a new tab — same side-effect
            // shape as the AX path, so we wrap identically.
            let prior_front = apps::frontmost_pid();
            let undo = crate::focus_arbiter::UndoWatch::arm();
            let snapshot = match activation_policy {
                PixelActivationPolicy::SuppressTarget => {
                    WindowChangeDetector::snapshot(prior_front)
                }
                PixelActivationPolicy::AllowTargetWithoutRaise => {
                    WindowChangeDetector::snapshot_allowing_activation(prior_front, pid)
                }
                PixelActivationPolicy::ForegroundAssist => {
                    WindowChangeDetector::snapshot_without_suppression(prior_front)
                }
            };

            // A raw click can open a menu (a popup drawn without AX, a canvas
            // context menu); keep the user's key focus if it does.
            let _key_focus = if fg {
                None
            } else {
                tokio::task::spawn_blocking(move || crate::key_focus_guard::protect(pid))
                    .await
                    .ok()
                    .flatten()
            };

            // The user's key window, read before the click, so the restore
            // below hands key status back to exactly that window.
            let prior_key_wid = match prior_front {
                Some(prior)
                    if activation_policy == PixelActivationPolicy::AllowTargetWithoutRaise
                        && prior != pid =>
                {
                    tokio::task::spawn_blocking(move || {
                        crate::input::skylight::key_window_of_pid(prior)
                    })
                    .await
                    .ok()
                    .flatten()
                }
                _ => None,
            };

            // Restore the Swift background-click prologue that was left
            // disconnected in the original Rust port. It makes an opaque
            // target AppKit-active without raising/restacking its window, which
            // is required by Chromium gates and remote-HID proxies such as
            // iPhone Mirroring. Re-pin after the focus record because changing
            // AppKit active state can disturb overlay ordering.
            let focus_without_raise = if activation_policy
                == PixelActivationPolicy::AllowTargetWithoutRaise
            {
                let wid = window_id.expect("activation policy requires window_id");
                match tokio::task::spawn_blocking(move || {
                    crate::input::mouse::prepare_background_pixel_click(pid, wid)
                })
                .await
                {
                    Ok(activated) => {
                        if !activated && crate::input::skylight::windowserver_connection_invalid() {
                            return super::windowserver_connection_refusal(pid, Some(wid));
                        }
                        crate::cursor::overlay::send_command(
                            cursor_key.clone(),
                            cursor_overlay::OverlayCommand::PinAbove(wid as u64),
                        );
                        activated
                    }
                    Err(error) => {
                        return ToolResult::error(format!(
                            "Background click activation task failed: {error}"
                        ));
                    }
                }
            } else {
                false
            };

            // Pulse only after the activation settle so it visually coincides
            // with the real target click rather than the private focus prelude.
            crate::cursor::overlay::send_command(
                cursor_key.clone(),
                cursor_overlay::OverlayCommand::ClickPulse {
                    x: screen_x,
                    y: screen_y,
                },
            );

            let mods_owned = modifiers.clone();
            // Surface 5: route to the right/middle CGEvent primitives when
            // button != left. Left-button path stays on the existing Chromium-
            // routed `click_at_xy_with_window_local` for back-compat.
            let button_kind = button_str.clone();
            let result = focus_guard::with_focus_suppressed(
                if activation_policy == PixelActivationPolicy::SuppressTarget {
                    Some(pid)
                } else {
                    None
                },
                prior_front,
                "click.pixel",
                || async move {
                    tokio::task::spawn_blocking(move || {
                        let do_click = move || -> anyhow::Result<()> {
                            let m: Vec<&str> = mods_owned.iter().map(String::as_str).collect();
                            if route == PixelClickRoute::ForegroundHid {
                                // Warp the hardware pointer to the mapped global
                                // point and post at the HID tap. The pointer stays
                                // at the target, as on Windows and X11, so apps that
                                // read the pointer when handling the event see it.
                                return crate::input::mouse::click_at_xy_desktop_with_modifiers(
                                    screen_x,
                                    screen_y,
                                    count,
                                    &button_kind,
                                    &m,
                                );
                            }
                            match button_kind.as_str() {
                                "right" => {
                                    if let Some(wid) = window_id {
                                        return crate::input::mouse::right_click_at_xy_with_window_local(
                                            pid, screen_x, screen_y, win_local_x, win_local_y, wid, &m,
                                        );
                                    }
                                    crate::input::mouse::right_click_at_xy(pid, screen_x, screen_y, &m)
                                }
                                "middle" => {
                                    if let Some(wid) = window_id {
                                        return crate::input::mouse::middle_click_at_xy_with_window_local(
                                            pid, screen_x, screen_y, win_local_x, win_local_y, wid, &m,
                                        );
                                    }
                                    crate::input::mouse::middle_click_at_xy(pid, screen_x, screen_y, &m)
                                }
                                // "left" (default) or anything else — preserve legacy left-click path.
                                _ => {
                                    // When we know the window_id, use the targeted primitive so
                                    // foreground delivery can stamp window-local coordinates and
                                    // background delivery can retain the translated screen point
                                    // while adding Chromium routing fields (f40, f51, f58, f91, f92).
                                    if let Some(wid) = window_id {
                                        return crate::input::mouse::click_at_xy_with_window_local(
                                            pid, screen_x, screen_y,
                                            win_local_x, win_local_y,
                                            wid, count, &m,
                                            crate::input::mouse::WindowClickDelivery::from_foreground(fg),
                                        );
                                    }
                                    crate::input::mouse::click_at_xy(pid, screen_x, screen_y, count, &m)
                                }
                            }
                        };
                        // Foreground rung: front the exact window → HID click →
                        // restore the prior front process. The HID tap has no
                        // pid addressing, so activation must be proven (the
                        // window is AX-focused) or no input is sent.
                        match (route, window_id) {
                            (PixelClickRoute::ForegroundHid, Some(wid)) => {
                                crate::input::skylight::with_foreground_hid_activation(
                                    pid as libc::pid_t,
                                    wid,
                                    do_click,
                                )
                            }
                            _ => do_click(),
                        }
                    })
                    .await
                },
            )
            .await;

            if activation_policy == PixelActivationPolicy::AllowTargetWithoutRaise {
                restore_user_focus_after_background_pixel(
                    undo,
                    pid,
                    prior_front,
                    prior_key_wid,
                    window_id,
                    focus_without_raise,
                )
                .await;
            }

            let changes = super::finish_window_observation(snapshot).await;

            let button_label = match (button_str.as_str(), count) {
                ("right", _) => "right-click",
                ("middle", _) => "middle-click",
                (_, 2) => "double-click",
                _ => "click",
            };
            match result {
                Ok(Ok(())) => {
                    let target = if route == PixelClickRoute::ForegroundHid {
                        format!("at screen-point ({screen_x:.0},{screen_y:.0}) for pid {pid}")
                    } else {
                        format!("to pid {pid}")
                    };
                    ToolResult::text(format!(
                        "✅ Posted {button_label} {target} ({}).{}",
                        super::pixel_route::delivery_note(route),
                        changes.result_suffix()
                    ))
                    .with_structured(serde_json::json!({
                        "path": super::pixel_route::path_label(route),
                        "verified": false,
                        "effect": "unverifiable",
                        "focus_without_raise": focus_without_raise
                    }))
                }
                Ok(Err(e)) if route == PixelClickRoute::ForegroundHid => {
                    super::pixel_route::foreground_unavailable(
                        button_label,
                        window_id.unwrap_or_default(),
                        &e.to_string(),
                    )
                }
                Ok(Err(e)) => ToolResult::error(format!("{button_label} failed: {e}")),
                Err(e) => ToolResult::error(format!("Task error: {e}")),
            }
        } else {
            ToolResult::error("Provide either element_token or (x + y). pid is required for x,y clicks; an element_token carries its own pid.")
        }
    }
}

// ── AX click implementation (blocking) ───────────────────────────────────────

/// Returns `(summary_text, needs_webkit_delay, suspected_noop,
/// selection_verified, selection_via_pixel)`.
///
/// `suspected_noop` is true when the element did not advertise the action we
/// dispatched — AXUIElementPerformAction returns success regardless, so this is
/// the driver's only signal that the press likely did nothing. The caller turns
/// it into `effect: "suspected_noop"` + an escalation hint so the agent crosses
/// to the vision/pixel path instead of trusting a hollow success.
///
/// `element` is the cached AX element pointer and its snapshot index; `window`
/// is the target (pid, window_id).
/// Whether a background AXPress needs its window to be the app's key window.
///
/// A toolbar or format-bar control in a native app sends its action to the
/// first responder through the key window (TextEdit's Bold checkbox applies
/// the trait to the text view that way). With the app behind the user's
/// window nothing is key, so the press flips the control but the document
/// does not change. Browsers and Electron shells run page content in their own
/// process and take no part in this.
fn press_needs_key_window(action: &str, front_pid: Option<i32>, pid: i32, native: bool) -> bool {
    action == "AXPress" && native && front_pid != Some(pid)
}

/// Make the target window AppKit-key without raising it, activating the app
/// or taking key status from the user's window (the focus record only, as the
/// background pixel click does), then let AppKit settle.
fn prime_key_window_for_background_press(pid: i32, window_id: u32, action: &str) {
    let native = !super::type_text::is_browser_pid(pid)
        && !crate::browser::electron_js::ElectronJs::is_electron(pid);
    if press_needs_key_window(action, apps::frontmost_pid(), pid, native)
        && crate::input::skylight::activate_without_raise_with(pid, window_id, false)
    {
        std::thread::sleep(std::time::Duration::from_millis(80));
    }
}

/// Whether an AX press on a text input must wait for the page to move DOM
/// focus (800 ms). Only text inputs inside web content (an `AXWebArea`
/// ancestor) have a DOM focus to settle; waiting on a native field only
/// delays the result. `ancestor_roles` is read only for a text input pressed
/// with `AXPress`.
fn needs_web_focus_settle(
    ax_action: &str,
    role: &str,
    ancestor_roles: impl FnOnce() -> Vec<String>,
) -> bool {
    ax_action == "AXPress"
        && matches!(role, "AXTextField" | "AXTextArea")
        && ancestor_roles().iter().any(|role| role == "AXWebArea")
}

/// Roles of `element`'s ancestors, nearest first, up to the window. Bounded so
/// a cyclic or very deep tree cannot stall the click.
///
/// # Safety
///
/// `element` must be a valid, live `AXUIElementRef`.
unsafe fn ancestor_roles(element: AXUIElementRef) -> Vec<String> {
    const MAX_DEPTH: usize = 40;
    let mut roles = Vec::new();
    let mut current = copy_element_attr(element, "AXParent");
    while let Some(parent) = current {
        if roles.len() >= MAX_DEPTH {
            CFRelease(parent as CFTypeRef);
            break;
        }
        let role = copy_string_attr(parent, "AXRole").unwrap_or_default();
        let next = copy_element_attr(parent, "AXParent");
        CFRelease(parent as CFTypeRef);
        let is_window = role == "AXWindow";
        roles.push(role);
        if is_window {
            if let Some(next) = next {
                CFRelease(next as CFTypeRef);
            }
            break;
        }
        current = next;
    }
    roles
}

fn perform_ax_click(
    element: (usize, usize),
    window: (i32, u32),
    action_str: &str,
    cursor_key: &str,
    selection_pixel: Option<SelectionPixelTarget>,
    modifiers: &[String],
    foreground: bool,
) -> anyhow::Result<(String, bool, bool, bool, bool)> {
    let (element_ptr, idx) = element;
    let (pid, window_id) = window;
    let ax_action = crate::input::ax_actions::map_action(action_str)?;
    let element = element_ptr as AXUIElementRef;

    // Check the live value immediately before dispatch. Foreground assist can
    // enable menu items that were disabled in the cached snapshot, while a
    // background transition can disable them after that snapshot. macOS may
    // otherwise return success for a disabled action that did nothing.
    crate::input::ax_actions::ensure_ax_action_enabled(element_ptr, ax_action)?;

    // Capture advertised actions BEFORE dispatching so we can detect silent no-ops
    // (AX returns success even when the element doesn't advertise the action).
    let advertised = unsafe { copy_action_names(element) };

    let role = unsafe { copy_string_attr(element, "AXRole") }.unwrap_or_default();
    let title = unsafe { copy_string_attr(element, "AXTitle") }.unwrap_or_default();

    // A click on an AppKit collection item is frequently represented by a
    // label child or row that does not advertise AXPress. Prefer a bounded,
    // read-back-verified AXSelected write over dispatching a known hollow press
    // or forcing the caller onto a less stable pixel coordinate.
    if ax_action == "AXPress" && !advertised.iter().any(|action| action == ax_action) {
        if modifiers.is_empty() {
            if let Some(selected_role) =
                crate::input::ax_actions::select_nearest_container(element_ptr)
            {
                return Ok((
                    format!(
                        "✅ Selected nearest {selected_role} for [{idx}] {role} \"{title}\"; \
                         confirmed AXSelected=true."
                    ),
                    false,
                    false,
                    true,
                    false,
                ));
            }
        }

        if let (Some(target), Some(selection)) = (
            selection_pixel,
            crate::input::ax_actions::capture_nearest_container_selection(element_ptr),
        ) {
            let selected_role = selection.role().to_owned();
            let Some((before, _)) = selection.observe() else {
                anyhow::bail!("selection target stopped exposing AXSelected before delivery");
            };
            let modifier_refs: Vec<&str> = modifiers.iter().map(String::as_str).collect();
            if foreground && !modifier_refs.is_empty() {
                crate::input::mouse::click_at_xy_desktop_with_modifiers_preserving_cursor(
                    target.screen_x,
                    target.screen_y,
                    1,
                    "left",
                    &modifier_refs,
                )?;
            } else {
                crate::input::mouse::click_at_xy_with_window_local(
                    pid,
                    target.screen_x,
                    target.screen_y,
                    target.window_x,
                    target.window_y,
                    window_id,
                    1,
                    &modifier_refs,
                    crate::input::mouse::WindowClickDelivery::from_foreground(foreground),
                )?;
            }
            // AppKit may publish a transient AXSelected transition while the
            // event queue is still resolving the gesture. Let it settle before
            // accepting a candidate, then require the same state to survive a
            // second observation. A modified selection additionally preserves
            // every peer that was selected before delivery.
            std::thread::sleep(SELECTION_READBACK_SETTLE);
            let deadline = std::time::Instant::now() + SELECTION_READBACK_TIMEOUT;
            let mut last_observation = None;
            loop {
                if let Some((after, peers_preserved)) = selection.observe() {
                    last_observation = Some((after, peers_preserved));
                    let verified = selection_readback_confirms(
                        before,
                        after,
                        !modifiers.is_empty(),
                        peers_preserved,
                    );
                    if verified {
                        std::thread::sleep(SELECTION_READBACK_STABILITY);
                        if let Some((stable_after, stable_peers_preserved)) = selection.observe() {
                            last_observation = Some((stable_after, stable_peers_preserved));
                            if stable_after == after
                                && selection_readback_confirms(
                                    before,
                                    stable_after,
                                    !modifiers.is_empty(),
                                    stable_peers_preserved,
                                )
                            {
                                return Ok((
                                    format!(
                                        "✅ Selected nearest {selected_role} for [{idx}] {role} \
                                         \"{title}\"; AX selection write was unavailable, so a \
                                         coordinate click was delivered and confirmed by stable \
                                         AXSelected read-back."
                                    ),
                                    false,
                                    false,
                                    true,
                                    true,
                                ));
                            }
                        }
                    }
                }
                if std::time::Instant::now() >= deadline {
                    break;
                }
                std::thread::sleep(SELECTION_READBACK_POLL);
            }
            if modifiers.is_empty() {
                anyhow::bail!(
                    "coordinate click did not produce a stable AXSelected transition; \
                     last_readback={last_observation:?}; retry after a fresh snapshot"
                );
            }
            anyhow::bail!(
                "foreground modified coordinate click did not produce a stable AXSelected \
                 transition while preserving the prior selection; \
                 before_selected={before}, last_readback={last_observation:?}; \
                 take a fresh snapshot before retrying"
            );
        }
    }

    let read_value = || unsafe {
        crate::ax::bindings::copy_stringish_attr(element, "AXValue").map(|value| value.state_value)
    };
    let toggle_before = is_toggle_press(ax_action, &role, &advertised)
        .then(read_value)
        .flatten();
    if !foreground {
        prime_key_window_for_background_press(pid, window_id, ax_action);
    }
    let err = unsafe { crate::ax::bindings::perform_action(element, ax_action) };
    if err != crate::ax::bindings::kAXErrorSuccess {
        // Checked before the row-selection fallback, which writes AXSelected
        // on an ancestor: a toggle whose press landed needs no other write.
        if let Some(before) = toggle_before.filter(|_| press_error_may_have_acted(err)) {
            if let Some(now) = settled_value(
                |now| toggle_press_shows(&role, &before, now),
                read_value,
                SELECTION_READBACK_TIMEOUT,
                SELECTION_READBACK_POLL,
                SELECTION_READBACK_STABILITY,
            ) {
                return Ok((
                    format!(
                        "✅ Performed {ax_action} on [{idx}] {role} \"{title}\"; AXValue is now \
                         {now} (was {before}) on two reads, although the app returned AX error \
                         {err}."
                    ),
                    false,
                    false,
                    true,
                    false,
                ));
            }
        }
        // Some collection rows claim a click-like action but Finder returns
        // kAXErrorCannotComplete. Use the same verified selection fallback
        // before surfacing the dispatch error.
        if ax_action == "AXPress" && modifiers.is_empty() {
            if let Some(selected_role) =
                crate::input::ax_actions::select_nearest_container(element_ptr)
            {
                return Ok((
                    format!(
                        "✅ Selected nearest {selected_role} for [{idx}] {role} \"{title}\" \
                         after AXPress returned {err}; confirmed AXSelected=true."
                    ),
                    false,
                    false,
                    true,
                    false,
                ));
            }
        }
        anyhow::bail!("AXUIElementPerformAction({ax_action}) returned {err}");
    }

    let mut summary = format!("✅ Performed {ax_action} on [{idx}] {role} \"{title}\".");

    // AXPopUpButton: list available options, redirect to set_value.
    if role == "AXPopUpButton" {
        let children = unsafe { copy_children(element) };
        if !children.is_empty() {
            let options: Vec<String> = children
                .iter()
                .filter_map(|&child| {
                    let t = unsafe { copy_string_attr(child, "AXTitle") }.unwrap_or_default();
                    let v = unsafe { copy_string_attr(child, "AXValue") }.unwrap_or_default();
                    if t.is_empty() && v.is_empty() {
                        return None;
                    }
                    Some(if v.is_empty() || v == t {
                        format!("\"{t}\"")
                    } else {
                        format!("\"{t}\" (value: {v})")
                    })
                })
                .collect();
            for &child in &children {
                unsafe {
                    CFRelease(child as _);
                }
            }

            if !options.is_empty() {
                let opt_list = options.join(", ");
                summary.push_str(
                    "\n\nThis is a popup/select button; its menu is open. Choose an item \
                     by clicking its AXMenuItem from a fresh get_window_state, or select \
                     in one call with:\n  set_value(pid, element_token, value)\n\
                     Available options: [",
                );
                summary.push_str(&opt_list);
                summary.push(']');
            }
        }
    }

    // Advertised-action warning: non-fatal but surfaces likely no-ops. Also the
    // machine-readable `suspected_noop` signal returned to the caller.
    let suspected_noop = !advertised.contains(&ax_action.to_string());
    if suspected_noop {
        let adv_list = if advertised.is_empty() {
            "none".into()
        } else {
            advertised.join(", ")
        };
        summary.push_str(&format!(
            "\n⚠️ Element does not advertise {ax_action} (actions: {adv_list}). \
             Action may have been a no-op."
        ));
    }

    // DOM focus settle: 800 ms for text inputs in web content (returned to the
    // async caller). A native text field has no DOM focus to wait for.
    let needs_webkit_delay =
        needs_web_focus_settle(ax_action, &role, || unsafe { ancestor_roles(element) });

    // Offer the focus-rect highlight around the element (matches Swift
    // showFocusRect); the overlay paints it only with `cursor.marks` on. Also
    // move the cursor to the element center so the glide animation plays.
    if let Some(rect) = unsafe { element_screen_rect(element) } {
        // Drive THIS session's cursor (threaded in via `cursor_key`), matching
        // the keyed glide already played in the invoke body above. The keyed
        // glide already played on the session's cursor in the invoke body.
        crate::cursor::overlay::send_command(
            cursor_key.to_owned(),
            cursor_overlay::OverlayCommand::ShowFocusRect(Some(rect)),
        );
        // Animate cursor to element center.
        let cx = rect[0] + rect[2] / 2.0;
        let cy = rect[1] + rect[3] / 2.0;
        crate::cursor::overlay::send_command(
            cursor_key.to_owned(),
            cursor_overlay::OverlayCommand::ClickPulse { x: cx, y: cy },
        );
    }
    let _ = pid;
    let _ = window_id; // used by caller context

    Ok((summary, needs_webkit_delay, suspected_noop, false, false))
}

#[cfg(test)]
mod web_focus_settle_tests {
    use super::needs_web_focus_settle;

    fn roles(list: &[&str]) -> impl FnOnce() -> Vec<String> {
        let list: Vec<String> = list.iter().map(|role| (*role).to_owned()).collect();
        move || list
    }

    #[test]
    fn a_text_input_in_web_content_waits_for_dom_focus() {
        assert!(needs_web_focus_settle(
            "AXPress",
            "AXTextField",
            roles(&["AXGroup", "AXWebArea", "AXScrollArea", "AXWindow"])
        ));
        assert!(needs_web_focus_settle(
            "AXPress",
            "AXTextArea",
            roles(&["AXWebArea", "AXWindow"])
        ));
    }

    #[test]
    fn a_native_text_input_does_not_wait() {
        assert!(!needs_web_focus_settle(
            "AXPress",
            "AXTextField",
            roles(&["AXGroup", "AXScrollArea", "AXWindow"])
        ));
        assert!(!needs_web_focus_settle("AXPress", "AXTextArea", roles(&[])));
    }

    #[test]
    fn only_a_press_on_a_text_input_is_considered() {
        for (action, role) in [
            ("AXShowMenu", "AXTextField"),
            ("AXPress", "AXButton"),
            ("AXPress", "AXCheckBox"),
        ] {
            assert!(!needs_web_focus_settle(action, role, || {
                panic!("ancestors are not read for {action} on {role}")
            }));
        }
    }
}

#[cfg(test)]
mod selection_fallback_tests {
    use super::{selection_pixel_target, selection_readback_confirms};

    #[test]
    fn selection_pixel_uses_container_center_in_both_coordinate_spaces() {
        let target = selection_pixel_target((360.0, 240.0), (100.0, 80.0));
        assert_eq!(target.screen_x, 360.0);
        assert_eq!(target.screen_y, 240.0);
        assert_eq!(target.window_x, 260.0);
        assert_eq!(target.window_y, 160.0);
    }

    #[test]
    fn plain_click_requires_selected_readback() {
        assert!(selection_readback_confirms(false, true, false, true));
        assert!(selection_readback_confirms(true, true, false, true));
        assert!(!selection_readback_confirms(false, false, false, true));
    }

    #[test]
    fn modified_click_requires_a_transition_and_preserves_prior_selection() {
        assert!(selection_readback_confirms(false, true, true, true));
        assert!(selection_readback_confirms(true, false, true, true));
        assert!(!selection_readback_confirms(true, true, true, true));
        assert!(!selection_readback_confirms(false, true, true, false));
    }
}

// ── Tests ────────────────────────────────────────────────────────────────────

/// Check `click`'s `action` before any dispatch. Element clicks take the
/// AX action names `map_action` knows. A pixel click also takes `focus`: the
/// background focus rung that pixel `type_text`, `press_key` and `scroll`
/// use to put the keyboard focus on a point (`focus_by_pixel`). It sets
/// AXFocused on the element under the point instead of pressing it.
fn validate_click_action(action: &str, element_target: bool) -> anyhow::Result<()> {
    if !element_target && action == "focus" {
        return Ok(());
    }
    crate::input::ax_actions::map_action(action).map(|_| ())
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The pixel focus rung keeps working after unknown AX action names
    /// started refusing: background pixel typing into Electron depended on it.
    #[test]
    fn pixel_click_accepts_the_internal_focus_action() {
        assert!(validate_click_action("focus", false).is_ok());
        assert!(validate_click_action("press", false).is_ok());
        assert!(validate_click_action("show_alternate_ui", true).is_ok());
        assert!(validate_click_action("focus", true).is_err());
        assert!(validate_click_action("hover", false).is_err());
    }

    /// #3835: Finder's toolbar view switcher applies an AXPress and returns
    /// -25205. Only a radio button or checkbox qualifies for the read-back.
    #[test]
    fn toggle_press_is_an_advertised_press_on_a_radio_or_checkbox() {
        let press = vec!["AXPress".to_owned()];
        assert!(is_toggle_press("AXPress", "AXRadioButton", &press));
        assert!(is_toggle_press("AXPress", "AXCheckBox", &press));
        assert!(
            !is_toggle_press("AXPress", "AXButton", &press),
            "a button has no value that shows the press"
        );
        assert!(!is_toggle_press("AXPick", "AXRadioButton", &press));
        assert!(
            !is_toggle_press("AXPress", "AXRadioButton", &[]),
            "AXPress not advertised"
        );
    }

    /// Only errors an app returns after acting qualify; errors that mean the
    /// request never reached the app keep failing (#3835).
    #[test]
    fn only_errors_an_app_returns_after_acting_qualify() {
        assert!(press_error_may_have_acted(-25200));
        assert!(press_error_may_have_acted(-25205));
        assert!(press_error_may_have_acted(-25206));
        for refused in [-25201, -25202, -25204, -25211] {
            assert!(!press_error_may_have_acted(refused), "{refused}");
        }
    }

    /// A checkbox press shows as any state change; a radio press only as the
    /// radio becoming selected.
    #[test]
    fn toggle_press_shows_as_the_value_the_press_produces() {
        assert!(toggle_press_shows("AXCheckBox", "0", "1"));
        assert!(toggle_press_shows("AXCheckBox", "1", "0"));
        assert!(toggle_press_shows("AXCheckBox", "2", "1"), "mixed state");
        assert!(!toggle_press_shows("AXCheckBox", "1", "1"));
        assert!(toggle_press_shows("AXRadioButton", "0", "1"));
        assert!(
            !toggle_press_shows("AXRadioButton", "1", "0"),
            "a radio turning off was another radio's press"
        );
        assert!(!toggle_press_shows("AXRadioButton", "1", "1"));
    }

    /// An erroring toggle press counts only when the value shows the press
    /// and a second read agrees on it.
    #[test]
    fn erroring_toggle_press_counts_only_when_its_value_settles() {
        let zero = std::time::Duration::ZERO;
        let settled = |reads: Vec<Option<&str>>| {
            let mut reads = reads.into_iter().map(|read| read.map(str::to_owned));
            let shows = |now: &str| toggle_press_shows("AXRadioButton", "0", now);
            settled_value(shows, || reads.next().flatten(), zero, zero, zero)
        };
        assert_eq!(settled(vec![Some("1"), Some("1")]), Some("1".to_owned()));
        assert_eq!(settled(vec![Some("1"), Some("0")]), None, "flickered back");
        assert_eq!(settled(vec![Some("1"), None]), None, "second read failed");
        assert_eq!(settled(vec![Some("0")]), None, "never moved");
        assert_eq!(settled(vec![None]), None, "unreadable");
    }

    /// Surface 5: schema must advertise the new `button` field with the three
    /// canonical values and default to "left". Hermes / Codex / Claude Code
    /// consumers branch on this enum being present.
    #[test]
    fn background_press_needs_the_key_window_only_in_a_native_background_app() {
        assert!(press_needs_key_window("AXPress", Some(1), 2, true));
        assert!(
            !press_needs_key_window("AXPress", Some(2), 2, true),
            "frontmost"
        );
        assert!(
            !press_needs_key_window("AXPress", Some(1), 2, false),
            "browser or Electron"
        );
        assert!(!press_needs_key_window("AXShowMenu", Some(1), 2, true));
    }

    #[test]
    fn schema_advertises_button_enum() {
        let d = def();
        let props = d.input_schema.get("properties").expect("properties");
        let button = props.get("button").expect("button field present");
        let kind = button.get("type").and_then(|v| v.as_str());
        assert_eq!(kind, Some("string"));
        let enum_vals: Vec<&str> = button
            .get("enum")
            .and_then(|v| v.as_array())
            .expect("button.enum present")
            .iter()
            .filter_map(|v| v.as_str())
            .collect();
        assert!(enum_vals.contains(&"left"));
        assert!(enum_vals.contains(&"right"));
        assert!(enum_vals.contains(&"middle"));
        assert_eq!(props["capture_id"]["type"], "string");
    }

    /// Surface 5 hard constraint: the tool description must mention the
    /// `button` argument and the "left" default so MCP introspection (which
    /// pipes description into LLM prompts) carries the back-compat note.
    #[test]
    fn description_mentions_button_default() {
        let d = def();
        let desc = d.description.to_ascii_lowercase();
        assert!(
            desc.contains("button"),
            "description should mention button arg"
        );
        assert!(
            desc.contains("left"),
            "description should mention left default"
        );
        assert!(
            desc.contains("middle"),
            "description should mention middle button"
        );
    }

    /// Regression for the Swift→Rust port gap: only a raw background left
    /// click with an exact window may intentionally activate the target
    /// without raising it. Other background buttons retain strict suppression,
    /// and the explicit foreground rung owns its separate activation.
    #[test]
    fn raw_background_left_click_restores_focus_without_raise_policy() {
        assert_eq!(
            pixel_activation_policy("left", false, true),
            PixelActivationPolicy::AllowTargetWithoutRaise
        );
        assert_eq!(
            pixel_activation_policy("left", false, false),
            PixelActivationPolicy::SuppressTarget
        );
        assert_eq!(
            pixel_activation_policy("right", false, true),
            PixelActivationPolicy::SuppressTarget
        );
        assert_eq!(
            pixel_activation_policy("middle", false, true),
            PixelActivationPolicy::SuppressTarget
        );
        assert_eq!(
            pixel_activation_policy("left", true, true),
            PixelActivationPolicy::ForegroundAssist
        );
    }

    /// Bench v2: re-posting the focus record to a user window that stayed key
    /// re-activated the user's app after every background pixel click.
    #[test]
    fn user_focus_record_only_when_focus_moved() {
        assert!(!user_focus_needs_restore(false, true, Some(7), 7));
        assert!(!user_focus_needs_restore(false, true, None, 7));
        assert!(!user_focus_needs_restore(false, false, Some(42), 7));
        assert!(user_focus_needs_restore(false, true, Some(42), 7));
        assert!(user_focus_needs_restore(true, false, Some(7), 7));
    }

    /// The no-foreground contract must not depend on the private activation
    /// recipe reporting full success. If that recipe is unavailable or only
    /// partially succeeds but the target is nevertheless observed frontmost,
    /// restore the user's prior app.
    #[test]
    fn failed_private_activation_still_restores_observed_target_focus() {
        assert_eq!(
            background_pixel_restore_pid(
                PixelActivationPolicy::AllowTargetWithoutRaise,
                Some(7),
                42,
                Some(42),
            ),
            Some(7)
        );

        assert_eq!(
            background_pixel_restore_pid(
                PixelActivationPolicy::AllowTargetWithoutRaise,
                Some(7),
                42,
                Some(99),
            ),
            None,
            "do not overwrite an unrelated app that became frontmost"
        );
        assert_eq!(
            background_pixel_restore_pid(
                PixelActivationPolicy::SuppressTarget,
                Some(7),
                42,
                Some(42),
            ),
            None,
            "strict-suppression paths retain their existing ownership"
        );
    }
}