feat #14740: retention planning and startup recovery

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-05 14:04:52 +01:00
co-authored by Claude Sonnet 5
parent 7cfe2963c0
commit 985beaa221
3 changed files with 328 additions and 0 deletions
+188
View File
@@ -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<Local>,
retention_days: u32,
) -> Vec<RetentionAction> {
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),
]
);
}
}