engram-core 0.17.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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
//! Graph Conflict Detection & Resolution (RML-1217)
//!
//! Inspired by Mem0's approach to managing conflicting knowledge in graphs.
//! Provides:
//! - Detection of four conflict types: direct contradictions, temporal inconsistencies,
//!   cyclic dependencies, and orphaned references.
//! - Resolution strategies: keep newer, keep higher confidence, merge, or manual.
//! - Persistence of detected conflicts in the `graph_conflicts` table.

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

use crate::error::{EngramError, Result};

// =============================================================================
// DDL
// =============================================================================

/// SQL that creates the `graph_conflicts` table.
///
/// Safe to run on an existing database — uses `IF NOT EXISTS`.
pub const CREATE_CONFLICTS_TABLE: &str = r#"
CREATE TABLE IF NOT EXISTS graph_conflicts (
    id                  INTEGER PRIMARY KEY AUTOINCREMENT,
    conflict_type       TEXT    NOT NULL,
    edge_ids            TEXT    NOT NULL DEFAULT '[]',
    description         TEXT    NOT NULL,
    severity            TEXT    NOT NULL,
    resolved_at         TEXT,
    resolution_strategy TEXT,
    created_at          TEXT    NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
);
CREATE INDEX IF NOT EXISTS idx_graph_conflicts_type     ON graph_conflicts(conflict_type);
CREATE INDEX IF NOT EXISTS idx_graph_conflicts_severity ON graph_conflicts(severity);
CREATE INDEX IF NOT EXISTS idx_graph_conflicts_resolved ON graph_conflicts(resolved_at);
"#;

// =============================================================================
// Types
// =============================================================================

/// The category of graph conflict.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConflictType {
    /// Two edges between the same pair of nodes carry contradicting relation
    /// types (e.g. "supports" AND "contradicts" for the same A→B pair).
    DirectContradiction,
    /// Two or more edges for the same entity pair have overlapping validity
    /// periods, indicating a temporal inconsistency.
    TemporalInconsistency,
    /// A cycle exists in the directed edge graph (A→B→C→A).
    CyclicDependency,
    /// An edge references a `from_id` or `to_id` that does not exist in the
    /// `memories` table.
    OrphanedReference,
}

impl ConflictType {
    fn as_str(&self) -> &'static str {
        match self {
            ConflictType::DirectContradiction => "direct_contradiction",
            ConflictType::TemporalInconsistency => "temporal_inconsistency",
            ConflictType::CyclicDependency => "cyclic_dependency",
            ConflictType::OrphanedReference => "orphaned_reference",
        }
    }

    fn from_str(s: &str) -> Option<Self> {
        match s {
            "direct_contradiction" => Some(ConflictType::DirectContradiction),
            "temporal_inconsistency" => Some(ConflictType::TemporalInconsistency),
            "cyclic_dependency" => Some(ConflictType::CyclicDependency),
            "orphaned_reference" => Some(ConflictType::OrphanedReference),
            _ => None,
        }
    }
}

/// Severity level of a detected conflict.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Severity {
    Low,
    Medium,
    High,
    Critical,
}

impl Severity {
    fn as_str(&self) -> &'static str {
        match self {
            Severity::Low => "low",
            Severity::Medium => "medium",
            Severity::High => "high",
            Severity::Critical => "critical",
        }
    }

    fn from_str(s: &str) -> Option<Self> {
        match s {
            "low" => Some(Severity::Low),
            "medium" => Some(Severity::Medium),
            "high" => Some(Severity::High),
            "critical" => Some(Severity::Critical),
            _ => None,
        }
    }
}

/// A detected conflict in the knowledge graph.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Conflict {
    /// Unique row identifier (`0` for unsaved conflicts).
    pub id: i64,
    /// Category of conflict.
    pub conflict_type: ConflictType,
    /// IDs of the edges involved in this conflict.
    pub edge_ids: Vec<i64>,
    /// Human-readable description of the conflict.
    pub description: String,
    /// How severe this conflict is.
    pub severity: Severity,
    /// When the conflict was resolved (`None` = unresolved).
    pub resolved_at: Option<String>,
    /// Which strategy was used to resolve this conflict.
    pub resolution_strategy: Option<ResolutionStrategy>,
}

/// Strategy to apply when resolving a conflict.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ResolutionStrategy {
    /// Remove all but the most recently created edge.
    KeepNewer,
    /// Remove all but the edge with the highest confidence / importance proxy.
    KeepHigherConfidence,
    /// Merge edge metadata into a single edge.
    Merge,
    /// Mark the conflict resolved without modifying any edges.
    Manual,
}

impl ResolutionStrategy {
    fn as_str(&self) -> &'static str {
        match self {
            ResolutionStrategy::KeepNewer => "keep_newer",
            ResolutionStrategy::KeepHigherConfidence => "keep_higher_confidence",
            ResolutionStrategy::Merge => "merge",
            ResolutionStrategy::Manual => "manual",
        }
    }

    fn from_str(s: &str) -> Option<Self> {
        match s {
            "keep_newer" => Some(ResolutionStrategy::KeepNewer),
            "keep_higher_confidence" => Some(ResolutionStrategy::KeepHigherConfidence),
            "merge" => Some(ResolutionStrategy::Merge),
            "manual" => Some(ResolutionStrategy::Manual),
            _ => None,
        }
    }
}

