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
Stop warm-monitor capture streams when recording pauses (#7133)
Ezra Ellette376f21d2b89e
diff --git a/apps/screenpipe-app-tauri/e2e/helpers/app-launcher.ts b/apps/screenpipe-app-tauri/e2e/helpers/app-launcher.tsindex 7baf90fff..8c153f723 100644--- a/apps/screenpipe-app-tauri/e2e/helpers/app-launcher.ts+++ b/apps/screenpipe-app-tauri/e2e/helpers/app-launcher.ts@@ -151,6 +151,10 @@ export const E2E_BUN_PATH = resolve();export function getAppPath(): string {+ // Disposable macOS guests run the same E2E binary inside a TCC-granted app.+ if (process.env.SCREENPIPE_E2E_APP_PATH) {+ return resolve(process.env.SCREENPIPE_E2E_APP_PATH);+ }const base = resolve(APP_ROOT, 'src-tauri/target/debug-dev');const name = process.platform === 'win32' ? 'screenpipe-app.exe' : 'screenpipe-app';return resolve(base, name);diff --git a/apps/screenpipe-app-tauri/e2e/specs/focus-warm-pause.spec.ts b/apps/screenpipe-app-tauri/e2e/specs/focus-warm-pause.spec.tsnew file mode 100644index 000000000..de974e174--- /dev/null+++ b/apps/screenpipe-app-tauri/e2e/specs/focus-warm-pause.spec.ts@@ -0,0 +1,106 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com++import { readdirSync, readFileSync } from "node:fs";+import { resolve } from "node:path";+import { E2E_DATA_DIR, E2E_SEED_FLAGS, getAppPid } from "../helpers/app-launcher.js";+import { authHeaders, fetchJson, getLocalApiConfig } from "../helpers/api-utils.js";+import { spawnTransientForegroundApp } from "../helpers/seed-capture-activity.js";+import { waitForAppReady } from "../helpers/test-utils.js";++type StreamSample = {+ second: number;+ phase: "warm" | "paused" | "resumed";+ stream_sequence: number | null;+};++function samples(): StreamSample[][] {+ return readdirSync(E2E_DATA_DIR)+ .filter((name) => /^e2e-warm-pause-\d+\.jsonl$/.test(name))+ .map((name) => readFileSync(resolve(E2E_DATA_DIR, name), "utf8")+ .split("\n").slice(0, -1).filter(Boolean).map((line) => JSON.parse(line)))+ .filter((rows) => rows.length > 0);+}++// Run only in a disposable macOS guest with Screen Recording permission:+// SCREENPIPE_E2E_SEED=onboarding,no-audio,focus-warm-pause bun run test:e2e -- --spec e2e/specs/focus-warm-pause.spec.ts+// The seed supplies Warm focus + FullPause inputs. Stream creation, release,+// image capture, and persistence remain the real production implementation.+describe("pause while a display is focus-warm", function () {+ this.timeout(180_000);+ let cleanup: (() => void) | undefined;+ afterEach(() => cleanup?.());++ it("releases the OS stream for the whole pause and records again afterward", async function () {+ if (process.platform !== "darwin" || !E2E_SEED_FLAGS.split(",").includes("focus-warm-pause")) this.skip();+ await waitForAppReady();+ await browser.switchToWindow("home");+ // Authentication is outside this capture regression. Use the existing+ // E2E account event so settings and the native token follow their normal+ // update path, with production account refresh explicitly disabled.+ await browser.execute(() => {+ localStorage.setItem("screenpipe_e2e_account_fixture_active", "1");+ localStorage.setItem("screenpipe_e2e_account_user", JSON.stringify({+ id: "e2e-warm-pause",+ email: "[email protected]",+ token: "e2e-fake-token-warm-pause",+ subscription_plan: "free",+ __e2eSkipAccountRefresh: true,+ }));+ window.dispatchEvent(new Event("screenpipe-e2e-seed-account-user"));+ });+ const pid = getAppPid();+ expect(pid).not.toBeNull();+ const cfg = await getLocalApiConfig();+ const health = async () => {+ const response = await fetchJson(`http://127.0.0.1:${cfg.port}/health`, authHeaders(cfg.key));+ expect(response.ok).toBe(true);+ return response.body as { pipeline: { frames_db_written: number; capture_loop_heartbeats: number } };+ };+ await browser.waitUntil(() => samples().some((rows) => rows.at(-1)!.second >= 35), {+ timeout: 90_000, interval: 250,+ timeoutMsg: "real Warm/pause capture receipt never arrived",+ });+ const paused = await health();+ await browser.waitUntil(() => samples().every((rows) => rows.at(-1)!.second >= 55), {+ timeout: 30_000, interval: 250,+ });+ const held = await health();+ expect(held.pipeline.frames_db_written).toBe(paused.pipeline.frames_db_written);+ expect(held.pipeline.capture_loop_heartbeats).toBeGreaterThan(paused.pipeline.capture_loop_heartbeats);+ await browser.waitUntil(() => samples().every((rows) => rows.at(-1)!.second >= 65), {+ timeout: 45_000, interval: 250,+ });+ cleanup = spawnTransientForegroundApp();+ await browser.waitUntil(() => samples().every((rows) => rows.at(-1)!.second >= 85), {+ timeout: 30_000, interval: 250,+ });+ const resumed = await health();+ console.log("Warm/pause capture health", {+ pausedFrames: paused.pipeline.frames_db_written,+ heldFrames: held.pipeline.frames_db_written,+ resumedFrames: resumed.pipeline.frames_db_written,+ pausedHeartbeats: paused.pipeline.capture_loop_heartbeats,+ heldHeartbeats: held.pipeline.capture_loop_heartbeats,+ resumedHeartbeats: resumed.pipeline.capture_loop_heartbeats,+ });+ for (const rows of samples()) {+ expect(rows.filter((row) => row.second >= 5 && row.second < 20)+ .some((row) => row.stream_sequence !== null)).toBe(true);+ // Allow native stop completion, then span several five-second Warm+ // probes: merely releasing once must not let a later probe reopen it.+ const held = rows.filter((row) => row.second >= 30 && row.second < 60);+ expect(held.length).toBe(30);+ expect(held.filter((row) => row.stream_sequence !== null)).toEqual([]);+ const resumed = rows.filter((row) => row.second >= 65 && row.stream_sequence !== null);+ expect(resumed.length).toBeGreaterThan(10);+ expect(resumed.at(-1)!.stream_sequence).toBeGreaterThan(resumed[0].stream_sequence!);+ }+ expect(resumed.pipeline.frames_db_written).toBeGreaterThan(paused.pipeline.frames_db_written);+ expect(resumed.pipeline.capture_loop_heartbeats).toBeGreaterThan(paused.pipeline.capture_loop_heartbeats);+ expect(getAppPid()).toBe(pid);+ const log = readFileSync(resolve(E2E_DATA_DIR, "app.log"), "utf8");+ expect(log).toContain("power_paused=true, drm=false, schedule=false); releasing capture stream before focus probes");+ expect(log).toContain("exiting pause state, capture resumes");+ });+});diff --git a/apps/screenpipe-app-tauri/src-tauri/src/diagnostic_logs.rs b/apps/screenpipe-app-tauri/src-tauri/src/diagnostic_logs.rsindex f3b8cf8dc..7b5eb158a 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/diagnostic_logs.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/diagnostic_logs.rs@@ -218,6 +218,28 @@ async fn build_bundle(files: &[LogFile]) -> String {#[cfg(test)]mod tests {use super::*;++ #[tokio::test]+ async fn capture_pause_cause_and_resume_survive_support_collection() {+ // The native focus-warm-pause E2E also asserts these actual messages.+ let dir = tempfile::tempdir().unwrap();+ let current = dir.path().join("screenpipe-app.2026-09-19.log");+ tokio::fs::write(¤t,+ "INFO monitor 1: entering pause state (locked=false, power_paused=true, drm=false, schedule=false); releasing capture stream before focus probes\n"+ ).await.unwrap();+ tokio::fs::rename(¤t, dir.path().join("screenpipe-app.2026-09-19.1.log"))+ .await+ .unwrap();+ tokio::fs::write(¤t,+ "INFO monitor 1: exiting pause state, capture resumes\nINFO [email protected]\n"+ ).await.unwrap();+ let files = crate::log_files::collect_log_files(&[dir.path().to_path_buf()]).await;+ let report = redact_files(&owned_log_files(files)).await.unwrap();+ assert!(report.contains("power_paused=true, drm=false, schedule=false"));+ assert!(report.contains("releasing capture stream before focus probes"));+ assert!(report.contains("exiting pause state, capture resumes"));+ assert!(!report.contains("[email protected]"));+ }use tempfile::tempdir;fn log_file(path: &Path, modified_at: u64) -> LogFile {diff --git a/crates/screenpipe-engine/src/event_driven_capture.rs b/crates/screenpipe-engine/src/event_driven_capture.rsindex 40206a1e1..733159692 100644--- a/crates/screenpipe-engine/src/event_driven_capture.rs+++ b/crates/screenpipe-engine/src/event_driven_capture.rs@@ -117,6 +117,59 @@ fn e2e_force_focus_cold(monitor_id: u32) -> bool {enabled}+/// Exercise the real Warm capture path and OS stream without a second display.+/// Only the focus and power inputs are injected; capture, release, and resume+/// all run through the production loop. The receipt records the native stream+/// sequence, including `None` after release, for the full-app regression.+#[cfg(all(debug_assertions, target_os = "macos"))]+fn e2e_warm_pause_profile(monitor: Arc<SafeMonitor>) -> Option<watch::Receiver<PowerProfile>> {+ let enabled = std::env::var("SCREENPIPE_E2E_SEED")+ .ok()+ .is_some_and(|seeds| {+ seeds+ .split(',')+ .any(|seed| seed.trim() == "focus-warm-pause")+ });+ if !enabled {+ return None;+ }+ let dir = std::env::var("SCREENPIPE_DATA_DIR").ok()?;+ let (tx, rx) = watch::channel(PowerProfile::performance());+ tokio::spawn(async move {+ use std::io::Write;+ let path =+ std::path::Path::new(&dir).join(format!("e2e-warm-pause-{}.jsonl", monitor.id()));+ let Ok(mut receipt) = std::fs::File::create(path) else {+ return;+ };+ for second in 0..90 {+ let phase = match second {+ 0..20 => "warm",+ 20..60 => "paused",+ _ => "resumed",+ };+ if second == 20 {+ info!("e2e: focus-warm-pause entering FullPause");+ let _ = tx.send(PowerProfile::full_pause());+ } else if second == 60 {+ info!("e2e: focus-warm-pause resuming capture");+ let _ = tx.send(PowerProfile::performance());+ }+ let _ = writeln!(+ receipt,+ "{}",+ serde_json::json!({+ "second": second,+ "phase": phase,+ "stream_sequence": monitor.last_capture_seq(),+ })+ );+ tokio::time::sleep(Duration::from_secs(1)).await;+ }+ });+ Some(rx)+}+/// Park every initial monitor loop after the pipeline has proved it can both/// attempt and reach a healthy terminal outcome (persist, dedup, or an explicit/// corrupt-frame skip). The tasks stay cancellable so VisionManager::stop can@@ -1236,6 +1289,14 @@ pub(crate) async fn event_driven_capture_loop(// pausing for battery / lock-screen / DRM reasons.let mut was_in_pause_state = false;+ #[cfg(all(debug_assertions, target_os = "macos"))]+ let e2e_warm_pause = if let Some(rx) = e2e_warm_pause_profile(monitor.clone()) {+ power_profile_rx = Some(rx);+ true+ } else {+ false+ };+loop {if stop_signal.load(Ordering::Relaxed) {info!("event-driven capture stopping for monitor {}", monitor_id);@@ -1256,6 +1317,63 @@ pub(crate) async fn event_driven_capture_loop(#[cfg(debug_assertions)]e2e_park_capture_loop_once(&vision_metrics, monitor_id).await;+ // Pause before focus gating: Warm probes can reopen an OS stream even+ // when they skip normal capture. Lock, power, DRM, and schedule pauses+ // must stop both the reader and WindowServer / replayd frame production.+ record_loop_stage(+ &vision_metrics,+ &monitor_liveness,+ screenpipe_screen::CaptureLoopStage::PauseGate,+ );+ let in_pause_state = crate::sleep_monitor::screen_is_locked()+ || power_profile_rx+ .as_ref()+ .map(|rx| rx.borrow().capture_paused)+ .unwrap_or(false)+ || crate::drm_detector::drm_content_paused()+ || crate::schedule_monitor::schedule_paused();++ if in_pause_state {+ if should_release_on_pause_entry(was_in_pause_state, in_pause_state) {+ info!(+ "monitor {}: entering pause state (locked={}, power_paused={}, drm={}, schedule={}); releasing capture stream before focus probes",+ monitor_id,+ crate::sleep_monitor::screen_is_locked(),+ power_profile_rx+ .as_ref()+ .map(|rx| rx.borrow().capture_paused)+ .unwrap_or(false),+ crate::drm_detector::drm_content_paused(),+ crate::schedule_monitor::schedule_paused(),+ );+ monitor.release_capture_stream();+ }+ was_in_pause_state = true;+ // Drain triggers that piled up while paused so the linker+ // doesn't hold their corr_ids for the full 60s TTL. The+ // recorder keeps emitting events through every pause state+ // (a11y observer is independent of capture), so without this+ // drain a multi-minute pause overflows the broadcast buffer+ // and the dropped ids show up as misleading "stale entries"+ // WARNs later.+ let drained = drain_pending_corr_ids(&mut trigger_rx);+ if !drained.is_empty() {+ report_triggers_dropped(+ linker_tx.as_ref(),+ drained,+ crate::frame_linker::DropReason::Paused,+ );+ }+ tokio::time::sleep(poll_interval).await;+ continue;+ } else if was_in_pause_state {+ info!(+ "monitor {}: exiting pause state, capture resumes",+ monitor_id+ );+ was_in_pause_state = false;+ }+// Focus-aware gating — always on. Skips or pauses capture on// non-focused monitors. If focus resolution fails on this platform// (Linux Wayland, permission denied, etc.) the controller's@@ -1282,6 +1400,10 @@ pub(crate) async fn event_driven_capture_loop(if e2e_force_focus_cold(monitor_id) {capture_state = CaptureState::Cold;}+ #[cfg(all(debug_assertions, target_os = "macos"))]+ if e2e_warm_pause {+ capture_state = CaptureState::Warm;+ }// Fires exactly once per focus-away transition, not every Cold// loop iteration, so the log line is meaningful and we don't@@ -1412,67 +1534,6 @@ pub(crate) async fn event_driven_capture_loop(}}- // Unified pause-state gate: when the screen is locked, the power- // profile says FullPause, DRM is on screen, or we're outside the- // user's capture schedule, we both skip downstream work AND release- // the OS-level capture handle. Otherwise WindowServer / replayd keep- // composing + delivering frames at the stream's frame interval into a- // sleeping reader for the entire pause window — the exact cost the- // user expected `capture_paused` to eliminate.- record_loop_stage(- &vision_metrics,- &monitor_liveness,- screenpipe_screen::CaptureLoopStage::PauseGate,- );- let in_pause_state = crate::sleep_monitor::screen_is_locked()- || power_profile_rx- .as_ref()- .map(|rx| rx.borrow().capture_paused)- .unwrap_or(false)- || crate::drm_detector::drm_content_paused()- || crate::schedule_monitor::schedule_paused();-- if in_pause_state {- if should_release_on_pause_entry(was_in_pause_state, in_pause_state) {- info!(- "monitor {}: entering pause state (locked={}, power_paused={}, drm={}, schedule={}); releasing capture stream",- monitor_id,- crate::sleep_monitor::screen_is_locked(),- power_profile_rx- .as_ref()- .map(|rx| rx.borrow().capture_paused)- .unwrap_or(false),- crate::drm_detector::drm_content_paused(),- crate::schedule_monitor::schedule_paused(),- );- monitor.release_capture_stream();- }- was_in_pause_state = true;- // Drain triggers that piled up while paused so the linker- // doesn't hold their corr_ids for the full 60s TTL. The- // recorder keeps emitting events through every pause state- // (a11y observer is independent of capture), so without this- // drain a multi-minute pause overflows the broadcast buffer- // and the dropped ids show up as misleading "stale entries"- // WARNs later.- let drained = drain_pending_corr_ids(&mut trigger_rx);- if !drained.is_empty() {- report_triggers_dropped(- linker_tx.as_ref(),- drained,- crate::frame_linker::DropReason::Paused,- );- }- tokio::time::sleep(poll_interval).await;- continue;- } else if was_in_pause_state {- info!(- "monitor {}: exiting pause state, capture resumes",- monitor_id- );- was_in_pause_state = false;- }-// After unlock or wake, invalidate persistent SCStream handles so// the next capture picks up fresh frames instead of stale ones.// Use spawn_blocking to avoid blocking the tokio thread — the