Compare commits

..

2 Commits

Author SHA1 Message Date
javis-bot
2a57b2b2f4 chore(app): version 0.1.40
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 23:32:37 +09:00
javis-bot
ba44cc224c fix(app): PC control as the real agent-desktop behaves on Windows
Checked against agent-desktop 1.2.0 on the Windows test VM (normal and
elevated sessions); each fault has a test:

- the state folder is made private to the current user: a data folder
  outside the user profile inherits Authenticated Users: Modify and
  agent-desktop then refused every reading (INTERNAL), which broke all
  of PC control on such PCs
- run with the program already open uses the window agent-desktop names
  instead of waiting for a new one (failed after 5.3 s, now 0.6 s);
  several copies get their own reason
- keys to a window Windows will not bring to the front says so
  (NOT_FRONT) and sends nothing
- close that a save dialog holds up, or several copies, get their own
  reasons instead of a generic failure
- an action that closes its own window answers done with no elements
- fields a reading showed as secure are refused as password fields
  before anything is sent

Verified on the VM: calculator 1 2 + 3 = read back 15, Notepad type,
ctrl+a and a letter, elevated windows refused from a normal session and
normal ones not, the emergency stop killing a running command and
blocking acting while reading works.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 23:32:37 +09:00
6 changed files with 230 additions and 27 deletions

2
app/Cargo.lock generated
View File

@@ -1520,7 +1520,7 @@ checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4"
[[package]] [[package]]
name = "jarvis-app" name = "jarvis-app"
version = "0.1.39" version = "0.1.40"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"base64 0.22.1", "base64 0.22.1",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "jarvis-app" name = "jarvis-app"
version = "0.1.39" version = "0.1.40"
edition = "2021" edition = "2021"
rust-version = "1.85" rust-version = "1.85"
description = "Jarvis v2 Windows app core: Discord voice, voice worker supervisor, chat channels" description = "Jarvis v2 Windows app core: Discord voice, voice worker supervisor, chat channels"

4
app/shell/Cargo.lock generated
View File