/// Outcome of resolving a conflict.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResolutionResult {
    /// The conflict that was resolved.
    pub conflict_id: i64,
    /// Strategy that was applied.
    pub strategy: ResolutionStrategy,
    /// Edge IDs that were deleted during resolution.
    pub edges_removed: Vec<i64>,
    /// Edge IDs that were kept during resolution.
    pub edges_kept: Vec<i64>,
}

// =============================================================================
// ConflictDetector
// =============================================================================

/// Detects conflicts in the `cross_references` graph.
pub struct ConflictDetector;

/// Pairs of relation types that are considered direct contradictions.
const CONTRADICTING_PAIRS: &[(&str, &str)] = &[
    ("supports", "contradicts"),
    ("agrees_with", "disagrees_with"),
    ("confirms", "refutes"),
    ("approves", "rejects"),
    ("enables", "prevents"),
    ("causes", "prevents"),
];

impl ConflictDetector {
    /// Run all detectors and return a combined, deduplicated list of conflicts.
    pub fn detect_all(conn: &Connection) -> Result<Vec<Conflict>> {
        let mut conflicts = Vec::new();
        conflicts.extend(Self::detect_contradictions(conn)?);
        conflicts.extend(Self::detect_temporal_inconsistencies(conn)?);
        conflicts.extend(Self::detect_cycles(conn)?);
        conflicts.extend(Self::detect_orphans(conn)?);
        Ok(conflicts)
    }

    /// Find edges where A→B has contradicting relation types
    /// (e.g. both "supports" and "contradicts" for the same pair).
    pub fn detect_contradictions(conn: &Connection) -> Result<Vec<Conflict>> {
        // Load all edges from cross_references.
        let edges = load_all_edges(conn)?;

        // Group by (from_id, to_id).
        let mut by_pair: HashMap<(i64, i64), Vec<EdgeRow>> = HashMap::new();
        for edge in edges {
            by_pair
                .entry((edge.from_id, edge.to_id))
                .or_default()
                .push(edge);
        }

        let mut conflicts = Vec::new();

        for ((from_id, to_id), group) in &by_pair {
            let relations: Vec<&str> = group.iter().map(|e| e.relation_type.as_str()).collect();

            for &(a, b) in CONTRADICTING_PAIRS {
                if relations.contains(&a) && relations.contains(&b) {
                    let involved_ids: Vec<i64> = group.iter().map(|e| e.id).collect();
                    conflicts.push(Conflict {
                        id: 0,
                        conflict_type: ConflictType::DirectContradiction,
                        edge_ids: involved_ids,
                        description: format!(
                            "Contradicting relations '{}' and '{}' between nodes {} and {}",
                            a, b, from_id, to_id
                        ),
                        severity: Severity::High,
                        resolved_at: None,
                        resolution_strategy: None,
                    });
                }
            }
        }

        Ok(conflicts)
    }

    /// Find edges with overlapping validity periods for the same entity pair.
    ///
    /// Queries the `cross_references` table and treats the `created_at` column
    /// as a proxy for validity start. If two edges share the same
    /// `(from_id, to_id, relation_type)` triple, that is considered a temporal
    /// inconsistency — one should have been closed when the next was created.
    pub fn detect_temporal_inconsistencies(conn: &Connection) -> Result<Vec<Conflict>> {
        // Self-join: same triple, both are unresolved / open, different IDs.
        let sql = "
            SELECT a.id, b.id, a.from_id, a.to_id, a.relation_type
            FROM   cross_references a
            JOIN   cross_references b
              ON   a.from_id       = b.from_id
             AND   a.to_id         = b.to_id
             AND   a.relation_type = b.relation_type
             AND   a.id < b.id
        ";

        let table_exists = table_exists(conn, "cross_references")?;
        if !table_exists {
            return Ok(Vec::new());
        }

        let mut stmt = conn.prepare(sql).map_err(EngramError::Database)?;

        let pairs = stmt
            .query_map([], |row| {
                Ok((
                    row.get::<_, i64>(0)?,
                    row.get::<_, i64>(1)?,
                    row.get::<_, i64>(2)?,
                    row.get::<_, i64>(3)?,
                    row.get::<_, String>(4)?,
                ))
            })
            .map_err(EngramError::Database)?
            .collect::<rusqlite::Result<Vec<_>>>()
            .map_err(EngramError::Database)?;

        let conflicts = pairs
            .into_iter()
            .map(|(id_a, id_b, from_id, to_id, rel)| Conflict {
                id: 0,
                conflict_type: ConflictType::TemporalInconsistency,
                edge_ids: vec![id_a, id_b],
                description: format!(
                    "Duplicate '{}' edges between nodes {} and {} (ids {} and {}); possible temporal overlap",
                    rel, from_id, to_id, id_a, id_b
                ),
                severity: Severity::Medium,
                resolved_at: None,
                resolution_strategy: None,
            })
            .collect();

        Ok(conflicts)
    }

