From 985beaa2212fafff591d9126935fd23b49bdf3e9 Mon Sep 17 00:00:00 2001 From: hauke Date: Thu, 5 Nov 2026 14:04:52 +0100 Subject: [PATCH] feat #14740: retention planning and startup recovery Co-Authored-By: Claude Sonnet 5 --- crates/core/src/lib.rs | 1 + crates/core/src/retention.rs | 188 +++++++++++++++++++++++++++++++++++ crates/core/src/store.rs | 139 ++++++++++++++++++++++++++ 3 files changed, 328 insertions(+) create mode 100644 crates/core/src/retention.rs diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index d79bc4a..f083447 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -5,6 +5,7 @@ //! application shell so it can be unit-tested without a GUI or network. pub mod model; +pub mod retention; pub mod store; #[cfg(test)] diff --git a/crates/core/src/retention.rs b/crates/core/src/retention.rs new file mode 100644 index 0000000..a3feea9 --- /dev/null +++ b/crates/core/src/retention.rs @@ -0,0 +1,188 @@ +//! Pure retention policy: decides which audio files or call rows should be +//! deleted, given the configured retention period. Does not touch the store +//! or the filesystem; callers act on the returned actions. + +use chrono::{DateTime, Duration, Local}; + +use crate::model::{Call, CallId, CallStatus}; + +/// An action the retention sweep should perform. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum RetentionAction { + /// Delete the call's audio file(s) on disk; the row stays (it has a + /// summary/transcript worth keeping). + DeleteAudio(CallId), + /// Delete the whole call row (and everything derived from it). + DeleteCall(CallId), +} + +/// Plans retention actions for `calls` as of `now`, given a retention period +/// of `retention_days`. A timestamp is in scope for deletion once it is +/// strictly older than the retention period (exactly `retention_days` old is +/// not yet planned). +/// +/// Rules: +/// - `Done` calls: delete audio once `transcribed_at` is older than the +/// retention period, unless the audio was already deleted. A `Done` call +/// with no `transcribed_at` is never planned. +/// - `AwaitingDecision` calls: delete the whole row once `ended_at` is older +/// than the retention period. A call with no `ended_at` is never planned. +/// - `Processing`, `Recording` and `Failed` calls are never planned (failed +/// calls keep their audio forever). +/// +/// Output preserves the order of `calls`. +pub fn plan_retention( + calls: &[Call], + now: DateTime, + retention_days: u32, +) -> Vec { + let retention = Duration::days(retention_days as i64); + let mut actions = Vec::new(); + + for call in calls { + match call.status { + CallStatus::Done => { + if call.audio_deleted { + continue; + } + if let Some(transcribed_at) = call.transcribed_at { + if now - transcribed_at > retention { + actions.push(RetentionAction::DeleteAudio(call.id)); + } + } + } + CallStatus::AwaitingDecision => { + if let Some(ended_at) = call.ended_at { + if now - ended_at > retention { + actions.push(RetentionAction::DeleteCall(call.id)); + } + } + } + CallStatus::Recording | CallStatus::Processing | CallStatus::Failed => {} + } + } + + actions +} + +#[cfg(test)] +mod tests { + use std::path::PathBuf; + + use chrono::{Duration, Local}; + + use crate::model::{Call, CallId, CallStatus}; + + use super::{plan_retention, RetentionAction}; + + fn call(id: CallId, status: CallStatus) -> Call { + Call { + id, + app: "teams.exe".to_string(), + started_at: Local::now(), + ended_at: None, + status, + interrupted: false, + error: None, + mic_path: PathBuf::from("/tmp/mic.wav"), + out_path: PathBuf::from("/tmp/out.wav"), + transcribed_at: None, + audio_deleted: false, + account_id: None, + } + } + + #[test] + fn done_audio_deleted_after_retention() { + let now = Local::now(); + let mut c = call(1, CallStatus::Done); + c.transcribed_at = Some(now - Duration::days(8)); + + assert_eq!( + plan_retention(&[c], now, 7), + vec![RetentionAction::DeleteAudio(1)] + ); + } + + #[test] + fn done_audio_kept_within_retention() { + let now = Local::now(); + let mut c = call(1, CallStatus::Done); + c.transcribed_at = Some(now - Duration::days(6)); + + assert_eq!(plan_retention(&[c], now, 7), vec![]); + } + + #[test] + fn done_without_transcribed_at_never_planned() { + let now = Local::now(); + let c = call(1, CallStatus::Done); + + assert_eq!(plan_retention(&[c], now, 7), vec![]); + } + + #[test] + fn undecided_call_deleted_after_retention() { + let now = Local::now(); + let mut c = call(1, CallStatus::AwaitingDecision); + c.ended_at = Some(now - Duration::days(8)); + + assert_eq!( + plan_retention(&[c], now, 7), + vec![RetentionAction::DeleteCall(1)] + ); + } + + #[test] + fn undecided_without_ended_at_never_planned() { + let now = Local::now(); + let c = call(1, CallStatus::AwaitingDecision); + + assert_eq!(plan_retention(&[c], now, 7), vec![]); + } + + #[test] + fn failed_call_keeps_audio_forever() { + let now = Local::now(); + let mut c = call(1, CallStatus::Failed); + c.ended_at = Some(now - Duration::days(30)); + + assert_eq!(plan_retention(&[c], now, 7), vec![]); + } + + #[test] + fn already_deleted_audio_not_planned_again() { + let now = Local::now(); + let mut c = call(1, CallStatus::Done); + c.transcribed_at = Some(now - Duration::days(30)); + c.audio_deleted = true; + + assert_eq!(plan_retention(&[c], now, 7), vec![]); + } + + #[test] + fn exactly_n_days_not_yet_planned() { + let now = Local::now(); + let mut c = call(1, CallStatus::Done); + c.transcribed_at = Some(now - Duration::days(7)); + + assert_eq!(plan_retention(&[c], now, 7), vec![]); + } + + #[test] + fn output_order_matches_input_order() { + let now = Local::now(); + let mut a = call(1, CallStatus::Done); + a.transcribed_at = Some(now - Duration::days(8)); + let mut b = call(2, CallStatus::AwaitingDecision); + b.ended_at = Some(now - Duration::days(8)); + + assert_eq!( + plan_retention(&[a, b], now, 7), + vec![ + RetentionAction::DeleteAudio(1), + RetentionAction::DeleteCall(2), + ] + ); + } +} diff --git a/crates/core/src/store.rs b/crates/core/src/store.rs index c837c4f..7438446 100644 --- a/crates/core/src/store.rs +++ b/crates/core/src/store.rs @@ -26,6 +26,20 @@ pub enum StoreError { pub type Result = std::result::Result; +/// Error text stored on calls that were mid-processing when the app last +/// shut down (or crashed). +const RECOVERY_ERROR: &str = "Verarbeitung unterbrochen – bitte erneut versuchen"; + +/// What [`Store::startup_recovery`] did to reconcile call states left over +/// from an unclean shutdown. +#[derive(Debug, Clone, PartialEq, Default)] +pub struct RecoveryReport { + /// Calls moved from `Recording` to `AwaitingDecision`. + pub interrupted: Vec, + /// Calls moved from `Processing` to `Failed`. + pub failed: Vec, +} + const SCHEMA_SQL: &str = " CREATE TABLE IF NOT EXISTS calls ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -340,6 +354,61 @@ impl Store { conn.execute("DELETE FROM remote_jobs WHERE call_id = ?1", params![id])?; Ok(()) } + + /// Reconciles call states left over from an unclean shutdown (the app + /// crashed or was killed mid-recording or mid-processing). + /// + /// - `Recording` calls were still being recorded: they move to + /// `AwaitingDecision`, with `interrupted` set and `ended_at` set to now + /// if it was not already set (a later pass corrects it from the WAV + /// length). + /// - `Processing` calls were mid-transcription/summarization: they move + /// to `Failed` with a fixed error message. + /// + /// Both updates run in a single transaction. + pub fn startup_recovery(&self) -> Result { + let now = Local::now(); + let mut conn = self.conn.lock().unwrap(); + let tx = conn.transaction()?; + + let interrupted = ids_with_status(&tx, CallStatus::Recording)?; + for id in &interrupted { + tx.execute( + "UPDATE calls SET status = ?1, interrupted = 1, ended_at = COALESCE(ended_at, ?2) \ + WHERE id = ?3", + params![ + status_to_str(CallStatus::AwaitingDecision), + dt_to_str(now), + id + ], + )?; + } + + let failed = ids_with_status(&tx, CallStatus::Processing)?; + for id in &failed { + tx.execute( + "UPDATE calls SET status = ?1, error = ?2 WHERE id = ?3", + params![status_to_str(CallStatus::Failed), RECOVERY_ERROR, id], + )?; + } + + tx.commit()?; + Ok(RecoveryReport { + interrupted, + failed, + }) + } +} + +/// Ids of calls currently in `status`, ordered by id (insertion order). +fn ids_with_status(conn: &rusqlite::Connection, status: CallStatus) -> Result> { + let mut stmt = conn.prepare("SELECT id FROM calls WHERE status = ?1 ORDER BY id ASC")?; + let mut rows = stmt.query(params![status_to_str(status)])?; + let mut ids = Vec::new(); + while let Some(row) = rows.next()? { + ids.push(row.get(0)?); + } + Ok(ids) } const SELECT_ALL_CALLS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id FROM calls"; @@ -605,6 +674,76 @@ mod tests { assert_eq!(ids, vec![id3, id2, id1]); } + #[test] + fn startup_recovery_marks_states() { + let store = Store::open_in_memory().unwrap(); + let started = Local::now(); + + let recording_id = store + .create_call( + "teams.exe", + started, + Path::new("/tmp/mic1.wav"), + Path::new("/tmp/out1.wav"), + ) + .unwrap(); + + let processing_id = store + .create_call( + "chrome.exe", + started, + Path::new("/tmp/mic2.wav"), + Path::new("/tmp/out2.wav"), + ) + .unwrap(); + store + .set_status(processing_id, CallStatus::Processing, None) + .unwrap(); + + let before = Local::now(); + let report = store.startup_recovery().unwrap(); + let after = Local::now(); + + assert_eq!(report.interrupted, vec![recording_id]); + assert_eq!(report.failed, vec![processing_id]); + + let recovered = store.get_call(recording_id).unwrap().unwrap(); + assert_eq!(recovered.status, CallStatus::AwaitingDecision); + assert!(recovered.interrupted); + let ended_at = recovered.ended_at.expect("ended_at set by recovery"); + assert!(ended_at >= before && ended_at <= after); + + let failed_call = store.get_call(processing_id).unwrap().unwrap(); + assert_eq!(failed_call.status, CallStatus::Failed); + assert_eq!( + failed_call.error.as_deref(), + Some("Verarbeitung unterbrochen – bitte erneut versuchen") + ); + } + + #[test] + fn startup_recovery_keeps_existing_ended_at() { + let store = Store::open_in_memory().unwrap(); + let started = Local::now(); + let id = store + .create_call( + "teams.exe", + started, + Path::new("/tmp/mic.wav"), + Path::new("/tmp/out.wav"), + ) + .unwrap(); + + let original_ended_at = started + chrono::Duration::minutes(5); + store.set_ended_at(id, original_ended_at).unwrap(); + + let report = store.startup_recovery().unwrap(); + assert_eq!(report.interrupted, vec![id]); + + let recovered = store.get_call(id).unwrap().unwrap(); + assert_eq!(recovered.ended_at, Some(original_ended_at)); + } + #[test] fn delete_call_cascades() { let store = Store::open_in_memory().unwrap();