second-brain-core 0.13.5

Core library for second-brain: SQLite storage with vector and lexical indexes, BGE embeddings, and hybrid retrieval
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! Writes held on disk for the daemon. An entry is either sealed to the
//! device's `device.pub`, keeping only its [`Label`] readable, or plaintext,
//! as older binaries and a writer without the recipient write it.

use std::io::Write;
use std::path::Path;

use age::x25519::{Identity, Recipient};
use anyhow::{Context, bail};
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// The directory under the data dir where writes wait for the daemon; the
/// MCP server, the daemon's write gate and its replayer all use it.
pub const DIR_NAME: &str = "queue";

const PLAIN_SUFFIX: &str = ".json";
/// Older binaries' entry rule skips this suffix, so after a downgrade a
/// sealed entry waits for a daemon that can open it instead of replaying
/// with no body.
const SEALED_SUFFIX: &str = ".sealed";

/// `<19-digit unix nanos>-<uuid>.json`, or `.sealed`; no other name in the
/// directory is an entry, so a file still being written under a temporary
/// name is never read.
pub fn is_entry_name(name: &str) -> bool {
    let Some(stem) = name
        .strip_suffix(PLAIN_SUFFIX)
        .or_else(|| name.strip_suffix(SEALED_SUFFIX))
    else {
        return false;
    };
    let Some((nanos, id)) = stem.split_at_checked(19) else {
        return false;
    };
    nanos.bytes().all(|b| b.is_ascii_digit())
        && id
            .strip_prefix('-')
            .is_some_and(|id| id.len() == 36 && uuid::Uuid::parse_str(id).is_ok())
}

pub fn is_sealed_name(name: &str) -> bool {
    name.ends_with(SEALED_SUFFIX) && is_entry_name(name)
}

/// A fresh entry name for a write made at `nanos` since the epoch.
pub fn entry_name(nanos: u128, sealed: bool) -> String {
    let suffix = if sealed { SEALED_SUFFIX } else { PLAIN_SUFFIX };
    format!("{nanos:019}-{}{suffix}", uuid::Uuid::new_v4())
}

/// One queued write, as it is sent to the daemon.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Entry {
    pub tool: String,
    pub method: String,
    pub path: String,
    /// Already URL-encoded, without the leading `?`.
    #[serde(default)]
    pub query: Option<String>,
    #[serde(default)]
    pub body: Option<Value>,
}

/// What a sealed entry keeps readable without the device key.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct Label {
    pub tool: String,
    pub method: String,
    pub path: String,
    /// An ingest's source, so a writer can collapse ingests per source.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
    /// A note's session and kind, so the stop hook can find a queued
    /// session-end note.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub kind: Option<String>,
}

#[derive(Serialize, Deserialize)]
struct SealedFile {
    #[serde(flatten)]
    label: Label,
    sealed: String,
}

#[derive(Deserialize)]
struct StoredFile {
    #[serde(flatten)]
    label: Label,
    #[serde(default)]
    sealed: Option<String>,
    #[serde(default)]
    query: Option<String>,
    #[serde(default)]
    body: Option<Value>,
}

#[derive(Debug)]
pub enum OpenError {
    /// A sealed entry, and no device identity to open it with.
    NoKey,
    Unreadable(anyhow::Error),
}

impl std::fmt::Display for OpenError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NoKey => f.write_str("the entry is sealed and the device key is unavailable"),
            Self::Unreadable(e) => write!(f, "{e:#}"),
        }
    }
}

impl std::error::Error for OpenError {}

impl Entry {
    pub fn label(&self) -> Label {
        let field = |name: &str| {
            self.body
                .as_ref()
                .and_then(|b| b.get(name))
                .and_then(Value::as_str)
                .map(str::to_string)
        };
        let (source, session_id, kind) = match self.tool.as_str() {
            "ingest" => (field("source"), None, None),
            "note" => (None, field("session_id"), field("kind")),
            _ => (None, None, None),
        };
        Label {
            tool: self.tool.clone(),
            method: self.method.clone(),
            path: self.path.clone(),
            source,
            session_id,
            kind,
        }
    }

