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
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
//! Quick performance smoke test against the AGENTS.md scale envelope
//! (≤100k nodes / ≤1M edges; traversal ≤10ms p95; hybrid search ≤50ms p95).
//! Not the criterion suite — a fixture generator + wall-clock percentiles to
//! see where we stand. Usage:
//!
//! ```sh
//! cargo run --release -p grit-core --example quickbench -- [nodes] [edges] [episodes] [dim]
//! ```
//!
//! `dim` (default 1024, nacre's embedding-3 reality; 0 disables) populates
//! every node and edge with a deterministic pseudo-random embedding and adds
//! the vector-leg measurements: full hybrid search with a query vector
//! (grouped = partition-pruned to one of the 8 fixture groups, ungrouped =
//! global scan), and raw sqlite-vec KNN per table for leg isolation. vec0
//! MATCH is an exhaustive scan — these sections exist because the ≤50ms
//! hybrid target was historically measured FTS-only.

use std::time::Instant;

use grit_core::{Budget, GraphOp, Grit, Options, Query, Traversal};
use uuid::Uuid;

/// Deterministic xorshift so runs are comparable without a rand dependency.
struct Rng(u64);

impl Rng {
    fn next(&mut self) -> u64 {
        self.0 ^= self.0 << 13;
        self.0 ^= self.0 >> 7;
        self.0 ^= self.0 << 17;
        self.0
    }

    fn below(&mut self, n: usize) -> usize {
        (self.next() % n as u64) as usize
    }
}

const WORDS: &[&str] = &[
    "yoneda",
    "functor",
    "kernel",
    "image",
    "exact",
    "sequence",
    "lemma",
    "sheaf",
    "topos",
    "adjoint",
    "limit",
    "colimit",
    "monad",
    "algebra",
    "module",
    "tensor",
    "homology",
    "cohomology",
    "spectral",
    "fibration",
    "bundle",
    "manifold",
    "scheme",
    "variety",
    "ideal",
    "ring",
    "field",
    "group",
    "torsion",
    "lattice",
    "category",
    "morphism",
    "natural",
    "transformation",
    "presheaf",
    "stalk",
    "germ",
    "local",
    "global",
    "section",
    "derived",
    "abelian",
    "simplicial",
    "chain",
    "complex",
    "boundary",
    "cycle",
    "closed",
    "open",
    "dense",
    "compact",
    "hausdorff",
    "metric",
    "norm",
    "banach",
    "hilbert",
    "operator",
    "spectrum",
    "eigenvalue",
    "duality",
];

fn percentiles(mut samples: Vec<f64>) -> (f64, f64, f64) {
    samples.sort_by(f64::total_cmp);
    let pick = |q: f64| samples[((samples.len() - 1) as f64 * q) as usize];
    (pick(0.50), pick(0.95), *samples.last().unwrap())
}

/// Deterministic pseudo-random embedding, components uniform in [-1, 1).
/// Unnormalized is fine: the vec tables use distance_metric=cosine.
fn fill_embedding(rng: &mut Rng, out: &mut [f32]) {
    for x in out.iter_mut() {
        *x = ((rng.next() >> 40) as f32 / (1u32 << 24) as f32) * 2.0 - 1.0;
    }
}

/// f32 slice → little-endian blob, as sqlite-vec stores it (the raw-KNN
/// isolation sections bind blobs directly).
fn f32s_as_bytes(v: &[f32]) -> Vec<u8> {
    let mut out = Vec::with_capacity(v.len() * 4);
    for x in v {
        out.extend_from_slice(&x.to_le_bytes());
    }
    out
}

fn arg(n: usize, default: usize) -> usize {
    std::env::args()
        .nth(n)
        .and_then(|s| s.parse().ok())
        .unwrap_or(default)
}

