grit-core 0.2.2

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
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
//! End-to-end coverage of the public API: writes, search, traversal, merge
//! candidates, purge, embeddings, export/import.

use std::sync::Arc;

use grit_core::{
    Budget, GraphOp, Grit, ManualClock, Options, Query, SearchKind, SearchTarget, Traversal,
    import_jsonl,
};
use serde_json::json;
use uuid::Uuid;

fn open(dir: &tempfile::TempDir, name: &str, clock: Arc<ManualClock>) -> Grit {
    Grit::open(
        dir.path().join(name),
        Options::new("test-device").clock(clock),
    )
    .unwrap()
}

fn add_node(g: &Grit, name: &str, summary: &str, group: &str) -> Uuid {
    let id = g.new_id();
    g.apply(GraphOp::AddNode {
        id,
        kind: "concept".into(),
        name: name.into(),
        summary: summary.into(),
        attrs: json!({}),
        group_id: group.into(),
    })
    .unwrap();
    id
}

fn add_edge(g: &Grit, src: Uuid, dst: Uuid, rel: &str, fact: &str, group: &str) -> Uuid {
    let id = g.new_id();
    g.apply(GraphOp::AddEdge {
        id,
        src,
        dst,
        rel: rel.into(),
        fact: fact.into(),
        attrs: json!({}),
        group_id: group.into(),
        valid_at: None,
        invalid_at: None,
    })
    .unwrap();
    id
}

#[test]
fn write_search_traverse_history() {
    let dir = tempfile::tempdir().unwrap();
    let clock = Arc::new(ManualClock::new(1_000_000));
    let g = open(&dir, "m.db", clock.clone());

    let yoneda = add_node(
        &g,
        "Yoneda lemma",
        "objects are their relationships",
        "algebra",
    );
    let functor = add_node(&g, "Functor", "structure-preserving map", "algebra");
    let exact = add_node(&g, "Exact sequence", "kernels meet images", "algebra");
    let offtopic = add_node(&g, "Sourdough starter", "flour and water", "cooking");

    let e1 = add_edge(
        &g,
        yoneda,
        functor,
        "USES",
        "The Yoneda lemma is about hom-functors",
        "algebra",
    );
    add_edge(
        &g,
        functor,
        exact,
        "PRESERVES",
        "Exact functors preserve exactness",
        "algebra",
    );

    let ep = g.new_id();
    g.apply(GraphOp::AddEpisode {
        id: ep,
        source: "chat".into(),
        kind: String::new(),
        content: "We discussed the Yoneda lemma and exactness today".into(),
        occurred_at: 999_000,
        group_id: "algebra".into(),
        mentions: vec![yoneda, e1],
    })
    .unwrap();

    // Search: text hit + provenance + group filter.
    let hits = g
        .search(
            Query::text("yoneda")
                .group("algebra")
                .budget(Budget::items(10)),
        )
        .unwrap();
    assert!(!hits.is_empty());
    let node_hit = hits
        .iter()
        .find(|h| matches!(&h.target, SearchTarget::Node(n) if n.id == yoneda))
        .expect("yoneda node should be a hit");
    assert_eq!(node_hit.episodes, vec![ep]);

    // Group filtering: cooking content is invisible from algebra queries.
    let hits = g.search(Query::text("sourdough").group("algebra")).unwrap();
    assert!(hits.is_empty());
    assert_eq!(g.node(offtopic).unwrap().unwrap().group_id, "cooking");

    // Traversal: depth 1 reaches functor but not exact; depth 2 reaches both.
    let one_hop = g
        .traverse(&[yoneda], &Traversal::default().depth(1))
        .unwrap();
    let ids: Vec<Uuid> = one_hop.nodes.iter().map(|n| n.id).collect();
    assert!(ids.contains(&yoneda) && ids.contains(&functor) && !ids.contains(&exact));

    let two_hop = g
        .traverse(&[yoneda], &Traversal::default().depth(2))
        .unwrap();
    assert_eq!(two_hop.nodes.len(), 3);
    assert_eq!(two_hop.edges.len(), 2);

    // History: every incident edge, with provenance intact.
    let history = g.node_history(yoneda).unwrap();
    assert_eq!(history.edges.len(), 1);
    assert_eq!(g.mentions_of(e1).unwrap(), vec![ep]);

    // Budget is honored.
    let hits = g
        .search(Query::text("yoneda").budget(Budget::items(1)))
        .unwrap();
    assert_eq!(hits.len(), 1);
}

