Skip to content

Computer use CLI for AI agents

fix: prevent wait --count false positives on incomplete snapshots

detail-app[bot]319e9c6c2e7c
diff --git a/crates/core/src/adapter/test_support.rs b/crates/core/src/adapter/test_support.rs
index 464666de6..baad22928 100644
--- a/crates/core/src/adapter/test_support.rs
+++ b/crates/core/src/adapter/test_support.rs
@@ -156,7 +156,12 @@ pub(crate) fn observed_tree(
},
};
let children = node.children.into_iter().map(subtree).collect();
- ObservedSubtree::new(evidence, children, true, node.children_count)
+ ObservedSubtree::new(
+ evidence,
+ children,
+ !node.subtree_truncated,
+ node.children_count,
+ )
}
ObservedTree::from_roots(
diff --git a/crates/core/src/commands/wait.rs b/crates/core/src/commands/wait.rs
index 9b9ea3506..f488aef61 100644
--- a/crates/core/src/commands/wait.rs
+++ b/crates/core/src/commands/wait.rs
@@ -206,9 +206,10 @@ fn wait_for_text(
match snapshot::build(adapter, &opts, app.as_deref(), None, deadline) {
Ok(mut result) => {
let matches = wait_text_match::find(&result.tree, &normalized_text, expected_count);
- let matched = expected_count
- .map(|expected| matches.len() == expected)
- .unwrap_or_else(|| !matches.is_empty());
+ let matched = match expected_count {
+ Some(expected) => result.complete && matches.len() == expected,
+ None => !matches.is_empty(),
+ };
if matched {
let store = RefStore::for_session(context.session_id())?;
let snapshot_id = store.save_new_snapshot(&result.refmap)?;
diff --git a/crates/core/src/commands/wait_scenario_tests.rs b/crates/core/src/commands/wait_scenario_tests.rs
index 604f069c7..dc58ce177 100644
--- a/crates/core/src/commands/wait_scenario_tests.rs
+++ b/crates/core/src/commands/wait_scenario_tests.rs
@@ -1,7 +1,7 @@
use super::test_support::wait_args;
use super::*;
use crate::adapter::{ActionOps, InputOps, ObservationOps, SystemOps};
-use crate::{AdapterError, WindowInfo, adapter::WindowFilter};
+use crate::{AdapterError, ErrorCode, WindowInfo, adapter::WindowFilter};
struct TextlessTreeAdapter;
@@ -180,6 +180,234 @@ fn text_wait_with_count_zero_detects_absence() {
assert_eq!(value["count"], 0);
}
+fn ready_button(name: &str) -> crate::AccessibilityNode {
+ crate::AccessibilityNode {
+ ref_id: None,
+ role: "button".into(),
+ identity: crate::NodeIdentity {
+ name: Some(name.into()),
+ ..Default::default()
+ },
+ presentation: Default::default(),
+ children_count: None,
+ subtree_truncated: false,
+ children: vec![],
+ }
+}
+
+fn truncated_container(child_count: u32) -> crate::AccessibilityNode {
+ crate::AccessibilityNode {
+ ref_id: None,
+ role: "group".into(),
+ identity: crate::NodeIdentity {
+ name: Some("list".into()),
+ ..Default::default()
+ },
+ presentation: Default::default(),
+ children_count: Some(child_count),
+ subtree_truncated: true,
+ children: vec![],
+ }
+}
+
+fn doc_window(children: Vec<crate::AccessibilityNode>) -> crate::AccessibilityNode {
+ crate::AccessibilityNode {
+ ref_id: None,
+ role: "window".into(),
+ identity: crate::NodeIdentity {
+ name: Some("Doc".into()),
+ ..Default::default()
+ },
+ presentation: Default::default(),
+ children_count: None,
+ subtree_truncated: false,
+ children,
+ }
+}
+
+fn test_app_window() -> WindowInfo {
+ WindowInfo {
+ id: "w-1".into(),
+ title: "Doc".into(),
+ app: "TestApp".into(),
+ pid: crate::ProcessId::new(1),
+ process_instance: Some("test-instance".into()),
+ bounds: None,
+ state: crate::WindowState {
+ is_focused: true,
+ ..Default::default()
+ },
+ }
+}
+
+fn test_app_windows() -> Result<Vec<WindowInfo>, AdapterError> {
+ Ok(vec![test_app_window()])
+}
+
+fn text_wait_args(text: &str, count: Option<usize>, timeout_ms: u64) -> WaitArgs {
+ WaitArgs {
+ mode: WaitModeArgs {
+ text: Some(text.into()),
+ ..wait_args().mode
+ },
+ predicate: WaitPredicateArgs {
+ count,
+ ..wait_args().predicate
+ },
+ timeout_ms,
+ app: Some("TestApp".into()),
+ }
+}
+
+fn expect_timeout(result: Result<Value, AppError>) -> crate::AdapterError {
+ let AppError::Adapter(adapter_err) = result.unwrap_err() else {
+ panic!("expected adapter error")
+ };
+ assert_eq!(adapter_err.code, ErrorCode::Timeout, "expected TIMEOUT");
+ adapter_err
+}
+
+macro_rules! text_tree_adapter {
+ ($name:ident, $tree:expr) => {
+ struct $name;
+
+ impl ObservationOps for $name {
+ fn observe_tree(
+ &self,
+ root: crate::live_locator::ObservationRoot<'_>,
+ _request: &crate::live_locator::ObservationRequest,
+ ) -> Result<crate::live_locator::ObservedTree, AdapterError> {
+ crate::adapter::observed_tree(&root, $tree)
+ }
+
+ fn list_windows(
+ &self,
+ _filter: &WindowFilter,
+ _deadline: crate::Deadline,
+ ) -> Result<Vec<WindowInfo>, AdapterError> {
+ test_app_windows()
+ }
+ }
+
+ impl ActionOps for $name {}
+
+ impl InputOps for $name {}
+
+ impl SystemOps for $name {}
+ };
+}
+
+text_tree_adapter!(
+ IncompleteTextTreeAdapter,
+ doc_window(vec![ready_button("ready one"), truncated_container(2)])
+);
+text_tree_adapter!(
+ AllHiddenIncompleteAdapter,
+ doc_window(vec![truncated_container(2)])
+);
+text_tree_adapter!(
+ CompleteTextTreeAdapter,
+ doc_window(vec![
+ ready_button("ready one"),
+ ready_button("ready two"),
+ ready_button("ready three"),
+ ])
+);
+
+#[test]
+fn text_wait_with_count_on_incomplete_observation_times_out() {
+ let _guard = crate::refs_test_support::HomeGuard::new();
+
+ let adapter_err = expect_timeout(execute(
+ text_wait_args("ready", Some(1), 600),
+ &IncompleteTextTreeAdapter,
+ &CommandContext::default(),
+ ));
+
+ let details = adapter_err.details.expect("timeout carries details");
+ assert_eq!(details["kind"], "wait_timeout");
+ assert_eq!(details["expected_count"], 1);
+}
+
+#[test]
+fn text_wait_with_count_zero_on_incomplete_observation_times_out() {
+ let _guard = crate::refs_test_support::HomeGuard::new();
+
+ let adapter_err = expect_timeout(execute(
+ text_wait_args("ready", Some(0), 600),
+ &AllHiddenIncompleteAdapter,
+ &CommandContext::default(),
+ ));
+
+ let details = adapter_err.details.expect("timeout carries details");
+ assert_eq!(details["kind"], "wait_timeout");
+ assert_eq!(details["expected_count"], 0);
+}
+
+#[test]
+fn text_wait_without_count_on_incomplete_observation_matches_visible() {
+ let _guard = crate::refs_test_support::HomeGuard::new();
+
+ let value = execute(
+ text_wait_args("ready", None, 1_000),
+ &IncompleteTextTreeAdapter,
+ &CommandContext::default(),
+ )
+ .unwrap();
+
+ assert_eq!(value["found"], true);
+ assert_eq!(value["role"], "button");
+ assert!(
+ value.get("count").is_none(),
+ "at-least-one waits must not report a count"
+ );
+}
+
+#[test]
+fn text_wait_without_count_on_all_hidden_incomplete_observation_times_out() {
+ let _guard = crate::refs_test_support::HomeGuard::new();
+
+ let adapter_err = expect_timeout(execute(
+ text_wait_args("ready", None, 600),
+ &AllHiddenIncompleteAdapter,
+ &CommandContext::default(),
+ ));
+
+ let details = adapter_err.details.expect("timeout carries details");
+ assert_eq!(details["kind"], "wait_timeout");
+ assert!(details["expected_count"].is_null());
+}
+
+#[test]
+fn text_wait_with_count_on_complete_observation_matches_exact_count() {
+ let _guard = crate::refs_test_support::HomeGuard::new();
+
+ let value = execute(
+ text_wait_args("ready", Some(3), 1_000),
+ &CompleteTextTreeAdapter,
+ &CommandContext::default(),
+ )
+ .unwrap();
+
+ assert_eq!(value["found"], true);
+ assert_eq!(value["count"], 3);
+}
+
+#[test]
+fn text_wait_with_count_on_complete_observation_times_out_when_count_differs() {
+ let _guard = crate::refs_test_support::HomeGuard::new();
+
+ let adapter_err = expect_timeout(execute(
+ text_wait_args("ready", Some(1), 600),
+ &CompleteTextTreeAdapter,
+ &CommandContext::default(),
+ ));
+
+ let details = adapter_err.details.expect("timeout carries details");
+ assert_eq!(details["kind"], "wait_timeout");
+ assert_eq!(details["expected_count"], 1);
+}
+
struct MenuWaitAdapter {
open_seen: std::sync::Mutex<Option<bool>>,
}