recall-graph 0.2.0

Knowledge graph with semantic search for AI memory systems
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
use recall_graph::types::*;
use recall_graph::GraphMemory;
use tempfile::TempDir;

async fn setup() -> (GraphMemory, TempDir) {
    let dir = TempDir::new().unwrap();
    let gm = GraphMemory::open(dir.path()).await.unwrap();
    (gm, dir)
}

fn rust_entity() -> NewEntity {
    NewEntity {
        name: "Rust".to_string(),
        entity_type: EntityType::Tool,
        abstract_text: "Systems programming language focused on safety and performance".to_string(),
        overview: Some("Rust is a multi-paradigm systems programming language.".to_string()),
        content: None,
        attributes: None,
        source: Some("test".to_string()),
    }
}

fn voice_echo_entity() -> NewEntity {
    NewEntity {
        name: "voice-echo".to_string(),
        entity_type: EntityType::Project,
        abstract_text: "Voice assistant pipeline built in Rust using Twilio and Claude".to_string(),
        overview: None,
        content: None,
        attributes: None,
        source: Some("test".to_string()),
    }
}

#[tokio::test]
async fn add_and_get_entity() {
    let (gm, _dir) = setup().await;

    let entity = gm.add_entity(rust_entity()).await.unwrap();
    assert_eq!(entity.name, "Rust");
    assert_eq!(entity.entity_type, EntityType::Tool);
    assert!(entity.embedding.is_some());
    assert!(entity.mutable); // Tool is mutable

    // Get by name
    let found = gm.get_entity("Rust").await.unwrap();
    assert!(found.is_some());
    assert_eq!(found.unwrap().name, "Rust");

    // Get by ID
    let found_by_id = gm.get_entity_by_id(&entity.id_string()).await.unwrap();
    assert!(found_by_id.is_some());
}

#[tokio::test]
async fn entity_not_found() {
    let (gm, _dir) = setup().await;

    let found = gm.get_entity("nonexistent").await.unwrap();
    assert!(found.is_none());
}

#[tokio::test]
async fn update_entity() {
    let (gm, _dir) = setup().await;

    let entity = gm.add_entity(rust_entity()).await.unwrap();

    let updated = gm
        .update_entity(
            &entity.id_string(),
            EntityUpdate {
                overview: Some("Updated overview for Rust.".to_string()),
                ..Default::default()
            },
        )
        .await
        .unwrap();

    assert_eq!(updated.overview, "Updated overview for Rust.");
}

#[tokio::test]
async fn delete_entity() {
    let (gm, _dir) = setup().await;

    let entity = gm.add_entity(rust_entity()).await.unwrap();
    gm.delete_entity(&entity.id_string()).await.unwrap();

    let found = gm.get_entity("Rust").await.unwrap();
    assert!(found.is_none());
}

#[tokio::test]
async fn list_entities() {
    let (gm, _dir) = setup().await;

    gm.add_entity(rust_entity()).await.unwrap();
    gm.add_entity(voice_echo_entity()).await.unwrap();

    let all = gm.list_entities(None).await.unwrap();
    assert_eq!(all.len(), 2);

    let tools = gm.list_entities(Some("tool")).await.unwrap();
    assert_eq!(tools.len(), 1);
    assert_eq!(tools[0].name, "Rust");

    let projects = gm.list_entities(Some("project")).await.unwrap();
    assert_eq!(projects.len(), 1);
    assert_eq!(projects[0].name, "voice-echo");
}

#[tokio::test]
async fn add_and_get_relationship() {
    let (gm, _dir) = setup().await;

    gm.add_entity(rust_entity()).await.unwrap();
    gm.add_entity(voice_echo_entity()).await.unwrap();

    let rel = gm
        .add_relationship(NewRelationship {
            from_entity: "voice-echo".to_string(),
            to_entity: "Rust".to_string(),
            rel_type: "WRITTEN_IN".to_string(),
            description: Some("voice-echo is written in Rust".to_string()),
            confidence: None,
            source: Some("test".to_string()),
        })
        .await
        .unwrap();

    assert_eq!(rel.rel_type, "WRITTEN_IN");
    assert!(rel.valid_until.is_none());

    // Get outgoing from voice-echo
    let rels = gm
        .get_relationships("voice-echo", Direction::Outgoing)
        .await
        .unwrap();
    assert_eq!(rels.len(), 1);
    assert_eq!(rels[0].rel_type, "WRITTEN_IN");
}

