pub(crate) mod attachment_sync; mod automation; mod chrome; mod cloud_api; mod device_approval; mod enterprise_capture; pub mod instruction; pub(crate) mod lifecycle; pub mod lock; mod meeting_schedule; mod note_windows; mod notifications; mod release_notes; mod shared_notes; mod startup; pub mod team_invitations; pub mod toasts; use std::{ path::PathBuf, sync::{ Arc, atomic::{AtomicBool, Ordering}, }, time::Duration, }; use desktop_runtime::{DocumentSnapshot, Reply, RuntimeHandle, ServiceError, SessionId}; use futures::{StreamExt, future::BoxFuture}; use gpui::{ AppContext, Context, Entity, Focusable, Render, Subscription, Window, div, prelude::*, px, }; use crate::ui::a11y::Accessible; use crate::{ application_services::{Effects, HostCommand, NativeServices}, contracts::{ EditorEvent, EditorInit, LaneContext, MeetingEvent, MeetingIntent, ProductEvent, ProductRoute, WorkspaceEvent, }, editor::{EditorPane, menu::EditorRequest}, meeting::{ MeetingPane, MeetingServices, capture::{CaptureService, Phase}, host::{self, MeetingHost}, playback::{self, Playback}, recovery, retention, }, product::{ OpenWorkspaceSection, ProductPane, cloud::{auth::SecureAuthProvider, host::NativeHost}, local::HostEffects, }, runtime_bridge, ui::theme::theme, workspace::{NoteEvent, WorkspaceAction, WorkspaceView, navigation::Route, pins}, }; struct ProfileStorage { root: PathBuf, vault: PathBuf, } impl anlg_storage::StorageRuntime for ProfileStorage { fn global_base(&self) -> Result { Ok(self.root.clone()) } fn vault_base(&self) -> Result { Ok(self.vault.clone()) } } struct PendingProduct { route: ProductRoute, workspace_route: Route, settings_section: Option, } pub struct ApplicationView { runtime: RuntimeHandle, window: gpui::AnyWindowHandle, focus: gpui::FocusHandle, restore_focus: Option, pre_gate_focus: Option, gate_active: bool, #[cfg(test)] force_window_chrome: bool, notifications: notifications::Notifications, schedule: meeting_schedule::MeetingSchedule, preferences_task: Option>, chrome: chrome::Chrome, workspace: Entity, editor: Option>, meeting: Option>, meeting_host: MeetingHost, main_window_active: bool, floating_pinned_session: Option, automation: Entity, dictation: Option>, product: Option>, pending_product: Option, startup_error: Option, trial_dialogs: Entity, instruction: Entity, services: Option, native: Option, storage_channel: Option, storage_root: PathBuf, editor_vault: PathBuf, writers_paused: bool, #[cfg(test)] host_effects: Arc, background_cancel: desktop_runtime::CancellationToken, lifecycle: lifecycle::Lifecycle, toasts: Entity, meeting_open: bool, capture_session: Option, /// When this app first saw the current capture, for the short-recording check. capture_started: Option, closing: bool, drain_timed_out: bool, drained: bool, subscriptions: Vec, editor_subscriptions: Vec, meeting_subscription: Option, product_subscriptions: Vec, pins_revision: Option, pins_saving: bool, pins_pending: Option>, note_windows: std::collections::HashMap>, note_subscriptions: std::collections::HashMap>, } impl ApplicationView { pub fn native_ready(&self) -> bool { self.native.is_some() } pub fn workspace(&self) -> &Entity { &self.workspace } pub fn trial_dialogs(&self) -> &Entity { &self.trial_dialogs } pub(super) fn background_cancel(&self) -> desktop_runtime::CancellationToken { self.background_cancel.child_token() } pub fn new( runtime: RuntimeHandle, ready: Reply<()>, storage: crate::launch::Storage, window: &mut Window, cx: &mut Context, ) -> Self { let storage_channel = storage.channel; let storage_root = storage.root.clone(); let profile = storage.root.clone(); let shipping_store = storage.shipping_store(); let shipping_webview = storage.shipping_webview(); let trial_mirror = storage.shipping_webview(); cx.set_global(crate::shipping_store::ShippingStore(shipping_store.clone())); // Installed first: the note and transcript views read the live meeting from it. let meeting_host = MeetingHost::install(cx); let toasts = toasts::install(cx); let live = meeting_host.live().clone(); let automation = cx.new(|cx| { crate::meeting::automation::MeetingAutomation::new( runtime.clone(), Some(profile.clone()), window, cx, ) }); let workspace = cx.new(|cx| { WorkspaceView::new( LaneContext { runtime: runtime.clone(), }, ready, window, cx, ) }); let uploads = crate::meeting::upload::Uploads::global(cx); let summary_jobs = crate::workspace::SummaryJobs::global(cx); let upload_runtime = runtime.clone(); let mut subscriptions = vec![ cx.observe(&live, |this, _, cx| this.live_changed(cx)), cx.subscribe(&uploads, move |_, _, event, cx| { if let crate::meeting::upload::UploadEvent::Transcribed(session) = event { let session = session.clone(); let runtime = upload_runtime.clone(); crate::workspace::SummaryJobs::global(cx).update(cx, |jobs, cx| { jobs.claim_auto_summary(runtime, session, cx); }); } }), cx.subscribe(&summary_jobs, |this, _, event, cx| { if let crate::workspace::SummaryEvent::Completed { session, title, .. } = event { this.summary_completed(session.clone(), title, cx); } }), cx.observe_window_activation(window, |this, window, cx| { let active = window.is_window_active(); if this.main_window_active != active { this.main_window_active = active; if active { this.floating_pinned_session = None; } this.sync_floating_bar(cx); } }), cx.on_focus_lost(window, |this, window, cx| { if this.closing || this.writers_paused { this.focus.focus(window, cx); } else if this.product.is_none() && !this.instruction.read(cx).is_active() && !startup::is_active(cx) && !lock::is_gated(cx) { cx.focus_view(&this.workspace, window); cx.notify(); } }), cx.subscribe_in(&workspace, window, |this, _, event, window, cx| { this.workspace_event(event, window, cx) }), cx.observe(&workspace, |this, workspace, cx| { if let Some(pending) = &this.pending_product && workspace.read(cx).current_route() != pending.workspace_route { this.pending_product = None; } }), cx.subscribe(&workspace, |this, _, event, cx| match event { WorkspaceAction::SidebarChanged => cx.notify(), WorkspaceAction::OpenShared(id) => this.mount_shared(id.to_string(), false, cx), WorkspaceAction::NewNoteWindow => this.open_note_window(None, cx), WorkspaceAction::OpenNoteWindow(id) => this.open_note_window(Some(id.clone()), cx), WorkspaceAction::PinnedChanged(routes) => this.save_pins(routes.clone(), cx), WorkspaceAction::OpenFloatingPanel => { this.floating_pinned_session = this .meeting_host .live() .read(cx) .current() .filter(|live| live.mode == crate::meeting::live::SessionMode::Active) .map(|live| live.session.clone()); this.sync_floating_bar(cx); this.meeting_host.show_floating(cx); } }), // Plan-gated surfaces read the account while rendering. cx.observe_global::(|_, cx| cx.notify()), // Re-resolve open editor attachments and audio playback when cloud hydration downloads finish. cx.observe_global::( |this, cx| { let generation = cx .try_global::() .map_or(0, |synced| synced.0); crate::editor::AttachmentService::notify_synced(generation); let service = crate::editor::AttachmentService::new( this.runtime.clone(), this.editor_vault.clone(), ) .with_generation(generation); if let Some(editor) = &this.editor { let session_id = editor.read(cx).init.session_id.clone(); editor.update(cx, |editor, cx| { editor.set_attachment_service(service.clone(), cx) }); // Propagates the bumped AttachmentService to all summary editors in NoteView. this.workspace.update(cx, |workspace, cx| { workspace.set_editor_content(&session_id, editor.clone().into(), cx) }); } // Refresh NoteView audio playback state across main and detached note windows. if cx.has_global::() { cx.update_global::(|_, _| {}); } this.note_windows .retain(|_, window| window.read(cx).is_ok()); for window in this.note_windows.values() { let _ = window.update(cx, |_, _, cx| cx.notify()); } cx.refresh_windows(); cx.notify(); }, ), cx.subscribe_in( &automation, window, |this, _, request: &crate::meeting::automation::Request, window, cx| { this.automation_request(request, window, cx) }, ), cx.subscribe(&automation, |this, _, observed, cx| { this.meeting_observed(observed, cx) }), ]; subscriptions.push(lock::install(&runtime, window, cx)); startup::install(&runtime, cx); subscriptions.push(cx.observe_global::(|_, cx| cx.notify())); let dictation = match crate::dictation::Dictation::install(runtime.clone(), cx) { Ok(dictation) => { subscriptions.push(cx.subscribe(&dictation, |this, _, failed, cx| { let crate::dictation::DictationFailed(message) = failed; this.status(message, cx) })); Some(dictation) } Err(error) => { tracing::warn!(%error, "dictation is unavailable"); None } }; crate::product::settings::apply_stored_preferences(&runtime, cx); let pins_runtime = runtime.clone(); let pins_store = shipping_store.clone(); cx.spawn(async move |this, cx| { let mut state = pins_runtime.state(); loop { if matches!( *state.borrow_and_update(), desktop_runtime::RuntimeState::Ready ) { break; } if matches!(*state.borrow(), desktop_runtime::RuntimeState::Closed(_)) { return; } if state.changed().await.is_err() { return; } } let result = match pins::load(&pins_runtime, pins_store) { Ok(reply) => reply.receive().await, Err(error) => Err(error), }; let _ = this.update(cx, |this, cx| match result { Ok((revision, routes)) => { this.pins_revision = Some(revision); this.workspace.update(cx, |workspace, cx| { workspace.restore_pinned(&routes, cx); workspace.set_pin_persistence(true); }); } Err(error) => this.status(&error.to_string(), cx), }); let _ = this.update(cx, |_, cx| { release_notes::open_after_update(pins_runtime.clone(), cx) }); }) .detach(); let startup_runtime = runtime.clone(); let telemetry_store = shipping_store.clone(); let editor_vault = storage.vault(); let tray_settings = match shipping_store.clone() { Some(store) => lifecycle::TraySettings::Shipping(store), None => lifecycle::TraySettings::File(profile.join("tray.json")), }; let (effects, mut commands) = Effects::channel(runtime.clone()); #[cfg(test)] let host_effects = effects.clone(); let flush_effects = effects.clone(); let instruction = cx.new(|cx| instruction::Instruction::new(window.window_handle(), cx)); subscriptions.push( cx.subscribe_in(&instruction, window, |this, _, event, window, cx| { this.instruction_event(event, window, cx) }), ); let card = instruction.downgrade(); let host = NativeHost::install( cx, move |_, _| flush_effects.flush_drafts(), move |url, handoff, cx| instruction::hand_off(card.clone(), url, handoff, cx), ); cx.spawn(async move |this, cx| { while let Some(command) = commands.next().await { match command { HostCommand::Flush(reply) => { let result = match this.update(cx, |this, cx| this.flush_drafts(cx)) { Ok(flush) => flush.await, Err(_) => Err(ServiceError::Closed), }; let _ = reply.send(result); } HostCommand::Pause(reply) => { let result = match this.update(cx, |this, cx| this.pause_writers(cx)) { Ok(flush) => flush.await, Err(_) => Err(ServiceError::Closed), }; // A pause that could not drain must not leave the app read-only. if result.is_err() { let _ = this.update(cx, |this, cx| { if this.writers_paused { this.resume_writers(cx); } }); } let _ = reply.send(result); } HostCommand::Resume(reply) => { let result = this .update(cx, |this, cx| { this.resume_writers(cx); }) .map_err(|_| ServiceError::Closed); let _ = reply.send(result); } HostCommand::Relaunch(reply) => { crate::platform::windows::request_restart( crate::platform::windows::Restart::App, ); let _ = reply.send(Ok(())); cx.background_executor() .timer(Duration::from_millis(100)) .await; let _ = this.update(cx, |this, cx| this.request_quit(cx)); } } } }) .detach(); let vault = editor_vault.clone(); let startup = cx.background_executor().spawn(async move { let mut state = startup_runtime.state(); loop { match state.borrow_and_update().clone() { desktop_runtime::RuntimeState::Ready => break, desktop_runtime::RuntimeState::Closed(result) => { return result.and(Err(ServiceError::Closed)); } _ => {} } state.changed().await.map_err(|_| ServiceError::Closed)?; } if let Some(store) = shipping_store { let imported = crate::product::onboarding::import_shipping(&startup_runtime, store).await; if let Err(error) = imported { tracing::warn!(%error, "the shipping app's onboarding state was not read"); } } if let Ok(reply) = crate::product::settings::watch(&startup_runtime) && let Ok(watch) = reply.receive().await { let snapshot = crate::product::settings::decode(&watch.snapshots.borrow().rows).ok(); let _ = watch.unsubscribe().await; if let Some(snapshot) = snapshot { let (telemetry, crash) = crate::telemetry::consent_from_settings(&snapshot); crate::telemetry::set_telemetry_enabled(telemetry); crate::telemetry::set_crash_reporting_enabled(crash); crate::telemetry::sync_settings_properties(&snapshot); } } crate::telemetry::track("app_started", &[]); crate::telemetry::track("app_opened", &[]); crate::telemetry::track_first_open_if_needed(telemetry_store.as_ref()); if let Some(mirror) = shipping_webview { crate::shipping_webview::import(&mirror, &profile); } let native = NativeServices::new(startup_runtime.clone(), storage, effects.clone(), host) .await?; let storage = Arc::new(ProfileStorage { root: profile, vault: vault.clone(), }); let audio_vault = vault.clone(); let recovery_runtime = startup_runtime.clone(); let resolver = native.providers.recovery_resolver(); let recovery_result = match startup_runtime.service(move |_| async move { recovery::recover_startup( &recovery_runtime, vault, retention::Activities::default(), resolver, ) .await }) { Ok(reply) => reply.receive().await, Err(error) => Err(error), }; let (interrupted, recovery_error) = match recovery_result { Ok(reports) => ( reports .into_iter() .filter(|report| report.result.is_err()) .collect::>(), None, ), Err(error) => { tracing::error!(%error, "interrupted capture recovery failed on startup"); (Vec::new(), Some(error)) } }; // Spawn CaptureService after startup recovery inspects SQLite retention markers so // RootActor's startup janitor cannot race ahead of recovery. let capture = CaptureService::spawn( startup_runtime.clone(), Arc::new(anlg_audio_actual::ActualAudio), storage, native.providers.start_resolver(), )?; let services = MeetingServices { capture, playback: Playback::spawn()?, vault: audio_vault, }; effects.attach(&services); let capture = services.capture.clone(); let ai = native .providers .ai_services(Arc::new(move |session| capture.is_active(session)))?; Ok((services, native, ai, interrupted, recovery_error)) }); cx.spawn(async move |this, cx| { let result = startup.await; let _ = this.update(cx, |this, cx| match result { Ok((services, native, ai, interrupted, recovery_error)) => { cx.set_global(services.clone()); cx.set_global(ai); cx.set_global(native.providers.clone()); this.subscriptions .push(crate::product::providers::configure_when_paid( native.providers.clone(), cx, )); if let Some(dictation) = &this.dictation { let voice = dictation.read(cx).voice(native.providers.clone()); cx.set_global(voice); } let vault = services.vault.clone(); this.start_retention(vault.clone(), services.capture.activities.clone(), cx); this.services = Some(services); let calendar = crate::workspace::calendar_accounts::HostCalendarAccounts::new( this.runtime.clone(), native.local.clone(), native.cloud.service.clone(), ); this.workspace.update(cx, |workspace, cx| { workspace.set_calendar_accounts(Some(Arc::new(calendar)), cx) }); if let Some(cloud) = native.cloud.service.clone() { this.watch_cloud(cloud, cx); } cloud_api::start(&native, cx); this.native = Some(native); if let Some(pending) = this.pending_product.take() && this.workspace.read(cx).current_route() == pending.workspace_route { let window = this.window; let view = cx.entity().downgrade(); let route = pending.route; let expected = pending.workspace_route; let section = pending.settings_section; cx.defer(move |cx| { let _ = cx.update_window(window, |_, window, cx| { let _ = view.update(cx, |this, cx| { if this.product.is_none() && this.workspace.read(cx).current_route() == expected { this.open_product_with_section(route, section, window, cx); } }); }); }); } this.start_schedule(cx); this.watch_preferences(cx); this.start_lifecycle(cx); this.watch_updates(cx); this.open_onboarding_if_needed(cx); if let Some(error) = recovery_error { this.status( &format!("Interrupted recording recovery failed: {error}"), cx, ); } for capture in interrupted { this.offer_discard(capture, vault.clone(), cx); } } Err(error) => { this.pending_product = None; this.startup_error = Some(error.to_string()); this.status(&format!("Meeting startup failed: {error}"), cx); } }); }) .detach(); let trial_dialogs = cx .new(|cx| crate::product::trial::TrialDialogs::new(runtime.clone(), trial_mirror, cx)); subscriptions.push(cx.subscribe( &trial_dialogs, |this, _, _: &crate::product::trial::ReferFriend, cx| { this.workspace.update(cx, |workspace, cx| { workspace.open_route(Route::settings("referrals"), true, cx) }) }, )); let app_weak = cx.weak_entity(); subscriptions.push(cx.intercept_keystrokes(move |event, window, cx| { let _ = app_weak.update(cx, |this, cx| { if !(this.closing || this.writers_paused) { return; } // Let quit shortcuts pass through to raw `KeyDownEvent` handlers so held // auto-repeat events (`event.is_held`) cannot trigger force-quit. if is_quit_shortcut(&event.keystroke) { return; } if crate::ui::a11y::is_window_input_blocked(window, cx) { cx.stop_propagation(); } }); })); Self { window: window.window_handle(), focus: cx.focus_handle(), restore_focus: None, pre_gate_focus: None, gate_active: false, #[cfg(test)] force_window_chrome: false, notifications: notifications::Notifications::default(), schedule: Default::default(), preferences_task: None, runtime, chrome: chrome::Chrome::default(), workspace, editor: None, meeting: None, meeting_host, main_window_active: window.is_window_active(), floating_pinned_session: None, automation, dictation, product: None, pending_product: None, startup_error: None, trial_dialogs, instruction, services: None, native: None, storage_channel, storage_root, editor_vault, writers_paused: false, #[cfg(test)] host_effects, background_cancel: desktop_runtime::CancellationToken::new(), lifecycle: lifecycle::Lifecycle::new(tray_settings), toasts, capture_session: None, capture_started: None, meeting_open: false, closing: false, drain_timed_out: false, drained: false, subscriptions, editor_subscriptions: Vec::new(), meeting_subscription: None, product_subscriptions: Vec::new(), pins_revision: None, pins_saving: false, pins_pending: None, note_windows: Default::default(), note_subscriptions: Default::default(), } } pub fn native_event( &mut self, event: crate::native_events::NativeEvent, window: &mut Window, cx: &mut Context, ) { use crate::native_events::NativeEvent; match event { NativeEvent::NewNote => { self.show_main(window, cx); self.workspace.update(cx, |view, cx| view.create(false, cx)); } NativeEvent::About => self.show_about(cx), NativeEvent::CloseMain => self.close_main(window, cx), NativeEvent::Quit => self.quit_now(window, cx), NativeEvent::Action(action) => self.lifecycle_action(action, window, cx), NativeEvent::DeepLink { raw, .. } => { if let Ok(link) = desktop_runtime::deeplink::DeepLink::parse(&raw) && instruction::dismissed_by(&link) { self.instruction.update(cx, |card, cx| card.dismiss(cx)); if matches!(link, desktop_runtime::deeplink::DeepLink::Integration(_)) { self.workspace .update(cx, |view, cx| view.integrations_changed(cx)); } } match desktop_runtime::deeplink::DeepLink::parse(&raw) { Ok(desktop_runtime::deeplink::DeepLink::ShareAccount { share_id }) => { self.open_shared(share_id, false, cx) } Ok(desktop_runtime::deeplink::DeepLink::ShareHandoff { request_id }) => { self.open_shared(request_id, true, cx) } Ok(desktop_runtime::deeplink::DeepLink::OnboardingComplete) => { let Some(services) = self.services.clone() else { return; }; let Some(session) = services.capture.take_update(u64::MAX).session else { return; }; let active = session.clone(); let reply = self.runtime.read(desktop_runtime::CancellationToken::new(), move |services| async move { let rows = services.executor.execute("SELECT id FROM sessions WHERE id=? AND deleted_at IS NULL AND CASE WHEN json_valid(event_json) THEN json_extract(event_json,'$.tracking_id') END='anarlog-onboarding-demo-v1'".into(), vec![serde_json::json!(session.0)]).await.map_err(|error| ServiceError::Failed(error.to_string().into()))?; Ok(!rows.is_empty()) }); cx.spawn(async move |this, cx| { let result: desktop_runtime::Result<()> = async { if reply?.receive().await? && services.capture.is_active(&active) { services .capture .stop()? .await .map_err(|_| ServiceError::Closed)??; } Ok(()) } .await; if let Err(error) = result { let _ = this.update(cx, |this, cx| this.status(&error.to_string(), cx)); } }) .detach(); } Ok(desktop_runtime::deeplink::DeepLink::Integration(callback)) => { let Some(native) = &self.native else { return; }; let calendar = native.local.calendar.clone(); let runtime = self.runtime.clone(); let return_to = callback.return_to.clone(); let job = runtime.clone().service(move |services| async move { calendar .reconcile(&runtime, callback, services.shutdown_requested) .await }); cx.spawn(async move |this, cx| { let result = async { job?.receive().await }.await; let _ = this.update(cx, |this, cx| match result { Ok(()) => { if matches!( return_to.as_deref(), Some("calendar" | "settings-calendar") ) { this.leave_product(Route::Calendar, cx); } toasts::success("Integration refreshed.", cx); } Err(error) => this.status(&error.to_string(), cx), }); }) .detach(); } Ok(link) => { let cloud = self .native .as_ref() .and_then(|native| native.cloud.service.clone()); let Some(cloud) = cloud else { self.status("Configure cloud sign-in before opening this link.", cx); return; }; cx.spawn(async move |this, cx| { let is_auth = matches!(link, desktop_runtime::deeplink::DeepLink::Auth { .. }); let result = match url::Url::parse(&raw) { Ok(url) if is_auth => cloud.handle_deep_link(url).await.map(|_| ()), Ok(_) => { let billing = matches!( link, desktop_runtime::deeplink::DeepLink::BillingRefresh ); cloud.refresh(billing).await } Err(_) => Err(ServiceError::Failed("Invalid deep link".into())), }; let _ = this.update(cx, |this, cx| match result { Ok(_) => { if is_auth { crate::telemetry::track("user_signed_in", &[]); } cloud.publish_account(cx); toasts::success("Account updated.", cx) } Err(error) => this.status(&error.to_string(), cx), }); }) .detach(); } Err(error) => self.status(&error.to_string(), cx), } } } } /// `share/open` links: an account link refreshes the cached copy, then opens the shared /// note; a handoff link opens a view-only preview. fn open_shared(&mut self, id: String, handoff: bool, cx: &mut Context) { if !self .workspace .update(cx, |workspace, cx| workspace.can_close(cx)) { return; } if self .editor .as_ref() .is_some_and(|editor| editor.read(cx).is_dirty()) { self.status("Save the note before opening a shared note.", cx); return; } if handoff { self.leave_product(Route::SharedPreview(id.clone().into()), cx); self.mount_shared(id, true, cx); return; } let route = Route::SharedSession(id.clone().into()); let cloud = self .native .as_ref() .and_then(|native| native.cloud.service.clone()) .filter(|_| crate::product::account::current(cx).signed_in()); let Some(cloud) = cloud else { self.leave_product(route, cx); return; }; cx.spawn(async move |this, cx| { let _ = cloud .refresh_shared(id, desktop_runtime::CancellationToken::new()) .await; let _ = this.update(cx, |this, cx| this.leave_product(route, cx)); }) .detach(); } /// Shows a shared note or preview once its route is current. fn mount_shared(&mut self, id: String, preview: bool, cx: &mut Context) { let route = if preview { Route::SharedPreview(id.clone().into()) } else { Route::SharedSession(id.clone().into()) }; if self.product.is_some() || self.workspace.read(cx).current_route() != route { return; } let runtime = self.runtime.clone(); let view = cx.new(|cx| { crate::product::sharing::shared_note::SharedNoteView::new(runtime, id, preview, cx) }); self.workspace.update(cx, |workspace, cx| { workspace.set_route_content(route, view.into(), cx) }); } fn summary_completed(&mut self, session: SessionId, title: &str, cx: &mut Context) { crate::meeting::automation::summary_ready(&session, title, cx); self.workspace.update(cx, |workspace, cx| { workspace.run_automations( crate::workspace::automation_runner::Trigger::NoteEnhanced, session, cx, ); }); } pub fn native_tick(&mut self, cx: &mut Context) { self.sync_recording_title(cx); self.poll_downloads(cx); let Some(services) = &self.services else { return; }; self.meeting_host.tick(services, cx); let update = services.capture.take_update(u64::MAX); match update.phase { Phase::Loading | Phase::Listening | Phase::Finalizing => { if self.capture_session.is_none() && update.session.is_some() { crate::telemetry::track( "recording_started", &[("provider", serde_json::json!(update.provider.as_ref()))], ); self.capture_started = Some(std::time::Instant::now()); } self.capture_session = update.session } Phase::Idle => { if let Some(session) = self.capture_session.take() { crate::telemetry::track("recording_completed", &[]); lock::capture_ended(&session, cx); crate::meeting::automation::capture_completed(cx); let elapsed = self.capture_started.take().map(|started| started.elapsed()); if !self.closing && !self.shutdown_begun() { // A very short recording asks whether to keep it first; its summary and // "After the meeting ends" automations wait for the answer. self.workspace.update(cx, |workspace, cx| { workspace.capture_finished(session, elapsed, cx) }); } else { self.workspace.update(cx, |workspace, cx| { workspace.run_automations( crate::workspace::automation_runner::Trigger::MeetingCompleted, session, cx, ) }); } } } Phase::Failed => { if let Some(session) = self.capture_session.take() { self.capture_started = None; crate::telemetry::track("recording_failed", &[]); lock::capture_ended(&session, cx); } } } self.schedule_capture_phase(cx); } pub(super) fn has_window_chrome(&self) -> bool { #[cfg(test)] if self.force_window_chrome { return true; } !cfg!(target_os = "macos") } fn sync_input_blocked(&self, cx: &mut gpui::App) { crate::ui::a11y::set_window_input_blocked( self.window, self.closing || self.writers_paused, cx, ); } pub(super) fn resume_writers(&mut self, cx: &mut Context) { self.writers_paused = false; self.sync_input_blocked(cx); if self.closing || self.drained || self.shutdown_begun() { cx.notify(); return; } self.background_cancel = desktop_runtime::CancellationToken::new(); crate::editor::AttachmentService::resume_imports(&self.editor_vault); if let Some(editor) = &self.editor { editor.update(cx, |editor, cx| editor.set_read_only(false, cx)); } let note = self.workspace.read(cx).note().clone(); note.update(cx, |note, cx| note.resume_summaries(cx)); for window in self.note_windows.values() { let _ = window.update(cx, |view, _, cx| view.resume(cx)); } cx.notify(); } fn can_flush_drafts(&mut self, cx: &mut Context) -> bool { if self.pins_saving || self.pins_pending.is_some() || self.note_windows.values().any(|window| { window .read(cx) .is_ok_and(|view| view.note().read(cx).has_unsaved_title(cx)) }) { return false; } let mounted_editor = self .editor .as_ref() .map(|editor| editor.read(cx).init.session_id.clone()); self.workspace.update(cx, |view, cx| { view.can_flush_to_close(mounted_editor.as_ref(), cx) }) } fn start_draft_flushes( &mut self, pause: bool, cx: &mut Context, ) -> BoxFuture<'static, desktop_runtime::Result<()>> { let dirty_editor = pause .then(|| { self.editor .as_ref() .filter(|editor| editor.read(cx).is_dirty()) .cloned() }) .flatten(); let editor_flush = self.editor.as_ref().map(|editor| { editor.update(cx, |editor, cx| { if pause { editor.set_read_only(true, cx); } editor.flush() }) }); let main_note = self.workspace.read(cx).note().clone(); let summaries = main_note.update(cx, |note, cx| note.flush_summaries(pause, cx)); let mut dirty_notes = Vec::new(); if pause { if !summaries.is_empty() { dirty_notes.push(main_note); } for window in self.note_windows.values() { if let Ok(note) = window.read(cx).map(|view| view.note()) && note.read(cx).has_unsaved_summary(cx) { dirty_notes.push(note); } } } let notes = self .note_windows .values() .filter_map(|window| { window .update(cx, |view, _, cx| { if pause { view.pause(cx) } else { view.flush(false, cx) } }) .ok() }) .collect::>(); let (wake_tx, mut wake_rx) = futures::channel::mpsc::unbounded::<()>(); let rename_failed = Arc::new(AtomicBool::new(false)); let mut subscriptions = Vec::new(); if let Some(editor) = &dirty_editor { let tx = wake_tx.clone(); subscriptions.push(cx.observe(editor, move |_, _, _| { let _ = tx.unbounded_send(()); })); } for note in &dirty_notes { let tx = wake_tx.clone(); subscriptions.push(cx.observe(note, move |_, _, _| { let _ = tx.unbounded_send(()); })); let tx = wake_tx.clone(); let failed = rename_failed.clone(); subscriptions.push(cx.subscribe(note, move |_, _, event: &NoteEvent, _| { if matches!(event, NoteEvent::RenameFailed) { failed.store(true, Ordering::SeqCst); let _ = tx.unbounded_send(()); } })); } drop(wake_tx); let runtime = self.runtime.clone(); Box::pin(cx.spawn(async move |_, cx| -> desktop_runtime::Result<()> { let _subscriptions = subscriptions; let mut first_error = None; if let Some(flush) = editor_flush && let Err(error) = flush .await .map_err(|_| ServiceError::Closed) .and_then(|result| result) { first_error.get_or_insert(error); } for summary in summaries { if let Err(error) = summary .await .map_err(|_| ServiceError::Closed) .and_then(|result| result) { first_error.get_or_insert(error); } } for note in notes { if let Err(error) = note.await { first_error.get_or_insert(error); } } if let Some(error) = first_error { return Err(error); } if let Some(editor) = dirty_editor { let editor = editor.downgrade(); while editor .read_with(cx, |editor, _| editor.is_dirty()) .map_err(|_| ServiceError::Closed)? { if wake_rx.next().await.is_none() { return Err(ServiceError::Closed); } } } for note in dirty_notes { let note = note.downgrade(); loop { if rename_failed.load(Ordering::SeqCst) { return Err(ServiceError::Failed("Title save failed.".into())); } let (unsaved_summary, renaming, unsaved_title) = note .read_with(cx, |note, cx| { ( note.has_unsaved_summary(cx), note.is_renaming(), note.has_unsaved_title(cx), ) }) .map_err(|_| ServiceError::Closed)?; match (unsaved_summary, renaming, unsaved_title) { (false, false, false) => break, (false, false, true) => return Err(ServiceError::Busy), _ => { if wake_rx.next().await.is_none() { return Err(ServiceError::Closed); } } } } } runtime.flush()?.receive().await })) } fn flush_drafts( &mut self, cx: &mut Context, ) -> BoxFuture<'static, desktop_runtime::Result<()>> { if !self.can_flush_drafts(cx) { return Box::pin(async { Err(ServiceError::Busy) }); } self.start_draft_flushes(false, cx) } fn open_note_window(&mut self, id: Option, cx: &mut Context) { self.note_windows .retain(|_, window| window.read(cx).is_ok()); self.note_subscriptions .retain(|id, _| self.note_windows.contains_key(id)); if let Some(id) = &id && let Some(window) = self.note_windows.get(id) { let _ = window.update(cx, |_, window, _| window.activate_window()); return; } if self.note_windows.len() >= 16 || self.writers_paused || self.closing { self.status( "Close a note window or finish pending work before opening another.", cx, ); return; } let request = match id { Some(id) => self .runtime .open_session(id, desktop_runtime::CancellationToken::new()), None => self.runtime.create_note("".into()), }; cx.spawn(async move |this, cx| { let result = match request { Ok(reply) => reply.receive().await, Err(error) => Err(error), }; let _ = this.update(cx, |this, cx| { let result = result.and_then(|note| { let id = note.summary.id.clone(); if this.closing || this.writers_paused || this.note_windows.len() >= 16 { return Err(ServiceError::Busy); } if let Some(window) = this.note_windows.get(&id) { let _ = window.update(cx, |_, window, _| window.activate_window()); return Ok(()); } let options = crate::platform::windows::WindowIdentity::Note(id.clone()) .options(None, cx)?; let runtime = this.runtime.clone(); let vault = this.editor_vault.clone(); let workspace = this.workspace.downgrade(); let app = cx.weak_entity(); let window = cx .open_window(options, move |window, cx| { cx.new(|cx| { let mut view = crate::note_window::NoteWindow::new( runtime, note, vault, workspace, window, cx, ); view.set_app(app); view }) }) .map_err(|error| ServiceError::Failed(error.to_string().into()))?; let view = window .update(cx, |_, _, cx| cx.entity()) .map_err(|error| ServiceError::Failed(error.to_string().into()))?; let subscriptions = this.note_window_subscriptions(&view, cx); this.note_subscriptions.insert(id.clone(), subscriptions); this.note_windows.insert(id, window); Ok(()) }); if let Err(error) = result { this.status(&error.to_string(), cx); } }); }) .detach(); } pub(super) fn pause_writers( &mut self, cx: &mut Context, ) -> BoxFuture<'static, desktop_runtime::Result<()>> { if self.closing || self.drained || self.shutdown_begun() || self.services.as_ref().is_some_and(|s| { matches!( s.capture.take_update(u64::MAX).phase, Phase::Loading | Phase::Listening | Phase::Finalizing ) }) || !self.can_flush_drafts(cx) { return Box::pin(async { Err(ServiceError::Busy) }); } self.writers_paused = true; self.sync_input_blocked(cx); self.background_cancel.cancel(); cx.notify(); let drain_imports = crate::editor::AttachmentService::pause_imports(&self.editor_vault); let import_drain_timeout = cx.background_executor().timer(Duration::from_secs(10)); let flush = self.start_draft_flushes(true, cx); let cloud = self.native.as_ref().and_then(|n| n.cloud.service.clone()); let runtime = self.runtime.clone(); Box::pin(async move { let drained = { let drain = std::pin::pin!(drain_imports); let timeout = std::pin::pin!(import_drain_timeout); matches!( futures::future::select(drain, timeout).await, futures::future::Either::Left(_) ) }; if !drained { return Err(ServiceError::Failed( "Attachment import did not finish in time; try again once the transfer completes." .into(), )); } flush.await?; if let Some(cloud) = cloud { cloud.shutdown().await?; } runtime .submit(|services| async move { services .executor .execute("PRAGMA wal_checkpoint(TRUNCATE)".into(), vec![]) .await .map_err(|e| ServiceError::Failed(e.to_string().into()))?; Ok(()) })? .receive() .await }) } fn watch_cloud(&mut self, cloud: crate::product::cloud::CloudServices, cx: &mut Context) { self.trial_dialogs .update(cx, |dialogs, _| dialogs.set_cloud(cloud.clone())); enterprise_capture::watch(cloud.clone(), cx); cx.set_global(crate::product::sharing::SharingCloud(cloud.clone())); cx.set_global(crate::product::cloud::calls::AccountCloud(Arc::new( cloud.clone(), ))); team_invitations::watch(cx); let session_audio_locks = self .services .as_ref() .map(|services| services.capture.activities.session_audio_locks()) .unwrap_or_default(); attachment_sync::watch( cloud.clone(), self.editor_vault.clone(), session_audio_locks, cx, ); shared_notes::watch(cloud.clone(), self.editor_vault.clone(), cx); device_approval::watch(cloud.clone(), cx); let mut identities = cloud.identities(); let identity_cloud = cloud.clone(); cx.spawn(async move |this, cx| { loop { let identity = identities.borrow_and_update().clone(); let _ = this.update(cx, |this, cx| { let status = identity_cloud.clone(); this.automation.update(cx, |automation, cx| { automation.set_account( identity.account_id.clone(), Some(Arc::new(move || status.sync_status().last_sync_at_ms)), cx, ) }); this.workspace.update(cx, |view, cx| { view.set_viewer(identity.account_id.clone(), cx); view.set_automation_client(None, cx); }); if let Some(product) = &this.product { product.update(cx, |view, cx| view.set_scope(identity_cloud.scope(), cx)); } identity_cloud.publish_account(cx); cx.notify(); }); if identity.account_id.is_some() { let client = identity_cloud.automation_client().await.ok(); let _ = this.update(cx, |this, cx| { this.workspace .update(cx, |view, cx| view.set_automation_client(client, cx)) }); } if identities.changed().await.is_err() { break; } } }) .detach(); cx.spawn(async move |this, cx| { loop { let paused = this .update(cx, |this, _| this.writers_paused || this.closing) .unwrap_or(true); if !paused { let result = cloud.refresh(false).await; let _ = this.update(cx, |_, cx| cloud.publish_account(cx)); if let Err(error) = result && !matches!(error, ServiceError::Cancelled | ServiceError::Closed) { let _ = this.update(cx, |this, cx| { this.status(&format!("Account refresh: {error}"), cx) }); } } cx.background_executor() .timer(Duration::from_secs(60)) .await; if this.upgrade().is_none() { break; } } }) .detach(); } fn save_pins(&mut self, routes: Arc<[Route]>, cx: &mut Context) { if self.pins_saving { self.pins_pending = Some(routes); return; } let Some(base) = self.pins_revision.clone() else { return; }; let shipping = crate::shipping_store::ShippingStore::get(cx); let reply = match pins::save(&self.runtime, shipping, base, routes.clone()) { Ok(reply) => reply, Err(error) => { self.pins_pending = Some(routes); self.status(&format!("Pins not saved: {error}"), cx); return; } }; self.pins_saving = true; cx.spawn(async move |this, cx| { let result = reply.receive().await; let _ = this.update(cx, |this, cx| { this.pins_saving = false; match result { Ok(revision) => { this.pins_revision = Some(revision); if let Some(routes) = this.pins_pending.take() { this.save_pins(routes, cx); } } Err(error) => { this.pins_pending.get_or_insert(routes); this.status( &format!("Pins not saved; in-memory tabs retained: {error}"), cx, ); } } }); }) .detach(); } /// Shipping's audio retention sweep: once after startup recovery, then every minute. fn start_retention( &self, vault: PathBuf, activities: retention::Activities, cx: &mut Context, ) { let runtime = self.runtime.clone(); cx.spawn(async move |this, cx| { loop { let paused = this .update(cx, |this, _| this.writers_paused || this.closing) .unwrap_or(true); if !paused { let (work, vault, activities) = (runtime.clone(), vault.clone(), activities.clone()); let reply = runtime.service(move |services| async move { let shutdown = services.shutdown_requested.clone(); tokio::select! { biased; _ = shutdown.cancelled() => Err(ServiceError::Cancelled), result = retention::sweep(&work, vault, activities) => result, } }); match async { reply?.receive().await }.await { Err(ServiceError::Closed | ServiceError::Cancelled) => break, Err(error) => tracing::warn!(%error, "audio retention sweep failed"), Ok(()) => {} } } cx.background_executor().timer(retention::SWEEP_EVERY).await; if this.update(cx, |_, _| ()).is_err() { break; } } }) .detach(); } /// An interrupted recording that recovery couldn't finish blocks new recordings in its note /// until a later launch recovers it or the user discards it here. fn offer_discard( &self, capture: recovery::InterruptedCapture, vault: PathBuf, cx: &mut Context, ) { let error = capture .result .err() .map(|error| error.to_string()) .unwrap_or_default(); let title = if capture.title.is_empty() { crate::i18n::t("Untitled", cx) } else { &capture.title }; let runtime = self.runtime.clone(); let activities = self .services .as_ref() .map(|services| services.capture.activities.clone()) .unwrap_or_default(); let (session, transcript, retained) = (capture.session, capture.transcript, capture.retained); let discard_label = crate::i18n::t_resolved("Discard", cx); toasts::show( toasts::Toast::warning(format!( "The interrupted recording in “{title}” could not be recovered" )) .id(format!("capture-recovery-{}", session.0)) .description(format!( "{error} Anarlog retries at the next launch, or you can discard its unrecovered audio." )) .sticky() .localized_action(discard_label, move |_, cx| { let work = runtime.clone(); let (vault, session, transcript, activities) = ( vault.clone(), session.clone(), transcript.clone(), activities.clone(), ); let reply = runtime.service(move |_| async move { recovery::discard(&work, &vault, session, transcript, retained, activities).await }); cx.spawn(async move |cx| { if let Err(error) = async { reply?.receive().await }.await { cx.update(|cx| { toasts::error( format!("The interrupted recording could not be discarded: {error}"), cx, ) }); } }) .detach(); }), cx, ); } /// App errors appear as shipping's error toasts. fn status(&mut self, message: &str, cx: &mut Context) { toasts::error(message.to_owned(), cx); } fn workspace_event( &mut self, event: &WorkspaceEvent, window: &mut Window, cx: &mut Context, ) { match event { WorkspaceEvent::AttachFile(id) => { if let Some(editor) = &self.editor && &editor.read(cx).init.session_id == id { editor.update(cx, |editor, cx| editor.choose_attachment(cx)); } } WorkspaceEvent::SessionsDeleted(ids) => { if self .editor .as_ref() .is_some_and(|editor| ids.contains(&editor.read(cx).init.session_id)) { self.editor_subscriptions.clear(); self.editor = None; } self.close_note_windows(ids, cx); cx.notify(); } WorkspaceEvent::OpenEditor { session_id, document, } => self.open_editor(session_id.clone(), document.clone(), window, cx), WorkspaceEvent::Meeting(intent) => self.open_meeting(intent.clone(), window, cx), WorkspaceEvent::Product(route) => self.open_product(route.clone(), window, cx), } } fn open_editor( &mut self, session_id: SessionId, document: DocumentSnapshot, window: &mut Window, cx: &mut Context, ) { if let Some(editor) = &self.editor && editor.read(cx).init.session_id == session_id { self.workspace.update(cx, |workspace, cx| { workspace.set_editor_content(&session_id, editor.clone().into(), cx) }); return; } if self .editor .as_ref() .is_some_and(|editor| editor.read(cx).is_dirty()) { self.status( "Unsaved editor retained. Save before opening another note.", cx, ); return; } self.pending_product = None; let return_focus = self.workspace.read(cx).focus_handle(cx); let editor = cx.new(|cx| { let mut editor = EditorPane::new( LaneContext { runtime: self.runtime.clone(), }, EditorInit { session_id: session_id.clone(), document, return_focus, }, window, cx, ); editor.configure_attachments(self.editor_vault.clone(), cx); let records = cx.new(|cx| { crate::workspace::MeetingRecords::new( self.runtime.clone(), crate::editor::AttachmentService::new( self.runtime.clone(), self.editor_vault.clone(), ), session_id.clone(), cx, ) }); editor.set_footer(Some(records.into()), cx); editor }); self.editor_subscriptions = vec![ cx.subscribe(&editor, |this, editor, event, cx| match event { EditorEvent::Dirty { session_id, dirty } => { this.workspace.update(cx, |workspace, cx| { workspace.set_session_dirty(session_id.clone(), *dirty, cx) }) } EditorEvent::Saved(snapshot) => { let dirty = editor.read(cx).is_dirty(); this.workspace.update(cx, |workspace, cx| { workspace.set_session_dirty(snapshot.session_id.clone(), dirty, cx) }); } EditorEvent::SaveFailed { session_id, .. } => { this.workspace.update(cx, |workspace, cx| { workspace.set_session_dirty(session_id.clone(), true, cx) }); } EditorEvent::MentionHuman(id) => this.workspace.update(cx, |workspace, cx| { workspace.open_route(Route::Human(id.0.clone()), false, cx) }), EditorEvent::OpenLink(url) => { if url.starts_with("https://") || url.starts_with("http://") || url.starts_with("mailto:") { cx.open_url(url); } else { this.status( "This link scheme is not supported by the native preview.", cx, ); } } EditorEvent::OpenAttachment(id) => { let attachments = crate::editor::AttachmentService::new( this.runtime.clone(), this.editor_vault.clone(), ); let session = editor.read(cx).init.session_id.clone(); let id = id.0.to_string(); let runtime = this.runtime.clone(); cx.spawn(async move |this, cx| { let result = async { let attachment = attachments .resolve(session, id, desktop_runtime::CancellationToken::new()) .await .map_err(|error| ServiceError::Failed(error.into()))?; runtime .service(move |_| async move { tokio::task::spawn_blocking(move || { crate::platform::os::open(attachment.path.as_ref()) }) .await .map_err(|error| { ServiceError::Failed(error.to_string().into()) })? .map_err(|error| ServiceError::Failed(error.to_string().into())) })? .receive() .await } .await; if let Err(error) = result { let _ = this.update(cx, |this, cx| this.status(&error.to_string(), cx)); } }) .detach(); } }), cx.subscribe(&editor, |this, _, event, cx| match event { EditorRequest::OpenSession(id) => this.workspace.update(cx, |workspace, cx| { workspace.open_session(SessionId(id.clone().into()), cx) }), EditorRequest::OpenOrganization(id) => { this.workspace.update(cx, |workspace, cx| { workspace.open_route(Route::Organization(id.clone().into()), false, cx) }) } EditorRequest::OpenTask(route) => this.workspace.update(cx, |workspace, cx| { workspace.open_route(route.clone(), true, cx) }), EditorRequest::MentionSearch { .. } => {} }), ]; self.workspace.update(cx, |workspace, cx| { workspace.set_editor_content(&session_id, editor.clone().into(), cx) }); self.editor = Some(editor); self.meeting_open = false; cx.notify(); } pub(super) fn sync_floating_bar(&mut self, cx: &mut Context) { let active_session = self .meeting_host .live() .read(cx) .current() .filter(|live| live.mode == crate::meeting::live::SessionMode::Active) .map(|live| &live.session); if active_session != self.floating_pinned_session.as_ref() { self.floating_pinned_session = None; } let enabled = self.notifications.floating_bar_enabled() && (!self.main_window_active || (active_session.is_some() && active_session == self.floating_pinned_session.as_ref())); if let Err(error) = self .meeting_host .sync_floating(enabled, self.services.as_ref(), cx) { self.status(&error.to_string(), cx); } } /// Live recording markers and the floating bar follow the live meeting. fn live_changed(&mut self, cx: &mut Context) { for (session, active) in self.meeting_host.recording_changes(cx) { self.workspace.update(cx, |workspace, cx| { workspace.set_recording(session, active, cx) }); } self.sync_floating_bar(cx); } fn open_meeting(&mut self, intent: MeetingIntent, window: &mut Window, cx: &mut Context) { if matches!(intent, MeetingIntent::Start { .. }) && (self.writers_paused || self.closing || self.drained || self.shutdown_begun()) { self.status("Finish pending work before starting capture.", cx); return; } let Some(services) = self.services.clone() else { self.status("Meeting services are still starting or recovery failed. Retry after resolving the startup error.", cx); return; }; self.pending_product = None; // Shipping records inside the note itself; only an explicit open shows the meeting pane. let reply = match &intent { MeetingIntent::Start { session_id } => Some(host::start(&services, session_id.clone())), MeetingIntent::Stop => Some(host::stop(&services)), MeetingIntent::Open(_) => None, }; if let Some(reply) = reply { cx.spawn(async move |this, cx| { if let Err(error) = host::acknowledged(reply).await { let _ = this.update(cx, |this, cx| this.status(&error.to_string(), cx)); } }) .detach(); return; } let meeting = cx.new(|cx| { MeetingPane::new( LaneContext { runtime: self.runtime.clone(), }, intent, window, cx, ) }); self.meeting_subscription = Some(cx.subscribe(&meeting, |this, _, event, cx| { match event { MeetingEvent::NoteEnhanced(session) => { this.summary_completed(session.clone(), "", cx); } MeetingEvent::Recording { session_id, active } => { this.workspace.update(cx, |workspace, cx| { workspace.set_recording(session_id.clone(), *active, cx) }) } MeetingEvent::OpenSession(id) => this .workspace .update(cx, |workspace, cx| workspace.open_session(id.clone(), cx)), MeetingEvent::Failed(error) => this.status(&error.to_string(), cx), } })); self.meeting = Some(meeting); self.meeting_open = true; cx.notify(); } fn open_product(&mut self, route: ProductRoute, window: &mut Window, cx: &mut Context) { self.open_product_with_section(route, None, window, cx); } fn open_product_with_section( &mut self, route: ProductRoute, section: Option, window: &mut Window, cx: &mut Context, ) { let Some(native) = &self.native else { if let Some(error) = &self.startup_error { self.status(&format!("Native services failed to start: {error}"), cx); } else { let workspace_route = self.workspace.read(cx).current_route(); let settings_section = section.or_else(|| match (&route, &workspace_route) { (ProductRoute::Settings, Route::Settings(section)) => Some(section.to_string()), _ => None, }); self.pending_product = Some(PendingProduct { route, workspace_route, settings_section, }); } return; }; self.pending_product = None; let local = native.local.clone(); let cloud = native.cloud.service.clone(); let scope = cloud.as_ref().map(|c| c.scope()).unwrap_or_default(); if self .product .as_ref() .is_some_and(|product| !product.update(cx, |product, cx| product.can_close(cx))) { return; } if self .editor .as_ref() .is_some_and(|editor| editor.read(cx).is_dirty()) { self.status("Save the note before opening this service.", cx); return; } if !self .workspace .update(cx, |workspace, cx| workspace.can_close(cx)) { return; } let general_settings = matches!(route, ProductRoute::Settings); let settings_section = section.or_else(|| match self.workspace.read(cx).current_route() { Route::Settings(section) => Some(section.to_string()), _ => None, }); let product = cx.new(|cx| { ProductPane::with_services( LaneContext { runtime: self.runtime.clone(), }, route, local, cloud, scope, window, cx, ) }); if general_settings && let Some(section) = settings_section { product.update(cx, |product, cx| product.navigate_section(§ion, cx)); } self.product_subscriptions = vec![ cx.observe(&product, |_, _, cx| cx.notify()), cx.subscribe(&product, |this, _, event, cx| match event { ProductEvent::NavigateWorkspace => this.leave_product(Route::Empty, cx), ProductEvent::OpenSession(id) => this.leave_product(Route::Session(id.clone()), cx), ProductEvent::Failed(error) => this.status(&error.to_string(), cx), }), cx.subscribe(&product, |this, _, event: &OpenWorkspaceSection, cx| { let route = match event.0 { "contacts" => Route::Contacts, "folders" => Route::Folders, "templates" => Route::Templates, "automations" => Route::Automations, "calendar" => Route::Calendar, // Shipping opens release notes in a new tab beside Settings. "changelog" => return this.leave_product_to(Route::Changelog, true, cx), _ => Route::settings(event.0), }; this.leave_product(route, cx); }), ]; self.product = Some(product); cx.notify(); } /// Like shipping's `/app` route guard: a profile that has not finished onboarding opens it. fn open_onboarding_if_needed(&mut self, cx: &mut Context) { let runtime = self.runtime.clone(); let window = self.window; cx.spawn(async move |this, cx| { let key = crate::product::onboarding::NEEDED_KEY.into(); let stored = crate::product::local::data::setting_value(&runtime, key).await; // An unreadable flag skips onboarding, as shipping's `getOnboardingNeeded` does. if stored.is_ok_and(|value| crate::product::onboarding::needed(value.as_ref())) { let _ = cx.update_window(window, |_, window, cx| { this.update(cx, |this, cx| { this.open_product(ProductRoute::Onboarding, window, cx) }) }); } }) .detach(); } fn leave_product(&mut self, route: Route, cx: &mut Context) { self.leave_product_to(route, false, cx) } fn leave_product_to(&mut self, route: Route, new_slot: bool, cx: &mut Context) { if self .product .as_ref() .is_some_and(|product| !product.update(cx, |product, cx| product.can_close(cx))) { return; } self.pending_product = None; self.product = None; self.product_subscriptions.clear(); self.workspace.update(cx, |workspace, cx| { workspace.open_route(route, new_slot, cx) }); let view = cx.entity().downgrade(); cx.defer(move |cx| { let _ = view.update(cx, |this, cx| { if this.product.is_none() { let focus = this.workspace.read(cx).focus_handle(cx); let _ = this .window .update(cx, |_, window, cx| focus.focus(window, cx)); } }); }); cx.notify(); } pub fn request_quit(&mut self, cx: &mut Context) { self.request_quit_inner(false, cx); } pub(super) fn request_quit_after_update(&mut self, cx: &mut Context) { self.request_quit_inner(true, cx); } fn request_quit_inner(&mut self, release_writer_pause_on_abort: bool, cx: &mut Context) { if self.closing { if self.drain_timed_out { self.force_quit_after_drain(cx); } return; } // A runtime that closed (or never started) has nothing left to drain. if self.drained || matches!( *self.runtime.state().borrow(), desktop_runtime::RuntimeState::Closed(_) ) { crate::telemetry::track("app_exit_requested", &[]); quit_drained(cx); return; } if self.pins_saving || self.pins_pending.is_some() { if release_writer_pause_on_abort && self.writers_paused { self.resume_writers(cx); } if !self.pins_saving && let Some(routes) = self.pins_pending.take() { self.save_pins(routes, cx); } self.status( "Pinned tabs are still saving; retry quit after the save completes.", cx, ); return; } if self .product .as_ref() .is_some_and(|product| !product.update(cx, |product, cx| product.can_close(cx))) { if release_writer_pause_on_abort && self.writers_paused { self.resume_writers(cx); } self.status("Save or restore product edits before quitting.", cx); return; } let flush = self .editor .as_ref() .filter(|editor| editor.read(cx).is_dirty()) .map(|editor| editor.update(cx, |editor, _| editor.flush())); let note = self.workspace.read(cx).note().clone(); let summaries = note.update(cx, |note, cx| note.flush_summaries(true, cx)); let notes = self .note_windows .values() .filter_map(|window| window.update(cx, |view, _, cx| view.pause(cx)).ok()) .collect::>(); let pending = self .workspace .update(cx, |workspace, cx| workspace.save_pending(cx)); self.closing = true; self.drain_timed_out = false; self.sync_input_blocked(cx); self.background_cancel.cancel(); cx.notify(); crate::telemetry::track("app_exit_requested", &[]); // Undo windows end at quit so deleted notes do not keep their folders. self.workspace .update(cx, |workspace, cx| workspace.confirm_pending_deletions(cx)); if let Some(editor) = &self.editor { editor.update(cx, |editor, cx| editor.set_read_only(true, cx)); } toasts::show( toasts::Toast::loading("Finishing saves and capture before quitting…").id("quit"), cx, ); let services = self.services.clone(); cx.spawn(async move |this, cx| { let result: desktop_runtime::Result<()> = async { // Every started save finishes before the first error stops the quit: dropping // the template save would cancel it, and its failed edits restore only from it. let mut saved = Vec::new(); for flush in flush.into_iter().chain(summaries) { saved.push(flush.await.map_err(|_| ServiceError::Closed).and_then(|result| result)); } for flush in notes { saved.push(flush.await); } saved.push(pending.await); saved.into_iter().collect::>()?; if let Some(services) = services { services.playback.send(playback::Command::Pause)?; let update = services.capture.take_update(u64::MAX); if matches!(update.phase, Phase::Loading | Phase::Listening | Phase::Finalizing) { services.capture.stop()?.await.map_err(|_| ServiceError::Closed)??; let deadline = std::time::Instant::now() + Duration::from_secs(30); loop { let update = services.capture.take_update(u64::MAX); match update.phase { Phase::Idle => break, Phase::Failed => return Err(update.error.unwrap_or(ServiceError::Closed)), _ if std::time::Instant::now() >= deadline => return Err(ServiceError::Failed("Capture is still finalizing; retry after it completes.".into())), _ => { cx.background_executor().timer(Duration::from_millis(50)).await; } } } } } let runtime = this.update(cx, |this, cx| { if !this.workspace.update(cx, |workspace, cx| workspace.can_close(cx)) { return Err(ServiceError::Failed("Save or restore the title and finish pending work before quitting.".into())); } Ok(this.runtime.clone()) }).map_err(|_| ServiceError::Closed)??; let drain = cx.update(|cx| runtime_bridge::drain_before_quit(runtime, cx)); drain.await }.await; let _ = this.update(cx, |this, cx| match result { Ok(()) => { this.closing = false; this.drain_timed_out = false; this.drained = true; this.sync_input_blocked(cx); toasts::dismiss("quit", cx); quit_drained(cx); } Err(error) if this.shutdown_begun() => this.finish_drain(error, cx), Err(error) => { this.closing = false; this.drain_timed_out = false; this.sync_input_blocked(cx); // An update-triggered quit owns the pause taken by `install_update`; a storage // move or still-running install keeps its pause until relaunch or abort. if release_writer_pause_on_abort && this.writers_paused { this.resume_writers(cx); } else if !this.writers_paused { this.background_cancel = desktop_runtime::CancellationToken::new(); if let Some(editor) = &this.editor { editor.update(cx, |editor, cx| editor.set_read_only(false, cx)); } note.update(cx, |note, cx| note.resume_summaries(cx)); for window in this.note_windows.values() { let _ = window.update(cx, |view, _, cx| view.resume(cx)); } cx.notify(); } else { cx.notify(); } toasts::dismiss("quit", cx); this.status(&format!("Quit paused: {error}"), cx); } }); }).detach(); } /// The runtime stopped admitting work, so nothing can save again in this process. pub(super) fn shutdown_begun(&self) -> bool { !matches!( *self.runtime.state().borrow(), desktop_runtime::RuntimeState::Starting | desktop_runtime::RuntimeState::Ready ) } fn show_quit_paused_toast(&self, error: &ServiceError, cx: &mut Context) { let app = cx.entity().downgrade(); toasts::show( toasts::Toast::error(format!("Quit paused: {error}")) .id("quit") .sticky() .action("Quit Anyway", move |_, cx| { if app .update(cx, |this, cx| this.force_quit_after_drain(cx)) .is_err() { quit_drained(cx); } }), cx, ); } pub(super) fn force_quit_after_drain(&mut self, cx: &mut Context) { self.closing = false; self.drain_timed_out = false; self.drained = true; self.sync_input_blocked(cx); toasts::dismiss("quit", cx); cx.notify(); crate::telemetry::track("app_exit_requested", &[]); quit_drained(cx); } /// Handles a raw quit-shortcut `KeyDownEvent` while input is blocked. Returns `true` when /// the drain deadline has already passed (force-quitting on a fresh press and swallowing /// held repeats so auto-repeat cannot bypass the drain). pub(crate) fn handle_quit_shortcut_key_down( &mut self, event: &gpui::KeyDownEvent, cx: &mut Context, ) -> bool { if !self.drain_timed_out { return false; } if !event.is_held { self.force_quit_after_drain(cx); } true } /// Notes stay read-only once the runtime began shutting down. The drain keeps running and /// the app quits when it finishes; once the drain deadline is exceeded, "Quit Anyway" or a /// second explicit quit exits immediately without waiting further. fn finish_drain(&mut self, error: ServiceError, cx: &mut Context) { self.drain_timed_out = true; cx.notify(); self.show_quit_paused_toast(&error, cx); let runtime = self.runtime.clone(); cx.spawn(async move |this, cx| { let result = runtime.shutdown().await; let _ = this.update(cx, |this, cx| { if this.drained { return; } this.closing = false; this.drain_timed_out = false; this.drained = true; this.sync_input_blocked(cx); cx.notify(); match result { Ok(()) => { toasts::dismiss("quit", cx); quit_drained(cx); } Err(error) => { this.show_quit_paused_toast(&error, cx); } } }); }) .detach(); } fn schedule_post_gate_focus_restore( &mut self, candidate: Option, window: &mut Window, cx: &mut Context, ) { let view = cx.entity().downgrade(); window.defer(cx, move |window, cx| { let _ = view.update(cx, |this, cx| { let candidate = candidate.filter(|handle| handle != &this.focus); if this.closing || this.writers_paused { if this.restore_focus.is_none() { this.restore_focus = candidate; } return; } if this.gate_active || startup::is_active(cx) || lock::is_gated(cx) || this.instruction.read(cx).is_active() { if this.pre_gate_focus.is_none() { this.pre_gate_focus = candidate; } return; } if window .focused(cx) .is_some_and(|focused| focused.is_rendered(window)) { return; } if let Some(previous) = candidate.filter(|handle| handle.is_rendered(window)) { previous.focus(window, cx); } else if this.product.is_none() { this.workspace.focus_handle(cx).focus(window, cx); } }); }); } } pub(crate) fn is_quit_shortcut(keystroke: &gpui::Keystroke) -> bool { let key = keystroke.key.as_str(); let modifiers = &keystroke.modifiers; if cfg!(target_os = "macos") { modifiers.platform && !modifiers.control && !modifiers.alt && key.eq_ignore_ascii_case("q") } else { (modifiers.alt && !modifiers.control && !modifiers.platform && key.eq_ignore_ascii_case("f4")) || (modifiers.control && !modifiers.alt && !modifiers.platform && key.eq_ignore_ascii_case("q")) } } impl Render for ApplicationView { fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { let colors = theme(window); let _ = &self.subscriptions; self.sync_notice(window, cx); let input_blocked = self.closing || self.writers_paused; self.sync_input_blocked(cx); let startup_active = startup::is_active(cx); let lock_gate = (!startup_active).then(|| lock::gate(window, cx)).flatten(); let instruction_active = !startup_active && lock_gate.is_none() && self.instruction.read(cx).is_active(); let gate_active = startup_active || lock_gate.is_some() || instruction_active; let was_gated = std::mem::replace(&mut self.gate_active, gate_active); let gate_entered = !was_gated && gate_active; let gate_cleared = was_gated && !gate_active; if gate_entered && let Some(focused) = window .focused(cx) .filter(|focused| focused != &self.focus && focused.is_rendered(window)) .or_else(|| self.restore_focus.clone()) { self.pre_gate_focus = Some(focused); } let pre_gate = gate_cleared.then(|| self.pre_gate_focus.take()).flatten(); if input_blocked { if self.restore_focus.is_none() && let Some(focused) = window.focused(cx) && focused != self.focus && focused.is_rendered(window) { self.restore_focus = Some(focused); } if gate_cleared && self.restore_focus.is_none() { self.restore_focus = pre_gate; } if window .focused(cx) .is_none_or(|focused| !focused.is_rendered(window)) || (self.focus.is_rendered(window) && !self.focus.contains_focused(window, cx)) { self.focus.focus(window, cx); } } else { let restore = self.restore_focus.take(); if self.focus.is_focused(window) { if gate_cleared { let candidate = restore.filter(|handle| handle != &self.focus).or(pre_gate); window.blur(cx); self.schedule_post_gate_focus_restore(candidate, window, cx); } else if !startup_active && lock_gate.is_none() && !gate_entered && let Some(previous) = restore .as_ref() .filter(|handle| **handle != self.focus && handle.is_rendered(window)) { previous.focus(window, cx); } else if !gate_active && self.product.is_none() { self.workspace.focus_handle(cx).focus(window, cx); } else { if gate_active && self.pre_gate_focus.is_none() { self.pre_gate_focus = restore.filter(|handle| handle != &self.focus); } window.blur(cx); } } else if lock_gate.is_some() { if window.focused(cx).is_some() { window.blur(cx); } } else if gate_cleared { self.schedule_post_gate_focus_restore(pre_gate, window, cx); } } let body = if let Some(gate) = startup::gate(window, cx) { gate.into_any_element() } else if let Some(gate) = lock_gate { gate.into_any_element() } else if instruction_active { // Shipping's `/app/instruction` route replaces the app while the browser hand-off runs. self.instruction_root(cx).into_any_element() } else { div() .size_full() .relative() .flex() .flex_col() .bg(colors.background) .text_color(colors.foreground) .capture_key_down(cx.listener(|this, event: &gpui::KeyDownEvent, window, cx| { if !(this.closing || this.writers_paused) && this.chrome_key(event, window, cx) { cx.stop_propagation(); } })) .when(self.has_window_chrome(), |view| { view.child(self.title_bar(window, cx)) }) .child( div() .id("application-content-scope") .relative() .flex_1() .min_h_0() .flex() .when(input_blocked, |view| { view.role(gpui::Role::Group) .a11y_synthetic_children(|builder| { let node = builder.parent_node(); node.set_hidden(); node.set_disabled(); }) .capture_any_mouse_down(|_, window, cx| { window.prevent_default(); cx.stop_propagation(); }) .capture_any_mouse_up(|_, window, cx| { window.prevent_default(); cx.stop_propagation(); }) }) .when_some(self.product.clone(), |view, product| view.child(product)) .when(self.product.is_none(), |view| { view.child( div() .flex() .flex_1() .min_w_0() .child(self.workspace.clone()), ) .when_some( self.meeting.clone().filter(|_| self.meeting_open), |view, meeting| { view.child( div() .w(px(420.)) .h_full() .flex() .flex_col() .child({ let hide = crate::i18n::t_resolved("Hide transcript", cx); div() .id("hide-meeting") .a11y_button(hide.text) .a11y_language(hide.locale) .p_2() .rounded(crate::ui::theme::radius::MD) .cursor_pointer() .child(hide.text) .on_click(cx.listener(|this, _, _, cx| { this.meeting_open = false; cx.notify(); })) }) .child(div().flex_1().min_h_0().child(meeting)), ) }, ) }) .when(input_blocked, |view| { view.child( div() .id("application-input-block") .absolute() .inset_0() .bg(colors.background.opacity(0.35)) .occlude() .on_any_mouse_down(|_, window, cx| { window.prevent_default(); cx.stop_propagation(); }) .on_scroll(|_, _, cx| { cx.stop_propagation(); }), ) }), ) .child(self.toasts.clone()) .when_some( self.title_menu(window, cx).filter(|_| !input_blocked), |view, menu| view.child(menu), ) .when(!input_blocked, |view| { view.child(self.trial_dialogs.clone()) }) .into_any_element() }; let root = div() .size_full() .when(input_blocked, |view| view.track_focus(&self.focus)) .capture_key_down(cx.listener(|this, event: &gpui::KeyDownEvent, _, cx| { if this.closing || this.writers_paused { if is_quit_shortcut(&event.keystroke) { if this.handle_quit_shortcut_key_down(event, cx) { cx.stop_propagation(); } return; } cx.stop_propagation(); } })) .child(body); // Untranslated workspace subtrees stay tagged as English; localized views wrap themselves // in `LocalizedRoot` / resolved-language scopes. crate::i18n::locale_scope("app-locale-root", crate::i18n::SOURCE_LOCALE, root) } } /// Quits after the drain, starting a requested restart first on macOS, where quitting ends /// the process before `main` regains control. fn quit_drained(cx: &mut gpui::App) { crate::telemetry::flush(Duration::from_secs(2)); #[cfg(target_os = "macos")] if let Err(error) = crate::platform::windows::run_requested_restart() { crate::telemetry::capture_operational_error( "app_restart", Some("relaunch"), &error.to_string(), None, None, ); tracing::error!(%error, "the app could not relaunch"); } cx.quit(); } #[cfg(test)] mod attachment_pause_tests { use std::{io::Cursor, path::PathBuf, sync::mpsc, time::Duration}; use desktop_runtime::{CancellationToken, Profile, RuntimeHandle, RuntimeState, ServiceError}; use futures::channel::oneshot; use super::*; struct PauseTestApp { _profile: tempfile::TempDir, runtime: RuntimeHandle, session_id: SessionId, vault: PathBuf, app_entity: Entity, } fn start_pause_test_app(cx: &mut gpui::TestAppContext) -> PauseTestApp { crate::platform::os::go_headless(); cx.executor().allow_parking(); let profile = tempfile::tempdir().unwrap(); let (runtime, ready) = crate::runtime_bridge::start(Profile { database: profile.path().join("library.sqlite"), }) .unwrap(); let mut state = runtime.state(); while matches!(*state.borrow_and_update(), RuntimeState::Starting) { futures::executor::block_on(state.changed()).unwrap(); } futures::executor::block_on( runtime .submit(|services| async move { sqlx::query("INSERT INTO app_settings (id, value_json) VALUES (?, 'false')") .bind(crate::product::onboarding::NEEDED_KEY) .execute(services.db.pool()) .await .map(|_| ()) .map_err(|error| ServiceError::Failed(error.to_string().into())) }) .unwrap() .receive(), ) .unwrap(); let created = futures::executor::block_on( runtime .create_note("Pause barrier note".into()) .unwrap() .receive(), ) .unwrap(); let session_id = created.summary.id; cx.update(crate::ui::theme::UiFonts::init); let root_path = profile.path().to_path_buf(); let rt = runtime.clone(); let window = cx.add_window(|window, cx| { ApplicationView::new( rt, ready, crate::launch::Storage { channel: None, root: root_path.clone(), database_dir: root_path.clone(), pointer: Some(root_path.join("storage.json")), }, window, cx, ) }); let app_entity = window.update(cx, |_, _, cx| cx.entity()).unwrap(); let deadline = std::time::Instant::now() + Duration::from_secs(10); while !cx.read(|cx| app_entity.read(cx).native_ready()) { assert!( std::time::Instant::now() < deadline, "timed out waiting for ApplicationView native_ready" ); cx.executor().advance_clock(Duration::from_millis(5)); cx.run_until_parked(); std::thread::sleep(Duration::from_millis(5)); } let vault = cx.read(|cx| app_entity.read(cx).editor_vault.clone()); PauseTestApp { _profile: profile, runtime, session_id, vault, app_entity, } } #[gpui::test] fn pause_writers_blocks_and_drains_attachment_imports_held_before_first_vault_write( cx: &mut gpui::TestAppContext, ) { let PauseTestApp { _profile, runtime, session_id, vault, app_entity, } = start_pause_test_app(cx); let service = crate::editor::AttachmentService::new(runtime.clone(), vault.clone()); let (entered_tx, entered_rx) = mpsc::channel::<()>(); let (release_tx, release_rx) = mpsc::channel::<()>(); let (import_done_tx, mut import_done_rx) = oneshot::channel(); let held_service = service.clone(); let held_session = session_id.clone(); cx.spawn(async move |_| { let result = held_service .import_reader_for_test( held_session, "held.bin".into(), Box::new(move || { let _ = entered_tx.send(()); let _ = release_rx.recv(); Ok(Box::new(Cursor::new(b"should-not-write".to_vec()))) }), ) .await; let _ = import_done_tx.send(result); }) .detach(); cx.run_until_parked(); entered_rx .recv_timeout(Duration::from_secs(5)) .expect("import worker should start before first vault write"); // File copying runs outside the sequential DB queue, so unrelated reads still proceed. let count: i64 = futures::executor::block_on( runtime .read(CancellationToken::new(), |services| async move { Ok(sqlx::query_scalar("SELECT COUNT(*) FROM sessions") .fetch_one(services.db.pool()) .await .unwrap()) }) .unwrap() .receive(), ) .unwrap(); assert!(count >= 1); let pause = cx.update(|cx| app_entity.update(cx, |app, cx| app.pause_writers(cx))); let (pause_done_tx, mut pause_done_rx) = oneshot::channel(); cx.spawn(async move |_| { let _ = pause_done_tx.send(pause.await); }) .detach(); cx.run_until_parked(); // New imports are immediately refused while writers are paused. assert!( futures::executor::block_on(service.import_bytes( session_id.clone(), "late.txt".into(), b"blocked".to_vec(), )) .is_err() ); // `pause_writers` must remain blocked while the admitted import worker is still held. for _ in 0..10 { cx.executor().advance_clock(Duration::from_millis(5)); cx.run_until_parked(); std::thread::sleep(Duration::from_millis(5)); } assert!( pause_done_rx.try_recv().unwrap().is_none(), "pause_writers must not finish while an admitted import worker is still active" ); // Releasing the worker lets it observe pause cancellation and drain cleanly. release_tx.send(()).unwrap(); let drain_deadline = std::time::Instant::now() + Duration::from_secs(5); let pause_result = loop { cx.executor().advance_clock(Duration::from_millis(5)); cx.run_until_parked(); if let Some(result) = pause_done_rx.try_recv().unwrap() { break result; } assert!( std::time::Instant::now() < drain_deadline, "timed out waiting for pause_writers to drain" ); std::thread::sleep(Duration::from_millis(5)); }; pause_result.unwrap(); let import_result = loop { cx.run_until_parked(); if let Some(result) = import_done_rx.try_recv().unwrap() { break result; } std::thread::sleep(Duration::from_millis(5)); }; assert!(import_result.is_err()); let attachments_dir = anlg_fs_sync_core::FsSyncCore::new(vault) .resolve_session_dir(session_id.0.as_ref()) .unwrap() .join("attachments"); assert!( !attachments_dir.exists() || std::fs::read_dir(&attachments_dir) .unwrap() .next() .is_none(), "cancelled import must not write files into the vault" ); let row_count: i64 = futures::executor::block_on( runtime .read(CancellationToken::new(), |services| async move { Ok( sqlx::query_scalar("SELECT COUNT(*) FROM session_attachments") .fetch_one(services.db.pool()) .await .unwrap(), ) }) .unwrap() .receive(), ) .unwrap(); assert_eq!(row_count, 0); // Resuming writers re-enables attachment imports. cx.update(|cx| app_entity.update(cx, |app, cx| app.resume_writers(cx))); let imported = futures::executor::block_on(service.import_bytes( session_id, "after-resume.txt".into(), b"resumed".to_vec(), )) .unwrap(); assert_eq!(imported.size, 7); let _ = futures::executor::block_on(runtime.shutdown()); } #[gpui::test] fn pause_writers_removes_staged_attachment_file_when_cancelled_before_metadata_commit( cx: &mut gpui::TestAppContext, ) { let PauseTestApp { _profile, runtime, session_id, vault, app_entity, } = start_pause_test_app(cx); let service = crate::editor::AttachmentService::new(runtime.clone(), vault.clone()); let (staged_tx, staged_rx) = mpsc::channel::(); let (release_tx, release_rx) = tokio::sync::oneshot::channel::<()>(); let (import_done_tx, mut import_done_rx) = oneshot::channel(); let held_service = service.clone(); let held_session = session_id.clone(); cx.spawn(async move |_| { let result = held_service .import_bytes_with_stage_barrier_for_test( held_session, "staged.bin".into(), b"copied-before-pause".to_vec(), staged_tx, release_rx, ) .await; let _ = import_done_tx.send(result); }) .detach(); cx.run_until_parked(); let staged_path = staged_rx .recv_timeout(Duration::from_secs(5)) .expect("import worker should stage file in vault before barrier"); assert!( staged_path.is_file(), "staged file must exist in the vault before pause_writers cancels metadata commit" ); let pause = cx.update(|cx| app_entity.update(cx, |app, cx| app.pause_writers(cx))); let (pause_done_tx, mut pause_done_rx) = oneshot::channel(); cx.spawn(async move |_| { let _ = pause_done_tx.send(pause.await); }) .detach(); cx.run_until_parked(); // `pause_writers` must wait until the staged import cancels and removes its staged file. for _ in 0..10 { cx.executor().advance_clock(Duration::from_millis(5)); cx.run_until_parked(); std::thread::sleep(Duration::from_millis(5)); } assert!( pause_done_rx.try_recv().unwrap().is_none(), "pause_writers must not finish before post-copy staged-file cleanup completes" ); release_tx.send(()).unwrap(); let drain_deadline = std::time::Instant::now() + Duration::from_secs(5); let pause_result = loop { cx.executor().advance_clock(Duration::from_millis(5)); cx.run_until_parked(); if let Some(result) = pause_done_rx.try_recv().unwrap() { break result; } assert!( std::time::Instant::now() < drain_deadline, "timed out waiting for pause_writers to drain post-copy cleanup" ); std::thread::sleep(Duration::from_millis(5)); }; pause_result.unwrap(); let import_result = loop { cx.run_until_parked(); if let Some(result) = import_done_rx.try_recv().unwrap() { break result; } std::thread::sleep(Duration::from_millis(5)); }; assert!(import_result.is_err()); assert!( !staged_path.exists(), "post-copy pause cancellation must remove the staged vault file" ); let attachments_dir = anlg_fs_sync_core::FsSyncCore::new(vault) .resolve_session_dir(session_id.0.as_ref()) .unwrap() .join("attachments"); assert!( !attachments_dir.exists() || std::fs::read_dir(&attachments_dir) .unwrap() .next() .is_none(), "vault attachments directory must remain empty after post-copy pause cleanup" ); let row_count: i64 = futures::executor::block_on( runtime .read(CancellationToken::new(), |services| async move { Ok( sqlx::query_scalar("SELECT COUNT(*) FROM session_attachments") .fetch_one(services.db.pool()) .await .unwrap(), ) }) .unwrap() .receive(), ) .unwrap(); assert_eq!(row_count, 0); let _ = futures::executor::block_on(runtime.shutdown()); } #[gpui::test] fn pause_writers_fails_recoverably_when_stalled_import_cannot_drain( cx: &mut gpui::TestAppContext, ) { let PauseTestApp { _profile, runtime, session_id, vault, app_entity, } = start_pause_test_app(cx); let service = crate::editor::AttachmentService::new(runtime.clone(), vault.clone()); let (entered_tx, entered_rx) = mpsc::channel::<()>(); let (release_tx, release_rx) = mpsc::channel::<()>(); let held_service = service.clone(); let held_session = session_id.clone(); cx.spawn(async move |_| { let _ = held_service .import_reader_for_test( held_session, "stalled.bin".into(), Box::new(move || { let _ = entered_tx.send(()); let _ = release_rx.recv(); Ok(Box::new(Cursor::new(b"stalled-source".to_vec()))) }), ) .await; }) .detach(); cx.run_until_parked(); entered_rx .recv_timeout(Duration::from_secs(5)) .expect("stalled import worker should enter reader"); let host_effects = cx.read(|cx| Arc::clone(&app_entity.read(cx).host_effects)); let (pause_done_tx, mut pause_done_rx) = oneshot::channel(); cx.spawn(async move |_| { let result = crate::product::local::HostEffects::pause_writers(&*host_effects).await; let _ = pause_done_tx.send(result); }) .detach(); cx.run_until_parked(); assert!( cx.read(|cx| app_entity.read(cx).writers_paused), "HostCommand::Pause must enter paused state before waiting for import drain" ); cx.executor().advance_clock(Duration::from_secs(11)); cx.run_until_parked(); let pause_result = pause_done_rx.try_recv().unwrap().expect( "HostCommand::Pause must time out instead of hanging forever on a stalled import", ); assert!(pause_result.is_err()); // HostCommand::Pause automatically resumes writers on drain timeout without manual recovery. assert!( !cx.read(|cx| app_entity.read(cx).writers_paused), "HostCommand::Pause must automatically clear writers_paused when drain fails" ); let recovered = futures::executor::block_on(service.import_bytes( session_id, "recovered.txt".into(), b"ok".to_vec(), )); assert!(recovered.is_ok()); let _ = release_tx.send(()); let _ = futures::executor::block_on(runtime.shutdown()); } }