    /// Detect cycles in the directed edge graph using iterative DFS.
    ///
    /// Returns one conflict per cycle found, listing the edge IDs that form
    /// that cycle.
    pub fn detect_cycles(conn: &Connection) -> Result<Vec<Conflict>> {
        let table_exists = table_exists(conn, "cross_references")?;
        if !table_exists {
            return Ok(Vec::new());
        }

        let edges = load_all_edges(conn)?;

        // Build adjacency list: from_id -> Vec<(to_id, edge_id)>
        let mut adj: HashMap<i64, Vec<(i64, i64)>> = HashMap::new();
        for edge in &edges {
            adj.entry(edge.from_id)
                .or_default()
                .push((edge.to_id, edge.id));
        }

        // Build edge lookup: (from_id, to_id) -> edge_id for path reconstruction.
        let mut edge_map: HashMap<(i64, i64), i64> = HashMap::new();
        for edge in &edges {
            edge_map.insert((edge.from_id, edge.to_id), edge.id);
        }

        let all_nodes: HashSet<i64> = edges.iter().flat_map(|e| [e.from_id, e.to_id]).collect();

        let mut visited: HashSet<i64> = HashSet::new();
        let mut rec_stack: HashSet<i64> = HashSet::new();
        let mut conflicts = Vec::new();

        for &start in &all_nodes {
            if !visited.contains(&start) {
                dfs_detect_cycle(
                    start,
                    &adj,
                    &edge_map,
                    &mut visited,
                    &mut rec_stack,
                    &mut conflicts,
                );
            }
        }

        Ok(conflicts)
    }

    /// Find edges whose `from_id` or `to_id` do not exist in the `memories`
    /// table.
    pub fn detect_orphans(conn: &Connection) -> Result<Vec<Conflict>> {
        let cr_exists = table_exists(conn, "cross_references")?;
        let mem_exists = table_exists(conn, "memories")?;

        if !cr_exists || !mem_exists {
            return Ok(Vec::new());
        }

        let sql = "
            SELECT cr.id, cr.from_id, cr.to_id
            FROM   cross_references cr
            WHERE  NOT EXISTS (SELECT 1 FROM memories m WHERE m.id = cr.from_id)
               OR  NOT EXISTS (SELECT 1 FROM memories m WHERE m.id = cr.to_id)
        ";

        let mut stmt = conn.prepare(sql).map_err(EngramError::Database)?;

        let rows = stmt
            .query_map([], |row| {
                Ok((
                    row.get::<_, i64>(0)?,
                    row.get::<_, i64>(1)?,
                    row.get::<_, i64>(2)?,
                ))
            })
            .map_err(EngramError::Database)?
            .collect::<rusqlite::Result<Vec<_>>>()
            .map_err(EngramError::Database)?;

        let conflicts = rows
            .into_iter()
            .map(|(edge_id, from_id, to_id)| Conflict {
                id: 0,
                conflict_type: ConflictType::OrphanedReference,
                edge_ids: vec![edge_id],
                description: format!(
                    "Edge {} references non-existent memory node(s): from_id={}, to_id={}",
                    edge_id, from_id, to_id
                ),
                severity: Severity::Critical,
                resolved_at: None,
                resolution_strategy: None,
            })
            .collect();

        Ok(conflicts)
    }
}

// =============================================================================
// ConflictResolver
// =============================================================================

/// Resolves conflicts and persists them to the `graph_conflicts` table.
pub struct ConflictResolver;

impl ConflictResolver {
    /// Resolve a saved conflict by its ID using the given strategy.
    pub fn resolve(
        conn: &Connection,
        conflict_id: i64,
        strategy: ResolutionStrategy,
    ) -> Result<ResolutionResult> {
        let conflict = Self::get_conflict(conn, conflict_id)?
            .ok_or_else(|| EngramError::NotFound(conflict_id))?;

        if conflict.resolved_at.is_some() {
            return Err(EngramError::InvalidInput(format!(
                "Conflict {} is already resolved",
                conflict_id
            )));
        }

        let edge_ids = &conflict.edge_ids;

        let (edges_removed, edges_kept) = match strategy {
            ResolutionStrategy::KeepNewer => resolve_keep_newer(conn, edge_ids)?,
            ResolutionStrategy::KeepHigherConfidence => {
                resolve_keep_higher_confidence(conn, edge_ids)?
            }
            ResolutionStrategy::Merge => resolve_merge(conn, edge_ids)?,
            ResolutionStrategy::Manual => {
                // No edge modifications — just mark resolved.
                (Vec::new(), edge_ids.clone())
            }
        };

        // Mark the conflict as resolved.
        let now = chrono_now();
        conn.execute(
            "UPDATE graph_conflicts
             SET resolved_at = ?1, resolution_strategy = ?2
             WHERE id = ?3",
            params![now, strategy.as_str(), conflict_id],
        )
        .map_err(EngramError::Database)?;

        Ok(ResolutionResult {
            conflict_id,
            strategy,
            edges_removed,
            edges_kept,
        })
    }

