engram-core 0.19.0

AI Memory Infrastructure - Persistent memory for AI agents with semantic search
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
//! Multi-hop graph traversal queries
//!
//! Provides graph traversal capabilities for exploring memory relationships
//! at various depths, with support for filtering by edge type and combining
//! with entity-based connections.

use rusqlite::Connection;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};

use crate::error::Result;
use crate::types::{CrossReference, EdgeType, MemoryId, RelationSource};
use chrono::{DateTime, Utc};

/// Options for multi-hop graph traversal
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraversalOptions {
    /// Maximum traversal depth (1 = direct relations only)
    #[serde(default = "default_depth")]
    pub depth: usize,
    /// Filter by edge types (empty = all types)
    #[serde(default)]
    pub edge_types: Vec<EdgeType>,
    /// Minimum score threshold
    #[serde(default)]
    pub min_score: f32,
    /// Minimum confidence threshold
    #[serde(default)]
    pub min_confidence: f32,
    /// Maximum number of results per hop
    #[serde(default = "default_limit_per_hop")]
    pub limit_per_hop: usize,
    /// Include entity-based connections
    #[serde(default = "default_include_entities")]
    pub include_entities: bool,
    /// Direction of traversal
    #[serde(default)]
    pub direction: TraversalDirection,
}

fn default_depth() -> usize {
    2
}

fn default_limit_per_hop() -> usize {
    50
}

fn default_include_entities() -> bool {
    true
}

impl Default for TraversalOptions {
    fn default() -> Self {
        Self {
            depth: 2,
            edge_types: vec![],
            min_score: 0.0,
            min_confidence: 0.0,
            limit_per_hop: 50,
            include_entities: true,
            direction: TraversalDirection::Both,
        }
    }
}

/// Direction of graph traversal
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum TraversalDirection {
    /// Follow outgoing edges only (from -> to)
    Outgoing,
    /// Follow incoming edges only (to -> from)
    Incoming,
    /// Follow edges in both directions
    #[default]
    Both,
}

/// A node in the traversal result with path information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraversalNode {
    /// Memory ID
    pub memory_id: MemoryId,
    /// Depth from the starting node (0 = start node)
    pub depth: usize,
    /// Path of memory IDs from start to this node
    pub path: Vec<MemoryId>,
    /// Edge types along the path
    pub edge_path: Vec<String>,
    /// Cumulative score (product of edge scores)
    pub cumulative_score: f32,
    /// How this node was reached
    pub connection_type: ConnectionType,
}

/// How a node was reached during traversal
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ConnectionType {
    /// Starting node
    Origin,
    /// Connected via cross-reference edge
    CrossReference,
    /// Connected via shared entity
    SharedEntity { entity_name: String },
}

