grit-core 0.2.4

Embedded, bi-temporal property graph for agent memory: one SQLite file, in-process, deterministic
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
//! # grit-core
//!
//! An embedded, bi-temporal property graph for agent memory: one SQLite file,
//! in-process, deterministic. See `AGENTS.md` at the repo root for the full
//! design contract; the short version:
//!
//! - All writes are [`GraphOp`]s appended to an op-log; graph tables are
//!   derived state applied in the same transaction (sync-ready by design).
//! - Facts are never mutated in place. Edges carry two timelines — event time
//!   (`valid_at`/`invalid_at`) and system time (`created_at`/`expired_at`) —
//!   so "what did I believe in March?" is a query, not archaeology.
//! - No LLM, no network, no embedding computation. Time is injected via
//!   [`Clock`]; embeddings are stored, never created.
//!
//! # Example
//! ```
//! use grit_core::{Budget, GraphOp, Grit, Options, Query, Traversal};
//! # fn main() -> Result<(), grit_core::Error> {
//! # let dir = std::env::temp_dir().join(format!("grit-doc-{}", std::process::id()));
//! # std::fs::create_dir_all(&dir)?;
//! # let path = dir.join("memory.db");
//! # let _ = std::fs::remove_file(&path);
//! let g = Grit::open(&path, Options::new("device-a"))?;
//!
//! let yoneda = g.new_id();
//! let functor = g.new_id();
//! g.apply(GraphOp::AddNode {
//!     id: yoneda,
//!     kind: "concept".into(),
//!     name: "Yoneda lemma".into(),
//!     summary: "Objects are determined by their relationships".into(),
//!     attrs: serde_json::json!({}),
//!     group_id: "category-theory".into(),
//! })?;
//! g.apply(GraphOp::AddNode {
//!     id: functor,
//!     kind: "concept".into(),
//!     name: "Functor".into(),
//!     summary: "Structure-preserving map between categories".into(),
//!     attrs: serde_json::json!({}),
//!     group_id: "category-theory".into(),
//! })?;
//! g.apply(GraphOp::AddEdge {
//!     id: g.new_id(),
//!     src: yoneda,
//!     dst: functor,
//!     rel: "STATED_IN_TERMS_OF".into(),
//!     fact: "The Yoneda lemma is stated in terms of hom-functors".into(),
//!     attrs: serde_json::json!({}),
//!     group_id: "category-theory".into(),
//!     valid_at: None,
//!     invalid_at: None,
//! })?;
//!
//! let hits = g.search(
//!     Query::text("yoneda").group("category-theory").budget(Budget::items(10)),
//! )?;
//! assert!(!hits.is_empty());
//!
//! let ctx = g.traverse(&[yoneda], &Traversal::default())?;
//! assert_eq!(ctx.nodes.len(), 2);
//! # Ok(()) }
//! ```
#![deny(unsafe_code)]
#![warn(missing_docs)]

mod clock;
mod error;
mod export;
mod hlc;
mod migrate;
mod model;
mod ops;
mod query;
mod search;
mod vecext;
mod writer;

use std::path::Path;
use std::sync::mpsc::{Receiver, Sender, channel};
use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;

use rusqlite::{Connection, OpenFlags};
use uuid::Uuid;

pub use crate::clock::{Clock, ManualClock, SystemClock, TimestampMs};
pub use crate::error::{Error, Result};
pub use crate::export::{ImportStats, import_jsonl};
pub use crate::hlc::{Hlc, HlcGenerator};
pub use crate::migrate::SCHEMA_VERSION;
pub use crate::model::{Edge, Episode, Node, Subgraph};
pub use crate::ops::{GraphOp, OplogEntry};
#[doc(hidden)]
pub use crate::query::{EXPAND_SQL_AS_AT, EXPAND_SQL_CURRENT};
pub use crate::query::{MergeCandidate, NodeHistory, Stats, Traversal};
pub use crate::search::{Budget, Query, SearchHit, SearchKind, SearchTarget};

use crate::writer::{VecTable, WriteMsg};

/// Open-time configuration for [`Grit`].
pub struct Options {
    /// Stable identifier for this device/installation; stamped on every op
    /// and used as the HLC tie-breaker.
    pub device_id: String,
    /// Time source. Defaults to [`SystemClock`]; tests inject [`ManualClock`].
    pub clock: Arc<dyn Clock>,
}

impl Options {
    /// Options with the given device id and the real system clock.
    pub fn new(device_id: impl Into<String>) -> Self {
        Self {
            device_id: device_id.into(),
            clock: Arc::new(SystemClock),
        }
    }

    /// Replace the time source (Design Invariant 3: time is injected).
    pub fn clock(mut self, clock: Arc<dyn Clock>) -> Self {
        self.clock = clock;
        self
    }
}

/// Handle to one grit database: a writer actor plus a read connection.
///
/// All methods take `&self`; `Grit` is `Send + Sync`. Writes are serialized
/// through the single writer thread (SQLite has one writer — embrace it);
/// reads run on a separate WAL connection and never block behind writes.
pub struct Grit {
    write_tx: Sender<WriteMsg>,
    writer: Option<JoinHandle<()>>,
    pub(crate) read_conn: Mutex<Connection>,
    clock: Arc<dyn Clock>,
    hlc: HlcGenerator,
    device_id: String,
}

