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(app): capture permission telemetry backend-side so headless revokes are visible (#6660)

Louis Beaumont590305042e45
diff --git a/apps/screenpipe-app-tauri/lib/hooks/use-permission-monitor.tsx b/apps/screenpipe-app-tauri/lib/hooks/use-permission-monitor.tsx
index bdc160969..a7fa7bace 100644
--- a/apps/screenpipe-app-tauri/lib/hooks/use-permission-monitor.tsx
+++ b/apps/screenpipe-app-tauri/lib/hooks/use-permission-monitor.tsx
@@ -8,7 +8,6 @@ import { useEffect, useRef } from "react";
import { usePathname } from "next/navigation";
import { listen } from "@tauri-apps/api/event";
import { commands } from "@/lib/utils/tauri";
-import posthog from "posthog-js";
interface PermissionLostPayload {
screen_recording: boolean;
@@ -43,7 +42,7 @@ export function usePermissionMonitor() {
if (skipPaths.some((p) => pathname?.startsWith(p))) return;
const unlisten = listen<PermissionLostPayload>("permission-lost", async (event) => {
- const { screen_recording, microphone, accessibility, browser_automation, reason } = event.payload;
+ const { screen_recording, microphone, accessibility, browser_automation } = event.payload;
if (hasShownRef.current) return;
@@ -54,13 +53,9 @@ export function usePermissionMonitor() {
hasShownRef.current = true;
- posthog.capture("permission_lost", {
- screen_recording_lost: screen_recording,
- microphone_lost: microphone,
- accessibility_lost: accessibility,
- browser_automation_lost: browser_automation,
- reason: reason ?? null,
- });
+ // Telemetry for this event is captured backend-side
+ // (engine_events/permission.rs) — the webview does not exist in
+ // dormant/headless mode, so capturing here missed tray-only users.
try {
await commands.showWindow("PermissionRecovery");
@@ -94,8 +89,6 @@ export function usePermissionMonitor() {
const { kind } = event.payload;
console.log("Permission needed event received:", kind);
- posthog.capture("permission_needed", { kind });
-
// If we're not on onboarding or permission-recovery, show the recovery window
// to guide user through the permission flow
if (!skipPaths.some((p) => pathname?.startsWith(p))) {
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/engine_events/permission.rs b/apps/screenpipe-app-tauri/src-tauri/src/engine_events/permission.rs
index d0a953468..5ae6ee11f 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/engine_events/permission.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/engine_events/permission.rs
@@ -14,10 +14,14 @@
//! the UI shows a softer notification instead of the blocking modal that
//! TCC losses trigger.
+use std::sync::Arc;
+
use serde_json::Value;
-use tauri::{AppHandle, Emitter};
+use tauri::{AppHandle, Emitter, Manager};
use tracing::{debug, info, warn};
+use crate::analytics::AnalyticsManager;
+
/// Handle one frame of `permission_lost`, `permission_restored`, or
/// `permission_needed`. Called from [`super::dispatch`].
pub(super) fn handle(app: &AppHandle, name: &str, data: &Value) {
@@ -29,6 +33,51 @@ pub(super) fn handle(app: &AppHandle, name: &str, data: &Value) {
}
}
+/// PostHog properties for a permission engine event.
+///
+/// `permission_lost` keeps the boolean property names of the old webview
+/// capture (`use-permission-monitor.tsx`) so existing PostHog insights keep
+/// working across the move; `kind` and `capture_source` are additive.
+fn telemetry_props(name: &str, data: &Value) -> Value {
+ let kind = data.get("kind").and_then(|v| v.as_str()).unwrap_or("");
+ let reason = data.get("reason").and_then(|v| v.as_str());
+ match name {
+ "permission_lost" => serde_json::json!({
+ "screen_recording_lost": kind == "screen_recording",
+ "microphone_lost": kind == "microphone",
+ "accessibility_lost": kind == "accessibility",
+ "browser_automation_lost": false,
+ "kind": kind,
+ "reason": reason,
+ "capture_source": "app_backend",
+ }),
+ _ => serde_json::json!({
+ "kind": kind,
+ "capture_source": "app_backend",
+ }),
+ }
+}
+
+/// Send the event to PostHog from the backend.
+///
+/// This used to happen in the main webview, which is destroyed in
+/// dormant/headless mode — exactly the tray-only population where a revoked
+/// grant (e.g. Deny on the macOS periodic screen-recording re-approval
+/// alert) otherwise kills capture with zero telemetry. The engine emits
+/// these events on state transitions only, so volume stays one event per
+/// loss/restore.
+fn track(app: &AppHandle, event: &'static str, data: &Value) {
+ let props = telemetry_props(event, data);
+ let app = app.clone();
+ tauri::async_runtime::spawn(async move {
+ if let Some(analytics) = app.try_state::<Arc<AnalyticsManager>>() {
+ if let Err(e) = analytics.send_event(event, Some(props)).await {
+ warn!("failed to send {} telemetry: {}", event, e);
+ }
+ }
+ });
+}
+
fn handle_lost(app: &AppHandle, data: &Value) {
// Suppress the recovery modal until onboarding completes. Otherwise the
// user sees "permission lost" on top of the first-run permission grant
@@ -38,6 +87,7 @@ fn handle_lost(app: &AppHandle, data: &Value) {
return;
}
let kind = data.get("kind").and_then(|v| v.as_str()).unwrap_or("");
+ track(app, "permission_lost", data);
if kind == "keychain" {
info!(event = %data, "permission-lost keychain (from engine)");
@@ -70,6 +120,9 @@ fn handle_lost(app: &AppHandle, data: &Value) {
fn handle_restored(app: &AppHandle, data: &Value) {
let kind = data.get("kind").and_then(|v| v.as_str()).unwrap_or("");
+ // Restores were never tracked before — without them there is no way to
+ // measure how many lost grants recover, or how long recovery takes.
+ track(app, "permission_restored", data);
if kind == "keychain" {
info!(event = %data, "permission-restored keychain (from engine)");
if let Err(e) = app.emit("permission-restored-keychain", data.clone()) {
@@ -91,6 +144,7 @@ fn handle_needed(app: &AppHandle, data: &Value) {
return;
}
let kind = data.get("kind").and_then(|v| v.as_str()).unwrap_or("");
+ track(app, "permission_needed", data);
info!(kind = %kind, "permission_needed (from engine)");
// Forward raw payload — frontend PermissionNeededPayload expects { kind }.
if let Err(e) = app.emit("permission_needed", data.clone()) {
@@ -106,3 +160,43 @@ fn onboarding_completed(app: &AppHandle) -> bool {
_ => false,
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::telemetry_props;
+ use serde_json::json;
+
+ #[test]
+ fn lost_props_map_kind_to_booleans_and_carry_reason() {
+ let props = telemetry_props(
+ "permission_lost",
+ &json!({"kind": "screen_recording", "reason": "poll"}),
+ );
+ assert_eq!(props["screen_recording_lost"], json!(true));
+ assert_eq!(props["microphone_lost"], json!(false));
+ assert_eq!(props["accessibility_lost"], json!(false));
+ assert_eq!(props["kind"], json!("screen_recording"));
+ assert_eq!(props["reason"], json!("poll"));
+ assert_eq!(props["capture_source"], json!("app_backend"));
+ }
+
+ #[test]
+ fn lost_props_keychain_sets_no_tcc_booleans() {
+ let props = telemetry_props("permission_lost", &json!({"kind": "keychain"}));
+ assert_eq!(props["screen_recording_lost"], json!(false));
+ assert_eq!(props["microphone_lost"], json!(false));
+ assert_eq!(props["accessibility_lost"], json!(false));
+ assert_eq!(props["kind"], json!("keychain"));
+ assert_eq!(props["reason"], json!(null));
+ }
+
+ #[test]
+ fn restored_and_needed_props_carry_kind() {
+ for name in ["permission_restored", "permission_needed"] {
+ let props = telemetry_props(name, &json!({"kind": "microphone"}));
+ assert_eq!(props["kind"], json!("microphone"));
+ assert_eq!(props["capture_source"], json!("app_backend"));
+ assert!(props.get("screen_recording_lost").is_none());
+ }
+ }
+}