perf(storage): prune error_log on startup with 90-day retention

The error_log table had no retention policy: every backend error was
appended forever, so across months of dogfooding it grew unbounded. That
silently bloats the diagnostic-bundle export and slows the
list_recent_errors query the Settings → About panel runs.

- New `kon_storage::prune_error_log(pool, keep_days)` does a single
  `DELETE FROM error_log WHERE timestamp < datetime('now', '-Nd days')`
  and returns the row count removed.
- src-tauri/src/lib.rs runs it once during setup() with a const
  ERROR_LOG_RETENTION_DAYS = 90. Failure is logged to stderr but does not
  block startup — a prune that fails is strictly less important than the
  app coming up.
- Test: insert three rows at now / -30d / -200d, verify a 90-day prune
  removes only the oldest, and a subsequent 14-day prune removes the
  -30d row. Storage suite at 60/60.

https://claude.ai/code/session_0189xUb6ie6t9qHkzatGZ9Rb
This commit is contained in:
Claude
2026-04-25 09:37:24 +00:00
parent 38da407942
commit c04c719d48
3 changed files with 86 additions and 2 deletions

View File

@@ -9,7 +9,14 @@ use tauri::Manager;
use kon_core::types::EngineName;
use kon_llm::LlmEngine;
use kon_storage::{database_path, get_setting, init as init_db, set_setting};
use kon_storage::{
database_path, get_setting, init as init_db, prune_error_log, set_setting,
};
/// How long to retain `error_log` rows. Pruned once on startup.
/// 90 days is long enough to triage "this happened a few weeks ago"
/// reports and short enough that the table stays kilobyte-scale.
const ERROR_LOG_RETENTION_DAYS: i64 = 90;
use kon_transcription::LocalEngine;
/// Shared app state holding the transcription engines and database pool.
@@ -158,6 +165,22 @@ pub fn run() {
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)?;
eprintln!("[startup] DB init: {:?}", t0.elapsed());
// Prune old `error_log` rows so the table doesn't grow unbounded
// across months of dogfooding. Best-effort — a prune failure is
// not worth blocking startup over.
let t_prune = Instant::now();
let pruned = tauri::async_runtime::block_on(async {
prune_error_log(&db, ERROR_LOG_RETENTION_DAYS).await
});
match pruned {
Ok(n) if n > 0 => eprintln!(
"[startup] Error log prune: {n} rows removed (>{ERROR_LOG_RETENTION_DAYS}d) in {:?}",
t_prune.elapsed()
),
Ok(_) => {}
Err(e) => eprintln!("[startup] Error log prune failed: {e}"),
}
// Load saved preferences for webview injection
let t1 = Instant::now();
let prefs_json = tauri::async_runtime::block_on(async {