    /// Persist a detected conflict to the `graph_conflicts` table.
    ///
    /// Returns the generated row ID.
    pub fn save_conflict(conn: &Connection, conflict: &Conflict) -> Result<i64> {
        let edge_ids_json = serde_json::to_string(&conflict.edge_ids)?;
        let resolution_strategy = conflict.resolution_strategy.as_ref().map(|s| s.as_str());

        conn.execute(
            "INSERT INTO graph_conflicts
                 (conflict_type, edge_ids, description, severity, resolved_at, resolution_strategy)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
            params![
                conflict.conflict_type.as_str(),
                edge_ids_json,
                conflict.description,
                conflict.severity.as_str(),
                conflict.resolved_at,
                resolution_strategy,
            ],
        )
        .map_err(EngramError::Database)?;

        Ok(conn.last_insert_rowid())
    }

    /// List conflicts from the `graph_conflicts` table.
    ///
    /// - `resolved = Some(true)`  — only resolved conflicts.
    /// - `resolved = Some(false)` — only unresolved conflicts.
    /// - `resolved = None`        — all conflicts.
    pub fn list_conflicts(conn: &Connection, resolved: Option<bool>) -> Result<Vec<Conflict>> {
        let sql = match resolved {
            Some(true) => {
                "SELECT id, conflict_type, edge_ids, description, severity,
                        resolved_at, resolution_strategy
                 FROM   graph_conflicts
                 WHERE  resolved_at IS NOT NULL
                 ORDER  BY id ASC"
            }
            Some(false) => {
                "SELECT id, conflict_type, edge_ids, description, severity,
                        resolved_at, resolution_strategy
                 FROM   graph_conflicts
                 WHERE  resolved_at IS NULL
                 ORDER  BY id ASC"
            }
            None => {
                "SELECT id, conflict_type, edge_ids, description, severity,
                        resolved_at, resolution_strategy
                 FROM   graph_conflicts
                 ORDER  BY id ASC"
            }
        };

        let mut stmt = conn.prepare(sql).map_err(EngramError::Database)?;

        let rows = stmt
            .query_map([], row_to_conflict)
            .map_err(EngramError::Database)?
            .collect::<rusqlite::Result<Vec<_>>>()
            .map_err(EngramError::Database)?;

        Ok(rows)
    }

    /// Retrieve a single conflict by ID.
    pub fn get_conflict(conn: &Connection, id: i64) -> Result<Option<Conflict>> {
        let mut stmt = conn
            .prepare(
                "SELECT id, conflict_type, edge_ids, description, severity,
                        resolved_at, resolution_strategy
                 FROM   graph_conflicts
                 WHERE  id = ?1",
            )
            .map_err(EngramError::Database)?;

        let mut rows = stmt
            .query_map(params![id], row_to_conflict)
            .map_err(EngramError::Database)?;

        match rows.next() {
            Some(row) => Ok(Some(row.map_err(EngramError::Database)?)),
            None => Ok(None),
        }
    }
}

// =============================================================================
// Resolution helpers (private)
// =============================================================================

/// Keep the edge with the highest ID (most recently inserted) and remove the
/// rest.  Returns `(removed, kept)`.
fn resolve_keep_newer(conn: &Connection, edge_ids: &[i64]) -> Result<(Vec<i64>, Vec<i64>)> {
    if edge_ids.is_empty() {
        return Ok((Vec::new(), Vec::new()));
    }

    // Load creation timestamps from cross_references.
    let mut id_times: Vec<(i64, String)> = edge_ids
        .iter()
        .filter_map(|&id| {
            let ts: rusqlite::Result<String> = conn.query_row(
                "SELECT created_at FROM cross_references WHERE id = ?1",
                params![id],
                |r| r.get(0),
            );
            ts.ok().map(|t| (id, t))
        })
        .collect();

    // Sort ascending; the last element is the newest.
    id_times.sort_by(|a, b| a.1.cmp(&b.1));

    if id_times.is_empty() {
        return Ok((Vec::new(), edge_ids.to_vec()));
    }

    let newest_id = id_times.last().unwrap().0;
    let to_remove: Vec<i64> = id_times
        .iter()
        .filter(|(id, _)| *id != newest_id)
        .map(|(id, _)| *id)
        .collect();

    for &id in &to_remove {
        conn.execute("DELETE FROM cross_references WHERE id = ?1", params![id])
            .map_err(EngramError::Database)?;
    }

    Ok((to_remove, vec![newest_id]))
}

/// Keep the edge with the highest `strength` (confidence proxy) and remove the
/// rest.  Returns `(removed, kept)`.
fn resolve_keep_higher_confidence(
    conn: &Connection,
    edge_ids: &[i64],
) -> Result<(Vec<i64>, Vec<i64>)> {
    if edge_ids.is_empty() {
        return Ok((Vec::new(), Vec::new()));
    }

    // Load strength from cross_references.
    let mut id_strengths: Vec<(i64, f64)> = edge_ids
        .iter()
        .filter_map(|&id| {
            let s: rusqlite::Result<f64> = conn.query_row(
                "SELECT strength FROM cross_references WHERE id = ?1",
                params![id],
                |r| r.get(0),
            );
            s.ok().map(|strength| (id, strength))
        })
        .collect();

    // Sort ascending; last element has highest strength.
    id_strengths.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

    if id_strengths.is_empty() {
        return Ok((Vec::new(), edge_ids.to_vec()));
    }

    let best_id = id_strengths.last().unwrap().0;
    let to_remove: Vec<i64> = id_strengths
        .iter()
        .filter(|(id, _)| *id != best_id)
        .map(|(id, _)| *id)
        .collect();

    for &id in &to_remove {
        conn.execute("DELETE FROM cross_references WHERE id = ?1", params![id])
            .map_err(EngramError::Database)?;
    }

    Ok((to_remove, vec![best_id]))
}

