Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

Browser loop stop: a repeat that changes the page runs

Ada (kwant bot)f2864b1cfa84
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f2b4fc9d..3ea6dd6b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -59,8 +59,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
### Changed
- **Bots see what each browser step changed, and stop going round in circles.** After every click,
key or typing, the bot gets one line saying what changed: the page it went to, or the new and gone
- lines. If its last 3 steps changed nothing, or it asks for the same click a 3rd time in a row, it
- is told to stop and try something else. The page text bots read now keeps its line breaks.
+ lines. If its last 3 steps changed nothing, or it asks a 3rd time in a row for the same click that
+ changed nothing, it is told to stop and try something else. The page text bots read now keeps its line breaks.
- **Bots click page controls by number, and never through a banner.** The page a bot reads lists
its buttons, links and fields as numbered refs with their role (`[9] button "Add to cart" · Sauce
Labs Bike Light`), naming the product or row when several share a name. A bot clicks or types by
diff --git a/docs/bot-browser.md b/docs/bot-browser.md
index 5ea36af7..488c6355 100644
--- a/docs/bot-browser.md
+++ b/docs/bot-browser.md
@@ -39,8 +39,10 @@ alone is no change; for keys (Tab) it is.
- 3 actions in a row that change nothing: the third returns `stalled: true` and `error` set to
`STALL_STOP` (try something else, or tell the person). The count then starts over.
- The same click (same ref or label) or the same typing into the same place a 3rd time in a row on
- the same address is refused before it runs (`REPEAT_STOP`), until the bot does something else.
- Scrolls and keys don't count and don't break a run.
+ the same address, when the two before it changed nothing, is refused before it runs
+ (`REPEAT_STOP`), until the bot does something else. A repeat that changes the page (a counter,
+ "Load more") resets the count, so it can run any number of times. Scrolls and keys don't count
+ and don't break a run.
The page text in page states is now `document.body.innerText` (laid out in lines, hidden parts left
out) instead of the text of a detached copy, which ran lines together.
diff --git a/src/sandbox/progress.rs b/src/sandbox/progress.rs
index b650744f..39d0f339 100644
--- a/src/sandbox/progress.rs
+++ b/src/sandbox/progress.rs
@@ -103,8 +103,8 @@ pub fn action_key(action: &str, target: Option<&ClickTarget>, text: Option<&str>
/// How many times in a row before a bot is stopped.
pub const STALL_AFTER: u32 = 3;
-pub const REPEAT_STOP: &str = "Not done: this is the 3rd time in a row you asked for exactly this action on this page. \
- Don't repeat it; try something else (another control, scroll, a different page), or tell the person what is in the way.";
+pub const REPEAT_STOP: &str = "Not done: you asked for exactly this action on this page twice in a row and it changed \
+ nothing either time. Don't repeat it; try something else (another control, scroll, a different page), or tell the person what is in the way.";
pub const STALL_STOP: &str = "Stop: your last 3 actions changed nothing on this page. Don't repeat them; try something else \
(another control, scroll, a different page), or tell the person what is in the way.";
@@ -112,7 +112,10 @@ pub const STALL_STOP: &str = "Stop: your last 3 actions changed nothing on this
#[derive(Debug, Default)]
pub struct Tracker {
last: Option<(String, String)>,
+ /// How many times in a row `last` ran and changed nothing.
repeats: u32,
+ /// Whether the action now running is `last`, so [`Tracker::after`] counts it.
+ running_last: bool,
unchanged: u32,
}
@@ -123,21 +126,29 @@ impl Tracker {
}
/// Before an action: `Some(REPEAT_STOP)` when it would be the same action a 3rd time in a row
- /// on `url`. It keeps refusing until the bot does something else.
+ /// on `url` and the two before it changed nothing. It keeps refusing until the bot does
+ /// something else. The same click that keeps changing the page (a counter, "Load more") runs.
pub fn before(&mut self, url: &str, key: Option<&str>) -> Option<&'static str> {
+ self.running_last = false;
let key = key?;
let same = self.last.as_ref().is_some_and(|(u, k)| u == url && k == key);
if same && self.repeats + 1 >= STALL_AFTER {
return Some(REPEAT_STOP);
}
- self.repeats = if same { self.repeats + 1 } else { 1 };
- self.last = Some((url.to_string(), key.to_string()));
+ if !same {
+ self.repeats = 0;
+ self.last = Some((url.to_string(), key.to_string()));
+ }
+ self.running_last = true;
None
}
/// After an action: `Some(STALL_STOP)` when it is the 3rd in a row that changed nothing.
/// The count starts over after it is reported.
pub fn after(&mut self, changed: bool) -> Option<&'static str> {
+ if std::mem::take(&mut self.running_last) {
+ self.repeats = if changed { 0 } else { self.repeats + 1 };
+ }
if changed {
self.unchanged = 0;
return None;
@@ -191,24 +202,32 @@ mod tests {
}
#[test]
- fn the_same_action_a_third_time_is_refused_until_the_bot_does_something_else() {
+ fn the_same_action_that_changed_nothing_is_refused_a_third_time_until_the_bot_does_something_else() {
let mut t = Tracker::default();
let add = action_key("click", Some(&ClickTarget::Ref(9)), None);
let add = add.as_deref();
- assert_eq!(t.before("u", add), None);
- assert_eq!(t.before("u", add), None);
- assert_eq!(t.before("u", add), Some(REPEAT_STOP));
- assert_eq!(t.before("u", add), Some(REPEAT_STOP), "still refused");
- assert_eq!(t.before("u", Some("click ref 3")), None);
- assert_eq!(t.before("u", add), None, "after something else it may again");
+ // Runs and changes nothing (`after` is only called for actions that ran)
+ let idle = |t: &mut Tracker, url: &str, key: Option<&str>| {
+ let stop = t.before(url, key);
+ if stop.is_none() {
+ t.after(false);
+ }
+ stop
+ };
+ assert_eq!(idle(&mut t, "u", add), None);
+ assert_eq!(idle(&mut t, "u", add), None);
+ assert_eq!(idle(&mut t, "u", add), Some(REPEAT_STOP));
+ assert_eq!(idle(&mut t, "u", add), Some(REPEAT_STOP), "still refused");
+ assert_eq!(idle(&mut t, "u", Some("click ref 3")), None);
+ assert_eq!(idle(&mut t, "u", add), None, "after something else it may again");
// Scrolls and keys don't count and don't break a run
assert_eq!(action_key("scroll", None, None), None);
assert_eq!(action_key("key", None, None), None);
- assert_eq!(t.before("u", None), None);
- assert_eq!(t.before("u", add), None);
- assert_eq!(t.before("u", add), Some(REPEAT_STOP));
+ assert_eq!(idle(&mut t, "u", None), None);
+ assert_eq!(idle(&mut t, "u", add), None);
+ assert_eq!(idle(&mut t, "u", add), Some(REPEAT_STOP));
// The same click on another page is another action
- assert_eq!(t.before("u2", add), None);
+ assert_eq!(idle(&mut t, "u2", add), None);
// Typing different text is a different action; a label is matched without case
assert_ne!(action_key("type", None, Some("a")), action_key("type", None, Some("b")));
assert_eq!(
@@ -216,7 +235,32 @@ mod tests {
action_key("click", Some(&ClickTarget::Text("next".into())), None)
);
t.reset();
- assert_eq!(t.before("u2", add), None);
+ assert_eq!(idle(&mut t, "u2", add), None);
+ }
+
+ #[test]
+ fn the_same_click_runs_as_long_as_it_changes_the_page() {
+ let mut t = Tracker::default();
+ let more = Some("click label increase");
+ for i in 1..=5 {
+ assert_eq!(t.before("u", more), None, "click {i}");
+ assert_eq!(t.after(true), None);
+ }
+ // Then it stops changing anything: two more run, the third is refused
+ assert_eq!(t.before("u", more), None);
+ t.after(false);
+ assert_eq!(t.before("u", more), None);
+ t.after(false);
+ assert_eq!(t.before("u", more), Some(REPEAT_STOP));
+ // A scroll that changes the page doesn't clear the click's count
+ let mut t = Tracker::default();
+ assert_eq!(t.before("u", more), None);
+ t.after(false);
+ assert_eq!(t.before("u", None), None);
+ t.after(true);
+ assert_eq!(t.before("u", more), None);
+ t.after(false);
+ assert_eq!(t.before("u", more), Some(REPEAT_STOP));
}
#[test]
diff --git a/tests/browser_progress_test.rs b/tests/browser_progress_test.rs
index 37327bd8..f784d375 100644
--- a/tests/browser_progress_test.rs
+++ b/tests/browser_progress_test.rs
@@ -1,7 +1,7 @@
//! What changed after each action in kwnt's own browser, and the loop stop, through a real
//! Chrome: each `/act` says what is new or gone (or that it went to another page), waits a moment
-//! for a slow change, and stops a bot whose last 3 actions changed nothing or who asks for the
-//! same action a 3rd time in a row. Opening an address starts over. Skipped when no Chrome is
+//! for a slow change, and stops a bot whose last 3 actions changed nothing or who asks a 3rd time
+//! in a row for the same action that changed nothing. Opening an address starts over. Skipped when no Chrome is
//! installed.
#[path = "support/chrome_guard.rs"]
mod chrome_guard;
@@ -145,20 +145,27 @@ async fn each_action_says_what_changed_and_a_loop_is_stopped() {
assert_eq!(page["error"], progress::STALL_STOP, "{page}");
assert_eq!(page["stalled"], true);
- // The same click a third time in a row is refused before it runs, until something else
- act(&e, click("Add")).await;
- act(&e, click("Add")).await;
- let page = act(&e, click("Add")).await;
+ // The same click that changes the page every time runs every time (Rigel's counter page)
+ for n in 2..=6 {
+ let page = act(&e, click("Add")).await;
+ assert!(page["error"].is_null(), "click {n}: {page}");
+ assert_eq!(page["changed"], format!("New: \"Count {n}\". Gone: \"Count {}\".", n - 1), "{page}");
+ }
+ // The same click that changed nothing twice is refused a third time before it runs, until
+ // something else
+ act(&e, click("Nothing")).await;
+ act(&e, click("Nothing")).await;
+ let page = act(&e, click("Nothing")).await;
assert_eq!(page["error"], progress::REPEAT_STOP, "{page}");
- assert!(page["text"].as_str().unwrap().contains("Count 3"), "not clicked: {page}");
- let page = act(&e, click("Add")).await;
+ let page = act(&e, click("Nothing")).await;
assert_eq!(page["error"], progress::REPEAT_STOP, "still refused");
act(&e, json!({"action": "key", "key": "Tab"})).await;
- let page = act(&e, click("Add")).await;
+ let page = act(&e, click("Nothing")).await;
assert_eq!(page["error"], progress::REPEAT_STOP, "a key doesn't count as something else");
- act(&e, click("Nothing")).await;
let page = act(&e, click("Add")).await;
- assert!(page["error"].is_null() && page["text"].as_str().unwrap().contains("Count 4"), "{page}");
+ assert!(page["error"].is_null() && page["text"].as_str().unwrap().contains("Count 7"), "{page}");
+ let page = act(&e, click("Nothing")).await;
+ assert!(page["error"].is_null(), "after something else it may again: {page}");
// Another page
let page = act(&e, click("Other page")).await;
@@ -166,10 +173,10 @@ async fn each_action_says_what_changed_and_a_loop_is_stopped() {
// Opening an address starts over
open(&e, &format!("{base}/counter")).await;
- act(&e, click("Add")).await;
- act(&e, click("Add")).await;
+ act(&e, click("Nothing")).await;
+ act(&e, click("Nothing")).await;
open(&e, &format!("{base}/counter")).await;
- let page = act(&e, click("Add")).await;
+ let page = act(&e, click("Nothing")).await;
assert!(page["error"].is_null(), "{page}");
}