Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(permissions): poll TCC without helper process

Ezra Ellette1e7c3ad3ab93
diff --git a/apps/screenpipe-app-tauri/scripts/probe-screen-recording-permission-state.sh b/apps/screenpipe-app-tauri/scripts/probe-screen-recording-permission-state.sh
index 2651808a2..321e22f9c 100755
--- a/apps/screenpipe-app-tauri/scripts/probe-screen-recording-permission-state.sh
+++ b/apps/screenpipe-app-tauri/scripts/probe-screen-recording-permission-state.sh
@@ -23,16 +23,9 @@ cat >"$probe_source" <<'EOF'
#include <ApplicationServices/ApplicationServices.h>
#include <CoreFoundation/CoreFoundation.h>
#include <dlfcn.h>
-#include <spawn.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/wait.h>
-#include <unistd.h>
-
-extern char **environ;
typedef uint32_t (*TCCAccessPreflightFn)(CFStringRef service);
@@ -78,65 +71,14 @@ static int direct_tcc_preflight(uint32_t *result_out) {
return 0;
}
-static int fresh_process_tcc_preflight(const char *executable, uint32_t *result_out) {
- int output_pipe[2];
- if (pipe(output_pipe) != 0) {
- return 20;
- }
-
- posix_spawn_file_actions_t actions;
- if (posix_spawn_file_actions_init(&actions) != 0 ||
- posix_spawn_file_actions_adddup2(&actions, output_pipe[1], STDOUT_FILENO) != 0 ||
- posix_spawn_file_actions_addclose(&actions, output_pipe[0]) != 0 ||
- posix_spawn_file_actions_addclose(&actions, output_pipe[1]) != 0) {
- close(output_pipe[0]);
- close(output_pipe[1]);
- return 21;
- }
-
- pid_t child = 0;
- char *child_argv[] = {(char *)executable, "--fresh-tcc-preflight", NULL};
- int spawn_result = posix_spawn(&child, executable, &actions, NULL, child_argv, environ);
- posix_spawn_file_actions_destroy(&actions);
- close(output_pipe[1]);
- if (spawn_result != 0) {
- close(output_pipe[0]);
- return 22;
- }
-
- char output[32] = {0};
- ssize_t output_length = read(output_pipe[0], output, sizeof(output) - 1);
- close(output_pipe[0]);
-
- int child_status = 0;
- if (waitpid(child, &child_status, 0) < 0 ||
- !WIFEXITED(child_status) || WEXITSTATUS(child_status) != 0) {
- return 23;
- }
- if (output_length <= 0 || sscanf(output, "%u", result_out) != 1) {
- return 24;
- }
- return 0;
-}
-
-int main(int argc, char **argv) {
+int main(void) {
uint32_t live_raw = 0;
int direct_result = direct_tcc_preflight(&live_raw);
if (direct_result != 0) {
return direct_result;
}
- if (argc == 2 && strcmp(argv[1], "--fresh-tcc-preflight") == 0) {
- printf("%u\n", live_raw);
- return 0;
- }
-
bool cached_preflight = CGPreflightScreenCaptureAccess();
- int fresh_result = fresh_process_tcc_preflight(argv[0], &live_raw);
- if (fresh_result != 0) {
- fprintf(stderr, "error: fresh TCC preflight failed (%d)\n", fresh_result);
- return fresh_result;
- }
bool live_preflight = live_raw == 0;
printf("cached_preflight=%s\n", cached_preflight ? "true" : "false");
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/main.rs b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
index 5dfa958a0..ff5c3ff10 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/main.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
@@ -334,15 +334,6 @@ async fn main() {
std::process::exit(exit_code);
}
- #[cfg(target_os = "macos")]
- if std::env::args().any(|arg| arg == permissions::SCREEN_RECORDING_PREFLIGHT_HELPER_ARG) {
- if let Err(error) = permissions::run_screen_recording_preflight_helper() {
- eprintln!("screen recording preflight helper failed: {error}");
- std::process::exit(1);
- }
- return;
- }
-
#[cfg(target_os = "linux")]
linux_webkit_env::configure();
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/permissions.rs b/apps/screenpipe-app-tauri/src-tauri/src/permissions.rs
index 81622825b..bf8fc073a 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/permissions.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/permissions.rs
@@ -9,10 +9,6 @@ use specta::Type;
use std::sync::atomic::Ordering;
use tracing::{debug, error, info, warn};
-#[cfg(target_os = "macos")]
-pub(crate) const SCREEN_RECORDING_PREFLIGHT_HELPER_ARG: &str =
- "--screen-recording-preflight-helper";
-
#[derive(Serialize, Deserialize, Type, Clone)]
#[serde(rename_all = "camelCase")]
pub enum OSPermission {
@@ -58,21 +54,17 @@ fn should_restart_after_screen_recording_grant(state: ScreenRecordingPermissionS
/// Returns the current process's Screen Recording permission state.
///
/// `CGPreflightScreenCaptureAccess` can return the result cached by SkyLight
-/// when this process requested permission. TCC's own preflight can also keep
-/// returning that process's old answer, so the live half of this check runs in
-/// a short-lived invocation of this same executable. A disagreement identifies
-/// a grant or revocation that happened after the running process cached its
-/// result.
+/// when this process requested permission. Calling TCC's preflight function
+/// directly bypasses that SkyLight cache, so a disagreement identifies a grant
+/// or revocation that happened after the cached result was recorded.
#[cfg(target_os = "macos")]
pub fn screen_recording_permission_state() -> ScreenRecordingPermissionState {
use core_graphics_helmer_fork::access::ScreenCaptureAccess;
let cached_preflight = ScreenCaptureAccess.preflight();
- let live_preflight = fresh_process_screen_recording_preflight().unwrap_or_else(|error| {
- warn!(
- "fresh-process Screen Recording preflight failed ({error}); falling back to the current process"
- );
- direct_tcc_screen_recording_preflight().unwrap_or(cached_preflight)
+ let live_preflight = direct_tcc_screen_recording_preflight().unwrap_or_else(|error| {
+ warn!("direct Screen Recording TCC preflight failed ({error}); using cached preflight");
+ cached_preflight
});
screen_recording_permission_state_from_checks(cached_preflight, live_preflight)
@@ -113,36 +105,6 @@ fn direct_tcc_screen_recording_preflight() -> Result<bool, String> {
}
}
-#[cfg(target_os = "macos")]
-fn parse_screen_recording_preflight_helper_output(output: &[u8]) -> Result<bool, String> {
- match std::str::from_utf8(output).map(str::trim) {
- Ok("granted") => Ok(true),
- Ok("not_granted") => Ok(false),
- Ok(other) => Err(format!("unexpected helper output: {other:?}")),
- Err(error) => Err(format!("helper output was not UTF-8: {error}")),
- }
-}
-
-#[cfg(target_os = "macos")]
-fn fresh_process_screen_recording_preflight() -> Result<bool, String> {
- let executable = std::env::current_exe()
- .map_err(|error| format!("failed to resolve current executable: {error}"))?;
- let output = std::process::Command::new(executable)
- .arg(SCREEN_RECORDING_PREFLIGHT_HELPER_ARG)
- .output()
- .map_err(|error| format!("failed to start preflight helper: {error}"))?;
-
- if !output.status.success() {
- return Err(format!(
- "preflight helper exited with {}: {}",
- output.status,
- String::from_utf8_lossy(&output.stderr).trim()
- ));
- }
-
- parse_screen_recording_preflight_helper_output(&output.stdout)
-}
-
#[cfg(target_os = "macos")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SystemSettingsModalState {
@@ -305,19 +267,6 @@ impl ScreenRecordingRestartWaitState {
}
}
-/// Runs before Tauri and the single-instance handoff in the helper process.
-/// It must stay limited to a silent preflight: requesting permission here
-/// would allow a background poll to reorder macOS permission prompts.
-#[cfg(target_os = "macos")]
-pub(crate) fn run_screen_recording_preflight_helper() -> Result<(), String> {
- if direct_tcc_screen_recording_preflight()? {
- println!("granted");
- } else {
- println!("not_granted");
- }
- Ok(())
-}
-
#[cfg(target_os = "macos")]
async fn relaunch_after_screen_recording_grant(app: tauri::AppHandle) {
use crate::recording::{bounded_teardown, TeardownOutcome, PRE_EXIT_TEARDOWN_TIMEOUT};
@@ -433,8 +382,8 @@ pub(crate) fn start_screen_recording_permission_restart_monitor(app: tauri::AppH
}
// Keep the common onboarding path responsive. If the user leaves
- // Settings open or cancels, back off rather than spawning a helper
- // every second forever; a later grant is still detected.
+ // Settings open or cancels, back off rather than polling every
+ // second forever; a later grant is still detected.
let delay = if started_at.elapsed() < std::time::Duration::from_secs(10 * 60) {
std::time::Duration::from_secs(1)
} else {
@@ -1718,20 +1667,6 @@ mod screen_recording_state_tests {
assert!(direct_tcc_screen_recording_preflight().is_ok());
}
- #[cfg(target_os = "macos")]
- #[test]
- fn parses_fresh_process_preflight_output_strictly() {
- assert_eq!(
- parse_screen_recording_preflight_helper_output(b"granted\n"),
- Ok(true)
- );
- assert_eq!(
- parse_screen_recording_preflight_helper_output(b"not_granted\n"),
- Ok(false)
- );
- assert!(parse_screen_recording_preflight_helper_output(b"granted later").is_err());
- }
-
#[test]
fn only_requests_when_live_tcc_is_not_granted() {
assert!(should_request_screen_recording(