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.

ci: make the cua-vmm streaming test and cua-cli agents_login tests deterministic (#5834)

Francesco Bonacci4764e9a385cf
diff --git a/libs/cua/crates/cua-agent-setup/src/lib.rs b/libs/cua/crates/cua-agent-setup/src/lib.rs
index 41e5dfd3c2..2aa0aaa806 100644
--- a/libs/cua/crates/cua-agent-setup/src/lib.rs
+++ b/libs/cua/crates/cua-agent-setup/src/lib.rs
@@ -182,7 +182,8 @@ impl HostEnv {
/// The real environment: `$HOME` (or `%USERPROFILE%`), `$PATH`, the
/// agent override variables, `/Applications` and `~/Applications`
- /// (or `$CUA_AGENT_APP_DIRS`).
+ /// (or `$CUA_AGENT_APP_DIRS`), and the usual install dirs after PATH
+ /// (or `$CUA_AGENT_BIN_DIRS`).
pub fn current() -> Self {
let home = std::env::var_os("HOME")
.or_else(|| std::env::var_os("USERPROFILE"))
@@ -218,7 +219,17 @@ impl HostEnv {
// A GUI app (Cua Spaces from the Dock) runs without the user's
// shell PATH, so an agent installed under a user bin dir would look
// missing. Search the usual install dirs after the real PATH.
- for dir in env.well_known_bin_dirs() {
+ // `CUA_AGENT_BIN_DIRS` (a path list, empty for none) replaces them,
+ // like `CUA_AGENT_APP_DIRS` does for apps: a hermetic run with a
+ // reduced PATH would otherwise still find the host's agents under
+ // `/opt/homebrew/bin` or `/usr/local/bin`.
+ let fallback = match std::env::var_os("CUA_AGENT_BIN_DIRS") {
+ Some(v) => std::env::split_paths(&v)
+ .filter(|p| !p.as_os_str().is_empty())
+ .collect(),
+ None => env.well_known_bin_dirs(),
+ };
+ for dir in fallback {
if !env.path.contains(&dir) {
env.path.push(dir);
}
diff --git a/libs/cua/crates/cua-cli/tests/agents_login.rs b/libs/cua/crates/cua-cli/tests/agents_login.rs
index 54a6b39881..b58b883602 100644
--- a/libs/cua/crates/cua-cli/tests/agents_login.rs
+++ b/libs/cua/crates/cua-cli/tests/agents_login.rs
@@ -1,8 +1,11 @@
//! `cua agents ...` and the `cua auth login` flows (browser PKCE with a
//! loopback redirect, device code, the automatic fallback) with the agent
//! onboarding, against a fake OIDC issuer in a temporary HOME. PATH is
-//! reduced to system dirs so no real agent CLI is ever found or run, and
-//! app bundles are hidden (`CUA_AGENT_APP_DIRS`).
+//! reduced to system dirs and the fallback install dirs (`/opt/homebrew/bin`,
+//! `/usr/local/bin`, ...) are dropped (`CUA_AGENT_BIN_DIRS`), so no real
+//! agent CLI is ever found or run, and app bundles are hidden
+//! (`CUA_AGENT_APP_DIRS`). The result does not depend on what is installed
+//! on the machine running the tests.
mod common;
use common::*;
@@ -17,7 +20,9 @@ use tokio::io::AsyncBufReadExt;
/// A home with no ambient agents.
fn home() -> Home {
let mut h = Home::new();
- h.set("PATH", "/usr/bin:/bin").set("CUA_AGENT_APP_DIRS", "");
+ h.set("PATH", "/usr/bin:/bin")
+ .set("CUA_AGENT_BIN_DIRS", "")
+ .set("CUA_AGENT_APP_DIRS", "");
h
}
diff --git a/libs/cua/crates/cua-vmm/src/lume/ssh.rs b/libs/cua/crates/cua-vmm/src/lume/ssh.rs
index 1557b9c2b2..65923d62e4 100644
--- a/libs/cua/crates/cua-vmm/src/lume/ssh.rs
+++ b/libs/cua/crates/cua-vmm/src/lume/ssh.rs
@@ -290,24 +290,84 @@ mod tests {
assert_eq!(out.stderr_str(), "it's set\n");
}
+ /// Opens the FIFO for writing without blocking: with no reader yet the
+ /// open fails at once with ENXIO instead of waiting for one.
+ #[cfg(unix)]
+ fn open_fifo_nonblocking(path: &std::path::Path) -> std::io::Result<std::fs::File> {
+ use std::os::unix::fs::OpenOptionsExt;
+ std::fs::OpenOptions::new()
+ .write(true)
+ .custom_flags(libc::O_NONBLOCK)
+ .open(path)
+ }
+
+ /// The command cannot finish until the test has seen its first chunk (it
+ /// blocks reading a FIFO that only the test writes to), so this fails
+ /// only if stdout is held back until the command ends. No timing
+ /// assumption: a slow or loaded machine only makes it slower. Nothing
+ /// here can block for good: the FIFO is opened without blocking and
+ /// retried until the command has opened it, and a command that ends
+ /// without ever reading it fails the test at once.
+ #[cfg(unix)]
#[tokio::test]
async fn streams_stdout_before_the_command_ends() {
let dir = tempfile::tempdir().unwrap();
+ let go = dir.path().join("go");
+ let made = std::process::Command::new("mkfifo")
+ .arg(&go)
+ .status()
+ .unwrap();
+ assert!(made.success(), "mkfifo failed");
let e = LumeSshExec::new(fake_lume(dir.path()), "cua-e2e-vm");
let (tx, mut rx) = mpsc::channel(16);
- let run = tokio::spawn(async move {
+ let go_path = go.display().to_string();
+ let mut run = tokio::spawn(async move {
e.exec_streaming(
- ExecRequest::sh("echo first; sleep 1; echo second")
- .timeout(Duration::from_secs(20)),
+ ExecRequest::sh("echo first; read _ < \"$GO\"; echo second")
+ .timeout(Duration::from_secs(60))
+ .env_var("GO", go_path),
tx,
)
.await
});
- let first = tokio::time::timeout(Duration::from_millis(900), rx.recv())
+ let first = tokio::time::timeout(Duration::from_secs(30), rx.recv())
.await
.expect("stdout arrives while the command still runs");
assert_eq!(first, Some(ExecChunk::Stdout(b"first\n".to_vec())));
- assert_eq!(run.await.unwrap().unwrap(), 0);
+ assert!(
+ !run.is_finished(),
+ "the command ended before it read the FIFO"
+ );
+ // Release the command once it has the FIFO open for reading.
+ let release = async {
+ loop {
+ match open_fifo_nonblocking(&go) {
+ Ok(mut fifo) => {
+ use std::io::Write;
+ break fifo.write_all(b"go\n").unwrap();
+ }
+ Err(err) if err.raw_os_error() == Some(libc::ENXIO) => {
+ assert!(
+ !run.is_finished(),
+ "the command ended without reading the FIFO"
+ );
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ }
+ Err(err) => panic!("opening the FIFO: {err}"),
+ }
+ }
+ };
+ tokio::time::timeout(Duration::from_secs(30), release)
+ .await
+ .expect("the command never opened the FIFO");
+ let code = tokio::time::timeout(Duration::from_secs(30), &mut run)
+ .await
+ .expect("the command did not finish after it was released");
+ assert_eq!(code.unwrap().unwrap(), 0);
+ assert_eq!(
+ rx.recv().await,
+ Some(ExecChunk::Stdout(b"second\n".to_vec()))
+ );
}
#[tokio::test]