/// Merge edges: keep the one with the highest strength, update its metadata to
/// be the JSON merge of all involved edges, then delete the rest.
/// Returns `(removed, kept)`.
fn resolve_merge(conn: &Connection, edge_ids: &[i64]) -> Result<(Vec<i64>, Vec<i64>)> {
    if edge_ids.is_empty() {
        return Ok((Vec::new(), Vec::new()));
    }

    // Load all edge metadata.
    let mut rows: Vec<(i64, f64, String)> = edge_ids
        .iter()
        .filter_map(|&id| {
            conn.query_row(
                "SELECT id, strength, metadata FROM cross_references WHERE id = ?1",
                params![id],
                |r| {
                    Ok((
                        r.get::<_, i64>(0)?,
                        r.get::<_, f64>(1)?,
                        r.get::<_, String>(2)?,
                    ))
                },
            )
            .ok()
        })
        .collect();

    if rows.is_empty() {
        return Ok((Vec::new(), edge_ids.to_vec()));
    }

    // Sort by strength desc; first element is the keeper.
    rows.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
    let (keep_id, keep_strength, keep_meta_str) = rows.remove(0);

    // Merge metadata JSON objects.
    let mut merged: serde_json::Map<String, serde_json::Value> =
        serde_json::from_str(&keep_meta_str).unwrap_or_default();

    for (_, _, meta_str) in &rows {
        if let Ok(serde_json::Value::Object(extra)) = serde_json::from_str(meta_str) {
            for (k, v) in extra {
                merged.entry(k).or_insert(v);
            }
        }
    }

    let merged_str = serde_json::to_string(&serde_json::Value::Object(merged))?;

    conn.execute(
        "UPDATE cross_references SET metadata = ?1, strength = ?2 WHERE id = ?3",
        params![merged_str, keep_strength, keep_id],
    )
    .map_err(EngramError::Database)?;

    let to_remove: Vec<i64> = rows.iter().map(|(id, _, _)| *id).collect();

    for &id in &to_remove {
        conn.execute("DELETE FROM cross_references WHERE id = ?1", params![id])
            .map_err(EngramError::Database)?;
    }

    Ok((to_remove, vec![keep_id]))
}

// =============================================================================
// Private helpers
// =============================================================================

/// Minimal representation of a row in `cross_references`.
#[derive(Debug)]
struct EdgeRow {
    id: i64,
    from_id: i64,
    to_id: i64,
    relation_type: String,
    /// Stored for temporal ordering; not read directly in Rust but used in SQL
    /// ordering.
    #[allow(dead_code)]
    created_at: String,
}

/// Load all rows from `cross_references`. Returns empty vec if table does not
/// exist.
fn load_all_edges(conn: &Connection) -> Result<Vec<EdgeRow>> {
    if !table_exists(conn, "cross_references")? {
        return Ok(Vec::new());
    }

    let mut stmt = conn
        .prepare(
            "SELECT id, from_id, to_id, relation_type, created_at
             FROM   cross_references
             ORDER  BY id ASC",
        )
        .map_err(EngramError::Database)?;

    let rows = stmt
        .query_map([], |row| {
            Ok(EdgeRow {
                id: row.get(0)?,
                from_id: row.get(1)?,
                to_id: row.get(2)?,
                relation_type: row.get(3)?,
                created_at: row.get(4)?,
            })
        })
        .map_err(EngramError::Database)?
        .collect::<rusqlite::Result<Vec<_>>>()
        .map_err(EngramError::Database)?;

    Ok(rows)
}

