diff --git a/crates/tinycomputer-browser/src/surface/mod.rs b/crates/tinycomputer-browser/src/surface/mod.rs index cc7129d7a..3386994be 100644 --- a/crates/tinycomputer-browser/src/surface/mod.rs +++ b/crates/tinycomputer-browser/src/surface/mod.rs @@ -393,6 +393,15 @@ impl BrowserSurface { // subtree can be read beside sight, not two. _ => return None, } + // Shadow reads may refresh the browser's native refs, so map Sight + // controls only after the last such read, against the exact snapshot + // generation that will be used by the next action. + let snapshot = self + .block(self.browser.snapshot(&id, SnapshotRequest::default())) + .ok()?; + screen + .candidates + .retain_mut(|candidate| attach_unique_snapshot_ref(candidate, &snapshot.refs)); self.keep_denoised(sight::denoised(result)); Some(screen) } @@ -461,5 +470,34 @@ impl BrowserSurface { } } +/// Resolve every native ref against the final accessibility snapshot, since +/// an intervening shadow read may have reassigned its `eN` identifiers. Sight +/// selectors remain available for hosts that support them, but are replaced +/// by a native ref only when the snapshot has one exact role/name match. +fn attach_unique_snapshot_ref( + candidate: &mut tinycomputer_core::surface::Candidate, + refs: &[tinycomputer_bus::browser::ElementRef], +) -> bool { + candidate + .children + .retain_mut(|child| attach_unique_snapshot_ref(child, refs)); + let Some(name) = candidate.name.as_deref().filter(|name| !name.is_empty()) else { + return sight::is_seen(&candidate.ref_id); + }; + let mut matches = refs + .iter() + .filter(|item| item.role == candidate.role && item.name.as_str() == name); + let Some(found) = matches.next() else { + // Sight refs are selector-backed and remain governed by the host's + // existing policy. Native refs must never survive a missing match. + return sight::is_seen(&candidate.ref_id); + }; + if matches.next().is_some() { + return sight::is_seen(&candidate.ref_id); + } + candidate.ref_id.clone_from(&found.id); + true +} + #[cfg(test)] mod surface_tests; diff --git a/crates/tinycomputer-browser/src/surface/surface_tests/operations_tests.rs b/crates/tinycomputer-browser/src/surface/surface_tests/operations_tests.rs index babcf5a4d..cb2a0584e 100644 --- a/crates/tinycomputer-browser/src/surface/surface_tests/operations_tests.rs +++ b/crates/tinycomputer-browser/src/surface/surface_tests/operations_tests.rs @@ -39,6 +39,28 @@ fn keys_are_spelled_the_way_agent_browser_reads_them() { assert_eq!(browser_key(" + ", Platform::MacOs), ""); } +#[test] +fn a_tree_snapshot_ref_does_not_run_sight_only_tab_preparation() { + let Harness { fake, surface, .. } = harness("tree-ref-same-tab", page_fake()); + let screen = surface.observe("shop", None, Depth::Full).unwrap(); + let search = screen + .candidates + .iter() + .find(|candidate| candidate.name.as_deref() == Some("Search")) + .unwrap() + .clone(); + assert!(search.ref_id.starts_with('e')); + + let result = surface.execute(JevOperation::Click, Some(search), None); + assert!(result.ok, "{:?}", result.error); + assert!(fake.sent().iter().all(|command| { + command["action"] != "evaluate" + || !command["script"] + .as_str() + .is_some_and(|script| script.contains("window.__tcOpen")) + })); +} + #[test] fn secret_type_preflight_uses_gate_metadata_without_sending_text_to_the_page() { #[derive(Default)] diff --git a/crates/tinycomputer-browser/src/surface/surface_tests/perception_tests.rs b/crates/tinycomputer-browser/src/surface/surface_tests/perception_tests.rs index 74ff3e20f..2191fb0bd 100644 --- a/crates/tinycomputer-browser/src/surface/surface_tests/perception_tests.rs +++ b/crates/tinycomputer-browser/src/surface/surface_tests/perception_tests.rs @@ -2,12 +2,92 @@ use serde_json::json; use tinycomputer_bus::JevOperation; -use tinycomputer_core::surface::{Depth, Surface}; +use tinycomputer_bus::browser::ElementRef; +use tinycomputer_core::surface::{Candidate, Depth, Surface}; use super::{Harness, harness, page_fake}; use crate::fake::{Fake, failure, ok}; use crate::surface::{Denoised, Perception}; +#[test] +fn sight_targets_map_to_unique_current_native_refs_only() { + let mut unique = Candidate { + ref_id: "seen:1".to_owned(), + role: "button".to_owned(), + name: Some("Accept cookies".to_owned()), + ..Candidate::default() + }; + crate::surface::attach_unique_snapshot_ref( + &mut unique, + &[ElementRef { + id: "e1".to_owned(), + role: "button".to_owned(), + name: "Accept cookies".to_owned(), + }], + ); + assert_eq!(unique.ref_id, "e1"); + + let mut duplicate = Candidate { + ref_id: "seen:2".to_owned(), + role: "button".to_owned(), + name: Some("Continue".to_owned()), + ..Candidate::default() + }; + crate::surface::attach_unique_snapshot_ref( + &mut duplicate, + &[ + ElementRef { + id: "e2".to_owned(), + role: "button".to_owned(), + name: "Continue".to_owned(), + }, + ElementRef { + id: "e3".to_owned(), + role: "button".to_owned(), + name: "Continue".to_owned(), + }, + ], + ); + assert_eq!(duplicate.ref_id, "seen:2"); + + let mut stale_shadow_ref = Candidate { + ref_id: "e2".to_owned(), + role: "button".to_owned(), + name: Some("Allow Selection".to_owned()), + ..Candidate::default() + }; + assert!(!crate::surface::attach_unique_snapshot_ref( + &mut stale_shadow_ref, + &[ElementRef { + id: "e2".to_owned(), + role: "button".to_owned(), + name: "Different target".to_owned(), + }], + )); + + let mut ambiguous_shadow_ref = Candidate { + ref_id: "e3".to_owned(), + role: "button".to_owned(), + name: Some("Continue".to_owned()), + ..Candidate::default() + }; + assert!(!crate::surface::attach_unique_snapshot_ref( + &mut ambiguous_shadow_ref, + &[ + ElementRef { + id: "e3".to_owned(), + role: "button".to_owned(), + name: "Continue".to_owned(), + }, + ElementRef { + id: "e4".to_owned(), + role: "button".to_owned(), + name: "Continue".to_owned(), + }, + ], + )); +} + /// A page read by sight: one field and one result link a card covers. fn sighted_fake() -> Fake { Fake::scripted(|command| match command["action"].as_str().unwrap() { @@ -29,6 +109,13 @@ fn sighted_fake() -> Fake { ] }}))) } + "snapshot" => Some(ok(&json!({ + "snapshot": "- textbox \"To\" [ref=e1]\n- link [ref=e2]", + "refs": { + "e1": {"role": "textbox", "name": "To"}, + "e2": {"role": "link", "name": ""} + } + }))), "click" => Some(failure( "Element is covered by