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.

feat(cua-driver): let macOS actions skip the post-action window-change wait (#6033)

injaneity41591b1b3969
diff --git a/docs/content/docs/cua-driver/reference/mcp-tools/click-and-type.mdx b/docs/content/docs/cua-driver/reference/mcp-tools/click-and-type.mdx
index c9ff962a12..127ff9b2b8 100644
--- a/docs/content/docs/cua-driver/reference/mcp-tools/click-and-type.mdx
+++ b/docs/content/docs/cua-driver/reference/mcp-tools/click-and-type.mdx
@@ -57,6 +57,7 @@ Click a target pid. Prefer `element_token` (from get_window_state): it identifie
| <span id="click--macos--from_zoom"></span>`from_zoom` | `boolean` | | x and y are in the last zoom image for this pid. |
| <span id="click--macos--modifier"></span>`modifier` | `string[]` | | Modifier keys: cmd, shift, option/alt, ctrl. |
| <span id="click--macos--pid"></span>`pid` | `integer` | | Target process ID. Optional with element_token (it carries the pid). |
+| <span id="click--macos--post_action_wait"></span>`post_action_wait` | `"default" \| "none"` | | Whether the result waits for the post-action window-change report (default "default": up to 300 ms for a new window, menu, dialog or foreground change). "none" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged. |
| <span id="click--macos--scope"></span>`scope` | `"window" \| "desktop"` | | Default "window". "desktop": x,y with no pid/window_id are true screen pixels from get_desktop_state. |
| <span id="click--macos--session"></span>`session` | `string` | | For multi-call work, prefer a short public session label and repeat it on every call that accepts it. |
| <span id="click--macos--window_id"></span>`window_id` | `integer` | | Target window ID. Omit with element_token. |
@@ -164,6 +165,7 @@ Pixel path (x, y provided): two down/up pairs ~80 ms apart at the given coordina
| Parameter | Type | Default | Description |
| --- | --- | --- | --- |
| <span id="double_click--macos--delivery_mode"></span>`delivery_mode` | `"background" \| "foreground"` | | Best-effort-background ladder rung (default "background"). "background": inject without fronting or raising the target; no focus steal. "foreground": briefly front the target, act, then restore the prior frontmost; the explicit last resort when a background attempt didn't land. Re-call with "foreground" only for the action that needs it. |
+| <span id="double_click--macos--post_action_wait"></span>`post_action_wait` | `"default" \| "none"` | | Whether the result waits for the post-action window-change report (default "default": up to 300 ms for a new window, menu, dialog or foreground change). "none" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged. |
| <span id="double_click--macos--window_id"></span>`window_id` | `integer` | | CGWindowID. Omit when element_token is supplied (the token carries it). |
| <span id="double_click--macos--x"></span>`x` | `number` | | Screen X coordinate (pixel path). |
| <span id="double_click--macos--y"></span>`y` | `number` | | Screen Y coordinate (pixel path). |
@@ -239,6 +241,7 @@ Exactly one of `element_token` or (`x` AND `y`) must be provided. `pid` is requi
| --- | --- | --- | --- |
| <span id="right_click--macos--delivery_mode"></span>`delivery_mode` | `"background" \| "foreground"` | | Best-effort-background ladder rung (default "background"). "background": inject without fronting or raising the target; no focus steal. "foreground": briefly front the target, act, then restore the prior frontmost; the explicit last resort when a background attempt didn't land. Re-call with "foreground" only for the action that needs it. |
| <span id="right_click--macos--modifier"></span>`modifier` | `string[]` | | Modifier keys held during the right-click: cmd/shift/option/ctrl. Pixel path only. |
+| <span id="right_click--macos--post_action_wait"></span>`post_action_wait` | `"default" \| "none"` | | Whether the result waits for the post-action window-change report (default "default": up to 300 ms for a new window, menu, dialog or foreground change). "none" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged. |
| <span id="right_click--macos--window_id"></span>`window_id` | `integer` | | CGWindowID. Omit when element_token is supplied (the token carries it). |
| <span id="right_click--macos--x"></span>`x` | `number` | | X in window-local screenshot pixels. Must be provided together with y. |
| <span id="right_click--macos--y"></span>`y` | `number` | | Y in window-local screenshot pixels. Must be provided together with x. |
@@ -317,6 +320,7 @@ Web content (browser tabs, Slack, VS Code): AXValue is not proof the DOM saw the
| <span id="type_text--macos--delivery_mode"></span>`delivery_mode` | `"background" \| "foreground"` | | Default "background": AX insert, then CGEvent keys, no focus steal. "foreground": front the window, type, restore; last resort for surfaces (WhatsApp/Catalyst) where background keys do not land. |
| <span id="type_text--macos--element_token"></span>`element_token` | `string` | | Element handle `<snapshot_id>:<row>` from get_window_state: row `[11]` of a read whose snapshot_id is s0000002a is "s0000002a:11" (also in `structuredContent.elements[].element_token`). Returns an explicit stale error naming the current snapshots once a newer read supersedes it. |
| <span id="type_text--macos--pid"></span>`pid` | `integer` | | Target process ID. |
+| <span id="type_text--macos--post_action_wait"></span>`post_action_wait` | `"default" \| "none"` | | Whether the result waits for the post-action window-change report (default "default": up to 300 ms for a new window, menu, dialog or foreground change). "none" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged. |
| <span id="type_text--macos--scope"></span>`scope` | `"window" \| "desktop"` | `"window"` | "desktop" with no pid/window_id types into the frontmost app. |
| <span id="type_text--macos--session"></span>`session` | `string` | | For multi-call work, prefer a short public session label and repeat it on every call that accepts it. |
| <span id="type_text--macos--text"></span>`text` | `string` | required | Text to insert. |
@@ -415,6 +419,7 @@ Confirmed only when a native AX value/selection read-back changes; otherwise `ef
| <span id="press_key--macos--key"></span>`key` | `string` | required | Key name. |
| <span id="press_key--macos--modifiers"></span>`modifiers` | `string[]` | | cmd, shift, option/alt, ctrl, fn. |
| <span id="press_key--macos--pid"></span>`pid` | `integer` | | Target process ID. |
+| <span id="press_key--macos--post_action_wait"></span>`post_action_wait` | `"default" \| "none"` | | Whether the result waits for the post-action window-change report (default "default": up to 300 ms for a new window, menu, dialog or foreground change). "none" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged. |
| <span id="press_key--macos--scope"></span>`scope` | `"window" \| "desktop"` | `"window"` | "desktop" with no pid/window_id sends to the frontmost app. |
| <span id="press_key--macos--session"></span>`session` | `string` | | For multi-call work, prefer a short public session label and repeat it on every call that accepts it. |
| <span id="press_key--macos--window_id"></span>`window_id` | `integer` | | Target window; required for foreground. Does not raise by itself. |
@@ -508,6 +513,7 @@ Recognized modifiers: cmd/command, shift, option/alt, ctrl/control, fn. Non-modi
| <span id="hotkey--macos--delivery_mode"></span>`delivery_mode` | `"background" \| "foreground"` | | Best-effort-background ladder rung (default "background"). "background": inject without fronting or raising the target; no focus steal. "foreground": briefly front the target, act, then restore the prior frontmost; the explicit last resort when a background attempt didn't land. Re-call with "foreground" only for the action that needs it. |
| <span id="hotkey--macos--keys"></span>`keys` | `string[]` | required | Modifier(s) and one non-modifier key, e.g. ["cmd", "c"]. Items: 2 to any. |
| <span id="hotkey--macos--pid"></span>`pid` | `integer` | | Target process ID. Required with x, y; optional with element_token (the token carries the pid). |
+| <span id="hotkey--macos--post_action_wait"></span>`post_action_wait` | `"default" \| "none"` | | Whether the result waits for the post-action window-change report (default "default": up to 300 ms for a new window, menu, dialog or foreground change). "none" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged. |
| <span id="hotkey--macos--scope"></span>`scope` | `"window" \| "desktop"` | `"window"` | Use desktop with no pid/window_id to send the chord to the frontmost application. |
| <span id="hotkey--macos--window_id"></span>`window_id` | `integer` | | Target window. Required for delivery_mode:"foreground" (the NSMenu activation needs a window). Does NOT itself raise the window: raising is gated on delivery_mode. |
| <span id="hotkey--macos--x"></span>`x` | `number` | | Screenshot-pixel X: the element px action form: pixel-click there to focus, then send the combo (so e.g. Cmd+V pastes into that field). Pass with y. Use for Chromium/Electron surfaces the background combo can't reach. |
@@ -599,6 +605,7 @@ A Finder file name cell (list or icon view, not being edited) is renamed through
| Parameter | Type | Default | Description |
| --- | --- | --- | --- |
+| <span id="set_value--macos--post_action_wait"></span>`post_action_wait` | `"default" \| "none"` | | Whether the result waits for the post-action window-change report (default "default": up to 300 ms for a new window, menu, dialog or foreground change). "none" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged. |
| <span id="set_value--macos--session"></span>`session` | `string` | | For multi-call work, prefer a short public session label and repeat it on every call that accepts it. |
| <span id="set_value--macos--value"></span>`value` | `string` | required | New value, coerced to the element's type. |
| <span id="set_value--macos--window_id"></span>`window_id` | `integer` | | Window ID. Omit with element_token. |
diff --git a/docs/content/docs/cua-driver/reference/mcp-tools/pointer-cursor-and-browser.mdx b/docs/content/docs/cua-driver/reference/mcp-tools/pointer-cursor-and-browser.mdx
index 2048ad84b3..4b961f659e 100644
--- a/docs/content/docs/cua-driver/reference/mcp-tools/pointer-cursor-and-browser.mdx
+++ b/docs/content/docs/cua-driver/reference/mcp-tools/pointer-cursor-and-browser.mdx
@@ -74,6 +74,7 @@ A window-scoped drag (pid + window_id) runs in the background by default: the ge
| <span id="drag--macos--from_zoom"></span>`from_zoom` | `boolean` | | When true, coordinates are in the last zoom image for this pid; driver maps back to window coordinates. |
| <span id="drag--macos--modifier"></span>`modifier` | `string[]` | | Modifier keys held across the entire gesture: cmd/shift/option/ctrl. |
| <span id="drag--macos--pid"></span>`pid` | `integer` | | Target process ID. |
+| <span id="drag--macos--post_action_wait"></span>`post_action_wait` | `"default" \| "none"` | | Whether the result waits for the post-action window-change report (default "default": up to 300 ms for a new window, menu, dialog or foreground change). "none" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged. |
| <span id="drag--macos--scope"></span>`scope` | `"window" \| "desktop"` | `"window"` | Use desktop with no pid/window_id for native get_desktop_state screenshot coordinates. |
| <span id="drag--macos--steps"></span>`steps` | `integer` | | Number of intermediate mouseDragged events linearly interpolated along the path. Default: 20. Range: 1 to 200. |
| <span id="drag--macos--to_x"></span>`to_x` | `number` | required | Drag-end X in window-local screenshot pixels. |
@@ -169,6 +170,7 @@ Electron/Chromium windows drop background wheel events: there a background scrol
| <span id="scroll--macos--amount"></span>`amount` | `integer` | | Wheel notches or key repetitions, clamped to the maximum. Default 3. Range: 1 to 50. |
| <span id="scroll--macos--delivery_mode"></span>`delivery_mode` | `"background" \| "foreground"` | | Default "background": no fronting or focus steal. "foreground": briefly front the window, act, restore the prior app; last resort when background did not land. |
| <span id="scroll--macos--pid"></span>`pid` | `integer` | | Target process ID. Required unless scope is "desktop" or element_token is supplied. |
+| <span id="scroll--macos--post_action_wait"></span>`post_action_wait` | `"default" \| "none"` | | Whether the result waits for the post-action window-change report (default "default": up to 300 ms for a new window, menu, dialog or foreground change). "none" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged. |
| <span id="scroll--macos--scope"></span>`scope` | `"window" \| "desktop"` | `"window"` | "desktop" with x,y and no pid/window_id uses get_desktop_state coordinates. |
| <span id="scroll--macos--session"></span>`session` | `string` | | For multi-call work, prefer a short public session label and repeat it on every call that accepts it. |
| <span id="scroll--macos--window_id"></span>`window_id` | `integer` | | Window ID. Required with x/y; optional with element_token. |
diff --git a/libs/cua-driver/rust/Skills/cua-driver/EMBEDDING.md b/libs/cua-driver/rust/Skills/cua-driver/EMBEDDING.md
index 8ab682382f..2fa3cf262d 100644
--- a/libs/cua-driver/rust/Skills/cua-driver/EMBEDDING.md
+++ b/libs/cua-driver/rust/Skills/cua-driver/EMBEDDING.md
@@ -281,6 +281,33 @@ example to record a demo, sets `CUA_DRIVER_BATCH_PACING=full` in the same
environment as the variables above. Any other value, or none, applies the
shortened waits. No tool argument can change it.
+### Skipping the report wait per call (macOS)
+
+An agent that reads the window state after every action does not need the
+result to wait for the window watch. Each action that waits for it (`click`,
+`double_click`, `right_click`, `drag`, `scroll`, `type_text`, `press_key`,
+`hotkey` and `set_value`) accepts an optional `post_action_wait` argument:
+
+- `"default"`, or omitting it, keeps the behavior above.
+- `"none"` returns as soon as the input is delivered. The result ends with
+ `Window changes not observed: post_action_wait "none" returned without
+ waiting.` instead of reporting no change. The watch still runs detached for
+ its whole deadline and keeps the focus-steal lease, exactly like the
+ remainder of a quiet watch above, so focus protection is unchanged and a
+ window that opens is reported on the next action's result.
+
+Linux and Windows do not advertise `post_action_wait`, and their action
+schemas refuse it as an unknown argument (`invalid_arguments`): the Linux X11
+foreground transaction uses its window watch as delivery evidence, and the
+Windows adapter does not wait for window changes after an action.
+
+Measured on macOS 26 with Calculator in the background (medians of 20 calls
+over three runs), an `element_token` click (AXPress) took 570 to 750 ms with
+the default and 250 to 350 ms with `"none"`. A coordinate click that
+Calculator routes through the pixel path took 870 to 1160 ms and 510 to 690 ms.
+A coordinate click that resolves to an AX press through the hit-test does not
+watch the window list, so the argument does not change it.
+
## Pacing synthesized keystrokes (macOS)
The macOS keyboard adapter waits a fixed gap between each key down and up and
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/lib.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/lib.rs
index f34132090a..730e294b4a 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/src/lib.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/lib.rs
@@ -90,6 +90,7 @@ pub mod perception_tools;
pub mod pip_hook;
pub mod pointer_shape;
pub mod policy;
+pub mod post_action_wait;
pub mod protocol;
pub mod recording;
pub mod recording_loader;
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/post_action_wait.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/post_action_wait.rs
new file mode 100644
index 0000000000..8018c4f1ec
--- /dev/null
+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/post_action_wait.rs
@@ -0,0 +1,148 @@
+//! Whether an action's result waits for its post-action window-change report.
+//!
+//! After an input action the macOS adapter watches the window list for a short
+//! time (300 ms for a direct call) so its result can name a menu, dialog, new
+//! window or foreground change the action caused. For an agent that reads the
+//! window state after every action anyway, that wait is most of a background
+//! click's latency.
+//!
+//! The optional per-call argument [`POST_ACTION_WAIT_ARG`] lets a caller skip
+//! it: with `"none"` the result returns as soon as the input is delivered and
+//! says that window changes were not observed. The watch itself, and the
+//! focus-steal suppression it holds, still run detached for the full deadline,
+//! and a change it sees is reported on the next action's result. Omitted, or
+//! `"default"`, keeps the current behavior.
+//!
+//! The dispatch boundary reads the argument and runs the tool inside [`scope`];
+//! adapters read [`current`]. Only macOS implements the skip: the Linux X11
+//! foreground transaction uses its window watch as delivery evidence, and the
+//! Windows adapter does not watch for window changes after an action. On those
+//! platforms the argument is not advertised, and the closed action schemas
+//! refuse it as an unknown argument.
+
+use std::future::Future;
+
+use serde_json::{json, Value};
+
+/// The per-call argument name.
+pub const POST_ACTION_WAIT_ARG: &str = "post_action_wait";
+
+/// Tools whose result waits for the post-action window-change report, and
+/// therefore accept [`POST_ACTION_WAIT_ARG`].
+pub const POST_ACTION_WAIT_TOOLS: &[&str] = &[
+ "click",
+ "double_click",
+ "right_click",
+ "drag",
+ "scroll",
+ "type_text",
+ "press_key",
+ "hotkey",
+ "set_value",
+];
+
+/// Whether this platform's adapter implements `"none"`.
+pub const SKIP_SUPPORTED: bool = cfg!(target_os = "macos");
+
+/// The caller's choice for the action being dispatched.
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+pub enum PostActionWait {
+ /// Wait for the window-change report as usual.
+ #[default]
+ Default,
+ /// Return without waiting; the result says nothing was observed.
+ None,
+}
+
+tokio::task_local! {
+ static CURRENT: PostActionWait;
+}
+
+/// Run `future` with `wait` as the choice of every action it dispatches.
+pub async fn scope<F: Future>(wait: PostActionWait, future: F) -> F::Output {
+ CURRENT.scope(wait, future).await
+}
+
+/// The choice for the action being dispatched; [`PostActionWait::Default`]
+/// outside a [`scope`].
+pub fn current() -> PostActionWait {
+ CURRENT.try_with(|wait| *wait).unwrap_or_default()
+}
+
+/// Whether `tool` accepts [`POST_ACTION_WAIT_ARG`] on this platform. Only
+/// macOS implements the skip; elsewhere the argument is not advertised, so the
+/// closed action schemas refuse it as an unknown argument.
+pub fn accepts(tool: &str) -> bool {
+ SKIP_SUPPORTED && POST_ACTION_WAIT_TOOLS.contains(&tool)
+}
+
+/// The JSON schema of [`POST_ACTION_WAIT_ARG`].
+pub fn schema() -> Value {
+ json!({
+ "type": "string",
+ "enum": ["default", "none"],
+ "description": "Whether the result waits for the post-action window-change report \
+ (default \"default\": up to 300 ms for a new window, menu, dialog or \
+ foreground change). \"none\" returns as soon as the input is delivered \
+ and says window changes were not observed; the watch keeps running in \
+ the background and a change it sees is reported on the next action's \
+ result. Only paths that already observe window changes are affected; \
+ for example a right_click that does not watch is unchanged."
+ })
+}
+
+/// Read the caller's choice from a `tool` call's arguments. `Err` carries the
+/// message and structured content of the `invalid_arguments` refusal.
+pub fn from_args(tool: &str, args: &Value) -> Result<PostActionWait, (String, Value)> {
+ match args.get(POST_ACTION_WAIT_ARG) {
+ None => Ok(PostActionWait::Default),
+ Some(Value::String(s)) if s == "default" => Ok(PostActionWait::Default),
+ Some(Value::String(s)) if s == "none" => Ok(PostActionWait::None),
+ Some(other) => {
+ let detail = format!("post_action_wait must be \"default\" or \"none\", got {other}");
+ Err((
+ format!("{tool}: {detail}"),
+ json!({ "code": "invalid_arguments", "tool": tool, "detail": detail }),
+ ))
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn omitted_and_default_keep_the_wait() {
+ for args in [json!({}), json!({"post_action_wait": "default"})] {
+ assert_eq!(from_args("click", &args).unwrap(), PostActionWait::Default);
+ }
+ }
+
+ #[test]
+ fn none_skips_the_wait() {
+ assert_eq!(
+ from_args("click", &json!({"post_action_wait": "none"})).unwrap(),
+ PostActionWait::None
+ );
+ }
+
+ #[test]
+ fn other_values_are_invalid() {
+ for value in [json!(null), json!("off"), json!(0), json!(true)] {
+ let (_, structured) =
+ from_args("click", &json!({"post_action_wait": value})).unwrap_err();
+ assert_eq!(structured["code"], "invalid_arguments", "{value}");
+ }
+ }
+
+ #[tokio::test]
+ async fn the_choice_is_scoped_to_the_call() {
+ assert_eq!(current(), PostActionWait::Default);
+ scope(PostActionWait::None, async {
+ assert_eq!(current(), PostActionWait::None);
+ })
+ .await;
+ assert_eq!(current(), PostActionWait::Default);
+ }
+}
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/tool.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/tool.rs
index afef86dc4e..845e3d6312 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/src/tool.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/tool.rs
@@ -619,6 +619,13 @@ pub(crate) fn advertised_runtime_input_schema(tool_name: &str, schema: &Value) -
.entry("session")
.or_insert_with(crate::tool_schema::session_schema);
}
+ // Dispatch reads `post_action_wait` for every action that waits for the
+ // post-action window-change report (see `crate::post_action_wait`).
+ if crate::post_action_wait::accepts(tool_name) {
+ properties
+ .entry(crate::post_action_wait::POST_ACTION_WAIT_ARG)
+ .or_insert_with(crate::post_action_wait::schema);
+ }
if !crate::action_target::supports_typed_target(tool_name) {
return schema;
}
@@ -1697,10 +1704,25 @@ impl ToolRegistry {
.and_then(Value::as_str)
.map(str::to_owned);
let in_batch = crate::step_pacing::in_batch();
- let mut result = self
- .invoke_authorized_unannotated(name, args, context, evidence)
- .await;
- let tool = renamed_tool(name).unwrap_or(name);
+ let tool = match name {
+ "type_text_chars" => "type_text",
+ other => renamed_tool(other).unwrap_or(other),
+ };
+ let wait = if crate::post_action_wait::accepts(tool) {
+ crate::post_action_wait::from_args(tool, &args)
+ } else {
+ Ok(crate::post_action_wait::PostActionWait::Default)
+ };
+ let mut result = match wait {
+ Ok(wait) => {
+ crate::post_action_wait::scope(
+ wait,
+ self.invoke_authorized_unannotated(name, args, context, evidence),
+ )
+ .await
+ }
+ Err((message, structured)) => ToolResult::error(message).with_structured(structured),
+ };
crate::recovery_hint::annotate(tool, &seed, &mut result);
crate::batch_nudge::annotate(tool, session.as_deref(), in_batch, &mut result);
result
@@ -3676,6 +3698,27 @@ mod runtime_isolation_tests {
def: super::ToolDef,
}
+ /// Records the post-action wait each invocation runs under.
+ struct WaitProbe {
+ seen: Arc<Mutex<Vec<crate::post_action_wait::PostActionWait>>>,
+ def: super::ToolDef,
+ }
+
+ #[async_trait::async_trait]
+ impl super::Tool for WaitProbe {
+ fn def(&self) -> &super::ToolDef {
+ &self.def
+ }
+
+ async fn invoke(&self, _args: serde_json::Value) -> crate::protocol::ToolResult {
+ self.seen
+ .lock()
+ .unwrap()
+ .push(crate::post_action_wait::current());
+ crate::protocol::ToolResult::text("input delivered")
+ }
+ }
+
struct AttestedProbe {
hits: Arc<AtomicUsize>,
scope: serde_json::Value,
@@ -4960,6 +5003,150 @@ resources:
);
}
+ #[tokio::test]
+ async fn dispatch_resolves_type_text_chars_before_reading_the_wait() {
+ use crate::post_action_wait::{PostActionWait, SKIP_SUPPORTED};
+ use serde_json::json;
+ if !SKIP_SUPPORTED {
+ return;
+ }
+ let seen = Arc::new(Mutex::new(Vec::new()));
+ let mut registry = super::ToolRegistry::new();
+ registry.register(Box::new(WaitProbe {
+ seen: seen.clone(),
+ def: super::ToolDef {
+ name: "type_text".into(),
+ description: "test input".into(),
+ input_schema: json!({"type": "object", "properties": {}, "additionalProperties": false}),
+ read_only: false,
+ destructive: false,
+ idempotent: false,
+ open_world: false,
+ },
+ }));
+ let registry = Arc::new(registry);
+ let ok = registry
+ .invoke_with_context(
+ "type_text_chars",
+ json!({"post_action_wait": "none"}),
+ standard_context(),
+ )
+ .await;
+ assert_ne!(ok.is_error, Some(true), "{ok:?}");
+ assert_eq!(seen.lock().unwrap().as_slice(), &[PostActionWait::None]);
+ for value in [json!(null), json!("off")] {
+ let r = registry
+ .invoke_with_context(
+ "type_text_chars",
+ json!({"post_action_wait": value}),
+ standard_context(),
+ )
+ .await;
+ assert_eq!(r.structured_content.unwrap()["code"], "invalid_arguments");
+ }
+ assert_eq!(seen.lock().unwrap().len(), 1);
+ }
+
+ #[tokio::test]
+ async fn dispatch_runs_the_action_under_its_post_action_wait() {
+ use crate::post_action_wait::{
+ current, PostActionWait, POST_ACTION_WAIT_ARG, POST_ACTION_WAIT_TOOLS, SKIP_SUPPORTED,
+ };
+ use serde_json::json;
+ let schema = json!({"type": "object", "properties": {}, "additionalProperties": false});
+ for tool in POST_ACTION_WAIT_TOOLS {
+ let advertised = super::advertised_runtime_input_schema(tool, &schema);
+ assert_eq!(
+ advertised["properties"].get(POST_ACTION_WAIT_ARG).is_some(),
+ SKIP_SUPPORTED,
+ "{tool}"
+ );
+ }
+
+ let seen = Arc::new(Mutex::new(Vec::new()));
+ let mut registry = super::ToolRegistry::new();
+ registry.register(Box::new(WaitProbe {
+ seen: seen.clone(),
+ def: super::ToolDef {
+ name: "click".into(),
+ description: "test input".into(),
+ input_schema: schema,
+ read_only: false,
+ destructive: false,
+ idempotent: false,
+ open_world: false,
+ },
+ }));
+ let registry = Arc::new(registry);
+ let refusal_code = |result: crate::protocol::ToolResult| {
+ assert_eq!(result.is_error, Some(true), "{result:?}");
+ let structured = result.structured_content.expect("structured refusal");
+ if SKIP_SUPPORTED {
+ structured["code"].clone()
+ } else {
+ structured["refusal"]["code"].clone()
+ }
+ };
+
+ // Omitted keeps the wait.
+ let result = registry
+ .invoke_with_context("click", json!({}), standard_context())
+ .await;
+ assert_ne!(result.is_error, Some(true), "{result:?}");
+
+ // "default" and "none" reach the adapter through the task-local scope
+ // on macOS; elsewhere the closed schema refuses the argument.
+ for value in ["default", "none"] {
+ let result = registry
+ .invoke_with_context(
+ "click",
+ json!({"post_action_wait": value}),
+ standard_context(),
+ )
+ .await;
+ if SKIP_SUPPORTED {
+ assert_ne!(result.is_error, Some(true), "{result:?}");
+ } else {
+ assert_eq!(refusal_code(result), "invalid_arguments", "{value}");
+ }
+ }
+ let expected: &[PostActionWait] = if SKIP_SUPPORTED {
+ &[
+ PostActionWait::Default,
+ PostActionWait::Default,
+ PostActionWait::None,
+ ]
+ } else {
+ &[PostActionWait::Default]
+ };
+ assert_eq!(seen.lock().unwrap().as_slice(), expected);
+ assert_eq!(
+ current(),
+ PostActionWait::Default,
+ "the choice must not leak"
+ );
+
+ // Values outside the enum, null included, never reach the tool.
+ for value in [json!(null), json!("off"), json!(0)] {
+ let result = registry
+ .invoke_with_context(
+ "click",
+ json!({"post_action_wait": value}),
+ standard_context(),
+ )
+ .await;
+ if SKIP_SUPPORTED {
+ assert!(result.structured_content.as_ref().unwrap()["hint"].is_string());
+ }
+ assert_eq!(refusal_code(result), "invalid_arguments", "{value}");
+ }
+ assert_eq!(
+ seen.lock().unwrap().as_slice(),
+ expected,
+ "must refuse before invocation"
+ );
+ }
+
#[tokio::test]
async fn desktop_background_click_refuses_before_platform_invocation() {
let hits = Arc::new(AtomicUsize::new(0));
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/window_change_detector.rs b/libs/cua-driver/rust/crates/platform-macos/src/window_change_detector.rs
index fe0429d451..9175042c1c 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/window_change_detector.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/window_change_detector.rs
@@ -91,6 +91,11 @@ pub struct Changes {
/// skipped it or the poll task was lost: an empty `new_windows` then means
/// nothing was watched, not that nothing opened.
pub polled: bool,
+ /// The caller passed `post_action_wait: "none"`: the result did not wait
+ /// for a change, and the watch runs detached (see
+ /// [`cua_driver_core::post_action_wait`]). The result says so instead of
+ /// reporting "no change".
+ pub deferred: bool,
}
impl Changes {
@@ -101,6 +106,7 @@ impl Changes {
earlier_new_windows: Vec::new(),
earlier_foreground_changed: false,
polled: true,
+ deferred: false,
}
}
@@ -119,6 +125,15 @@ impl Changes {
}
}
+ /// The result returned without waiting for a change; the watch went on
+ /// detached.
+ pub fn deferred() -> Self {
+ Self {
+ deferred: true,
+ ..Self::not_polled()
+ }
+ }
+
/// True when we found evidence that the action triggered a cross-app
/// side-effect that required (or would have required) a foreground
/// restore. Matches Swift's `Changes.needsRestore`.
@@ -144,6 +159,10 @@ impl Changes {
"\n\n🔀 After the previous action returned, a different app became frontmost.",
);
}
+ if self.deferred {
+ suffix.push_str(DEFERRED_NOTE);
+ return suffix;
+ }
if !self.needs_restore() {
return suffix;
}
@@ -160,6 +179,12 @@ impl Changes {
}
}
+/// Result suffix for an action whose caller skipped the window-change wait.
+pub(crate) const DEFERRED_NOTE: &str =
+ "\n\n⏭️ Window changes not observed: post_action_wait \"none\" \
+ returned without waiting. A new window or foreground change seen afterwards is reported \
+ on the next action's result.";
+
/// `App ("Title", …); Other` for a result suffix, grouped by app name.
fn window_summaries(windows: &[WindowEvent]) -> String {
let mut by_app: std::collections::BTreeMap<&str, Vec<&str>> = std::collections::BTreeMap::new();
@@ -445,6 +470,12 @@ impl Snapshot {
return Changes::not_polled().with_earlier(earlier);
}
let started = Instant::now();
+ if report_window.is_zero() {
+ // The caller skipped the wait: the whole deadline runs detached,
+ // lease held, exactly as the remainder of a quiet poll does.
+ self.continue_detached(started + bounds.timeout, bounds.poll, observe);
+ return Changes::deferred().with_earlier(earlier);
+ }
let report = report_window.min(bounds.timeout);
let mut observe = observe;
let changes = match self.poll_until(started + report, bounds.poll, &mut observe) {
@@ -465,7 +496,13 @@ impl Snapshot {
// A step of a batch returns after a shorter report window, because
// the next step follows at once. The pacing is read here: a blocking
// thread does not inherit the task's batch marker.
- let report_window = cua_driver_core::step_pacing::report_window(REPORT_WINDOW);
+ // `post_action_wait: "none"` skips the wait altogether.
+ let report_window = match cua_driver_core::post_action_wait::current() {
+ cua_driver_core::post_action_wait::PostActionWait::None => Duration::ZERO,
+ cua_driver_core::post_action_wait::PostActionWait::Default => {
+ cua_driver_core::step_pacing::report_window(REPORT_WINDOW)
+ }
+ };
// Move the Snapshot (and its embedded lease) onto the blocking
// thread; the lease's Drop runs there when detect_with returns.
tokio::task::spawn_blocking(move || {
@@ -981,6 +1018,53 @@ mod tests {
assert_eq!(changes.new_windows.len(), 1);
}
+ /// `post_action_wait: "none"` (a zero report window) returns at once
+ /// without observing, says so, and hands the whole deadline to the
+ /// detached watch, which still records a late window.
+ #[test]
+ fn a_skipped_wait_returns_at_once_and_watches_detached() {
+ let _serial = lock(&TAIL_TEST_LOCK);
+ end_tail();
+ lock(&LATE).take();
+ let opened = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
+ let observed_opened = opened.clone();
+ let snap = WindowChangeDetector::capture_from(HashSet::from([1]), Some(7), false, None);
+ let started = Instant::now();
+ let changes = snap.detect_bounded_reporting(
+ observation_bounds_from(Some("2000"), Some("10")),
+ Duration::ZERO,
+ move || {
+ if observed_opened.load(Ordering::SeqCst) {
+ (
+ vec![win(1, 7, "App", "Main"), win(2, 7, "App", "Save")],
+ Some(7),
+ )
+ } else {
+ (vec![win(1, 7, "App", "Main")], Some(7))
+ }
+ },
+ );
+ let elapsed = started.elapsed();
+ assert!(changes.deferred && !changes.polled);
+ assert!(!changes.needs_restore());
+ assert!(
+ elapsed < Duration::from_millis(100),
+ "returned after {elapsed:?}"
+ );
+ assert_eq!(changes.result_suffix(), DEFERRED_NOTE);
+ assert!(lock(&TAIL).is_some(), "the deadline runs detached");
+ opened.store(true, Ordering::SeqCst);
+ let wait_until = Instant::now() + Duration::from_secs(2);
+ while lock(&LATE).is_none() && Instant::now() < wait_until {
+ std::thread::sleep(Duration::from_millis(10));
+ }
+ assert!(
+ take_late().is_some_and(|late| late.new_windows.len() == 1),
+ "the detached watch records the late window"
+ );
+ end_tail();
+ }
+
/// A deadline inside the report window behaves as before: no tail.
#[test]
fn a_short_deadline_leaves_no_detached_tail() {
diff --git a/scripts/docs-generators/cli-specs/cua-driver-mcp-macos.json b/scripts/docs-generators/cli-specs/cua-driver-mcp-macos.json
index e918b6394f..b8c8b62870 100644
--- a/scripts/docs-generators/cli-specs/cua-driver-mcp-macos.json
+++ b/scripts/docs-generators/cli-specs/cua-driver-mcp-macos.json
@@ -545,6 +545,14 @@
"description": "Target process ID. Optional with element_token (it carries the pid).",
"type": "integer"
},
+ "post_action_wait": {
+ "description": "Whether the result waits for the post-action window-change report (default \"default\": up to 300 ms for a new window, menu, dialog or foreground change). \"none\" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged.",
+ "enum": [
+ "default",
+ "none"
+ ],
+ "type": "string"
+ },
"scope": {
"description": "Default \"window\". \"desktop\": x,y with no pid/window_id are true screen pixels from get_desktop_state.",
"enum": [
@@ -632,6 +640,14 @@
"description": "Target process ID. Optional with element_token (the token carries the pid).",
"type": "integer"
},
+ "post_action_wait": {
+ "description": "Whether the result waits for the post-action window-change report (default \"default\": up to 300 ms for a new window, menu, dialog or foreground change). \"none\" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged.",
+ "enum": [
+ "default",
+ "none"
+ ],
+ "type": "string"
+ },
"session": {
"description": "For multi-call work, prefer a short public session label and repeat it on every call that accepts it. Omit it to use the authenticated transport's implicit lifecycle session.",
"type": "string"
@@ -686,6 +702,14 @@
"description": "Target process ID. Optional with element_token (the token carries the pid).",
"type": "integer"
},
+ "post_action_wait": {
+ "description": "Whether the result waits for the post-action window-change report (default \"default\": up to 300 ms for a new window, menu, dialog or foreground change). \"none\" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged.",
+ "enum": [
+ "default",
+ "none"
+ ],
+ "type": "string"
+ },
"session": {
"description": "For multi-call work, prefer a short public session label and repeat it on every call that accepts it. Omit it to use the authenticated transport's implicit lifecycle session.",
"type": "string"
@@ -762,6 +786,14 @@
"description": "Target process ID.",
"type": "integer"
},
+ "post_action_wait": {
+ "description": "Whether the result waits for the post-action window-change report (default \"default\": up to 300 ms for a new window, menu, dialog or foreground change). \"none\" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged.",
+ "enum": [
+ "default",
+ "none"
+ ],
+ "type": "string"
+ },
"scope": {
"default": "window",
"description": "Use desktop with no pid/window_id for native get_desktop_state screenshot coordinates.",
@@ -867,6 +899,14 @@
"description": "Target process ID.",
"type": "integer"
},
+ "post_action_wait": {
+ "description": "Whether the result waits for the post-action window-change report (default \"default\": up to 300 ms for a new window, menu, dialog or foreground change). \"none\" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged.",
+ "enum": [
+ "default",
+ "none"
+ ],
+ "type": "string"
+ },
"scope": {
"default": "window",
"description": "\"desktop\" with no pid/window_id types into the frontmost app.",
@@ -972,6 +1012,14 @@
"description": "Target process ID.",
"type": "integer"
},
+ "post_action_wait": {
+ "description": "Whether the result waits for the post-action window-change report (default \"default\": up to 300 ms for a new window, menu, dialog or foreground change). \"none\" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged.",
+ "enum": [
+ "default",
+ "none"
+ ],
+ "type": "string"
+ },
"scope": {
"default": "window",
"description": "\"desktop\" with no pid/window_id sends to the frontmost app.",
@@ -1070,6 +1118,14 @@
"description": "Target process ID. Required with x, y; optional with element_token (the token carries the pid).",
"type": "integer"
},
+ "post_action_wait": {
+ "description": "Whether the result waits for the post-action window-change report (default \"default\": up to 300 ms for a new window, menu, dialog or foreground change). \"none\" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged.",
+ "enum": [
+ "default",
+ "none"
+ ],
+ "type": "string"
+ },
"scope": {
"default": "window",
"description": "Use desktop with no pid/window_id to send the chord to the frontmost application.",
@@ -1152,6 +1208,14 @@
"description": "Target process ID. Optional with element_token (the token carries the pid).",
"type": "integer"
},
+ "post_action_wait": {
+ "description": "Whether the result waits for the post-action window-change report (default \"default\": up to 300 ms for a new window, menu, dialog or foreground change). \"none\" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged.",
+ "enum": [
+ "default",
+ "none"
+ ],
+ "type": "string"
+ },
"session": {
"description": "For multi-call work, prefer a short public session label and repeat it on every call that accepts it.",
"type": "string"
@@ -1221,6 +1285,14 @@
"description": "Target process ID. Required unless scope is \"desktop\" or element_token is supplied.",
"type": "integer"
},
+ "post_action_wait": {
+ "description": "Whether the result waits for the post-action window-change report (default \"default\": up to 300 ms for a new window, menu, dialog or foreground change). \"none\" returns as soon as the input is delivered and says window changes were not observed; the watch keeps running in the background and a change it sees is reported on the next action's result. Only paths that already observe window changes are affected; for example a right_click that does not watch is unchanged.",
+ "enum": [
+ "default",
+ "none"
+ ],
+ "type": "string"
+ },
"scope": {
"default": "window",
"description": "\"desktop\" with x,y and no pid/window_id uses get_desktop_state coordinates.",