@@ -2637,7 +2637,7 @@ checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4"
[[package]] [[package]]
name = "jarvis-app" name = "jarvis-app"
version = "0.1.39" version = "0.1.40"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"base64 0.22.1", "base64 0.22.1",
@@ -2666,7 +2666,7 @@ dependencies = [
[[package]] [[package]]
name = "jarvis-shell" name = "jarvis-shell"
version = "0.1.39" version = "0.1.40"
dependencies = [ dependencies = [
"jarvis-app", "jarvis-app",
"serde_json", "serde_json",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "jarvis-shell" name = "jarvis-shell"
version = "0.1.39" version = "0.1.40"
edition = "2021" edition = "2021"
rust-version = "1.85" rust-version = "1.85"
description = "Jarvis v2 management app window (Tauri 2 over the app core's AdminApi)" description = "Jarvis v2 management app window (Tauri 2 over the app core's AdminApi)"

View File

@@ -226,16 +226,23 @@ agent-desktop and decides who may.
next to `jarvis.exe` (`agent-desktop.exe`, about 4 MB) and checked before every start of the next to `jarvis.exe` (`agent-desktop.exe`, about 4 MB) and checked before every start of the
services (a wrong hash means not available). One CLI run per command (M0 `desktop`: 0.10 s, as services (a wrong hash means not available). One CLI run per command (M0 `desktop`: 0.10 s, as
fast as its MCP server), its APPDATA, LOCALAPPDATA and state folder (`AGENT_DESKTOP_HOME`, where fast as its MCP server), its APPDATA, LOCALAPPDATA and state folder (`AGENT_DESKTOP_HOME`, where
it keeps its readings) in the data folder (`runtime/agent-desktop`), a 15 s limit per command. A it keeps its readings) in the data folder (`runtime/agent-desktop`, made afresh at each start and
private to the person: agent-desktop refuses a state folder others may write to, which a folder
outside the user's profile is by inheritance), a 15 s limit per command. A
program it starts gets the person's own APPDATA and LOCALAPPDATA back and starts in their user program it starts gets the person's own APPDATA and LOCALAPPDATA back and starts in their user
folder. Actions are headless (no cursor, no focus), except `keys`, which brings the window to the folder. Actions are headless (no cursor, no focus), except `keys`, which brings the window to the
front first (`focus-window`, then `press`). `--force` (which lifts its block on Alt+F4, Win+R, front first (`focus-window`, then `press`); when Windows does not let it come forward (its
Ctrl+Shift+Esc and the like) and the raw mouse and key commands are never used; what the brain foreground lock), no key is sent ("그 창을 앞으로 가져오지 못해 키를 보내지 않았습니다"). `--force`
sends is checked so nothing in it reads as an option, and typed text goes in through stdin (which lifts its block on Alt+F4, Win+R, Ctrl+Shift+Esc and the like) and the raw mouse and key
(`set-value --stdin`), never in the arguments. The commands: `list-windows`, `snapshot commands are never used; what the brain sends is checked so nothing in it reads as an option,
and typed text goes in through stdin (`set-value --stdin`), never in the arguments. The commands: `list-windows`, `snapshot
--window-id` (`read`), `click`, `get --property states` then `set-value` (`type`), `focus-window` --window-id` (`read`), `click`, `get --property states` then `set-value` (`type`), `focus-window`
then `press` (`keys`), `launch` (`run`), `close-app` (`close`: agent-desktop has no command for then `press` (`keys`), `launch` (`run`), `close-app` (`close`: agent-desktop has no command for
one window, so the program closes gracefully with all its windows), `restore`. one window, so the program closes gracefully with all its windows; one still running when
agent-desktop gives up, a save question say, is "프로그램이 아직 닫히지 않았습니다(저장할지 묻는 창이
떠 있을 수 있습니다). 창을 다시 읽어 주세요", and with several of it running close-app cannot
choose: "그 프로그램이 여러 개 실행 중이라 이 창만 닫을 수 없습니다. 창의 닫기 단추를 눌러 주세요"),
`restore`.
- **Windows**: as `list-windows` gives them, without this app's own windows, windows without a - **Windows**: as `list-windows` gives them, without this app's own windows, windows without a
title and the programs of `pcControl.screenExclude` (never listed, read or acted on, as for title and the programs of `pcControl.screenExclude` (never listed, read or acted on, as for
screen viewing); at most 30. `window` is a window id or part of a title or program name (the screen viewing); at most 30. `window` is a window id or part of a title or program name (the
@@ -243,7 +250,10 @@ agent-desktop and decides who may.
it was read from; one from a reading this run did not make gets `STALE_REF`'s reason. `run` takes it was read from; one from a reading this run did not make gets `STALE_REF`'s reason. `run` takes
a program name only (no folders): one in Windows' folders as it is, else the file Windows' App a program name only (no folders): one in Windows' folders as it is, else the file Windows' App
Paths or a Start menu shortcut without arguments points at; the window is the one `launch` names, Paths or a Start menu shortcut without arguments points at; the window is the one `launch` names,
else a new one of its process or name, else the only new one, waited for up to 5 s. else a new one of its process or name, else the only new one, waited for up to 5 s. A program
already running is not started again: `launch` attaches to it and names its window; with several
running it cannot choose ("그 프로그램이 이미 여러 개 실행 중이라 새로 열지 못했습니다. 열린 창 중에서
골라 주세요").
- **Available** only on Windows with `features.pcControl` on, someone in `pcControl.allowedUsers` - **Available** only on Windows with `features.pcControl` on, someone in `pcControl.allowedUsers`
and agent-desktop present; otherwise `pcControl` is in the brain's `disabled_tools`. Settings and agent-desktop present; otherwise `pcControl` is in the brain's `disabled_tools`. Settings
apply on the next start. apply on the next start.
@@ -253,8 +263,8 @@ agent-desktop and decides who may.
사람만 PC를 조작할 수 있습니다"), a kind at `never` ("PC 제어 화면에서 이 동작이 꺼져 있습니다"). 사람만 PC를 조작할 수 있습니다"), a kind at `never` ("PC 제어 화면에서 이 동작이 꺼져 있습니다").
At `ask` the action is done only with `consent: true`, else the answer is `ask: true` with the At `ask` the action is done only with `consent: true`, else the answer is `ask: true` with the
kinds and the window (the brain asks the person; the app does not keep consent). kinds and the window (the brain asks the person; the app does not keep consent).
- **Never**: typing into a password field (agent-desktop reports it, or Windows UI Automation's - **Never**: typing into a password field (agent-desktop reports it as `secure`: in the reading the
IsPassword) is refused ("비밀번호 칸에는 입력하지 않습니다"); a window run as administrator is element came from, anywhere in its tree, or by `get` now) is refused ("비밀번호 칸에는 입력하지 않습니다"); a window run as administrator is
refused before acting ("관리자 권한으로 실행된 창은 조작할 수 없습니다"), and `run` never asks for refused before acting ("관리자 권한으로 실행된 창은 조작할 수 없습니다"), and `run` never asks for
administrator rights. administrator rights.
- **Anti-cheat**: while a game with an anti-cheat runs, every action but `windows` and `read` is - **Anti-cheat**: while a game with an anti-cheat runs, every action but `windows` and `read` is
@@ -275,7 +285,8 @@ agent-desktop and decides who may.
holds it) is a warning in the log. A reading started while the stop is on runs to its end. holds it) is a warning in the log. A reading started while the stop is on runs to its end.
- **Results**: `read` and every action answer with the window `{id, program, title}` and its - **Results**: `read` and every action answer with the window `{id, program, title}` and its
interactive elements read afresh after the action (at most 200), so the brain says it was done interactive elements read afresh after the action (at most 200), so the brain says it was done
only from what it sees. agent-desktop's error codes come back as Korean reasons (`STALE_REF`: "화면이 only from what it sees; an action that closes its window (a dialog's button, Enter) answers the
window with no elements. agent-desktop's error codes come back as Korean reasons (`STALE_REF`: "화면이
바뀌었습니다. 다시 읽어 주세요"; a minimised window is restored before acting). 바뀌었습니다. 다시 읽어 주세요"; a minimised window is restored before acting).
- **Refused before asking**: the stop, the anti-cheat, an administrator window (a process whose - **Refused before asking**: the stop, the anti-cheat, an administrator window (a process whose
elevation cannot be read counts as one) and a password field are refused without asking. elevation cannot be read counts as one) and a password field are refused without asking.

