second-brain-api 0.13.4

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{Sender, channel};
use std::time::{Duration, Instant};

use anyhow::{Context, Result, anyhow, bail, ensure};
use serde::Serialize;

use second_brain_core::cold;
use second_brain_core::dir_copy::{
    copy_dir_stable, copy_file_synced, copy_symlink, is_not_found, sync_dir,
};
use second_brain_core::index::raw;
use second_brain_core::keys::{
    self, DeviceKeys, WRAPPED_STORE_KEY_FILE, WrapRefresh, every_held_epoch, left_behind,
    unwrap_store_key_with_epochs,
};
use second_brain_core::private_files::create_private_dir;
use second_brain_core::sqlite_store::encrypt_migration::key_opens_file;
use second_brain_core::sqlite_store::{AtRestPurpose, SqliteStore};
use second_brain_core::transcript_archive::TranscriptArchive;

use crate::maintenance::{DrainRequest, PauseCommand};
use crate::write_gate::WriteGate;

const DB_FILE: &str = "brain.db";
const TRANSCRIPTS_DIR: &str = "transcripts";
const MODEL_CACHE_DIR: &str = "fastembed";
const STALE_MARKER: &str = ".stale-";
const COLD_RETRY: Duration = Duration::from_secs(5);
const COLD_WAIT: Duration = Duration::from_secs(30 * 60);

/// What the backup reads: the live store and the threads that write beside
/// it, each held only while its part is copied.
pub struct Backup<'a> {
    pub store: &'a SqliteStore,
    pub data_dir: &'a Path,
    pub gate: &'a WriteGate,
    /// The index drain thread; None when the indexes never opened, and then
    /// the backup carries no index for the restored daemon to reuse.
    pub drain: Option<&'a Sender<DrainRequest>>,
    pub archive: Option<&'a TranscriptArchive>,
}

#[derive(Debug, Serialize)]
pub struct BackupSummary {
    pub path: PathBuf,
    pub memory_count: u64,
    pub stored_total: u64,
    pub bytes: u64,
    pub outbox_settled: bool,
    pub indexes: &'static str,
    pub raw_index: &'static str,
}

#[derive(Debug)]
pub enum BackupError {
    BadDestination(String),
    Refused(String),
    Failed(anyhow::Error),
}

impl std::fmt::Display for BackupError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::BadDestination(m) | Self::Refused(m) => f.write_str(m),
            Self::Failed(e) => write!(f, "{e:#}"),
        }
    }
}

impl From<anyhow::Error> for BackupError {
    fn from(e: anyhow::Error) -> Self {
        Self::Failed(e)
    }
}

struct Live {
    db: PathBuf,
    indexes: PathBuf,
    raw_index: PathBuf,
    cold: PathBuf,
}

impl Live {
    fn of(db: PathBuf) -> Self {
        Self {
            indexes: db.with_extension("indexes"),
            raw_index: raw::dir_for_db(&db),
            cold: cold::dir_for_db(&db),
            db,
        }
    }

    fn handled(&self, name: &str) -> bool {
        let db = self
            .db
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or(DB_FILE);
        [&self.indexes, &self.raw_index, &self.cold]
            .iter()
            .any(|dir| dir.file_name().and_then(|n| n.to_str()) == Some(name))
            || name == db
            || name == format!("{db}-wal")
            || name == TRANSCRIPTS_DIR
            || name == WRAPPED_STORE_KEY_FILE
    }
}