#[test]
fn merge_candidates_and_merge() {
    let dir = tempfile::tempdir().unwrap();
    let clock = Arc::new(ManualClock::new(1_000_000));
    let g = open(&dir, "m.db", clock.clone());

    let a = add_node(&g, "Yoneda lemma", "", "algebra");
    let b = add_node(&g, "Yoneda Lemma", "duplicate from a later chat", "algebra");
    let c = add_node(&g, "Snake lemma", "", "algebra");
    let edge_on_b = add_edge(
        &g,
        b,
        c,
        "RELATED",
        "the Yoneda lemma relates to diagram chasing",
        "algebra",
    );

    // Scoring only — grit never merges by itself.
    let candidates = g.find_merge_candidates(a, 0.9).unwrap();
    assert_eq!(candidates[0].node.id, b);
    assert!(candidates[0].name_score > 0.99);
    assert!(!candidates.iter().any(|c2| c2.node.id == c));

    clock.advance_ms(10);
    g.apply(GraphOp::MergeNodes { from: b, into: a }).unwrap();

    // b is expired with an audit pointer; its edge now hangs off a.
    let b_node = g.node(b).unwrap().unwrap();
    assert_eq!(b_node.merged_into, Some(a));
    assert!(b_node.expired_at.is_some());
    assert_eq!(g.edge(edge_on_b).unwrap().unwrap().src, a);

    // Traversal from the stale handle resolves to the canonical node.
    let sub = g.traverse(&[b], &Traversal::default().depth(1)).unwrap();
    let ids: Vec<Uuid> = sub.nodes.iter().map(|n| n.id).collect();
    assert!(ids.contains(&a) && ids.contains(&c) && !ids.contains(&b));

    // Self-merge is rejected.
    assert!(g.apply(GraphOp::MergeNodes { from: a, into: a }).is_err());
}

#[test]
fn purge_is_exact_audited_and_permanent() {
    let dir = tempfile::tempdir().unwrap();
    let clock = Arc::new(ManualClock::new(1_000_000));
    let g = open(&dir, "m.db", clock.clone());

    let secret = add_node(&g, "Secret project", "codename butterfly", "work");
    let other = add_node(&g, "Coworker", "", "work");
    let edge = add_edge(
        &g,
        secret,
        other,
        "KNOWS",
        "Coworker knows about codename butterfly",
        "work",
    );

    // Layer 2 lists the node AND its incident edges (their facts carry the
    // content being forgotten); node_history enumerates them.
    let to_purge: Vec<Uuid> = std::iter::once(secret)
        .chain(g.node_history(secret).unwrap().edges.iter().map(|e| e.id))
        .collect();
    g.apply(GraphOp::Purge { ids: to_purge }).unwrap();

    assert!(g.node(secret).unwrap().is_none());
    assert!(g.edge(edge).unwrap().is_none());
    assert!(g.search(Query::text("butterfly")).unwrap().is_empty());

    // Tombstones are permanent: re-adding the purged id is a no-op.
    g.apply(GraphOp::AddNode {
        id: secret,
        kind: "concept".into(),
        name: "Secret project".into(),
        summary: String::new(),
        attrs: json!({}),
        group_id: "work".into(),
    })
    .unwrap();
    assert!(g.node(secret).unwrap().is_none());

    // ...but the audit trail survives in the oplog.
    let ops = g.oplog_since(0).unwrap();
    assert!(
        ops.iter()
            .any(|(_, e)| matches!(&e.op, GraphOp::Purge { ids } if ids.contains(&secret)))
    );

    // Empty purges are rejected.
    assert!(g.apply(GraphOp::Purge { ids: vec![] }).is_err());
}