/// Result of a multi-hop traversal
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraversalResult {
    /// Starting memory ID
    pub start_id: MemoryId,
    /// All nodes found during traversal
    pub nodes: Vec<TraversalNode>,
    /// Edges that led to newly discovered nodes
    pub discovery_edges: Vec<CrossReference>,
    /// Statistics about the traversal
    pub stats: TraversalStats,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TraversalStats {
    /// Total nodes visited
    pub nodes_visited: usize,
    /// Nodes at each depth level
    pub nodes_per_depth: HashMap<usize, usize>,
    /// Count by connection type
    pub connection_type_counts: HashMap<String, usize>,
    /// Maximum depth reached
    pub max_depth_reached: usize,
}

/// Get related memories with multi-hop traversal
pub fn get_related_multi_hop(
    conn: &Connection,
    start_id: MemoryId,
    options: &TraversalOptions,
) -> Result<TraversalResult> {
    let mut visited: HashSet<MemoryId> = HashSet::new();
    let mut nodes: Vec<TraversalNode> = Vec::new();
    let mut discovery_edges: Vec<CrossReference> = Vec::new();
    let mut stats = TraversalStats::default();

    // Queue: (memory_id, depth, path, edge_path, cumulative_score)
    let mut queue: VecDeque<(MemoryId, usize, Vec<MemoryId>, Vec<String>, f32)> = VecDeque::new();

    // Start with the origin node
    visited.insert(start_id);
    nodes.push(TraversalNode {
        memory_id: start_id,
        depth: 0,
        path: vec![start_id],
        edge_path: vec![],
        cumulative_score: 1.0,
        connection_type: ConnectionType::Origin,
    });
    queue.push_back((start_id, 0, vec![start_id], vec![], 1.0));

    *stats.nodes_per_depth.entry(0).or_insert(0) += 1;
    *stats
        .connection_type_counts
        .entry("origin".to_string())
        .or_insert(0) += 1;

    // Level-based BFS traversal
    while !queue.is_empty() {
        let level_size = queue.len();
        let mut current_batch = Vec::with_capacity(level_size);
        for _ in 0..level_size {
            if let Some(item) = queue.pop_front() {
                current_batch.push(item);
            }
        }

        if current_batch.is_empty() {
            break;
        }

        // All nodes in this batch should be at the same depth
        let current_depth = current_batch[0].1;

        if current_depth >= options.depth {
            continue;
        }

        let node_ids: Vec<MemoryId> = current_batch.iter().map(|(id, _, _, _, _)| *id).collect();

        // Batch fetch cross-reference edges (with SQL-level per-node limiting)
        let crossrefs_map = get_edges_for_traversal_batch(
            conn,
            &node_ids,
            &options.edge_types,
            options.min_score,
            options.min_confidence,
            options.direction,
            options.limit_per_hop,
        )?;

        // Batch fetch entity-based connections if enabled
        let entity_connections_map = if options.include_entities {
            get_entity_connections_batch(conn, &node_ids, options.limit_per_hop)?
        } else {
            HashMap::new()
        };

        // Process each node in the batch
        for (current_id, _current_depth, current_path, current_edge_path, current_score) in
            current_batch
        {
            // Process cross-references (already limited per-node in SQL)
            if let Some(crossrefs) = crossrefs_map.get(&current_id) {
                for crossref in crossrefs.iter() {
                    // Determine the neighbor ID based on direction
                    let neighbor_id = if crossref.from_id == current_id {
                        crossref.to_id
                    } else {
                        crossref.from_id
                    };

                    if visited.contains(&neighbor_id) {
                        continue;
                    }

                    visited.insert(neighbor_id);

                    let mut new_path = current_path.clone();
                    new_path.push(neighbor_id);

                    let mut new_edge_path = current_edge_path.clone();
                    new_edge_path.push(crossref.edge_type.as_str().to_string());

                    let new_score = current_score * crossref.score * crossref.confidence;
                    let new_depth = current_depth + 1;

                    nodes.push(TraversalNode {
                        memory_id: neighbor_id,
                        depth: new_depth,
                        path: new_path.clone(),
                        edge_path: new_edge_path.clone(),
                        cumulative_score: new_score,
                        connection_type: ConnectionType::CrossReference,
                    });

                    discovery_edges.push(crossref.clone());

                    *stats.nodes_per_depth.entry(new_depth).or_insert(0) += 1;
                    *stats
                        .connection_type_counts
                        .entry("cross_reference".to_string())
                        .or_insert(0) += 1;

                    if new_depth < options.depth {
                        queue.push_back((
                            neighbor_id,
                            new_depth,
                            new_path,
                            new_edge_path,
                            new_score,
                        ));
                    }

                    stats.max_depth_reached = stats.max_depth_reached.max(new_depth);
                }
            }

            // Process entity connections
            if let Some(entity_connections) = entity_connections_map.get(&current_id) {
                for (neighbor_id, entity_name) in
                    entity_connections.iter().take(options.limit_per_hop)
                {
                    let neighbor_id = *neighbor_id;
                    if visited.contains(&neighbor_id) {
                        continue;
                    }

                    visited.insert(neighbor_id);

                    let mut new_path = current_path.clone();
                    new_path.push(neighbor_id);

                    let mut new_edge_path = current_edge_path.clone();
                    new_edge_path.push(format!("entity:{}", entity_name));

                    let new_depth = current_depth + 1;
                    // Entity connections get a base score of 0.5
                    let new_score = current_score * 0.5;

                    nodes.push(TraversalNode {
                        memory_id: neighbor_id,
                        depth: new_depth,
                        path: new_path.clone(),
                        edge_path: new_edge_path.clone(),
                        cumulative_score: new_score,
                        connection_type: ConnectionType::SharedEntity {
                            entity_name: entity_name.clone(),
                        },
                    });

                    *stats.nodes_per_depth.entry(new_depth).or_insert(0) += 1;
                    *stats
                        .connection_type_counts
                        .entry("shared_entity".to_string())
                        .or_insert(0) += 1;

                    if new_depth < options.depth {
                        queue.push_back((
                            neighbor_id,
                            new_depth,
                            new_path,
                            new_edge_path,
                            new_score,
                        ));
                    }

                    stats.max_depth_reached = stats.max_depth_reached.max(new_depth);
                }
            }
        }
    }

    stats.nodes_visited = nodes.len();

    Ok(TraversalResult {
        start_id,
        nodes,
        discovery_edges,
        stats,
    })
}

/// Get edges for multiple memory IDs with per-node SQL limiting
///
/// Uses ROW_NUMBER() window function to limit results per source node in SQL,
/// preventing memory/time blowup on high-degree nodes.
fn get_edges_for_traversal_batch(
    conn: &Connection,
    memory_ids: &[MemoryId],
    edge_types: &[EdgeType],
    min_score: f32,
    min_confidence: f32,
    direction: TraversalDirection,
    limit_per_node: usize,
) -> Result<HashMap<MemoryId, Vec<CrossReference>>> {
    if memory_ids.is_empty() {
        return Ok(HashMap::new());
    }

    let mut result: HashMap<MemoryId, Vec<CrossReference>> = HashMap::new();
    let id_set: HashSet<MemoryId> = memory_ids.iter().cloned().collect();

    // SQLite limit safety: chunk the IDs
    for chunk in memory_ids.chunks(100) {
        let placeholders = chunk.iter().map(|_| "?").collect::<Vec<_>>().join(", ");

        let edge_type_clause = if edge_types.is_empty() {
            String::new()
        } else {
            let types: Vec<String> = edge_types
                .iter()
                .map(|e| format!("'{}'", e.as_str()))
                .collect();
            format!(" AND edge_type IN ({})", types.join(", "))
        };

        // Build query based on direction, using ROW_NUMBER() to limit per source node
        let (partition_col, filter_clause) = match direction {
            TraversalDirection::Outgoing => ("from_id", format!("from_id IN ({})", placeholders)),
            TraversalDirection::Incoming => ("to_id", format!("to_id IN ({})", placeholders)),
            TraversalDirection::Both => {
                // For Both direction, we need a UNION approach to properly partition
                // by source node from both directions
                let query = format!(
                    r#"
                    WITH ranked_edges AS (
                        SELECT *, ROW_NUMBER() OVER (
                            PARTITION BY from_id ORDER BY score * confidence DESC
                        ) as rn
                        FROM crossrefs
                        WHERE from_id IN ({placeholders}) AND valid_to IS NULL
                          AND score >= ? AND confidence >= ?
                          {edge_type_clause}
                        UNION ALL
                        SELECT *, ROW_NUMBER() OVER (
                            PARTITION BY to_id ORDER BY score * confidence DESC
                        ) as rn
                        FROM crossrefs
                        WHERE to_id IN ({placeholders}) AND from_id NOT IN ({placeholders}) AND valid_to IS NULL
                          AND score >= ? AND confidence >= ?
                          {edge_type_clause}
                    )
                    SELECT from_id, to_id, edge_type, score, confidence, strength, source,
                           source_context, created_at, valid_from, valid_to, pinned, metadata
                    FROM ranked_edges
                    WHERE rn <= ?
                    "#,
                    placeholders = placeholders,
                    edge_type_clause = edge_type_clause,
                );

                let mut stmt = conn.prepare(&query)?;
                let mut params: Vec<Box<dyn rusqlite::ToSql>> = Vec::new();

                // First subquery params: from_id IN, min_score, min_confidence
                for id in chunk {
                    params.push(Box::new(*id));
                }
                params.push(Box::new(min_score));
                params.push(Box::new(min_confidence));

                // Second subquery params: to_id IN, from_id NOT IN, min_score, min_confidence
                for id in chunk {
                    params.push(Box::new(*id));
                }
                for id in chunk {
                    params.push(Box::new(*id));
                }
                params.push(Box::new(min_score));
                params.push(Box::new(min_confidence));

                // Limit param
                params.push(Box::new(limit_per_node as i64));

                let param_refs: Vec<&dyn rusqlite::ToSql> =
                    params.iter().map(|p| p.as_ref()).collect();

                let crossrefs = stmt
                    .query_map(param_refs.as_slice(), crossref_from_row)?
                    .filter_map(|r| r.ok());

                for crossref in crossrefs {
                    if id_set.contains(&crossref.from_id) {
                        result
                            .entry(crossref.from_id)
                            .or_default()
                            .push(crossref.clone());
                    }
                    if id_set.contains(&crossref.to_id) && crossref.from_id != crossref.to_id {
                        result.entry(crossref.to_id).or_default().push(crossref);
                    }
                }

                continue; // Skip the common path below for Both direction
            }
        };

        // Common path for Outgoing and Incoming directions
        let query = format!(
            r#"
            WITH ranked_edges AS (
                SELECT *, ROW_NUMBER() OVER (
                    PARTITION BY {partition_col} ORDER BY score * confidence DESC
                ) as rn
                FROM crossrefs
                WHERE {filter_clause} AND valid_to IS NULL
                  AND score >= ? AND confidence >= ?
                  {edge_type_clause}
            )
            SELECT from_id, to_id, edge_type, score, confidence, strength, source,
                   source_context, created_at, valid_from, valid_to, pinned, metadata
            FROM ranked_edges
            WHERE rn <= ?
            "#,
            partition_col = partition_col,
            filter_clause = filter_clause,
            edge_type_clause = edge_type_clause,
        );

        let mut stmt = conn.prepare(&query)?;

        let mut params: Vec<Box<dyn rusqlite::ToSql>> = Vec::new();
        for id in chunk {
            params.push(Box::new(*id));
        }
        params.push(Box::new(min_score));
        params.push(Box::new(min_confidence));
        params.push(Box::new(limit_per_node as i64));

        let param_refs: Vec<&dyn rusqlite::ToSql> = params.iter().map(|p| p.as_ref()).collect();

        let crossrefs = stmt
            .query_map(param_refs.as_slice(), crossref_from_row)?
            .filter_map(|r| r.ok());

        for crossref in crossrefs {
            match direction {
                TraversalDirection::Outgoing => {
                    if id_set.contains(&crossref.from_id) {
                        result.entry(crossref.from_id).or_default().push(crossref);
                    }
                }
                TraversalDirection::Incoming => {
                    if id_set.contains(&crossref.to_id) {
                        result.entry(crossref.to_id).or_default().push(crossref);
                    }
                }
                TraversalDirection::Both => unreachable!(), // Handled above with continue
            }
        }
    }

    Ok(result)
}

/// Get memories connected through shared entities for multiple memory IDs
fn get_entity_connections_batch(
    conn: &Connection,
    memory_ids: &[MemoryId],
    _limit: usize,
) -> Result<HashMap<MemoryId, Vec<(MemoryId, String)>>> {
    if memory_ids.is_empty() {
        return Ok(HashMap::new());
    }

    let mut result: HashMap<MemoryId, Vec<(MemoryId, String)>> = HashMap::new();
    let id_set: HashSet<MemoryId> = memory_ids.iter().cloned().collect();

    for chunk in memory_ids.chunks(100) {
        let placeholders = chunk.iter().map(|_| "?").collect::<Vec<_>>().join(", ");

        let query = format!(
            r#"
            SELECT DISTINCT me1.memory_id, me2.memory_id, e.name
            FROM memory_entities me1
            JOIN memory_entities me2 ON me1.entity_id = me2.entity_id
            JOIN entities e ON me1.entity_id = e.id
            WHERE me1.memory_id IN ({}) AND me2.memory_id != me1.memory_id
            ORDER BY e.mention_count DESC
            "#,
            placeholders
        );

        let mut stmt = conn.prepare(&query)?;

        let mut params: Vec<Box<dyn rusqlite::ToSql>> = Vec::new();
        for id in chunk {
            params.push(Box::new(*id));
        }

        let param_refs: Vec<&dyn rusqlite::ToSql> = params.iter().map(|p| p.as_ref()).collect();

        let rows = stmt
            .query_map(param_refs.as_slice(), |row| {
                Ok((
                    row.get::<_, i64>(0)?,
                    row.get::<_, i64>(1)?,
                    row.get::<_, String>(2)?,
                ))
            })?
            .filter_map(|r| r.ok());

        for (source_id, target_id, entity_name) in rows {
            if id_set.contains(&source_id) {
                result
                    .entry(source_id)
                    .or_default()
                    .push((target_id, entity_name));
            }
        }
    }

    Ok(result)
}

/// Helper to parse CrossReference from row
fn crossref_from_row(row: &rusqlite::Row) -> rusqlite::Result<CrossReference> {
    let edge_type_str: String = row.get("edge_type")?;
    let source_str: String = row.get("source")?;
    let created_at_str: String = row.get("created_at")?;
    let valid_from_str: String = row.get("valid_from")?;
    let valid_to_str: Option<String> = row.get("valid_to")?;
    let metadata_str: String = row.get("metadata")?;

    Ok(CrossReference {
        from_id: row.get("from_id")?,
        to_id: row.get("to_id")?,
        edge_type: edge_type_str.parse().unwrap_or(EdgeType::RelatedTo),
        score: row.get("score")?,
        confidence: row.get("confidence")?,
        strength: row.get("strength")?,
        source: match source_str.as_str() {
            "manual" => RelationSource::Manual,
            "llm" => RelationSource::Llm,
            _ => RelationSource::Auto,
        },
        source_context: row.get("source_context")?,
        created_at: DateTime::parse_from_rfc3339(&created_at_str)
            .map(|dt| dt.with_timezone(&Utc))
            .unwrap_or_else(|_| Utc::now()),
        valid_from: DateTime::parse_from_rfc3339(&valid_from_str)
            .map(|dt| dt.with_timezone(&Utc))
            .unwrap_or_else(|_| Utc::now()),
        valid_to: valid_to_str.and_then(|s| {
            DateTime::parse_from_rfc3339(&s)
                .map(|dt| dt.with_timezone(&Utc))
                .ok()
        }),
        pinned: row.get::<_, i32>("pinned")? != 0,
        metadata: serde_json::from_str(&metadata_str).unwrap_or_default(),
    })
}

/// Find shortest path between two memories
pub fn find_path(
    conn: &Connection,
    from_id: MemoryId,
    to_id: MemoryId,
    max_depth: usize,
) -> Result<Option<TraversalNode>> {
    let options = TraversalOptions {
        depth: max_depth,
        include_entities: true,
        ..Default::default()
    };

    let result = get_related_multi_hop(conn, from_id, &options)?;

    // Find the target node in results
    Ok(result.nodes.into_iter().find(|n| n.memory_id == to_id))
}

/// Get all memories within a certain graph distance
pub fn get_neighborhood(
    conn: &Connection,
    center_id: MemoryId,
    radius: usize,
) -> Result<Vec<MemoryId>> {
    let options = TraversalOptions {
        depth: radius,
        include_entities: true,
        ..Default::default()
    };

    let result = get_related_multi_hop(conn, center_id, &options)?;

    Ok(result.nodes.into_iter().map(|n| n.memory_id).collect())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::intelligence::entities::{EntityRelation, EntityType, ExtractedEntity};
    use crate::storage::entity_queries::{link_entity_to_memory, upsert_entity};
    use crate::storage::queries::{create_crossref, create_memory};
    use crate::storage::Storage;
    use crate::types::{CreateCrossRefInput, CreateMemoryInput, MemoryType};

    fn create_test_memory(conn: &Connection, content: &str) -> MemoryId {
        let input = CreateMemoryInput {
            content: content.to_string(),
            memory_type: MemoryType::Note,
            tags: vec![],
            importance: None,
            metadata: Default::default(),
            scope: Default::default(),
            workspace: None,
            tier: Default::default(),
            defer_embedding: false,
            ttl_seconds: None,
            dedup_mode: Default::default(),
            dedup_threshold: None,
            event_time: None,
            event_duration_seconds: None,
            trigger_pattern: None,
            summary_of_id: None,
                media_url: None,
        };
        create_memory(conn, &input).unwrap().id
    }

    fn create_test_crossref(
        conn: &Connection,
        from_id: MemoryId,
        to_id: MemoryId,
        edge_type: EdgeType,
    ) -> crate::error::Result<()> {
        let input = CreateCrossRefInput {
            from_id,
            to_id,
            edge_type,
            strength: None,
            source_context: None,
            pinned: false,
        };
        create_crossref(conn, &input)?;
        Ok(())
    }

    #[test]
    fn test_multi_hop_traversal() {
        let storage = Storage::open_in_memory().unwrap();
        storage
            .with_transaction(|conn| {
                // Create a chain: A -> B -> C -> D
                let id_a = create_test_memory(conn, "Memory A");
                let id_b = create_test_memory(conn, "Memory B");
                let id_c = create_test_memory(conn, "Memory C");
                let id_d = create_test_memory(conn, "Memory D");

                // Create edges
                create_test_crossref(conn, id_a, id_b, EdgeType::RelatedTo)?;
                create_test_crossref(conn, id_b, id_c, EdgeType::RelatedTo)?;
                create_test_crossref(conn, id_c, id_d, EdgeType::RelatedTo)?;

                // Traverse from A with depth 1 - should only reach B
                let options = TraversalOptions {
                    depth: 1,
                    include_entities: false,
                    ..Default::default()
                };
                let result = get_related_multi_hop(conn, id_a, &options)?;
                assert_eq!(result.nodes.len(), 2); // A + B
                assert!(result.nodes.iter().any(|n| n.memory_id == id_a));
                assert!(result.nodes.iter().any(|n| n.memory_id == id_b));

                // Traverse from A with depth 2 - should reach B and C
                let options = TraversalOptions {
                    depth: 2,
                    include_entities: false,
                    ..Default::default()
                };
                let result = get_related_multi_hop(conn, id_a, &options)?;
                assert_eq!(result.nodes.len(), 3); // A + B + C
                assert!(result.nodes.iter().any(|n| n.memory_id == id_c));

                // Traverse from A with depth 3 - should reach all
                let options = TraversalOptions {
                    depth: 3,
                    include_entities: false,
                    ..Default::default()
                };
                let result = get_related_multi_hop(conn, id_a, &options)?;
                assert_eq!(result.nodes.len(), 4); // A + B + C + D

                Ok(())
            })
            .unwrap();
    }

    #[test]
    fn test_entity_based_connections() {
        let storage = Storage::open_in_memory().unwrap();
        storage
            .with_transaction(|conn| {
                // Create memories
                let id_a = create_test_memory(conn, "Memory about Rust programming");
                let id_b = create_test_memory(conn, "Another memory about Rust");
                let id_c = create_test_memory(conn, "Memory about Python");

                // Create shared entity using ExtractedEntity
                let entity = ExtractedEntity {
                    text: "Rust".to_string(),
                    normalized: "rust".to_string(),
                    entity_type: EntityType::Concept,
                    confidence: 0.9,
                    offset: 0,
                    length: 4,
                    suggested_relation: EntityRelation::Mentions,
                };
                let entity_id = upsert_entity(conn, &entity)?;
                let _ = link_entity_to_memory(
                    conn,
                    id_a,
                    entity_id,
                    EntityRelation::Mentions,
                    0.9,
                    None,
                )?;
                let _ = link_entity_to_memory(
                    conn,
                    id_b,
                    entity_id,
                    EntityRelation::Mentions,
                    0.8,
                    None,
                )?;

                // Traverse from A with entities enabled
                let options = TraversalOptions {
                    depth: 1,
                    include_entities: true,
                    ..Default::default()
                };
                let result = get_related_multi_hop(conn, id_a, &options)?;

                // Should find B through shared entity
                assert!(result.nodes.iter().any(|n| n.memory_id == id_b));
                let b_node = result.nodes.iter().find(|n| n.memory_id == id_b).unwrap();
                assert!(matches!(
                    &b_node.connection_type,
                    ConnectionType::SharedEntity { entity_name } if entity_name == "Rust"
                ));

                // Should NOT find C (no shared entity)
                assert!(!result.nodes.iter().any(|n| n.memory_id == id_c));

                Ok(())
            })
            .unwrap();
    }

    #[test]
    fn test_find_path() {
        let storage = Storage::open_in_memory().unwrap();
        storage
            .with_transaction(|conn| {
                let id_a = create_test_memory(conn, "Start");
                let id_b = create_test_memory(conn, "Middle");
                let id_c = create_test_memory(conn, "End");

                create_test_crossref(conn, id_a, id_b, EdgeType::RelatedTo)?;
                create_test_crossref(conn, id_b, id_c, EdgeType::DependsOn)?;

                let path = find_path(conn, id_a, id_c, 5)?;
                assert!(path.is_some());
                let path = path.unwrap();
                assert_eq!(path.memory_id, id_c);
                assert_eq!(path.depth, 2);
                assert_eq!(path.path.len(), 3);
                assert_eq!(path.path, vec![id_a, id_b, id_c]);

                Ok(())
            })
            .unwrap();
    }

    #[test]
    fn test_traversal_direction() {
        let storage = Storage::open_in_memory().unwrap();
        storage
            .with_transaction(|conn| {
                let id_a = create_test_memory(conn, "A");
                let id_b = create_test_memory(conn, "B");
                let id_c = create_test_memory(conn, "C");

                // A -> B and C -> B (B has incoming from both)
                create_test_crossref(conn, id_a, id_b, EdgeType::RelatedTo)?;
                create_test_crossref(conn, id_c, id_b, EdgeType::RelatedTo)?;

                // Outgoing from B - should find nothing (B has no outgoing)
                let options = TraversalOptions {
                    depth: 1,
                    direction: TraversalDirection::Outgoing,
                    include_entities: false,
                    ..Default::default()
                };
                let result = get_related_multi_hop(conn, id_b, &options)?;
                assert_eq!(result.nodes.len(), 1); // Just B itself

                // Incoming to B - should find A and C
                let options = TraversalOptions {
                    depth: 1,
                    direction: TraversalDirection::Incoming,
                    include_entities: false,
                    ..Default::default()
                };
                let result = get_related_multi_hop(conn, id_b, &options)?;
                assert_eq!(result.nodes.len(), 3); // B, A, C

                Ok(())
            })
            .unwrap();
    }

    #[test]
    fn test_edge_type_filter() {
        let storage = Storage::open_in_memory().unwrap();
        storage
            .with_transaction(|conn| {
                let id_a = create_test_memory(conn, "A");
                let id_b = create_test_memory(conn, "B");
                let id_c = create_test_memory(conn, "C");

                create_test_crossref(conn, id_a, id_b, EdgeType::RelatedTo)?;
                create_test_crossref(conn, id_a, id_c, EdgeType::DependsOn)?;

                // Filter to only RelatedTo edges
                let options = TraversalOptions {
                    depth: 1,
                    edge_types: vec![EdgeType::RelatedTo],
                    include_entities: false,
                    ..Default::default()
                };
                let result = get_related_multi_hop(conn, id_a, &options)?;
                assert_eq!(result.nodes.len(), 2); // A + B only
                assert!(result.nodes.iter().any(|n| n.memory_id == id_b));
                assert!(!result.nodes.iter().any(|n| n.memory_id == id_c));

                Ok(())
            })
            .unwrap();
    }
}