#[tokio::test]
async fn semantic_search() {
    let (gm, _dir) = setup().await;

    gm.add_entity(rust_entity()).await.unwrap();
    gm.add_entity(voice_echo_entity()).await.unwrap();

    let results = gm.search("programming language", 5).await.unwrap();
    assert!(!results.is_empty());
    // Rust should rank higher for "programming language"
    assert_eq!(results[0].entity.name, "Rust");
}

#[tokio::test]
async fn traverse_graph() {
    let (gm, _dir) = setup().await;

    gm.add_entity(rust_entity()).await.unwrap();
    gm.add_entity(voice_echo_entity()).await.unwrap();

    gm.add_relationship(NewRelationship {
        from_entity: "voice-echo".to_string(),
        to_entity: "Rust".to_string(),
        rel_type: "WRITTEN_IN".to_string(),
        description: None,
        confidence: None,
        source: None,
    })
    .await
    .unwrap();

    let tree = gm.traverse("voice-echo", 1).await.unwrap();
    assert_eq!(tree.entity.name, "voice-echo");
    assert_eq!(tree.edges.len(), 1);
    assert_eq!(tree.edges[0].rel_type, "WRITTEN_IN");
    assert_eq!(tree.edges[0].target.entity.name, "Rust");
}

#[tokio::test]
async fn graph_stats() {
    let (gm, _dir) = setup().await;

    let stats = gm.stats().await.unwrap();
    assert_eq!(stats.entity_count, 0);
    assert_eq!(stats.relationship_count, 0);

    gm.add_entity(rust_entity()).await.unwrap();
    gm.add_entity(voice_echo_entity()).await.unwrap();

    let stats = gm.stats().await.unwrap();
    assert_eq!(stats.entity_count, 2);
}

#[tokio::test]
async fn immutable_entity_type() {
    let (gm, _dir) = setup().await;

    let entity = gm
        .add_entity(NewEntity {
            name: "Architecture Decision".to_string(),
            entity_type: EntityType::Decision,
            abstract_text: "Chose SurrealDB for graph storage".to_string(),
            overview: None,
            content: None,
            attributes: None,
            source: None,
        })
        .await
        .unwrap();

    assert!(!entity.mutable); // Decision is immutable
}

#[tokio::test]
async fn supersede_relationship() {
    let (gm, _dir) = setup().await;

    gm.add_entity(rust_entity()).await.unwrap();
    gm.add_entity(voice_echo_entity()).await.unwrap();

    let old_rel = gm
        .add_relationship(NewRelationship {
            from_entity: "voice-echo".to_string(),
            to_entity: "Rust".to_string(),
            rel_type: "USES".to_string(),
            description: Some("old relationship".to_string()),
            confidence: Some(0.5),
            source: None,
        })
        .await
        .unwrap();

    let new_rel = gm
        .supersede_relationship(
            &old_rel.id_string(),
            NewRelationship {
                from_entity: "voice-echo".to_string(),
                to_entity: "Rust".to_string(),
                rel_type: "WRITTEN_IN".to_string(),
                description: Some("superseded relationship".to_string()),
                confidence: Some(1.0),
                source: None,
            },
        )
        .await
        .unwrap();

    assert_eq!(new_rel.rel_type, "WRITTEN_IN");
    assert!(new_rel.valid_until.is_none());
}

// ── Phase 3: Search & Retrieval ──────────────────────────────────────

#[tokio::test]
async fn search_with_type_filter() {
    let (gm, _dir) = setup().await;

    gm.add_entity(rust_entity()).await.unwrap();
    gm.add_entity(voice_echo_entity()).await.unwrap();

    // Search for "programming" filtered to tools only
    let results = gm
        .search_with_options(
            "programming language",
            &SearchOptions {
                limit: 5,
                entity_type: Some("tool".to_string()),
                keyword: None,
            },
        )
        .await
        .unwrap();

    assert!(!results.is_empty());
    assert_eq!(results[0].entity.name, "Rust");
    // All results should be tools
    for r in &results {
        assert_eq!(r.entity.entity_type, EntityType::Tool);
    }

    // Search filtered to projects — should not return Rust
    let project_results = gm
        .search_with_options(
            "programming language",
            &SearchOptions {
                limit: 5,
                entity_type: Some("project".to_string()),
                keyword: None,
            },
        )
        .await
        .unwrap();

    for r in &project_results {
        assert_ne!(r.entity.name, "Rust");
    }
}

