oxirag 0.1.1

A four-layer RAG engine with SMT-based logic verification and knowledge graph support
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//! Graph traversal algorithms.

use std::collections::{HashSet, VecDeque};

use crate::error::GraphError;
use crate::layer4_graph::traits::GraphStore;
use crate::layer4_graph::types::{
    Direction, EntityId, EntityType, GraphPath, GraphQuery, RelationshipType,
};

/// Perform breadth-first traversal of the graph.
///
/// # Errors
///
/// Returns an error if graph store operations fail.
pub async fn bfs_traverse<S: GraphStore + ?Sized>(
    store: &S,
    query: &GraphQuery,
) -> Result<Vec<GraphPath>, GraphError> {
    let mut paths = Vec::new();

    for start_id in &query.start_entities {
        // Get the starting entity
        let Some(start_entity) = store.get_entity(start_id).await? else {
            continue; // Skip if entity not found
        };

        // Check entity filter for start
        if let Some(ref filter) = query.entity_filter
            && !filter.contains(&start_entity.entity_type)
        {
            continue;
        }

        // BFS queue: (current path, visited set)
        let mut queue: VecDeque<(GraphPath, HashSet<EntityId>)> = VecDeque::new();

        let initial_path = GraphPath::from_entity(start_entity);
        let mut initial_visited = HashSet::new();
        initial_visited.insert(start_id.clone());

        queue.push_back((initial_path, initial_visited));

        while let Some((current_path, visited)) = queue.pop_front() {
            // Add path if it meets criteria
            if current_path.total_confidence >= query.min_confidence {
                paths.push(current_path.clone());
            }

            // Stop if max hops reached
            if current_path.len() >= query.max_hops {
                continue;
            }

            // Get current entity ID
            let current_id = current_path.end().map(|e| e.id.clone()).unwrap_or_default();

            // Get neighbors based on direction
            let neighbors = store.get_neighbors(&current_id, query.direction).await?;

            for (relationship, neighbor) in neighbors {
                // Skip if already visited
                if visited.contains(&neighbor.id) {
                    continue;
                }

                // Apply relationship filter
                if let Some(ref filter) = query.relationship_filter
                    && !filter.contains(&relationship.relationship_type)
                {
                    continue;
                }

                // Apply entity filter
                if let Some(ref filter) = query.entity_filter
                    && !filter.contains(&neighbor.entity_type)
                {
                    continue;
                }

                // Create new path
                let mut new_path = current_path.clone();
                new_path.extend(relationship, neighbor.clone());

                // Check confidence threshold
                if new_path.total_confidence >= query.min_confidence {
                    // Mark as visited
                    let mut new_visited = visited.clone();
                    new_visited.insert(neighbor.id.clone());

                    queue.push_back((new_path, new_visited));
                }
            }
        }
    }

    // Sort paths by confidence (descending)
    paths.sort_by(|a, b| {
        b.total_confidence
            .partial_cmp(&a.total_confidence)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    Ok(paths)
}

/// Find shortest path between two entities using BFS.
///
/// # Errors
///
/// Returns an error if graph store operations fail.
pub async fn find_shortest_path<S: GraphStore + ?Sized>(
    store: &S,
    start_id: &EntityId,
    end_id: &EntityId,
    max_hops: usize,
    direction: Direction,
) -> Result<Option<GraphPath>, GraphError> {
    // Get the starting entity
    let Some(start_entity) = store.get_entity(start_id).await? else {
        return Ok(None);
    };

    // BFS queue
    let mut queue: VecDeque<(GraphPath, HashSet<EntityId>)> = VecDeque::new();

    let initial_path = GraphPath::from_entity(start_entity);
    let mut initial_visited = HashSet::new();
    initial_visited.insert(start_id.clone());

    queue.push_back((initial_path, initial_visited));

    while let Some((current_path, visited)) = queue.pop_front() {
        // Check if we've reached the target
        if let Some(end_entity) = current_path.end()
            && end_entity.id == *end_id
        {
            return Ok(Some(current_path));
        }

        // Stop if max hops reached
        if current_path.len() >= max_hops {
            continue;
        }

        // Get current entity ID
        let current_id = current_path.end().map(|e| e.id.clone()).unwrap_or_default();

        // Get neighbors
        let neighbors = store.get_neighbors(&current_id, direction).await?;

        for (relationship, neighbor) in neighbors {
            if visited.contains(&neighbor.id) {
                continue;
            }

            // Create new path
            let mut new_path = current_path.clone();
            new_path.extend(relationship, neighbor.clone());

            // Mark as visited
            let mut new_visited = visited.clone();
            new_visited.insert(neighbor.id.clone());

            queue.push_back((new_path, new_visited));
        }
    }

    Ok(None)
}

/// Find all entities within N hops from a starting entity.
///
/// # Errors
///
/// Returns an error if graph store operations fail.
pub async fn find_entities_within_hops<S: GraphStore + ?Sized>(
    store: &S,
    start_id: &EntityId,
    max_hops: usize,
    direction: Direction,
    entity_filter: Option<&[EntityType]>,
    relationship_filter: Option<&[RelationshipType]>,
) -> Result<Vec<(crate::layer4_graph::types::GraphEntity, usize)>, GraphError> {
    let mut results = Vec::new();
    let mut visited: HashSet<EntityId> = HashSet::new();

    // BFS queue: (entity_id, hop_count)
    let mut queue: VecDeque<(EntityId, usize)> = VecDeque::new();
    queue.push_back((start_id.clone(), 0));
    visited.insert(start_id.clone());

    while let Some((current_id, hops)) = queue.pop_front() {
        // Get the entity
        if let Some(entity) = store.get_entity(&current_id).await? {
            // Apply entity filter
            let include = entity_filter.is_none_or(|f| f.contains(&entity.entity_type));

            if include && current_id != *start_id {
                results.push((entity, hops));
            }
        }

        // Stop expanding if max hops reached
        if hops >= max_hops {
            continue;
        }

        // Get neighbors
        let neighbors = store.get_neighbors(&current_id, direction).await?;

        for (relationship, neighbor) in neighbors {
            if visited.contains(&neighbor.id) {
                continue;
            }

            // Apply relationship filter
            if let Some(filter) = relationship_filter
                && !filter.contains(&relationship.relationship_type)
            {
                continue;
            }

            visited.insert(neighbor.id.clone());
            queue.push_back((neighbor.id, hops + 1));
        }
    }

    Ok(results)
}

#[cfg(disabled)]
mod tests {
    use super::*;
    use crate::layer4_graph::memory::InMemoryGraphStore;
    use crate::layer4_graph::types::{GraphEntity, GraphRelationship, RelationshipType};

    async fn create_test_graph() -> InMemoryGraphStore {
        let mut store = InMemoryGraphStore::new();

        // Create entities: A -> B -> C -> D
        //                    \-> E
        store
            .add_entity(GraphEntity::new("A", EntityType::Concept).with_id("a"))
            .await
            .unwrap();
        store
            .add_entity(GraphEntity::new("B", EntityType::Concept).with_id("b"))
            .await
            .unwrap();
        store
            .add_entity(GraphEntity::new("C", EntityType::Concept).with_id("c"))
            .await
            .unwrap();
        store
            .add_entity(GraphEntity::new("D", EntityType::Concept).with_id("d"))
            .await
            .unwrap();
        store
            .add_entity(GraphEntity::new("E", EntityType::Technology).with_id("e"))
            .await
            .unwrap();

        store
            .add_relationship(GraphRelationship::new(
                "a",
                "b",
                RelationshipType::RelatedTo,
            ))
            .await
            .unwrap();
        store
            .add_relationship(GraphRelationship::new(
                "b",
                "c",
                RelationshipType::RelatedTo,
            ))
            .await
            .unwrap();
        store
            .add_relationship(GraphRelationship::new(
                "c",
                "d",
                RelationshipType::RelatedTo,
            ))
            .await
            .unwrap();
        store
            .add_relationship(GraphRelationship::new("a", "e", RelationshipType::Uses))
            .await
            .unwrap();

        store
    }

    #[tokio::test]
    async fn test_bfs_traverse_basic() {
        let store = create_test_graph().await;

        let query = GraphQuery::new(vec!["a".to_string()]).with_max_hops(2);

        let paths = bfs_traverse(&store, &query).await.unwrap();

        // Should find multiple paths from A
        assert!(!paths.is_empty());

        // Check that we can reach B, E, and C (but not D at 2 hops via B)
        let reached_ids: HashSet<_> = paths
            .iter()
            .filter_map(|p| p.end().map(|e| e.id.clone()))
            .collect();
        assert!(
            reached_ids.contains("a") || reached_ids.contains("b") || reached_ids.contains("e")
        );
    }

    #[tokio::test]
    async fn test_bfs_traverse_with_filter() {
        let store = create_test_graph().await;

        let query = GraphQuery::new(vec!["a".to_string()])
            .with_max_hops(3)
            .with_entity_filter(vec![EntityType::Technology]);

        let paths = bfs_traverse(&store, &query).await.unwrap();

        // Should only find paths that pass through Technology entities
        for path in &paths {
            for entity in &path.entities {
                if entity.id != "a" {
                    // Starting entity might not match filter
                    assert_eq!(entity.entity_type, EntityType::Technology);
                }
            }
        }
    }

    #[tokio::test]
    async fn test_bfs_traverse_with_relationship_filter() {
        let store = create_test_graph().await;

        let query = GraphQuery::new(vec!["a".to_string()])
            .with_max_hops(3)
            .with_relationship_filter(vec![RelationshipType::Uses]);

        let paths = bfs_traverse(&store, &query).await.unwrap();

        // Should only follow USES relationships
        for path in &paths {
            for rel in &path.relationships {
                assert_eq!(rel.relationship_type, RelationshipType::Uses);
            }
        }
    }

    #[tokio::test]
    async fn test_find_shortest_path() {
        let store = create_test_graph().await;

        // A -> B -> C -> D (3 hops)
        let path = find_shortest_path(
            &store,
            &"a".to_string(),
            &"d".to_string(),
            5,
            Direction::Outgoing,
        )
        .await
        .unwrap();

        assert!(path.is_some());
        let path = path.unwrap();
        assert_eq!(path.len(), 3); // 3 relationships
        assert_eq!(path.start().unwrap().id, "a");
        assert_eq!(path.end().unwrap().id, "d");
    }

    #[tokio::test]
    async fn test_find_shortest_path_not_found() {
        let store = create_test_graph().await;

        // E has no outgoing edges
        let path = find_shortest_path(
            &store,
            &"e".to_string(),
            &"d".to_string(),
            5,
            Direction::Outgoing,
        )
        .await
        .unwrap();

        assert!(path.is_none());
    }

    #[tokio::test]
    async fn test_find_entities_within_hops() {
        let store = create_test_graph().await;

        let entities =
            find_entities_within_hops(&store, &"a".to_string(), 2, Direction::Outgoing, None, None)
                .await
                .unwrap();

        // Should find B (1 hop), E (1 hop), C (2 hops)
        assert_eq!(entities.len(), 3);

        let ids: HashSet<_> = entities.iter().map(|(e, _)| e.id.clone()).collect();
        assert!(ids.contains("b"));
        assert!(ids.contains("e"));
        assert!(ids.contains("c"));
    }

    #[tokio::test]
    async fn test_find_entities_within_hops_with_filter() {
        let store = create_test_graph().await;

        let entities = find_entities_within_hops(
            &store,
            &"a".to_string(),
            3,
            Direction::Outgoing,
            Some(&[EntityType::Technology]),
            None,
        )
        .await
        .unwrap();

        // Should only find E (Technology)
        assert_eq!(entities.len(), 1);
        assert_eq!(entities[0].0.id, "e");
    }

    // Property-based tests
    #[cfg(disabled)]
    mod proptest_tests {
        use super::*;
        use proptest::prelude::*;

        // Helper to create a chain graph: 0 -> 1 -> 2 -> ... -> n
        async fn create_chain_graph(n: usize) -> InMemoryGraphStore {
            let mut store = InMemoryGraphStore::new();

            for i in 0..n {
                store
                    .add_entity(
                        GraphEntity::new(&format!("Entity {}", i), EntityType::Concept)
                            .with_id(&format!("{}", i)),
                    )
                    .await
                    .ok();
            }

            for i in 0..n - 1 {
                store
                    .add_relationship(GraphRelationship::new(
                        &format!("{}", i),
                        &format!("{}", i + 1),
                        RelationshipType::RelatedTo,
                    ))
                    .await
                    .ok();
            }

            store
        }

        proptest! {
            /// Shortest path length should equal hop count

            fn shortest_path_length_equals_hops(
                chain_length in 3usize..15,
                start_idx in 0usize..3,
                end_offset in 1usize..5
            ) {
                tokio_test::block_on(async {
                    let chain_length = chain_length.max(5);
                    let start_idx = start_idx.min(chain_length - 2);
                    let end_idx = (start_idx + end_offset).min(chain_length - 1);

                    let store = create_chain_graph(chain_length).await;

                    let path_result = find_shortest_path(
                        &store,
                        &format!("{}", start_idx),
                        &format!("{}", end_idx),
                        chain_length,
                        Direction::Outgoing,
                    )
                    .await;

                    if let Ok(Some(p)) = path_result {
                        let expected_hops = end_idx - start_idx;
                        prop_assert_eq!(p.len(), expected_hops,
                            "Path from {} to {} should have {} hops, got {}",
                            start_idx, end_idx, expected_hops, p.len());
                    }
                    Ok(())
                });
            }

            /// BFS should visit all reachable nodes within max_hops
            #[test]
            fn bfs_visits_all_reachable(
                chain_length in 3usize..12,
                max_hops in 1usize..8
            ) {
                tokio_test::block_on(async {
                    let store = create_chain_graph(chain_length).await;

                    let query = GraphQuery::new(vec!["0".to_string()]).with_max_hops(max_hops);

                    let paths = bfs_traverse(&store, &query).await.ok();

                    if let Some(paths) = paths {
                        // Collect all visited entities
                        let mut visited: std::collections::HashSet<String> = std::collections::HashSet::new();
                        for path in &paths {
                            for entity in &path.entities {
                                visited.insert(entity.id.clone());
                            }
                        }

                        // Should visit start node + nodes within max_hops
                        let expected_max = (max_hops + 1).min(chain_length);
                        prop_assert!(visited.len() <= expected_max,
                            "BFS visited {} nodes, expected at most {} (max_hops={}, chain_length={})",
                            visited.len(), expected_max, max_hops, chain_length);
                    }
                    Ok(())
                });
            }

            /// find_entities_within_hops respects hop limit
            #[test]
            fn entities_within_hops_respects_limit(
                chain_length in 4usize..12,
                max_hops in 1usize..6
            ) {
                tokio_test::block_on(async {
                    let store = create_chain_graph(chain_length).await;

                    let entities = find_entities_within_hops(
                        &store,
                        &"0".to_string(),
                        max_hops,
                        Direction::Outgoing,
                        None,
                        None,
                    )
                    .await
                    .ok();

                    if let Some(entities) = entities {
                        // All entities should be within max_hops
                        for (_, hops) in &entities {
                            prop_assert!(*hops <= max_hops,
                                "Entity found at {} hops, exceeds max_hops {}",
                                hops, max_hops);
                        }

                        // Should find at most max_hops entities (excluding start)
                        let expected_max = max_hops.min(chain_length - 1);
                        prop_assert!(entities.len() <= expected_max,
                            "Found {} entities, expected at most {} (max_hops={}, chain_length={})",
                            entities.len(), expected_max, max_hops, chain_length);
                    }
                    Ok(())
                });
            }

            /// Shortest path from node to itself should be empty or single node
            #[test]
            fn shortest_path_to_self(
                chain_length in 2usize..10,
                node_idx in 0usize..5
            ) {
                tokio_test::block_on(async {
                    let node_idx = node_idx.min(chain_length - 1);
                    let store = create_chain_graph(chain_length).await;

                    let node_id = format!("{}", node_idx);
                    let path_result = find_shortest_path(
                        &store,
                        &node_id,
                        &node_id,
                        10,
                        Direction::Outgoing,
                    )
                    .await;

                    // Path to self should exist and have 0 hops
                    if let Ok(Some(p)) = path_result {
                        prop_assert_eq!(p.len(), 0,
                            "Path from node to itself should have 0 hops, got {}",
                            p.len());
                    }
                    Ok(())
                });
            }

            /// All paths returned by BFS should satisfy min_confidence
            #[test]
            fn bfs_respects_min_confidence(
                chain_length in 3usize..8,
                min_confidence in 0.0f32..0.9
            ) {
                tokio_test::block_on(async {
                    let store = create_chain_graph(chain_length).await;

                    let query = GraphQuery::new(vec!["0".to_string()])
                        .with_max_hops(chain_length)
                        .with_min_confidence(min_confidence);

                    let paths = bfs_traverse(&store, &query).await.ok();

                    if let Some(paths) = paths {
                        for path in &paths {
                            prop_assert!(path.total_confidence >= min_confidence,
                                "Path has confidence {}, below min {}",
                                path.total_confidence, min_confidence);
                        }
                    }
                    Ok(())
                });
            }

            /// Paths should not contain cycles (duplicate entities)
            #[test]
            fn paths_have_no_cycles(
                chain_length in 3usize..10,
                max_hops in 2usize..6
            ) {
                tokio_test::block_on(async {
                    let store = create_chain_graph(chain_length).await;

                    let query = GraphQuery::new(vec!["0".to_string()]).with_max_hops(max_hops);

                    let paths = bfs_traverse(&store, &query).await.ok();

                    if let Some(paths) = paths {
                        for path in &paths {
                            let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
                            for entity in &path.entities {
                                prop_assert!(!seen.contains(&entity.id),
                                    "Path contains duplicate entity: {}", entity.id);
                                seen.insert(entity.id.clone());
                            }
                        }
                    }
                    Ok(())
                });
            }

            /// find_entities_within_hops returns entities sorted by hop distance
            #[test]
            fn entities_within_hops_sorted_by_distance(
                chain_length in 4usize..10
            ) {
                tokio_test::block_on(async {
                    let store = create_chain_graph(chain_length).await;

                    let entities = find_entities_within_hops(
                        &store,
                        &"0".to_string(),
                        chain_length - 1,
                        Direction::Outgoing,
                        None,
                        None,
                    )
                    .await
                    .ok();

                    if let Some(entities) = entities {
                        // Check that hop distances are valid
                        for (entity, hops) in &entities {
                            // In a chain, entity ID is the index, so hop count should match
                            if let Ok(entity_idx) = entity.id.parse::<usize>() {
                                prop_assert_eq!(*hops, entity_idx,
                                    "Entity {} should be at {} hops, found at {}",
                                    entity.id, entity_idx, hops);
                            }
                        }
                    }
                    Ok(())
                });
            }
        }
    }
}