second-brain-core 0.13.2

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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
use std::fmt;
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::{Result, ensure};
use rusqlite::{Connection, OptionalExtension, params};

/// 2026-01-01T00:00:00Z: incarnations count seconds from here.
pub const INCARNATION_EPOCH_SECS: i64 = 1_767_225_600;

/// The time at which a wall-clock incarnation would reach 2^31 - 2^24. No
/// clock reading, peer basis or cursor above it is accepted, so
/// `incarnation << 32 | n` always fits an i64, and the value stays below
/// 2^53 for JSON.
pub const HLC_CEILING_MICROS: i64 = (INCARNATION_EPOCH_SECS + INCARNATION_LIMIT) * 1_000_000;

/// Every origin_seq minted with an incarnation is at least this, and every
/// legacy one (a local_seq from before clock seqs) is below it.
pub const NEW_LAYOUT_MIN: i64 = 1 << 32;

const INCARNATION_LIMIT: i64 = (1 << 31) - (1 << 24);
const N_LIMIT: i64 = 1 << 32;

const LAST: &str = "clock_last";
const BASIS: &str = "clock_basis";
const INCARNATION: &str = "clock_incarnation";
const INCARNATION_N: &str = "clock_incarnation_n";
const HELD_ORIGIN_SEQ: &str = "clock_held_origin_seq";
const AUTHORED: &str = "clock_authored_last";

pub trait WallClock: Send + Sync + std::panic::RefUnwindSafe {
    /// Wall-clock reading in microseconds since the Unix epoch, or None while
    /// time is untrusted.
    fn now_micros(&self) -> Option<i64>;
}

/// Always trusted, so an embedder that never configures a clock is never
/// held.
pub struct SystemClock;

impl WallClock for SystemClock {
    fn now_micros(&self) -> Option<i64> {
        Some(match SystemTime::now().duration_since(UNIX_EPOCH) {
            Ok(since) => since.as_micros() as i64,
            Err(before) => -(before.duration().as_micros() as i64),
        })
    }
}

/// A write that would mint a seq while the store's `WallClock` returns
/// None fails with this error inside its `anyhow::Error`.
#[derive(Debug)]
pub struct ClockUntrusted;

impl fmt::Display for ClockUntrusted {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("wall clock not yet trusted: writes are held")
    }
}

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

/// A peer report `raise_clock_floors` refused because a value lies past the
/// clock ceiling, as opposed to a store failure.
#[derive(Debug)]
pub struct ClockFloorRejected(pub String);