impl Backup<'_> {
    /// Writes a copy of the data dir to `dest` that `sb restore` accepts,
    /// with the store, its indexes, its cold segments and its wrapped key
    /// all taken at one moment. `dest` appears only once complete.
    pub fn write(&self, dest: &Path) -> Result<BackupSummary, BackupError> {
        let _maintenance = self.gate.hold_maintenance();
        let live = Live::of(self.db_path()?);
        let (parent, name) = self.check_destination(dest)?;
        let dest = parent.join(&name);
        remove_stale_partials(&parent, &name);
        let partial = parent.join(format!(".{name}.partial-{}", uuid::Uuid::new_v4().simple()));
        create_private_dir(&partial)?;
        let published = self.fill(&partial, &live).and_then(|summary| {
            sync_dir(&partial)?;
            fs::rename(&partial, &dest)
                .with_context(|| format!("renaming {} to {}", partial.display(), dest.display()))?;
            Ok(summary)
        });
        match published {
            Ok(summary) => {
                if let Err(e) = sync_dir(&parent) {
                    tracing::warn!("the backup is in place but not yet flushed to disk: {e:#}");
                }
                Ok(BackupSummary {
                    path: dest,
                    ..summary
                })
            }
            Err(e) => {
                if let Err(cleanup) = fs::remove_dir_all(&partial) {
                    tracing::warn!(
                        "cannot remove the partial backup {}: {cleanup}",
                        partial.display()
                    );
                }
                Err(e)
            }
        }
    }

    fn db_path(&self) -> Result<PathBuf, BackupError> {
        let db = self.store.file_path()?.ok_or_else(|| {
            BackupError::Refused("an in-memory store has no file to back up".into())
        })?;
        let dir = db.parent().map(Path::to_path_buf).unwrap_or_default();
        if fs::canonicalize(&dir).ok() != fs::canonicalize(self.data_dir).ok() {
            return Err(BackupError::Refused(format!(
                "the store {} is outside the data dir {}, so restore would not find it",
                db.display(),
                self.data_dir.display()
            )));
        }
        Ok(db)
    }

    fn check_destination(&self, dest: &Path) -> Result<(PathBuf, String), BackupError> {
        let bad = |m: String| Err(BackupError::BadDestination(m));
        if !dest.is_absolute() {
            return bad(format!("{} is not an absolute path", dest.display()));
        }
        let (Some(parent), Some(name)) = (dest.parent(), dest.file_name()) else {
            return bad(format!("{} names no directory", dest.display()));
        };
        let parent = fs::canonicalize(parent)
            .map_err(|e| BackupError::BadDestination(format!("{}: {e}", parent.display())))?;
        let data_dir = fs::canonicalize(self.data_dir).context("reading the data dir")?;
        if parent.starts_with(&data_dir) {
            return bad(format!(
                "{} is inside the data dir {}",
                dest.display(),
                data_dir.display()
            ));
        }
        if fs::symlink_metadata(parent.join(name)).is_ok() {
            return Err(BackupError::Refused(format!(
                "{} already exists; a backup never writes over one",
                dest.display()
            )));
        }
        Ok((parent, name.to_string_lossy().into_owned()))
    }

    fn fill(&self, partial: &Path, live: &Live) -> Result<BackupSummary, BackupError> {
        let device = self.fix_keys(partial)?;
        let copy_db = partial.join(DB_FILE);
        let indexes = self.snapshot(&copy_db, live)?;
        let checked = self.store.check_backup_copy(&copy_db)?;
        if let Some(device) = &device {
            self.check_wrapped_key(device, partial, &copy_db)?;
        }
        let raw_index = if live.raw_index.is_dir()
            && copy_dir_stable(&live.raw_index, &raw::dir_for_db(&copy_db))?
        {
            "copied"
        } else {
            "rebuilt at first start"
        };
        self.copy_transcripts(&partial.join(TRANSCRIPTS_DIR))?;
        self.copy_rest(partial, live)?;
        Ok(BackupSummary {
            path: partial.to_path_buf(),
            memory_count: checked.counts.live,
            stored_total: checked.counts.stored_total,
            bytes: tree_bytes(partial)?,
            outbox_settled: checked.outbox_settled,
            indexes,
            raw_index,
        })
    }

    /// Settles every key the copy depends on before it is taken: the wrapped
    /// store key restore opens, and the at-rest keys that seal the index and
    /// transcript files, which the copied store must already hold.
    fn fix_keys(&self, partial: &Path) -> Result<Option<DeviceKeys>, BackupError> {
        if !self.store.is_encrypted() {
            return Ok(None);
        }
        let device = keys::load_device_keys(self.data_dir)?.ok_or_else(|| {
            BackupError::Refused("this device has no device key to wrap the store key with".into())
        })?;
        if let WrapRefresh::NoIdentity =
            keys::refresh_store_key_wrap(self.store, self.data_dir, &device)?
        {
            return Err(BackupError::Refused(
                "this device has no sync identity, so no key could open its backup; \
                 pair it with `sb pair` first"
                    .into(),
            ));
        }
        for purpose in [
            AtRestPurpose::Indexes,
            AtRestPurpose::RawIndex,
            AtRestPurpose::TranscriptArchive,
        ] {
            self.store.at_rest_keys(purpose)?;
        }
        copy_file_synced(
            &self.data_dir.join(WRAPPED_STORE_KEY_FILE),
            &partial.join(WRAPPED_STORE_KEY_FILE),
        )?;
        Ok(Some(device))
    }

    /// Takes the store copy while the drain thread and cold passes are held,
    /// then copies the files they write, so both match the copy. Lock order:
    /// maintenance, drain pause, cold guard; the drain thread's purge takes
    /// the cold guard, so it must be paused first.
    fn snapshot(&self, copy_db: &Path, live: &Live) -> Result<&'static str> {
        let deadline = Instant::now() + COLD_WAIT;
        loop {
            let paused = self.drain.map(Paused::start).transpose()?;
            let guard = match self.store.cold_tier() {
                None => None,
                Some(tier) => match tier.try_hold_passes() {
                    Some(guard) => Some(guard),
                    None => {
                        drop(paused);
                        ensure!(
                            Instant::now() < deadline,
                            "a cold pass held the cold tier for 30 minutes; run the backup again"
                        );
                        std::thread::sleep(COLD_RETRY);
                        continue;
                    }
                },
            };
            if let Some(paused) = &paused {
                paused.settle()?;
            }
            self.store.vacuum_into(copy_db)?;
            let indexes = match &paused {
                None => "unavailable",
                Some(_) if !live.indexes.is_dir() => "unavailable",
                Some(_) => {
                    if copy_dir_stable(&live.indexes, &copy_db.with_extension("indexes"))? {
                        "copied"
                    } else {
                        "rebuilt at first start"
                    }
                }
            };
            drop(paused);
            if live.cold.is_dir() {
                ensure!(
                    copy_dir_stable(&live.cold, &cold::dir_for_db(copy_db))?,
                    "the cold segments at {} changed while the cold tier was held",
                    live.cold.display()
                );
            }
            drop(guard);
            return Ok(indexes);
        }
    }

    /// Unwraps the copied store key the way `sb restore --own-keys` does
    /// and proves it opens the copied store.
    fn check_wrapped_key(&self, device: &DeviceKeys, partial: &Path, copy_db: &Path) -> Result<()> {
        let held = every_held_epoch(self.store, device)?;
        let key = unwrap_store_key_with_epochs(partial, &held)?
            .ok_or_else(|| anyhow!("the backup holds no {WRAPPED_STORE_KEY_FILE}"))?;
        ensure!(
            key_opens_file(copy_db, &key),
            "the copied {WRAPPED_STORE_KEY_FILE} does not open the copied store"
        );
        Ok(())
    }

    fn copy_transcripts(&self, dest: &Path) -> Result<()> {
        match self.archive {
            Some(archive) => archive.copy_into(dest),
            None => {
                let live = self.data_dir.join(TRANSCRIPTS_DIR);
                if live.is_dir() && !copy_dir_stable(&live, dest)? {
                    bail!("the transcript archive at {} kept changing", live.display());
                }
                Ok(())
            }
        }
    }

    /// Every other top-level entry restore would carry over.
    fn copy_rest(&self, partial: &Path, live: &Live) -> Result<()> {
        for entry in fs::read_dir(self.data_dir)
            .with_context(|| format!("reading {}", self.data_dir.display()))?
        {
            let entry = entry?;
            let Some(name) = entry.file_name().to_str().map(str::to_owned) else {
                continue;
            };
            if left_behind(&name)
                || live.handled(&name)
                || name == MODEL_CACHE_DIR
                || name.contains(STALE_MARKER)
            {
                continue;
            }
            let (source, target) = (entry.path(), partial.join(&name));
            let kind = match entry.file_type() {
                Ok(kind) => kind,
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
                Err(e) => return Err(e).with_context(|| format!("reading {}", source.display())),
            };
            if kind.is_dir() {
                ensure!(
                    copy_dir_stable(&source, &target)? || !source.exists(),
                    "{} kept changing",
                    source.display()
                );
            } else {
                let copied = if kind.is_symlink() {
                    copy_symlink(&source, &target)
                } else {
                    copy_file_synced(&source, &target)
                };
                match copied {
                    Err(e) if is_not_found(&e) => {}
                    other => other?,
                }
            }
        }
        Ok(())
    }
}