    pub fn to_plain_file(&self) -> anyhow::Result<Vec<u8>> {
        Ok(serde_json::to_vec(self)?)
    }

    /// The whole entry is sealed, to every recipient; the label beside it is
    /// only an index, and [`open`] takes nothing from it.
    pub fn to_sealed_file(&self, recipients: &[Recipient]) -> anyhow::Result<Vec<u8>> {
        if recipients.is_empty() {
            bail!("sealing a queued write needs a recipient");
        }
        let encryptor =
            age::Encryptor::with_recipients(recipients.iter().map(|r| r as &dyn age::Recipient))
                .context("sealing a queued write")?;
        let mut ciphertext = Vec::new();
        let mut writer = encryptor.wrap_output(&mut ciphertext)?;
        writer.write_all(&serde_json::to_vec(self)?)?;
        writer.finish()?;
        Ok(serde_json::to_vec(&SealedFile {
            label: self.label(),
            sealed: STANDARD.encode(ciphertext),
        })?)
    }
}

/// Reads an entry file of either kind; a sealed one needs `identity`.
pub fn open(bytes: &[u8], identity: Option<&Identity>) -> Result<Entry, OpenError> {
    let stored: StoredFile =
        serde_json::from_slice(bytes).map_err(|e| OpenError::Unreadable(e.into()))?;
    let Some(sealed) = stored.sealed else {
        return Ok(Entry {
            tool: stored.label.tool,
            method: stored.label.method,
            path: stored.label.path,
            query: stored.query,
            body: stored.body,
        });
    };
    let identity = identity.ok_or(OpenError::NoKey)?;
    let opened = STANDARD
        .decode(sealed)
        .context("decoding a sealed queued write")
        .and_then(|ciphertext| {
            age::decrypt(identity, &ciphertext).context("opening a sealed queued write")
        })
        .and_then(|plain| Ok(serde_json::from_slice(&plain)?))
        .map_err(OpenError::Unreadable)?;
    Ok(opened)
}

/// Seals again, to `recipients`, every sealed entry in `dir` that `old`
/// opens, so writes queued under a device key outlive it; entries `old`
/// can't open, or that can't be read, are left as they are. Failing to
/// write a resealed entry is an error, since the caller may be about to
/// drop `old`. Returns how many it resealed.
pub fn reseal_dir(dir: &Path, old: &Identity, recipients: &[Recipient]) -> anyhow::Result<usize> {
    let items = match std::fs::read_dir(dir) {
        Ok(items) => items,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(0),
        Err(e) => return Err(e).with_context(|| format!("listing {}", dir.display())),
    };
    let mut resealed = 0;
    for item in items {
        let name = item?.file_name().to_string_lossy().into_owned();
        if !is_sealed_name(&name) {
            continue;
        }
        let path = dir.join(&name);
        let bytes = match std::fs::read(&path) {
            Ok(bytes) => bytes,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
            Err(e) => {
                tracing::warn!("cannot reseal {}: {e}", path.display());
                continue;
            }
        };
        let Ok(entry) = open(&bytes, Some(old)) else {
            continue;
        };
        replace_file(dir, &name, &entry.to_sealed_file(recipients)?)?;
        resealed += 1;
    }
    if resealed > 0 {
        std::fs::File::open(dir)
            .and_then(|d| d.sync_all())
            .with_context(|| format!("syncing {}", dir.display()))?;
    }
    Ok(resealed)
}

fn replace_file(dir: &Path, name: &str, bytes: &[u8]) -> anyhow::Result<()> {
    let tmp = dir.join(format!("{name}.{}.tmp", uuid::Uuid::new_v4().simple()));
    let mut options = std::fs::OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
    let written = options
        .open(&tmp)
        .and_then(|mut file| {
            file.write_all(bytes)?;
            file.sync_all()
        })
        .and_then(|()| std::fs::rename(&tmp, dir.join(name)));
    if let Err(e) = written {
        if let Err(cleanup) = std::fs::remove_file(&tmp)
            && cleanup.kind() != std::io::ErrorKind::NotFound
        {
            tracing::warn!("cannot remove {}: {cleanup}", tmp.display());
        }
        return Err(e).with_context(|| format!("resealing {}", dir.join(name).display()));
    }
    Ok(())
}

