Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-driver): report the same capture refusal codes on every platform (#4111)

Francesco Bonacci8946c6d49871
diff --git a/docs/content/docs/how-to-guides/driver/parse-visual-regions.mdx b/docs/content/docs/how-to-guides/driver/parse-visual-regions.mdx
index ddd0048ebe..1be4d04ab7 100644
--- a/docs/content/docs/how-to-guides/driver/parse-visual-regions.mdx
+++ b/docs/content/docs/how-to-guides/driver/parse-visual-regions.mdx
@@ -142,6 +142,24 @@ coordinates. Do not remove `capture_id` and retry an expired, stale, retired,
or mismatched capture as an unbound coordinate click. Do not reuse the capture
for a second action.
+A refused capture-bound `click` returns `effect:"refused"` and one of these
+`code` values on macOS, Windows, and Linux:
+
+| Code | Cause |
+| ----------------------------- | ------------------------------------------------------------------------- |
+| `capture_id_invalid` | The capture ID is malformed. |
+| `capture_not_found` | The capture is unknown, already consumed, or its session was retired. |
+| `capture_expired` | The capture outlived its lifetime. |
+| `capture_generation_mismatch` | The capture belongs to another runtime or session generation. |
+| `capture_target_mismatch` | The click names a different target than the capture. |
+| `capture_coordinate_invalid` | The point is outside the capture or cannot be mapped to a finite action point. |
+| `capture_frame_mismatch` | The live target's action frame changed size since the capture. |
+
+Driver can also refuse before admission when it cannot inspect the live target.
+Windows and Linux report that as `capture_action_refused`; macOS reports a
+changed or unreadable live window as `capture_target_mismatch`. Every refusal
+means the click was not dispatched. Capture again before retrying.
+
Parsing a window does not make its pixel input background-safe. Driver applies
the platform's normal background route, and `background_unavailable` does not
authorize a foreground retry. `delivery_mode:"foreground"` and desktop input
diff --git a/libs/cua-driver/docs/perception-extension.md b/libs/cua-driver/docs/perception-extension.md
index 318c2693e6..f54b458ef6 100644
--- a/libs/cua-driver/docs/perception-extension.md
+++ b/libs/cua-driver/docs/perception-extension.md
@@ -106,6 +106,15 @@ registry, so a later process cannot resolve its `capture_id`.
5. Reobserve after every action attempt. Never remove `capture_id` and retry an
expired, stale, retired, or mismatched capture as an unbound click.
+Capture-bound click refusals use one shared code table on every platform,
+owned by `CaptureActionError::wire_code` in `cua-driver-core`:
+`capture_id_invalid`, `capture_not_found`, `capture_expired`,
+`capture_generation_mismatch`, `capture_target_mismatch`,
+`capture_coordinate_invalid`, and `capture_frame_mismatch`. Live-target
+inspection failures before admission remain adapter-owned
+(`capture_action_refused` on Windows and Linux, `capture_target_mismatch` on
+macOS).
+
A capture-bound click consumes the capture before native dispatch. At most one
action may derive from a capture. Capture again after an action, timeout,
unknown result, resize, move, scroll, navigation, display-layout change, or
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/capture_registry.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/capture_registry.rs
index 874392aec9..ab14a00e41 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/src/capture_registry.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/capture_registry.rs
@@ -488,6 +488,55 @@ pub enum CaptureActionError {
NativeActionFrameMismatch,
}
+/// Wire code for a capture ID that does not parse.
+pub const CAPTURE_ID_INVALID_CODE: &str = "capture_id_invalid";
+
+/// Wire code for a capture-bound action refusal with no more specific cause.
+pub const CAPTURE_ACTION_REFUSED_CODE: &str = "capture_action_refused";
+
+impl CaptureIdParseError {
+ /// Stable structured `code` reported by every platform adapter.
+ pub const fn wire_code(&self) -> &'static str {
+ CAPTURE_ID_INVALID_CODE
+ }
+}
+
+impl CaptureLookupError {
+ /// Stable structured `code` reported by every platform adapter.
+ pub const fn wire_code(&self) -> &'static str {
+ match self {
+ Self::Unknown => "capture_not_found",
+ Self::Expired => "capture_expired",
+ Self::GenerationMismatch => "capture_generation_mismatch",
+ Self::TargetMismatch => "capture_target_mismatch",
+ }
+ }
+}
+
+impl CaptureActionError {
+ /// Stable structured `code` reported by every platform adapter when
+ /// capture-bound action admission refuses. Adapters must not remap it.
+ pub const fn wire_code(&self) -> &'static str {
+ match self {
+ Self::Lookup(lookup) => lookup.wire_code(),
+ Self::InvalidScreenshotPoint | Self::InvalidMappedPoint => "capture_coordinate_invalid",
+ Self::NativeActionFrameMismatch => "capture_frame_mismatch",
+ }
+ }
+}
+
+/// Maps an adapter's admission failure to its shared wire code. Parse and
+/// admission errors keep their core code; any other failure is a generic
+/// capture-bound action refusal.
+pub fn admission_error_code(error: &anyhow::Error) -> &'static str {
+ if let Some(parse) = error.downcast_ref::<CaptureIdParseError>() {
+ return parse.wire_code();
+ }
+ error
+ .downcast_ref::<CaptureActionError>()
+ .map_or(CAPTURE_ACTION_REFUSED_CODE, CaptureActionError::wire_code)
+}
+
#[derive(Clone)]
struct StoredCapture {
id: CaptureId,
@@ -1872,4 +1921,49 @@ mod tests {
1
);
}
+
+ #[test]
+ fn capture_refusals_have_one_wire_code_table() {
+ for (error, code) in [
+ (
+ CaptureActionError::Lookup(CaptureLookupError::Unknown),
+ "capture_not_found",
+ ),
+ (
+ CaptureActionError::Lookup(CaptureLookupError::Expired),
+ "capture_expired",
+ ),
+ (
+ CaptureActionError::Lookup(CaptureLookupError::GenerationMismatch),
+ "capture_generation_mismatch",
+ ),
+ (
+ CaptureActionError::Lookup(CaptureLookupError::TargetMismatch),
+ "capture_target_mismatch",
+ ),
+ (
+ CaptureActionError::InvalidScreenshotPoint,
+ "capture_coordinate_invalid",
+ ),
+ (
+ CaptureActionError::InvalidMappedPoint,
+ "capture_coordinate_invalid",
+ ),
+ (
+ CaptureActionError::NativeActionFrameMismatch,
+ "capture_frame_mismatch",
+ ),
+ ] {
+ assert_eq!(error.wire_code(), code);
+ assert_eq!(admission_error_code(&anyhow::Error::new(error)), code);
+ }
+ assert_eq!(
+ admission_error_code(&anyhow::Error::new(CaptureIdParseError::InvalidFormat)),
+ "capture_id_invalid"
+ );
+ assert_eq!(
+ admission_error_code(&anyhow::anyhow!("live target unavailable")),
+ "capture_action_refused"
+ );
+ }
}
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/capture_runtime.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/capture_runtime.rs
index cd8c868b1f..a661cc4278 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/src/capture_runtime.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/capture_runtime.rs
@@ -1,8 +1,9 @@
//! Public runtime facade for capture publication and capture-bound actions.
pub use crate::capture_registry::{
- CaptureActionAdmission, CaptureActionError, CaptureActionRequest, CaptureBinding, CaptureId,
- CaptureIdParseError, CaptureLookupError, CapturePublication, CaptureService, CaptureStoreError,
- CaptureTarget, EncodedScreenshotDimensions, NativeActionDimensions, PruneOutcome,
- ScreenshotToActionTransform,
+ admission_error_code, CaptureActionAdmission, CaptureActionError, CaptureActionRequest,
+ CaptureBinding, CaptureId, CaptureIdParseError, CaptureLookupError, CapturePublication,
+ CaptureService, CaptureStoreError, CaptureTarget, EncodedScreenshotDimensions,
+ NativeActionDimensions, PruneOutcome, ScreenshotToActionTransform, CAPTURE_ACTION_REFUSED_CODE,
+ CAPTURE_ID_INVALID_CODE,
};
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/capture_action_frame.rs b/libs/cua-driver/rust/crates/platform-linux/src/capture_action_frame.rs
index af6cb35e03..156aa8eb16 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/capture_action_frame.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/capture_action_frame.rs
@@ -1,7 +1,6 @@
use cua_driver_core::capture_runtime::{
- CaptureActionError, CaptureActionRequest, CaptureIdParseError, CaptureLookupError,
- CapturePublication, CaptureService, CaptureTarget, EncodedScreenshotDimensions,
- NativeActionDimensions, ScreenshotToActionTransform,
+ CaptureActionRequest, CapturePublication, CaptureService, CaptureTarget,
+ EncodedScreenshotDimensions, NativeActionDimensions, ScreenshotToActionTransform,
};
use serde_json::Value;
@@ -206,27 +205,6 @@ fn select_desktop_action_dimensions(
Ok(dimensions)
}
-pub(crate) fn admission_error_code(error: &anyhow::Error) -> &'static str {
- if error.downcast_ref::<CaptureIdParseError>().is_some() {
- return "capture_id_invalid";
- }
- match error.downcast_ref::<CaptureActionError>() {
- Some(CaptureActionError::Lookup(CaptureLookupError::Unknown)) => "capture_not_found",
- Some(CaptureActionError::Lookup(CaptureLookupError::Expired)) => "capture_expired",
- Some(CaptureActionError::Lookup(CaptureLookupError::GenerationMismatch)) => {
- "capture_generation_mismatch"
- }
- Some(CaptureActionError::Lookup(CaptureLookupError::TargetMismatch)) => {
- "capture_target_mismatch"
- }
- Some(
- CaptureActionError::InvalidScreenshotPoint | CaptureActionError::InvalidMappedPoint,
- ) => "capture_coordinate_invalid",
- Some(CaptureActionError::NativeActionFrameMismatch) => "capture_frame_mismatch",
- None => "capture_action_refused",
- }
-}
-
pub fn admit_window_click(
service: &CaptureService,
args: &Value,
@@ -270,6 +248,7 @@ pub fn retire_runtime(service: &CaptureService) {
#[cfg(test)]
mod tests {
use super::*;
+ use cua_driver_core::capture_runtime::admission_error_code;
use sha2::{Digest, Sha256};
fn png(width: u32, height: u32, value: u8) -> Vec<u8> {
@@ -450,42 +429,6 @@ mod tests {
);
}
- #[test]
- fn capture_action_refusals_have_stable_specific_codes() {
- for (error, code) in [
- (
- CaptureActionError::Lookup(CaptureLookupError::Unknown),
- "capture_not_found",
- ),
- (
- CaptureActionError::Lookup(CaptureLookupError::Expired),
- "capture_expired",
- ),
- (
- CaptureActionError::Lookup(CaptureLookupError::GenerationMismatch),
- "capture_generation_mismatch",
- ),
- (
- CaptureActionError::Lookup(CaptureLookupError::TargetMismatch),
- "capture_target_mismatch",
- ),
- (
- CaptureActionError::InvalidScreenshotPoint,
- "capture_coordinate_invalid",
- ),
- (
- CaptureActionError::InvalidMappedPoint,
- "capture_coordinate_invalid",
- ),
- (
- CaptureActionError::NativeActionFrameMismatch,
- "capture_frame_mismatch",
- ),
- ] {
- assert_eq!(admission_error_code(&anyhow::Error::new(error)), code);
- }
- }
-
#[test]
fn desktop_publication_uses_the_post_normalization_bytes() {
let service = CaptureService::default();
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/tools/impl_.rs b/libs/cua-driver/rust/crates/platform-linux/src/tools/impl_.rs
index 0ec20ad3b3..2f2d51878a 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/tools/impl_.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/tools/impl_.rs
@@ -6165,7 +6165,7 @@ pub struct ClickTool {
}
fn capture_admission_error(error: anyhow::Error) -> ToolResult {
- let code = crate::capture_action_frame::admission_error_code(&error);
+ let code = cua_driver_core::capture_runtime::admission_error_code(&error);
ToolResult::error(format!("capture-bound click refused: {error}"))
.with_structured(json!({ "code": code, "effect": "refused" }))
}
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/capture_binding.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/capture_binding.rs
index b9ec3f61e7..ee69f2963b 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/capture_binding.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/capture_binding.rs
@@ -5,8 +5,8 @@
use cua_driver_core::{
capture_runtime::{
- CaptureActionError, CaptureActionRequest, CaptureLookupError, CapturePublication,
- CaptureService, CaptureTarget, EncodedScreenshotDimensions, NativeActionDimensions,
+ CaptureActionRequest, CaptureIdParseError, CapturePublication, CaptureService,
+ CaptureTarget, EncodedScreenshotDimensions, NativeActionDimensions,
ScreenshotToActionTransform,
},
protocol::ToolResult,
@@ -153,7 +153,7 @@ impl MacCaptureBindings {
.map_err(|error| capture_error("capture_binding_failed", error))?;
let parsed = capture_id
.parse()
- .map_err(|error| capture_error("capture_id_invalid", error))?;
+ .map_err(|error: CaptureIdParseError| capture_error(error.wire_code(), error))?;
self.service
.admit_action(CaptureActionRequest {
capture_id: parsed,
@@ -164,7 +164,7 @@ impl MacCaptureBindings {
screenshot_y: y,
})
.map(|admission| (admission.action_x, admission.action_y))
- .map_err(|error| capture_error(action_error_code(&error), error))
+ .map_err(|error| capture_error(error.wire_code(), error))
}
pub(super) fn retire_session(&self, session_id: &str) {
@@ -255,22 +255,6 @@ fn capture_error(code: &str, error: impl std::fmt::Display) -> ToolResult {
}))
}
-fn action_error_code(error: &CaptureActionError) -> &'static str {
- match error {
- CaptureActionError::Lookup(CaptureLookupError::Unknown | CaptureLookupError::Expired) => {
- "capture_stale"
- }
- CaptureActionError::Lookup(CaptureLookupError::GenerationMismatch) => {
- "capture_generation_mismatch"
- }
- CaptureActionError::Lookup(CaptureLookupError::TargetMismatch) => "capture_target_mismatch",
- CaptureActionError::InvalidScreenshotPoint | CaptureActionError::InvalidMappedPoint => {
- "capture_coordinate_invalid"
- }
- CaptureActionError::NativeActionFrameMismatch => "capture_frame_mismatch",
- }
-}
-
#[cfg(test)]
mod tests {
use super::*;
@@ -349,30 +333,6 @@ mod tests {
);
}
- #[test]
- fn action_errors_keep_stale_mismatch_and_coordinate_failures_distinct() {
- assert_eq!(
- action_error_code(&CaptureActionError::Lookup(CaptureLookupError::Expired)),
- "capture_stale"
- );
- assert_eq!(
- action_error_code(&CaptureActionError::Lookup(
- CaptureLookupError::GenerationMismatch
- )),
- "capture_generation_mismatch"
- );
- assert_eq!(
- action_error_code(&CaptureActionError::Lookup(
- CaptureLookupError::TargetMismatch
- )),
- "capture_target_mismatch"
- );
- assert_eq!(
- action_error_code(&CaptureActionError::InvalidScreenshotPoint),
- "capture_coordinate_invalid"
- );
- }
-
#[test]
fn publication_retains_exact_png_and_content_digest() {
let service = Arc::new(CaptureService::default());
@@ -424,7 +384,7 @@ mod tests {
.unwrap_err()
.structured_content
.unwrap()["code"],
- "capture_stale"
+ "capture_not_found"
);
}
@@ -452,7 +412,7 @@ mod tests {
}
#[test]
- fn session_retirement_makes_published_capture_stale() {
+ fn session_retirement_reports_capture_not_found() {
let service = Arc::new(CaptureService::default());
let bindings = MacCaptureBindings::new(service);
let args = args("retired-session");
@@ -461,7 +421,10 @@ mod tests {
.unwrap();
bindings.retire_session("retired-session");
let error = admit_desktop(&bindings, &capture_id, &args, 1.0, 1.0, (2, 2)).unwrap_err();
- assert_eq!(error.structured_content.unwrap()["code"], "capture_stale");
+ assert_eq!(
+ error.structured_content.unwrap()["code"],
+ "capture_not_found"
+ );
}
#[test]
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/capture_admission.rs b/libs/cua-driver/rust/crates/platform-windows/src/capture_admission.rs
index f3fce5f24e..0c2c86564f 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/capture_admission.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/capture_admission.rs
@@ -1,9 +1,8 @@
use std::sync::Arc;
use cua_driver_core::capture_runtime::{
- CaptureActionError, CaptureActionRequest, CaptureIdParseError, CaptureLookupError,
- CapturePublication, CaptureService, CaptureTarget, EncodedScreenshotDimensions,
- NativeActionDimensions, ScreenshotToActionTransform,
+ CaptureActionRequest, CapturePublication, CaptureService, CaptureTarget,
+ EncodedScreenshotDimensions, NativeActionDimensions, ScreenshotToActionTransform,
};
use serde_json::Value;
@@ -66,26 +65,6 @@ pub(crate) fn round_action_point(x: f64, y: f64) -> anyhow::Result<(i32, i32)> {
Ok((round(x)?, round(y)?))
}
-pub(crate) fn admission_error_code(error: &anyhow::Error) -> &'static str {
- if error.downcast_ref::<CaptureIdParseError>().is_some() {
- return "capture_id_invalid";
- }
- match error.downcast_ref::<CaptureActionError>() {
- Some(CaptureActionError::Lookup(CaptureLookupError::Unknown)) => "capture_not_found",
- Some(CaptureActionError::Lookup(CaptureLookupError::Expired)) => "capture_expired",
- Some(CaptureActionError::Lookup(CaptureLookupError::GenerationMismatch)) => {
- "capture_generation_mismatch"
- }
- Some(CaptureActionError::Lookup(CaptureLookupError::TargetMismatch)) => {
- "capture_target_mismatch"
- }
- Some(CaptureActionError::InvalidScreenshotPoint) => "capture_point_invalid",
- Some(CaptureActionError::InvalidMappedPoint) => "capture_mapping_invalid",
- Some(CaptureActionError::NativeActionFrameMismatch) => "capture_frame_mismatch",
- None => "capture_action_refused",
- }
-}
-
pub(crate) struct WindowsCaptureBridge {
service: Arc<CaptureService>,
}
@@ -202,13 +181,10 @@ fn live_geometry(_target: WindowsCaptureTarget) -> anyhow::Result<CaptureGeometr
mod tests {
use std::sync::Arc;
- use cua_driver_core::capture_runtime::CaptureService;
+ use cua_driver_core::capture_runtime::{admission_error_code, CaptureService};
use serde_json::json;
- use super::{
- admission_error_code, round_action_point, CaptureGeometry, WindowsCaptureBridge,
- WindowsCaptureTarget,
- };
+ use super::{round_action_point, CaptureGeometry, WindowsCaptureBridge, WindowsCaptureTarget};
fn png() -> Vec<u8> {
cua_driver_core::image_utils::encode_rgba_to_png(&[10, 20, 30, 255], 1, 1).unwrap()
@@ -293,6 +269,26 @@ mod tests {
assert_eq!(dispatches, 0);
}
+ #[test]
+ fn out_of_bounds_point_reports_the_shared_coordinate_code() {
+ let bridge = WindowsCaptureBridge::new(Arc::new(CaptureService::default()));
+ let target = WindowsCaptureTarget::PrimaryDesktop;
+ let capture_id = publish(&bridge, target);
+ let mut request = args();
+ request["capture_id"] = json!(capture_id);
+ let admission = bridge.admit_click_with_geometry(
+ &request,
+ target,
+ CaptureGeometry::new(1, 1, 1, 1).unwrap(),
+ 5.0,
+ 0.0,
+ );
+ assert_eq!(
+ admission_error_code(&admission.unwrap_err()),
+ "capture_coordinate_invalid"
+ );
+ }
+
#[test]
fn retired_capture_refuses_before_dispatch() {
let bridge = WindowsCaptureBridge::new(Arc::new(CaptureService::default()));
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_.rs b/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_.rs
index e5a238d90f..20f036d0eb 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_.rs
@@ -603,7 +603,7 @@ fn screenshot_scale(
}
fn capture_admission_refusal(error: anyhow::Error) -> ToolResult {
- let code = crate::capture_admission::admission_error_code(&error);
+ let code = cua_driver_core::capture_runtime::admission_error_code(&error);
ToolResult::error(format!("Capture-bound click refused: {error}")).with_structured(json!({
"code": code,
"effect": "refused",