From 9252f854368325953c0a8bb1974a6f4beb793076 Mon Sep 17 00:00:00 2001 From: x Date: Wed, 11 Nov 2026 14:18:45 -0500 Subject: [PATCH 01/17] feat(automation): let a host check the action before a delete --- .github/workflows/ci.yml | 1 + crates/automation/src/lib.rs | 42 +++++++++-- crates/automation/src/tests.rs | 133 ++++++++++++++++++++++++++++++++- 3 files changed, 170 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b274a54e2c2..5a8e58fab57 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -343,6 +343,7 @@ jobs: - name: Test desktop regressions run: | cargo test --locked -p cap-utils -p cap-cli-install --lib + cargo test --locked -p cap-automation cargo test --locked -p cap --bin cap record::tests cargo test --locked -p cap --bin cap recordings::tests cargo test --locked -p cap --bin cap automation::tests::cli_save_to_location diff --git a/crates/automation/src/lib.rs b/crates/automation/src/lib.rs index 8189ef97f95..14aea024c45 100644 --- a/crates/automation/src/lib.rs +++ b/crates/automation/src/lib.rs @@ -271,6 +271,22 @@ pub trait AutomationHost: Send + Sync { &self, ctx: &TriggerContext, ) -> impl std::future::Future> + Send; + + /// Called before `DeleteLocalFiles` runs, once the upload checks have passed, with the action + /// that came right before it in the same rule. A host that returns an error keeps the files. + fn verify_before_delete( + &self, + _ctx: &TriggerContext, + _preceding: Option>, + ) -> impl std::future::Future> + Send { + std::future::ready(Ok(())) + } +} + +#[derive(Debug, Clone, Copy)] +pub struct PrecedingAction<'a> { + pub action: &'a Action, + pub succeeded: bool, } pub struct RunResult { @@ -343,16 +359,16 @@ pub async fn run( continue; } - let result = if matches!(action, Action::DeleteLocalFiles) - && requires_upload_verification - { - if remaining_uploads > 0 { + let result = if matches!(action, Action::DeleteLocalFiles) { + let authorized = if !requires_upload_verification { + Ok(()) + } else if remaining_uploads > 0 { Err("Upload has not finished; local recording retained".to_string()) } else if upload_failed { Err("Upload failed; local recording retained".to_string()) } else { match host.upload_is_verified(ctx).await { - Ok(true) => execute_action(host, action, ctx).await, + Ok(true) => Ok(()), Ok(false) => Err( "Upload submitted, but remote verification is unavailable; local recording retained" .to_string(), @@ -361,6 +377,22 @@ pub async fn run( "Upload verification failed; local recording retained: {error}" )), } + }; + match authorized { + Ok(()) => { + let preceding = + action_results + .last() + .map(|previous: &ActionResult| PrecedingAction { + action: &previous.action, + succeeded: previous.success, + }); + match host.verify_before_delete(ctx, preceding).await { + Ok(()) => execute_action(host, action, ctx).await, + Err(error) => Err(error), + } + } + Err(error) => Err(error), } } else { execute_action(host, action, ctx).await diff --git a/crates/automation/src/tests.rs b/crates/automation/src/tests.rs index 059564ece1d..f66f07cb94f 100644 --- a/crates/automation/src/tests.rs +++ b/crates/automation/src/tests.rs @@ -7,6 +7,9 @@ struct MockHost { actions_run: Mutex>, upload_results: Mutex>>, upload_verification: Result, + export_result: Result<(), String>, + delete_guard: Option>) -> Result<(), String>>, + preceding_seen: Mutex>>, } impl MockHost { @@ -16,6 +19,9 @@ impl MockHost { actions_run: Mutex::new(Vec::new()), upload_results: Mutex::new(VecDeque::new()), upload_verification: Ok(false), + export_result: Ok(()), + delete_guard: None, + preceding_seen: Mutex::new(Vec::new()), } } @@ -59,7 +65,7 @@ impl AutomationHost for MockHost { _dest: &ExportDestination, ) -> Result<(), String> { self.record("export"); - Ok(()) + self.export_result.clone() } async fn upload( @@ -141,6 +147,20 @@ impl AutomationHost for MockHost { self.record("delete_local_files"); Ok(()) } + + async fn verify_before_delete( + &self, + _ctx: &TriggerContext, + preceding: Option>, + ) -> Result<(), String> { + self.preceding_seen.lock().unwrap().push( + preceding.map(|preceding| (format!("{:?}", preceding.action), preceding.succeeded)), + ); + match self.delete_guard { + Some(guard) => guard(preceding), + None => Ok(()), + } + } } fn screenshot_copy_rule() -> AutomationRule { @@ -1076,3 +1096,114 @@ fn multiple_rules_same_trigger() { let matched = evaluate(&store, &Trigger::ScreenshotTaken, &TriggerContext::new()); assert_eq!(matched.len(), 2); } + +fn export_then_delete_store() -> AutomationsStore { + let mut store = AutomationsStore { + version: 1, + rules: vec![studio_export_rule()], + }; + store.rules[0].conditions.clear(); + store.rules[0].actions.push(Action::DeleteLocalFiles); + store +} + +fn require_successful_export(preceding: Option>) -> Result<(), String> { + match preceding { + Some(PrecedingAction { + action: Action::Export { .. }, + succeeded: true, + }) => Ok(()), + _ => Err("refused".to_string()), + } +} + +#[tokio::test] +async fn delete_guard_sees_the_action_right_before_it_and_its_outcome() { + for export_succeeds in [true, false] { + let mut host = MockHost::new(desktop_capabilities()); + if !export_succeeds { + host.export_result = Err("encoder failed".to_string()); + } + let result = run( + &host, + &export_then_delete_store(), + &Trigger::StudioRecordingFinished, + &TriggerContext::new(), + ) + .await; + let seen = host.preceding_seen.lock().unwrap().clone(); + assert_eq!(seen.len(), 1); + let (action, succeeded) = seen[0].clone().unwrap(); + assert!(action.starts_with("Export"), "{action}"); + assert_eq!(succeeded, export_succeeds); + assert_eq!(result[0].action_results[0].success, export_succeeds); + } +} + +#[tokio::test] +async fn delete_guard_refusal_keeps_files_and_reports_the_reason() { + let mut host = MockHost::new(desktop_capabilities()); + host.export_result = Err("encoder failed".to_string()); + host.delete_guard = Some(require_successful_export); + let result = run( + &host, + &export_then_delete_store(), + &Trigger::StudioRecordingFinished, + &TriggerContext::new(), + ) + .await; + assert_eq!(host.actions_run(), vec!["export"]); + let delete = &result[0].action_results[1]; + assert!(!delete.success); + assert_eq!(delete.error.as_deref(), Some("refused")); + + let mut host = MockHost::new(desktop_capabilities()); + host.delete_guard = Some(require_successful_export); + let mut store = export_then_delete_store(); + store.rules[0].actions.remove(0); + let result = run( + &host, + &store, + &Trigger::StudioRecordingFinished, + &TriggerContext::new(), + ) + .await; + assert!(host.actions_run().is_empty()); + assert_eq!(*host.preceding_seen.lock().unwrap(), vec![None]); + assert!(!result[0].action_results[0].success); + + let mut host = MockHost::new(desktop_capabilities()); + host.delete_guard = Some(require_successful_export); + run( + &host, + &export_then_delete_store(), + &Trigger::StudioRecordingFinished, + &TriggerContext::new(), + ) + .await; + assert_eq!(host.actions_run(), vec!["export", "delete_local_files"]); +} + +#[tokio::test] +async fn delete_guard_runs_only_after_upload_checks_pass() { + let mut host = MockHost::new(desktop_capabilities()); + host.delete_guard = Some(require_successful_export); + let store = upload_cleanup_store( + Trigger::StudioRecordingFinished, + vec![upload_action(), Action::DeleteLocalFiles], + ); + let result = run( + &host, + &store, + &Trigger::StudioRecordingFinished, + &TriggerContext::new(), + ) + .await; + assert!(host.preceding_seen.lock().unwrap().is_empty()); + assert!(!result[0].action_results[1].success); + assert!( + !host + .actions_run() + .contains(&"delete_local_files".to_string()) + ); +} From 92902059b8bcc875b1c187d28683c8ebb45e7488 Mon Sep 17 00:00:00 2001 From: x Date: Wed, 11 Nov 2026 14:18:45 -0500 Subject: [PATCH 02/17] feat(desktop-gpui): run saved automation rules --- apps/desktop-gpui/Cargo.lock | 16 + apps/desktop-gpui/Cargo.toml | 1 + apps/desktop-gpui/src/app_windows.rs | 36 +- apps/desktop-gpui/src/automation.rs | 1054 +++++++++++++++++++++ apps/desktop-gpui/src/automation/tests.rs | 646 +++++++++++++ apps/desktop-gpui/src/import.rs | 1 + apps/desktop-gpui/src/main.rs | 2 + apps/desktop-gpui/src/screenshot.rs | 14 +- apps/desktop-gpui/src/session.rs | 27 +- apps/desktop-gpui/src/store.rs | 4 + apps/desktop-gpui/src/upload/queue.rs | 12 + 11 files changed, 1801 insertions(+), 12 deletions(-) create mode 100644 apps/desktop-gpui/src/automation.rs create mode 100644 apps/desktop-gpui/src/automation/tests.rs diff --git a/apps/desktop-gpui/Cargo.lock b/apps/desktop-gpui/Cargo.lock index f0376b8c303..d92ee369ed7 100644 --- a/apps/desktop-gpui/Cargo.lock +++ b/apps/desktop-gpui/Cargo.lock @@ -1397,6 +1397,21 @@ dependencies = [ "workspace-hack", ] +[[package]] +name = "cap-automation" +version = "0.1.0" +dependencies = [ + "cap-project", + "serde", + "serde_json", + "specta", + "thiserror 1.0.69", + "tokio", + "tracing", + "uuid", + "workspace-hack", +] + [[package]] name = "cap-camera" version = "0.1.0" @@ -1544,6 +1559,7 @@ dependencies = [ "block2 0.6.2", "bytes", "cap-audio", + "cap-automation", "cap-camera", "cap-camera-effects", "cap-cli-install", diff --git a/apps/desktop-gpui/Cargo.toml b/apps/desktop-gpui/Cargo.toml index cfe82c3cfc3..e21e8aaeb5e 100644 --- a/apps/desktop-gpui/Cargo.toml +++ b/apps/desktop-gpui/Cargo.toml @@ -57,6 +57,7 @@ cap-enc-ffmpeg = { path = "../../crates/enc-ffmpeg" } cap-timestamp = { path = "../../crates/timestamp" } cap-utils = { path = "../../crates/utils" } cap-project = { path = "../../crates/project" } +cap-automation = { path = "../../crates/automation" } # The cursor style picker draws the same SVG assets the renderer composites. # gpui's `svg()` keeps only a glyph's alpha, so the two-tone cursor art has to # be rasterised instead: `resvg` at the pin cap-rendering already builds, into diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs index 2549b3935db..c91f2c3d027 100644 --- a/apps/desktop-gpui/src/app_windows.rs +++ b/apps/desktop-gpui/src/app_windows.rs @@ -634,10 +634,11 @@ pub fn init(main: WindowHandle, session: Entity, c if !restore_clean_capture_ui(cx) { return; } - let (finished_studio, preparing_editor, finished_instant, deleted) = + let (finished_studio, studio_behaviour, preparing_editor, finished_instant, deleted) = session.update(cx, |session, _| { ( session.finished_studio.take(), + session.studio_behaviour.take(), session.take_preparing_studio_editor(), std::mem::take(&mut session.finished_instant), std::mem::take(&mut session.deleted_recording), @@ -687,7 +688,8 @@ pub fn init(main: WindowHandle, session: Entity, c // deleted from the bar reshows it only for "Reopen recording // window", as the Tauri `delete_recording` did. let settings = crate::store::GeneralSettings::load(); - let behaviour = settings.post_studio_recording_behaviour; + let behaviour = + studio_behaviour.unwrap_or(settings.post_studio_recording_behaviour); // Linux restores the main window through the clean-capture // lease before this runs, so it keeps reshowing as before. A // main window reopened mid-recording (even if minimized since) @@ -767,8 +769,7 @@ fn open_preparing_studio_editor(session: &Entity, cx: &mut App if !pending.capture_stopped || pending.presentation != StudioEditorPresentation::Pending || !pending.finalization.is_finalizing() - || crate::store::GeneralSettings::load().post_studio_recording_behaviour - != crate::store::PostStudioBehaviour::OpenEditor + || session.read(cx).studio_behaviour != Some(crate::store::PostStudioBehaviour::OpenEditor) { return; } @@ -7489,7 +7490,7 @@ pub fn empty_overlays_for_screenshot(cx: &mut App) -> Option