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(enterprise): preserve certified survivors after record revocation (#7762)

Ezra Ellette97d864c5c90d
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/diagnostic_logs.rs b/apps/screenpipe-app-tauri/src-tauri/src/diagnostic_logs.rs
index d43d56837..4d192ac1e 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/diagnostic_logs.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/diagnostic_logs.rs
@@ -579,6 +579,31 @@ mod tests {
assert!(!report.contains("[email protected]"));
}
+ #[cfg(feature = "enterprise-support")]
+ #[tokio::test(flavor = "current_thread")]
+ async fn protected_upload_revocation_survives_collected_support_redaction() {
+ let dir = tempfile::tempdir().unwrap();
+ crate::enterprise_sync::check_protected_upload_revocations(dir.path()).await;
+ let path = dir.path().join("screenpipe-app.2026-10-23.log");
+ std::fs::rename(&path, dir.path().join("screenpipe-app.2026-10-23.1.log")).unwrap();
+ std::fs::write(
+ path,
+ "INFO recorder restarted [email protected]\n",
+ )
+ .unwrap();
+ let report = collect_redacted_from_dirs(&[dir.path().to_path_buf()])
+ .await
+ .unwrap();
+ assert!(report.contains("privacy revision changed"));
+ assert!(report.contains("certified survivors retained; stale selections excluded"));
+ assert!(report.contains("selected_records="));
+ assert!(report.contains("retained_records="));
+ assert!(report.contains("recorder restarted"));
+ assert!(!report.contains("[email protected]"));
+ assert!(!report.contains("PRIVATE_ORIGINAL"));
+ assert!(!report.contains("UNPROCESSED_PRIVATE_EDIT"));
+ }
+
#[tokio::test]
async fn installer_failure_reaches_redacted_support_bundle() {
let dir = tempfile::tempdir().unwrap();
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/enterprise/export_filter.rs b/apps/screenpipe-app-tauri/src-tauri/src/enterprise/export_filter.rs
index 1066d93be..e51071497 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/enterprise/export_filter.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/enterprise/export_filter.rs
@@ -166,6 +166,106 @@ pub struct ExportRows<'a> {
}
impl ExportRows<'_> {
+ pub fn record_count(&self) -> usize {
+ self.frames.len()
+ + self.audio.len()
+ + self.ui.len()
+ + self.snapshots.len()
+ + self.memories.len()
+ + self.parsed.len()
+ + self.activities.len()
+ + self.starred.len()
+ + self.feedback.len()
+ }
+
+ /// Read current certified copies by selected ID. Revoked rows stay in the
+ /// processing queue; a later completion has its own delivery sequence.
+ pub async fn refresh_protected(
+ &mut self,
+ db: &std::sync::Arc<screenpipe_db::DatabaseManager>,
+ local: &dyn LocalApiClient,
+ ) -> Result<(), EnterpriseSyncError> {
+ async fn refresh<T: serde::de::DeserializeOwned + Clone>(
+ rows: &mut Vec<T>,
+ db: &std::sync::Arc<screenpipe_db::DatabaseManager>,
+ kind: &str,
+ key: impl Fn(&T) -> (String, String),
+ ) -> Result<(), EnterpriseSyncError> {
+ if rows.is_empty() {
+ return Ok(());
+ }
+ let ids = rows.iter().map(|row| key(row).0).collect::<Vec<_>>();
+ let current = db
+ .refresh_release_export_selection(kind, &ids)
+ .await
+ .map_err(|e| EnterpriseSyncError::Storage {
+ operation: "refresh protected export",
+ cause: e.to_string(),
+ })?;
+ let current: Vec<T> = serde_json::from_value(current).map_err(|e| {
+ EnterpriseSyncError::LocalApi(format!("decode protected export: {e}"))
+ })?;
+ let current = current
+ .into_iter()
+ .map(|row| (key(&row), row))
+ .collect::<std::collections::HashMap<_, _>>();
+ rows.retain_mut(|row| match current.get(&key(row)) {
+ Some(fresh) => {
+ *row = fresh.clone();
+ true
+ }
+ None => false,
+ });
+ Ok(())
+ }
+ // A changed time belongs to a later completion/eligible range scan.
+ // Do not move an already selected ID across its enrollment boundary.
+ refresh(self.frames, db, "frames", |r| {
+ (r.frame_id.to_string(), r.timestamp.clone())
+ })
+ .await?;
+ refresh(self.audio, db, "audio", |r| {
+ (r.transcription_id.to_string(), r.timestamp.clone())
+ })
+ .await?;
+ refresh(self.ui, db, "input", |r| {
+ (r.event_id.to_string(), r.timestamp.clone())
+ })
+ .await?;
+ refresh(self.memories, db, "memory", |r| {
+ (r.memory_id.to_string(), r.created_at.clone())
+ })
+ .await?;
+ refresh(self.parsed, db, "parsed", |r| {
+ (r.frame_id.to_string(), r.timestamp.clone())
+ })
+ .await?;
+ refresh(self.activities, db, "activities", |r| {
+ (r.activity_id.clone(), r.timestamp.clone())
+ })
+ .await?;
+ refresh(self.feedback, db, "feedback", |r| {
+ (r.feedback_id.clone(), r.updated_at.clone())
+ })
+ .await?;
+ // Image reads already enforce their independent receipt and original
+ // frame identity. Starred intervals carry structural fields only.
+ ExportRows {
+ frames: &mut vec![],
+ audio: &mut vec![],
+ ui: &mut vec![],
+ snapshots: self.snapshots,
+ memories: &mut vec![],
+ parsed: &mut vec![],
+ activities: &mut vec![],
+ starred: self.starred,
+ feedback: &mut vec![],
+ }
+ .refresh(local)
+ .await?;
+ Ok(())
+ }
+
pub async fn refresh(
&mut self,
local: &dyn LocalApiClient,
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/enterprise/sync.rs b/apps/screenpipe-app-tauri/src-tauri/src/enterprise/sync.rs
index 704de247c..f9f98bfd7 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/enterprise/sync.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/enterprise/sync.rs
@@ -1284,26 +1284,6 @@ async fn run_one_sync_page(
checkpoint.sequence = page.sequence;
ready.insert(kind, page.rows);
}
- if let Some(request) = backfill {
- let kinds = protected_streams
- .iter()
- .filter(|(_, enabled)| *enabled)
- .map(|(kind, _)| (*kind).to_string())
- .collect::<Vec<_>>();
- let after = privacy_next
- .iter()
- .map(|(kind, cursor)| (kind.clone(), cursor.sequence))
- .collect();
- privacy_more |= local
- .release_replay_pending(
- &request.id,
- &kinds,
- &request.start_at.to_rfc3339(),
- &request.end_at.to_rfc3339(),
- &after,
- )
- .await?;
- }
}
let mut pending_read = false;
let starred_since = cursor.boundary.starred_since.clone().unwrap_or_else(|| {
@@ -1603,8 +1583,27 @@ async fn run_one_sync_page(
{
if protected {
if let Some(token) = &export {
+ token
+ .filter(
+ &mut ExportRows {
+ frames: &mut frames,
+ audio: &mut audio,
+ ui: &mut ui,
+ snapshots: &mut snapshots,
+ memories: &mut memories,
+ parsed: &mut parsed,
+ activities: &mut activities,
+ starred: &mut starred,
+ feedback: &mut feedback,
+ },
+ local,
+ )
+ .await?;
drop(token.admit().await?);
}
+ privacy_more |=
+ protected_replay_pending(local, backfill, &protected_streams, &privacy_next)
+ .await?;
cursor.privacy = privacy_next;
cursor.save(&cfg.cursor_path)?;
local.acknowledge_release_exports(&cursor.privacy).await?;
@@ -1913,6 +1912,14 @@ async fn run_one_sync_page(
}
}
+ // Per-request filtering can exclude a record revoked during an earlier
+ // HTTP wait. Keep its replay active until processing and delivery finish.
+ if protected {
+ privacy_more |=
+ protected_replay_pending(local, backfill, &protected_streams, &next_cursor.privacy)
+ .await?;
+ }
+
// Advance cursor only on success — partial failure must not skip records.
*cursor = next_cursor;
// The backfill caller commits an image-only cursor with its parent (or
@@ -1928,6 +1935,35 @@ async fn run_one_sync_page(
Ok(report)
}
+async fn protected_replay_pending(
+ local: &dyn LocalApiClient,
+ request: Option<&backfill::BackfillRequest>,
+ streams: &[(&str, bool)],
+ cursors: &std::collections::BTreeMap<String, PrivacyCursor>,
+) -> Result<bool, EnterpriseSyncError> {
+ let Some(request) = request else {
+ return Ok(false);
+ };
+ let kinds = streams
+ .iter()
+ .filter(|(_, enabled)| *enabled)
+ .map(|(kind, _)| (*kind).to_string())
+ .collect::<Vec<_>>();
+ let after = cursors
+ .iter()
+ .map(|(kind, cursor)| (kind.clone(), cursor.sequence))
+ .collect();
+ local
+ .release_replay_pending(
+ &request.id,
+ &kinds,
+ &request.start_at.to_rfc3339(),
+ &request.end_at.to_rfc3339(),
+ &after,
+ )
+ .await
+}
+
fn advance_timestamp_boundary<T>(
cursor_ts: &mut Option<String>,
boundary_offset: &mut u32,
@@ -5076,7 +5112,7 @@ pub(crate) mod tests {
/// Restore the process-global stream policy even when an intentionally
/// red regression test panics on its assertion.
- pub(super) struct RestoreDefaultSyncStreams;
+ pub(crate) struct RestoreDefaultSyncStreams;
impl Drop for RestoreDefaultSyncStreams {
fn drop(&mut self) {
@@ -5185,6 +5221,19 @@ pub(crate) mod tests {
}
}
+ pub(crate) async fn requested_replay_page(
+ cfg: &EnterpriseSyncConfig,
+ cursor: &mut Cursor,
+ local: &dyn LocalApiClient,
+ http: &reqwest::Client,
+ request: serde_json::Value,
+ ) -> Result<(SyncTickReport, bool), EnterpriseSyncError> {
+ let request: backfill::BackfillRequest = serde_json::from_value(request).unwrap();
+ let page = backfill::run_replay_page(cfg, cursor, local, http, &request).await?;
+ let more = page.may_have_more();
+ Ok((page, more))
+ }
+
pub(crate) fn test_cfg(dir: &TempDir, ingest_url: String) -> EnterpriseSyncConfig {
EnterpriseSyncConfig {
stable_device_id: None,
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/enterprise_sync.rs b/apps/screenpipe-app-tauri/src-tauri/src/enterprise_sync.rs
index 87a003d96..13c5ce19e 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/enterprise_sync.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/enterprise_sync.rs
@@ -211,8 +211,11 @@ mod imp {
struct StorageExport {
db: Arc<screenpipe_db::DatabaseManager>,
- token: screenpipe_db::storage::StorageReadToken,
- activities: Option<(tauri::AppHandle, std::collections::BTreeMap<String, String>)>,
+ token: tokio::sync::Mutex<screenpipe_db::storage::StorageReadToken>,
+ policy_generation: Option<i64>,
+ refresh_cause: std::sync::Mutex<Option<String>>,
+ activities: Option<tauri::AppHandle>,
+ selected_activities: std::sync::Mutex<std::collections::BTreeMap<String, String>>,
}
impl StorageExport {
@@ -221,21 +224,14 @@ mod imp {
app: tauri::AppHandle,
) -> Result<Self, EnterpriseSyncError> {
let mut export = Self::begin_storage(db.clone()).await?;
- let activities = if crate::enterprise_policy::current_sync_streams().activities
+ if crate::enterprise_policy::current_sync_streams().activities
&& db
.release_policy()
.await
.map_err(|e| EnterpriseSyncError::LocalApi(e.to_string()))?
.text
{
- db.activity_export_versions()
- .await
- .map_err(|e| EnterpriseSyncError::LocalApi(e.to_string()))?
- } else {
- Default::default()
- };
- if !activities.is_empty() {
- export.activities = Some((app, activities));
+ export.activities = Some(app);
}
Ok(export)
}
@@ -250,10 +246,19 @@ mod imp {
operation: "prepare export",
cause: error.to_string(),
})?;
+ let policy_generation = db.release_policy_generation().await.map_err(|error| {
+ EnterpriseSyncError::Storage {
+ operation: "prepare export policy",
+ cause: error.to_string(),
+ }
+ })?;
Ok(Self {
db,
- token,
+ token: tokio::sync::Mutex::new(token),
+ policy_generation,
+ refresh_cause: Default::default(),
activities: None,
+ selected_activities: Default::default(),
})
}
}
@@ -261,7 +266,12 @@ mod imp {
#[async_trait::async_trait]
impl crate::enterprise::sync::ExportFilter for StorageExport {
async fn admit(&self) -> Result<Option<Box<dyn Send>>, EnterpriseSyncError> {
- let history = if let Some((app, activities)) = &self.activities {
+ let selected_activities = self.selected_activities.lock().unwrap().clone();
+ let history = if let Some(app) = self
+ .activities
+ .as_ref()
+ .filter(|_| !selected_activities.is_empty())
+ {
let guard = app
.state::<crate::activity_history::ActivityHistoryState>()
.export_lock
@@ -275,18 +285,25 @@ mod imp {
.map(serde_json::to_value)
.collect::<Result<Vec<_>, _>>()
.map_err(|e| EnterpriseSyncError::LocalApi(e.to_string()))?;
- screenpipe_db::privacy_exports::validate_activity_exports(activities, &rows)
- .map_err(|e| EnterpriseSyncError::Configuration(e.to_string()))?;
+ screenpipe_db::privacy_exports::validate_activity_exports(
+ &selected_activities,
+ &rows,
+ )
+ .map_err(|e| EnterpriseSyncError::Configuration(e.to_string()))?;
Some(guard)
} else {
None
};
- let storage = self.token.admit(&self.db.pool).await.map_err(|error| {
- EnterpriseSyncError::Storage {
+ let storage = self
+ .token
+ .lock()
+ .await
+ .admit(&self.db.pool)
+ .await
+ .map_err(|error| EnterpriseSyncError::Storage {
operation: "admit export",
cause: error.to_string(),
- }
- })?;
+ })?;
Ok(Some(Box::new((storage, history))))
}
@@ -296,11 +313,117 @@ mod imp {
local: &dyn LocalApiClient,
) -> Result<ee_sync::ExportFilterReport, EnterpriseSyncError> {
if local.privacy_release_required().await? {
- drop(self.admit().await?);
+ let generation = self.db.release_policy_generation().await.map_err(|e| {
+ EnterpriseSyncError::Storage {
+ operation: "validate export policy",
+ cause: e.to_string(),
+ }
+ })?;
+ if generation != self.policy_generation {
+ return Err(EnterpriseSyncError::Configuration(
+ "privacy policy changed during export; retry pending".into(),
+ ));
+ }
+ // Activity sources live outside SQLite. Scope their validation
+ // to this request, retaining only unchanged certified versions.
+ self.selected_activities.lock().unwrap().clear();
+ if let Some(app) = self
+ .activities
+ .as_ref()
+ .filter(|_| !rows.activities.is_empty())
+ {
+ let history = app.state::<crate::activity_history::ActivityHistoryState>();
+ let _guard = history.export_lock.lock().await;
+ let staged = self.db.activity_export_versions().await.map_err(|e| {
+ EnterpriseSyncError::Storage {
+ operation: "read certified activity versions",
+ cause: e.to_string(),
+ }
+ })?;
+ let current = crate::activity_history::export_rows(app)
+ .map_err(EnterpriseSyncError::LocalApi)?;
+ let mut versions = std::collections::BTreeMap::new();
+ for row in current {
+ let value = serde_json::to_value(row)
+ .map_err(|e| EnterpriseSyncError::LocalApi(e.to_string()))?;
+ let id = value["activity_id"].as_str().ok_or_else(|| {
+ EnterpriseSyncError::LocalApi("activity ID missing".into())
+ })?;
+ let version = screenpipe_db::privacy_exports::export_version(&value)
+ .map_err(|e| EnterpriseSyncError::LocalApi(e.to_string()))?;
+ if staged.get(id) == Some(&version) {
+ versions.insert(id.to_string(), version);
+ }
+ }
+ rows.activities
+ .retain(|row| versions.contains_key(&row.activity_id));
+ versions
+ .retain(|id, _| rows.activities.iter().any(|row| &row.activity_id == id));
+ *self.selected_activities.lock().unwrap() = versions;
+ }
+ let previous_cause = self.refresh_cause.lock().unwrap().clone();
+ let cause = match previous_cause {
+ Some(cause) => cause,
+ None => match self.admit().await {
+ Ok(guard) => {
+ drop(guard);
+ return Ok(Default::default());
+ }
+ Err(EnterpriseSyncError::Storage { cause, .. })
+ if screenpipe_db::storage::is_revision_change_message(&cause)
+ || cause.contains("privacy revision changed") =>
+ {
+ cause
+ }
+ Err(error) => return Err(error),
+ },
+ };
+ // The policy generation remains authoritative. A
+ // record revocation refreshes certified copies only.
+ let fresh = self.db.storage_export_token().await.map_err(|e| {
+ EnterpriseSyncError::Storage {
+ operation: "refresh protected admission",
+ cause: e.to_string(),
+ }
+ })?;
+ let selected_records = rows.record_count();
+ rows.refresh_protected(&self.db, local).await?;
+ let retained_records = rows.record_count();
+ drop(fresh.admit(&self.db.pool).await.map_err(|e| {
+ EnterpriseSyncError::Storage {
+ operation: "validate refreshed protected export",
+ cause: e.to_string(),
+ }
+ })?);
+ if self.db.release_policy_generation().await.map_err(|e| {
+ EnterpriseSyncError::Storage {
+ operation: "validate refreshed export policy",
+ cause: e.to_string(),
+ }
+ })? != self.policy_generation
+ {
+ return Err(EnterpriseSyncError::Configuration(
+ "privacy policy changed during export; retry pending".into(),
+ ));
+ }
+ *self.token.lock().await = fresh;
+ // The serialized remainder still belongs to the old
+ // selection, so renewal must not authorize later chunks
+ // without checking their own certified copies.
+ *self.refresh_cause.lock().unwrap() = Some(cause.clone());
+ tracing::info!(
+ cause,
+ selected_records,
+ retained_records,
+ outcome = "certified survivors retained; stale selections excluded",
+ "enterprise export refreshed after record revocation"
+ );
return Ok(Default::default());
}
// A token also detects enabling privacy after raw selection.
self.token
+ .lock()
+ .await
.validate_privacy(&self.db.pool)
.await
.map_err(|error| EnterpriseSyncError::Storage {
@@ -312,7 +435,7 @@ mod imp {
if self.db.storage_mode() == screenpipe_db::storage::StorageMode::Sqlite {
return rows.refresh(local).await;
}
- match self.token.admit(&self.db.pool).await {
+ match self.token.lock().await.admit(&self.db.pool).await {
Ok(guard) => {
drop(guard);
Ok(Default::default())
@@ -2434,7 +2557,7 @@ mod imp {
}
#[cfg(test)]
- mod device_id_tests {
+ pub(crate) mod device_id_tests {
use super::{
choose_device_id, classify_failed_enterprise_response, credential_authorizes_policy,
enterprise_license_hash, exact_frame_url, explicitly_rejects_authorization,
@@ -2758,12 +2881,24 @@ mod imp {
.await
.unwrap();
db.execute_raw_sql_write(
- "INSERT INTO frames(id,timestamp,full_text) VALUES(1,'2026-10-16','safe')",
+ "INSERT INTO frames(id,timestamp,full_text) VALUES(1,'2026-10-16','safe'),(2,'2026-10-16','survivor')",
)
.await
.unwrap();
- let job = db.release_pending("frames", 1).await.unwrap().remove(0);
- assert!(db.publish_release(&job, &job.values).await.unwrap());
+ for job in db.release_pending("frames", 10).await.unwrap() {
+ assert!(db.publish_release(&job, &job.values).await.unwrap());
+ }
+ let protected = db.protected_reads().await.unwrap();
+ let reused = Arc::new(std::sync::atomic::AtomicUsize::new(0));
+ {
+ let mut conn = protected.pool.acquire().await.unwrap();
+ let mut handle = conn.lock_handle().await.unwrap();
+ let observed = reused.clone();
+ handle.set_progress_handler(1, move || {
+ observed.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
+ true
+ });
+ }
let export = StorageExport::begin_storage(db.clone()).await.unwrap();
let mut frames = vec![FrameRow {
frame_id: 1,
@@ -2774,6 +2909,10 @@ mod imp {
browser_url: None,
accessibility_tree: None,
}];
+ let mut survivor = frames[0].clone();
+ survivor.frame_id = 2;
+ survivor.text = Some("survivor".into());
+ frames.push(survivor);
let mut rows = ExportRows {
frames: &mut frames,
audio: &mut vec![],
@@ -2787,16 +2926,631 @@ mod imp {
};
export.filter(&mut rows, &Protected).await.unwrap();
assert_eq!(rows.frames[0].text.as_deref(), Some("safe"));
- db.execute_raw_sql_write("INSERT INTO frames(id,timestamp,full_text) VALUES(2,'2026-10-16','new capture')").await.unwrap();
+ db.execute_raw_sql_write("INSERT INTO frames(id,timestamp,full_text) VALUES(3,'2026-10-16','new capture')").await.unwrap();
export.filter(&mut rows, &Protected).await.unwrap();
db.execute_raw_sql_write("UPDATE frames SET full_text='PRIVATE edit' WHERE id=1")
.await
.unwrap();
+ // A revoked record waits for a new completion; the certified
+ // survivor continues without reading any raw source API.
+ export.filter(&mut rows, &Protected).await.unwrap();
+ assert!(reused.load(std::sync::atomic::Ordering::SeqCst) > 0,
+ "refresh must reuse the existing protected reader, not allocate a pool per chunk");
+ assert_eq!(rows.frames.len(), 1);
+ assert_eq!(rows.frames[0].frame_id, 2);
+ assert_eq!(rows.frames[0].text.as_deref(), Some("survivor"));
+ drop(export.admit().await.unwrap());
+ assert!(!db.release_pending("frames", 10).await.unwrap().is_empty());
+ db.execute_raw_sql_write("DELETE FROM frames WHERE id=2")
+ .await
+ .unwrap();
+ export.filter(&mut rows, &Protected).await.unwrap();
+ assert!(rows.frames.is_empty());
+ drop(export.admit().await.unwrap());
+ // Policy changes still revoke the entire prepared selection,
+ // even after all selected rows have been removed.
+ db.configure_release(&screenpipe_db::privacy_release::ReleasePolicy {
+ identity: "v2".into(),
+ text: true,
+ images: false,
+ })
+ .await
+ .unwrap();
assert!(export.filter(&mut rows, &Protected).await.is_err());
+ db.configure_release(&screenpipe_db::privacy_release::ReleasePolicy {
+ identity: "v1".into(),
+ text: true,
+ images: false,
+ })
+ .await
+ .unwrap();
+ assert!(
+ export.filter(&mut rows, &Protected).await.is_err(),
+ "a policy cycle must not revive an old export"
+ );
db.close().await;
}
}
+ #[tokio::test(flavor = "current_thread")]
+ async fn protected_upload_filters_revoked_rows_and_recovers_after_restart() {
+ let logs = tempfile::tempdir().unwrap();
+ check_protected_upload_revocations(logs.path()).await;
+ }
+
+ pub(crate) async fn check_protected_upload_revocations(logs: &std::path::Path) {
+ use super::{AudioRow, EnterpriseSyncError, FrameRow, LocalApiClient, StorageExport};
+ use crate::enterprise::sync::{self, ExportFilter};
+ use screenpipe_db::privacy_release::{ReleasePage, ReleasePolicy};
+ use std::sync::{
+ atomic::{AtomicU8, Ordering},
+ Arc,
+ };
+ let _streams = crate::enterprise_policy::sync_streams_test_lock();
+ let _restore = sync::tests::RestoreDefaultSyncStreams;
+ crate::enterprise_policy::set_sync_streams(
+ true,
+ false,
+ false,
+ false,
+ false,
+ false,
+ "off".into(),
+ "off".into(),
+ );
+ let file = std::fs::File::create(logs.join("screenpipe-app.2026-10-23.log")).unwrap();
+ let _subscriber = tracing::subscriber::set_default(
+ tracing_subscriber::fmt()
+ .with_ansi(false)
+ .with_writer(std::sync::Mutex::new(file))
+ .finish(),
+ );
+ struct Local {
+ db: Arc<screenpipe_db::DatabaseManager>,
+ protected: Arc<screenpipe_db::DatabaseManager>,
+ mutation: AtomicU8,
+ }
+ #[async_trait::async_trait]
+ impl LocalApiClient for Local {
+ async fn privacy_release_required(&self) -> Result<bool, EnterpriseSyncError> {
+ Ok(true)
+ }
+ async fn begin_export(
+ &self,
+ ) -> Result<Option<Box<dyn ExportFilter>>, EnterpriseSyncError> {
+ Ok(Some(Box::new(
+ StorageExport::begin_storage(self.db.clone()).await?,
+ )))
+ }
+ async fn fetch_release_page(
+ &self,
+ kind: &str,
+ after: i64,
+ cutoff: &str,
+ end: Option<&str>,
+ limit: u32,
+ ) -> Result<ReleasePage, EnterpriseSyncError> {
+ let page = self
+ .protected
+ .release_export_range_page(kind, after, cutoff, end, limit)
+ .await
+ .unwrap();
+ if kind == "frames" && !page.rows.as_array().unwrap().is_empty() {
+ let mutation = self.mutation.swap(0, Ordering::SeqCst);
+ let sql = match mutation {
+ 1 => Some("DELETE FROM frames WHERE id=1"),
+ 2 | 4 => Some(
+ "UPDATE frames SET full_text='UNPROCESSED_PRIVATE_EDIT' WHERE id=1",
+ ),
+ 3 => Some("DELETE FROM frames WHERE id IN (1,2)"),
+ 5 => Some(
+ "UPDATE frames SET timestamp='2026-10-21T01:00:00Z' WHERE id=1",
+ ),
+ _ => None,
+ };
+ if let Some(sql) = sql {
+ self.db.execute_raw_sql_write(sql).await.unwrap();
+ }
+ if matches!(mutation, 4 | 5) {
+ let job = self
+ .db
+ .release_pending("frames", 10)
+ .await
+ .unwrap()
+ .into_iter()
+ .find(|j| j.id == "1")
+ .unwrap();
+ let mut safe = job.values.clone();
+ safe.insert("full_text".into(), Some("new certified edit".into()));
+ assert!(self.db.publish_release(&job, &safe).await.unwrap());
+ }
+ if mutation != 0 {
+ self.db.execute_raw_sql_write("INSERT INTO frames(id,timestamp,full_text) VALUES(3,'2026-10-23T01:00:00Z','durable new capture')").await.unwrap();
+ }
+ }
+ Ok(page)
+ }
+ async fn fetch_frames_since(
+ &self,
+ _: Option<&str>,
+ _: u32,
+ _: u32,
+ ) -> Result<Vec<FrameRow>, EnterpriseSyncError> {
+ panic!("protected upload must not use raw capture APIs")
+ }
+ async fn fetch_audio_since(
+ &self,
+ _: Option<&str>,
+ _: u32,
+ _: u32,
+ ) -> Result<Vec<AudioRow>, EnterpriseSyncError> {
+ Ok(vec![])
+ }
+ }
+ for hybrid in [false, true] {
+ for mutation in 1..=5 {
+ let dir = tempfile::tempdir().unwrap();
+ let db = Arc::new(if hybrid {
+ let mut options = screenpipe_db::storage::MigrationOptions::default();
+ options.budget.disk_reserve_bytes = 0;
+ screenpipe_db::DatabaseManager::new_hybrid(
+ dir.path(),
+ Default::default(),
+ options,
+ )
+ .await
+ .unwrap()
+ } else {
+ screenpipe_db::DatabaseManager::new(
+ dir.path().join("db.sqlite").to_str().unwrap(),
+ Default::default(),
+ )
+ .await
+ .unwrap()
+ });
+ db.configure_release(&ReleasePolicy {
+ identity: "fixture-v1".into(),
+ text: true,
+ images: false,
+ })
+ .await
+ .unwrap();
+ db.execute_raw_sql_write("INSERT INTO frames(id,timestamp,full_text) VALUES(1,'2026-10-23T01:00:00Z','PRIVATE_ORIGINAL'),(2,'2026-10-23T01:00:00Z','certified survivor')").await.unwrap();
+ for job in db.release_pending("frames", 10).await.unwrap() {
+ let mut safe = job.values.clone();
+ if job.id == "1" {
+ safe.insert("full_text".into(), Some("certified original".into()));
+ }
+ assert!(db.publish_release(&job, &safe).await.unwrap());
+ }
+ let server = wiremock::MockServer::start().await;
+ let fail = Arc::new(std::sync::atomic::AtomicBool::new(mutation == 2));
+ let response = fail.clone();
+ wiremock::Mock::given(wiremock::matchers::path("/ingest"))
+ .respond_with(move |_: &wiremock::Request| {
+ wiremock::ResponseTemplate::new(if response.load(Ordering::SeqCst) {
+ 503
+ } else {
+ 200
+ })
+ })
+ .mount(&server)
+ .await;
+ let cfg = sync::tests::test_cfg(&dir, format!("{}/ingest", server.uri()));
+ let mut cursor = sync::Cursor::default();
+ cursor.privacy.insert(
+ "frames".into(),
+ sync::PrivacyCursor {
+ sequence: 0,
+ cutoff: "2026-10-22T00:00:00Z".into(),
+ },
+ );
+ let local = Local {
+ protected: db.protected_reads().await.unwrap(),
+ db: db.clone(),
+ mutation: AtomicU8::new(mutation),
+ };
+ let http = reqwest::Client::new();
+ let result = sync::run_one_sync(&cfg, &mut cursor, &local, &http).await;
+ if mutation == 2 {
+ assert!(result.is_err(), "real transport failure remains retryable");
+ assert_eq!(
+ sync::Cursor::load(&cfg.cursor_path).privacy["frames"].sequence,
+ 0
+ );
+ fail.store(false, Ordering::SeqCst);
+ cursor = sync::Cursor::load(&cfg.cursor_path);
+ assert_eq!(
+ sync::run_one_sync(&cfg, &mut cursor, &local, &http)
+ .await
+ .unwrap()
+ .frames,
+ 1
+ );
+ } else {
+ assert_eq!(
+ result.unwrap().frames,
+ if mutation == 3 {
+ 0
+ } else if mutation == 4 {
+ 2
+ } else {
+ 1
+ }
+ );
+ }
+ assert!(sync::Cursor::load(&cfg.cursor_path).privacy["frames"].sequence > 0);
+ let requests = server.received_requests().await.unwrap();
+ if mutation == 3 {
+ assert!(requests.is_empty());
+ }
+ for request in &requests {
+ let body = String::from_utf8_lossy(&request.body);
+ assert!(!body.contains("PRIVATE_ORIGINAL"));
+ assert!(!body.contains("UNPROCESSED_PRIVATE_EDIT"));
+ assert!(
+ !body.contains("durable new capture"),
+ "new captures stay outside the selected page"
+ );
+ assert!(body.contains("certified survivor"));
+ assert!(!body.contains("2026-10-21"), "a corrected time outside the cutoff is not authorized by the old selection");
+ }
+ // Source edits remain pending after the surviving page was
+ // acknowledged. Close/reopen both the DB and checkpoint,
+ // then release the pending work and prove later delivery.
+ drop(local);
+ db.close().await;
+ drop(db);
+ // Reopen an existing logical root through the normal
+ // startup path; new_hybrid initializes an empty root.
+ let db = Arc::new(
+ screenpipe_db::DatabaseManager::new(
+ dir.path().join("db.sqlite").to_str().unwrap(),
+ Default::default(),
+ )
+ .await
+ .unwrap(),
+ );
+ let capture = db
+ .frame_payloads(&[3], screenpipe_db::storage::Projection::All)
+ .await
+ .unwrap();
+ assert_eq!(
+ capture[&3].full_text.as_deref(),
+ Some("durable new capture")
+ );
+ for job in db.release_pending("frames", 10).await.unwrap() {
+ let mut safe = job.values.clone();
+ if job.id == "1" {
+ safe.insert("full_text".into(), Some("new certified edit".into()));
+ }
+ assert!(db.publish_release(&job, &safe).await.unwrap());
+ }
+ let local = Local {
+ protected: db.protected_reads().await.unwrap(),
+ db: db.clone(),
+ mutation: AtomicU8::new(0),
+ };
+ let mut cursor = sync::Cursor::load(&cfg.cursor_path);
+ let report = sync::run_one_sync(&cfg, &mut cursor, &local, &http)
+ .await
+ .unwrap();
+ assert_eq!(report.frames, if matches!(mutation, 2 | 4) { 2 } else { 1 });
+ let requests = server.received_requests().await.unwrap();
+ let body = String::from_utf8_lossy(&requests.last().unwrap().body);
+ assert!(body.contains("durable new capture"));
+ if matches!(mutation, 2 | 4) {
+ assert!(body.contains("new certified edit"));
+ }
+ db.close().await;
+ }
+ }
+ }
+
+ #[tokio::test]
+ async fn protected_upload_refreshes_every_remaining_http_chunk_after_revocation() {
+ use super::{AudioRow, EnterpriseSyncError, FrameRow, LocalApiClient, StorageExport};
+ use crate::enterprise::sync::{self, ExportFilter, ExportFilterReport, ExportRows};
+ use std::sync::{
+ atomic::{AtomicBool, AtomicUsize, Ordering},
+ Arc,
+ };
+ let _streams = crate::enterprise_policy::sync_streams_test_lock();
+ let _restore = sync::tests::RestoreDefaultSyncStreams;
+ crate::enterprise_policy::set_sync_streams(
+ true,
+ false,
+ false,
+ false,
+ false,
+ false,
+ "off".into(),
+ "off".into(),
+ );
+ struct ChangingExport {
+ inner: StorageExport,
+ db: Arc<screenpipe_db::DatabaseManager>,
+ calls: AtomicUsize,
+ mutate: Arc<AtomicBool>,
+ }
+ #[async_trait::async_trait]
+ impl ExportFilter for ChangingExport {
+ async fn admit(&self) -> Result<Option<Box<dyn Send>>, EnterpriseSyncError> {
+ self.inner.admit().await
+ }
+ async fn filter(
+ &self,
+ rows: &mut ExportRows<'_>,
+ local: &dyn LocalApiClient,
+ ) -> Result<ExportFilterReport, EnterpriseSyncError> {
+ if self.calls.fetch_add(1, Ordering::SeqCst) == 1
+ && self.mutate.swap(false, Ordering::SeqCst)
+ {
+ // The preceding HTTP request has completed. Change two
+ // later chunks before either reaches the real filter.
+ self.db
+ .execute_raw_sql_write("DELETE FROM frames WHERE id=2")
+ .await
+ .unwrap();
+ self.db
+ .execute_raw_sql_write(
+ "UPDATE frames SET full_text='PRIVATE_CHANGED' WHERE id=3",
+ )
+ .await
+ .unwrap();
+ self.db.execute_raw_sql_write("INSERT INTO frames(id,timestamp,full_text) VALUES(4,'2026-10-23T03:00:00Z','new durable capture')").await.unwrap();
+ }
+ self.inner.filter(rows, local).await
+ }
+ }
+ struct Local {
+ db: Arc<screenpipe_db::DatabaseManager>,
+ protected: Arc<screenpipe_db::DatabaseManager>,
+ mutate: Arc<AtomicBool>,
+ finished: Arc<AtomicBool>,
+ }
+ #[async_trait::async_trait]
+ impl LocalApiClient for Local {
+ async fn privacy_release_required(&self) -> Result<bool, EnterpriseSyncError> {
+ Ok(true)
+ }
+ async fn prepare_release_replay(
+ &self,
+ id: &str,
+ streams: &[String],
+ start: &str,
+ end: &str,
+ limit: u32,
+ ) -> Result<bool, EnterpriseSyncError> {
+ self.db
+ .prepare_release_replay(id, streams, start, end, limit)
+ .await
+ .map_err(|e| EnterpriseSyncError::LocalApi(e.to_string()))
+ }
+ async fn release_replay_pending(
+ &self,
+ id: &str,
+ streams: &[String],
+ start: &str,
+ end: &str,
+ after: &std::collections::BTreeMap<String, i64>,
+ ) -> Result<bool, EnterpriseSyncError> {
+ self.db
+ .release_replay_pending(id, streams, start, end, Some(after))
+ .await
+ .map_err(|e| EnterpriseSyncError::LocalApi(e.to_string()))
+ }
+ async fn finish_release_replay(&self, id: &str) -> Result<(), EnterpriseSyncError> {
+ self.db
+ .finish_release_replay(id)
+ .await
+ .map_err(|e| EnterpriseSyncError::LocalApi(e.to_string()))?;
+ self.finished.store(true, Ordering::SeqCst);
+ Ok(())
+ }
+ async fn begin_export(
+ &self,
+ ) -> Result<Option<Box<dyn ExportFilter>>, EnterpriseSyncError> {
+ Ok(Some(Box::new(ChangingExport {
+ inner: StorageExport::begin_storage(self.db.clone()).await?,
+ db: self.db.clone(),
+ calls: AtomicUsize::new(0),
+ mutate: self.mutate.clone(),
+ })))
+ }
+ async fn fetch_release_page(
+ &self,
+ kind: &str,
+ after: i64,
+ cutoff: &str,
+ end: Option<&str>,
+ limit: u32,
+ ) -> Result<screenpipe_db::privacy_release::ReleasePage, EnterpriseSyncError>
+ {
+ Ok(self
+ .protected
+ .release_export_range_page(kind, after, cutoff, end, limit)
+ .await
+ .unwrap())
+ }
+ async fn fetch_frames_since(
+ &self,
+ _: Option<&str>,
+ _: u32,
+ _: u32,
+ ) -> Result<Vec<FrameRow>, EnterpriseSyncError> {
+ panic!("raw frame refresh is forbidden")
+ }
+ async fn fetch_audio_since(
+ &self,
+ _: Option<&str>,
+ _: u32,
+ _: u32,
+ ) -> Result<Vec<AudioRow>, EnterpriseSyncError> {
+ Ok(vec![])
+ }
+ }
+ for (hybrid, replay) in [false, true]
+ .into_iter()
+ .flat_map(|hybrid| [(hybrid, false), (hybrid, true)])
+ {
+ let dir = tempfile::tempdir().unwrap();
+ let db = Arc::new(if hybrid {
+ let mut options = screenpipe_db::storage::MigrationOptions::default();
+ options.budget.disk_reserve_bytes = 0;
+ screenpipe_db::DatabaseManager::new_hybrid(
+ dir.path(),
+ Default::default(),
+ options,
+ )
+ .await
+ .unwrap()
+ } else {
+ screenpipe_db::DatabaseManager::new(
+ dir.path().join("db.sqlite").to_str().unwrap(),
+ Default::default(),
+ )
+ .await
+ .unwrap()
+ });
+ db.configure_release(&screenpipe_db::privacy_release::ReleasePolicy {
+ identity: "chunks-v1".into(),
+ text: true,
+ images: false,
+ })
+ .await
+ .unwrap();
+ // Match the actual HTTP transport target; each pair of records
+ // exceeds it, so every original record occupies its own chunk.
+ let text = "x".repeat(sync::HOSTED_INGEST_REQUEST_BYTES / 2);
+ for id in 1..=3 {
+ db.execute_raw_sql_write(&format!("INSERT INTO frames(id,timestamp,full_text) VALUES({id},'2026-10-23T01:00:00Z','{text}')")).await.unwrap();
+ }
+ let mut jobs = db.release_pending("frames", 10).await.unwrap();
+ jobs.sort_by_key(|job| job.id.parse::<i64>().unwrap());
+ for job in jobs {
+ assert!(db.publish_release(&job, &job.values).await.unwrap());
+ }
+ let server = wiremock::MockServer::start().await;
+ wiremock::Mock::given(wiremock::matchers::path("/ingest"))
+ .respond_with(wiremock::ResponseTemplate::new(200))
+ .mount(&server)
+ .await;
+ let cfg = sync::tests::test_cfg(&dir, format!("{}/ingest", server.uri()));
+ let mut cursor = sync::Cursor::default();
+ cursor.privacy.insert(
+ "frames".into(),
+ sync::PrivacyCursor {
+ sequence: 0,
+ cutoff: "2026-10-22T00:00:00Z".into(),
+ },
+ );
+ let request = serde_json::json!({
+ "id": uuid::Uuid::new_v4().to_string(), "license_id": uuid::Uuid::new_v4().to_string(),
+ "device_id": cfg.device_id, "start_at": "2026-10-22T00:00:00Z", "end_at": "2026-10-23T02:00:00Z",
+ "streams": ["frames"], "images_only": false, "recover_images": false
+ });
+ let finished = Arc::new(AtomicBool::new(false));
+ let local = Local {
+ db: db.clone(),
+ protected: db.protected_reads().await.unwrap(),
+ mutate: Arc::new(AtomicBool::new(true)),
+ finished: finished.clone(),
+ };
+ let http = reqwest::Client::new();
+ let report = if replay {
+ let (page, more) = sync::tests::requested_replay_page(
+ &cfg,
+ &mut cursor,
+ &local,
+ &http,
+ request.clone(),
+ )
+ .await
+ .unwrap();
+ assert!(
+ more,
+ "revoked pending records must keep the accepted replay active"
+ );
+ assert!(!finished.load(Ordering::SeqCst));
+ page
+ } else {
+ sync::run_one_sync(&cfg, &mut cursor, &local, &http)
+ .await
+ .unwrap()
+ };
+ assert_eq!(report.frames, 1);
+ let requests = server.received_requests().await.unwrap();
+ assert_eq!(
+ requests.len(),
+ 1,
+ "revoked later chunks must never reach HTTP"
+ );
+ let record: serde_json::Value = serde_json::from_slice(&requests[0].body).unwrap();
+ assert_eq!(record["frame_id"], 1);
+ drop(local);
+ db.close().await;
+ drop(db);
+ let db = Arc::new(
+ screenpipe_db::DatabaseManager::new(
+ dir.path().join("db.sqlite").to_str().unwrap(),
+ Default::default(),
+ )
+ .await
+ .unwrap(),
+ );
+ assert_eq!(
+ db.frame_payloads(&[4], screenpipe_db::storage::Projection::All)
+ .await
+ .unwrap()[&4]
+ .full_text
+ .as_deref(),
+ Some("new durable capture")
+ );
+ let local = Local {
+ db: db.clone(),
+ protected: db.protected_reads().await.unwrap(),
+ mutate: Arc::new(AtomicBool::new(false)),
+ finished: finished.clone(),
+ };
+ for job in db.release_pending("frames", 10).await.unwrap() {
+ let mut safe = job.values.clone();
+ if job.id == "3" {
+ safe.insert("full_text".into(), Some("certified changed record".into()));
+ }
+ assert!(db.publish_release(&job, &safe).await.unwrap());
+ }
+ let mut restarted = sync::Cursor::load(&cfg.cursor_path);
+ let report = if replay {
+ let (page, more) = sync::tests::requested_replay_page(
+ &cfg,
+ &mut restarted,
+ &local,
+ &http,
+ request,
+ )
+ .await
+ .unwrap();
+ assert!(
+ !more,
+ "unrelated captures outside the request must not strand replay"
+ );
+ assert!(finished.load(Ordering::SeqCst));
+ page
+ } else {
+ sync::run_one_sync(&cfg, &mut restarted, &local, &http)
+ .await
+ .unwrap()
+ };
+ assert_eq!(report.frames, if replay { 1 } else { 2 });
+ let requests = server.received_requests().await.unwrap();
+ let body = String::from_utf8_lossy(&requests.last().unwrap().body);
+ assert!(body.contains("certified changed record"));
+ assert_eq!(body.contains("new durable capture"), !replay);
+ assert!(!body.contains("PRIVATE_CHANGED"));
+ db.close().await;
+ }
+ }
#[tokio::test]
async fn export_upload_filters_deleted_rows_and_refreshes_redactions() {
use super::{AudioRow, EnterpriseSyncError, FrameRow, LocalApiClient, StorageExport};
@@ -3571,6 +4325,9 @@ mod imp {
#[cfg(feature = "enterprise-support")]
pub use imp::{configure_telemetry_context, spawn};
+#[cfg(all(test, feature = "enterprise-support"))]
+pub(crate) use imp::device_id_tests::check_protected_upload_revocations;
+
#[cfg(feature = "enterprise-support")]
pub(crate) use imp::{authorize_startup, verify_recording_authorization};
diff --git a/crates/screenpipe-db/src/db/privacy_release.rs b/crates/screenpipe-db/src/db/privacy_release.rs
index 3069a052d..716776b7d 100644
--- a/crates/screenpipe-db/src/db/privacy_release.rs
+++ b/crates/screenpipe-db/src/db/privacy_release.rs
@@ -862,7 +862,11 @@ impl DatabaseManager {
}
registry.push(pool.clone());
drop(registry);
- Ok(Arc::new(Self {
+ Ok(self.protected_read_adapter(pool))
+ }
+
+ fn protected_read_adapter(self: &Arc<Self>, pool: SqlitePool) -> Arc<Self> {
+ Arc::new(Self {
release_owner: Some(Arc::clone(self)),
release_read_pools: Arc::clone(&self.release_read_pools),
storage: self.storage.clone(),
@@ -876,7 +880,7 @@ impl DatabaseManager {
persistent_failure_hook: self.persistent_failure_hook.clone(),
manager_lease: None,
close_token: self.close_token.clone(),
- }))
+ })
}
/// Revokes assembled uploads only when already released content or the
@@ -1024,13 +1028,7 @@ impl DatabaseManager {
.derived_release_page(kind, after, cutoff, end, limit)
.await;
}
- let (table,projection,time)=match kind {
- "frames"=>("frames","s.id AS frame_id,s.timestamp,s.app_name,s.window_name,s.browser_url,s.accessibility_tree_json AS accessibility_tree,COALESCE(s.full_text,s.accessibility_text,'') AS text","timestamp"),
- "audio"=>("audio_transcriptions","s.id AS transcription_id,s.timestamp,s.transcription,NULL AS speaker,s.device","timestamp"),
- "input"=>("ui_events","s.id AS event_id,s.timestamp,s.event_type,s.app_name,s.window_title,s.browser_url,s.element_role,s.element_name,s.text_content","timestamp"),
- "memory"=>("memories","s.id AS memory_id,s.created_at,s.updated_at,s.content,s.source,s.tags,s.importance,s.frame_id","created_at"),
- _=>return Err(err("privacy export surface is not supported")),
- };
+ let (table, projection, time) = export_surface(kind)?;
let (sequence, more) = self
.ready_range_window(kind, table, time, None, after, cutoff, end, limit)
.await?;
@@ -1044,7 +1042,99 @@ impl DatabaseManager {
.bind(end)
.fetch_all(&self.pool)
.await?;
- let mut rows = Self::raw_sql_rows(&rows);
+ let rows = self.hydrate_release_export(kind, &rows).await?;
+ Ok(ReleasePage {
+ sequence,
+ rows,
+ more,
+ })
+ }
+ /// The policy generation changes for policy replacement, including a cycle
+ /// back to the same identity. Record revocations leave this value unchanged.
+ pub async fn release_policy_generation(&self) -> Result<Option<i64>, sqlx::Error> {
+ let exists: bool = sqlx::query_scalar(
+ "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE name='_privacy_policy')",
+ )
+ .fetch_one(&self.pool)
+ .await?;
+ if !exists {
+ return Ok(None);
+ }
+ sqlx::query_scalar("SELECT receipt_key FROM _privacy_policy WHERE id=1")
+ .fetch_one(&self.pool)
+ .await
+ .map(Some)
+ }
+
+ /// Refresh only selected IDs through current completion receipts. Never
+ /// replace a processed export with a raw source, or enroll new captures.
+ pub async fn refresh_release_export_selection(
+ self: &Arc<Self>,
+ kind: &str,
+ ids: &[String],
+ ) -> Result<serde_json::Value, sqlx::Error> {
+ if self.close_token.is_cancelled() {
+ return Err(sqlx::Error::PoolClosed);
+ }
+ let reader = if self.protected_reads_enabled() {
+ Arc::clone(self)
+ } else {
+ // API admission already registered its protected pool. Reuse it
+ // across chunks/ticks instead of accumulating a pool per refresh.
+ let existing = self
+ .release_read_pools
+ .lock()
+ .await
+ .iter()
+ .find(|pool| !pool.is_closed())
+ .cloned();
+ match existing {
+ Some(pool) => self.protected_read_adapter(pool),
+ None => self.protected_reads().await?,
+ }
+ };
+ reader
+ .refresh_release_export_selection_inner(kind, ids)
+ .await
+ }
+
+ async fn refresh_release_export_selection_inner(
+ &self,
+ kind: &str,
+ ids: &[String],
+ ) -> Result<serde_json::Value, sqlx::Error> {
+ if !self.protected_reads_enabled() {
+ return Err(err("protected read adapter required"));
+ }
+ if ids.is_empty() {
+ return Ok(serde_json::json!([]));
+ }
+ let ids = serde_json::to_string(ids).map_err(|_| err("invalid export IDs"))?;
+ if matches!(kind, "activities" | "parsed" | "feedback") {
+ let values: Vec<(String, String)> = sqlx::query_as("SELECT s.template_json,s.text_json FROM json_each(?) selected JOIN _privacy_exports s ON s.source_id=selected.value AND s.stream=? JOIN _privacy_release r ON r.kind='exports' AND r.id=CAST(s.id AS TEXT) JOIN _privacy_policy p ON CAST(p.receipt_key AS TEXT)=r.policy")
+ .bind(ids).bind(kind).fetch_all(&self.pool).await?;
+ let rows = values
+ .into_iter()
+ .map(|(template, texts)| super::privacy_exports::restore_payload(&template, &texts))
+ .collect::<Result<Vec<_>, _>>()?;
+ return Ok(serde_json::json!(rows));
+ }
+ let (table, projection, _) = export_surface(kind)?;
+ let sql = format!("SELECT {projection} FROM json_each(?) selected JOIN {table} s ON s.id=selected.value JOIN _privacy_release r ON r.kind=? AND r.id=CAST(s.id AS TEXT) JOIN _privacy_policy p ON CAST(p.receipt_key AS TEXT)=r.policy");
+ let rows = sqlx::query(sqlx::AssertSqlSafe(sql))
+ .bind(ids)
+ .bind(kind)
+ .fetch_all(&self.pool)
+ .await?;
+ self.hydrate_release_export(kind, &rows).await
+ }
+
+ async fn hydrate_release_export(
+ &self,
+ kind: &str,
+ rows: &[sqlx::sqlite::SqliteRow],
+ ) -> Result<serde_json::Value, sqlx::Error> {
+ let mut rows = Self::raw_sql_rows(rows);
if let Some(items) = rows.as_array_mut() {
if kind == "memory" {
for item in items.iter_mut() {
@@ -1091,11 +1181,7 @@ impl DatabaseManager {
}
}
}
- Ok(ReleasePage {
- sequence,
- rows,
- more,
- })
+ Ok(rows)
}
}
@@ -1172,3 +1258,13 @@ impl DatabaseManager {
})
}
}
+
+fn export_surface(kind: &str) -> Result<(&'static str, &'static str, &'static str), sqlx::Error> {
+ Ok(match kind {
+ "frames"=>("frames","s.id AS frame_id,s.timestamp,s.app_name,s.window_name,s.browser_url,s.accessibility_tree_json AS accessibility_tree,COALESCE(s.full_text,s.accessibility_text,'') AS text","timestamp"),
+ "audio"=>("audio_transcriptions","s.id AS transcription_id,s.timestamp,s.transcription,NULL AS speaker,s.device","timestamp"),
+ "input"=>("ui_events","s.id AS event_id,s.timestamp,s.event_type,s.app_name,s.window_title,s.browser_url,s.element_role,s.element_name,s.text_content","timestamp"),
+ "memory"=>("memories","s.id AS memory_id,s.created_at,s.updated_at,s.content,s.source,s.tags,s.importance,s.frame_id","created_at"),
+ _=>return Err(err("privacy export surface is not supported")),
+ })
+}