impl fmt::Display for ClockFloorRejected {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

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

/// What a peer's handshake reports, each raising its own floor: the HLC
/// basis (µs), `last` (the local_seq space, from the peer's cursor on this
/// log) and the incarnation floor (from the highest origin_seq of this
/// store's own records the peer holds).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ClockFloors {
    pub basis_micros: Option<i64>,
    pub last_seq: Option<i64>,
    pub held_origin_seq: Option<i64>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
pub struct ClockState {
    pub last: i64,
    pub basis: i64,
    pub incarnation: i64,
    pub incarnation_n: i64,
    pub incarnation_floor: i64,
}

pub(super) fn is_new_layout(origin: &str, origin_seq: i64) -> bool {
    origin_seq >= NEW_LAYOUT_MIN || origin.starts_with("snap:") || origin.starts_with("replay:")
}

pub(super) fn wall_now(clock: &dyn WallClock) -> Result<i64> {
    match clock.now_micros() {
        Some(wall) => Ok(wall.min(HLC_CEILING_MICROS)),
        None => Err(ClockUntrusted.into()),
    }
}

pub(super) fn meta(conn: &Connection, key: &str) -> Result<Option<i64>> {
    let value: Option<String> = conn
        .query_row(
            "SELECT value FROM index_meta WHERE key = ?1",
            params![key],
            |r| r.get(0),
        )
        .optional()?;
    value
        .map(|v| {
            v.parse::<i64>()
                .map_err(|e| anyhow::anyhow!("index_meta {key} = {v:?}: {e}"))
        })
        .transpose()
}

pub(super) fn set_meta(conn: &Connection, key: &str, value: i64) -> Result<()> {
    conn.execute(
        "INSERT OR REPLACE INTO index_meta (key, value) VALUES (?1, ?2)",
        params![key, value.to_string()],
    )?;
    Ok(())
}

/// A store from before clock seqs has no `clock_last`; its log's top seq is
/// where it carries on.
fn last(conn: &Connection) -> Result<i64> {
    match meta(conn, LAST)? {
        Some(last) => Ok(last),
        None => Ok(conn.query_row(
            "SELECT COALESCE(MAX(local_seq), 0) FROM sync_log",
            [],
            |r| r.get(0),
        )?),
    }
}

pub(super) fn state(conn: &Connection) -> Result<ClockState> {
    Ok(ClockState {
        last: last(conn)?,
        basis: meta(conn, BASIS)?.unwrap_or(0),
        incarnation: meta(conn, INCARNATION)?.unwrap_or(0),
        incarnation_n: meta(conn, INCARNATION_N)?.unwrap_or(0),
        incarnation_floor: meta(conn, HELD_ORIGIN_SEQ)?.unwrap_or(0) >> 32,
    })
}

pub(super) fn mint_local_seq(conn: &Connection, wall: i64) -> Result<i64> {
    let basis = meta(conn, BASIS)?.unwrap_or(0);
    let seq = (last(conn)? + 1).max(wall).max(basis);
    set_meta(conn, LAST, seq)?;
    Ok(seq)
}

/// A local write's authored time: strictly monotone per store, never behind
/// the peer basis, so a device whose clock lags or steps back never writes
/// in the past.
pub(super) fn mint_authored(conn: &Connection, wall: i64) -> Result<i64> {
    let basis = meta(conn, BASIS)?.unwrap_or(0);
    let previous = meta(conn, AUTHORED)?.unwrap_or(0);
    let authored = (previous + 1).max(wall).max(basis);
    set_meta(conn, AUTHORED, authored)?;
    Ok(authored)
}

/// The receive half of authored time: a write that landed here, applied or
/// seeded, moves the next local write past it, so a local delete or update
/// never carries a stamp older than the state it replaces. Only the clock
/// ceiling bounds it, so a record authored by a clock running ahead carries
/// every later local write that far ahead too.
pub(super) fn observe_authored(conn: &Connection, micros: i64) -> Result<()> {
    let seen = micros.min(HLC_CEILING_MICROS);
    if seen > meta(conn, AUTHORED)?.unwrap_or(0) {
        set_meta(conn, AUTHORED, seen)?;
    }
    Ok(())
}

/// `drawn` is the incarnation this open drew. It is redrawn when the
/// persisted one differs (the transaction that drew it rolled back), when a
/// peer holds a key above the last one minted here (this store is a restored
/// copy), or when `n` runs out.
pub(super) fn mint_origin_seq(
    conn: &Connection,
    wall: i64,
    drawn: &Mutex<Option<i64>>,
) -> Result<i64> {
    let stored = meta(conn, INCARNATION)?.unwrap_or(0);
    let n = meta(conn, INCARNATION_N)?.unwrap_or(0);
    let held = meta(conn, HELD_ORIGIN_SEQ)?.unwrap_or(0);
    let mut drawn = drawn.lock().unwrap();
    let keeps = *drawn == Some(stored) && held <= stored << 32 | n && n + 1 < N_LIMIT;
    let (incarnation, n) = if keeps {
        (stored, n + 1)
    } else {
        let basis = meta(conn, BASIS)?.unwrap_or(0);
        let from_clock = wall.max(basis) / 1_000_000 - INCARNATION_EPOCH_SECS;
        let incarnation = (stored + 1).max(from_clock).max((held >> 32) + 1).max(1);
        ensure!(
            incarnation < 1 << 31,
            "incarnation {incarnation} does not fit an origin_seq"
        );
        (incarnation, 1)
    };
    set_meta(conn, INCARNATION, incarnation)?;
    set_meta(conn, INCARNATION_N, n)?;
    *drawn = Some(incarnation);
    Ok(incarnation << 32 | n)
}

pub(super) fn raise_floors(conn: &Connection, floors: &ClockFloors) -> Result<()> {
    for (what, value) in [
        ("peer time", floors.basis_micros),
        ("peer cursor", floors.last_seq),
    ] {
        if let Some(v) = value
            && v > HLC_CEILING_MICROS
        {
            return Err(ClockFloorRejected(format!(
                "{what} {v} is past the clock ceiling {HLC_CEILING_MICROS}"
            ))
            .into());
        }
    }
    if let Some(held) = floors.held_origin_seq
        && held >> 32 >= INCARNATION_LIMIT
    {
        return Err(ClockFloorRejected(format!(
            "held origin_seq {held} has an incarnation past the clock ceiling"
        ))
        .into());
    }
    if let Some(basis) = floors.basis_micros
        && basis > meta(conn, BASIS)?.unwrap_or(0)
    {
        set_meta(conn, BASIS, basis)?;
    }
    if let Some(cursor) = floors.last_seq
        && cursor > last(conn)?
    {
        set_meta(conn, LAST, cursor)?;
    }
    if let Some(held) = floors.held_origin_seq
        && held > meta(conn, HELD_ORIGIN_SEQ)?.unwrap_or(0)
    {
        set_meta(conn, HELD_ORIGIN_SEQ, held)?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, AtomicI64, Ordering};

    use super::{
        ClockFloors, ClockUntrusted, HLC_CEILING_MICROS, INCARNATION_EPOCH_SECS, INCARNATION_LIMIT,
        WallClock, set_meta,
    };
    use crate::schema::{Memory, MemoryType, SyncNodeType, SyncOp, SyncRecord};
    use crate::sqlite_store::SqliteStore;
    use crate::store::{Store, StoreAdmin};

    const SECOND: i64 = 1_000_000;
    const T0: i64 = (INCARNATION_EPOCH_SECS + 20_000_000) * SECOND;
    const T0_INCARNATION: i64 = 20_000_000;

    struct TestClock {
        micros: AtomicI64,
        trusted: AtomicBool,
    }

    impl WallClock for TestClock {
        fn now_micros(&self) -> Option<i64> {
            self.trusted
                .load(Ordering::SeqCst)
                .then(|| self.micros.load(Ordering::SeqCst))
        }
    }

    fn store_at(micros: i64) -> (SqliteStore, Arc<TestClock>) {
        let clock = Arc::new(TestClock {
            micros: AtomicI64::new(micros),
            trusted: AtomicBool::new(true),
        });
        let store = SqliteStore::in_memory("m".to_string())
            .unwrap()
            .with_wall_clock(clock.clone());
        (store, clock)
    }

    fn write(store: &SqliteStore) -> (i64, i64) {
        store
            .store_memory(&Memory::new(
                "clock test".to_string(),
                MemoryType::Semantic,
                "test".to_string(),
                String::new(),
            ))
            .unwrap();
        let entries = store.sync_log_since(0).unwrap();
        let last = entries.last().unwrap();
        (last.local_seq, last.origin_seq)
    }

    fn split(origin_seq: i64) -> (i64, i64) {
        (origin_seq >> 32, origin_seq & 0xffff_ffff)
    }

    fn reopen(store: &SqliteStore) {
        *store.incarnation.lock().unwrap() = None;
    }

    #[test]
    fn local_seq_is_the_highest_of_last_plus_one_wall_and_basis() {
        let (store, clock) = store_at(T0);
        assert_eq!(write(&store).0, T0);
        assert_eq!(write(&store).0, T0 + 1);
        clock.micros.store(T0 - 3600 * SECOND, Ordering::SeqCst);
        assert_eq!(write(&store).0, T0 + 2, "a clock stepped back carries on");
        store
            .raise_clock_floors(&ClockFloors {
                basis_micros: Some(T0 + 60 * SECOND),
                ..Default::default()
            })
            .unwrap();
        assert_eq!(write(&store).0, T0 + 60 * SECOND);
        clock
            .micros
            .store(HLC_CEILING_MICROS + 3600 * SECOND, Ordering::SeqCst);
        assert_eq!(write(&store).0, HLC_CEILING_MICROS, "the clock is capped");
    }

    fn authored(store: &SqliteStore) -> i64 {
        write(store);
        let entries = store.sync_log_since(0).unwrap();
        entries.last().unwrap().timestamp.timestamp_micros()
    }

    #[test]
    fn authored_time_is_the_highest_of_previous_plus_one_wall_and_basis() {
        let (store, clock) = store_at(T0);
        assert_eq!(authored(&store), T0);
        assert_eq!(
            authored(&store),
            T0 + 1,
            "writes in one microsecond never tie"
        );
        clock.micros.store(T0 - 3600 * SECOND, Ordering::SeqCst);
        assert_eq!(
            authored(&store),
            T0 + 2,
            "a clock stepped back never writes in the past"
        );
        store
            .raise_clock_floors(&ClockFloors {
                basis_micros: Some(T0 + 60 * SECOND),
                ..Default::default()
            })
            .unwrap();
        assert_eq!(authored(&store), T0 + 60 * SECOND);
        clock.micros.store(T0 + 120 * SECOND, Ordering::SeqCst);
        assert_eq!(authored(&store), T0 + 120 * SECOND);
    }

    #[test]
    fn a_relay_keeps_the_incoming_authored_time() {
        let (store, _) = store_at(T0);
        let memory = Memory::new(
            "relayed".to_string(),
            MemoryType::Semantic,
            "test".to_string(),
            String::new(),
        );
        let authored = chrono::DateTime::from_timestamp_micros(T0 - 7 * SECOND).unwrap();
        let record = SyncRecord {
            local_seq: 1,
            origin_machine_id: "peer".to_string(),
            origin_seq: (13u64 << 32) | 1,
            op: SyncOp::Create,
            node_type: SyncNodeType::Memory,
            node_id: memory.id.to_string(),
            timestamp: authored,
            data: Some(serde_json::to_value(&memory).unwrap()),
        };
        store.apply_record(&record).unwrap();
        let entries = store.sync_log_since(0).unwrap();
        assert_eq!(entries.last().unwrap().timestamp, authored);
    }

    #[test]
    fn an_open_draws_one_incarnation_and_counts_within_it() {
        let (store, _) = store_at(T0);
        assert_eq!(split(write(&store).1), (T0_INCARNATION, 1));
        assert_eq!(split(write(&store).1), (T0_INCARNATION, 2));
        reopen(&store);
        assert_eq!(
            split(write(&store).1),
            (T0_INCARNATION + 1, 1),
            "a new open in the same second still draws above the last"
        );
        let state = store.clock_state().unwrap();
        assert_eq!(
            (state.incarnation, state.incarnation_n),
            (T0_INCARNATION + 1, 1)
        );
    }

    #[test]
    fn a_clock_before_the_epoch_still_draws_incarnation_one() {
        let (store, _) = store_at(0);
        assert_eq!(split(write(&store).1), (1, 1));
    }

    #[test]
    fn a_rolled_back_draw_is_redrawn_and_n_restarts() {
        let (store, _) = store_at(T0);
        assert_eq!(split(write(&store).1), (T0_INCARNATION, 1));
        reopen(&store);
        let failed: anyhow::Result<()> = store.with_txn(|tx| {
            store.append_sync_log(tx, SyncOp::Delete, SyncNodeType::Memory, "x", None, None)?;
            anyhow::bail!("roll back")
        });
        assert!(failed.is_err());
        assert_eq!(store.clock_state().unwrap().incarnation, T0_INCARNATION);
        assert_eq!(split(write(&store).1), (T0_INCARNATION + 1, 1));
    }

    #[test]
    fn a_raised_floor_forces_a_draw_above_it() {
        let (store, _) = store_at(T0);
        write(&store);
        let held = (T0_INCARNATION + 5) << 32 | 9;
        store
            .raise_clock_floors(&ClockFloors {
                held_origin_seq: Some(held),
                ..Default::default()
            })
            .unwrap();
        assert_eq!(split(write(&store).1), (T0_INCARNATION + 6, 1));
        assert_eq!(split(write(&store).1), (T0_INCARNATION + 6, 2));
    }

    #[test]
    fn a_peer_holding_this_stores_current_key_keeps_its_incarnation() {
        let (store, _) = store_at(T0);
        write(&store);
        let (_, own) = write(&store);
        store
            .raise_clock_floors(&ClockFloors {
                held_origin_seq: Some(own),
                ..Default::default()
            })
            .unwrap();
        assert_eq!(split(write(&store).1), (T0_INCARNATION, 3));
        assert_eq!(
            store.clock_state().unwrap().incarnation_floor,
            T0_INCARNATION
        );
    }

    #[test]
    fn running_out_of_n_starts_a_new_incarnation() {
        let (store, _) = store_at(T0);
        write(&store);
        set_meta(&store.conn(), super::INCARNATION_N, (1 << 32) - 2).unwrap();
        assert_eq!(split(write(&store).1), (T0_INCARNATION, (1 << 32) - 1));
        assert_eq!(split(write(&store).1), (T0_INCARNATION + 1, 1));
    }

    #[test]
    fn floors_only_rise() {
        let (store, _) = store_at(T0);
        write(&store);
        let raise = |basis, last, held| {
            store
                .raise_clock_floors(&ClockFloors {
                    basis_micros: Some(basis),
                    last_seq: Some(last),
                    held_origin_seq: Some(held),
                })
                .unwrap()
        };
        raise(T0 + 10, T0 + 20, 30 << 32);
        raise(T0 - 10, T0 - 20, 3 << 32);
        let state = store.clock_state().unwrap();
        assert_eq!(
            (state.basis, state.last, state.incarnation_floor),
            (T0 + 10, T0 + 20, 30)
        );
    }

    #[test]
    fn floors_past_the_ceiling_are_rejected_and_raise_nothing() {
        let (store, _) = store_at(T0);
        write(&store);
        let before = store.clock_state().unwrap();
        for floors in [
            ClockFloors {
                basis_micros: Some(HLC_CEILING_MICROS + 1),
                last_seq: Some(T0 + 100),
                held_origin_seq: None,
            },
            ClockFloors {
                basis_micros: Some(T0 + 100),
                last_seq: Some(HLC_CEILING_MICROS + 1),
                held_origin_seq: None,
            },
            ClockFloors {
                basis_micros: Some(T0 + 100),
                last_seq: None,
                held_origin_seq: Some(INCARNATION_LIMIT << 32),
            },
        ] {
            assert!(store.raise_clock_floors(&floors).is_err(), "{floors:?}");
            assert_eq!(store.clock_state().unwrap(), before, "{floors:?}");
        }
        store
            .raise_clock_floors(&ClockFloors {
                basis_micros: Some(HLC_CEILING_MICROS),
                last_seq: Some(HLC_CEILING_MICROS),
                held_origin_seq: Some((INCARNATION_LIMIT - 1) << 32 | 7),
            })
            .unwrap();
    }

    #[test]
    fn an_untrusted_clock_fails_own_writes_and_relays_without_writing() {
        let (store, clock) = store_at(T0);
        clock.trusted.store(false, Ordering::SeqCst);
        let memory = Memory::new(
            "held".to_string(),
            MemoryType::Semantic,
            "test".to_string(),
            String::new(),
        );
        let err = store.store_memory(&memory).unwrap_err();
        assert!(err.downcast_ref::<ClockUntrusted>().is_some(), "{err:#}");
        let relayed = SyncRecord {
            local_seq: 1,
            origin_machine_id: "peer".to_string(),
            origin_seq: 1,
            op: SyncOp::Create,
            node_type: SyncNodeType::Memory,
            node_id: memory.id.to_string(),
            timestamp: chrono::Utc::now(),
            data: Some(serde_json::to_value(&memory).unwrap()),
        };
        let err = store.apply_record(&relayed).unwrap_err();
        assert!(err.downcast_ref::<ClockUntrusted>().is_some(), "{err:#}");
        assert_eq!(store.memory_count().unwrap(), 0);
        assert!(store.sync_log_since(0).unwrap().is_empty());
        assert_eq!(store.clock_state().unwrap().last, 0);

        clock.trusted.store(true, Ordering::SeqCst);
        store.store_memory(&memory).unwrap();
        assert_eq!(store.memory_count().unwrap(), 1);
    }
}