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
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
Cache flag refreshes and scope health reporter lifetimes (#7672)
Louis Beaumont78f510537d57
diff --git a/apps/screenpipe-app-tauri/components/desktop-remote-control.tsx b/apps/screenpipe-app-tauri/components/desktop-remote-control.tsxindex 1c92d86e3..69234004a 100644--- a/apps/screenpipe-app-tauri/components/desktop-remote-control.tsx+++ b/apps/screenpipe-app-tauri/components/desktop-remote-control.tsx@@ -6,6 +6,7 @@import { useEffect, useRef } from "react";import posthog from "posthog-js";+import { subscribeToFeatureFlagRefresh } from "@/lib/feature-flag-refresh";import { commands } from "@/lib/utils/tauri";import { useSettings, type Settings } from "@/lib/hooks/use-settings";import { isPrimaryWindow } from "@/lib/utils/is-primary-window";@@ -20,7 +21,6 @@ import {trialActivationState,} from "@/lib/first-run/trial-activation";-const REFRESH_INTERVAL_MS = 60_000;const RESTART_SETTLE_MS = 500;async function applyTrialActivationForceUnlock(): Promise<boolean> {@@ -80,7 +80,6 @@ export function DesktopRemoteControl({ enabled }: { enabled: boolean }) {let cancelled = false;let unsubscribe: (() => void) | undefined;- let refreshTimer: ReturnType<typeof setInterval> | undefined;const reconcile = () => {if (cancelled) return;@@ -212,29 +211,12 @@ export function DesktopRemoteControl({ enabled }: { enabled: boolean }) {// PostHog is not initialized in debug, browser-dev, or E2E builds.}- const refresh = () => {- if (cancelled || document.visibilityState === "hidden") return;- try {- posthog.reloadFeatureFlags();- } catch {- // Keep the last persisted policy while offline or uninitialized.- }- };- const onVisibilityChange = () => {- if (document.visibilityState === "visible") refresh();- };-- window.addEventListener("focus", refresh);- document.addEventListener("visibilitychange", onVisibilityChange);- refreshTimer = setInterval(refresh, REFRESH_INTERVAL_MS);- refresh();+ const stopRefreshing = subscribeToFeatureFlagRefresh();return () => {cancelled = true;unsubscribe?.();- if (refreshTimer) clearInterval(refreshTimer);- window.removeEventListener("focus", refresh);- document.removeEventListener("visibilitychange", onVisibilityChange);+ stopRefreshing();};}, [allowRemoteAutoUpdate, enabled, isSettingsLoaded]);diff --git a/apps/screenpipe-app-tauri/lib/feature-flag-refresh.test.ts b/apps/screenpipe-app-tauri/lib/feature-flag-refresh.test.tsnew file mode 100644index 000000000..2ac08aea1--- /dev/null+++ b/apps/screenpipe-app-tauri/lib/feature-flag-refresh.test.ts@@ -0,0 +1,140 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com++import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++const { reload } = vi.hoisted(() => ({ reload: vi.fn() }));+vi.mock("posthog-js", () => ({ default: { reloadFeatureFlags: reload } }));+import { subscribeToFeatureFlagRefresh } from "./feature-flag-refresh";++const stops: (() => void)[] = [];+function subscribe(canRefresh?: () => boolean) {+ const stop = subscribeToFeatureFlagRefresh(canRefresh);+ stops.push(stop);+ return stop;+}+function visible(value: "visible" | "hidden") {+ Object.defineProperty(document, "visibilityState", { configurable: true, value });+ document.dispatchEvent(new Event("visibilitychange"));+}++describe("shared feature flag refresh", () => {+ beforeEach(() => {+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance", "Date"] });+ reload.mockReset();+ visible("visible");+ });+ afterEach(() => {+ stops.splice(0).forEach((stop) => stop());+ vi.useRealTimers();+ });++ it("shares a minute schedule even when consumers mount beyond SDK debounce", () => {+ subscribe();+ vi.advanceTimersByTime(300);+ subscribe();+ expect(reload).toHaveBeenCalledTimes(1);+ vi.advanceTimersByTime(59_700);+ expect(reload).toHaveBeenCalledTimes(2);+ vi.advanceTimersByTime(300);+ expect(reload).toHaveBeenCalledTimes(2);+ });++ it("reuses fresh flags through repeated focus and visibility events", () => {+ subscribe();+ for (let i = 0; i < 59; i++) {+ vi.advanceTimersByTime(1000);+ window.dispatchEvent(new Event("focus"));+ visible("visible");+ }+ expect(reload).toHaveBeenCalledTimes(1);+ vi.advanceTimersByTime(1000);+ expect(reload).toHaveBeenCalledTimes(2);+ });++ it("refreshes stale flags on foreground and schedules from that refresh", () => {+ subscribe();+ visible("hidden");+ vi.advanceTimersByTime(75_000);+ expect(reload).toHaveBeenCalledTimes(1);+ visible("visible");+ expect(reload).toHaveBeenCalledTimes(2);+ vi.advanceTimersByTime(59_999);+ expect(reload).toHaveBeenCalledTimes(2);+ vi.advanceTimersByTime(1);+ expect(reload).toHaveBeenCalledTimes(3);+ });++ it("refreshes on wake when wall time advanced but the performance clock paused", () => {+ subscribe();+ vi.advanceTimersByTime(10_000);+ vi.setSystemTime(Date.now() + 6 * 60 * 60 * 1000);+ window.dispatchEvent(new Event("focus"));+ expect(reload).toHaveBeenCalledTimes(2);+ vi.advanceTimersByTime(59_999);+ expect(reload).toHaveBeenCalledTimes(2);+ vi.advanceTimersByTime(1);+ expect(reload).toHaveBeenCalledTimes(3);+ });++ it("keeps the minute deadline when the wall clock moves backwards", () => {+ subscribe();+ vi.advanceTimersByTime(10_000);+ vi.setSystemTime(Date.now() - 6 * 60 * 60 * 1000);+ window.dispatchEvent(new Event("focus"));+ expect(reload).toHaveBeenCalledTimes(1);+ vi.advanceTimersByTime(49_999);+ expect(reload).toHaveBeenCalledTimes(1);+ vi.advanceTimersByTime(1);+ expect(reload).toHaveBeenCalledTimes(2);+ });++ it("does not poll while hidden or after the last consumer unmounts", () => {+ const stopA = subscribe();+ const stopB = subscribe();+ stopA();+ vi.advanceTimersByTime(60_000);+ expect(reload).toHaveBeenCalledTimes(2);+ visible("hidden");+ vi.advanceTimersByTime(3_600_000);+ expect(reload).toHaveBeenCalledTimes(2);+ stopB();+ visible("visible");+ window.dispatchEvent(new Event("focus"));+ vi.advanceTimersByTime(60_000);+ expect(reload).toHaveBeenCalledTimes(2);+ expect(vi.getTimerCount()).toBe(0);+ });++ it("retains each consumer's opt-out policy and resumes when allowed", () => {+ let allowed = false;+ subscribe(() => allowed);+ vi.advanceTimersByTime(60_000);+ expect(reload).not.toHaveBeenCalled();+ allowed = true;+ window.dispatchEvent(new Event("focus"));+ expect(reload).toHaveBeenCalledTimes(1);+ allowed = false;+ vi.advanceTimersByTime(60_000);+ expect(reload).toHaveBeenCalledTimes(1);+ subscribe(); // Functional remote-control flags retain their own policy.+ expect(reload).toHaveBeenCalledTimes(2);+ });++ it("retries when SDK initialization initially throws", () => {+ reload.mockImplementationOnce(() => {+ throw new Error("not initialized");+ });+ subscribe();+ vi.advanceTimersByTime(60_000);+ expect(reload).toHaveBeenCalledTimes(2);+ });++ it("an unavailable consumer does not block an eligible consumer", () => {+ subscribe(() => {+ throw new Error("unavailable");+ });+ subscribe();+ expect(reload).toHaveBeenCalledTimes(1);+ });+});diff --git a/apps/screenpipe-app-tauri/lib/feature-flag-refresh.ts b/apps/screenpipe-app-tauri/lib/feature-flag-refresh.tsnew file mode 100644index 000000000..6af1eb625--- /dev/null+++ b/apps/screenpipe-app-tauri/lib/feature-flag-refresh.ts@@ -0,0 +1,84 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com++import posthog from "posthog-js";++export const FEATURE_FLAG_REFRESH_INTERVAL_MS = 60_000;++// One refresh schedule per webview. Consumers read the SDK's cached values;+// only this schedule refreshes them. SDK-triggered identity refreshes are separate.+const consumers = new Set<() => boolean>();+let timer: ReturnType<typeof setTimeout> | undefined;+let lastAttempt: { monotonic: number; wall: number } | undefined;++function attemptAge(): number {+ if (!lastAttempt) return Infinity;+ // WebKit's performance clock can pause during system sleep. Wall time+ // catches that elapsed time; monotonic time handles wall-clock rollback.+ return Math.max(+ performance.now() - lastAttempt.monotonic,+ Date.now() - lastAttempt.wall,+ );+}++function refresh() {+ if (timer !== undefined) clearTimeout(timer);+ const age = attemptAge();+ if (+ document.visibilityState !== "hidden" &&+ age >= FEATURE_FLAG_REFRESH_INTERVAL_MS &&+ [...consumers].some((canRefresh) => {+ try {+ return canRefresh();+ } catch {+ return false;+ }+ })+ ) {+ try {+ posthog.reloadFeatureFlags();+ // This is an attempt TTL, not proof of a successful response. The SDK+ // retains its cached values on failure; the next minute retries.+ lastAttempt = { monotonic: performance.now(), wall: Date.now() };+ } catch {+ // The SDK may not be initialized yet. Retry on focus or the next tick.+ }+ }++ if (consumers.size === 0) return;+ // A focus refresh after a hidden interval must move the next deadline too,+ // otherwise a fixed timer can leave values stale for almost two minutes.+ const remaining =+ lastAttempt === undefined+ ? FEATURE_FLAG_REFRESH_INTERVAL_MS+ : FEATURE_FLAG_REFRESH_INTERVAL_MS - attemptAge();+ timer = setTimeout(+ refresh,+ remaining > 0 ? remaining : FEATURE_FLAG_REFRESH_INTERVAL_MS,+ );+}++/** Share polling without changing each consumer's consent or build gating. */+export function subscribeToFeatureFlagRefresh(+ canRefresh: () => boolean = () => true,+): () => void {+ // Unique wrapper allows multiple subscribers to pass the same predicate.+ const consumer = () => canRefresh();+ const first = consumers.size === 0;+ consumers.add(consumer);+ if (first) {+ window.addEventListener("focus", refresh);+ document.addEventListener("visibilitychange", refresh);+ }+ refresh();++ return () => {+ consumers.delete(consumer);+ if (consumers.size !== 0) return;+ if (timer !== undefined) clearTimeout(timer);+ timer = undefined;+ lastAttempt = undefined;+ window.removeEventListener("focus", refresh);+ document.removeEventListener("visibilitychange", refresh);+ };+}diff --git a/apps/screenpipe-app-tauri/lib/hooks/__tests__/use-announcement.test.tsx b/apps/screenpipe-app-tauri/lib/hooks/__tests__/use-announcement.test.tsxindex 97ec274b1..c154985f2 100644--- a/apps/screenpipe-app-tauri/lib/hooks/__tests__/use-announcement.test.tsx+++ b/apps/screenpipe-app-tauri/lib/hooks/__tests__/use-announcement.test.tsx@@ -132,16 +132,16 @@ describe("useAnnouncement", () => {});});- it("refreshes flags on mount and when an already-open app regains focus", async () => {+ it("refreshes flags on mount and reuses fresh cached flags on focus", async () => {const { unmount } = renderHook(() => useAnnouncement());await flushAnnouncementEffects();expect(reloadFlagsMock).toHaveBeenCalledTimes(1);act(() => window.dispatchEvent(new Event("focus")));- expect(reloadFlagsMock).toHaveBeenCalledTimes(2);+ expect(reloadFlagsMock).toHaveBeenCalledTimes(1);unmount();act(() => window.dispatchEvent(new Event("focus")));- expect(reloadFlagsMock).toHaveBeenCalledTimes(2);+ expect(reloadFlagsMock).toHaveBeenCalledTimes(1);});it("does not load remote prompts after analytics opt-out", async () => {diff --git a/apps/screenpipe-app-tauri/lib/hooks/use-announcement.tsx b/apps/screenpipe-app-tauri/lib/hooks/use-announcement.tsxindex 07d415c26..681400732 100644--- a/apps/screenpipe-app-tauri/lib/hooks/use-announcement.tsx+++ b/apps/screenpipe-app-tauri/lib/hooks/use-announcement.tsx@@ -6,6 +6,10 @@import { useCallback, useEffect, useMemo, useRef, useState } from "react";import posthog from "posthog-js";+import {+ FEATURE_FLAG_REFRESH_INTERVAL_MS,+ subscribeToFeatureFlagRefresh,+} from "@/lib/feature-flag-refresh";import { useRouter } from "next/navigation";import { getIdentifier, getName } from "@tauri-apps/api/app";import { useTauriEvent } from "./use-tauri-event";@@ -45,7 +49,7 @@ import {* rendering; it cannot name code, components, scripts, or arbitrary events.*/export const ANNOUNCEMENT_FLAG_KEY = "app-announcement";-export const ANNOUNCEMENT_REFRESH_INTERVAL_MS = 60_000;+export const ANNOUNCEMENT_REFRESH_INTERVAL_MS = FEATURE_FLAG_REFRESH_INTERVAL_MS;interface UseAnnouncementResult {announcement: Announcement | null;@@ -99,7 +103,7 @@ export function useAnnouncement(): UseAnnouncementResult {let cancelled = false;let unsubscribe: (() => void) | undefined;- let refreshTimer: ReturnType<typeof setInterval> | undefined;+ let stopRefreshing: (() => void) | undefined;const read = () => {if (cancelled) return;@@ -116,22 +120,6 @@ export function useAnnouncement(): UseAnnouncementResult {}};- const refresh = () => {- if (cancelled || document.visibilityState === "hidden") return;- try {- if (!posthog.has_opted_out_capturing?.()) {- posthog.reloadFeatureFlags();- }- } catch {- // PostHog is not initialized or the network is unavailable. The- // cached one-shot read still keeps the UI usable.- }- };-- const onVisibilityChange = () => {- if (document.visibilityState === "visible") refresh();- };-const start = async () => {const [name, identifier] = await Promise.all([getName().catch(() => null),@@ -149,13 +137,11 @@ export function useAnnouncement(): UseAnnouncementResult {} catch {// posthog not ready / disabled — the one-shot read above is enough.}- // A release webview can stay open for days. Refresh on foreground and- // once per minute so a newly enabled PostHog payload reaches an already- // running app instead of requiring a restart.- window.addEventListener("focus", refresh);- document.addEventListener("visibilitychange", onVisibilityChange);- refreshTimer = setInterval(refresh, ANNOUNCEMENT_REFRESH_INTERVAL_MS);- refresh();+ // Share the one-minute refresh with other flag consumers. Repeated+ // focus events reuse fresh cached values instead of forcing new requests.+ stopRefreshing = subscribeToFeatureFlagRefresh(+ () => !posthog.has_opted_out_capturing?.(),+ );};start().catch(() => {@@ -165,9 +151,7 @@ export function useAnnouncement(): UseAnnouncementResult {return () => {cancelled = true;unsubscribe?.();- if (refreshTimer) clearInterval(refreshTimer);- window.removeEventListener("focus", refresh);- document.removeEventListener("visibilitychange", onVisibilityChange);+ stopRefreshing?.();};}, []);diff --git a/crates/screenpipe-engine/src/lib.rs b/crates/screenpipe-engine/src/lib.rsindex 08847b30f..d6141e588 100644--- a/crates/screenpipe-engine/src/lib.rs+++ b/crates/screenpipe-engine/src/lib.rs@@ -87,6 +87,7 @@ pub mod recording_config;pub mod recording_coverage;pub mod recording_detail;mod resource_monitor;+mod resource_summary;pub mod retention;pub mod routes;pub mod schedule_monitor;@@ -98,6 +99,7 @@ pub mod structured_outputs;mod sync_api;pub mod sync_provider;pub mod telemetry_context;+mod telemetry_tasks;pub mod ui_recorder;// Exposed publicly so the commercial `screenpipe-sdk` (screenpipe/sdk repo)// can wrap `start_ffmpeg_process` / `write_frame_to_ffmpeg` /diff --git a/crates/screenpipe-engine/src/resource_monitor.rs b/crates/screenpipe-engine/src/resource_monitor.rsindex 1843d73ef..bcfe5e988 100644--- a/crates/screenpipe-engine/src/resource_monitor.rs+++ b/crates/screenpipe-engine/src/resource_monitor.rs@@ -17,6 +17,7 @@ use tracing::debug;use tracing::trace;use tracing::{error, info, warn};+use crate::resource_summary::ResourceReportWindow;use crate::telemetry_context::TelemetryContext;const RESOURCE_PROCESS_GROUPS: [(&str, &str); 5] = [@@ -525,6 +526,7 @@ impl ResourceTelemetryReporter {snapshot: &ResourceSnapshot,runtime: Duration,mem_trend: Option<&LeakStats>,+ window: &ResourceReportWindow,) {let Some(client) = &self.posthog_client else {return;@@ -532,6 +534,24 @@ impl ResourceTelemetryReporter {// Avoid unnecessary cloning by using referenceslet mut properties = Map::new();+ properties.insert(+ "telemetry_window_samples".to_string(),+ json!(window.samples),+ );+ properties.insert(+ "window_cpu_percent_mean".to_string(),+ json!(window.cpu_mean()),+ );+ properties.insert("window_cpu_percent_max".to_string(), json!(window.cpu_max));+ properties.insert(+ "window_memory_gb_mean".to_string(),+ json!(window.memory_mean()),+ );+ properties.insert("window_memory_gb_max".to_string(), json!(window.memory_max));+ properties.insert(+ "window_phys_footprint_gb_max".to_string(),+ json!(window.footprint_max),+ );properties.insert("distinct_id".to_string(), json!(&self.distinct_id));properties.insert("$lib".to_string(), json!("rust-reqwest"));properties.insert(@@ -743,7 +763,7 @@ impl ResourceTelemetryReporter {Self::log_process_breakdown(&snapshot.process_breakdown);}- async fn log_status(&self, snapshot: &ResourceSnapshot) {+ async fn log_status(&self, snapshot: &ResourceSnapshot, window: &ResourceReportWindow) {let runtime = self.start_time.elapsed();self.log_snapshot(snapshot, runtime);@@ -755,7 +775,7 @@ impl ResourceTelemetryReporter {// Send to PostHog if enabledif self.posthog_enabled {tokio::select! {- _ = self.send_to_posthog(snapshot, runtime, mem_trend.as_ref()) => {},+ _ = self.send_to_posthog(snapshot, runtime, mem_trend.as_ref(), window) => {},_ = tokio::time::sleep(Duration::from_secs(5)) => {warn!("PostHog request timed out");}@@ -773,6 +793,7 @@ impl ResourceTelemetryReporter {let monitor = Arc::clone(self);let posthog_interval = posthog_interval.unwrap_or(interval);let mut last_posthog_update = Instant::now();+ let mut window = ResourceReportWindow::default();tokio::spawn(async move {let mut sampler = ResourceSampler::new();@@ -807,13 +828,18 @@ impl ResourceTelemetryReporter {}unsafe { malloc_trim(0) };}+ let snapshot = sampler.snapshot();+ window.observe(+ f64::from(snapshot.total_cpu_percent),+ snapshot.total_memory_gb,+ snapshot.phys_footprint_gb,+ );let now = Instant::now();let should_send_to_posthog = now.duration_since(last_posthog_update) >= posthog_interval;- let snapshot = sampler.snapshot();if should_send_to_posthog {last_posthog_update = now;- monitor.log_status(&snapshot).await;+ monitor.log_status(&snapshot, &std::mem::take(&mut window)).await;} else {monitor.log_status_local(&snapshot).await;}diff --git a/crates/screenpipe-engine/src/resource_summary.rs b/crates/screenpipe-engine/src/resource_summary.rsnew file mode 100644index 000000000..76dd99ba2--- /dev/null+++ b/crates/screenpipe-engine/src/resource_summary.rs@@ -0,0 +1,84 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com++/// Summarize every local resource sample between remote reports. Legacy fields+/// remain point samples; these additive fields expose peaks and averages between reports.+/// This summary does not replace samples or change the reporting cadence.+#[derive(Default)]+pub(crate) struct ResourceReportWindow {+ pub(crate) samples: u64,+ cpu_sum: f64,+ memory_sum: f64,+ pub(crate) cpu_max: f64,+ pub(crate) memory_max: f64,+ pub(crate) footprint_max: f64,+}++impl ResourceReportWindow {+ pub(crate) fn observe(&mut self, cpu: f64, memory: f64, footprint: f64) {+ self.samples += 1;+ self.cpu_sum += cpu;+ self.memory_sum += memory;+ self.cpu_max = self.cpu_max.max(cpu);+ self.memory_max = self.memory_max.max(memory);+ self.footprint_max = self.footprint_max.max(footprint);+ }++ pub(crate) fn cpu_mean(&self) -> f64 {+ self.cpu_sum / self.samples.max(1) as f64+ }++ pub(crate) fn memory_mean(&self) -> f64 {+ self.memory_sum / self.samples.max(1) as f64+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn window_retains_short_peaks_and_resets_between_reports() {+ let mut window = ResourceReportWindow::default();+ for (cpu, memory, footprint) in [(10.0, 1.0, 2.0), (100.0, 4.0, 5.0), (10.0, 1.0, 2.0)] {+ window.observe(cpu, memory, footprint);+ }+ let report = std::mem::take(&mut window);+ assert_eq!(report.samples, 3);+ assert_eq!(report.cpu_mean(), 40.0);+ assert_eq!(report.memory_mean(), 2.0);+ assert_eq!(report.cpu_max, 100.0);+ assert_eq!(report.memory_max, 4.0);+ assert_eq!(report.footprint_max, 5.0);+ window.observe(5.0, 0.5, 1.0);+ assert_eq!(window.samples, 1);+ assert_eq!(window.cpu_mean(), 5.0);+ assert_eq!(window.memory_max, 0.5);+ }++ #[test]+ fn empty_window_is_finite() {+ let window = ResourceReportWindow::default();+ assert_eq!(window.samples, 0);+ assert_eq!(window.cpu_mean(), 0.0);+ assert_eq!(window.memory_mean(), 0.0);+ }++ #[test]+ fn sample_counts_preserve_weighted_means_across_unequal_windows() {+ let mut short = ResourceReportWindow::default();+ short.observe(100.0, 4.0, 5.0);+ let mut long = ResourceReportWindow::default();+ for _ in 0..3 {+ long.observe(20.0, 2.0, 3.0);+ }+ let count = (short.samples + long.samples) as f64;+ let cpu = (short.cpu_mean() * short.samples as f64 + long.cpu_mean() * long.samples as f64)+ / count;+ let memory = (short.memory_mean() * short.samples as f64+ + long.memory_mean() * long.samples as f64)+ / count;+ assert_eq!(cpu, 40.0);+ assert_eq!(memory, 2.5);+ }+}diff --git a/crates/screenpipe-engine/src/server.rs b/crates/screenpipe-engine/src/server.rsindex 8bbab339a..186e0f8b6 100644--- a/crates/screenpipe-engine/src/server.rs+++ b/crates/screenpipe-engine/src/server.rs@@ -612,6 +612,7 @@ impl SCServer {async fn create_router_inner(&self) -> Router {let api_request_count = Arc::new(AtomicUsize::new(0));let analytics_enabled = analytics::is_enabled();+ let mut telemetry_tasks = crate::telemetry_tasks::TelemetryTasks::default();let api_usage_counter = analytics_enabled.then(|| api_request_count.clone());// Recording-coverage sampler: accumulates working-time-vs-healthy-capture@@ -648,7 +649,7 @@ impl SCServer {// Spawn periodic vision pipeline metrics reporter (every 60 seconds)let metrics_for_posthog = self.vision_metrics.clone();- tokio::spawn(async move {+ telemetry_tasks.spawn(async move {let mut interval = tokio::time::interval(Duration::from_secs(60));loop {interval.tick().await;@@ -703,7 +704,7 @@ impl SCServer {// Spawn periodic audio pipeline metrics reporter (every 60 seconds)let audio_metrics_for_posthog = self.audio_metrics.clone();let audio_manager_for_posthog = self.audio_manager.clone();- tokio::spawn(async move {+ telemetry_tasks.spawn(async move {let mut interval = tokio::time::interval(Duration::from_secs(60));loop {interval.tick().await;@@ -1540,6 +1541,9 @@ impl SCServer {}),).layer(Extension(crate::routes::workflows::WorkflowCatalogSource(self.workflow_catalog_dir.clone())))+ // Router clones keep this generation alive; the last drop cancels+ // its reporters, including on failed startup or server restart.+ .layer(Extension(Arc::new(telemetry_tasks))).with_state(app_state.clone()).layer(axum::middleware::from_fn(crate::search_only::history_only)).layer(axum::middleware::from_fn_with_state(diff --git a/crates/screenpipe-engine/src/telemetry_tasks.rs b/crates/screenpipe-engine/src/telemetry_tasks.rsnew file mode 100644index 000000000..2ae3a2595--- /dev/null+++ b/crates/screenpipe-engine/src/telemetry_tasks.rs@@ -0,0 +1,90 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com++use std::future::Future;+use tokio::task::JoinHandle;+use tokio_util::task::AbortOnDropHandle;++/// Own periodic reporters with their router generation. Dropping a bare+/// JoinHandle detaches its task; these handles cancel it instead. Reporters must+/// not capture their own owner, which would create a lifetime cycle.+#[derive(Default)]+pub(crate) struct TelemetryTasks {+ tasks: Vec<AbortOnDropHandle<()>>,+}++impl TelemetryTasks {+ pub(crate) fn spawn(&mut self, future: impl Future<Output = ()> + Send + 'static) {+ self.track(tokio::spawn(future));+ }++ fn track(&mut self, task: JoinHandle<()>) {+ self.tasks.push(AbortOnDropHandle::new(task));+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use axum::{routing::get, Extension, Router};+ use std::{sync::Arc, time::Duration};+ use tokio::sync::mpsc;++ fn reporter(tasks: &mut TelemetryTasks, id: usize, tx: mpsc::UnboundedSender<usize>) {+ tasks.spawn(async move {+ let mut interval = tokio::time::interval(Duration::from_secs(60));+ loop {+ interval.tick().await;+ if tx.send(id).is_err() {+ return;+ }+ }+ });+ }++ async fn minute() {+ tokio::time::advance(Duration::from_secs(60)).await;+ tokio::task::yield_now().await;+ }++ #[tokio::test(start_paused = true)]+ async fn router_clones_keep_reporters_alive_until_last_drop() {+ let (tx, mut rx) = mpsc::unbounded_channel();+ let mut tasks = TelemetryTasks::default();+ for id in 0..2 {+ reporter(&mut tasks, id, tx.clone());+ }+ drop(tx);+ let router = Router::<()>::new()+ .route("/", get(|| async { "ok" }))+ .layer(Extension(Arc::new(tasks)));+ let connection = router.clone();+ tokio::task::yield_now().await;+ for _ in 0..2 {+ assert!(rx.try_recv().is_ok());+ }+ drop(router);+ minute().await;+ for _ in 0..2 {+ assert!(rx.try_recv().is_ok());+ }+ drop(connection);+ tokio::task::yield_now().await;+ minute().await;+ assert_eq!(rx.try_recv(), Err(mpsc::error::TryRecvError::Disconnected));+ }++ #[tokio::test(start_paused = true)]+ async fn failed_router_construction_cancels_reporters_before_ownership_transfer() {+ let (tx, mut rx) = mpsc::unbounded_channel();+ let mut tasks = TelemetryTasks::default();+ reporter(&mut tasks, 1, tx);+ tokio::task::yield_now().await;+ assert_eq!(rx.try_recv(), Ok(1));+ // A router-build error drops the local guard before any Extension owns it.+ drop(tasks);+ tokio::task::yield_now().await;+ minute().await;+ assert_eq!(rx.try_recv(), Err(mpsc::error::TryRecvError::Disconnected));+ }+}diff --git a/docs/COVERAGE.md b/docs/COVERAGE.mdindex 09837cfd0..e40fcd0c9 100644--- a/docs/COVERAGE.md+++ b/docs/COVERAGE.md@@ -37,16 +37,16 @@ results and `cargo llvm-cov` data on top when judging release confidence.### Core Engine- Mapped suites: 37-- Mapped Rust files: 392-- Active test blocks: 3864+- Mapped Rust files: 394+- Active test blocks: 3869- Ignored/manual test blocks: 181-- Weighted coverage points: 3190.9+- Weighted coverage points: 3194.4| Platform | Suites | Active tests | Ignored tests | Weighted points | Layers | Flows | Critical score || --- | --- | --- | --- | --- | --- | --- | --- |-| windows | 33 | 3716 | 166 | 3123.9 | 21 | 11 | 100% |-| macos | 32 | 3744 | 142 | 3113.2 | 22 | 11 | 100% |-| linux | 29 | 3294 | 141 | 2742.2 | 20 | 11 | 100% |+| windows | 33 | 3721 | 166 | 3127.4 | 21 | 11 | 100% |+| macos | 32 | 3749 | 142 | 3116.7 | 22 | 11 | 100% |+| linux | 29 | 3299 | 141 | 2745.7 | 20 | 11 | 100% |## Refreshdiff --git a/docs/coverage/CORE.md b/docs/coverage/CORE.mdindex af52b51d6..1255e3fec 100644--- a/docs/coverage/CORE.md+++ b/docs/coverage/CORE.md@@ -8,11 +8,11 @@ confidence, and criticality.- Manifest: `docs/coverage/core-engine-map.json`- Tracked crates: screenpipe-engine, screenpipe-db, screenpipe-sqlite-coordinator, screenpipe-audio, screenpipe-screen, screenpipe-a11y, screenpipe-fs- Mapped suites: 37-- Mapped Rust files: 392-- Active test blocks: 3864+- Mapped Rust files: 394+- Active test blocks: 3869- Ignored/manual test blocks: 181-- Declared test blocks: 4045-- Weighted coverage points: 3190.9+- Declared test blocks: 4050+- Weighted coverage points: 3194.4Confidence weights: strong=1.0, partial=0.7, conditional=0.4, smoke=0.3.Criticality weights: high=1.0, medium=0.7, low=0.4.@@ -23,15 +23,15 @@ are explicitly enabled in a runtime lane.| Platform | Suites | Active tests | Ignored tests | Weighted points | Layers | Flows | Critical score || --- | --- | --- | --- | --- | --- | --- | --- |-| windows | 33 | 3716 | 166 | 3123.9 | 21 | 11 | 100% |-| macos | 32 | 3744 | 142 | 3113.2 | 22 | 11 | 100% |-| linux | 29 | 3294 | 141 | 2742.2 | 20 | 11 | 100% |+| windows | 33 | 3721 | 166 | 3127.4 | 21 | 11 | 100% |+| macos | 32 | 3749 | 142 | 3116.7 | 22 | 11 | 100% |+| linux | 29 | 3299 | 141 | 2745.7 | 20 | 11 | 100% |## Crate Summary| Crate | Suites | Integration files | Source unit files | Active tests | Ignored tests | Weighted points | Flows || --- | --- | --- | --- | --- | --- | --- | --- |-| screenpipe-engine | 12 | 22 | 128 | 1851 | 53 | 1426.4 | 10 |+| screenpipe-engine | 12 | 22 | 130 | 1856 | 53 | 1429.9 | 10 || screenpipe-db | 5 | 68 | 27 | 626 | 31 | 596.7 | 9 || screenpipe-sqlite-coordinator | 1 | 0 | 3 | 27 | 0 | 27.0 | 2 || screenpipe-audio | 8 | 29 | 53 | 695 | 53 | 606.9 | 5 |@@ -68,12 +68,12 @@ bun run coverage:core -- --llvm-cov-summary ../../docs/coverage/core-llvm-cov-su| configuration | 2 suites / 153 active / 3 ignored / 138.7 pts | 2 suites / 153 active / 3 ignored / 138.7 pts | 2 suites / 153 active / 3 ignored / 138.7 pts || database | 7 suites / 583 active / 27 ignored / 553.7 pts | 7 suites / 583 active / 27 ignored / 553.7 pts | 7 suites / 583 active / 27 ignored / 553.7 pts || db-search | 2 suites / 119 active / 9 ignored / 119.0 pts | 2 suites / 119 active / 9 ignored / 119.0 pts | 2 suites / 119 active / 9 ignored / 119.0 pts |-| engine-lifecycle | 6 suites / 265 active / 1 ignored / 238.5 pts | 6 suites / 265 active / 1 ignored / 238.5 pts | 6 suites / 261 active / 9 ignored / 237.6 pts |+| engine-lifecycle | 6 suites / 270 active / 1 ignored / 242.0 pts | 6 suites / 270 active / 1 ignored / 242.0 pts | 6 suites / 266 active / 9 ignored / 241.1 pts || local-api | 3 suites / 479 active / 12 ignored / 339.2 pts | 3 suites / 479 active / 12 ignored / 339.2 pts | 3 suites / 479 active / 12 ignored / 339.2 pts || meeting | 6 suites / 1751 active / 32 ignored / 1433.0 pts | 6 suites / 1751 active / 32 ignored / 1433.0 pts | 4 suites / 1397 active / 28 ignored / 1102.7 pts || ocr | 4 suites / 140 active / 7 ignored / 129.8 pts | 4 suites / 133 active / 7 ignored / 127.6 pts | 3 suites / 124 active / 6 ignored / 118.6 pts || os-integration | 1 suites / 6 active / 0 ignored / 1.7 pts | 1 suites / 6 active / 0 ignored / 1.7 pts | - |-| performance | 13 suites / 1677 active / 82 ignored / 1481.6 pts | 14 suites / 1787 active / 87 ignored / 1525.6 pts | 13 suites / 1677 active / 82 ignored / 1481.6 pts |+| performance | 13 suites / 1682 active / 82 ignored / 1485.1 pts | 14 suites / 1792 active / 87 ignored / 1529.1 pts | 13 suites / 1682 active / 82 ignored / 1485.1 pts || pipes | 1 suites / 551 active / 3 ignored / 385.7 pts | 1 suites / 551 active / 3 ignored / 385.7 pts | 1 suites / 551 active / 3 ignored / 385.7 pts || privacy | 5 suites / 1013 active / 42 ignored / 816.8 pts | 5 suites / 1054 active / 18 ignored / 812.5 pts | 5 suites / 971 active / 14 ignored / 781.7 pts || real-app | - | 1 suites / 110 active / 5 ignored / 44.0 pts | - |@@ -146,7 +146,7 @@ bun run coverage:core -- --llvm-cov-summary ../../docs/coverage/core-llvm-cov-su| engine-meeting-privacy-sync | screenpipe-engine | windows, macos, linux | meeting, privacy, ui-events, pipes, sync | meeting-live-notes, privacy-and-redaction, accessibility-ui-events, performance-liveness | medium | strong | unit | 33 | 551 | 3 | Unit-heavy coverage for privacy filter policy, capture exclusions, UI recorder safety, pipes/live-view/structured-output helpers, sync helpers, and CLI parsing. Meeting detection moved to the engine-meeting-watcher suite. || engine-meeting-watcher | screenpipe-engine | windows, macos | meeting | meeting-live-notes | high | strong | mixed | 10 | 275 | 3 | Successor of src/meeting_detector.rs and src/meeting_telemetry.rs. Audio-process and UI-scan backend state machines, candidate resolution, shared telemetry, and a scored trajectory eval. ui_scan/macos.rs and ui_scan/windows.rs are cfg-gated and only execute on their target OS; both backends are null on Linux. || engine-retention-storage | screenpipe-engine | windows, macos, linux | storage, engine-lifecycle, performance | engine-health-lifecycle, performance-liveness | medium | strong | mixed | 5 | 39 | 0 | Local retention deletion (including lean-mode heavy-text stripping), cloud archive watermarking, atomic state-file replacement, and the low-disk capture monitor. |-| engine-telemetry-observability | screenpipe-engine | windows, macos, linux | engine-lifecycle, performance | engine-health-lifecycle, performance-liveness | medium | strong | unit | 6 | 35 | 0 | PostHog capture gating, telemetry context shaping, piggyback telemetry forwarding, crash-log helpers, resource monitoring, and the recording-coverage reliability metric. |+| engine-telemetry-observability | screenpipe-engine | windows, macos, linux | engine-lifecycle, performance | engine-health-lifecycle, performance-liveness | medium | strong | unit | 8 | 40 | 0 | PostHog capture gating, telemetry context shaping, piggyback telemetry forwarding, crash-log helpers, resource monitoring, and the recording-coverage reliability metric. || engine-workflows | screenpipe-engine | windows, macos, linux | local-api, storage | local-api-search | high | partial | mixed | 7 | 36 | 0 | Catalog identity and persistence, revision-scoped feedback, workflow evidence and pipeline contracts, and denial of agent-granted rollout access. Does not assert live cloud discovery quality. Workflow publication orchestration deadlines and nested query backpressure are covered with virtual-time router tests. || filesystem-durable-sync | screenpipe-fs | macos, linux, windows | database, vision-capture, audio | capture-ocr-pipeline, audio-record-transcribe, engine-health-lifecycle | high | strong | unit | 1 | 3 | 0 | Durable flushes preserve local full-sync behavior, fall back to fsync only for macOS ENOTSUP, and propagate real I/O or fallback failures. Real SMB migration and continued recording are covered by the marked-volume integration test. || screen-capture-ocr-contract | screenpipe-screen | windows, macos, linux | vision-capture, ocr | capture-ocr-pipeline | high | partial | unit | 3 | 18 | 0 | Cross-platform cached-OCR unit coverage for RawCaptureResult to CaptureResult metadata, browser URL, focus state, window-to-screen OCR coordinate transformation, Tesseract output parsing, and contour-based text-region detection for the meeting OCR gate. |@@ -449,6 +449,7 @@ bun run coverage:core -- --llvm-cov-summary ../../docs/coverage/core-llvm-cov-su| engine-telemetry-observability | screenpipe-engine | src/recording_coverage.rs | source | 5 | 0 | 5 || engine-config-lifecycle | screenpipe-engine | src/recording_detail.rs | source | 6 | 0 | 6 || engine-telemetry-observability | screenpipe-engine | src/resource_monitor.rs | source | 7 | 0 | 7 |+| engine-telemetry-observability | screenpipe-engine | src/resource_summary.rs | source | 3 | 0 | 3 || engine-retention-storage | screenpipe-engine | src/retention.rs | source | 6 | 0 | 6 || engine-api-routes | screenpipe-engine | src/routes/activity_ledger.rs | source | 5 | 0 | 5 || engine-api-routes | screenpipe-engine | src/routes/activity_summary_hybrid_tests.rs | source | 1 | 1 | 2 |@@ -492,6 +493,7 @@ bun run coverage:core -- --llvm-cov-summary ../../docs/coverage/core-llvm-cov-su| engine-meeting-privacy-sync | screenpipe-engine | src/sync_api.rs | source | 13 | 0 | 13 || engine-meeting-privacy-sync | screenpipe-engine | src/sync_provider.rs | source | 4 | 0 | 4 || engine-telemetry-observability | screenpipe-engine | src/telemetry_context.rs | source | 8 | 0 | 8 |+| engine-telemetry-observability | screenpipe-engine | src/telemetry_tasks.rs | source | 2 | 0 | 2 || engine-meeting-privacy-sync | screenpipe-engine | src/ui_recorder.rs | source | 65 | 0 | 65 || engine-capture-timeline | screenpipe-engine | src/video_cache.rs | source | 4 | 0 | 4 || engine-capture-timeline | screenpipe-engine | src/video_utils.rs | source | 12 | 0 | 12 |diff --git a/docs/coverage/core-engine-map.json b/docs/coverage/core-engine-map.jsonindex 71a88ede7..5f26a63f6 100644--- a/docs/coverage/core-engine-map.json+++ b/docs/coverage/core-engine-map.json@@ -579,11 +579,13 @@"crate": "screenpipe-engine","files": ["src/analytics.rs",+ "src/resource_summary.rs","src/crash_log.rs","src/piggyback_telemetry.rs","src/recording_coverage.rs","src/resource_monitor.rs",- "src/telemetry_context.rs"+ "src/telemetry_context.rs",+ "src/telemetry_tasks.rs"],"platforms": ["windows",diff --git a/packages/screenpipe-mcp/README.md b/packages/screenpipe-mcp/README.mdindex 8e46a6b14..3b7ae41cf 100644--- a/packages/screenpipe-mcp/README.md+++ b/packages/screenpipe-mcp/README.md@@ -49,6 +49,19 @@ existing successful-retrieval path, including cached searches. No new events areadded for cache hits. Health/resource telemetry and local monitoring retain theirexisting cadence.+Resource events additionally report `telemetry_window_samples`,+`window_cpu_percent_mean`, `window_cpu_percent_max`, `window_memory_gb_mean`,+`window_memory_gb_max`, and `window_phys_footprint_gb_max` across local samples+since the previous reporting attempt. These supplement the existing point-in-time+properties. Weight window means by their sample counts when aggregating them.+Transport remains best effort; these summaries are not a durable sample archive.++Cost reductions must preserve successful-retrieval counts and cohort-relative+retention windows. A future reduction in diagnostic cadence needs a validated+replacement for fault timelines, short-session coverage, and resource peaks,+including shutdown and failed-delivery handling. This change does not reduce+PostHog event volume or claim billing savings.+Attribution adds fields to existing events and uses the existing analyticsopt-out. It does not scan running apps or send prompts, search text, or rawclient names. HTTP MCP now reports successful searches through the same