fn main() -> Result<(), grit_core::Error> {
    let n_nodes = arg(1, 100_000);
    let n_edges = arg(2, 300_000);
    let n_episodes = arg(3, 10_000);
    let dim = arg(4, 1024);
    let n_invalidations = n_edges / 20; // invalidate 5% of edges

    let dir = std::env::temp_dir().join(format!("grit-quickbench-{}", std::process::id()));
    std::fs::create_dir_all(&dir)?;
    let path = dir.join("bench.db");
    let _ = std::fs::remove_file(&path);
    let g = Grit::open(&path, Options::new("bench"))?;
    let mut rng = Rng(0x9E37_79B9_7F4A_7C15);

    println!(
        "fixture: {n_nodes} nodes, {n_edges} edges, {n_episodes} episodes, {n_invalidations} invalidations, dim {dim}"
    );
    println!("db: {}\n", path.display());

    // ---- write path ----
    let t = Instant::now();
    let mut node_ids: Vec<Uuid> = Vec::with_capacity(n_nodes);
    for i in 0..n_nodes {
        let id = g.new_id();
        let (w1, w2, w3, w4) = (
            WORDS[rng.below(WORDS.len())],
            WORDS[rng.below(WORDS.len())],
            WORDS[rng.below(WORDS.len())],
            WORDS[rng.below(WORDS.len())],
        );
        g.apply(GraphOp::AddNode {
            id,
            kind: "concept".into(),
            name: format!("{w1} {w2} {i}"),
            summary: format!("the {w3} of the {w4} in context {i}"),
            attrs: serde_json::json!({}),
            group_id: format!("g{}", i % 8),
        })?;
        node_ids.push(id);
    }
    let dt = t.elapsed().as_secs_f64();
    println!(
        "insert nodes:      {:>9.0} ops/s  ({dt:.1}s)",
        n_nodes as f64 / dt
    );

    let now_ms = 1_752_000_000_000i64; // roughly "now"; only relative order matters
    let t = Instant::now();
    let mut edge_ids: Vec<Uuid> = Vec::with_capacity(n_edges);
    for i in 0..n_edges {
        let id = g.new_id();
        let src = node_ids[rng.below(n_nodes)];
        let dst = node_ids[rng.below(n_nodes)];
        let (w1, w2) = (WORDS[rng.below(WORDS.len())], WORDS[rng.below(WORDS.len())]);
        g.apply(GraphOp::AddEdge {
            id,
            src,
            dst,
            rel: "RELATES".into(),
            fact: format!("the {w1} constrains the {w2} ({i})"),
            attrs: serde_json::json!({}),
            group_id: format!("g{}", i % 8),
            valid_at: Some(now_ms - (rng.below(1_000_000_000) as i64)),
            invalid_at: None,
        })?;
        edge_ids.push(id);
    }
    let dt = t.elapsed().as_secs_f64();
    println!(
        "insert edges:      {:>9.0} ops/s  ({dt:.1}s)",
        n_edges as f64 / dt
    );

    let t = Instant::now();
    for i in 0..n_episodes {
        let (w1, w2, w3) = (
            WORDS[rng.below(WORDS.len())],
            WORDS[rng.below(WORDS.len())],
            WORDS[rng.below(WORDS.len())],
        );
        g.apply(GraphOp::AddEpisode {
            id: g.new_id(),
            source: "bench".into(),
            kind: String::new(),
            content: format!("today we discussed the {w1} and how the {w2} affects the {w3}"),
            occurred_at: now_ms - i as i64,
            group_id: format!("g{}", i % 8),
            mentions: vec![node_ids[rng.below(n_nodes)], edge_ids[rng.below(n_edges)]],
        })?;
    }
    let dt = t.elapsed().as_secs_f64();
    println!(
        "insert episodes:   {:>9.0} ops/s  ({dt:.1}s)",
        n_episodes as f64 / dt
    );

    let t = Instant::now();
    for _ in 0..n_invalidations {
        g.apply(GraphOp::InvalidateEdge {
            edge_id: edge_ids[rng.below(n_edges)],
            invalid_at: now_ms - (rng.below(500_000_000) as i64),
        })?;
    }
    let dt = t.elapsed().as_secs_f64();
    println!(
        "invalidate edges:  {:>9.0} ops/s  ({dt:.1}s)\n",
        n_invalidations as f64 / dt
    );

    // ---- embeddings (dim 0 skips; every row gets a vector, like nacre) ----
    let mut embed_rng = Rng(0xA5A5_5A5A_D00D_F00D);
    let mut embedding = vec![0f32; dim];
    if dim > 0 {
        g.register_embedding_model("bench-model", dim, "1")?;
        let t = Instant::now();
        for id in &node_ids {
            fill_embedding(&mut embed_rng, &mut embedding);
            g.set_node_embedding(*id, embedding.clone())?;
        }
        let dt = t.elapsed().as_secs_f64();
        println!(
            "embed nodes:       {:>9.0} ops/s  ({dt:.1}s)",
            n_nodes as f64 / dt
        );
        let t = Instant::now();
        for id in &edge_ids {
            fill_embedding(&mut embed_rng, &mut embedding);
            g.set_edge_embedding(*id, embedding.clone())?;
        }
        let dt = t.elapsed().as_secs_f64();
        println!(
            "embed edges:       {:>9.0} ops/s  ({dt:.1}s)\n",
            n_edges as f64 / dt
        );
    }

    // ---- read path ----
    const ITERS: usize = 200;
    /// Fewer iterations for the vector sections: at envelope scale each
    /// query linear-scans hundreds of MB, and 60 samples pin p50/p95 well
    /// enough for a smoke tool.
    const VEC_ITERS: usize = 60;

    let mut samples = Vec::with_capacity(ITERS);
    let mut reached = 0usize;
    for _ in 0..ITERS {
        let seed = node_ids[rng.below(n_nodes)];
        let t = Instant::now();
        let sub = g.traverse(&[seed], &Traversal::default().depth(3))?;
        samples.push(t.elapsed().as_secs_f64() * 1e3);
        reached += sub.nodes.len();
    }
    let (p50, p95, max) = percentiles(samples);
    println!(
        "traverse depth-3:  p50 {p50:7.2}ms  p95 {p95:7.2}ms  max {max:7.2}ms  \
         (avg {} nodes reached, default 256 budget; target p95 ≤ 10ms)",
        reached / ITERS
    );

    let mut samples = Vec::with_capacity(ITERS);
    let mut reached = 0usize;
    for _ in 0..ITERS {
        let seed = node_ids[rng.below(n_nodes)];
        let t = Instant::now();
        let sub = g.traverse(
            &[seed],
            &Traversal::default().depth(3).max_nodes(usize::MAX),
        )?;
        samples.push(t.elapsed().as_secs_f64() * 1e3);
        reached += sub.nodes.len();
    }
    let (p50, p95, max) = percentiles(samples);
    println!(
        "traverse (no cap): p50 {p50:7.2}ms  p95 {p95:7.2}ms  max {max:7.2}ms  \
         (avg {} nodes reached, unbounded)",
        reached / ITERS
    );

    let mut samples = Vec::with_capacity(ITERS);
    for _ in 0..ITERS {
        let seed = node_ids[rng.below(n_nodes)];
        let t = Instant::now();
        g.traverse(
            &[seed],
            &Traversal::default().depth(3).as_at(now_ms - 250_000_000),
        )?;
        samples.push(t.elapsed().as_secs_f64() * 1e3);
    }
    let (p50, p95, max) = percentiles(samples);
    println!("traverse (as_at):  p50 {p50:7.2}ms  p95 {p95:7.2}ms  max {max:7.2}ms  (time travel)");

    let mut samples = Vec::with_capacity(ITERS);
    let mut hits_total = 0usize;
    for _ in 0..ITERS {
        let (w1, w2) = (WORDS[rng.below(WORDS.len())], WORDS[rng.below(WORDS.len())]);
        let t = Instant::now();
        let hits = g.search(Query::text(format!("{w1} {w2}")).budget(Budget::items(20)))?;
        samples.push(t.elapsed().as_secs_f64() * 1e3);
        hits_total += hits.len();
    }
    let (p50, p95, max) = percentiles(samples);
    println!(
        "hybrid search:     p50 {p50:7.2}ms  p95 {p95:7.2}ms  max {max:7.2}ms  \
         (avg {} hits; FTS only — no query vector; target p95 ≤ 50ms)",
        hits_total / ITERS
    );

    if dim > 0 {
        // Full hybrid search WITH a query vector, ungrouped: both vec legs
        // scan every partition — the envelope worst case.
        let mut samples = Vec::with_capacity(VEC_ITERS);
        let mut hits_total = 0usize;
        for _ in 0..VEC_ITERS {
            let (w1, w2) = (WORDS[rng.below(WORDS.len())], WORDS[rng.below(WORDS.len())]);
            fill_embedding(&mut embed_rng, &mut embedding);
            let t = Instant::now();
            let hits = g.search(
                Query::text(format!("{w1} {w2}"))
                    .vector(embedding.clone())
                    .budget(Budget::items(20)),
            )?;
            samples.push(t.elapsed().as_secs_f64() * 1e3);
            hits_total += hits.len();
        }
        let (p50, p95, max) = percentiles(samples);
        println!(
            "hybrid+vec (all):  p50 {p50:7.2}ms  p95 {p95:7.2}ms  max {max:7.2}ms  \
             (avg {} hits; global vec scan; target p95 ≤ 50ms)",
            hits_total / VEC_ITERS
        );

        // Grouped: the vec legs prune to one of the 8 fixture partitions.
        let mut samples = Vec::with_capacity(VEC_ITERS);
        let mut hits_total = 0usize;
        for _ in 0..VEC_ITERS {
            let (w1, w2) = (WORDS[rng.below(WORDS.len())], WORDS[rng.below(WORDS.len())]);
            fill_embedding(&mut embed_rng, &mut embedding);
            let t = Instant::now();
            let hits = g.search(
                Query::text(format!("{w1} {w2}"))
                    .vector(embedding.clone())
                    .group(format!("g{}", rng.below(8)))
                    .budget(Budget::items(20)),
            )?;
            samples.push(t.elapsed().as_secs_f64() * 1e3);
            hits_total += hits.len();
        }
        let (p50, p95, max) = percentiles(samples);
        println!(
            "hybrid+vec (grp):  p50 {p50:7.2}ms  p95 {p95:7.2}ms  max {max:7.2}ms  \
             (avg {} hits; 1/8 partition; target p95 ≤ 50ms)",
            hits_total / VEC_ITERS
        );

        // Vector legs through the public API with no FTS work (empty text):
        // vec legs + expansion + RRF + fetch.
        let mut samples = Vec::with_capacity(VEC_ITERS);
        for _ in 0..VEC_ITERS {
            fill_embedding(&mut embed_rng, &mut embedding);
            let t = Instant::now();
            g.search(
                Query::text("")
                    .vector(embedding.clone())
                    .budget(Budget::items(20)),
            )?;
            samples.push(t.elapsed().as_secs_f64() * 1e3);
        }
        let (p50, p95, max) = percentiles(samples);
        println!("vec legs only:     p50 {p50:7.2}ms  p95 {p95:7.2}ms  max {max:7.2}ms");

        // Raw KNN per table over a read-only connection — true leg
        // isolation, exactly the SQL search.rs runs (vec0 is available via
        // the auto-extension the Grit open registered).
        let raw = rusqlite::Connection::open_with_flags(
            &path,
            rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY,
        )
        .expect("read-only connection");
        for (label, table) in [("nodes", "vec_nodes"), ("edges", "vec_edges")] {
            let mut stmt = raw
                .prepare(&format!(
                    "SELECT id FROM {table} WHERE embedding MATCH ?1 AND k = ?2 ORDER BY distance"
                ))
                .expect("prepare knn");
            let mut samples = Vec::with_capacity(VEC_ITERS);
            for _ in 0..VEC_ITERS {
                fill_embedding(&mut embed_rng, &mut embedding);
                let blob = f32s_as_bytes(&embedding);
                let t = Instant::now();
                let n = stmt
                    .query_map(rusqlite::params![blob, 50i64], |r| r.get::<_, String>(0))
                    .expect("knn query")
                    .count();
                samples.push(t.elapsed().as_secs_f64() * 1e3);
                assert!(n <= 50);
            }
            let (p50, p95, max) = percentiles(samples);
            println!(
                "raw knn {label} all:  p50 {p50:7.2}ms  p95 {p95:7.2}ms  max {max:7.2}ms  (k=50)"
            );

            let mut stmt = raw
                .prepare(&format!(
                    "SELECT id FROM {table}
                     WHERE embedding MATCH ?1 AND k = ?2 AND group_id = ?3
                     ORDER BY distance"
                ))
                .expect("prepare grouped knn");
            let mut samples = Vec::with_capacity(VEC_ITERS);
            for _ in 0..VEC_ITERS {
                fill_embedding(&mut embed_rng, &mut embedding);
                let blob = f32s_as_bytes(&embedding);
                let group = format!("g{}", rng.below(8));
                let t = Instant::now();
                let n = stmt
                    .query_map(rusqlite::params![blob, 50i64, group], |r| {
                        r.get::<_, String>(0)
                    })
                    .expect("grouped knn query")
                    .count();
                samples.push(t.elapsed().as_secs_f64() * 1e3);
                assert!(n <= 50);
            }
            let (p50, p95, max) = percentiles(samples);
            println!(
                "raw knn {label} grp:  p50 {p50:7.2}ms  p95 {p95:7.2}ms  max {max:7.2}ms  (k=50, 1 of 8 partitions)"
            );
        }
    }

    let mut samples = Vec::with_capacity(ITERS);
    for _ in 0..ITERS {
        let id = node_ids[rng.below(n_nodes)];
        let t = Instant::now();
        g.node_history(id)?;
        samples.push(t.elapsed().as_secs_f64() * 1e3);
    }
    let (p50, p95, max) = percentiles(samples);
    println!("node_history:      p50 {p50:7.2}ms  p95 {p95:7.2}ms  max {max:7.2}ms");

    let stats = g.stats()?;
    drop(g); // checkpoint WAL before measuring size
    let size = std::fs::metadata(&path)?.len() as f64 / 1e6;
    let vec_mb = if dim > 0 {
        ((n_nodes + n_edges) * dim * 4) as f64 / 1e6
    } else {
        0.0
    };
    println!(
        "\nfinal: {} nodes, {} edges, {} episodes, {} oplog entries, {size:.0} MB on disk \
         (~{vec_mb:.0} MB of that is raw vector data)",
        stats.nodes, stats.edges, stats.episodes, stats.oplog
    );
    Ok(())
}