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-driver): restrict Windows launch candidates to the receipt process family

injaneity0b235ad57c75
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/lib.rs b/libs/cua-driver/rust/crates/platform-windows/src/lib.rs
index b38d1633df..215270bd9c 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/lib.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/lib.rs
@@ -70,6 +70,9 @@ pub fn install_pointer_shape_backend() -> bool {
}
}
+#[cfg(any(target_os = "windows", test))]
+mod process_selection;
+
#[cfg(target_os = "windows")]
pub mod win32;
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/process_selection.rs b/libs/cua-driver/rust/crates/platform-windows/src/process_selection.rs
new file mode 100644
index 0000000000..83a315233e
--- /dev/null
+++ b/libs/cua-driver/rust/crates/platform-windows/src/process_selection.rs
@@ -0,0 +1,96 @@
+#[derive(Debug, Clone)]
+pub struct ProcessInfo {
+ pub pid: u32,
+ pub parent_pid: u32,
+ pub name: String,
+}
+
+pub(crate) fn descendants_from_processes(root_pid: u32, all: &[ProcessInfo]) -> Vec<u32> {
+ let mut result = vec![root_pid];
+ let mut frontier = vec![root_pid];
+ while let Some(parent) = frontier.pop() {
+ for p in all {
+ if p.parent_pid == parent && !result.contains(&p.pid) {
+ result.push(p.pid);
+ frontier.push(p.pid);
+ }
+ }
+ }
+ result
+}
+
+pub(crate) fn related_processes_from_processes(
+ root_pid: u32,
+ _exe_basename: &str,
+ all: &[ProcessInfo],
+) -> Vec<u32> {
+ descendants_from_processes(root_pid, all)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn process(pid: u32, parent_pid: u32, name: &str) -> ProcessInfo {
+ ProcessInfo {
+ pid,
+ parent_pid,
+ name: name.into(),
+ }
+ }
+
+ #[test]
+ fn descendants_include_root_and_only_its_transitive_process_tree() {
+ let processes = [
+ process(42, 1, "process-42.exe"),
+ process(43, 42, "process-43.exe"),
+ process(44, 43, "process-44.exe"),
+ process(45, 42, "process-45.exe"),
+ process(99, 1, "process-99.exe"),
+ process(100, 99, "process-100.exe"),
+ ];
+ assert_eq!(
+ descendants_from_processes(42, &processes),
+ vec![42, 43, 45, 44]
+ );
+ }
+
+ #[test]
+ fn launch_candidates_exclude_unrelated_same_name_processes() {
+ let processes = [
+ process(42, 1, "maya.exe"),
+ process(99, 1, "maya.exe"),
+ process(100, 1, "maya.exe"),
+ ];
+ assert_eq!(
+ related_processes_from_processes(42, "maya.exe", &processes),
+ vec![42]
+ );
+ }
+
+ #[test]
+ fn launch_candidates_include_renamed_descendants() {
+ let processes = [
+ process(42, 1, "maya.exe"),
+ process(43, 42, "maya-helper.exe"),
+ process(44, 43, "python.exe"),
+ ];
+ assert_eq!(
+ related_processes_from_processes(42, "maya.exe", &processes),
+ vec![42, 43, 44]
+ );
+ }
+
+ #[test]
+ fn launch_candidates_keep_exited_root_first() {
+ let processes = [process(43, 42, "python.exe")];
+ assert_eq!(
+ related_processes_from_processes(42, "maya.exe", &processes),
+ vec![42, 43]
+ );
+ assert_eq!(
+ related_processes_from_processes(42, "maya.exe", &[]),
+ vec![42]
+ );
+ }
+}
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/win32/apps.rs b/libs/cua-driver/rust/crates/platform-windows/src/win32/apps.rs
index 3dc95dfc18..fdc0218b6c 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/win32/apps.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/win32/apps.rs
@@ -8,12 +8,8 @@ use windows::Win32::System::Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W, TH32CS_SNAPPROCESS,
};
-#[derive(Debug, Clone)]
-pub struct ProcessInfo {
- pub pid: u32,
- pub parent_pid: u32,
- pub name: String,
-}
+pub use crate::process_selection::ProcessInfo;
+use crate::process_selection::{descendants_from_processes, related_processes_from_processes};
/// Return all running processes (pid, parent_pid, executable name).
pub fn list_processes() -> Vec<ProcessInfo> {
@@ -67,107 +63,8 @@ pub fn list_descendants(root_pid: u32) -> Vec<u32> {
descendants_from_processes(root_pid, &all)
}
-fn descendants_from_processes(root_pid: u32, all: &[ProcessInfo]) -> Vec<u32> {
- let mut result = vec![root_pid];
- let mut frontier = vec![root_pid];
- while let Some(parent) = frontier.pop() {
- for p in all {
- if p.parent_pid == parent && !result.contains(&p.pid) {
- result.push(p.pid);
- frontier.push(p.pid);
- }
- }
- }
- result
-}
-
-/// Like `list_descendants` but ALSO returns processes whose executable name
-/// matches a prefix derived from `exe_basename`. This catches the LibreOffice
-/// pattern (swriter.exe spawns soffice.bin via a parent-pid relationship we
-/// might miss if the spawn happened before our pre-launch snapshot, OR via
-/// CreateProcess flags that detach the child from the launcher's tree) as
-/// well as the GIMP pattern (gimp-3.exe spawns gimp-3.2.exe whose name starts
-/// with the same prefix).
-///
-/// Heuristic: strip extension and trailing version digits/dots/dashes from
-/// the basename to derive a stable prefix. E.g.:
-/// `gimp-3.exe` → prefix `gimp`
-/// `gimp-3.2.exe` → prefix `gimp`
-/// `swriter.exe` → prefix `swriter` (won't match `soffice.bin` — that's
-/// LibreOffice's parent-pid path; usually still reachable
-/// via `list_descendants`)
-/// `notepad++.exe` → prefix `notepad++` (no version stripping needed)
-///
-/// Returns deduplicated pids; ordering favors descendants over name-matches.
-pub fn related_processes(root_pid: u32, exe_basename: &str) -> Vec<u32> {
- let mut out = list_descendants(root_pid);
- let prefix = strip_version_suffix(exe_basename);
- if !prefix.is_empty() {
- let all = list_processes();
- for p in &all {
- let p_prefix = strip_version_suffix(&p.name);
- if p_prefix.eq_ignore_ascii_case(&prefix) && !out.contains(&p.pid) {
- out.push(p.pid);
- }
- }
- }
- out
-}
-
-/// Strip `.exe` (case-insensitive) and any trailing `-<digits>.<digits>...`
-/// or `<digits>.<digits>...` version suffix. Used by `related_processes` to
-/// match `gimp-3.exe` and `gimp-3.2.exe` under the common prefix `gimp`.
-fn strip_version_suffix(basename: &str) -> String {
- let mut s = basename.to_ascii_lowercase();
- if let Some(stripped) = s.strip_suffix(".exe") {
- s = stripped.to_owned();
- }
- // Strip trailing version-like tail: `-3`, `-3.2`, `3`, `3.2`, etc.
- let bytes = s.as_bytes();
- let mut cut = bytes.len();
- while cut > 0 {
- let c = bytes[cut - 1] as char;
- if c.is_ascii_digit() || c == '.' || c == '-' {
- cut -= 1;
- } else {
- break;
- }
- }
- // Avoid stripping an entire name (e.g. "7z" → "" would lose information).
- // If everything past cut is purely digits/dots/dashes AND cut > 0, accept.
- if cut == 0 {
- return s;
- }
- s[..cut].to_string()
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- fn process(pid: u32, parent_pid: u32) -> ProcessInfo {
- ProcessInfo {
- pid,
- parent_pid,
- name: format!("process-{pid}.exe"),
- }
- }
-
- #[test]
- fn descendants_include_root_and_only_its_transitive_process_tree() {
- let processes = vec![
- process(42, 1),
- process(43, 42),
- process(44, 43),
- process(45, 42),
- process(99, 1),
- process(100, 99),
- ];
-
- let descendants = descendants_from_processes(42, &processes);
-
- assert_eq!(descendants, vec![42, 43, 45, 44]);
- assert!(!descendants.contains(&99));
- assert!(!descendants.contains(&100));
- }
+/// Return the launch receipt root and its transitive descendants, root first.
+/// Executable-name similarity does not establish launch ancestry.
+pub fn related_processes(root_pid: u32, _exe_basename: &str) -> Vec<u32> {
+ related_processes_from_processes(root_pid, _exe_basename, &list_processes())
}