/// Iterative DFS for cycle detection. Detected cycles are appended to
/// `conflicts`.
fn dfs_detect_cycle(
    start: i64,
    adj: &HashMap<i64, Vec<(i64, i64)>>,
    edge_map: &HashMap<(i64, i64), i64>,
    visited: &mut HashSet<i64>,
    rec_stack: &mut HashSet<i64>,
    conflicts: &mut Vec<Conflict>,
) {
    // Stack items: (node, index into adj[node], parent_edge_id)
    let mut stack: Vec<(i64, usize, Option<i64>)> = vec![(start, 0, None)];
    let mut path: Vec<i64> = vec![start];
    let mut path_set: HashSet<i64> = {
        let mut s = HashSet::new();
        s.insert(start);
        s
    };

    visited.insert(start);
    rec_stack.insert(start);

    while let Some((node, idx, _parent_edge)) = stack.last_mut() {
        let node = *node;
        let neighbors = adj.get(&node).map(|v| v.as_slice()).unwrap_or(&[]);

        if *idx < neighbors.len() {
            let (neighbor, edge_id) = neighbors[*idx];
            *idx += 1;

            if !visited.contains(&neighbor) {
                visited.insert(neighbor);
                rec_stack.insert(neighbor);
                path.push(neighbor);
                path_set.insert(neighbor);
                stack.push((neighbor, 0, Some(edge_id)));
            } else if rec_stack.contains(&neighbor) {
                // Cycle detected — collect the edge IDs that form the cycle.
                let cycle_start_pos = path.iter().position(|&n| n == neighbor).unwrap_or(0);
                let cycle_nodes = &path[cycle_start_pos..];
                let mut cycle_edge_ids: Vec<i64> = Vec::new();
                for window in cycle_nodes.windows(2) {
                    if let Some(&eid) = edge_map.get(&(window[0], window[1])) {
                        cycle_edge_ids.push(eid);
                    }
                }
                // Close the cycle: last node -> neighbor
                if let Some(&eid) =
                    edge_map.get(&(*cycle_nodes.last().unwrap_or(&neighbor), neighbor))
                {
                    cycle_edge_ids.push(eid);
                }

                if !cycle_edge_ids.is_empty() {
                    conflicts.push(Conflict {
                        id: 0,
                        conflict_type: ConflictType::CyclicDependency,
                        edge_ids: cycle_edge_ids.clone(),
                        description: format!("Cycle detected involving nodes: {:?}", cycle_nodes),
                        severity: Severity::Medium,
                        resolved_at: None,
                        resolution_strategy: None,
                    });
                }
            }
        } else {
            // Done with this node — pop.
            stack.pop();
            path.pop();
            path_set.remove(&node);
            rec_stack.remove(&node);
        }
    }
}

/// Check whether a table exists in the current SQLite database.
fn table_exists(conn: &Connection, name: &str) -> Result<bool> {
    let count: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?1",
            params![name],
            |r| r.get(0),
        )
        .map_err(EngramError::Database)?;
    Ok(count > 0)
}

/// Map a rusqlite row to a `Conflict`.
fn row_to_conflict(row: &rusqlite::Row<'_>) -> rusqlite::Result<Conflict> {
    let id: i64 = row.get(0)?;
    let conflict_type_str: String = row.get(1)?;
    let edge_ids_str: String = row.get(2)?;
    let description: String = row.get(3)?;
    let severity_str: String = row.get(4)?;
    let resolved_at: Option<String> = row.get(5)?;
    let resolution_strategy_str: Option<String> = row.get(6)?;

    let conflict_type =
        ConflictType::from_str(&conflict_type_str).unwrap_or(ConflictType::DirectContradiction);
    let edge_ids: Vec<i64> = serde_json::from_str(&edge_ids_str).unwrap_or_default();
    let severity = Severity::from_str(&severity_str).unwrap_or(Severity::Low);
    let resolution_strategy = resolution_strategy_str
        .as_deref()
        .and_then(ResolutionStrategy::from_str);

    Ok(Conflict {
        id,
        conflict_type,
        edge_ids,
        description,
        severity,
        resolved_at,
        resolution_strategy,
    })
}