#[test]
fn embeddings_and_vector_search() {
    let dir = tempfile::tempdir().unwrap();
    let clock = Arc::new(ManualClock::new(1_000_000));
    let g = open(&dir, "m.db", clock.clone());

    let cat = add_node(&g, "Category", "", "math");
    let set = add_node(&g, "Set", "", "math");
    let bread = add_node(&g, "Bread", "", "food");

    // Embedding before registering a model fails loudly; the getter
    // reports "no vector" instead of erroring (the tables don't exist yet).
    assert!(g.set_node_embedding(cat, vec![1.0, 0.0, 0.0, 0.0]).is_err());
    assert_eq!(g.get_node_embedding(cat).unwrap(), None);

    g.register_embedding_model("test-model", 4, "1").unwrap();
    g.set_node_embedding(cat, vec![1.0, 0.0, 0.0, 0.0]).unwrap();
    g.set_node_embedding(set, vec![0.9, 0.1, 0.0, 0.0]).unwrap();
    g.set_node_embedding(bread, vec![0.0, 0.0, 1.0, 0.0])
        .unwrap();

    // Dimension mismatches fail loudly.
    assert!(g.set_node_embedding(cat, vec![1.0]).is_err());
    assert!(g.register_embedding_model("other", 8, "1").is_err());

    // Vector leg: query near `cat` should rank cat/set above bread even
    // though the text matches nothing.
    let hits = g
        .search(
            Query::text("")
                .vector(vec![1.0, 0.05, 0.0, 0.0])
                .budget(Budget::items(2)),
        )
        .unwrap();
    let ids: Vec<Uuid> = hits
        .iter()
        .filter_map(|h| match &h.target {
            SearchTarget::Node(n) => Some(n.id),
            _ => None,
        })
        .collect();
    assert_eq!(ids.len(), 2);
    assert!(ids.contains(&cat) && ids.contains(&set));

    // Vector similarity feeds merge candidates.
    let candidates = g.find_merge_candidates(cat, 0.95).unwrap();
    assert!(
        candidates
            .iter()
            .any(|c| c.node.id == set && c.vector_score.unwrap() > 0.95)
    );

    // Getters return exactly what the setter stored; missing ids are None;
    // overwrites are visible.
    assert_eq!(
        g.get_node_embedding(cat).unwrap(),
        Some(vec![1.0, 0.0, 0.0, 0.0])
    );
    assert_eq!(g.get_node_embedding(g.new_id()).unwrap(), None);
    g.set_node_embedding(cat, vec![0.5, 0.5, 0.0, 0.0]).unwrap();
    assert_eq!(
        g.get_node_embedding(cat).unwrap(),
        Some(vec![0.5, 0.5, 0.0, 0.0])
    );
    let e = add_edge(&g, cat, set, "REL", "categories generalize sets", "math");
    assert_eq!(g.get_edge_embedding(e).unwrap(), None);
    g.set_edge_embedding(e, vec![0.0, 1.0, 0.0, 0.0]).unwrap();
    assert_eq!(
        g.get_edge_embedding(e).unwrap(),
        Some(vec![0.0, 1.0, 0.0, 0.0])
    );

    // A purged id stays vectorless (set_embedding no-ops on tombstones).
    g.apply(GraphOp::Purge { ids: vec![bread] }).unwrap();
    assert_eq!(g.get_node_embedding(bread).unwrap(), None);
}