/// The drain thread held between `start` and drop.
struct Paused(Sender<PauseCommand>);

impl Paused {
    fn start(drain: &Sender<DrainRequest>) -> Result<Self> {
        let (commands, held) = channel();
        drain
            .send(DrainRequest::Pause(held))
            .map_err(|_| anyhow!("the index drain thread is not running"))?;
        let paused = Self(commands);
        paused.settle()?;
        Ok(paused)
    }

    fn settle(&self) -> Result<()> {
        let (reply, done) = channel();
        self.0
            .send(PauseCommand::Settle(reply))
            .map_err(|_| anyhow!("the index drain thread stopped"))?;
        done.recv()
            .map_err(|_| anyhow!("the index drain thread stopped"))?
    }
}

/// No other backup runs while the maintenance mutex is held, so any
/// partial copy beside `dest` is from one that was killed.
fn remove_stale_partials(parent: &Path, name: &str) {
    let prefix = format!(".{name}.partial-");
    let entries = match fs::read_dir(parent) {
        Ok(entries) => entries,
        Err(e) => {
            tracing::warn!(
                "cannot look for partial backups in {}: {e}",
                parent.display()
            );
            return;
        }
    };
    for entry in entries {
        let entry = match entry {
            Ok(entry) => entry,
            Err(e) => {
                tracing::warn!(
                    "cannot look for partial backups in {}: {e}",
                    parent.display()
                );
                continue;
            }
        };
        if entry.file_name().to_string_lossy().starts_with(&prefix)
            && let Err(e) = fs::remove_dir_all(entry.path())
        {
            tracing::warn!(
                "cannot remove the partial backup {}: {e}",
                entry.path().display()
            );
        }
    }
}

fn tree_bytes(dir: &Path) -> Result<u64> {
    let mut total = 0;
    for entry in fs::read_dir(dir).with_context(|| format!("reading {}", dir.display()))? {
        let entry = entry?;
        let kind = entry.file_type()?;
        if kind.is_dir() {
            total += tree_bytes(&entry.path())?;
        } else if kind.is_file() {
            total += entry.metadata()?.len();
        }
    }
    Ok(total)
}