#[tokio::test]
async fn search_episodes() {
    let (gm, _dir) = setup().await;

    // Add an episode
    gm.add_episode(NewEpisode {
        session_id: "sess-001".to_string(),
        abstract_text: "Discussed Rust memory safety and ownership model".to_string(),
        overview: Some("Deep dive into borrow checker".to_string()),
        content: None,
        log_number: Some(1),
    })
    .await
    .unwrap();

    gm.add_episode(NewEpisode {
        session_id: "sess-002".to_string(),
        abstract_text: "Set up Docker containers for the web app deployment".to_string(),
        overview: None,
        content: None,
        log_number: Some(2),
    })
    .await
    .unwrap();

    let results = gm.search_episodes("Rust ownership", 5).await.unwrap();
    assert!(!results.is_empty());
    assert_eq!(results[0].episode.session_id, "sess-001");
}

#[tokio::test]
async fn hybrid_query() {
    let (gm, _dir) = setup().await;

    // Create entities with a relationship
    gm.add_entity(rust_entity()).await.unwrap();
    gm.add_entity(voice_echo_entity()).await.unwrap();
    gm.add_entity(NewEntity {
        name: "SurrealDB".to_string(),
        entity_type: EntityType::Tool,
        abstract_text: "Multi-model database with embedded and distributed modes".to_string(),
        overview: None,
        content: None,
        attributes: None,
        source: Some("test".to_string()),
    })
    .await
    .unwrap();

    // voice-echo WRITTEN_IN Rust
    gm.add_relationship(NewRelationship {
        from_entity: "voice-echo".to_string(),
        to_entity: "Rust".to_string(),
        rel_type: "WRITTEN_IN".to_string(),
        description: None,
        confidence: None,
        source: None,
    })
    .await
    .unwrap();

    // Add an episode
    gm.add_episode(NewEpisode {
        session_id: "sess-003".to_string(),
        abstract_text: "Built voice assistant pipeline with Rust and Twilio integration"
            .to_string(),
        overview: None,
        content: None,
        log_number: Some(3),
    })
    .await
    .unwrap();

    // Hybrid query — should find entities via semantic + graph expansion, and episodes
    let result = gm
        .query(
            "voice assistant",
            &QueryOptions {
                limit: 10,
                entity_type: None,
                keyword: None,
                graph_depth: 1,
                include_episodes: true,
            },
        )
        .await
        .unwrap();

    // Should have entities
    assert!(!result.entities.is_empty());
    // voice-echo should be in results (direct semantic match)
    let entity_names: Vec<&str> = result
        .entities
        .iter()
        .map(|e| e.entity.name.as_str())
        .collect();
    assert!(entity_names.contains(&"voice-echo"));

    // Should have episodes
    assert!(!result.episodes.is_empty());

    // Check that graph-expanded entities have Graph source
    let has_graph_source = result
        .entities
        .iter()
        .any(|e| matches!(e.source, MatchSource::Graph { .. }));
    // Rust should be found via graph expansion from voice-echo
    let has_rust = entity_names.contains(&"Rust");
    // Either Rust was found semantically or via graph — it should be present either way
    assert!(has_rust || has_graph_source);
}

#[tokio::test]
async fn traversal_with_type_filter() {
    let (gm, _dir) = setup().await;

    gm.add_entity(rust_entity()).await.unwrap();
    gm.add_entity(voice_echo_entity()).await.unwrap();
    gm.add_entity(NewEntity {
        name: "SurrealDB".to_string(),
        entity_type: EntityType::Tool,
        abstract_text: "Multi-model database".to_string(),
        overview: None,
        content: None,
        attributes: None,
        source: None,
    })
    .await
    .unwrap();

    gm.add_relationship(NewRelationship {
        from_entity: "voice-echo".to_string(),
        to_entity: "Rust".to_string(),
        rel_type: "WRITTEN_IN".to_string(),
        description: None,
        confidence: None,
        source: None,
    })
    .await
    .unwrap();

    gm.add_relationship(NewRelationship {
        from_entity: "voice-echo".to_string(),
        to_entity: "SurrealDB".to_string(),
        rel_type: "USES".to_string(),
        description: None,
        confidence: None,
        source: None,
    })
    .await
    .unwrap();

    // Traverse without filter — should see both Rust and SurrealDB
    let tree = gm.traverse("voice-echo", 1).await.unwrap();
    assert_eq!(tree.edges.len(), 2);

    // Traverse with type filter — only tools
    let filtered = gm
        .traverse_filtered("voice-echo", 1, Some("tool"))
        .await
        .unwrap();
    assert_eq!(filtered.edges.len(), 2); // Both Rust and SurrealDB are tools

    // Traverse with type filter — only projects (should find none as neighbors)
    let filtered_proj = gm
        .traverse_filtered("voice-echo", 1, Some("project"))
        .await
        .unwrap();
    assert_eq!(filtered_proj.edges.len(), 0);
}