#[test]
fn group_scans_match_export_view() {
    let dir = tempfile::tempdir().unwrap();
    let clock = Arc::new(ManualClock::new(1_000_000));
    let g = open(&dir, "scan.db", clock.clone());

    let a = add_node(&g, "Alpha", "", "g1");
    let b = add_node(&g, "Beta", "", "g1");
    let _other = add_node(&g, "Gamma", "", "g2");
    let e = add_edge(&g, a, b, "REL", "alpha relates to beta", "g1");
    g.apply(GraphOp::AddEpisode {
        id: g.new_id(),
        source: "chat".into(),
        kind: String::new(),
        content: "later episode".into(),
        occurred_at: 2_000,
        group_id: "g1".into(),
        mentions: vec![a],
    })
    .unwrap();
    g.apply(GraphOp::AddEpisode {
        id: g.new_id(),
        source: "chat".into(),
        kind: String::new(),
        content: "earlier episode".into(),
        occurred_at: 1_000,
        group_id: "g1".into(),
        mentions: vec![b],
    })
    .unwrap();
    // A merged-away node stays visible to the scan (callers filter).
    g.apply(GraphOp::MergeNodes { from: b, into: a }).unwrap();

    let nodes = g.nodes_in_group("g1").unwrap();
    assert_eq!(nodes.len(), 2, "expired rows included");
    assert!(nodes.iter().any(|n| n.id == b && n.expired_at.is_some()));
    assert!(g.nodes_in_group("g2").unwrap().len() == 1);
    assert!(g.nodes_in_group("nope").unwrap().is_empty());

    let edges = g.edges_in_group("g1").unwrap();
    assert_eq!(edges.len(), 1);
    assert_eq!(edges[0].id, e);
    assert_eq!(edges[0].src, a, "merge re-pointing visible in the scan");

    // Chronological episode order (occurred_at, not insertion).
    let eps = g.episodes_in_group("g1").unwrap();
    assert_eq!(eps.len(), 2);
    assert!(eps[0].occurred_at < eps[1].occurred_at);

    // Scan view == export view (the permanent compatibility surface).
    let mut export = Vec::new();
    g.export_jsonl(&mut export).unwrap();
    let export = String::from_utf8(export).unwrap();
    let count = |t: &str, g_id: &str| {
        export
            .lines()
            .filter(|l| l.contains(&format!("\"t\":\"{t}\"")) && l.contains(g_id))
            .count()
    };
    assert_eq!(g.nodes_in_group("g1").unwrap().len(), count("node", "g1"));
    assert_eq!(g.edges_in_group("g1").unwrap().len(), count("edge", "g1"));
    assert_eq!(
        g.episodes_in_group("g1").unwrap().len(),
        count("episode", "g1")
    );
}

#[test]
fn update_node_revises_metadata() {
    let dir = tempfile::tempdir().unwrap();
    let clock = Arc::new(ManualClock::new(1_000_000));
    let g = open(&dir, "u.db", clock.clone());
    let n = add_node(&g, "Priya", "junior engineer", "g1");

    // Partial update: absent fields keep their values.
    clock.advance_ms(10);
    g.apply(GraphOp::UpdateNode {
        id: n,
        name: Some("Priya Raman".into()),
        summary: Some("senior data engineer at Meridian Health".into()),
        kind: None,
        attrs: Some(serde_json::json!({"labels": ["Entity", "Person"]})),
    })
    .unwrap();
    let node = g.node(n).unwrap().unwrap();
    assert_eq!(node.name, "Priya Raman");
    assert_eq!(node.summary, "senior data engineer at Meridian Health");
    assert_eq!(node.kind, "concept", "untouched field survives");
    assert_eq!(node.attrs["labels"][1], "Person");

    // FTS mirrors follow the revision: new text found, old text gone.
    let found = |query: &str| {
        g.search(Query::text(query).group("g1").budget(Budget::items(5)))
            .unwrap()
            .iter()
            .any(|h| matches!(&h.target, SearchTarget::Node(node) if node.id == n))
    };
    assert!(found("Meridian"), "revised summary in FTS");
    assert!(!found("junior"), "old summary out of FTS");

    // An empty update is a decider bug, not a no-op.
    let err = g
        .apply(GraphOp::UpdateNode {
            id: n,
            name: None,
            summary: None,
            kind: None,
            attrs: None,
        })
        .unwrap_err();
    assert!(err.to_string().contains("no fields"));

    // Updating a purged node stays dead (right-to-forget).
    g.apply(GraphOp::Purge { ids: vec![n] }).unwrap();
    g.apply(GraphOp::UpdateNode {
        id: n,
        name: None,
        summary: Some("revenant".into()),
        kind: None,
        attrs: None,
    })
    .unwrap();
    assert!(g.node(n).unwrap().is_none());
}

