diff --git a/crates/tinycomputer-engine/src/agentic/flow/act/mod.rs b/crates/tinycomputer-engine/src/agentic/flow/act/mod.rs index 66e3db009..bbcf001d1 100644 --- a/crates/tinycomputer-engine/src/agentic/flow/act/mod.rs +++ b/crates/tinycomputer-engine/src/agentic/flow/act/mod.rs @@ -135,7 +135,9 @@ const MOVES: &[(&str, &str)] = &[ /// Standard macOS shortcuts that are safe to try: none sends, deletes, or /// quits. `confirm` (Return) commits text typed into a field; it is refused -/// while a sheet or alert is showing, where Return presses the default button. +/// while a sheet or alert is showing, where Return presses the default button, +/// and outside a search box in a flow with a `stop_before`, where Return may +/// send. pub(super) const SHORTCUTS: &[(&str, &str, &str)] = &[ ( "new_item", diff --git a/crates/tinycomputer-engine/src/agentic/flow/act/moves.rs b/crates/tinycomputer-engine/src/agentic/flow/act/moves.rs index 0ec2f604b..1f11d6d1f 100644 --- a/crates/tinycomputer-engine/src/agentic/flow/act/moves.rs +++ b/crates/tinycomputer-engine/src/agentic/flow/act/moves.rs @@ -85,6 +85,12 @@ impl FlowRun<'_, B> { )); return Ok(Move::Skipped); } + // In a composer Return sends: a flow that stops before an + // irreversible step must press a control, which is gated. + if combo == "return" && !searching && !self.stop_before.is_empty() { + self.history.push("refused return: this flow stops before an irreversible step, and Return may perform it; press the control instead".to_owned()); + return Ok(Move::Skipped); + } let app = self.app.clone(); let reply = self .act(log, &format!("press {combo}"), None, move |backend| { diff --git a/crates/tinycomputer-engine/src/agentic/flow/flow_tests/do_loop_tests.rs b/crates/tinycomputer-engine/src/agentic/flow/flow_tests/do_loop_tests.rs index ec7c33d29..6f2d1cbe6 100644 --- a/crates/tinycomputer-engine/src/agentic/flow/flow_tests/do_loop_tests.rs +++ b/crates/tinycomputer-engine/src/agentic/flow/flow_tests/do_loop_tests.rs @@ -1260,3 +1260,24 @@ fn a_pressed_controls_copies_are_only_on_the_other_items_of_its_list() { "the bar's own has none" ); } + +#[tokio::test] +async fn return_is_not_pressed_when_the_flow_stops_before_sending() { + // In a message composer Return sends. The flow declared it stops before + // sending, so the "confirm" shortcut must not press Return unasked. + let run = run_with( + App::default(), + json!({"app": "Mail", "steps": [ + "confirm the message text", + {"stop_before": "sending the message"} + ]}), + |request| request.max_actions = 2, + |id, question, _| match id { + "shortcut" => Some(pick(question, "confirm", 0.9)), + _ => None, + }, + ) + .await; + let presses = run.app.sim().presses.clone(); + assert!(!presses.iter().any(|key| key == "return"), "{presses:?}"); +} diff --git a/crates/tinycomputer-engine/src/agentic/flow/view/mod.rs b/crates/tinycomputer-engine/src/agentic/flow/view/mod.rs index 2c8224c88..cf79b3729 100644 --- a/crates/tinycomputer-engine/src/agentic/flow/view/mod.rs +++ b/crates/tinycomputer-engine/src/agentic/flow/view/mod.rs @@ -32,26 +32,51 @@ pub(in crate::agentic) fn is_banned( banned.contains(&signature(candidate)) || banned.contains(&press_key(candidate)) } +/// Words that commit to something wherever they sit in a label ("Post", +/// "Transfer $500", "Log out"). Matched as whole words, so "Postcode" and +/// "Design" stay pressable; "sign" counts unless it is signing in. +const COMMITTING_WORDS: &[&str] = &[ + "post", + "transfer", + "checkout", + "check out", + "log out", + "logout", + "sign", +]; + /// Whether a lower-cased label names an action that is hard to undo. A /// counter's minus button ("remove adult") is not: it only lowers a number. pub(in crate::agentic) fn destructive_label(evidence: &str) -> bool { + let words = format!( + " {} ", + evidence + .split(|character: char| !character.is_alphanumeric()) + .filter(|word| !word.is_empty()) + .collect::>() + .join(" ") + ) + .replace(" sign in ", " "); !tinycomputer_core::adjusts_a_count(evidence) - && [ - "delete", - "remove", - "send", - "purchase", - "buy", - "pay", - "submit", - "confirm", - "overwrite", - "quit without saving", - "empty trash", - "sign out", - ] - .iter() - .any(|term| evidence.contains(term)) + && (COMMITTING_WORDS + .iter() + .any(|word| words.contains(&format!(" {word} "))) + || [ + "delete", + "remove", + "send", + "purchase", + "buy", + "pay", + "submit", + "confirm", + "overwrite", + "quit without saving", + "empty trash", + "sign out", + ] + .iter() + .any(|term| evidence.contains(term))) } /// Whether `label` is named by a `stop_before` phrase the flow itself diff --git a/crates/tinycomputer-engine/src/agentic/flow/view/view_tests.rs b/crates/tinycomputer-engine/src/agentic/flow/view/view_tests.rs index 40d972850..379a0bc9c 100644 --- a/crates/tinycomputer-engine/src/agentic/flow/view/view_tests.rs +++ b/crates/tinycomputer-engine/src/agentic/flow/view/view_tests.rs @@ -224,3 +224,37 @@ fn the_elements_a_purpose_names_are_offered_first_in_their_own_order() { "filler names nothing" ); } + +#[test] +fn posting_transferring_checking_out_paying_deleting_logging_out_and_signing_are_destructive() { + let screen = clickable_screen(); + let pressable: Vec<&str> = [ + "Post", + "Transfer $500", + "Checkout", + "Check out", + "Pay", + "Delete", + "Log out", + "Sign", + "Sign up", + ] + .into_iter() + .filter(|name| !is_destructive(&button(name), &screen, &[])) + .collect(); + assert!( + pressable.is_empty(), + "pressed with no approval: {pressable:?}" + ); + // Signing in commits to nothing, and a word that only contains one of + // these is not one of them. + for name in [ + "Sign in", + "Sign-in with Google", + "Postcode", + "Design", + "Logo", + ] { + assert!(!is_destructive(&button(name), &screen, &[]), "{name}"); + } +}