Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(browser): show approval targets for selectors and locators

Steven Enamakelcee667a8d5f1
diff --git a/crates/openhuman-core/src/tools/impl/browser/browser.rs b/crates/openhuman-core/src/tools/impl/browser/browser.rs
index 67b0744184..f8d0666631 100644
--- a/crates/openhuman-core/src/tools/impl/browser/browser.rs
+++ b/crates/openhuman-core/src/tools/impl/browser/browser.rs
@@ -61,6 +61,48 @@ fn needs_host_confirmation(action: &Action) -> bool {
)
}
+fn approval_target(action: &Action) -> (Option<&str>, String) {
+ let target = match action {
+ Action::Click { target, .. }
+ | Action::DoubleClick { target }
+ | Action::Fill { target, .. }
+ | Action::Select { target, .. }
+ | Action::Check { target, .. } => Some(target),
+ Action::Type { target, .. } => target.as_ref(),
+ _ => None,
+ };
+ let preview = |raw: &str| {
+ let cleaned = raw.chars().filter(|c| !c.is_control()).collect::<String>();
+ let mut short = cleaned.chars().take(96).collect::<String>();
+ if cleaned.chars().count() > 96 {
+ short.push('…');
+ }
+ short
+ };
+ match target {
+ Some(Target::Ref { value }) => (Some(value), format!(" @{value}")),
+ Some(Target::Selector { value }) => (None, format!(" CSS selector {:?}", preview(value))),
+ Some(Target::Locator { value }) => {
+ let name = value
+ .name
+ .as_deref()
+ .map(|name| format!(" named {:?}", preview(name)))
+ .unwrap_or_default();
+ (
+ None,
+ format!(
+ " {:?} locator {:?}{name} (match {}, exact={})",
+ value.by,
+ preview(&value.value),
+ value.index.saturating_add(1),
+ value.exact
+ ),
+ )
+ }
+ None => (None, String::new()),
+ }
+}
+
async fn approve_browser_action(
client: &BrowserClient,
session: &SessionId,
@@ -91,41 +133,13 @@ async fn approve_browser_action(
let digest = Sha256::digest(serde_json::to_vec(
&json!({"url": before.url, "action": action_json}),
)?);
- let target_ref = match action {
- Action::Click {
- target: Target::Ref { value },
- ..
- }
- | Action::DoubleClick {
- target: Target::Ref { value },
- }
- | Action::Fill {
- target: Target::Ref { value },
- ..
- }
- | Action::Select {
- target: Target::Ref { value },
- ..
- }
- | Action::Check {
- target: Target::Ref { value },
- ..
- } => Some(value.as_str()),
- Action::Type {
- target: Some(Target::Ref { value }),
- ..
- } => Some(value.as_str()),
- _ => None,
- };
+ let (target_ref, target_detail) = approval_target(action);
let input_detail = match action {
Action::Fill { value, .. } => format!(" ({} input characters)", value.chars().count()),
Action::Type { text, .. } => format!(" ({} input characters)", text.chars().count()),
Action::Press { key } => format!(" ({key})"),
_ => String::new(),
};
- let target_detail = target_ref
- .map(|reference| format!(" @{reference}"))
- .unwrap_or_default();
let digest_hex = format!("{digest:x}");
let display_target = format!(
"{kind}{target_detail}{input_detail} on {origin} [action {}] — review the browser tool input before allowing",
@@ -133,7 +147,8 @@ async fn approve_browser_action(
);
let summary = format!("Browser {display_target}");
// A digest binds the prompt to the complete action and URL without
- // persisting form values, selectors, or sensitive URL query parameters.
+ // persisting form values or sensitive URL query parameters. The bounded
+ // selector/locator preview lets the host review which element is targeted.
let args = json!({"action": kind, "origin": origin, "target": display_target,
"target_ref": target_ref, "exact_action_sha256": digest_hex});
match gate.intercept_forced("browser", &summary, args).await {
diff --git a/crates/openhuman-core/src/tools/impl/browser/browser_tinybrowser_tests.rs b/crates/openhuman-core/src/tools/impl/browser/browser_tinybrowser_tests.rs
index c1cd3cf76d..e36bb65e5a 100644
--- a/crates/openhuman-core/src/tools/impl/browser/browser_tinybrowser_tests.rs
+++ b/crates/openhuman-core/src/tools/impl/browser/browser_tinybrowser_tests.rs
@@ -57,6 +57,32 @@ fn every_direct_or_controller_action_that_can_submit_needs_confirmation() {
}));
}
+#[test]
+fn approval_describes_selector_and_locator_targets() {
+ let click = |selector: &str| Action::Click {
+ target: Target::selector(selector),
+ new_tab: false,
+ };
+ let submit = click("#submit");
+ let (reference, first) = approval_target(&submit);
+ let (_, second) = approval_target(&click("#delete"));
+ assert!(reference.is_none());
+ assert!(first.contains("CSS selector") && first.contains("#submit"));
+ assert_ne!(first, second);
+
+ let (_, locator) = approval_target(&Action::Fill {
+ target: Target::locator(Locator {
+ by: LocateBy::Role,
+ value: "textbox".into(),
+ name: Some("Email".into()),
+ ..Locator::default()
+ }),
+ value: "[email protected]".into(),
+ });
+ assert!(locator.contains("Role locator") && locator.contains("Email"));
+ assert!(!locator.contains("[email protected]"));
+}
+
#[test]
fn direct_actions_use_typed_targets_and_reject_unbounded_inputs() {
assert!(