diff --git a/src/crates/execution/tool-contracts/src/computer_use_geometry.rs b/src/crates/execution/tool-contracts/src/computer_use_geometry.rs index 45350da621..ea18c9ec3b 100644 --- a/src/crates/execution/tool-contracts/src/computer_use_geometry.rs +++ b/src/crates/execution/tool-contracts/src/computer_use_geometry.rs @@ -129,7 +129,7 @@ impl CaptureGeometry { && y.checked_add(height) .is_some_and(|bottom| bottom <= top + content_height); if !inside { - return Err("[INVALID_COORDINATES] Region must be a nonempty rectangle inside the captured image content".into()); + return Err(format!("[INVALID_COORDINATES] Region must be a nonempty rectangle inside the captured image content: requested x={x} y={y} width={width} height={height}; content is x={left} y={top} width={content_width} height={content_height}")); } let scale_x = self.bounds[2] / f64::from(content_width); let scale_y = self.bounds[3] / f64::from(content_height); diff --git a/src/crates/services/computer-use/AGENTS.md b/src/crates/services/computer-use/AGENTS.md index 8e992159cb..dc781c578f 100644 --- a/src/crates/services/computer-use/AGENTS.md +++ b/src/crates/services/computer-use/AGENTS.md @@ -150,6 +150,18 @@ does nothing (WebKit). Select the existing directed-keyboard route before submission for these proxies; native controls retain AX insertion at the current selection. Never retry a submitted text write based only on unchanged AX state. +Text controls inside an `AXWebArea` need a real directed click to become the +keyboard destination. AXPress and AXFocused succeed there but move only DOM +focus: the window's first responder stays elsewhere and later directed keys are +lost while still reporting `submitted`. Route them through the pointer path and +directed keyboard, without app-name rules. Verified on Electron; native controls +keep the AX path. A field scrolled out of view is an error; web +content covering the center cannot be detected, because Chromium's synchronous +AX hit test ignores stacking. A later focus request on a field that a real click +already focused sends no click, which preserves its caret and selection; other +pointer or semantic input, shortcuts that can move focus and focus-loss events +end that state. + The directed-input route can change a target application's internal focus without changing the human foreground application. Its window-addressed focus pair at local `(-8,-8)` must remain outside content; the requested gesture diff --git a/src/crates/services/computer-use/src/computer_use/desktop_host/ax_orchestration.rs b/src/crates/services/computer-use/src/computer_use/desktop_host/ax_orchestration.rs index 9b2423f991..1de085d802 100644 --- a/src/crates/services/computer-use/src/computer_use/desktop_host/ax_orchestration.rs +++ b/src/crates/services/computer-use/src/computer_use/desktop_host/ax_orchestration.rs @@ -166,7 +166,8 @@ fn resolve_macos_node_click( let target = crate::computer_use::macos_ax_dump::retained_cached_target(pid, idx) .ok_or_else(|| OpenBitFunError::tool(format!("AX_NODE_STALE: idx={idx} is not in the observed app snapshot; observe the app again")))?; crate::computer_use::macos_ax_dump::validate_bound_target(pid, target.reference())?; - if semantic { + let web_text_field = semantic && target.is_web_text_input(); + if semantic && !web_text_field { match crate::computer_use::macos_ax_write::try_ax_press(target.reference()) { crate::computer_use::macos_ax_write::AxWriteOutcome::Ok => return Ok(None), // kAXErrorActionUnsupported guarantees no semantic action was sent. @@ -181,9 +182,61 @@ fn resolve_macos_node_click( "AX_NODE_STALE: idx={idx} no longer has a readable frame; observe the app again" )) })?; + if web_text_field { + require_visible_web_text_field(pid, idx, frame)?; + } observed_node_center(frame).map(Some) } +/// A web text field takes a real click instead of AXPress, and that click +/// needs a visible point. Chromium clips a scrolled-away node's frame to the +/// viewport edge, leaving under one pixel. Web content can still cover the +/// point: its synchronous AX hit test ignores stacking, so it cannot tell. +#[cfg(target_os = "macos")] +fn require_visible_web_text_field( + pid: i32, + idx: u32, + (field_left, field_top, field_width, field_height): (f64, f64, f64, f64), +) -> OpenBitFunResult<()> { + let (x, y) = ( + field_left + field_width / 2.0, + field_top + field_height / 2.0, + ); + let window = + crate::computer_use::macos_capture::bound_window_id(pid).map_err(OpenBitFunError::tool)?; + let [left, top, width, height] = crate::computer_use::macos_capture::window_bounds(pid, window) + .map_err(OpenBitFunError::tool)?; + if field_width < 1.0 + || field_height < 1.0 + || x < left + || y < top + || x >= left + width + || y >= top + height + { + return Err(OpenBitFunError::tool(format!("TARGET_COORDINATES_OUTSIDE_WINDOW: Text field idx={idx} is not visible in the window; no click was sent. Scroll it into view and observe again"))); + } + Ok(()) +} + +/// Remember the field a real click just focused, so a later focus request on +/// it does not click again and lose the caret or selection. +#[cfg(target_os = "macos")] +fn remember_web_text_click(pid: i32, target: &ClickTarget, x: f64, y: f64) -> OpenBitFunResult<()> { + let element = match target { + ClickTarget::NodeIdx { idx } => { + crate::computer_use::macos_ax_dump::retained_cached_target(pid, *idx) + } + ClickTarget::ImageXy { .. } => { + crate::computer_use::macos_ax_dump::retained_target_at_point(pid, x, y)? + } + _ => None, + }; + if let Some(element) = element.filter(|element| element.is_web_text_input()) { + crate::computer_use::macos_input_focus::note_web_text_click(pid, element); + } + Ok(()) +} + #[cfg(any(test, target_os = "macos"))] fn observed_node_center( (x, y, width, height): (f64, f64, f64, f64), @@ -548,6 +601,18 @@ impl NativeComputerUseHost { }) .await .map_err(|e| OpenBitFunError::tool(e.to_string()))??; + if is_plain_activation_click( + ¶ms.mouse_button, + params.click_count, + ¶ms.modifier_keys, + ) { + let target = params.target.clone(); + // Read-only bookkeeping: without it the next focus request clicks again. + let _ = crate::computer_use::control_session::spawn_blocking(move || { + macos::catch_objc_local(|| remember_web_text_click(pid, &target, x, y)) + }) + .await; + } } let settle_ms = params.wait_ms_after.unwrap_or(0); if settle_ms > 0 { @@ -679,6 +744,11 @@ impl NativeComputerUseHost { } else { None }; let Some(element) = element else { return Ok(false); }; crate::computer_use::macos_ax_dump::validate_bound_target(pid, element.reference())?; + if element.is_web_text_input() { + // Text goes through the directed keyboard. Focus needs a real + // click unless an earlier click already gave this field focus. + return Ok(insertion.is_none() && crate::computer_use::macos_input_focus::holds_web_text_focus(pid, &element)); + } if !element.is_text_input() { return Ok(false); } if let Some(text) = insertion.as_deref() { return crate::computer_use::macos_ax_write::insert_selected_text(element.reference(), text); diff --git a/src/crates/services/computer-use/src/computer_use/desktop_host/observation.rs b/src/crates/services/computer-use/src/computer_use/desktop_host/observation.rs index ca71349a24..85430f58af 100644 --- a/src/crates/services/computer-use/src/computer_use/desktop_host/observation.rs +++ b/src/crates/services/computer-use/src/computer_use/desktop_host/observation.rs @@ -1,6 +1,8 @@ //! Observation lifecycle shared by AX, UIA and AT-SPI providers. use super::*; +const UNPAINTED_WINDOW_NOTE: &str = "[note] WINDOW_NOT_REPAINTED: The window image is identical to the previous observation although the accessibility tree changed. The application may not have repainted this window, which is common for covered Chromium and Electron windows, or the change may not be visible. Use the tree for the current state; the image does not show whether the input took effect.\n"; + impl NativeComputerUseHost { pub(crate) async fn get_app_state_inner( &self, @@ -28,6 +30,21 @@ impl NativeComputerUseHost { .record_application(pid) .map_err(OpenBitFunError::tool)?, ); + if let Some(shot) = snapshot.screenshot.as_ref() { + let pixels = { + use std::hash::{Hash, Hasher}; + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + shot.bytes.hash(&mut hasher); + hasher.finish() + }; + if state + .observations + .record_frame(pid, pixels, &snapshot.digest) + { + // First, because long trees are clipped at the end for the model. + snapshot.tree_text.insert_str(0, UNPAINTED_WINDOW_NOTE); + } + } // Native node indices were rebuilt, including same-content observations. #[cfg(any(target_os = "macos", target_os = "windows"))] { diff --git a/src/crates/services/computer-use/src/computer_use/desktop_host/pointer_input.rs b/src/crates/services/computer-use/src/computer_use/desktop_host/pointer_input.rs index 6784386f53..8360c38809 100644 --- a/src/crates/services/computer-use/src/computer_use/desktop_host/pointer_input.rs +++ b/src/crates/services/computer-use/src/computer_use/desktop_host/pointer_input.rs @@ -718,6 +718,9 @@ impl NativeComputerUseHost { } pub(super) async fn mouse_click_impl(&self, button: &str) -> OpenBitFunResult<()> { + // A global press can move focus away from a field clicked earlier. + #[cfg(target_os = "macos")] + crate::computer_use::macos_input_focus::forget_web_text_focus(); debug!("computer_use: mouse_click button={}", button); ComputerUseHost::computer_use_guard_click_allowed(self)?; self.mouse_click_at_current_pointer(button).await @@ -732,6 +735,9 @@ impl NativeComputerUseHost { } pub(super) async fn mouse_down_impl(&self, button: &str) -> OpenBitFunResult<()> { + // A global press can move focus away from a field clicked earlier. + #[cfg(target_os = "macos")] + crate::computer_use::macos_input_focus::forget_web_text_focus(); #[cfg(target_os = "linux")] if let Some(session) = crate::computer_use::linux_control::session().map_err(OpenBitFunError::tool)? diff --git a/src/crates/services/computer-use/src/computer_use/macos_ax_dump.rs b/src/crates/services/computer-use/src/computer_use/macos_ax_dump.rs index 1096f36df5..24a8e6d1c8 100644 --- a/src/crates/services/computer-use/src/computer_use/macos_ax_dump.rs +++ b/src/crates/services/computer-use/src/computer_use/macos_ax_dump.rs @@ -180,6 +180,18 @@ impl RetainedCachedTarget { ) } + /// A text control rendered inside a web area. AXPress and AXFocused move + /// only the page's DOM focus there; the window's first responder stays + /// put, so directed keys vanish while the call reports success. + pub(crate) fn is_web_text_input(&self) -> bool { + (self.is_text_input() || self.role().as_deref() == Some("AXComboBox")) + && unsafe { has_web_area_ancestor(self.0 .0) } + } + + pub(crate) fn same_element(&self, other: AxRef) -> bool { + !other.0.is_null() && unsafe { CFEqual(self.0 .0 as CFTypeRef, other.0 as CFTypeRef) != 0 } + } + /// Only controls whose primary activation is independent of the exact /// point can replace a plain coordinate click with AXPress. Text, sliders, /// containers and custom canvases retain their coordinate semantics. @@ -236,6 +248,33 @@ pub(crate) fn validate_bound_target(pid: i32, target: AxRef) -> OpenBitFunResult .map_err(OpenBitFunError::tool) } +const WEB_AREA_ANCESTOR_LIMIT: usize = 64; + +/// Whether a parent chain reaches a web area within the depth bound. An +/// unreadable parent ends the chain and counts as native content. +fn role_chain_reaches_web_area(mut next_role: impl FnMut() -> Option) -> bool { + (0..WEB_AREA_ANCESTOR_LIMIT) + .map_while(|_| next_role()) + .any(|role| role == "AXWebArea") +} + +unsafe fn has_web_area_ancestor(element: AXUIElementRef) -> bool { + let mut current: Option = None; + let found = role_chain_reaches_web_area(|| unsafe { + let child = current.map_or(element, |owned| owned as AXUIElementRef); + let parent = ax_copy_attr(child, "AXParent"); + if let Some(owned) = current.take() { + ax_release(owned); + } + current = parent; + read_cf_string_attr(current? as AXUIElementRef, "AXRole") + }); + if let Some(owned) = current { + unsafe { ax_release(owned) }; + } + found +} + pub(crate) fn element_window_id(target: AxRef) -> Option { unsafe { super::macos_ax_ui::ax_window_id(target.0) @@ -1141,7 +1180,7 @@ fn render_tree_text(nodes: &[AxNode]) -> String { } if let Some(v) = &n.value { if !v.is_empty() { - out.push_str(&format!(" value={}", quote_clip(v, 120))); + out.push_str(&format!(" value={}", quote_clip(v, value_clip_chars(n)))); } } if let Some(id) = &n.identifier { @@ -1178,8 +1217,8 @@ fn render_tree_text(nodes: &[AxNode]) -> String { } match n.expanded { Some(true) => out.push_str(" [expanded]"), - Some(false) => out.push_str(" [collapsed]"), - None => {} + Some(false) if expansion_is_meaningful(&n.role) => out.push_str(" [collapsed]"), + _ => {} } // Surface non-trivial AX actions inline so the model can pick // AXShowMenu / AXIncrement / AXDecrement etc. without re-querying. @@ -1187,7 +1226,12 @@ fn render_tree_text(nodes: &[AxNode]) -> String { .actions .iter() .map(String::as_str) - .filter(|a| !matches!(*a, "AXPress" | "AXShowAlternateUI" | "AXShowDefaultUI")) + .filter(|a| { + !matches!( + *a, + "AXPress" | "AXShowAlternateUI" | "AXShowDefaultUI" | "AXScrollToVisible" + ) + }) .collect(); if !extra.is_empty() { out.push_str(&format!(" actions=[{}]", extra.join(","))); @@ -1202,6 +1246,38 @@ fn render_tree_text(nodes: &[AxNode]) -> String { out } +/// The caller verifies a draft against the focused field before sending it, +/// so that one value keeps enough text to read to the end. +fn value_clip_chars(node: &AxNode) -> usize { + const VALUE_CHARS: usize = 120; + const FOCUSED_TEXT_VALUE_CHARS: usize = 4000; + if node.focused + && matches!( + node.role.as_str(), + "AXTextField" | "AXTextArea" | "AXSearchField" + ) + { + FOCUSED_TEXT_VALUE_CHARS + } else { + VALUE_CHARS + } +} + +/// Chromium reports `AXExpanded = false` on nearly every web node. Rendering +/// it for structural roles only adds noise; the digest still covers the state. +fn expansion_is_meaningful(role: &str) -> bool { + !matches!( + role, + "AXGroup" + | "AXStaticText" + | "AXImage" + | "AXWebArea" + | "AXHeading" + | "AXTextField" + | "AXTextArea" + ) +} + /// Compose a Codex-style label: prefer humanised role description, fall /// back to `role + (subrole)`. fn format_label(n: &AxNode) -> String { @@ -1233,6 +1309,52 @@ fn quote_clip(s: &str, max: usize) -> String { mod tests { use super::*; + #[test] + fn web_area_ancestor_decides_the_text_focus_route() { + let walk = |roles: &[&str]| { + let mut roles = roles.iter().map(|role| role.to_string()); + role_chain_reaches_web_area(|| roles.next()) + }; + assert!(walk(&["AXGroup", "AXGroup", "AXWebArea", "AXGroup"])); + assert!(!walk(&["AXGroup", "AXWindow", "AXApplication"])); + assert!(!walk(&[])); + let mut steps = 0; + assert!(!role_chain_reaches_web_area(|| { + steps += 1; + Some("AXGroup".to_string()) + })); + assert_eq!(steps, WEB_AREA_ANCESTOR_LIMIT); + } + + #[test] + fn only_the_focused_text_field_keeps_its_full_value() { + let text = "\u{4e2d}".repeat(300); + let node = |role: &str, focused: bool| AxNode { + role: role.to_string(), + focused, + value: Some(text.clone()), + ..n(0, None, role, None) + }; + let rendered = |focused| render_tree_text(&[node("AXTextArea", focused)]); + assert!(rendered(true).contains(&format!("value=\"{text}\""))); + assert!(rendered(false).contains('\u{2026}')); + assert!(render_tree_text(&[node("AXStaticText", true)]).contains('\u{2026}')); + } + + #[test] + fn structural_collapsed_state_and_scroll_action_are_not_rendered() { + let node = |role: &str| AxNode { + role: role.to_string(), + expanded: Some(false), + actions: vec!["AXShowMenu".to_string(), "AXScrollToVisible".to_string()], + ..n(0, None, role, None) + }; + let group = render_tree_text(&[node("AXGroup")]); + assert!(!group.contains("[collapsed]")); + assert!(group.contains("actions=[AXShowMenu]")); + assert!(render_tree_text(&[node("AXComboBox")]).contains("[collapsed]")); + } + #[test] fn optional_point_hit_read_failure_preserves_directed_input_route() { assert!(optional_point_hit_available(0).unwrap()); diff --git a/src/crates/services/computer-use/src/computer_use/macos_ax_write.rs b/src/crates/services/computer-use/src/computer_use/macos_ax_write.rs index 8e9dab38f1..b0d6b8d7a4 100644 --- a/src/crates/services/computer-use/src/computer_use/macos_ax_write.rs +++ b/src/crates/services/computer-use/src/computer_use/macos_ax_write.rs @@ -61,6 +61,8 @@ fn can_write(target: AxRef) -> bool { } fn note_dispatched_input(target: AxRef) { + // A semantic action can move DOM focus without any pointer input. + super::macos_input_focus::forget_web_text_focus(); let mut pid = 0; if unsafe { AXUIElementGetPid(target.0, &mut pid) } == 0 { super::macos_capture::note_input(pid); diff --git a/src/crates/services/computer-use/src/computer_use/macos_bg_input.rs b/src/crates/services/computer-use/src/computer_use/macos_bg_input.rs index 3e04519acb..ef55fe6853 100644 --- a/src/crates/services/computer-use/src/computer_use/macos_bg_input.rs +++ b/src/crates/services/computer-use/src/computer_use/macos_bg_input.rs @@ -479,6 +479,12 @@ fn post_directed(pid: i32, event: &CGEvent, authenticated: bool) -> OpenBitFunRe event.post_to_pid(pid); } super::macos_capture::note_input(pid); + if matches!( + event.get_type(), + CGEventType::LeftMouseDown | CGEventType::RightMouseDown | CGEventType::OtherMouseDown + ) { + super::macos_input_focus::forget_web_text_focus(); + } match event.get_type() { CGEventType::KeyDown | CGEventType::KeyUp | CGEventType::FlagsChanged => { record_keyboard_input(&mut held, owner, event, authenticated); @@ -1026,6 +1032,9 @@ pub(super) fn bg_key_chord(pid: i32, modifiers: &[BgModifier], key: u16) -> Open "bg_key_chord.enter pid={} keycode={} modifiers={:?}", pid, key, modifiers ); + if !super::macos_input_focus::chord_keeps_text_focus(modifiers, key) { + super::macos_input_focus::forget_web_text_focus(); + } for event in key_chord_events(modifiers, key, "key_chord")? { post_keyboard(pid, &event)?; } @@ -1267,6 +1276,7 @@ pub(super) fn bg_key_chord_no_auth( "bg_key_chord_no_auth.enter pid={} keycode={} modifiers={:?}", pid, key, modifiers ); + super::macos_input_focus::forget_web_text_focus(); for event in key_chord_events(modifiers, key, "key_chord_no_auth")? { post_keyboard_no_auth(pid, &event)?; } diff --git a/src/crates/services/computer-use/src/computer_use/macos_input_focus.rs b/src/crates/services/computer-use/src/computer_use/macos_input_focus.rs index 01e456f587..2c56aa9a77 100644 --- a/src/crates/services/computer-use/src/computer_use/macos_input_focus.rs +++ b/src/crates/services/computer-use/src/computer_use/macos_input_focus.rs @@ -5,6 +5,8 @@ //! pair is addressed outside that window's frame; no content is clicked. The //! requested gesture retains its original modifiers and target. Delivery is //! reported as submitted; the next observation verifies the application result. +use super::macos_ax_dump::RetainedCachedTarget; +use super::macos_bg_input::BgModifier; use core_graphics::event::{CGEvent, CGEventFlags, CGEventType, CGMouseButton}; use core_graphics::geometry::CGPoint; use foreign_types::ForeignType; @@ -83,6 +85,83 @@ impl Focus { } static FOCUS: Mutex> = Mutex::new(None); +/// The conditions under which a click's keyboard focus was established. +#[derive(Clone, Copy, PartialEq, Eq)] +struct FocusWitness { + pid: i32, + window: u32, + generation: u64, + foreground: i32, + activation_epoch: u64, + loss_epoch: u64, +} +impl FocusWitness { + /// Background focus can only be trusted while its loss observer reports. + fn still_holds(self, current: Self) -> bool { + self == current && (current.loss_epoch != 0 || current.foreground == current.pid) + } +} +struct WebTextFocus { + witness: FocusWitness, + element: RetainedCachedTarget, +} +static WEB_TEXT_FOCUS: Mutex> = Mutex::new(None); + +fn focus_witness(pid: i32) -> Option { + let window = super::macos_capture::bound_window_id(pid).ok()?; + let foreground = super::macos_bg_input::frontmost_pid_macos()?; + let generation = super::control_session::snapshot().generation; + let loss_epoch = FOCUS + .lock() + .ok()? + .filter(|focus| focus.pid == pid && focus.window == window) + .map_or(0, |focus| focus.observed_loss_epoch(false)); + Some(FocusWitness { + pid, + window, + generation, + foreground, + activation_epoch: super::macos_capture::activation_epoch(), + loss_epoch, + }) +} + +pub(super) fn note_web_text_click(pid: i32, element: RetainedCachedTarget) { + let witness = focus_witness(pid); + if let Ok(mut state) = WEB_TEXT_FOCUS.lock() { + *state = witness.map(|witness| WebTextFocus { witness, element }); + } +} + +/// Whether an earlier real click gave this field keyboard focus and nothing +/// known has moved it. AXFocused cannot answer this for web content. +pub(super) fn holds_web_text_focus(pid: i32, element: &RetainedCachedTarget) -> bool { + let current = focus_witness(pid); + WEB_TEXT_FOCUS.lock().is_ok_and(|state| { + state.as_ref().is_some_and(|held| { + element.same_element(held.element.reference()) + && current.is_some_and(|current| held.witness.still_holds(current)) + }) + }) +} + +pub(super) fn forget_web_text_focus() { + if let Ok(mut state) = WEB_TEXT_FOCUS.lock() { + state.take(); + } +} + +/// Plain editing keys stay in the field. Tab and application shortcuts may +/// move focus, so the next focus request clicks again. +pub(super) fn chord_keeps_text_focus(modifiers: &[BgModifier], key: u16) -> bool { + const TAB: u16 = 48; + // Carbon virtual keycodes for A, Z, X, C and V. + const EDIT_COMMANDS: [u16; 5] = [0, 6, 7, 8, 9]; + let only_shift = modifiers.iter().all(|m| *m == BgModifier::Shift); + let only_command = !modifiers.is_empty() && modifiers.iter().all(|m| *m == BgModifier::Command); + (only_shift && key != TAB) || (only_command && EDIT_COMMANDS.contains(&key)) +} + fn cleanup(focus: Focus) { // A user may have deliberately switched to the target in the meantime. // Never deactivate that real foreground application. Also reject PID reuse. @@ -94,6 +173,7 @@ fn cleanup(focus: Focus) { } pub(super) fn stop() { + forget_web_text_focus(); if let Ok(mut state) = FOCUS.lock() { if let Some(focus) = state.take() { focus.stop_observer(); @@ -283,6 +363,79 @@ mod tests { })); } + #[test] + fn web_text_focus_survives_only_unchanged_conditions() { + let witness = super::FocusWitness { + pid: 11, + window: 22, + generation: 1, + foreground: 33, + activation_epoch: 4, + loss_epoch: 1, + }; + assert!(witness.still_holds(witness)); + for changed in [ + super::FocusWitness { + window: 23, + ..witness + }, + super::FocusWitness { + generation: 2, + ..witness + }, + super::FocusWitness { + foreground: 34, + ..witness + }, + super::FocusWitness { + activation_epoch: 5, + ..witness + }, + super::FocusWitness { + loss_epoch: 2, + ..witness + }, + ] { + assert!(!witness.still_holds(changed)); + } + let unobserved = super::FocusWitness { + loss_epoch: 0, + ..witness + }; + assert!( + !unobserved.still_holds(unobserved), + "background needs the loss observer" + ); + let frontmost = super::FocusWitness { + loss_epoch: 0, + foreground: 11, + ..witness + }; + assert!( + frontmost.still_holds(frontmost), + "the frontmost target has no private focus" + ); + } + + #[test] + fn only_editing_keys_keep_the_text_field_focus() { + use super::{chord_keeps_text_focus as keeps, BgModifier::*}; + assert!(keeps(&[], 36), "Return"); + assert!(keeps(&[], 51), "Delete"); + assert!(keeps(&[Shift], 123), "selection by arrow"); + for key in [0, 6, 7, 8, 9] { + assert!(keeps(&[Command], key)); + } + assert!(!keeps(&[], 48), "Tab"); + assert!(!keeps(&[Shift], 48), "Shift-Tab"); + assert!( + !keeps(&[Command], 40), + "an application shortcut such as Cmd-K" + ); + assert!(!keeps(&[Command, Shift], 0)); + assert!(!keeps(&[Control], 0)); + } + #[test] fn only_native_focus_loss_events_invalidate_preparation() { // AppKit app deactivation, process deactivation and key-focus loss. diff --git a/src/crates/services/computer-use/src/computer_use/observation_cache.rs b/src/crates/services/computer-use/src/computer_use/observation_cache.rs index c3c4306571..16e9395e00 100644 --- a/src/crates/services/computer-use/src/computer_use/observation_cache.rs +++ b/src/crates/services/computer-use/src/computer_use/observation_cache.rs @@ -18,6 +18,7 @@ pub(super) struct ObservationCache { application: Option<(i32, String)>, images: VecDeque<(String, G)>, latest: Option, + last_frame: Option<(i32, u64, String)>, } impl Default for ObservationCache { @@ -29,6 +30,7 @@ impl Default for ObservationCache { application: None, images: VecDeque::new(), latest: None, + last_frame: None, } } } @@ -46,6 +48,7 @@ impl ObservationCache { pub(super) fn clear(&mut self) { self.scope = None; self.application = None; + self.last_frame = None; self.clear_images(); } @@ -54,6 +57,18 @@ impl ObservationCache { self.latest = None; } + /// Record an image-bearing observation. True when the accessibility tree + /// moved on since the previous one while the window image did not, which + /// means the application has not repainted what the tree already reports. + pub(super) fn record_frame(&mut self, pid: i32, pixels: u64, digest: &str) -> bool { + let unpainted = self + .last_frame + .as_ref() + .is_some_and(|(p, hash, previous)| *p == pid && *hash == pixels && previous != digest); + self.last_frame = Some((pid, pixels, digest.to_owned())); + unpainted + } + pub(super) fn record_image(&mut self, id: String, geometry: G) { self.images.retain(|(existing, _)| *existing != id); self.images.push_back((id, geometry)); @@ -114,6 +129,18 @@ mod tests { } } + #[test] + fn unpainted_window_requires_same_pixels_with_a_changed_tree() { + let mut cache = ObservationCache::::default(); + assert!(!cache.record_frame(12, 1, "a"), "no previous frame"); + assert!(!cache.record_frame(12, 1, "a"), "nothing changed"); + assert!(cache.record_frame(12, 1, "b"), "tree moved, pixels did not"); + assert!(!cache.record_frame(12, 2, "c"), "both moved"); + assert!(!cache.record_frame(13, 2, "d"), "another process"); + cache.clear(); + assert!(!cache.record_frame(13, 2, "e"), "cleared with the scope"); + } + #[test] fn interleaved_images_are_bounded_without_changing_their_geometry() { let mut cache = ObservationCache::default();