#[test]
fn targets_filter_spends_budget_on_requested_kinds_only() {
    let dir = tempfile::tempdir().unwrap();
    let clock = Arc::new(ManualClock::new(1_000_000));
    let g = open(&dir, "t.db", clock.clone());
    // Several nodes sharing the query term so the mixed ranking is
    // node-heavy; one matching edge.
    let a = add_node(&g, "Northwind Labs", "", "g1");
    let b = add_node(&g, "Northwind Warehouse", "", "g1");
    let _c = add_node(&g, "Northwind Cafe", "", "g1");
    let e = add_edge(
        &g,
        a,
        b,
        "REL",
        "Northwind Labs supplies Northwind Warehouse.",
        "g1",
    );

    // A mixed top-1 cut would spend the budget on a node; the edges-only
    // target must return the edge.
    let hits = g
        .search(
            Query::text("Northwind")
                .group("g1")
                .targets(&[SearchKind::Edge])
                .budget(Budget::items(1)),
        )
        .unwrap();
    assert_eq!(hits.len(), 1);
    assert!(matches!(&hits[0].target, SearchTarget::Edge(edge) if edge.id == e));

    // Unfiltered queries are unchanged.
    let mixed = g
        .search(
            Query::text("Northwind")
                .group("g1")
                .budget(Budget::items(10)),
        )
        .unwrap();
    assert!(mixed.len() > 1);
}

#[test]
fn export_import_roundtrip_is_lossless() {
    let dir = tempfile::tempdir().unwrap();
    let clock = Arc::new(ManualClock::new(1_000_000));
    let g = open(&dir, "a.db", clock.clone());

    let n1 = add_node(&g, "Alpha", "first", "g1");
    let n2 = add_node(&g, "Beta", "second", "g1");
    let e = add_edge(&g, n1, n2, "REL", "alpha relates to beta", "g1");
    g.apply(GraphOp::AddEpisode {
        id: g.new_id(),
        source: "doc".into(),
        kind: String::new(),
        content: "alpha and beta".into(),
        occurred_at: 5,
        group_id: "g1".into(),
        mentions: vec![n1, e],
    })
    .unwrap();
    clock.advance_ms(10);
    g.apply(GraphOp::InvalidateEdge {
        edge_id: e,
        invalid_at: 1_000_500,
    })
    .unwrap();
    g.apply(GraphOp::UpdateNode {
        id: n1,
        name: None,
        summary: Some("first, revised".into()),
        kind: None,
        attrs: None,
    })
    .unwrap();
    g.apply(GraphOp::Purge { ids: vec![n2] }).unwrap();
    g.register_embedding_model("m", 4, "1").unwrap();

    let mut first = Vec::new();
    g.export_jsonl(&mut first).unwrap();
    assert!(
        String::from_utf8(first.clone())
            .unwrap()
            .contains("\"t\":\"node_update\""),
        "the per-field update record must round-trip"
    );

    let stats = import_jsonl(dir.path().join("b.db"), first.as_slice()).unwrap();
    assert_eq!(stats.nodes, 1); // n2 was purged
    assert_eq!(stats.edges, 1);
    assert_eq!(stats.oplog, 7);

    let g2 = open(&dir, "b.db", clock.clone());
    let mut second = Vec::new();
    g2.export_jsonl(&mut second).unwrap();
    assert_eq!(
        String::from_utf8(first).unwrap(),
        String::from_utf8(second).unwrap(),
        "export → import → export must be byte-identical"
    );

    // Import refuses to merge into non-empty databases.
    let mut third = Vec::new();
    g2.export_jsonl(&mut third).unwrap();
    assert!(import_jsonl(dir.path().join("b.db"), third.as_slice()).is_err());
}