View File

@@ -51,6 +51,10 @@ pub const ELEVATED: &str = "관리자 권한으로 실행된 창은 조작할
pub const STOPPED: &str = "비상 정지 중입니다. 단축키를 한 번 더 누르거나 PC 제어 화면에서 풀어 주세요"; pub const STOPPED: &str = "비상 정지 중입니다. 단축키를 한 번 더 누르거나 PC 제어 화면에서 풀어 주세요";
pub const STALE: &str = "화면이 바뀌었습니다. 다시 읽어 주세요"; pub const STALE: &str = "화면이 바뀌었습니다. 다시 읽어 주세요";
const NO_WINDOW: &str = "그 창을 찾지 못했습니다"; const NO_WINDOW: &str = "그 창을 찾지 못했습니다";
const NOT_FRONT: &str = "그 창을 앞으로 가져오지 못해 키를 보내지 않았습니다";
const NOT_CLOSED: &str = "프로그램이 아직 닫히지 않았습니다(저장할지 묻는 창이 떠 있을 수 있습니다). 창을 다시 읽어 주세요";
const CLOSE_SEVERAL: &str = "그 프로그램이 여러 개 실행 중이라 이 창만 닫을 수 없습니다. 창의 닫기 단추를 눌러 주세요";
const RUNNING_SEVERAL: &str = "그 프로그램이 이미 여러 개 실행 중이라 새로 열지 못했습니다. 열린 창 중에서 골라 주세요";
/// Anti-cheats found by their processes (file names, lower case): the anti-cheat's name, and the /// Anti-cheats found by their processes (file names, lower case): the anti-cheat's name, and the
/// game's when the process is the game itself. Vanguard's driver is always loaded, so it is found /// game's when the process is the game itself. Vanguard's driver is always loaded, so it is found
@@ -221,7 +225,16 @@ pub struct Cli {
impl Cli { impl Cli {
pub fn new(exe: &Path, data: &Path) -> Self { pub fn new(exe: &Path, data: &Path) -> Self {
let home = data.join("runtime").join("agent-desktop"); let home = data.join("runtime").join("agent-desktop");
// agent-desktop refuses a state folder others may write to ("private file DACL grants an
// untrusted principal": a data folder outside the user's profile inherits Authenticated
// Users' Modify), so it is made afresh, private to this user; a run's readings are of no
// use to the next.
let _ = std::fs::remove_dir_all(&home);
let _ = std::fs::create_dir_all(&home); let _ = std::fs::create_dir_all(&home);
#[cfg(windows)]
if !win::make_private(&home) {
crate::warn_log!("conversation", "Warning: agent-desktop's state folder could not be made private: PC control may fail");
}
Cli { exe: exe.to_path_buf(), home, limit: COMMAND_LIMIT } Cli { exe: exe.to_path_buf(), home, limit: COMMAND_LIMIT }
} }
} }
@@ -389,6 +402,15 @@ fn refused(reason: &str) -> Fail {
/// agent-desktop's error code as the reason the person hears. /// agent-desktop's error code as the reason the person hears.
fn failed(code: &str, action: &str) -> Fail { fn failed(code: &str, action: &str) -> Fail {
let reason = match code { let reason = match code {
// Windows' foreground lock: focus-window could not bring the window forward, or press
// could not confirm its focus; no key was sent.
"ACTION_FAILED" | "STALE_REF" if action == "keys" => NOT_FRONT,
// close-app gives up while the program still runs (a save question, say).
"TIMEOUT" | "APP_TIMEOUT" if action == "close" => NOT_CLOSED,
// close-app picks the program by name; with several running it cannot choose.
"AMBIGUOUS_TARGET" if action == "close" => CLOSE_SEVERAL,
// launch attaches to one running instance; with several it cannot choose.
"AMBIGUOUS_TARGET" if action == "run" => RUNNING_SEVERAL,
"STALE_REF" | "SNAPSHOT_NOT_FOUND" | "ELEMENT_NOT_FOUND" => STALE, "STALE_REF" | "SNAPSHOT_NOT_FOUND" | "ELEMENT_NOT_FOUND" => STALE,
"AMBIGUOUS_TARGET" => "같은 모양의 항목이 여럿이라 고를 수 없습니다. 다시 읽어 주세요", "AMBIGUOUS_TARGET" => "같은 모양의 항목이 여럿이라 고를 수 없습니다. 다시 읽어 주세요",
"APP_NOT_FOUND" if action == "run" => "그 프로그램을 찾지 못했습니다", "APP_NOT_FOUND" if action == "run" => "그 프로그램을 찾지 못했습니다",
@@ -482,6 +504,22 @@ fn elements(tree: &Value) -> Vec<Value> {
})).collect() })).collect()
} }
/// The refs of a reading's fields agent-desktop reports as secure (password fields), in the whole
/// tree, not only the elements shown.
fn secure_refs(tree: &Value) -> HashSet<String> {
fn walk(n: &Value, out: &mut HashSet<String>) {
for c in n["children"].as_array().into_iter().flatten() {
if let Some(r) = c["ref_id"].as_str().filter(|_| c["states"].as_array().is_some_and(|s| s.iter().any(|x| x == "secure"))) {
out.insert(r.to_owned());
}
walk(c, out);
}
}
let mut out = HashSet::new();
walk(tree, &mut out);
out
}
fn args(a: &[&str]) -> Vec<String> { fn args(a: &[&str]) -> Vec<String> {
a.iter().map(|s| s.to_string()).collect() a.iter().map(|s| s.to_string()).collect()
} }
@@ -495,8 +533,9 @@ pub struct Pc {
game: Box<dyn Fn() -> Option<String> + Send + Sync>, game: Box<dyn Fn() -> Option<String> + Send + Sync>,
/// This app's process: its windows are never listed or acted on. /// This app's process: its windows are never listed or acted on.
own_pid: u64, own_pid: u64,
/// The latest readings' windows by snapshot id, so an element names its window. /// The latest readings by snapshot id: the window, so an element names its window, and the
readings: Mutex<VecDeque<(String, Win)>>, /// fields read as secure (password fields; agent-desktop cannot address them again by `get`).
readings: Mutex<VecDeque<(String, Win, HashSet<String>)>>,
/// One action at a time. /// One action at a time.
one: Mutex<()>, one: Mutex<()>,
} }
@@ -587,6 +626,10 @@ impl Pc {
if text.chars().count() > MAX_TEXT { if text.chars().count() > MAX_TEXT {
return Err(refused("글자가 너무 깁니다 (2,000자까지)")); return Err(refused("글자가 너무 깁니다 (2,000자까지)"));
} }
let read_secure = self.readings.lock().unwrap().iter().any(|(_, _, s)| s.contains(element.unwrap()));
if read_secure {
return Err(refused(PASSWORD));
}
let states = self.cmd(&args(&["get", element.unwrap(), "--property", "states"]), None, "type")?; let states = self.cmd(&args(&["get", element.unwrap(), "--property", "states"]), None, "type")?;
let secure = states["value"].as_array().is_none_or(|s| s.iter().any(|x| x == "secure")); let secure = states["value"].as_array().is_none_or(|s| s.iter().any(|x| x == "secure"));
if secure { if secure {
@@ -619,7 +662,11 @@ impl Pc {
return Ok(Done::Item(json!({"window": w.view(), "elements": []}))); return Ok(Done::Item(json!({"window": w.view(), "elements": []})));
} }
} }
self.read(&w).map(Done::Item) match self.read(&w) {
// The action closed its window (a dialog's button, Enter): done, nothing left to read.
Err(f) if f.code == "WINDOW_NOT_FOUND" => Ok(Done::Item(json!({"window": w.view(), "elements": []}))),
r => r.map(Done::Item),
}
} }
fn cmd(&self, args: &[String], stdin: Option<&str>, action: &str) -> Result<Value, Fail> { fn cmd(&self, args: &[String], stdin: Option<&str>, action: &str) -> Result<Value, Fail> {
@@ -659,7 +706,7 @@ impl Pc {
/// The window an element was read from, as it is listed now. /// The window an element was read from, as it is listed now.
fn window_of(&self, element: &str) -> Result<Win, Fail> { fn window_of(&self, element: &str) -> Result<Win, Fail> {
let snap = element.trim_start_matches('@').split(':').next().unwrap_or(""); let snap = element.trim_start_matches('@').split(':').next().unwrap_or("");
let id = self.readings.lock().unwrap().iter().find(|(s, _)| s == snap).map(|(_, w)| w.id.clone()) let id = self.readings.lock().unwrap().iter().find(|(s, _, _)| s == snap).map(|(_, w, _)| w.id.clone())
.ok_or_else(|| refused(STALE))?; .ok_or_else(|| refused(STALE))?;
self.list()?.into_iter().find(|w| w.id == id).ok_or_else(|| refused(STALE)) self.list()?.into_iter().find(|w| w.id == id).ok_or_else(|| refused(STALE))
} }
@@ -673,7 +720,7 @@ impl Pc {
} }
if let Some(snap) = data["snapshot_id"].as_str() { if let Some(snap) = data["snapshot_id"].as_str() {
let mut r = self.readings.lock().unwrap(); let mut r = self.readings.lock().unwrap();
r.push_back((snap.to_owned(), w.clone())); r.push_back((snap.to_owned(), w.clone(), secure_refs(&data["tree"])));
while r.len() > KEPT_READINGS { while r.len() > KEPT_READINGS {
r.pop_front(); r.pop_front();
} }
@@ -696,11 +743,14 @@ impl Pc {
let lower = name.to_lowercase(); let lower = name.to_lowercase();
let started = Instant::now(); let started = Instant::now();
loop { loop {
let new: Vec<Win> = self.list()?.into_iter().filter(|w| !before.contains(&w.id)).collect(); let list = self.list()?;
let found = new.iter().find(|w| Some(&w.id) == id.as_ref()) // A program already running is not started again: launch names its window.
.or_else(|| new.iter().find(|w| pid.is_some_and(|p| p == w.pid))) let named = list.iter().find(|w| Some(&w.id) == id.as_ref());
.or_else(|| new.iter().find(|w| stem(&w.program).contains(&lower) || w.title.to_lowercase().contains(&lower))) let new: Vec<&Win> = list.iter().filter(|w| !before.contains(&w.id)).collect();
.or(if new.len() == 1 { new.first() } else { None }); let found = named
.or_else(|| new.iter().copied().find(|w| pid.is_some_and(|p| p == w.pid)))
.or_else(|| new.iter().copied().find(|w| stem(&w.program).contains(&lower) || w.title.to_lowercase().contains(&lower)))
.or(if new.len() == 1 { new.first().copied() } else { None });
if let Some(w) = found { if let Some(w) = found {
return self.read(w); return self.read(w);
} }
@@ -842,6 +892,48 @@ mod win {
} }
} }
/// Make `dir` private to this user: a protected DACL with one entry, full control for the user,
/// inherited by what is made inside.
pub fn make_private(dir: &std::path::Path) -> bool {
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::Security::{
AddAccessAllowedAceEx, GetLengthSid, InitializeAcl, InitializeSecurityDescriptor, SetFileSecurityW,
SetSecurityDescriptorControl, SetSecurityDescriptorDacl, TokenUser, ACCESS_ALLOWED_ACE, ACL, ACL_REVISION,
CONTAINER_INHERIT_ACE, DACL_SECURITY_INFORMATION, OBJECT_INHERIT_ACE, PROTECTED_DACL_SECURITY_INFORMATION,
SECURITY_DESCRIPTOR, SE_DACL_PROTECTED, TOKEN_USER,
};
use windows_sys::Win32::System::Threading::GetCurrentProcess;
const FILE_ALL_ACCESS: u32 = 0x001F_01FF;
// SAFETY: this process's token queried into a sized, aligned buffer and closed; the ACL and
// security descriptor live in buffers that outlive the call that uses them.
unsafe {
let mut token: HANDLE = std::ptr::null_mut();
if OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) == 0 {
return false;
}
let mut user = [0u64; 64];
let mut len = 0u32;
let ok = GetTokenInformation(token, TokenUser, user.as_mut_ptr().cast(), std::mem::size_of_val(&user) as u32, &mut len) != 0;
CloseHandle(token);
if !ok {
return false;
}
let sid = (*(user.as_ptr() as *const TOKEN_USER)).User.Sid;
let acl_len = std::mem::size_of::<ACL>() + std::mem::size_of::<ACCESS_ALLOWED_ACE>() + GetLengthSid(sid) as usize;
let mut acl = vec![0u32; acl_len.div_ceil(4)];
let acl = acl.as_mut_ptr() as *mut ACL;
let mut sd: SECURITY_DESCRIPTOR = std::mem::zeroed();
let psd = (&mut sd as *mut SECURITY_DESCRIPTOR).cast();
let path: Vec<u16> = dir.as_os_str().encode_wide().chain([0]).collect();
InitializeAcl(acl, (acl_len.div_ceil(4) * 4) as u32, ACL_REVISION) != 0
&& AddAccessAllowedAceEx(acl, ACL_REVISION, OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE, FILE_ALL_ACCESS, sid) != 0
&& InitializeSecurityDescriptor(psd, 1) != 0
&& SetSecurityDescriptorDacl(psd, 1, acl, 0) != 0
&& SetSecurityDescriptorControl(psd, SE_DACL_PROTECTED, SE_DACL_PROTECTED) != 0
&& SetFileSecurityW(path.as_ptr(), DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION, psd) != 0
}
}
/// Every running process's file name, lower case (also of processes this user may not open, /// Every running process's file name, lower case (also of processes this user may not open,
/// such as anti-cheat services). /// such as anti-cheat services).
pub fn processes() -> Vec<String> { pub fn processes() -> Vec<String> {
@@ -990,6 +1082,9 @@ mod tests {
fail: Option<(&'static str, &'static str)>, fail: Option<(&'static str, &'static str)>,
calls: Mutex<Vec<(Vec<String>, Option<String>)>>, calls: Mutex<Vec<(Vec<String>, Option<String>)>>,
launched: Value, launched: Value,
/// A password field in every reading, past the elements shown; `get` on it is refused as
/// the real agent-desktop refuses it (a secure field's identity is not unique).
secure_ref: Option<&'static str>,
} }
impl Fake { impl Fake {
@@ -999,7 +1094,7 @@ mod tests {
{"id": "w-2", "title": "Vault", "app_name": "KeePassXC.exe", "pid": 11, "is_focused": true, "accessible": true}, {"id": "w-2", "title": "Vault", "app_name": "KeePassXC.exe", "pid": 11, "is_focused": true, "accessible": true},
{"id": "w-3", "title": "Calc", "app_name": "CalculatorApp.exe", "pid": 12, "is_focused": false, "minimized": true}])])), {"id": "w-3", "title": "Calc", "app_name": "CalculatorApp.exe", "pid": 12, "is_focused": false, "minimized": true}])])),
processes: vec!["explorer.exe".into()], elevated: false, secure: false, fail: None, processes: vec!["explorer.exe".into()], elevated: false, secure: false, fail: None,
calls: Mutex::new(vec![]), launched: json!({"app": "notepad", "pid": 10})} calls: Mutex::new(vec![]), launched: json!({"app": "notepad", "pid": 10}), secure_ref: None}
} }
fn commands(&self) -> Vec<String> { fn commands(&self) -> Vec<String> {
self.calls.lock().unwrap().iter().map(|(a, _)| a[0].clone()).collect() self.calls.lock().unwrap().iter().map(|(a, _)| a[0].clone()).collect()
@@ -1021,12 +1116,22 @@ mod tests {
return Err(code.into()); return Err(code.into());
} }
} }
if args[0] == "get" && self.secure_ref == Some(args[1].as_str()) {
return Err("AMBIGUOUS_TARGET".into());
}
Ok(match args[0].as_str() { Ok(match args[0].as_str() {
"list-windows" => { "list-windows" => {
let mut w = self.windows.lock().unwrap(); let mut w = self.windows.lock().unwrap();
if w.len() > 1 { w.pop_front().unwrap() } else { w[0].clone() } if w.len() > 1 { w.pop_front().unwrap() } else { w[0].clone() }
} }
"snapshot" => json!({"snapshot_id": "s1", "window": {"id": args[2], "title": TITLE}, "tree": tree()}), "snapshot" => {
let mut t = tree();
if let Some(r) = self.secure_ref {
t["children"][0]["children"].as_array_mut().unwrap()
.push(json!({"role": "group", "children": [{"role": "textfield", "ref_id": r, "states": ["secure"]}]}));
}
json!({"snapshot_id": "s1", "window": {"id": args[2], "title": TITLE}, "tree": t})
}
"get" => json!({"property": "states", "value": if self.secure { json!(["secure"]) } else { json!([]) }}), "get" => json!({"property": "states", "value": if self.secure { json!(["secure"]) } else { json!([]) }}),
"launch" => self.launched.clone(), "launch" => self.launched.clone(),
_ => json!({"disposition": {"delivery": "delivered_unverified"}}), _ => json!({"disposition": {"delivery": "delivered_unverified"}}),
@@ -1219,6 +1324,19 @@ mod tests {
assert!(fake.commands().iter().all(|c| c == "list-windows" || c == "snapshot")); assert!(fake.commands().iter().all(|c| c == "list-windows" || c == "snapshot"));
} }
#[test]
fn a_field_read_as_secure_is_refused_as_a_password_field() {
let mut s = settings();
s.pc_control.type_text = Level::Auto;
let (pc, fake, _) = pc_with(s, Fake { secure_ref: Some("@s1:e900"), ..Fake::new() });
read_first(&pc);
let o = pc.act(&action("type", OWNER, json!({"ref": "@s1:e900", "text": TYPED, "consent": true})));
assert_eq!(o.reply, json!({"ok": false, "error": PASSWORD}));
assert!(!fake.commands().contains(&"set-value".to_owned()));
// Other fields of the same reading are typed into.
assert_eq!(pc.act(&action("type", OWNER, json!({"ref": "@s1:e3", "text": TYPED}))).code, "ok");
}
#[test] #[test]
fn a_reading_has_interactive_elements_first_at_most_200_and_cut_to_120_characters() { fn a_reading_has_interactive_elements_first_at_most_200_and_cut_to_120_characters() {
let (pc, _, _) = pc_with(settings(), Fake::new()); let (pc, _, _) = pc_with(settings(), Fake::new());
@@ -1300,6 +1418,66 @@ mod tests {
} }
} }
#[test]
fn run_with_the_program_already_running_answers_the_window_launch_names() {
let mut s = settings();
s.pc_control.run = Level::Auto;
// agent-desktop attaches to a running instance and names its window; nothing new appears.
let fake = Fake { launched: json!({"app": "notepad.exe", "pid": 10, "window": {"id": "w-1", "title": TITLE}}), ..Fake::new() };
let (pc, _, _) = pc_with(s.clone(), fake);
let started = Instant::now();
let o = pc.act(&action("run", OWNER, json!({"program": "notepad"})));
assert_eq!(o.reply["item"]["window"]["id"], "w-1", "{o:?}");
assert!(started.elapsed() < WINDOW_WAIT, "answered without waiting for a new window");
// Several running: launch cannot choose, and the answer says so (not an element's reason).
let fake = Fake { fail: Some(("launch", "AMBIGUOUS_TARGET")), ..Fake::new() };
let (pc, _, _) = pc_with(s, fake);
let o = pc.act(&action("run", OWNER, json!({"program": "notepad"})));
assert_eq!(o.reply, json!({"ok": false, "error": RUNNING_SEVERAL}));
}
#[test]
fn keys_to_a_window_that_cannot_come_to_the_front_say_nothing_was_sent() {
let mut s = settings();
s.pc_control.type_text = Level::Auto;
// Windows' foreground lock: focus-window fails, or press cannot confirm the window's focus.
for (cmd, code) in [("focus-window", "ACTION_FAILED"), ("press", "STALE_REF")] {
let fake = Fake { fail: Some((cmd, code)), ..Fake::new() };
let (pc, fake, _) = pc_with(s.clone(), fake);
let o = pc.act(&action("keys", OWNER, json!({"window": "notepad", "keys": "ctrl+s"})));
assert_eq!(o.reply, json!({"ok": false, "error": NOT_FRONT}), "{cmd}");
assert!(cmd == "press" || !fake.commands().contains(&"press".to_owned()), "no key after a failed focus");
}
}
#[test]
fn an_action_that_closes_its_window_is_done_with_nothing_left_to_read() {
let mut s = settings();
s.pc_control.type_text = Level::Auto;
// Enter on a dialog (or a click on its button) closes it: the fresh reading finds no window.
let (pc, _, _) = pc_with(s, Fake { fail: Some(("snapshot", "WINDOW_NOT_FOUND")), ..Fake::new() });
let o = pc.act(&action("keys", OWNER, json!({"window": "notepad", "keys": "enter"})));
assert_eq!(o.reply, json!({"ok": true, "item": {"window": {"id": "w-1", "program": "notepad", "title": TITLE}, "elements": []}}));
assert_eq!(o.code, "ok");
}
#[test]
fn a_program_that_does_not_close_in_time_is_said_to_be_still_open() {
let mut s = settings();
s.pc_control.click = Level::Auto;
// A save question keeps the program open; close-app gives up after its own deadline.
let fake = Fake { fail: Some(("close-app", "TIMEOUT")), ..Fake::new() };
let (pc, _, _) = pc_with(s, fake);
let o = pc.act(&action("close", OWNER, json!({"window": "notepad"})));
assert_eq!(o.reply, json!({"ok": false, "error": NOT_CLOSED}));
// Several of it running: close-app picks by name and cannot choose.
let mut s = settings();
s.pc_control.click = Level::Auto;
let (pc, _, _) = pc_with(s, Fake { fail: Some(("close-app", "AMBIGUOUS_TARGET")), ..Fake::new() });
let o = pc.act(&action("close", OWNER, json!({"window": "notepad"})));
assert_eq!(o.reply, json!({"ok": false, "error": CLOSE_SEVERAL}));
}
#[test] #[test]
fn the_activity_names_the_program_only() { fn the_activity_names_the_program_only() {
let mut s = settings(); let mut s = settings();
@@ -1352,6 +1530,20 @@ mod tests {
assert_eq!(envelope(b"garbage"), Err("APP_NO_JSON".into())); assert_eq!(envelope(b"garbage"), Err("APP_NO_JSON".into()));
} }
#[test]
fn agent_desktops_state_folder_is_made_afresh_each_start() {
// What a folder made under another ACL holds would keep that ACL, and readings of an
// earlier run are of no use: the folder starts empty.
let data = std::env::temp_dir().join(format!("jarvis-pc-{}-{}", std::process::id(), rand::random::<u32>()));
let home = data.join("runtime").join("agent-desktop");
std::fs::create_dir_all(home.join("refmaps")).unwrap();
std::fs::write(home.join("refmaps").join("old.json"), "{}").unwrap();
let _cli = Cli::new(Path::new("agent-desktop.exe"), &data);
assert!(home.is_dir());
assert_eq!(std::fs::read_dir(&home).unwrap().count(), 0);
let _ = std::fs::remove_dir_all(&data);
}
#[test] #[test]
fn the_package_and_m0_pin_the_same_agent_desktop() { fn the_package_and_m0_pin_the_same_agent_desktop() {
let root = Path::new(env!("CARGO_MANIFEST_DIR")); let root = Path::new(env!("CARGO_MANIFEST_DIR"));