From 4f89de817fb1f7474f0ad46e819398e2553184c4 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sun, 25 Oct 2026 10:14:04 +0000 Subject: [PATCH 1/4] fix(desktop-gpui): play the start cue only once capture is primed --- apps/desktop-gpui/src/session.rs | 78 +++++++++++++++++-- .../desktop-gpui/src/upload/real_app_tests.rs | 50 +++++++++++- 2 files changed, 122 insertions(+), 6 deletions(-) diff --git a/apps/desktop-gpui/src/session.rs b/apps/desktop-gpui/src/session.rs index b964bbead50..b9851f3f518 100644 --- a/apps/desktop-gpui/src/session.rs +++ b/apps/desktop-gpui/src/session.rs @@ -78,6 +78,24 @@ enum CountdownAdvance { Start, } +/// Waits for the pipeline to be primed, then plays the cue. A start that is +/// dropped before it primes plays nothing. +async fn play_cue_when_primed( + primed: Option>, + gate: cap_recording::RecordingStartGate, + play: F, +) where + F: FnOnce(cap_recording::RecordingStartGate) -> Fut, + Fut: Future, +{ + if let Some(mut primed) = primed + && primed.wait_for(|primed| *primed).await.is_err() + { + return; + } + play(gate).await; +} + fn countdown_advance( phase: Phase, current_generation: u64, @@ -440,6 +458,10 @@ pub struct RecordingSession { /// A capture pipeline that finished warming up while the countdown or the /// start cue was still running; activated the moment the cue ends. primed: Option, + /// Set once the pipeline is primed. The start cue waits for it, so the cue + /// never promises a recording that has not begun capturing (an Instant + /// start waits on the server's video before it builds the pipeline). + primed_ready: Option>, /// Teardown of a cancelled primed start still releasing its devices; the /// next start waits for it before locking the mic and camera again. discarding: Option>>, @@ -488,6 +510,7 @@ impl RecordingSession { start_gate: None, start_cue: None, primed: None, + primed_ready: None, discarding: None, pause_control: PauseControlState::default(), pause_control_error: None, @@ -665,6 +688,7 @@ impl RecordingSession { self.start_gate = Some(start_gate); self.start_cue = Some(crate::app_sounds::prime_recording_start_sound()); self.discard_primed(cx); + self.primed_ready = Some(tokio::sync::watch::channel(false).0); self.pause_control.invalidate(); self.pause_control.uncertain = false; self.pause_control_error = None; @@ -687,7 +711,8 @@ impl RecordingSession { cx.notify(); // The pipeline warms up during the final countdown second so capture is - // live when the cue ends; without a countdown it warms up under the cue. + // live when the cue ends; without a countdown it warms up before the cue. + // Either way the cue waits until the pipeline is primed. if countdown <= 1 { self.prime_engine(config.clone(), generation, cx); } @@ -742,27 +767,29 @@ impl RecordingSession { .detach(); } - /// Plays the start cue; the returned future completes once the recording's - /// start gate is armed, whether the cue played, failed or was aborted. + /// Plays the start cue once the pipeline is primed; the returned future + /// completes once the recording's start gate is armed, whether the cue + /// played, failed or was aborted. fn play_start_cue( &mut self, cx: &mut Context, ) -> std::pin::Pin>> { let gate = self.start_gate.clone().unwrap_or_default(); let cue = self.start_cue.take(); + let primed = self.primed_ready.as_ref().map(|ready| ready.subscribe()); let (abort, registration) = futures_util::future::AbortHandle::new_pair(); self.countdown_cue_abort = Some(abort); let task = gpui_tokio::Tokio::spawn( cx, futures_util::future::Abortable::new( - async move { + play_cue_when_primed(primed, gate, move |gate| async move { match cue { Some(cue) => crate::app_sounds::play_recording_start_sound(cue, gate).await, None => { gate.arm(); } } - }, + }), registration, ), ); @@ -842,6 +869,9 @@ impl RecordingSession { Ok(Ok(active)) => { if this.countdown_remaining.is_some() { this.primed = Some(active); + if let Some(ready) = &this.primed_ready { + ready.send_replace(true); + } } else { this.activate(active, cx); } @@ -1945,6 +1975,7 @@ impl RecordingSession { fn finish(&mut self, cx: &mut Context) { self.abort_start_cue(); self.discard_primed(cx); + self.primed_ready = None; self.start_gate = None; #[cfg(target_os = "linux")] if !self.instant_cleanup_safe() { @@ -1991,6 +2022,43 @@ fn recording_failure_is_current(phase: Phase, current: u64, completed: u64) -> b mod countdown_tests { use super::*; + /// The start cue promises that recording has begun, so it must not play + /// before the pipeline is built (an Instant start first waits for the + /// server to create the video). + #[tokio::test] + async fn start_cue_waits_for_the_primed_pipeline() { + let (ready, primed) = tokio::sync::watch::channel(false); + let played = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let gate = cap_recording::RecordingStartGate::new(); + let cue = tokio::spawn({ + let played = played.clone(); + let gate = gate.clone(); + play_cue_when_primed(Some(primed), gate, move |gate| async move { + played.store(true, std::sync::atomic::Ordering::SeqCst); + gate.arm(); + }) + }); + tokio::time::sleep(Duration::from_millis(200)).await; + assert!(!played.load(std::sync::atomic::Ordering::SeqCst)); + assert!(!gate.is_armed()); + ready.send_replace(true); + cue.await.unwrap(); + assert!(played.load(std::sync::atomic::Ordering::SeqCst)); + assert!(gate.is_armed()); + } + + #[tokio::test] + async fn start_cue_stays_silent_when_the_start_is_dropped_before_it_primes() { + let (ready, primed) = tokio::sync::watch::channel(false); + let gate = cap_recording::RecordingStartGate::new(); + let cue = tokio::spawn(play_cue_when_primed(Some(primed), gate.clone(), |_| async { + panic!("the cue played for a start that never primed"); + })); + drop(ready); + cue.await.unwrap(); + assert!(!gate.is_armed()); + } + #[cfg(target_os = "linux")] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn countdown_stop_cancels_an_unpolled_instant_start() { diff --git a/apps/desktop-gpui/src/upload/real_app_tests.rs b/apps/desktop-gpui/src/upload/real_app_tests.rs index 8db6686779f..becb6247f58 100644 --- a/apps/desktop-gpui/src/upload/real_app_tests.rs +++ b/apps/desktop-gpui/src/upload/real_app_tests.rs @@ -8,7 +8,7 @@ //! cargo test --bin cap-gpui real_app -- --ignored --test-threads=1 //! ``` -use super::verify_tests::{API_KEY, MockCap, OWNER}; +use super::verify_tests::{API_KEY, Fault, MockCap, OWNER, Route, rule}; use serde_json::{Value, json}; use std::{ path::{Path, PathBuf}, @@ -460,3 +460,51 @@ async fn real_app_cancelled_start_deletes_its_video() { } panic!("no stop landed while the start was in flight"); } + +/// The start cue tells the user they are recording. An Instant start first +/// waits for the server to create its video, which takes seconds when earlier +/// takes are still uploading. The cue must wait for capture too: it used to +/// play and arm the start gate long before the pipeline existed, so the +/// opening words of the take were never recorded. +#[ignore] +#[tokio::test(flavor = "multi_thread")] +async fn real_app_start_cue_waits_for_a_slow_server_video() { + let mock = MockCap::start(vec![rule( + Route::Api("video/create"), + Fault::Delay(Duration::from_secs(4)), + )]) + .await; + let sandbox = Sandbox::new(&mock.url, 0); + let app = sandbox.launch("slow-create", &[("CAP_GPUI_AUTO_RECORD", "instant:6")]); + wait_for_app( + &sandbox, + &app, + "a verified upload", + Duration::from_secs(180), + || { + sandbox + .projects() + .first() + .and_then(|project| state(project)) + .is_some_and(|state| state["phase"] == "Verified") + }, + ) + .await; + kill(app); + let log = sandbox.log("slow-create"); + let line = |needle: &str| { + log.lines() + .position(|line| line.contains(needle)) + .unwrap_or_else(|| panic!("no {needle:?} in the app log:\n{log}")) + }; + let building = line("starting recording"); + assert!( + line("Recording start cue playing") > building, + "the start cue played before capture was being built" + ); + assert!( + line("Recording start gate armed") > building, + "the start gate opened before capture was being built" + ); + assert_eq!(mock.created().len(), 1); +} From b8d773c24049cfb69c6043ece271464cabd57a62 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sun, 25 Oct 2026 10:16:33 +0000 Subject: [PATCH 2/4] style: rustfmt --- apps/desktop-gpui/src/session.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/desktop-gpui/src/session.rs b/apps/desktop-gpui/src/session.rs index b9851f3f518..c30faff40e9 100644 --- a/apps/desktop-gpui/src/session.rs +++ b/apps/desktop-gpui/src/session.rs @@ -2051,9 +2051,13 @@ mod countdown_tests { async fn start_cue_stays_silent_when_the_start_is_dropped_before_it_primes() { let (ready, primed) = tokio::sync::watch::channel(false); let gate = cap_recording::RecordingStartGate::new(); - let cue = tokio::spawn(play_cue_when_primed(Some(primed), gate.clone(), |_| async { - panic!("the cue played for a start that never primed"); - })); + let cue = tokio::spawn(play_cue_when_primed( + Some(primed), + gate.clone(), + |_| async { + panic!("the cue played for a start that never primed"); + }, + )); drop(ready); cue.await.unwrap(); assert!(!gate.is_armed()); From 034319da4f8549b3353906853dd6bf13d59ffb34 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sun, 25 Oct 2026 10:47:16 +0000 Subject: [PATCH 3/4] test(desktop-gpui): the slow-server start cue test checks capture was running when the cue ended --- .../desktop-gpui/src/upload/real_app_tests.rs | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/apps/desktop-gpui/src/upload/real_app_tests.rs b/apps/desktop-gpui/src/upload/real_app_tests.rs index becb6247f58..50470710657 100644 --- a/apps/desktop-gpui/src/upload/real_app_tests.rs +++ b/apps/desktop-gpui/src/upload/real_app_tests.rs @@ -252,6 +252,23 @@ fn kill(app: App) { drop(app); } +fn without_ansi(line: &str) -> String { + let mut plain = String::with_capacity(line.len()); + let mut chars = line.chars(); + while let Some(c) = chars.next() { + if c == '\u{1b}' { + for c in chars.by_ref() { + if c.is_ascii_alphabetic() { + break; + } + } + } else { + plain.push(c); + } + } + plain +} + /// A normal Instant recording uploads, verifies, and leaves nothing for the /// cleanup journal. #[ignore] @@ -502,9 +519,23 @@ async fn real_app_start_cue_waits_for_a_slow_server_video() { line("Recording start cue playing") > building, "the start cue played before capture was being built" ); + // Capture that is already running when the cue ends holds its first frame + // behind the start gate. A cue that played first opened the gate before + // any capture existed, so nothing was ever held. + let first_frame = log + .lines() + .map(without_ansi) + .find(|line| line.contains("Start gate") && line.contains("video frame")) + .unwrap_or_else(|| panic!("no first video frame in the app log:\n{log}")); + let held = first_frame + .split("held_frames=") + .nth(1) + .and_then(|rest| rest.split(|c: char| !c.is_ascii_digit()).next()) + .and_then(|count| count.parse::().ok()) + .unwrap_or_else(|| panic!("no held_frames in {first_frame:?}")); assert!( - line("Recording start gate armed") > building, - "the start gate opened before capture was being built" + held >= 1, + "capture was not running when the start cue ended: {first_frame}" ); assert_eq!(mock.created().len(), 1); } From 982c64e7b437566b1de6b98251f9bcd26ccd14ee Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sun, 25 Oct 2026 11:04:37 +0000 Subject: [PATCH 4/4] test(desktop-gpui): accept the held static-screen frame as the first frame --- apps/desktop-gpui/src/upload/real_app_tests.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/desktop-gpui/src/upload/real_app_tests.rs b/apps/desktop-gpui/src/upload/real_app_tests.rs index 50470710657..51851157af5 100644 --- a/apps/desktop-gpui/src/upload/real_app_tests.rs +++ b/apps/desktop-gpui/src/upload/real_app_tests.rs @@ -525,7 +525,10 @@ async fn real_app_start_cue_waits_for_a_slow_server_video() { let first_frame = log .lines() .map(without_ansi) - .find(|line| line.contains("Start gate") && line.contains("video frame")) + .find(|line| { + line.contains("Start gate") + && (line.contains("video frame") || line.contains("frame on screen")) + }) .unwrap_or_else(|| panic!("no first video frame in the app log:\n{log}")); let held = first_frame .split("held_frames=")