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

feat(enterprise): per-stream sync gate (frames / audio / ui / memories / snapshots) (#3581)

Louis Beaumont5fb2a67932e6
diff --git a/apps/screenpipe-app-tauri/lib/hooks/use-enterprise-policy.ts b/apps/screenpipe-app-tauri/lib/hooks/use-enterprise-policy.ts
index 31f491e37..6afc3822b 100644
--- a/apps/screenpipe-app-tauri/lib/hooks/use-enterprise-policy.ts
+++ b/apps/screenpipe-app-tauri/lib/hooks/use-enterprise-policy.ts
@@ -8,6 +8,7 @@ import { commands } from "@/lib/utils/tauri";
import { fetch as tauriFetch } from "@tauri-apps/plugin-http";
import { getStore } from "./use-settings";
import { getVersion } from "@tauri-apps/api/app";
+import { invoke } from "@tauri-apps/api/core";
import { localFetch } from "@/lib/api";
import { platform as getPlatform } from "@tauri-apps/plugin-os";
@@ -389,6 +390,28 @@ export function useEnterprisePolicy() {
console.warn("[enterprise] failed to push policy to Rust:", e);
}
+ // Push per-stream sync toggles to Rust so the enterprise sync task
+ // gates each upload kind. Defaults to all-true server-side, so an
+ // older server that doesn't return syncStreams ends up here as
+ // undefined → all true (no behavior change). Uses `invoke` directly
+ // because the typed `commands.*` wrapper in lib/utils/tauri.ts is
+ // auto-generated by tauri-specta — adding a new command requires a
+ // tauri build to regenerate it.
+ try {
+ const streams = (data.syncStreams ?? {}) as Record<string, unknown>;
+ const pickBool = (key: string): boolean =>
+ typeof streams[key] === "boolean" ? (streams[key] as boolean) : true;
+ await invoke("set_sync_streams", {
+ frames: pickBool("frames"),
+ audio: pickBool("audio"),
+ uiEvents: pickBool("ui_events"),
+ memories: pickBool("memories"),
+ snapshots: pickBool("snapshots"),
+ });
+ } catch (e) {
+ console.warn("[enterprise] failed to push sync streams to Rust:", e);
+ }
+
// Persist admin status into ~/.screenpipe/enterprise.json so the
// pi-agent can decide whether to install the screenpipe-team skill
// on its next boot. Only meaningful when we sent a cloud token in
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/main.rs b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
index 41a3ffdea..1b97a45a1 100755
--- a/apps/screenpipe-app-tauri/src-tauri/src/main.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
@@ -752,6 +752,7 @@ async fn main() {
commands::get_enterprise_team_api_token,
commands::get_cloud_token,
enterprise_policy::set_enterprise_policy,
+ enterprise_policy::set_sync_streams,
commands::get_disk_usage,
commands::list_cache_files,
commands::delete_cache_files,
@@ -1032,6 +1033,7 @@ async fn main() {
commands::set_cloud_media_analysis_skill,
commands::get_enterprise_license_key,
enterprise_policy::set_enterprise_policy,
+ enterprise_policy::set_sync_streams,
commands::save_enterprise_license_key,
commands::save_enterprise_team_config,
commands::get_enterprise_team_api_token,
diff --git a/ee/desktop-rust/enterprise_policy.rs b/ee/desktop-rust/enterprise_policy.rs
index b1a318df1..d27a761bc 100644
--- a/ee/desktop-rust/enterprise_policy.rs
+++ b/ee/desktop-rust/enterprise_policy.rs
@@ -16,6 +16,34 @@ static HIDDEN_SECTIONS: Lazy<RwLock<HashSet<String>>> = Lazy::new(|| RwLock::new
static DEPLOYMENT_APP_UI_HIDDEN: Lazy<bool> =
Lazy::new(|| env_hides_app_ui() || enterprise_json_hides_app_ui());
+/// Per-stream sync policy. Defaults to all-true so an unconfigured device
+/// behaves exactly like before this feature shipped. The frontend pulls the
+/// admin's choices from `GET /api/enterprise/policy` (`syncStreams` field) on
+/// the 5-min poll and pushes them in via `set_sync_streams`. The sync state
+/// machine in `enterprise_sync::run_one_sync` reads this on every tick.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct SyncStreams {
+ pub frames: bool,
+ pub audio: bool,
+ pub ui_events: bool,
+ pub memories: bool,
+ pub snapshots: bool,
+}
+
+impl Default for SyncStreams {
+ fn default() -> Self {
+ Self {
+ frames: true,
+ audio: true,
+ ui_events: true,
+ memories: true,
+ snapshots: true,
+ }
+ }
+}
+
+static SYNC_STREAMS: Lazy<RwLock<SyncStreams>> = Lazy::new(|| RwLock::new(SyncStreams::default()));
+
const APP_UI_HIDDEN_SECTIONS: &[&str] = &[
"app_ui",
"desktop_app",
@@ -128,6 +156,51 @@ pub fn set_enterprise_policy(hidden_sections: Vec<String>) {
}
}
+/// Called by the frontend after fetching the `syncStreams` block from
+/// `/api/enterprise/policy`. Flat booleans rather than a struct so the
+/// specta-generated TS binding stays trivial.
+#[tauri::command]
+#[specta::specta]
+pub fn set_sync_streams(
+ frames: bool,
+ audio: bool,
+ ui_events: bool,
+ memories: bool,
+ snapshots: bool,
+) {
+ let next = SyncStreams {
+ frames,
+ audio,
+ ui_events,
+ memories,
+ snapshots,
+ };
+ if let Ok(mut guard) = SYNC_STREAMS.write() {
+ if *guard != next {
+ tracing::info!(
+ "enterprise: sync streams updated frames={} audio={} ui={} memories={} snapshots={}",
+ frames,
+ audio,
+ ui_events,
+ memories,
+ snapshots,
+ );
+ }
+ *guard = next;
+ }
+}
+
+/// Snapshot of the current per-stream sync policy. Read by the sync state
+/// machine on every tick. Returns the defaults (all-true) if the lock is
+/// poisoned — fail-open here mirrors the centralized-data master-switch
+/// behavior: the ingest endpoint will still enforce policy server-side.
+pub fn current_sync_streams() -> SyncStreams {
+ SYNC_STREAMS
+ .read()
+ .map(|guard| *guard)
+ .unwrap_or_default()
+}
+
/// Check if a section is hidden by enterprise policy.
/// Used by the tray menu builder.
pub fn is_tray_item_hidden(section_id: &str) -> bool {
@@ -176,4 +249,31 @@ mod tests {
assert!(is_app_ui_hidden());
set_enterprise_policy(vec![]);
}
+
+ #[test]
+ fn sync_streams_default_is_all_true() {
+ // Brand-new process should see all streams enabled — preserves the
+ // behavior of every existing enterprise deployment before this
+ // feature ships.
+ let s = SyncStreams::default();
+ assert!(s.frames);
+ assert!(s.audio);
+ assert!(s.ui_events);
+ assert!(s.memories);
+ assert!(s.snapshots);
+ }
+
+ #[test]
+ fn set_sync_streams_round_trips() {
+ // Touches the global static; reset to defaults after to avoid
+ // poisoning sibling tests that read current_sync_streams.
+ set_sync_streams(false, true, false, true, false);
+ let s = current_sync_streams();
+ assert!(!s.frames);
+ assert!(s.audio);
+ assert!(!s.ui_events);
+ assert!(s.memories);
+ assert!(!s.snapshots);
+ set_sync_streams(true, true, true, true, true);
+ }
}
diff --git a/ee/desktop-rust/enterprise_sync.rs b/ee/desktop-rust/enterprise_sync.rs
index 28a21b6a7..70365a892 100644
--- a/ee/desktop-rust/enterprise_sync.rs
+++ b/ee/desktop-rust/enterprise_sync.rs
@@ -626,47 +626,75 @@ pub async fn run_one_sync(
cursor.last_memory_ts = Some(cutoff.to_rfc3339());
}
- let frames = local
- .fetch_frames_since(cursor.last_frame_ts.as_deref(), PAGE_LIMIT)
- .await?;
- let audio = local
- .fetch_audio_since(cursor.last_audio_ts.as_deref(), PAGE_LIMIT)
- .await?;
+ // Per-stream sync policy is fetched fresh on every tick — the admin can
+ // flip toggles in the dashboard and the device picks them up on the next
+ // 5-min policy poll. A disabled stream means we don't even hit the local
+ // API for its rows; the cursor for that kind stays put, so re-enabling
+ // resumes from where the toggle-off happened (capped by SAFE_BACKFILL
+ // anyway).
+ let streams = crate::enterprise_policy::current_sync_streams();
+
+ let frames = if streams.frames {
+ local
+ .fetch_frames_since(cursor.last_frame_ts.as_deref(), PAGE_LIMIT)
+ .await?
+ } else {
+ Vec::new()
+ };
+ let audio = if streams.audio {
+ local
+ .fetch_audio_since(cursor.last_audio_ts.as_deref(), PAGE_LIMIT)
+ .await?
+ } else {
+ Vec::new()
+ };
// UI events are best-effort — a backend that doesn't expose them yet
// (or blocks the search query) shouldn't kill the whole sync batch.
// The frame + audio paths are the load-bearing ones.
- let ui = match local
- .fetch_ui_events_since(cursor.last_ui_ts.as_deref(), PAGE_LIMIT)
- .await
- {
- Ok(rows) => rows,
- Err(e) => {
- warn!("enterprise sync: ui fetch failed (skipping): {}", e);
- Vec::new()
+ let ui = if streams.ui_events {
+ match local
+ .fetch_ui_events_since(cursor.last_ui_ts.as_deref(), PAGE_LIMIT)
+ .await
+ {
+ Ok(rows) => rows,
+ Err(e) => {
+ warn!("enterprise sync: ui fetch failed (skipping): {}", e);
+ Vec::new()
+ }
}
+ } else {
+ Vec::new()
};
// One snapshot per tick. Best-effort — failure to encode/fetch
// shouldn't block the rest of the batch.
- let snapshots: Vec<SnapshotRow> = match local.fetch_latest_snapshot().await {
- Ok(Some(s)) => vec![s],
- Ok(None) => Vec::new(),
- Err(e) => {
- warn!("enterprise sync: snapshot fetch failed (skipping): {}", e);
- Vec::new()
+ let snapshots: Vec<SnapshotRow> = if streams.snapshots {
+ match local.fetch_latest_snapshot().await {
+ Ok(Some(s)) => vec![s],
+ Ok(None) => Vec::new(),
+ Err(e) => {
+ warn!("enterprise sync: snapshot fetch failed (skipping): {}", e);
+ Vec::new()
+ }
}
+ } else {
+ Vec::new()
};
// Memories are best-effort too — a client that predates the trait
// method, or a server without the /memories route, must not kill
// the frame+audio path. The default trait impl returns empty.
- let memories = match local
- .fetch_memories_since(cursor.last_memory_ts.as_deref(), PAGE_LIMIT)
- .await
- {
- Ok(rows) => rows,
- Err(e) => {
- warn!("enterprise sync: memory fetch failed (skipping): {}", e);
- Vec::new()
+ let memories = if streams.memories {
+ match local
+ .fetch_memories_since(cursor.last_memory_ts.as_deref(), PAGE_LIMIT)
+ .await
+ {
+ Ok(rows) => rows,
+ Err(e) => {
+ warn!("enterprise sync: memory fetch failed (skipping): {}", e);
+ Vec::new()
+ }
}
+ } else {
+ Vec::new()
};
if frames.is_empty()