impl Grit {
    /// Open (creating or migrating as needed) the database at `path`.
    ///
    /// The file plus its WAL sidecars is the entire truth (Design Invariant 2).
    /// In-memory databases are not supported: grit uses two connections
    /// (writer + reader) that must see the same file.
    ///
    /// # Example
    /// ```no_run
    /// use grit_core::{Grit, Options};
    /// let g = Grit::open("memory.db", Options::new("laptop"))?;
    /// # Ok::<(), grit_core::Error>(())
    /// ```
    pub fn open(path: impl AsRef<Path>, opts: Options) -> Result<Self> {
        let path = path.as_ref();
        // Two connections must see the same on-disk file: reject every SQLite
        // spelling of "not a real file" (bare :memory:, memory-mode URIs, and
        // the empty path, which opens a distinct temp db per connection).
        if let Some(s) = path.to_str()
            && (s.is_empty()
                || s == ":memory:"
                || (s.starts_with("file:")
                    && (s.contains(":memory:") || s.contains("mode=memory"))))
        {
            return Err(Error::InvalidOp(
                "in-memory or empty database paths are not supported".into(),
            ));
        }
        vecext::register_sqlite_vec();

        let mut write_conn = Connection::open(path)?;
        configure(&write_conn)?;
        migrate::migrate(&mut write_conn)?;

        let read_conn = Connection::open_with_flags(
            path,
            OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
        )?;
        read_conn.busy_timeout(std::time::Duration::from_secs(5))?;
        configure_cache(&read_conn)?;

        let (write_tx, write_rx): (Sender<WriteMsg>, Receiver<WriteMsg>) = channel();
        let writer = writer::spawn(write_conn, Arc::clone(&opts.clock), write_rx);

        Ok(Self {
            write_tx,
            writer: Some(writer),
            read_conn: Mutex::new(read_conn),
            hlc: HlcGenerator::new(opts.device_id.clone()),
            clock: opts.clock,
            device_id: opts.device_id,
        })
    }

    /// Mint a UUIDv7 from the injected clock. Use for the `id` fields of
    /// [`GraphOp`]s.
    pub fn new_id(&self) -> Uuid {
        let ms = self.clock.now_ms().max(0) as u64;
        let ts = uuid::Timestamp::from_unix(
            uuid::NoContext,
            ms / 1000,
            ((ms % 1000) * 1_000_000) as u32,
        );
        Uuid::new_v7(ts)
    }

    /// Apply a local write: mint an [`OplogEntry`] (op id + HLC), append it to
    /// the oplog and update the graph tables in one transaction. Returns the
    /// entry — the unit a sync mechanism would ship to other devices.
    ///
    /// # Example
    /// ```no_run
    /// # use grit_core::{GraphOp, Grit, Options};
    /// # let g = Grit::open("memory.db", Options::new("laptop"))?;
    /// let entry = g.apply(GraphOp::AddNode {
    ///     id: g.new_id(),
    ///     kind: "person".into(),
    ///     name: "Ada Lovelace".into(),
    ///     summary: String::new(),
    ///     attrs: serde_json::json!({}),
    ///     group_id: String::new(),
    /// })?;
    /// assert_eq!(entry.device_id, "laptop");
    /// # Ok::<(), grit_core::Error>(())
    /// ```
    pub fn apply(&self, op: GraphOp) -> Result<OplogEntry> {
        validate(&op)?;
        let entry = OplogEntry {
            id: self.new_id(),
            hlc: self.hlc.next(self.clock.as_ref()),
            device_id: self.device_id.clone(),
            op,
        };
        writer::call(&self.write_tx, |reply| WriteMsg::Apply {
            entry: entry.clone(),
            reply,
        })?;
        Ok(entry)
    }

    /// Apply an op that originated on another device (future sync ingest, and
    /// the test seam for the oplog merge laws). Idempotent: returns `false`
    /// if this op id was already applied. Folds the remote HLC into the local
    /// generator so subsequent local ops sort after everything observed.
    ///
    /// Unlike [`Grit::apply`], structurally invalid ops (e.g. a self-merge)
    /// are recorded and applied as no-ops rather than erroring — one
    /// malformed entry from a buggy device must not wedge a sync loop. The
    /// one exception is a negative HLC wall time, which is rejected: it
    /// cannot be ordered, so nothing downstream of it can converge.
    pub fn apply_remote(&self, entry: OplogEntry) -> Result<bool> {
        if entry.hlc.wall_ms < 0 {
            return Err(Error::InvalidHlc(entry.hlc.encode()));
        }
        self.hlc.observe(&entry.hlc);
        writer::call(&self.write_tx, |reply| WriteMsg::Apply { entry, reply })
    }

