From 1877b01e53eaed786172a1eab5b13345e0c680c0 Mon Sep 17 00:00:00 2001 From: Steven Enamakel Date: Wed, 21 Oct 2026 10:35:41 +0530 Subject: [PATCH 1/2] fix(browser): resolve sight targets from fresh snapshots Co-authored-by: Medulla --- .../tinycomputer-browser/src/surface/mod.rs | 34 +++++++ .../surface/surface_tests/operations_tests.rs | 22 +++++ .../surface/surface_tests/perception_tests.rs | 90 ++++++++++++++++--- .../tinycomputer-browser/src/surface/tabs.rs | 5 ++ 4 files changed, 141 insertions(+), 10 deletions(-) diff --git a/crates/tinycomputer-browser/src/surface/mod.rs b/crates/tinycomputer-browser/src/surface/mod.rs index cc7129d7a..de33a0c65 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()?; + for candidate in &mut screen.candidates { + attach_unique_snapshot_ref(candidate, &snapshot.refs); + } self.keep_denoised(sight::denoised(result)); Some(screen) } @@ -461,5 +470,30 @@ impl BrowserSurface { } } +/// Replace a Sight-only selector with a native snapshot ref only when the +/// current accessibility snapshot identifies exactly one control with the +/// same role and accessible name. Unmatched and duplicate controls retain +/// their Sight refs, which the host action gate will not treat as trusted. +fn attach_unique_snapshot_ref( + candidate: &mut tinycomputer_core::surface::Candidate, + refs: &[tinycomputer_bus::browser::ElementRef], +) { + if sight::is_seen(&candidate.ref_id) + && let Some(name) = candidate.name.as_deref() + { + let mut matches = refs + .iter() + .filter(|item| item.role == candidate.role && item.name.as_str() == name); + if let Some(found) = matches.next() + && matches.next().is_none() + { + candidate.ref_id.clone_from(&found.id); + } + } + for child in &mut candidate.children { + attach_unique_snapshot_ref(child, refs); + } +} + #[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..572d472c8 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,55 @@ 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"); +} + /// 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 +72,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 at its click point, so the input would land on that element instead.", )), @@ -44,10 +94,10 @@ fn sighted_fake() -> Fake { fn sight_reads_the_page_and_its_refs_reach_their_marks() { let Harness { fake, surface, .. } = harness("sight", sighted_fake()); let screen = surface.observe("flights", None, Depth::Skeleton).unwrap(); - assert!(!fake.actions().iter().any(|action| action == "snapshot")); + assert!(fake.actions().iter().any(|action| action == "snapshot")); assert_eq!(screen.app, "flights"); let field = screen.candidates[0].clone(); - assert_eq!(field.ref_id, "seen:1"); + assert_eq!(field.ref_id, "e1"); let typed = surface.execute( JevOperation::TypeText, @@ -56,8 +106,8 @@ fn sight_reads_the_page_and_its_refs_reach_their_marks() { ); assert!(typed.ok, "{:?}", typed.error); let fill = fake.last("fill"); - assert_eq!(fill["selector"], r#"[data-tc-seen="1"]"#, "{fill}"); - assert_eq!(fake.last("focus")["selector"], r#"[data-tc-seen="1"]"#); + assert_eq!(fill["selector"], "@e1", "{fill}"); + assert_eq!(fake.last("focus")["selector"], "@e1"); // An unnamed link a card covers is still clicked through its card: its // mark names it exactly. @@ -79,9 +129,17 @@ fn sight_reads_the_page_and_its_refs_reach_their_marks() { "{script}" ); - surface.observe("", Some("seen:1"), Depth::Full).unwrap(); - let scoped = fake.last("evaluate")["script"].as_str().unwrap().to_owned(); - assert!(scoped.contains(r#"("[data-tc-seen=\"1\"]", {"#)); + surface.observe("", Some("seen:2"), Depth::Full).unwrap(); + let sent = fake.sent(); + let scoped = sent + .iter() + .filter(|command| command["action"] == "evaluate") + .filter_map(|command| command["script"].as_str()) + .find(|script| { + script.contains("__tinycomputerSeen") && script.contains("data-tc-seen=\\\"2\\\"") + }) + .expect("the scoped Sight script uses the retained Sight ref"); + assert!(scoped.contains(r#"("[data-tc-seen=\"2\"]", {"#), "{scoped}"); } #[test] @@ -162,6 +220,14 @@ fn shadowed_fake(shadows: serde_json::Value, subtree_fails: bool) -> Fake { "e3": {"role": "button", "name": "Allow all"} } }))), + "snapshot" => Some(ok(&json!({ + "snapshot": "- button \"Add To Cart\" [ref=e1]\n- button \"Allow Selection\" [ref=e2]\n- button \"Allow all\" [ref=e3]", + "refs": { + "e1": {"role": "button", "name": "Add To Cart"}, + "e2": {"role": "button", "name": "Allow Selection"}, + "e3": {"role": "button", "name": "Allow all"} + } + }))), _ => None, }) } @@ -187,7 +253,7 @@ fn a_shadow_roots_controls_are_read_by_the_tree_beside_sight() { assert_eq!( names, [ - ("seen:1", "Add To Cart"), + ("e1", "Add To Cart"), ("e2", "Allow Selection"), ("e3", "Allow all") ] @@ -208,7 +274,11 @@ fn a_shadow_roots_controls_are_read_by_the_tree_beside_sight() { .iter() .any(|line| line.contains("We value your privacy")) ); - let subtree = fake.last("snapshot"); + let sent = fake.sent(); + let subtree = sent + .iter() + .find(|command| command["action"] == "snapshot" && command.get("selector").is_some()) + .unwrap(); assert_eq!(subtree["selector"], r#"[data-tc-seen="9"]"#, "{subtree}"); // A shadow root that draws no layer keeps the tree's own places. diff --git a/crates/tinycomputer-browser/src/surface/tabs.rs b/crates/tinycomputer-browser/src/surface/tabs.rs index 0a09209c0..ea5761f8c 100644 --- a/crates/tinycomputer-browser/src/surface/tabs.rs +++ b/crates/tinycomputer-browser/src/surface/tabs.rs @@ -120,6 +120,11 @@ impl BrowserSurface { /// (`SAME_TAB_JS`). Best effort: a page that refuses the script, or an /// element it cannot find, is pressed as it is. pub(super) fn keep_in_tab(&self, reference: &str) { + // Sight refs are CSS marks. A tree ref such as `e3` addresses the + // browser's own snapshot and cannot be resolved by document.querySelector. + if !sight::is_seen(reference) { + return; + } let selector = Value::String(sight::selector(reference)); let script = format!("{SAME_TAB_JS}(document.querySelector({selector}))"); if let Ok(id) = self.ensure_session() { From 9992b77ccef23603bc9bee649512d7ae85f07392 Mon Sep 17 00:00:00 2001 From: Steven Enamakel Date: Wed, 21 Oct 2026 10:40:32 +0530 Subject: [PATCH 2/2] fix(browser): reject stale shadow snapshot refs Co-authored-by: Medulla --- .../tinycomputer-browser/src/surface/mod.rs | 48 ++++++++++--------- .../surface/surface_tests/perception_tests.rs | 37 ++++++++++++++ 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/crates/tinycomputer-browser/src/surface/mod.rs b/crates/tinycomputer-browser/src/surface/mod.rs index de33a0c65..3386994be 100644 --- a/crates/tinycomputer-browser/src/surface/mod.rs +++ b/crates/tinycomputer-browser/src/surface/mod.rs @@ -399,9 +399,9 @@ impl BrowserSurface { let snapshot = self .block(self.browser.snapshot(&id, SnapshotRequest::default())) .ok()?; - for candidate in &mut screen.candidates { - attach_unique_snapshot_ref(candidate, &snapshot.refs); - } + screen + .candidates + .retain_mut(|candidate| attach_unique_snapshot_ref(candidate, &snapshot.refs)); self.keep_denoised(sight::denoised(result)); Some(screen) } @@ -470,29 +470,33 @@ impl BrowserSurface { } } -/// Replace a Sight-only selector with a native snapshot ref only when the -/// current accessibility snapshot identifies exactly one control with the -/// same role and accessible name. Unmatched and duplicate controls retain -/// their Sight refs, which the host action gate will not treat as trusted. +/// 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], -) { - if sight::is_seen(&candidate.ref_id) - && let Some(name) = candidate.name.as_deref() - { - let mut matches = refs - .iter() - .filter(|item| item.role == candidate.role && item.name.as_str() == name); - if let Some(found) = matches.next() - && matches.next().is_none() - { - candidate.ref_id.clone_from(&found.id); - } - } - for child in &mut candidate.children { - attach_unique_snapshot_ref(child, refs); +) -> 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)] 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 572d472c8..2191fb0bd 100644 --- a/crates/tinycomputer-browser/src/surface/surface_tests/perception_tests.rs +++ b/crates/tinycomputer-browser/src/surface/surface_tests/perception_tests.rs @@ -49,6 +49,43 @@ fn sight_targets_map_to_unique_current_native_refs_only() { ], ); 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.