Skip to content

Computer use CLI for AI agents

fix: name the launching app in the permission guide under tmux (#681)

Lahfir8ffecc1d3d08
diff --git a/apps/docs/content/docs/platforms/macos.mdx b/apps/docs/content/docs/platforms/macos.mdx
index 2c5a5d4ba..22a35014b 100644
--- a/apps/docs/content/docs/platforms/macos.mdx
+++ b/apps/docs/content/docs/platforms/macos.mdx
@@ -35,7 +35,7 @@ The grant belongs to the app that launched agent-desktop, not to the shell. A di
}
```
-`automation` stays `unknown` until macOS would need to prompt. `permissions --request` is the only path that prompts, and it covers Automation; `--guide` covers Accessibility and Screen Recording. Under tmux or SSH there is no launching app, so add the terminal app that started tmux by hand.
+`automation` stays `unknown` until macOS would need to prompt. `permissions --request` is the only path that prompts, and it covers Automation; `--guide` covers Accessibility and Screen Recording. Under tmux, the guide offers the app that started the tmux server, which can differ from the terminal you attach from. Under SSH there is no launching app, so the guide offers the `agent-desktop` binary.
## What works headless
diff --git a/crates/macos/src/system/permissions/permission_guide.rs b/crates/macos/src/system/permissions/permission_guide.rs
index f12b0ef3d..27ad9d498 100644
--- a/crates/macos/src/system/permissions/permission_guide.rs
+++ b/crates/macos/src/system/permissions/permission_guide.rs
@@ -127,19 +127,35 @@ fn parse_steps(value: &str) -> Option<u8> {
.filter(|steps| (1..=(ACCESSIBILITY | SCREEN_RECORDING)).contains(steps))
}
-/// TCC charges a command-line tool to the app that launched its process
-/// tree, so the guide shows the nearest ancestor that runs from an app
-/// bundle. Terminal servers such as Rex Server are not registered with
-/// LaunchServices, so the bundle comes from the executable path.
+/// TCC charges a command-line tool to its responsible process, which stays the
+/// app that launched it even when a terminal multiplexer server detaches and is
+/// reparented to launchd. The ancestor walk is the fallback. Terminal servers
+/// such as Rex Server are not registered with LaunchServices, so the bundle
+/// comes from the executable path.
fn host_app() -> Option<PathBuf> {
- let mut pid = i32::try_from(std::process::id()).ok()?;
+ host_app_of(i32::try_from(std::process::id()).ok()?, responsible_pid)
+}
+
+fn host_app_of(pid: i32, responsible: impl Fn(i32) -> Option<i32>) -> Option<PathBuf> {
+ responsible(pid)
+ .and_then(app_of)
+ .or_else(|| ancestor_app(pid))
+}
+
+fn app_of(pid: i32) -> Option<PathBuf> {
+ crate::system::renderer_kind::executable_path(pid)
+ .as_deref()
+ .and_then(outermost_app)
+}
+
+/// The nearest ancestor that runs from an app bundle, used when the
+/// responsible process is unknown or runs outside an app bundle.
+fn ancestor_app(mut pid: i32) -> Option<PathBuf> {
for _ in 0..MAX_ANCESTORS {
if pid <= 1 {
return None;
}
- let app = crate::system::renderer_kind::executable_path(pid)
- .as_deref()
- .and_then(outermost_app);
+ let app = app_of(pid);
if app.is_some() {
return app;
}
@@ -148,6 +164,24 @@ fn host_app() -> Option<PathBuf> {
None
}
+/// Resolves the private libSystem symbol at runtime, so a macOS that drops it
+/// leaves the binary launching instead of failing to link.
+fn responsible_pid(pid: i32) -> Option<i32> {
+ let symbol = unsafe {
+ libc::dlsym(
+ libc::RTLD_DEFAULT,
+ c"responsibility_get_pid_responsible_for_pid".as_ptr(),
+ )
+ };
+ if symbol.is_null() {
+ return None;
+ }
+ let responsible: unsafe extern "C" fn(libc::pid_t) -> libc::pid_t =
+ unsafe { std::mem::transmute(symbol) };
+ let responsible = unsafe { responsible(pid) };
+ (responsible > 0).then_some(responsible)
+}
+
/// A helper such as `Code Helper (Plugin).app` sits inside the app that TCC
/// names, so the outermost bundle wins.
fn outermost_app(executable: &Path) -> Option<PathBuf> {
@@ -228,103 +262,5 @@ unsafe extern "C" {
}
#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn guide_covers_only_missing_permissions() {
- assert_eq!(missing_steps(true, true), 0);
- assert_eq!(missing_steps(false, true), ACCESSIBILITY);
- assert_eq!(missing_steps(true, false), SCREEN_RECORDING);
- assert_eq!(
- missing_steps(false, false),
- ACCESSIBILITY | SCREEN_RECORDING
- );
- assert_eq!(parse_steps("3"), Some(3));
- assert_eq!(parse_steps("0"), None);
- assert_eq!(parse_steps("4"), None);
- }
-
- #[test]
- fn the_guide_lock_stays_held_from_the_check_through_the_child() {
- let path = std::env::temp_dir().join(format!("{LOCK_NAME}.{}.test", std::process::id()));
- let lock = lock_file(&path).unwrap().unwrap();
- let mut command = Command::new("/bin/sleep");
- command.arg("30");
- hand_over(&mut command, &lock);
- let mut child = command.spawn().unwrap();
- drop(lock);
- assert!(lock_file(&path).unwrap().is_none());
- child.kill().unwrap();
- child.wait().unwrap();
- assert!(lock_released_within(&path, Duration::from_secs(10)));
- std::fs::remove_file(&path).unwrap();
- }
-
- fn lock_released_within(path: &Path, limit: Duration) -> bool {
- let give_up = Instant::now() + limit;
- loop {
- if lock_file(path).unwrap().is_some() {
- return true;
- }
- if Instant::now() >= give_up {
- return false;
- }
- std::thread::sleep(Duration::from_millis(5));
- }
- }
-
- #[test]
- fn the_guide_offers_the_outermost_app_bundle_of_the_launching_process() {
- let rex = Path::new(
- "/Users/me/Library/Application Support/com.superlogical.rex/RexServer/Versions/Current/Rex Server.app/Contents/MacOS/rex",
- );
- assert_eq!(
- outermost_app(rex),
- Some(PathBuf::from(
- "/Users/me/Library/Application Support/com.superlogical.rex/RexServer/Versions/Current/Rex Server.app"
- ))
- );
- let code_helper = Path::new(
- "/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Plugin).app/Contents/MacOS/Code Helper (Plugin)",
- );
- assert_eq!(
- outermost_app(code_helper),
- Some(PathBuf::from("/Applications/Visual Studio Code.app"))
- );
- assert_eq!(
- outermost_app(Path::new(
- "/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal"
- )),
- Some(PathBuf::from("/System/Applications/Utilities/Terminal.app"))
- );
- assert_eq!(
- outermost_app(Path::new("/usr/local/bin/agent-desktop")),
- None
- );
- }
-
- #[test]
- fn the_host_walk_passes_through_a_process_of_another_user() {
- let parent = parent_pid(i32::try_from(std::process::id()).unwrap());
- assert_eq!(parent, Some(unsafe { libc::getppid() }));
- let launchd_child = std::process::Command::new("/bin/ps")
- .args(["-axo", "pid=,ppid=,uid="])
- .output()
- .unwrap();
- let root_child = String::from_utf8(launchd_child.stdout)
- .unwrap()
- .lines()
- .filter_map(|line| {
- let fields: Vec<i32> = line
- .split_whitespace()
- .filter_map(|field| field.parse().ok())
- .collect();
- (fields.len() == 3 && fields[0] > 1 && fields[1] == 1 && fields[2] == 0)
- .then_some(fields[0])
- })
- .next()
- .unwrap();
- assert_eq!(parent_pid(root_child), Some(1));
- }
-}
+#[path = "permission_guide_tests.rs"]
+mod tests;
diff --git a/crates/macos/src/system/permissions/permission_guide_tests.rs b/crates/macos/src/system/permissions/permission_guide_tests.rs
new file mode 100644
index 000000000..72b398f1b
--- /dev/null
+++ b/crates/macos/src/system/permissions/permission_guide_tests.rs
@@ -0,0 +1,152 @@
+use super::*;
+
+fn detached_sleep() -> i32 {
+ let launched = Command::new("/bin/sh")
+ .args(["-c", "/bin/sleep 30 >/dev/null 2>&1 & echo $!"])
+ .output()
+ .unwrap();
+ let orphan: i32 = String::from_utf8(launched.stdout)
+ .unwrap()
+ .trim()
+ .parse()
+ .unwrap();
+ let give_up = Instant::now() + Duration::from_secs(5);
+ while parent_pid(orphan) != Some(1) && Instant::now() < give_up {
+ std::thread::sleep(Duration::from_millis(5));
+ }
+ orphan
+}
+
+#[test]
+fn guide_covers_only_missing_permissions() {
+ assert_eq!(missing_steps(true, true), 0);
+ assert_eq!(missing_steps(false, true), ACCESSIBILITY);
+ assert_eq!(missing_steps(true, false), SCREEN_RECORDING);
+ assert_eq!(
+ missing_steps(false, false),
+ ACCESSIBILITY | SCREEN_RECORDING
+ );
+ assert_eq!(parse_steps("3"), Some(3));
+ assert_eq!(parse_steps("0"), None);
+ assert_eq!(parse_steps("4"), None);
+}
+
+#[test]
+fn the_guide_lock_stays_held_from_the_check_through_the_child() {
+ let path = std::env::temp_dir().join(format!("{LOCK_NAME}.{}.test", std::process::id()));
+ let lock = lock_file(&path).unwrap().unwrap();
+ let mut command = Command::new("/bin/sleep");
+ command.arg("30");
+ hand_over(&mut command, &lock);
+ let mut child = command.spawn().unwrap();
+ drop(lock);
+ assert!(lock_file(&path).unwrap().is_none());
+ child.kill().unwrap();
+ child.wait().unwrap();
+ assert!(lock_released_within(&path, Duration::from_secs(10)));
+ std::fs::remove_file(&path).unwrap();
+}
+
+fn lock_released_within(path: &Path, limit: Duration) -> bool {
+ let give_up = Instant::now() + limit;
+ loop {
+ if lock_file(path).unwrap().is_some() {
+ return true;
+ }
+ if Instant::now() >= give_up {
+ return false;
+ }
+ std::thread::sleep(Duration::from_millis(5));
+ }
+}
+
+#[test]
+fn the_guide_offers_the_outermost_app_bundle_of_the_launching_process() {
+ let rex = Path::new(
+ "/Users/me/Library/Application Support/com.superlogical.rex/RexServer/Versions/Current/Rex Server.app/Contents/MacOS/rex",
+ );
+ assert_eq!(
+ outermost_app(rex),
+ Some(PathBuf::from(
+ "/Users/me/Library/Application Support/com.superlogical.rex/RexServer/Versions/Current/Rex Server.app"
+ ))
+ );
+ let code_helper = Path::new(
+ "/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Plugin).app/Contents/MacOS/Code Helper (Plugin)",
+ );
+ assert_eq!(
+ outermost_app(code_helper),
+ Some(PathBuf::from("/Applications/Visual Studio Code.app"))
+ );
+ assert_eq!(
+ outermost_app(Path::new(
+ "/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal"
+ )),
+ Some(PathBuf::from("/System/Applications/Utilities/Terminal.app"))
+ );
+ assert_eq!(
+ outermost_app(Path::new("/usr/local/bin/agent-desktop")),
+ None
+ );
+}
+
+#[test]
+fn the_host_walk_passes_through_a_process_of_another_user() {
+ let parent = parent_pid(i32::try_from(std::process::id()).unwrap());
+ assert_eq!(parent, Some(unsafe { libc::getppid() }));
+ let launchd_child = std::process::Command::new("/bin/ps")
+ .args(["-axo", "pid=,ppid=,uid="])
+ .output()
+ .unwrap();
+ let root_child = String::from_utf8(launchd_child.stdout)
+ .unwrap()
+ .lines()
+ .filter_map(|line| {
+ let fields: Vec<i32> = line
+ .split_whitespace()
+ .filter_map(|field| field.parse().ok())
+ .collect();
+ (fields.len() == 3 && fields[0] > 1 && fields[1] == 1 && fields[2] == 0)
+ .then_some(fields[0])
+ })
+ .next()
+ .unwrap();
+ assert_eq!(parent_pid(root_child), Some(1));
+}
+
+#[test]
+fn the_host_survives_a_process_detaching_from_its_launcher() {
+ let orphan = detached_sleep();
+ let own = i32::try_from(std::process::id()).unwrap();
+ let orphan_responsible = responsible_pid(orphan);
+ let own_responsible = responsible_pid(own);
+ unsafe { libc::kill(orphan, libc::SIGKILL) };
+ let inherited = orphan_responsible.is_some() && orphan_responsible == own_responsible;
+ let nothing_to_inherit = own_responsible == Some(own) && orphan_responsible == Some(orphan);
+ assert!(
+ inherited || nothing_to_inherit,
+ "orphan {orphan} answers {orphan_responsible:?}, this process {own} answers {own_responsible:?}"
+ );
+}
+
+#[test]
+fn the_responsible_app_wins_over_the_ancestor_walk() {
+ let listing = Command::new("/bin/ps")
+ .args(["-axo", "pid="])
+ .output()
+ .unwrap();
+ let (app_pid, bundle) = String::from_utf8(listing.stdout)
+ .unwrap()
+ .lines()
+ .filter_map(|line| line.trim().parse::<i32>().ok())
+ .find_map(|pid| app_of(pid).map(|bundle| (pid, bundle)))
+ .unwrap();
+ let orphan = detached_sleep();
+ let with_responsible = host_app_of(orphan, |_| Some(app_pid));
+ let without = host_app_of(orphan, |_| None);
+ let outside_a_bundle = host_app_of(app_pid, |_| Some(orphan));
+ unsafe { libc::kill(orphan, libc::SIGKILL) };
+ assert_eq!(with_responsible, Some(bundle.clone()));
+ assert_eq!(without, None);
+ assert_eq!(outside_a_bundle, Some(bundle));
+}
diff --git a/skills/agent-desktop-macos/references/troubleshooting.md b/skills/agent-desktop-macos/references/troubleshooting.md
index b847950ed..c0bc5675c 100644
--- a/skills/agent-desktop-macos/references/troubleshooting.md
+++ b/skills/agent-desktop-macos/references/troubleshooting.md
@@ -10,7 +10,7 @@ Each entry is a symptom, its cause, and the fix. Read `error.code`, `error.sugge
**Fix.** Follow "First contact" in the macOS skill: `permissions --guide`, turn on the launching app (and the `agent-desktop` binary if it is listed on its own) in System Settings > Privacy & Security > Accessibility or Screen Recording, then run `agent-desktop permissions` to confirm `granted`. The launching app does not need a restart.
-The guide offers the app bundle of the nearest process above agent-desktop. Under a tmux server or an SSH session, no such app exists, so the guide offers the `agent-desktop` binary. In that case, add the terminal app that started tmux by hand. macOS 27 and later name the Accessibility list Device Control and Data Access.
+The guide offers the app that macOS charges the permission to, which is the app that started agent-desktop's process tree. Under tmux, that is the app that started the tmux server, which can differ from the terminal you attach from. Under an SSH session, no such app exists, so the guide offers the `agent-desktop` binary. macOS 27 and later name the Accessibility list Device Control and Data Access.
For Automation, open Privacy & Security > Automation and allow the launching app to control System Events.