Skip to main content

Module retention

Module retention 

Source
Expand description

history-retention: age-based deletion of old tune runs.

The actual DELETE lives in bhtune_db::models::TuneRunRow::delete_matching (a single statement, which SQLite already treats as its own transaction); this module owns the one thing bhtune-db deliberately doesn’t – turning “N days” into a cutoff timestamp and logging what happened, since bhtune-db has no logging dependency of its own (see its crate doc comment).

sweep_retention is the single code path shared by every caller that enforces the policy, so “what a bhtune history prune run deletes”, “what crate::db::open’s startup sweep deletes”, and “what bhtune-server’s periodic timer deletes” can never disagree:

  • crate::db::open calls it once, synchronously, on every startup of both binaries – the “on startup” half of the policy described in AGENTS.md’s history-retention design note. A failure here is propagated (?), matching how that function already treats a failed template-seed as fatal: a one-shot CLI invocation failing fast and clearly beats silently skipping a maintenance step that might be masking a real database problem.
  • bhtune-server’s main.rs additionally calls it on a periodic timer for as long as the process keeps running, so a long-lived server doesn’t have to be restarted just to have its retention policy re-applied. Unlike the startup call, a failure there is logged and the timer keeps ticking – crashing a process that’s actively serving HTTP requests (and possibly mid-tune) over a background housekeeping error would be a far worse outcome than one skipped sweep.
  • bhtune history prune’s non---dry-run path calls it directly for an immediately-requested, possibly policy-overriding one-off sweep.

Functions§

cutoff_for
The started_at cutoff for a days-day retention policy evaluated at now: runs started at or before this instant are in scope for deletion. Pulled out of sweep_retention so commands::history::prune’s --dry-run preview can compute and display the exact same cutoff its non-dry-run sibling would actually delete against, without needing a database handle to do it.
sweep_retention
Deletes every tune run with started_at at or before the days-day cutoff (see cutoff_for), along with – via ON DELETE CASCADE – its samples, results, and write-back audit rows. Returns the number of runs deleted.