    /// Read the oplog from local sequence `after_seq` (exclusive; 0 = start).
    /// Returns `(seq, entry)` pairs in local apply order.
    pub fn oplog_since(&self, after_seq: i64) -> Result<Vec<(i64, OplogEntry)>> {
        let conn = self.read();
        let mut stmt = conn.prepare_cached(
            "SELECT seq, id, hlc, device_id, op FROM oplog WHERE seq > ?1 ORDER BY seq",
        )?;
        let rows = stmt.query_map([after_seq], |row| {
            Ok((
                row.get::<_, i64>(0)?,
                row.get::<_, String>(1)?,
                row.get::<_, String>(2)?,
                row.get::<_, String>(3)?,
                row.get::<_, String>(4)?,
            ))
        })?;
        let mut out = Vec::new();
        for row in rows {
            let (seq, id, hlc, device_id, op) = row?;
            out.push((
                seq,
                OplogEntry {
                    id: Uuid::parse_str(&id)
                        .map_err(|_| Error::Corrupt(format!("bad uuid in oplog: {id}")))?,
                    hlc: hlc.parse()?,
                    device_id,
                    op: serde_json::from_str(&op)?,
                },
            ));
        }
        Ok(out)
    }

    /// Register the embedding model whose vectors this file stores. Creates
    /// the `vec_nodes`/`vec_edges` tables sized to `dim` if they are missing
    /// (also after an import — vectors are never exported, the tables are
    /// rebuilt empty and rows re-embed). Vectors are partitioned by their
    /// row's `group_id`, so vector search filters by group inside the KNN
    /// scan. Embeddings are recomputable local state — not part of the oplog
    /// (Design Invariant 5).
    pub fn register_embedding_model(
        &self,
        model_id: impl Into<String>,
        dim: usize,
        model_version: impl Into<String>,
    ) -> Result<()> {
        writer::call(&self.write_tx, |reply| WriteMsg::RegisterModel {
            model_id: model_id.into(),
            dim,
            version: model_version.into(),
            reply,
        })
    }

    /// Store (or replace) the embedding for a node. The caller computed it;
    /// grit only stores it, in the node's group partition. The node row must
    /// already exist ([`Error::NotFound`] otherwise) — apply the
    /// [`GraphOp::AddNode`] first, then embed. Embedding a purged id is a
    /// silent no-op (right to forget).
    pub fn set_node_embedding(&self, node_id: Uuid, vector: Vec<f32>) -> Result<()> {
        writer::call(&self.write_tx, |reply| WriteMsg::SetEmbedding {
            table: VecTable::Nodes,
            id: node_id,
            vector,
            reply,
        })
    }

    /// Store (or replace) the embedding for an edge (its fact sentence), in
    /// the edge's group partition. The edge row must already exist
    /// ([`Error::NotFound`] otherwise); embedding a purged id is a no-op.
    pub fn set_edge_embedding(&self, edge_id: Uuid, vector: Vec<f32>) -> Result<()> {
        writer::call(&self.write_tx, |reply| WriteMsg::SetEmbedding {
            table: VecTable::Edges,
            id: edge_id,
            vector,
            reply,
        })
    }

    pub(crate) fn now_ms(&self) -> TimestampMs {
        self.clock.now_ms()
    }

    /// Lock the read connection, recovering from poisoning: a panic on
    /// another thread mid-read leaves the connection itself sound (rusqlite
    /// resets statements), and a storage library must not convert one panic
    /// into a permanent panic on every subsequent call.
    pub(crate) fn read(&self) -> std::sync::MutexGuard<'_, Connection> {
        self.read_conn
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }
}

// Compile-time proof of the documented `Send + Sync` claim on `Grit`.
const _: () = {
    const fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<Grit>();
};

impl Drop for Grit {
    fn drop(&mut self) {
        let _ = self.write_tx.send(WriteMsg::Shutdown);
        if let Some(handle) = self.writer.take() {
            let _ = handle.join();
        }
    }
}

fn validate(op: &GraphOp) -> Result<()> {
    match op {
        GraphOp::MergeNodes { from, into } if from == into => {
            Err(Error::InvalidOp("cannot merge a node into itself".into()))
        }
        GraphOp::Purge { ids } if ids.is_empty() => {
            Err(Error::InvalidOp("purge with no ids".into()))
        }
        GraphOp::UpdateNode {
            name: None,
            summary: None,
            kind: None,
            attrs: None,
            ..
        } => Err(Error::InvalidOp("update with no fields".into())),
        _ => Ok(()),
    }
}

fn configure(conn: &Connection) -> Result<()> {
    conn.busy_timeout(std::time::Duration::from_secs(5))?;
    conn.pragma_update(None, "journal_mode", "WAL")?;
    conn.pragma_update(None, "synchronous", "NORMAL")?;
    configure_cache(conn)?;
    Ok(())
}

/// Read-performance pragmas, applied to writer and reader connections alike.
/// SQLite's defaults (2 MB page cache, no mmap) assume a shared server box;
/// grit is a personal memory store where the traversal latency budget
/// (AGENTS.md invariant 6) is worth 32 MB of cache and a read-only mmap.
fn configure_cache(conn: &Connection) -> Result<()> {
    conn.pragma_update(None, "cache_size", -32_768)?; // KiB units when negative: 32 MB
    conn.pragma_update(None, "mmap_size", 268_435_456)?; // 256 MB
    Ok(())
}