/// The label of an entry file of either kind, read without any key.
pub fn read_label(bytes: &[u8]) -> anyhow::Result<Label> {
    let stored: StoredFile = serde_json::from_slice(bytes)?;
    if stored.sealed.is_some() {
        return Ok(stored.label);
    }
    Ok(Entry {
        tool: stored.label.tool,
        method: stored.label.method,
        path: stored.label.path,
        query: stored.query,
        body: stored.body,
    }
    .label())
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    fn note() -> Entry {
        Entry {
            tool: "note".to_string(),
            method: "POST".to_string(),
            path: "/v1/notes".to_string(),
            query: None,
            body: Some(json!({
                "session_id": "s-1",
                "kind": "session_end",
                "content": "the plan for the migration",
            })),
        }
    }

    fn forget() -> Entry {
        Entry {
            tool: "forget".to_string(),
            method: "DELETE".to_string(),
            path: "/v1/memory/m-1".to_string(),
            query: Some("hard=true&reason=leaked+the+password".to_string()),
            body: None,
        }
    }

    #[test]
    fn only_timestamped_uuid_entry_names_count() {
        let id = "0b5c2f9e-5d0e-4c1a-9a57-0c7f4f1f2a10";
        assert!(is_entry_name(&format!("1793505622000000000-{id}.json")));
        assert!(is_entry_name(&format!("1793505622000000000-{id}.sealed")));
        for name in [
            "projects.json".to_string(),
            ".lock".to_string(),
            format!("1793505622000000000-{id}.json.tmp"),
            format!("1793505622000000000-{id}.sealed.tmp"),
            format!("179350562200000000-{id}.json"),
            format!("1793505622000000000-{id}.unreadable"),
            format!("1793505622000000000-{id}.sealed.json"),
            "1793505622000000000-not-a-uuid.json".to_string(),
        ] {
            assert!(!is_entry_name(&name), "{name}");
        }
        assert!(is_entry_name(&entry_name(7, true)));
        assert!(is_sealed_name(&entry_name(7, true)));
        assert!(!is_sealed_name(&entry_name(7, false)));
        assert!(entry_name(7, true).ends_with(".sealed"));
        assert!(entry_name(7, false).ends_with(".json"));
        assert!(entry_name(7, false).starts_with("0000000000000000007-"));
    }

    #[test]
    fn a_sealed_entry_shows_only_its_label_and_opens_with_the_device_key() {
        let identity = Identity::generate();
        for entry in [note(), forget()] {
            let file = entry.to_sealed_file(&[identity.to_public()]).unwrap();
            let text = String::from_utf8_lossy(&file);
            for secret in ["migration", "password", "reason", "content"] {
                assert!(!text.contains(secret), "{secret} in {text}");
            }
            assert_eq!(read_label(&file).unwrap(), entry.label());
            assert_eq!(open(&file, Some(&identity)).unwrap(), entry);
            assert!(matches!(open(&file, None), Err(OpenError::NoKey)));
            assert!(matches!(
                open(&file, Some(&Identity::generate())),
                Err(OpenError::Unreadable(_))
            ));
        }
    }

    #[test]
    fn a_plaintext_entry_from_an_older_binary_opens_without_a_key() {
        let older = br#"{"tool":"note","method":"POST","path":"/v1/notes","query":null,"body":{"session_id":"s-1","kind":"session_end","content":"x"}}"#;
        assert_eq!(
            open(older, None).unwrap().body,
            note().body.map(|mut b| {
                b["content"] = json!("x");
                b
            })
        );
        let label = read_label(older).unwrap();
        assert_eq!(label.session_id.as_deref(), Some("s-1"));
        assert_eq!(label.kind.as_deref(), Some("session_end"));
        let entry = forget();
        assert_eq!(open(&entry.to_plain_file().unwrap(), None).unwrap(), entry);
    }

    #[test]
    fn labels_carry_an_ingests_source_and_a_notes_session_only() {
        let ingest = Entry {
            tool: "ingest".to_string(),
            method: "POST".to_string(),
            path: "/v1/ingest".to_string(),
            query: None,
            body: Some(json!({ "source": "cursor", "session_id": "s-1" })),
        };
        let label = ingest.label();
        assert_eq!(label.source.as_deref(), Some("cursor"));
        assert_eq!(label.session_id, None);
        let note = note().label();
        assert_eq!(
            (
                note.source,
                note.session_id.as_deref(),
                note.kind.as_deref()
            ),
            (None, Some("s-1"), Some("session_end"))
        );
        let remember = Entry {
            tool: "remember".to_string(),
            body: Some(json!({ "session_id": "s-1", "source": "x" })),
            ..forget()
        };
        assert_eq!(
            remember.label(),
            Label {
                tool: "remember".to_string(),
                method: "DELETE".to_string(),
                path: "/v1/memory/m-1".to_string(),
                ..Label::default()
            }
        );
    }

    #[test]
    fn a_sealed_entry_opens_with_any_of_its_recipients() {
        let (a, b) = (Identity::generate(), Identity::generate());
        let file = forget()
            .to_sealed_file(&[a.to_public(), b.to_public()])
            .unwrap();
        assert_eq!(open(&file, Some(&a)).unwrap(), forget());
        assert_eq!(open(&file, Some(&b)).unwrap(), forget());
        assert!(forget().to_sealed_file(&[]).is_err());
    }

    #[test]
    fn resealing_moves_what_the_old_key_opens_to_the_new_recipients() {
        let dir = tempfile::tempdir().unwrap();
        let (old, fresh, stranger) = (
            Identity::generate(),
            Identity::generate(),
            Identity::generate(),
        );
        let ours = entry_name(1, true);
        let theirs = entry_name(2, true);
        let plain = entry_name(3, false);
        let write =
            |name: &str, bytes: Vec<u8>| std::fs::write(dir.path().join(name), bytes).unwrap();
        write(&ours, note().to_sealed_file(&[old.to_public()]).unwrap());
        write(
            &theirs,
            forget().to_sealed_file(&[stranger.to_public()]).unwrap(),
        );
        write(&plain, forget().to_plain_file().unwrap());

        let resealed = reseal_dir(dir.path(), &old, &[fresh.to_public()]).unwrap();
        assert_eq!(resealed, 1);
        let read = |name: &str| std::fs::read(dir.path().join(name)).unwrap();
        assert_eq!(open(&read(&ours), Some(&fresh)).unwrap(), note());
        assert!(open(&read(&ours), Some(&old)).is_err());
        assert_eq!(open(&read(&theirs), Some(&stranger)).unwrap(), forget());
        assert_eq!(open(&read(&plain), None).unwrap(), forget());
        let names: Vec<_> = std::fs::read_dir(dir.path())
            .unwrap()
            .flatten()
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .collect();
        assert_eq!(names.len(), 3, "{names:?}");
        assert_eq!(
            reseal_dir(&dir.path().join("missing"), &old, &[fresh.to_public()]).unwrap(),
            0
        );
    }

    #[test]
    fn a_reseal_that_cannot_write_fails_and_leaves_the_entry() {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir().unwrap();
        let (old, fresh) = (Identity::generate(), Identity::generate());
        let name = entry_name(1, true);
        let sealed = note().to_sealed_file(&[old.to_public()]).unwrap();
        std::fs::write(dir.path().join(&name), &sealed).unwrap();
        let read_only = std::fs::Permissions::from_mode(0o500);
        std::fs::set_permissions(dir.path(), read_only).unwrap();
        let result = reseal_dir(dir.path(), &old, &[fresh.to_public()]);
        std::fs::set_permissions(dir.path(), std::fs::Permissions::from_mode(0o700)).unwrap();
        assert!(result.is_err());
        assert_eq!(std::fs::read(dir.path().join(&name)).unwrap(), sealed);
    }

    #[test]
    fn a_corrupt_entry_is_unreadable() {
        assert!(matches!(open(b"{", None), Err(OpenError::Unreadable(_))));
        let bad = br#"{"tool":"note","method":"POST","path":"/v1/notes","sealed":"!!"}"#;
        assert!(matches!(
            open(bad, Some(&Identity::generate())),
            Err(OpenError::Unreadable(_))
        ));
    }
}