Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-cli): never take a stray key or end of input as the answer to the agents question (CUA-1862) (#5435)

Francesco Bonacci5df19ea9ba78
diff --git a/libs/cua/crates/cua-cli/src/agents.rs b/libs/cua/crates/cua-cli/src/agents.rs
index d08e62adb6..1819346749 100644
--- a/libs/cua/crates/cua-cli/src/agents.rs
+++ b/libs/cua/crates/cua-cli/src/agents.rs
@@ -143,18 +143,31 @@ impl Prompt {
}
}
+ /// One answer, lowercased and trimmed, or `None` when the input ended
+ /// before an answer came (Ctrl-D, a closed terminal). Running out of
+ /// input is never an answer: callers must not treat `None` as yes, no
+ /// or never.
fn read(&self, out: &mut dyn Write, q: &str) -> Option<String> {
+ // Keys typed before the question was on screen (an extra Enter at
+ // an earlier prompt, a press while the browser sign-in ran) are not
+ // answers to it.
+ discard_typeahead();
let _ = write!(out, "{q} ");
let _ = out.flush();
let mut s = String::new();
// Bounded: one line.
match std::io::stdin().lock().read_line(&mut s) {
- Ok(0) | Err(_) => None,
+ Ok(0) | Err(_) => {
+ line(out, "");
+ None
+ }
Ok(_) => Some(s.trim().to_ascii_lowercase()),
}
}
- /// `[Y/n]` (default yes) or `[y/N]`.
+ /// `[Y/n]` (default yes) or `[y/N]`. Enter takes the default; with no
+ /// answer at all (the input ended) the step is skipped, with a line
+ /// saying so.
pub fn confirm(&self, out: &mut dyn Write, q: &str, default: bool) -> bool {
if self.yes {
return true;
@@ -166,7 +179,10 @@ impl Prompt {
// Re-ask a bounded number of times on nonsense.
for _ in 0..5 {
match self.read(out, &format!("{q} {hint}")).as_deref() {
- None => return default,
+ None => {
+ line(out, NO_ANSWER);
+ return false;
+ }
Some("") => return default,
Some("y" | "yes") => return true,
Some("n" | "no") => return false,
@@ -176,21 +192,45 @@ impl Prompt {
default
}
- /// `[y/n/never]`; `None` means never.
- pub fn yes_no_never(&self, out: &mut dyn Write, q: &str) -> Option<bool> {
+ /// `[y/n/never]`.
+ pub fn yes_no_never(&self, out: &mut dyn Write, q: &str) -> Choice {
if self.yes {
- return Some(true);
+ return Choice::Yes;
}
for _ in 0..5 {
match self.read(out, &format!("{q} [y/n/never]")).as_deref() {
- None => return Some(false),
- Some("y" | "yes") => return Some(true),
- Some("n" | "no" | "") => return Some(false),
- Some("never") => return None,
+ None => return Choice::NoAnswer,
+ Some("y" | "yes") => return Choice::Yes,
+ Some("n" | "no" | "") => return Choice::No,
+ Some("never") => return Choice::Never,
Some(_) => line(out, "Please answer y, n or never."),
}
}
- Some(false)
+ Choice::No
+ }
+}
+
+/// The answer to a `[y/n/never]` question.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum Choice {
+ Yes,
+ No,
+ /// Do not ask again.
+ Never,
+ /// The input ended before anyone answered. Not a "no": nothing is
+ /// recorded, and the caller says what to run later.
+ NoAnswer,
+}
+
+const NO_ANSWER: &str = "No answer (the input ended), so cua skipped this step.";
+
+/// Throws away input waiting on the terminal (see [`Prompt::read`]).
+fn discard_typeahead() {
+ #[cfg(unix)]
+ // SAFETY: tcflush only acts on the descriptor; it fails harmlessly when
+ // stdin is not a terminal.
+ unsafe {
+ libc::tcflush(libc::STDIN_FILENO, libc::TCIFLUSH);
}
}
@@ -801,14 +841,21 @@ pub fn after_login(
line(out, "");
match prompt.yes_no_never(
out,
- "Configure cua skills and the cua MCP server for your AI coding agent(s)?",
+ "Let your AI coding agents (like Claude Code and Codex) use cua?",
) {
- Some(true) => {}
- Some(false) => {
+ Choice::Yes => {}
+ Choice::No => {
line(out, "Skipped. Run `cua agents setup` any time.");
return Ok(0);
}
- None => {
+ Choice::NoAnswer => {
+ line(
+ out,
+ "No answer (the input ended), so your agents are unchanged. Run `cua agents setup` when you are ready.",
+ );
+ return Ok(0);
+ }
+ Choice::Never => {
cua_agent_setup::config::set_onboarding_declined(setup.env(), true)
.map_err(|e| internal(e.to_string()))?;
line(
diff --git a/libs/cua/crates/cua-cli/tests/agents_login.rs b/libs/cua/crates/cua-cli/tests/agents_login.rs
index 45d9f5abe9..c5dff87a6c 100644
--- a/libs/cua/crates/cua-cli/tests/agents_login.rs
+++ b/libs/cua/crates/cua-cli/tests/agents_login.rs
@@ -598,6 +598,11 @@ impl Pty {
}
}
+ /// Everything printed so far.
+ fn output(&self) -> String {
+ self.buf.lock().unwrap().clone()
+ }
+
fn send(&mut self, s: &str) {
self.writer.write_all(s.as_bytes()).unwrap();
self.writer.flush().unwrap();
@@ -633,7 +638,7 @@ async fn interactive_login_onboarding_prompts() {
let mut p = Pty::spawn(&h, &["auth", "login"]);
let at = p.expect("Logged in to Cua as tester", 0);
let at = p.expect(
- "Configure cua skills and the cua MCP server for your AI coding agent(s)? [y/n/never]",
+ "Let your AI coding agents (like Claude Code and Codex) use cua? [y/n/never]",
at,
);
p.send("y\r");
@@ -708,6 +713,92 @@ async fn interactive_login_onboarding_prompts() {
drop(iss);
}
+/// The sign-in's agent question is answered by a person, not by input that
+/// was already waiting (an extra Enter at an earlier prompt, a key pressed
+/// during the browser step) and not by the input ending.
+#[cfg(unix)]
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn onboarding_question_is_not_answered_by_typeahead() {
+ let (iss, _) = issuer(true).await;
+ let mut h = home();
+ h.set("CUA_OIDC_ISSUER", &iss.url)
+ .set("CUA_OIDC_POLL_UNIT_MS", "5")
+ .set("CUA_AUTH_FLOW", "device");
+ mkdirs(&h, &[".claude"]);
+ let h = h;
+ tokio::task::spawn_blocking(move || {
+ let mut p = Pty::spawn(&h, &["auth", "login"]);
+ // Two Enters typed before the question exists.
+ p.send("\r\r");
+ let at = p.expect("[y/n/never]", 0);
+ // Had the stray Enter been taken as "no", this would already be there.
+ std::thread::sleep(Duration::from_millis(500));
+ assert!(
+ !p.output().contains("Skipped."),
+ "typeahead answered the question: {}",
+ p.output()
+ );
+ p.send("y\r");
+ let at = p.expect("✓ Claude Code", at);
+ let at = p.expect("default cua skills? [Y/n]", at);
+ p.send("\r");
+ p.expect("Configure cua MCP server connection? [Y/n]", at);
+ p.send("n\r");
+ let (code, out) = p.wait();
+ assert_eq!(code, 0, "{out}");
+ assert!(
+ h.dir
+ .path()
+ .join(".claude/skills/cua-driver/SKILL.md")
+ .is_file(),
+ "the answer after the wait was taken: {out}"
+ );
+ })
+ .await
+ .unwrap();
+ drop(iss);
+}
+
+#[cfg(unix)]
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn onboarding_question_with_no_answer_changes_nothing() {
+ let (iss, _) = issuer(true).await;
+ let mut h = home();
+ h.set("CUA_OIDC_ISSUER", &iss.url)
+ .set("CUA_OIDC_POLL_UNIT_MS", "5")
+ .set("CUA_AUTH_FLOW", "device");
+ mkdirs(&h, &[".claude"]);
+ let h = h;
+ tokio::task::spawn_blocking(move || {
+ let mut p = Pty::spawn(&h, &["auth", "login"]);
+ p.expect("[y/n/never]", 0);
+ // Ctrl-D at an empty line: the input ends.
+ p.send("\x04");
+ let (code, out) = p.wait();
+ assert_eq!(code, 0, "{out}");
+ assert!(
+ out.contains("No answer (the input ended), so your agents are unchanged."),
+ "{out}"
+ );
+ assert!(!out.contains("Skipped."), "no input is not a 'no': {out}");
+ assert!(
+ !h.dir.path().join(".claude/skills").exists(),
+ "nothing was installed"
+ );
+ // Not recorded as "never": the next sign-in asks again.
+ let cfg = std::fs::read_to_string(h.cua_home().join("config")).unwrap_or_default();
+ assert!(!cfg.contains("never"), "{cfg}");
+ let mut p = Pty::spawn(&h, &["auth", "login"]);
+ p.expect("[y/n/never]", 0);
+ p.send("n\r");
+ let (code, out) = p.wait();
+ assert_eq!(code, 0, "{out}");
+ })
+ .await
+ .unwrap();
+ drop(iss);
+}
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn the_spaces_app_session_migrates_into_the_shared_store() {
let h = home();