/// Return the current UTC timestamp in RFC3339 format.
fn chrono_now() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    // Use chrono if available; otherwise fall back to a formatted timestamp.
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    let dt = chrono::DateTime::<chrono::Utc>::from_timestamp(secs as i64, 0)
        .unwrap_or(chrono::Utc::now());
    dt.format("%Y-%m-%dT%H:%M:%SZ").to_string()
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use rusqlite::Connection;

    // -------------------------------------------------------------------------
    // Helpers
    // -------------------------------------------------------------------------

    const CREATE_CROSS_REFS: &str = "
        CREATE TABLE IF NOT EXISTS cross_references (
            id              INTEGER PRIMARY KEY AUTOINCREMENT,
            from_id         INTEGER NOT NULL,
            to_id           INTEGER NOT NULL,
            relation_type   TEXT    NOT NULL DEFAULT 'related',
            strength        REAL    NOT NULL DEFAULT 0.5,
            metadata        TEXT    DEFAULT '{}',
            created_at      TEXT    NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
        );
    ";

    const CREATE_MEMORIES: &str = "
        CREATE TABLE IF NOT EXISTS memories (
            id         INTEGER PRIMARY KEY AUTOINCREMENT,
            content    TEXT    NOT NULL,
            created_at TEXT    NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
        );
    ";

    fn setup_db() -> Connection {
        let conn = Connection::open_in_memory().expect("open in-memory DB");
        conn.execute_batch(CREATE_CROSS_REFS)
            .expect("create cross_references");
        conn.execute_batch(CREATE_MEMORIES)
            .expect("create memories");
        conn.execute_batch(CREATE_CONFLICTS_TABLE)
            .expect("create graph_conflicts");
        conn
    }

    fn insert_edge(conn: &Connection, from_id: i64, to_id: i64, rel: &str, strength: f64) -> i64 {
        conn.execute(
            "INSERT INTO cross_references (from_id, to_id, relation_type, strength)
             VALUES (?1, ?2, ?3, ?4)",
            params![from_id, to_id, rel, strength],
        )
        .expect("insert edge");
        conn.last_insert_rowid()
    }

    fn insert_memory(conn: &Connection, id: i64) {
        conn.execute(
            "INSERT INTO memories (id, content) VALUES (?1, 'test')",
            params![id],
        )
        .expect("insert memory");
    }

    // -------------------------------------------------------------------------
    // Test 1: detect_contradictions — finds contradicting relation pair
    // -------------------------------------------------------------------------
    #[test]
    fn test_detect_contradiction() {
        let conn = setup_db();

        // A --supports--> B
        insert_edge(&conn, 1, 2, "supports", 0.8);
        // A --contradicts--> B  (same pair, contradicting semantics)
        insert_edge(&conn, 1, 2, "contradicts", 0.8);

        let conflicts =
            ConflictDetector::detect_contradictions(&conn).expect("detect_contradictions");

        assert_eq!(conflicts.len(), 1);
        assert_eq!(
            conflicts[0].conflict_type,
            ConflictType::DirectContradiction
        );
        assert_eq!(conflicts[0].severity, Severity::High);
        assert!(conflicts[0].edge_ids.len() >= 2);
        assert!(conflicts[0].description.contains("Contradicting"));
    }

    // -------------------------------------------------------------------------
    // Test 2: detect_temporal_inconsistencies — duplicate triple
    // -------------------------------------------------------------------------
    #[test]
    fn test_detect_temporal_inconsistency() {
        let conn = setup_db();

        // Two edges for the exact same (from, to, relation) triple.
        let id_a = insert_edge(&conn, 10, 20, "works_at", 0.9);
        let id_b = insert_edge(&conn, 10, 20, "works_at", 0.7);

        let conflicts = ConflictDetector::detect_temporal_inconsistencies(&conn)
            .expect("detect_temporal_inconsistencies");

        assert_eq!(conflicts.len(), 1);
        assert_eq!(
            conflicts[0].conflict_type,
            ConflictType::TemporalInconsistency
        );
        assert_eq!(conflicts[0].severity, Severity::Medium);
        assert!(conflicts[0].edge_ids.contains(&id_a));
        assert!(conflicts[0].edge_ids.contains(&id_b));
    }

    // -------------------------------------------------------------------------
    // Test 3: detect_cycles — simple A→B→C→A cycle
    // -------------------------------------------------------------------------
    #[test]
    fn test_detect_cycle() {
        let conn = setup_db();

        // A→B, B→C, C→A forms a cycle.
        insert_edge(&conn, 1, 2, "depends_on", 0.9);
        insert_edge(&conn, 2, 3, "depends_on", 0.9);
        insert_edge(&conn, 3, 1, "depends_on", 0.9); // closes the cycle

        let conflicts = ConflictDetector::detect_cycles(&conn).expect("detect_cycles");

        assert!(
            !conflicts.is_empty(),
            "expected at least one cycle conflict"
        );
        assert_eq!(conflicts[0].conflict_type, ConflictType::CyclicDependency);
        assert!(conflicts[0].description.contains("Cycle"));
    }

    // -------------------------------------------------------------------------
    // Test 4: detect_orphans — edge references missing memory
    // -------------------------------------------------------------------------
    #[test]
    fn test_detect_orphan() {
        let conn = setup_db();

        // Only memory 1 exists; edge references memory 99 which doesn't exist.
        insert_memory(&conn, 1);
        let edge_id = insert_edge(&conn, 1, 99, "related", 0.5); // to_id=99 is orphan

        let conflicts = ConflictDetector::detect_orphans(&conn).expect("detect_orphans");

        assert_eq!(conflicts.len(), 1);
        assert_eq!(conflicts[0].conflict_type, ConflictType::OrphanedReference);
        assert_eq!(conflicts[0].severity, Severity::Critical);
        assert!(conflicts[0].edge_ids.contains(&edge_id));
    }

    // -------------------------------------------------------------------------
    // Test 5: resolve with KeepNewer removes older edges
    // -------------------------------------------------------------------------
    #[test]
    fn test_resolve_keep_newer() {
        let conn = setup_db();

        let id_old = insert_edge(&conn, 5, 6, "supports", 0.5);
        // Ensure the second edge has a later created_at by updating it.
        conn.execute(
            "UPDATE cross_references SET created_at = '2099-01-01T00:00:00.000Z' WHERE id = ?1",
            params![id_old + 1],
        )
        .ok();
        let id_new = insert_edge(&conn, 5, 6, "supports", 0.5);
        // Make the new edge newer.
        conn.execute(
            "UPDATE cross_references SET created_at = '2099-01-02T00:00:00.000Z' WHERE id = ?1",
            params![id_new],
        )
        .expect("update ts");

        // Save a conflict manually.
        let conflict = Conflict {
            id: 0,
            conflict_type: ConflictType::TemporalInconsistency,
            edge_ids: vec![id_old, id_new],
            description: "duplicate triple".to_string(),
            severity: Severity::Medium,
            resolved_at: None,
            resolution_strategy: None,
        };
        let cid = ConflictResolver::save_conflict(&conn, &conflict).expect("save");

        let result =
            ConflictResolver::resolve(&conn, cid, ResolutionStrategy::KeepNewer).expect("resolve");

        assert_eq!(result.conflict_id, cid);
        assert_eq!(result.strategy, ResolutionStrategy::KeepNewer);
        assert_eq!(result.edges_removed.len(), 1);
        assert_eq!(result.edges_kept.len(), 1);
        assert!(result.edges_kept.contains(&id_new));
        assert!(result.edges_removed.contains(&id_old));

        // Verify the conflict is marked resolved.
        let saved = ConflictResolver::get_conflict(&conn, cid)
            .expect("get")
            .expect("exists");
        assert!(saved.resolved_at.is_some());
    }

    // -------------------------------------------------------------------------
    // Test 6: no conflicts when graph is clean
    // -------------------------------------------------------------------------
    #[test]
    fn test_no_conflicts_clean_graph() {
        let conn = setup_db();

        // Insert valid memories and non-contradicting edges.
        insert_memory(&conn, 1);
        insert_memory(&conn, 2);
        insert_memory(&conn, 3);
        insert_edge(&conn, 1, 2, "supports", 0.9);
        insert_edge(&conn, 2, 3, "related", 0.7);

        let all = ConflictDetector::detect_all(&conn).expect("detect_all");

        // No cycles (1→2→3, no back-edge), no orphans, no contradictions, no temporal.
        assert!(all.is_empty(), "expected zero conflicts, got: {:?}", all);
    }

    // -------------------------------------------------------------------------
    // Test 7: save and list conflicts
    // -------------------------------------------------------------------------
    #[test]
    fn test_save_and_list_conflicts() {
        let conn = setup_db();

        let c1 = Conflict {
            id: 0,
            conflict_type: ConflictType::DirectContradiction,
            edge_ids: vec![1, 2],
            description: "supports vs contradicts".to_string(),
            severity: Severity::High,
            resolved_at: None,
            resolution_strategy: None,
        };
        let c2 = Conflict {
            id: 0,
            conflict_type: ConflictType::OrphanedReference,
            edge_ids: vec![3],
            description: "missing node 99".to_string(),
            severity: Severity::Critical,
            resolved_at: None,
            resolution_strategy: None,
        };

        let id1 = ConflictResolver::save_conflict(&conn, &c1).expect("save c1");
        let id2 = ConflictResolver::save_conflict(&conn, &c2).expect("save c2");

        let all = ConflictResolver::list_conflicts(&conn, None).expect("list all");
        assert_eq!(all.len(), 2);

        let unresolved =
            ConflictResolver::list_conflicts(&conn, Some(false)).expect("list unresolved");
        assert_eq!(unresolved.len(), 2);

        let resolved = ConflictResolver::list_conflicts(&conn, Some(true)).expect("list resolved");
        assert_eq!(resolved.len(), 0);

        // Verify we can retrieve by ID.
        let fetched = ConflictResolver::get_conflict(&conn, id1)
            .expect("get c1")
            .expect("exists");
        assert_eq!(fetched.conflict_type, ConflictType::DirectContradiction);
        assert_eq!(fetched.severity, Severity::High);

        let fetched2 = ConflictResolver::get_conflict(&conn, id2)
            .expect("get c2")
            .expect("exists");
        assert_eq!(fetched2.conflict_type, ConflictType::OrphanedReference);
    }

    // -------------------------------------------------------------------------
    // Test 8: multiple conflict types in one scan
    // -------------------------------------------------------------------------
    #[test]
    fn test_detect_all_multiple_types() {
        let conn = setup_db();

        // Memory 1 exists, 99 does not → orphan.
        insert_memory(&conn, 1);
        insert_memory(&conn, 2);

        // Contradiction: supports + contradicts between same pair.
        insert_edge(&conn, 1, 2, "supports", 0.8);
        insert_edge(&conn, 1, 2, "contradicts", 0.6);

        // Orphan: edge references non-existent memory 99.
        insert_edge(&conn, 1, 99, "related", 0.5);

        let all = ConflictDetector::detect_all(&conn).expect("detect_all");

        let types: Vec<&ConflictType> = all.iter().map(|c| &c.conflict_type).collect();

        assert!(
            types.contains(&&ConflictType::DirectContradiction),
            "expected DirectContradiction in {:?}",
            types
        );
        assert!(
            types.contains(&&ConflictType::OrphanedReference),
            "expected OrphanedReference in {:?}",
            types
        );
    }

    // -------------------------------------------------------------------------
    // Test 9: resolve already-resolved conflict returns error
    // -------------------------------------------------------------------------
    #[test]
    fn test_resolve_already_resolved_returns_error() {
        let conn = setup_db();

        let id_a = insert_edge(&conn, 5, 6, "rel", 0.5);
        let id_b = insert_edge(&conn, 5, 6, "rel", 0.5);

        let conflict = Conflict {
            id: 0,
            conflict_type: ConflictType::TemporalInconsistency,
            edge_ids: vec![id_a, id_b],
            description: "dup".to_string(),
            severity: Severity::Medium,
            resolved_at: None,
            resolution_strategy: None,
        };
        let cid = ConflictResolver::save_conflict(&conn, &conflict).expect("save");

        // First resolution should succeed.
        ConflictResolver::resolve(&conn, cid, ResolutionStrategy::Manual).expect("first resolve");

        // Second resolution on the same conflict should fail.
        let result = ConflictResolver::resolve(&conn, cid, ResolutionStrategy::Manual);
        assert!(result.is_err(), "expected error on double-resolve");
    }
}