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): unblock AX backfills from screenshot failures (#7676)

Ezra Ellette35ed62162f99
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/enterprise/backfill.rs b/apps/screenpipe-app-tauri/src-tauri/src/enterprise/backfill.rs
index 23e45e1f9..9dac7c528 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/enterprise/backfill.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/enterprise/backfill.rs
@@ -8,6 +8,8 @@
use super::*;
use crate::enterprise_policy::{FeedbackSyncMode, SyncStreams};
+pub(super) const INDEPENDENT_IMAGES: &str = "independent-v1";
+
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum BackfillStream {
@@ -19,7 +21,7 @@ pub(super) enum BackfillStream {
Feedback,
}
-#[derive(Debug, Deserialize)]
+#[derive(Debug, Clone, Deserialize)]
pub(super) struct BackfillRequest {
id: String,
license_id: String,
@@ -31,6 +33,9 @@ pub(super) struct BackfillRequest {
pub(super) images_only: bool,
#[serde(default)]
pub(super) source_id: Option<String>,
+ // Server policy snapshot admits image work, never authorizes pixel reads.
+ #[serde(default)]
+ pub(super) recover_images: Option<bool>,
}
impl BackfillRequest {
@@ -155,6 +160,16 @@ impl BackfillRequest {
));
}
}
+ if let Some(replay) = &cursor.boundary.backfill_image_replay {
+ if replay.cursor.boundary.backfill_image_replay.is_some() {
+ return Err(EnterpriseSyncError::Configuration(
+ "invalid nested image checkpoint".into(),
+ ));
+ }
+ let mut images = self.clone();
+ images.streams = vec![BackfillStream::Frames];
+ images.validate_cursor(&replay.cursor)?;
+ }
Ok(())
}
@@ -181,7 +196,7 @@ struct Pending {
request: Option<BackfillRequest>,
}
-/// Outcomes acknowledged by storage, checkpointed with the telemetry page.
+/// Outcomes acknowledged by storage, checkpointed with the image cursor.
/// Missing pixels are never counted as uploaded screenshots.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub(super) struct ImageProgress {
@@ -189,6 +204,80 @@ pub(super) struct ImageProgress {
pub unavailable: u64,
}
+/// Accepted image work remains part of the same request until acknowledged,
+/// even when policy revokes pixels or telemetry has reached the end of the range.
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub(super) struct ImageReplay {
+ cursor: Cursor,
+ pub(super) complete: bool,
+}
+
+impl ImageReplay {
+ pub(super) fn new(telemetry: &Cursor) -> Self {
+ let mut cursor = telemetry.clone();
+ cursor.boundary.backfill_image_replay = None;
+ cursor.boundary.backfill_telemetry_complete = false;
+ Self {
+ cursor,
+ complete: false,
+ }
+ }
+}
+
+/// One telemetry page and one independent image page. Image failures must not
+/// roll back acknowledged AX or prevent the next telemetry page on a later tick.
+pub(super) async fn run_replay_page(
+ cfg: &EnterpriseSyncConfig,
+ cursor: &mut Cursor,
+ local: &dyn LocalApiClient,
+ http: &reqwest::Client,
+ request: &BackfillRequest,
+) -> Result<SyncTickReport, EnterpriseSyncError> {
+ if request.images_only {
+ return run_one_sync_inner(cfg, cursor, local, http, false, Some(request)).await;
+ }
+ let mut page = SyncTickReport::default();
+ if !cursor.boundary.backfill_telemetry_complete {
+ page = run_one_sync_inner(cfg, cursor, local, http, false, Some(request)).await?;
+ cursor.boundary.backfill_telemetry_complete = !page.may_have_more();
+ cursor.save(&cfg.cursor_path)?;
+ }
+ if let Some(replay) = cursor
+ .boundary
+ .backfill_image_replay
+ .as_mut()
+ .filter(|r| !r.complete)
+ {
+ let mut images = request.clone();
+ images.images_only = true;
+ images.streams = vec![BackfillStream::Frames];
+ images.source_id = Some(
+ replay
+ .cursor
+ .source_id
+ .clone()
+ .unwrap_or_else(|| cfg.device_id.clone()),
+ );
+ let result =
+ run_one_sync_inner(cfg, &mut replay.cursor, local, http, false, Some(&images)).await;
+ if let Ok(images_page) = &result {
+ replay.complete = !images_page.may_have_more();
+ }
+ cursor.boundary.backfill_images = replay.cursor.boundary.backfill_images.clone();
+ // Persist the unchanged image boundary on failure alongside acknowledged
+ // telemetry, so restart never loses accepted image work.
+ cursor.save(&cfg.cursor_path)?;
+ result?;
+ }
+ page.scanned_full_page = !cursor.boundary.backfill_telemetry_complete
+ || cursor
+ .boundary
+ .backfill_image_replay
+ .as_ref()
+ .is_some_and(|r| !r.complete);
+ Ok(page)
+}
+
pub(super) async fn upload_page_images(
cfg: &EnterpriseSyncConfig,
local: &dyn LocalApiClient,
@@ -313,6 +402,7 @@ async fn report(
"id": request.id, "status": status, "uploaded_records": cursor.boundary.backfill_records.unwrap_or(0),
"screenshots": cursor.boundary.backfill_images,
"images_only": request.images_only,
+ "telemetry_complete": if request.images_only { None } else { Some(cursor.boundary.backfill_telemetry_complete) },
"last_error": failure.map(|error| match error { EnterpriseSyncError::BackfillImages(code) => *code, _ => "retrying" }),
"cursors": { "frames": cursor.last_frame_ts, "audio": cursor.last_audio_ts, "ui_events": cursor.last_ui_ts,
"parsed": cursor.last_parsed_ts, "memories": cursor.last_memory_ts, "feedback": cursor.last_feedback_ts }
@@ -451,6 +541,8 @@ mod tests {
#[tokio::test]
async fn backfill_resumes_timestamp_ties_after_lost_ack_without_touching_live_cursor() {
let _guard = crate::enterprise_policy::sync_streams_test_lock();
+ let _restore = super::super::tests::RestoreDefaultSyncStreams;
+ crate::enterprise_policy::set_sync_streams(true, true, true, true, true, true, "off".into(), "off".into());
let server = MockServer::start().await;
let dir = tempfile::TempDir::new().unwrap();
let cfg = cfg(&dir, &server);
@@ -507,7 +599,10 @@ mod tests {
for upload in &uploads {
for line in String::from_utf8_lossy(&upload.body).lines() {
let record: serde_json::Value = serde_json::from_str(line).unwrap();
- assert_eq!(record["accessibility_tree"][0]["properties"]["automation_id"], "search");
+ assert_eq!(
+ record["accessibility_tree"][0]["properties"]["automation_id"],
+ "search"
+ );
}
}
assert_eq!(
@@ -526,12 +621,16 @@ mod tests {
"enterprise_backfill_{}_{}.json",
req.license_id, req.id
)));
- assert_eq!(checkpoint.boundary.backfill_records, Some(super::FRAME_PAGE_LIMIT as u64 + 1));
+ assert_eq!(
+ checkpoint.boundary.backfill_records,
+ Some(super::FRAME_PAGE_LIMIT as u64 + 1)
+ );
}
#[tokio::test]
async fn backfill_failed_optional_read_does_not_complete_or_upload() {
let _guard = crate::enterprise_policy::sync_streams_test_lock();
+ let _restore = super::super::tests::RestoreDefaultSyncStreams;
let server = MockServer::start().await;
let dir = tempfile::TempDir::new().unwrap();
let cfg = cfg(&dir, &server);
@@ -559,6 +658,8 @@ mod tests {
#[tokio::test]
async fn backfill_direct_retries_keep_batch_identity_without_advancing_cursor() {
let _guard = crate::enterprise_policy::sync_streams_test_lock();
+ let _restore = super::super::tests::RestoreDefaultSyncStreams;
+ crate::enterprise_policy::set_sync_streams(true, true, true, true, true, true, "off".into(), "off".into());
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/ticket"))
@@ -607,12 +708,343 @@ mod tests {
.collect();
assert_eq!(manifests[0]["batch_id"], manifests[1]["batch_id"]);
assert_eq!(manifests[2]["batch_id"], manifests[3]["batch_id"]);
- assert_eq!(manifests[0]["record_counts"]["frames"], super::FRAME_PAGE_LIMIT as u64);
+ assert_eq!(
+ manifests[0]["record_counts"]["frames"],
+ super::FRAME_PAGE_LIMIT as u64
+ );
+ }
+
+ struct PixelGate {
+ history: Local,
+ refused: std::sync::atomic::AtomicBool,
+ pixel_reads: AtomicUsize,
+ oversized_ax: bool,
+ }
+
+ #[async_trait::async_trait]
+ impl LocalApiClient for PixelGate {
+ async fn fetch_frames_since(
+ &self,
+ since: Option<&str>,
+ offset: u32,
+ limit: u32,
+ ) -> Result<Vec<FrameRow>, EnterpriseSyncError> {
+ let mut frames = self
+ .history
+ .fetch_frames_since(since, offset, limit)
+ .await?;
+ if self.oversized_ax && offset == 0 {
+ if let Some(frame) = frames.first_mut() {
+ frame.accessibility_tree.as_mut().unwrap()[0]["properties"]["value"] =
+ serde_json::json!("界".repeat(2_100_000));
+ }
+ }
+ Ok(frames)
+ }
+ async fn fetch_frame_jpeg(&self, _: i64) -> Result<Option<Vec<u8>>, EnterpriseSyncError> {
+ self.pixel_reads.fetch_add(1, Ordering::SeqCst);
+ if self.refused.load(Ordering::SeqCst) {
+ Err(EnterpriseSyncError::BackfillImages(
+ "screenshot_local_settings_disabled",
+ ))
+ } else {
+ Ok(None)
+ }
+ }
+ async fn fetch_audio_since(
+ &self,
+ _: Option<&str>,
+ _: u32,
+ _: u32,
+ ) -> Result<Vec<AudioRow>, EnterpriseSyncError> {
+ panic!("unselected audio")
+ }
}
#[tokio::test]
- async fn direct_backfill_images_ack_before_telemetry_and_cursor_survive_retry() {
+ async fn ax_finishes_through_pixel_refusal_and_policy_revocation_then_images_resume() {
let _guard = crate::enterprise_policy::sync_streams_test_lock();
+ let _restore = super::super::tests::RestoreDefaultSyncStreams;
+ crate::enterprise_policy::set_sync_streams(
+ true,
+ false,
+ false,
+ false,
+ false,
+ false,
+ "off".into(),
+ "all".into(),
+ );
+ let server = MockServer::start().await;
+ Mock::given(method("GET"))
+ .and(path("/api/enterprise/backfill-requests"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(pending()))
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/api/enterprise/backfill-requests"))
+ .respond_with(
+ ResponseTemplate::new(200).set_body_json(serde_json::json!({"accepted": true})),
+ )
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/api/enterprise/ingest"))
+ .respond_with({
+ let target = format!("{}/ax-blob", server.uri());
+ move |r: &wiremock::Request| {
+ let value: serde_json::Value =
+ serde_json::from_slice(&r.body).unwrap_or_default();
+ if value["action"] == "ticket" {
+ ResponseTemplate::new(200).set_body_json(serde_json::json!({
+ "ok": true, "method": "PUT", "upload_url": target, "headers": {}
+ }))
+ } else {
+ ResponseTemplate::new(200)
+ }
+ }
+ })
+ .expect(4)
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/api/enterprise/frame-uploads"))
+ .respond_with(|r: &wiremock::Request| {
+ let value: serde_json::Value = serde_json::from_slice(&r.body).unwrap();
+ let ids: Vec<_> = value["frames"]
+ .as_array()
+ .unwrap()
+ .iter()
+ .map(|f| f["frame_id"].clone())
+ .collect();
+ ResponseTemplate::new(200)
+ .set_body_json(serde_json::json!({"stored": [], "unavailable": ids}))
+ })
+ .mount(&server)
+ .await;
+ Mock::given(method("PUT"))
+ .and(path("/ax-blob"))
+ .respond_with(ResponseTemplate::new(200))
+ .expect(1)
+ .mount(&server)
+ .await;
+ let dir = tempfile::TempDir::new().unwrap();
+ let cfg = cfg(&dir, &server);
+ let req = request();
+ let checkpoint = dir.path().join(format!(
+ "enterprise_backfill_{}_{}.json",
+ req.license_id, req.id
+ ));
+ let local = PixelGate {
+ history: Local {
+ reads: AtomicUsize::new(0),
+ fail_ui: false,
+ },
+ refused: std::sync::atomic::AtomicBool::new(true),
+ pixel_reads: AtomicUsize::new(0),
+ oversized_ax: true,
+ };
+ let (_, shutdown) = tokio::sync::watch::channel(false);
+ let http = enterprise_http_client();
+ assert!(fulfill_requests(&cfg, &local, &http, &shutdown)
+ .await
+ .unwrap_err()
+ .to_string()
+ .contains("screenshot_local_settings_disabled"));
+ let first = Cursor::load(&checkpoint);
+ assert_eq!(
+ first.boundary.backfill_records,
+ Some(super::FRAME_PAGE_LIMIT as u64)
+ );
+ assert_eq!(
+ first
+ .boundary
+ .backfill_image_replay
+ .as_ref()
+ .unwrap()
+ .cursor
+ .boundary
+ .frames,
+ 0
+ );
+ // Native policy is revoked between pages. Allowed AX still reaches its
+ // final tied row; no forbidden pixel fetch or upload is attempted.
+ crate::enterprise_policy::set_sync_streams(
+ true,
+ false,
+ false,
+ false,
+ false,
+ false,
+ "off".into(),
+ "off".into(),
+ );
+ assert!(fulfill_requests(&cfg, &local, &http, &shutdown)
+ .await
+ .unwrap_err()
+ .to_string()
+ .contains("screenshot_policy_disabled"));
+ assert_eq!(local.pixel_reads.load(Ordering::SeqCst), 1);
+ let done = Cursor::load(&checkpoint);
+ assert!(done.boundary.backfill_telemetry_complete);
+ assert_eq!(
+ done.boundary.backfill_records,
+ Some(super::FRAME_PAGE_LIMIT as u64 + 1)
+ );
+ assert!(!done.boundary.backfill_image_replay.unwrap().complete);
+ let requests = server.received_requests().await.unwrap();
+ assert!(requests
+ .iter()
+ .all(|r| r.url.path() != "/api/enterprise/frame-uploads"));
+ let report: serde_json::Value =
+ serde_json::from_slice(&requests.last().unwrap().body).unwrap();
+ assert_eq!(report["telemetry_complete"], true);
+ assert_eq!(report["status"], "running");
+ assert_eq!(report["last_error"], "screenshot_policy_disabled");
+ // Each call reloads disk state. Re-enable only the test fixture policy;
+ // image recovery uses its old boundary and sends no duplicate AX.
+ crate::enterprise_policy::set_sync_streams(
+ true,
+ false,
+ false,
+ false,
+ false,
+ false,
+ "off".into(),
+ "all".into(),
+ );
+ local.refused.store(false, Ordering::SeqCst);
+ for _ in 0..2 {
+ fulfill_requests(&cfg, &local, &http, &shutdown)
+ .await
+ .unwrap();
+ }
+ let done = Cursor::load(&checkpoint);
+ assert!(done.boundary.backfill_image_replay.unwrap().complete);
+ assert_eq!(
+ done.boundary.backfill_images.unwrap().unavailable,
+ super::FRAME_PAGE_LIMIT as u64 + 1
+ );
+ assert!(!cfg.cursor_path.exists(), "live cursor is untouched");
+ let requests = server.received_requests().await.unwrap();
+ let last: serde_json::Value =
+ serde_json::from_slice(&requests.last().unwrap().body).unwrap();
+ assert_eq!(last["status"], "completed");
+ assert_eq!(last["last_error"], serde_json::Value::Null);
+ let rows: Vec<serde_json::Value> = requests
+ .iter()
+ .filter(|r| r.url.path() == "/api/enterprise/ingest" || r.url.path() == "/ax-blob")
+ .flat_map(|r| {
+ std::str::from_utf8(&r.body)
+ .unwrap()
+ .lines()
+ .map(|line| serde_json::from_str::<serde_json::Value>(line).unwrap())
+ })
+ .filter(|row| row["kind"] == "frame")
+ .collect();
+ let large = rows
+ .iter()
+ .find_map(|row| row["accessibility_tree"][0]["properties"]["value"].as_str())
+ .unwrap();
+ assert_eq!(
+ large.len(),
+ 6_300_000,
+ "full UTF-8 AX bytes survive ticketed replay"
+ );
+ assert!(requests
+ .iter()
+ .filter(|r| r.url.path() == "/api/enterprise/ingest")
+ .all(|r| r
+ .headers
+ .get("x-screenpipe-backfill-images")
+ .is_some_and(|v| v == INDEPENDENT_IMAGES)));
+ assert!(requests
+ .iter()
+ .filter(|r| r.url.path() == "/ax-blob")
+ .all(|r| !r.headers.contains_key("x-license-key")));
+ assert_eq!(rows.len(), super::FRAME_PAGE_LIMIT as usize + 1);
+ assert!(rows
+ .iter()
+ .all(|row| row.to_string().contains("automation_id")));
+ }
+
+ #[tokio::test]
+ async fn screenshots_off_replays_ax_without_accepting_or_reading_pixels() {
+ let _guard = crate::enterprise_policy::sync_streams_test_lock();
+ let _restore = super::super::tests::RestoreDefaultSyncStreams;
+ crate::enterprise_policy::set_sync_streams(
+ true,
+ false,
+ false,
+ false,
+ false,
+ false,
+ "off".into(),
+ "off".into(),
+ );
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/api/enterprise/ingest"))
+ .respond_with(ResponseTemplate::new(200))
+ .expect(4)
+ .mount(&server)
+ .await;
+ let dir = tempfile::TempDir::new().unwrap();
+ let cfg = cfg(&dir, &server);
+ let req = request();
+ let local = PixelGate {
+ history: Local {
+ reads: AtomicUsize::new(0),
+ fail_ui: false,
+ },
+ refused: std::sync::atomic::AtomicBool::new(true),
+ pixel_reads: AtomicUsize::new(0),
+ oversized_ax: false,
+ };
+ let mut cursor = req.initial_cursor();
+ let http = enterprise_http_client();
+ assert!(run_replay_page(&cfg, &mut cursor, &local, &http, &req)
+ .await
+ .unwrap()
+ .may_have_more());
+ cursor = Cursor::load(&cfg.cursor_path);
+ assert!(!run_replay_page(&cfg, &mut cursor, &local, &http, &req)
+ .await
+ .unwrap()
+ .may_have_more());
+ assert!(cursor.boundary.backfill_telemetry_complete);
+ assert!(cursor.boundary.backfill_image_replay.is_none());
+ assert_eq!(local.pixel_reads.load(Ordering::SeqCst), 0);
+ assert_eq!(
+ cursor.boundary.backfill_records,
+ Some(super::FRAME_PAGE_LIMIT as u64 + 1)
+ );
+ // A current server can require image recovery while the native policy
+ // snapshot is off. Accept the work durably, but never read pixels.
+ let mut cloud_all = req.clone();
+ cloud_all.recover_images = Some(true);
+ let mut cfg = cfg.clone();
+ cfg.cursor_path = dir.path().join("cloud-all.json");
+ let mut cursor = cloud_all.initial_cursor();
+ for _ in 0..2 {
+ assert!(
+ run_replay_page(&cfg, &mut cursor, &local, &http, &cloud_all)
+ .await
+ .unwrap_err()
+ .to_string()
+ .contains("screenshot_policy_disabled")
+ );
+ cursor = Cursor::load(&cfg.cursor_path);
+ }
+ assert!(cursor.boundary.backfill_telemetry_complete);
+ assert!(!cursor.boundary.backfill_image_replay.unwrap().complete);
+ assert_eq!(local.pixel_reads.load(Ordering::SeqCst), 0);
+ }
+
+ #[tokio::test]
+ async fn direct_backfill_telemetry_and_images_have_independent_retry_checkpoints() {
+ let _guard = crate::enterprise_policy::sync_streams_test_lock();
+ let _restore = super::super::tests::RestoreDefaultSyncStreams;
crate::enterprise_policy::set_sync_streams(
true,
false,
@@ -676,35 +1108,47 @@ mod tests {
let mut cursor = request.initial_cursor();
let initial = cursor.last_frame_ts.clone();
let http = enterprise_http_client();
- let error = run_one_sync_inner(&cfg, &mut cursor, &local, &http, false, Some(&request))
+ // Telemetry failure retains both initial boundaries. Image work was
+ // accepted durably, but no image reads or uploads have happened yet.
+ assert!(run_replay_page(&cfg, &mut cursor, &local, &http, &request)
+ .await
+ .is_err());
+ assert_eq!(cursor.last_frame_ts, initial);
+ assert_eq!(image_attempts.load(Ordering::SeqCst), 0);
+ cursor = Cursor::load(&cfg.cursor_path);
+ assert!(cursor.boundary.backfill_image_replay.is_some());
+ // A failed image ACK must not roll back successful telemetry.
+ let error = run_replay_page(&cfg, &mut cursor, &local, &http, &request)
.await
.unwrap_err();
assert!(error.to_string().contains("screenshot_status_write_failed"));
- assert_eq!(cursor.last_frame_ts, initial);
- assert!(!cfg.cursor_path.exists());
- assert_eq!(completes.load(Ordering::SeqCst), 0);
- // Next image pass succeeds but telemetry fails: still no checkpoint.
- assert!(
- run_one_sync_inner(&cfg, &mut cursor, &local, &http, false, Some(&request))
- .await
- .is_err()
+ assert_eq!(
+ cursor.boundary.backfill_records,
+ Some(super::FRAME_PAGE_LIMIT as u64)
);
- assert_eq!(cursor.last_frame_ts, initial);
- assert_eq!(cursor.boundary.backfill_records, None);
- assert!(!cfg.cursor_path.exists());
- run_one_sync_inner(&cfg, &mut cursor, &local, &http, false, Some(&request))
- .await
- .unwrap();
- assert_eq!(cursor.boundary.backfill_records, Some(super::FRAME_PAGE_LIMIT as u64));
- let mut restarted = Cursor::load(&cfg.cursor_path);
assert_eq!(
- restarted
+ cursor
.boundary
- .backfill_images
+ .backfill_image_replay
.as_ref()
.unwrap()
- .uploaded,
- 0
+ .cursor
+ .last_frame_ts,
+ initial
+ );
+ // Restart from disk, upload the last tied AX row, retry the first image
+ // page, then finish only the pending image tail without telemetry replay.
+ let mut restarted = Cursor::load(&cfg.cursor_path);
+ assert!(
+ run_replay_page(&cfg, &mut restarted, &local, &http, &request)
+ .await
+ .unwrap()
+ .may_have_more()
+ );
+ assert!(restarted.boundary.backfill_telemetry_complete);
+ assert_eq!(
+ restarted.boundary.backfill_records,
+ Some(super::FRAME_PAGE_LIMIT as u64 + 1)
);
assert_eq!(
restarted
@@ -715,11 +1159,18 @@ mod tests {
.unavailable,
super::FRAME_PAGE_LIMIT as u64
);
- run_one_sync_inner(&cfg, &mut restarted, &local, &http, false, Some(&request))
- .await
- .unwrap();
- assert_eq!(restarted.boundary.backfill_records, Some(super::FRAME_PAGE_LIMIT as u64 + 1));
- assert_eq!(restarted.boundary.backfill_images.unwrap().unavailable, (super::FRAME_PAGE_LIMIT as u64 + 1));
+ restarted = Cursor::load(&cfg.cursor_path);
+ assert!(
+ !run_replay_page(&cfg, &mut restarted, &local, &http, &request)
+ .await
+ .unwrap()
+ .may_have_more()
+ );
+ assert_eq!(
+ restarted.boundary.backfill_images.unwrap().unavailable,
+ super::FRAME_PAGE_LIMIT as u64 + 1
+ );
+ assert_eq!(completes.load(Ordering::SeqCst), 3);
let requests = server.received_requests().await.unwrap();
assert!(!requests
.iter()
@@ -730,22 +1181,13 @@ mod tests {
.map(|r| serde_json::from_slice(&r.body).unwrap())
.collect();
assert_eq!(completions[0], completions[1]);
- // Restore defaults for the shared policy fixture.
- crate::enterprise_policy::set_sync_streams(
- true,
- false,
- true,
- true,
- true,
- true,
- "off".into(),
- "cited".into(),
- );
+
}
#[tokio::test]
async fn all_image_history_exceeds_queue_limits_and_resumes_after_restart() {
let _guard = crate::enterprise_policy::sync_streams_test_lock();
+ let _restore = super::super::tests::RestoreDefaultSyncStreams;
crate::enterprise_policy::set_sync_streams(
true,
false,
@@ -829,13 +1271,12 @@ mod tests {
let history = History(jpeg.into_inner());
let mut cursor = request.initial_cursor();
for _ in 0..2501_u32.div_ceil(super::FRAME_PAGE_LIMIT) {
- run_one_sync_inner(
+ run_replay_page(
&cfg,
&mut cursor,
&history,
&enterprise_http_client(),
- false,
- Some(&request),
+ &request,
)
.await
.unwrap();
@@ -853,21 +1294,13 @@ mod tests {
.unwrap()
.iter()
.all(|r| r.url.path() != "/api/enterprise/frame-requests"));
- crate::enterprise_policy::set_sync_streams(
- true,
- false,
- true,
- true,
- true,
- true,
- "off".into(),
- "cited".into(),
- );
+
}
#[tokio::test]
async fn image_continuation_uses_its_own_checkpoint_and_never_replays_telemetry() {
let _guard = crate::enterprise_policy::sync_streams_test_lock();
+ let _restore = super::super::tests::RestoreDefaultSyncStreams;
crate::enterprise_policy::set_sync_streams(
true,
false,
@@ -1018,16 +1451,7 @@ mod tests {
.to_string()
.contains("screenshot_storage_mode_blocked"));
assert_eq!(local.reads.load(Ordering::SeqCst), reads);
- crate::enterprise_policy::set_sync_streams(
- true,
- false,
- true,
- true,
- true,
- true,
- "off".into(),
- "cited".into(),
- );
+
}
#[tokio::test]
@@ -1063,6 +1487,7 @@ mod tests {
#[tokio::test]
async fn backfill_stable_identity_preserves_range_and_rejects_database_switch() {
let _guard = crate::enterprise_policy::sync_streams_test_lock();
+ let _restore = super::super::tests::RestoreDefaultSyncStreams;
let server = MockServer::start().await;
let dir = tempfile::TempDir::new().unwrap();
let mut cfg = cfg(&dir, &server);
@@ -1201,8 +1626,7 @@ pub(super) async fn fulfill_requests(
}
};
report(cfg, http, &url, &request, &cursor, "running", None).await?;
- match run_one_sync_inner(&replay_cfg, &mut cursor, local, http, false, Some(&request)).await
- {
+ match run_replay_page(&replay_cfg, &mut cursor, local, http, &request).await {
Ok(page) => {
let complete = !page.may_have_more();
// Persist even an empty result; lost completion ACKs then retry
@@ -1220,7 +1644,11 @@ pub(super) async fn fulfill_requests(
.await?;
// JPEG pages are heavier than telemetry pages. Yield to live
// sync after one acknowledged image page, retaining its cursor.
- if complete || page.pending_read || cursor.boundary.backfill_images.is_some() {
+ if complete
+ || page.pending_read
+ || request.images_only
+ || cursor.boundary.backfill_image_replay.is_some()
+ {
break;
}
}
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 3d3dc6894..adab78a07 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
@@ -272,7 +272,11 @@ impl ExportRows<'_> {
for row in self.snapshots.iter() {
let jpeg = match local.fetch_frame_jpeg(row.frame_id).await {
Ok(jpeg) => jpeg,
- Err(EnterpriseSyncError::BackfillImages("screenshot_policy_disabled")) => None,
+ Err(EnterpriseSyncError::BackfillImages(
+ "screenshot_policy_disabled"
+ | "screenshot_local_settings_disabled"
+ | "screenshot_settings_unavailable",
+ )) => None,
Err(error) => return Err(error),
};
if let Some(jpeg) = jpeg {
@@ -513,3 +517,96 @@ mod tests {
assert_eq!(selected, before);
}
}
+
+#[cfg(test)]
+mod snapshot_refusal_tests {
+ use super::*;
+
+ #[tokio::test]
+ async fn snapshot_refusal_keeps_allowed_ax_but_transport_failures_remain_retryable() {
+ struct Local(FrameRow, &'static str);
+ #[async_trait::async_trait]
+ impl LocalApiClient for Local {
+ async fn fetch_frames_since(
+ &self,
+ _: Option<&str>,
+ _: u32,
+ _: u32,
+ ) -> Result<Vec<FrameRow>, EnterpriseSyncError> {
+ Ok(vec![self.0.clone()])
+ }
+ async fn fetch_audio_since(
+ &self,
+ _: Option<&str>,
+ _: u32,
+ _: u32,
+ ) -> Result<Vec<AudioRow>, EnterpriseSyncError> {
+ Ok(vec![])
+ }
+ async fn fetch_frame_jpeg(
+ &self,
+ _: i64,
+ ) -> Result<Option<Vec<u8>>, EnterpriseSyncError> {
+ Err(EnterpriseSyncError::BackfillImages(self.1))
+ }
+ }
+ let frame = FrameRow {
+ frame_id: 1,
+ timestamp: "2026-01-01T01:00:00Z".into(),
+ app_name: None,
+ window_name: None,
+ browser_url: None,
+ text: Some("allowed AX".into()),
+ accessibility_tree: Some(
+ serde_json::json!([{ "role": "Button", "text": "", "properties": { "automation_id": "retained" } }]),
+ ),
+ };
+ for cause in [
+ "screenshot_policy_disabled",
+ "screenshot_local_settings_disabled",
+ "screenshot_settings_unavailable",
+ "screenshot_fetch_failed",
+ ] {
+ let local = Local(frame.clone(), cause);
+ let mut frames = vec![frame.clone()];
+ let mut snapshots = vec![SnapshotRow {
+ frame_id: 1,
+ timestamp: frame.timestamp.clone(),
+ mime: "image/jpeg".into(),
+ image_b64: "AAAA".into(),
+ width: 1,
+ height: 1,
+ }];
+ let result = ExportRows {
+ frames: &mut frames,
+ snapshots: &mut snapshots,
+ audio: &mut vec![],
+ ui: &mut vec![],
+ memories: &mut vec![],
+ parsed: &mut vec![],
+ activities: &mut vec![],
+ feedback: &mut vec![],
+ }
+ .refresh(&local)
+ .await;
+ if cause == "screenshot_fetch_failed" {
+ assert!(
+ result.is_err(),
+ "technical failure must retain the page for retry"
+ );
+ continue;
+ }
+ result.unwrap();
+ assert!(snapshots.is_empty(), "refused pixels must be removed");
+ let body = build_jsonl("device", "label", &frames, &[], &[], &snapshots, &[]);
+ let records = screenpipe_telemetry_wire::parse_jsonl(&body);
+ assert_eq!(records.records.len(), 1);
+ let row: serde_json::Value = serde_json::from_slice(&body).unwrap();
+ assert_eq!(
+ row["accessibility_tree"],
+ frame.accessibility_tree.clone().unwrap()
+ );
+ assert_eq!(row["text"], "allowed AX");
+ }
+ }
+}
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 333cee27c..d508a386b 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/enterprise/sync.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/enterprise/sync.rs
@@ -256,6 +256,10 @@ struct CursorBoundary {
backfill_records: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
backfill_images: Option<backfill::ImageProgress>,
+ #[serde(default)]
+ backfill_telemetry_complete: bool,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ backfill_image_replay: Option<Box<backfill::ImageReplay>>,
frames: u32,
audio: u32,
ui: u32,
@@ -678,7 +682,7 @@ pub async fn post_jsonl(
license_key: &str,
body: Vec<u8>,
) -> Result<(), EnterpriseSyncError> {
- post_jsonl_with_identity(client, url, license_key, body, None, false).await
+ post_jsonl_with_identity(client, url, license_key, body, None, false, false).await
}
async fn post_jsonl_with_identity(
@@ -688,6 +692,7 @@ async fn post_jsonl_with_identity(
body: Vec<u8>,
stable_device_id: Option<&str>,
backfill: bool,
+ independent_images: bool,
) -> Result<(), EnterpriseSyncError> {
// A single retained AX record can exceed the hosted HTTP body limit even
// after splitting the page. Keep every byte and use the same ticketed
@@ -709,20 +714,37 @@ async fn post_jsonl_with_identity(
}
let mut headers = HeaderMap::new();
for (key, value) in identity_headers {
- headers.insert(key, value.parse().map_err(|_| EnterpriseSyncError::Configuration(
- "invalid hosted upload identity header".into(),
- ))?);
+ headers.insert(
+ key,
+ value.parse().map_err(|_| {
+ EnterpriseSyncError::Configuration(
+ "invalid hosted upload identity header".into(),
+ )
+ })?,
+ );
}
headers.insert("x-screenpipe-ingest-transfer", "1".parse().unwrap());
- if backfill { headers.insert("x-screenpipe-backfill", "1".parse().unwrap()); }
+ if backfill {
+ headers.insert("x-screenpipe-backfill", "1".parse().unwrap());
+ if independent_images {
+ headers.insert(
+ "x-screenpipe-backfill-images",
+ backfill::INDEPENDENT_IMAGES.parse().unwrap(),
+ );
+ }
+ }
let ticket = serde_json::json!({"action": "ticket", "device_id": source.device_id,
"content_length": body.len(), "sha256": screenpipe_core::sync::crypto::compute_checksum(&body)});
let mut complete = ticket.clone();
complete["action"] = serde_json::json!("complete");
- TicketedPipeline::new(TicketedConfig::new(url, url)
- .with_http(client.clone()).with_control_headers(headers)
- .with_pinned_upload_hosts(enterprise_upload::pinned_hosts_from_env()))
- .upload(&body, "application/x-ndjson", &ticket, &complete).await?;
+ TicketedPipeline::new(
+ TicketedConfig::new(url, url)
+ .with_http(client.clone())
+ .with_control_headers(headers)
+ .with_pinned_upload_hosts(enterprise_upload::pinned_hosts_from_env()),
+ )
+ .upload(&body, "application/x-ndjson", &ticket, &complete)
+ .await?;
return Ok(());
}
let mut request = client
@@ -731,6 +753,9 @@ async fn post_jsonl_with_identity(
.header("Content-Type", "application/x-ndjson");
if backfill {
request = request.header("X-Screenpipe-Backfill", "1");
+ if independent_images {
+ request = request.header("X-Screenpipe-Backfill-Images", backfill::INDEPENDENT_IMAGES);
+ }
}
if let Some(device) = stable_device_id {
request = request.header("X-Screenpipe-Stable-Device-Id", device);
@@ -997,7 +1022,9 @@ async fn run_one_sync_page(
// Pin this recovery to its database without resetting the admin's
// historical start to the normal first-run safety window.
cursor.source_id = Some(cfg.device_id.clone());
- cursor.save(&cfg.cursor_path)?;
+ if !backfill.is_some_and(|request| request.images_only) {
+ cursor.save(&cfg.cursor_path)?;
+ }
} else {
*cursor = Cursor {
source_id: Some(cfg.device_id.clone()),
@@ -1281,17 +1308,31 @@ async fn run_one_sync_page(
));
}
- // The history cursor is the retry checkpoint for images as well as text.
- // Store this page's images first: the existing server sees them as already
- // resolved, so it need not accumulate another historical request backlog.
- let images = if backfill.is_some()
- && streams.frame_images == crate::enterprise_policy::FrameImagesMode::All
+ // Accept image recovery durably before advancing telemetry. Its independent
+ // timestamp/tie cursor keeps a failed image page retryable after AX moves on.
+ if backfill.is_some_and(|request| !request.images_only)
&& matches!(
cfg.upload_mode,
EnterpriseUploadMode::HostedIngest | EnterpriseUploadMode::DirectReadable(_)
)
&& !frames.is_empty()
{
+ if let Some(replay) = cursor.boundary.backfill_image_replay.as_mut() {
+ // A later telemetry page may observe additional retained frames in
+ // this accepted range. Recheck its tail before declaring completion.
+ replay.complete = false;
+ cursor.save(&cfg.cursor_path)?;
+ } else if backfill.is_some_and(|request| {
+ request
+ .recover_images
+ .unwrap_or(streams.frame_images == crate::enterprise_policy::FrameImagesMode::All)
+ }) {
+ cursor.boundary.backfill_image_replay =
+ Some(Box::new(backfill::ImageReplay::new(cursor)));
+ cursor.save(&cfg.cursor_path)?;
+ }
+ }
+ let images = if backfill.is_some_and(|request| request.images_only) && !frames.is_empty() {
Some(backfill::upload_page_images(cfg, local, http, &frames).await?)
} else {
None
@@ -1401,6 +1442,7 @@ async fn run_one_sync_page(
));
let bytes = body.len();
+ let independent_images = cursor.boundary.backfill_image_replay.is_some();
let mut uploaded = None;
if !body.is_empty() && !backfill.is_some_and(|request| request.images_only) {
match &cfg.upload_mode {
@@ -1446,6 +1488,7 @@ async fn run_one_sync_page(
request_body,
cfg.stable_device_id.as_deref(),
backfill.is_some(),
+ independent_images,
)
.await?;
}
@@ -1493,6 +1536,7 @@ async fn run_one_sync_page(
counts,
enterprise_upload::direct_upload_cursors(&next_cursor),
backfill.is_some(),
+ independent_images,
);
upload.await?;
}
@@ -1533,7 +1577,11 @@ async fn run_one_sync_page(
// Advance cursor only on success — partial failure must not skip records.
*cursor = next_cursor;
- cursor.save(&cfg.cursor_path)?;
+ // The backfill caller commits an image-only cursor with its parent (or
+ // its standalone continuation); it must never overwrite telemetry here.
+ if !backfill.is_some_and(|request| request.images_only) {
+ cursor.save(&cfg.cursor_path)?;
+ }
Ok(report)
}
@@ -3765,7 +3813,7 @@ pub(crate) mod tests {
/// Restore the process-global stream policy even when an intentionally
/// red regression test panics on its assertion.
- struct RestoreDefaultSyncStreams;
+ pub(super) struct RestoreDefaultSyncStreams;
impl Drop for RestoreDefaultSyncStreams {
fn drop(&mut self) {
@@ -5765,15 +5813,12 @@ pub(crate) mod tests {
last_parsed_ts: None,
boundary: CursorBoundary::default(),
};
- let local = MockLocal::new(
- vec![vec![frame(
- 1,
- "2026-05-07T10:00:00Z",
- "Arc",
- "customer-readable text",
- )]],
- vec![vec![]],
- );
+ let rows = vec![frame(1, "2026-05-07T10:00:00Z", "Arc", "customer-readable text")];
+ // The image cursor independently rereads the retained page.
+ let pages = if readable && historical && frame_mode == "all" {
+ vec![rows.clone(), rows]
+ } else { vec![rows] };
+ let local = MockLocal::new(pages, vec![vec![]]);
let http = reqwest::Client::new();
let replay: Option<backfill::BackfillRequest> = historical.then(|| serde_json::from_value(serde_json::json!({
"id": "11111111-1111-1111-1111-111111111111",
@@ -5781,10 +5826,11 @@ pub(crate) mod tests {
"device_id": cfg.device_id,
"start_at": "2026-05-07T09:00:00Z", "end_at": "2026-05-07T11:00:00Z", "streams": ["frames"]
})).unwrap());
- let report =
- run_one_sync_inner(&cfg, &mut cursor, &local, &http, false, replay.as_ref())
- .await
- .unwrap();
+ let report = if let Some(replay) = replay.as_ref() {
+ backfill::run_replay_page(&cfg, &mut cursor, &local, &http, replay).await
+ } else {
+ run_one_sync_inner(&cfg, &mut cursor, &local, &http, false, None).await
+ }.unwrap();
assert_eq!(report.frames, 1);
assert_eq!(
@@ -5800,6 +5846,8 @@ pub(crate) mod tests {
complete.headers.contains_key("x-screenpipe-backfill"),
readable && historical
);
+ assert_eq!(complete.headers.contains_key("x-screenpipe-backfill-images"),
+ readable && historical && frame_mode == "all");
for request in &requests {
if request.url.path() == "/blob" {
assert!(!request.headers.contains_key("x-screenpipe-backfill"));
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/enterprise/upload.rs b/apps/screenpipe-app-tauri/src-tauri/src/enterprise/upload.rs
index 32ba850fd..8d623af9e 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/enterprise/upload.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/enterprise/upload.rs
@@ -436,7 +436,7 @@ pub async fn upload_direct_write_only_batch(
cursors: DirectUploadCursors,
) -> Result<DirectUploadManifest, EnterpriseSyncError> {
let manifest = write_only_direct_upload_manifest(cfg, &plaintext, counts, cursors)?;
- run_ticketed_upload(http, cfg, direct, &manifest, &plaintext, false).await?;
+ run_ticketed_upload(http, cfg, direct, &manifest, &plaintext, false, false).await?;
Ok(manifest)
}
@@ -448,9 +448,19 @@ pub async fn upload_direct_readable_batch(
counts: DirectUploadRecordCounts,
cursors: DirectUploadCursors,
backfill: bool,
+ independent_images: bool,
) -> Result<DirectUploadManifest, EnterpriseSyncError> {
let manifest = readable_direct_upload_manifest(cfg, &plaintext, counts, cursors)?;
- run_ticketed_upload(http, cfg, direct, &manifest, &plaintext, backfill).await?;
+ run_ticketed_upload(
+ http,
+ cfg,
+ direct,
+ &manifest,
+ &plaintext,
+ backfill,
+ independent_images,
+ )
+ .await?;
Ok(manifest)
}
@@ -466,6 +476,7 @@ async fn run_ticketed_upload(
manifest: &DirectUploadManifest,
body: &[u8],
backfill: bool,
+ independent_images: bool,
) -> Result<(), EnterpriseSyncError> {
let mut control_headers = HeaderMap::new();
control_headers.insert(
@@ -479,6 +490,12 @@ async fn run_ticketed_upload(
// requests only; the customer-storage PUT retains its signed headers.
if backfill {
control_headers.insert("x-screenpipe-backfill", "1".parse().unwrap());
+ if independent_images {
+ control_headers.insert(
+ "x-screenpipe-backfill-images",
+ super::backfill::INDEPENDENT_IMAGES.parse().unwrap(),
+ );
+ }
}
let pipeline_cfg = TicketedConfig::new(direct.ticket_url.clone(), direct.complete_url.clone())
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 fcf41176f..f019bbf1e 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/enterprise_sync.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/enterprise_sync.rs
@@ -712,9 +712,22 @@ mod imp {
&self,
frame_id: i64,
) -> Result<Option<Vec<u8>>, EnterpriseSyncError> {
- if !self.image_uploads_allowed() {
- // A settings refusal is not evidence that the local image is missing.
- return Err(EnterpriseSyncError::BackfillImages("screenshot_policy_disabled"));
+ // Separate the local recording preference from the org sync mode.
+ // Missing/unreadable settings fail closed and remain retryable too.
+ let settings = crate::store::SettingsStore::get(&self.app)
+ .map_err(|error| {
+ tracing::warn!(
+ "enterprise screenshot recovery: recording settings read failed: {error}"
+ );
+ EnterpriseSyncError::BackfillImages("screenshot_settings_unavailable")
+ })?
+ .ok_or(EnterpriseSyncError::BackfillImages(
+ "screenshot_settings_unavailable",
+ ))?;
+ if settings.recording.disable_screenshots {
+ return Err(EnterpriseSyncError::BackfillImages(
+ "screenshot_local_settings_disabled",
+ ));
}
// Same image `/frames/{id}` serves in the UI — decoded from local