//! Meeting retention: how long finished meetings, and separately their WAV //! tracks, are kept before moving to the Trash. use std::path::Path; use color_eyre::eyre::{Result, eyre}; use super::{ FINAL_PENDING_FILE, MeetingManifest, MeetingStatus, exported_markdown, file_sha256, list_all, meeting_is_active, notes_in_progress, now_ms, os, pending_final_transcripts, read_manifest, root, write_manifest, }; use crate::app_settings::MeetingRetention; /// Every meeting whose final transcript is still owed: one with a /// `final.pending` marker, or one the launch recovery will finish while it /// can actually run. With Meetings off or no installed model, unfinished /// meetings expire like any other. fn awaiting_final_transcript(meetings: &[MeetingManifest]) -> Vec { let mut ids = pending_final_transcripts(); if super::recovery::can_recover() { ids.extend(super::recovery::stranded_among(meetings)); } ids.sort(); ids.dedup(); ids } /// User-facing readers omit expired meetings even before the Trash sweep. pub fn list() -> Result> { let chosen = crate::app_settings::meeting_retention(); let now = now_ms(); let meetings = list_all()?; let pending = awaiting_final_transcript(&meetings); Ok(meetings .into_iter() .filter(|meeting| { readable( meeting, meeting.status, pending.contains(&meeting.id), chosen, now, ) }) .collect()) } /// How long `manifest` is kept: the retention the user chose, else /// [`MeetingRetention::DEFAULT`] for meetings recorded since it began, and /// older meetings for good. pub fn retention_for( manifest: &MeetingManifest, chosen: Option, ) -> MeetingRetention { chosen.unwrap_or(if manifest.retention_applies { MeetingRetention::DEFAULT } else { MeetingRetention::Forever }) } /// Read admission and retention share the same pending-final exception. /// A retained draft must remain readable while its final pass waits or fails. pub(super) fn readable( manifest: &MeetingManifest, status: MeetingStatus, pending_final: bool, chosen: Option, now_ms: u64, ) -> bool { pending_final || status.is_active() || retention_for(manifest, chosen) .max_age_ms() .is_none_or(|max_age_ms| { let ended = manifest.ended_at_ms.unwrap_or(manifest.created_at_ms); now_ms.saturating_sub(ended) <= max_age_ms }) } /// Moves every expired meeting to the Trash: its folder, and its exported /// Markdown while that is still the copy Hex wrote. One that is recording, /// transcribing, writing notes, or waiting for its final transcript stays /// for a later pass. Returns how many moved. pub fn expire(chosen: Option) -> Result { let meetings = list_all()?; let pending = awaiting_final_transcript(&meetings); let root = root()?; let mut moved = 0; for id in expired_meetings(&meetings, chosen, &pending, now_ms()) { match trash_expired(&root.join(id), os::move_to_trash) { Ok(()) => { moved += 1; tracing::info!(meeting = id, "moved an expired meeting to the Trash"); } Err(error) => { tracing::info!(%error, meeting = id, "kept an expired meeting for now"); } } } Ok(moved) } /// The expired meetings in `meetings`, leaving out `pending` ones. pub(super) fn expired_meetings<'a>( meetings: &'a [MeetingManifest], chosen: Option, pending: &[String], now_ms: u64, ) -> Vec<&'a str> { meetings .iter() .filter(|meeting| { !readable( meeting, meeting.status, pending.contains(&meeting.id), chosen, now_ms, ) }) .map(|meeting| meeting.id.as_str()) .collect() } /// [`super::trash_meeting`] for expiry: an exported Markdown the user changed /// is left where it is. pub(super) fn trash_expired( directory: &Path, mut move_to_trash: impl FnMut(&Path) -> Result<()>, ) -> Result<()> { let manifest = read_manifest(directory)?; if meeting_is_active(directory) != Some(false) { return Err(eyre!("the meeting is recording")); } if notes_in_progress(&manifest.id) { return Err(eyre!("its notes are being written")); } if let Some(path) = exported_markdown(&manifest) { if manifest.export_sha256.is_some() && file_sha256(&path) == manifest.export_sha256 { move_to_trash(&path)?; } else { tracing::info!( meeting = manifest.id, "kept the meeting's Markdown, which changed since Hex wrote it" ); } } if let Some(path) = super::notes_file::tracked(&manifest) { if manifest.notes_export_sha256.is_some() && file_sha256(&path) == manifest.notes_export_sha256 { move_to_trash(&path)?; } else { tracing::info!( meeting = manifest.id, "kept the meeting's notes file, which changed since Hex wrote it" ); } } move_to_trash(directory) } /// Whether a finished meeting's audio outlived a separate audio retention, /// counted from when it ended (or, with no end, started). Only a Complete /// meeting with its final transcript published and no audio removed yet can. pub fn audio_expired( manifest: &MeetingManifest, status: MeetingStatus, chosen: Option, now_ms: u64, ) -> bool { status == MeetingStatus::Complete && manifest.audio_removed_at_ms.is_none() && chosen .and_then(MeetingRetention::max_age_ms) .is_some_and(|max_age_ms| { let ended = manifest.ended_at_ms.unwrap_or(manifest.created_at_ms); now_ms.saturating_sub(ended) > max_age_ms }) } /// Moves the WAV tracks of meetings past a separate audio retention to the /// Trash, keeping their transcripts, notes, summary, and exported Markdown. /// One that is not Complete, has a pending final transcript, is recording, /// or is writing notes stays for a later pass. Returns how many lost audio. pub fn expire_audio(chosen: Option) -> Result { let Some(chosen) = chosen else { return Ok(0); }; let meetings = list_all()?; let pending = awaiting_final_transcript(&meetings); let root = root()?; let mut removed = 0; for id in audio_expired_meetings(&meetings, chosen, &pending, now_ms()) { match trash_audio_tracks(&root.join(id), notes_in_progress, os::move_to_trash) { Ok(()) => { removed += 1; tracing::info!( meeting = id, "moved an expired meeting's audio to the Trash" ); } Err(error) => { tracing::info!(%error, meeting = id, "kept an expired meeting's audio for now"); } } } Ok(removed) } /// The meetings in `meetings` whose audio outlived `chosen`, leaving out /// `pending` ones. pub(super) fn audio_expired_meetings<'a>( meetings: &'a [MeetingManifest], chosen: MeetingRetention, pending: &[String], now_ms: u64, ) -> Vec<&'a str> { meetings .iter() .filter(|meeting| !pending.contains(&meeting.id)) .filter(|meeting| audio_expired(meeting, meeting.status, Some(chosen), now_ms)) .map(|meeting| meeting.id.as_str()) .collect() } /// Moves only a finished meeting's WAV tracks to the Trash and records it in /// the manifest; its text and Markdown stay. pub(super) fn trash_audio_tracks( directory: &Path, notes_busy: impl Fn(&str) -> bool, mut move_to_trash: impl FnMut(&Path) -> Result<()>, ) -> Result<()> { let manifest = read_manifest(directory)?; if manifest.status != MeetingStatus::Complete { return Err(eyre!("the meeting is not finished")); } if directory.join(FINAL_PENDING_FILE).exists() { return Err(eyre!("its final transcript is still pending")); } if meeting_is_active(directory) != Some(false) { return Err(eyre!("the meeting is recording")); } if notes_busy(&manifest.id) { return Err(eyre!("its notes are being written")); } if manifest.audio_removed_at_ms.is_some() { return Ok(()); } for file in [&manifest.system.file, &manifest.microphone.file] { let path = directory.join(file); if path.exists() { move_to_trash(&path)?; } } // Re-read so concurrent manifest updates are not lost. let mut manifest = read_manifest(directory)?; manifest.audio_removed_at_ms = Some(now_ms()); write_manifest(directory, &manifest) }