Skip to main content

second_brain_api/
stored_redaction.rs

1use std::sync::Arc;
2use std::time::Duration;
3
4use axum::Json;
5use axum::extract::State;
6use axum::response::IntoResponse;
7
8use second_brain_core::embedding::Embedder;
9use second_brain_core::residency;
10use second_brain_core::sqlite_store::{RedactionRun, SqliteStore};
11
12use crate::AppState;
13use crate::routes::internal_error;
14use crate::store_compaction::Compaction;
15
16const FIRST_RETRY: Duration = Duration::from_secs(30);
17const MAX_RETRY: Duration = Duration::from_secs(3600);
18
19/// Runs the one-time pass on its own thread, retrying a failed run with
20/// capped backoff, then the compaction that clears what the pass left on
21/// disk. The store records progress of both, so a restart resumes them.
22pub fn spawn_pass(
23    store: Arc<SqliteStore>,
24    embedder: Option<Arc<Embedder>>,
25    compaction: Compaction,
26) -> std::io::Result<std::thread::JoinHandle<()>> {
27    std::thread::Builder::new()
28        .name("redaction-pass".to_string())
29        .spawn(move || {
30            let embed = embedder
31                .as_ref()
32                .map(|e| move |texts: &[&str]| e.embed_batch(texts));
33            let embed_ref = embed
34                .as_ref()
35                .map(|f| f as &dyn Fn(&[&str]) -> anyhow::Result<Vec<Vec<f32>>>);
36            let run = run_until_done(|| store.run_stored_redaction(embed_ref), std::thread::sleep);
37            tracing::info!(?run, "stored-content redaction pass finished");
38            compaction.run_until_done(std::thread::sleep);
39            residency::release_free_heap();
40        })
41}
42
43fn run_until_done(
44    mut run: impl FnMut() -> anyhow::Result<RedactionRun>,
45    mut sleep: impl FnMut(Duration),
46) -> RedactionRun {
47    let mut delay = FIRST_RETRY;
48    loop {
49        match run() {
50            Ok(done) => return done,
51            Err(e) => {
52                tracing::error!(
53                    "stored-content redaction pass failed, retrying in {}s: {e:#}",
54                    delay.as_secs()
55                );
56                sleep(delay);
57                delay = (delay * 2).min(MAX_RETRY);
58            }
59        }
60    }
61}
62
63pub async fn report(State(state): State<Arc<AppState>>) -> impl IntoResponse {
64    let sqlite = state.sqlite.clone();
65    match tokio::task::spawn_blocking(move || sqlite.stored_redaction_report()).await {
66        Ok(Ok(report)) => Json(report).into_response(),
67        Ok(Err(e)) => internal_error(format!("{e:#}")),
68        Err(e) => internal_error(e),
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn a_failed_run_is_retried_with_capped_backoff() {
78        let mut attempts = 0;
79        let mut slept = Vec::new();
80        let run = run_until_done(
81            || {
82                attempts += 1;
83                if attempts < 9 {
84                    anyhow::bail!("database is locked");
85                }
86                Ok(RedactionRun {
87                    memories_rewritten: 1,
88                    ..RedactionRun::default()
89                })
90            },
91            |d| slept.push(d.as_secs()),
92        );
93        assert_eq!(run.memories_rewritten, 1);
94        assert_eq!(slept, [30, 60, 120, 240, 480, 960, 1920, 3600]);
95    }
96}