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::opencalls it once, synchronously, on every startup of both binaries – the “on startup” half of the policy described in AGENTS.md’shistory-retentiondesign 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’smain.rsadditionally 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-runpath calls it directly for an immediately-requested, possibly policy-overriding one-off sweep.
Functions§
- cutoff_
for - The
started_atcutoff for adays-day retention policy evaluated atnow: runs started at or before this instant are in scope for deletion. Pulled out ofsweep_retentionsocommands::history::prune’s--dry-runpreview 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_atat or before thedays-day cutoff (seecutoff_for), along with – viaON DELETE CASCADE– its samples, results, and write-back audit rows. Returns the number of runs deleted.