post-cortex-core 0.3.1

Core domain library for post-cortex: lock-free conversation memory, semantic search, knowledge graph, and storage backends. Transport-agnostic — no axum/tonic/rmcp.
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
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
// Copyright (c) 2025 Julius ML
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//! Petgraph-backed entity graph with deterministic serialization

use crate::core::context_update::{EntityData, EntityRelationship, EntityType, RelationType};
use chrono::{DateTime, Utc};
use petgraph::Direction;
use petgraph::graph::{DiGraph, NodeIndex};
use petgraph::visit::EdgeRef;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
use tracing::warn;

/// Edge data containing relation type and context
/// Supports backwards-compatible deserialization from v1.0.0 format (RelationType only)
#[derive(Clone, Debug, Serialize, PartialEq, Eq)]
pub struct EdgeData {
    /// Kind of relationship between two entities
    pub relation_type: RelationType,
    /// Free-text context describing this relationship
    #[serde(default)]
    pub context: String,
}

impl EdgeData {
    /// Create new EdgeData with context
    pub fn new(relation_type: RelationType, context: String) -> Self {
        Self {
            relation_type,
            context,
        }
    }

    /// Create EdgeData from just RelationType (for v1.0.0 compatibility)
    pub fn from_relation_type(relation_type: RelationType) -> Self {
        Self {
            relation_type,
            context: String::new(),
        }
    }
}

/// Custom deserializer for EdgeData - handles both v1.0.0 (RelationType) and v1.1.0 (EdgeData) formats
impl<'de> serde::Deserialize<'de> for EdgeData {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de::{self, MapAccess, Visitor};

        struct EdgeDataVisitor;

        impl<'de> Visitor<'de> for EdgeDataVisitor {
            type Value = EdgeData;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("EdgeData object or RelationType string (v1.0.0 format)")
            }

            // v1.1.0 format: {"relation_type": "...", "context": "..."}
            fn visit_map<M>(self, mut map: M) -> Result<EdgeData, M::Error>
            where
                M: MapAccess<'de>,
            {
                let mut relation_type: Option<RelationType> = None;
                let mut context: Option<String> = None;

                while let Some(key) = map.next_key::<String>()? {
                    match key.as_str() {
                        "relation_type" => relation_type = Some(map.next_value()?),
                        "context" => context = Some(map.next_value()?),
                        _ => {
                            let _: serde::de::IgnoredAny = map.next_value()?;
                        }
                    }
                }

                let relation_type =
                    relation_type.ok_or_else(|| de::Error::missing_field("relation_type"))?;

                Ok(EdgeData {
                    relation_type,
                    context: context.unwrap_or_default(),
                })
            }

            // v1.0.0 format: just "RelationType" string
            fn visit_str<E>(self, value: &str) -> Result<EdgeData, E>
            where
                E: de::Error,
            {
                let relation_type: RelationType = value.parse().map_err(|_| {
                    de::Error::unknown_variant(
                        value,
                        &[
                            "RequiredBy",
                            "LeadsTo",
                            "RelatedTo",
                            "ConflictsWith",
                            "DependsOn",
                            "Implements",
                            "CausedBy",
                            "Solves",
                        ],
                    )
                })?;
                Ok(EdgeData::from_relation_type(relation_type))
            }
        }

        deserializer.deserialize_any(EdgeDataVisitor)
    }
}

/// Enhanced entity graph using petgraph for efficient relationship operations
#[derive(Clone, Debug, Serialize)]
pub struct SimpleEntityGraph {
    /// Entity metadata storage - uses BTreeMap for deterministic JSON serialization
    pub entities: BTreeMap<String, EntityData>,

    /// Entity mentions tracking - uses BTreeMap for deterministic JSON serialization
    pub entity_mentions: BTreeMap<String, Vec<uuid::Uuid>>,

    /// Petgraph directed graph for relationships (now stores context with edges)
    #[serde(with = "petgraph_serde")]
    graph: DiGraph<String, EdgeData>,

    /// Bidirectional mapping between entity names and graph nodes
    /// CRITICAL: Rebuilt automatically after deserialization
    #[serde(skip)]
    entity_to_node: HashMap<String, NodeIndex>,

    /// CRITICAL: Rebuilt automatically after deserialization
    #[serde(skip)]
    node_to_entity: HashMap<NodeIndex, String>,
}

// Intermediate structure for serializing graph data with entity names instead of indices
#[derive(Serialize, Deserialize)]
struct SerializableGraphData {
    /// Sorted list of entity names (nodes) for deterministic deserialization
    nodes: Vec<String>,
    /// Edges represented by entity name pairs with full edge data (including context)
    edges: Vec<(String, String, EdgeData)>,
}

// Custom serde module for petgraph serialization with full graph reconstruction
mod petgraph_serde {
    use super::*;
    use serde::{Deserializer, Serializer};

    pub fn serialize<S>(graph: &DiGraph<String, EdgeData>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        // Extract all nodes (entity names) in sorted order for determinism
        let mut nodes: Vec<String> = graph.node_weights().cloned().collect();
        nodes.sort(); // CRITICAL: sorted for deterministic NodeIndex mapping

        // Extract edges using entity names instead of NodeIndex (includes context)
        let mut edges: Vec<(String, String, EdgeData)> = graph
            .edge_references()
            .map(|edge| {
                let from_entity = graph.node_weight(edge.source()).unwrap().clone();
                let to_entity = graph.node_weight(edge.target()).unwrap().clone();
                (from_entity, to_entity, edge.weight().clone())
            })
            .collect();

        // Sort edges for determinism (by source, then target, then relation type)
        edges.sort_by(|a, b| {
            a.0.cmp(&b.0)
                .then_with(|| a.1.cmp(&b.1))
                .then_with(|| a.2.relation_type.cmp(&b.2.relation_type))
        });

        let data = SerializableGraphData { nodes, edges };
        data.serialize(serializer)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<DiGraph<String, EdgeData>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let data = SerializableGraphData::deserialize(deserializer)?;

        // Recreate graph with nodes in deterministic order
        let mut graph = DiGraph::new();

        // Add all nodes in the same order they were serialized
        // This ensures NodeIndex values are consistent across serialization
        for entity_name in &data.nodes {
            graph.add_node(entity_name.clone());
        }

        // Build temporary mapping from entity name to NodeIndex for edge reconstruction
        let entity_to_node: std::collections::HashMap<&String, NodeIndex> = data
            .nodes
            .iter()
            .enumerate()
            .map(|(idx, name)| (name, NodeIndex::new(idx)))
            .collect();

        // Recreate all edges (now with full EdgeData including context)
        for (from_entity, to_entity, edge_data) in data.edges {
            if let (Some(&from_node), Some(&to_node)) = (
                entity_to_node.get(&from_entity),
                entity_to_node.get(&to_entity),
            ) {
                graph.add_edge(from_node, to_node, edge_data);
            }
        }

        Ok(graph)
    }
}

// Custom Deserialize implementation to rebuild entity_to_node and node_to_entity mappings
impl<'de> Deserialize<'de> for SimpleEntityGraph {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct SimpleEntityGraphData {
            entities: BTreeMap<String, EntityData>,
            entity_mentions: BTreeMap<String, Vec<uuid::Uuid>>,
            #[serde(with = "petgraph_serde")]
            graph: DiGraph<String, EdgeData>,
        }

        let data = SimpleEntityGraphData::deserialize(deserializer)?;

        // Rebuild entity_to_node and node_to_entity mappings from deserialized graph
        let mut entity_to_node = HashMap::new();
        let mut node_to_entity = HashMap::new();

        for node_idx in data.graph.node_indices() {
            if let Some(entity_name) = data.graph.node_weight(node_idx) {
                entity_to_node.insert(entity_name.clone(), node_idx);
                node_to_entity.insert(node_idx, entity_name.clone());
            }
        }

        Ok(SimpleEntityGraph {
            entities: data.entities,
            entity_mentions: data.entity_mentions,
            graph: data.graph,
            entity_to_node,
            node_to_entity,
        })
    }
}

impl Default for SimpleEntityGraph {
    fn default() -> Self {
        Self::new()
    }
}

impl SimpleEntityGraph {
    /// Creates a new empty entity graph
    pub fn new() -> Self {
        Self {
            entities: BTreeMap::new(),
            entity_mentions: BTreeMap::new(),
            graph: DiGraph::new(),
            entity_to_node: HashMap::new(),
            node_to_entity: HashMap::new(),
        }
    }

    /// Clear all entities, relationships, and graph data
    pub fn clear(&mut self) {
        self.entities.clear();
        self.entity_mentions.clear();
        self.graph.clear();
        self.entity_to_node.clear();
        self.node_to_entity.clear();
    }

    /// Remove a single entity and all its incident edges. Returns true if
    /// the entity existed and was removed.
    ///
    /// `DiGraph::remove_node` moves the LAST node into the vacated slot,
    /// which invalidates the previous-last `NodeIndex`. We patch both
    /// index maps for the swapped node so subsequent lookups stay correct.
    pub fn remove_entity(&mut self, name: &str) -> bool {
        let Some(&node_idx) = self.entity_to_node.get(name) else {
            return false;
        };

        self.graph.remove_node(node_idx);
        self.entity_to_node.remove(name);
        self.node_to_entity.remove(&node_idx);

        if let Some(swapped_name) = self.graph.node_weight(node_idx).cloned() {
            if let Some(old_idx) = self.entity_to_node.insert(swapped_name.clone(), node_idx) {
                self.node_to_entity.remove(&old_idx);
            }
            self.node_to_entity.insert(node_idx, swapped_name);
        }

        self.entities.remove(name);
        self.entity_mentions.remove(name);
        true
    }

    /// Check if entity exists in the graph
    pub fn has_entity(&self, name: &str) -> bool {
        self.entities.contains_key(name)
    }

    /// Get the number of entities in the graph
    pub fn entity_count(&self) -> usize {
        self.entities.len()
    }

    /// Update entity timestamp without changing other properties
    pub fn update_entity_timestamp(&mut self, name: &str, timestamp: DateTime<Utc>) {
        if let Some(entity) = self.entities.get_mut(name) {
            entity.last_mentioned = timestamp;
            entity.mention_count += 1;
        }
    }

    /// Add or update entity - maintains same API as original SimpleEntityGraph
    pub fn add_or_update_entity(
        &mut self,
        name: String,
        entity_type: EntityType,
        timestamp: DateTime<Utc>,
        description: &str,
    ) {
        match self.entities.get_mut(&name) {
            Some(entity) => {
                entity.last_mentioned = timestamp;
                entity.mention_count += 1;
                if entity.description.is_none() && !description.is_empty() {
                    entity.description = Some(description.to_string());
                }

                // CRITICAL FIX: Ensure node exists in graph even for existing entities
                if !self.entity_to_node.contains_key(&name) {
                    let node_index = self.graph.add_node(name.clone());
                    self.entity_to_node.insert(name.clone(), node_index);
                    self.node_to_entity.insert(node_index, name.clone());
                }
            }
            None => {
                // Create new entity data
                self.entities.insert(
                    name.clone(),
                    EntityData {
                        name: name.clone(),
                        entity_type,
                        first_mentioned: timestamp,
                        last_mentioned: timestamp,
                        mention_count: 1,
                        importance_score: 1.0,
                        description: if description.is_empty() {
                            None
                        } else {
                            Some(description.to_string())
                        },
                    },
                );

                // Add node to graph
                let node_index = self.graph.add_node(name.clone());
                self.entity_to_node.insert(name.clone(), node_index);
                self.node_to_entity.insert(node_index, name);
            }
        }
    }

    /// Get or create a node for an entity, returning its NodeIndex
    /// This is a safe helper that guarantees a valid NodeIndex is returned
    fn get_or_create_node(
        &mut self,
        name: &str,
        entity_type: EntityType,
        timestamp: DateTime<Utc>,
    ) -> NodeIndex {
        // Check if node already exists
        if let Some(&node_idx) = self.entity_to_node.get(name) {
            return node_idx;
        }

        // Entity doesn't exist or doesn't have a node - create it
        self.add_or_update_entity(name.to_string(), entity_type, timestamp, "");

        // Now it must exist - but use defensive programming
        match self.entity_to_node.get(name) {
            Some(&node_idx) => node_idx,
            None => {
                // This should never happen, but if it does, create the node directly
                let node_idx = self.graph.add_node(name.to_string());
                self.entity_to_node.insert(name.to_string(), node_idx);
                self.node_to_entity.insert(node_idx, name.to_string());
                node_idx
            }
        }
    }

    /// Mention entity - updates importance and tracking
    pub fn mention_entity(&mut self, name: &str, update_id: uuid::Uuid, timestamp: DateTime<Utc>) {
        if let Some(entity) = self.entities.get_mut(name) {
            entity.last_mentioned = timestamp;
            entity.mention_count += 1;
            entity.importance_score = (entity.mention_count as f32).log2().max(1.0);
        }

        self.entity_mentions
            .entry(name.to_string())
            .or_default()
            .push(update_id);
    }

    /// Add relationship - now uses petgraph for efficient storage
    pub fn add_relationship(&mut self, relationship: EntityRelationship) {
        let timestamp = Utc::now();

        // Use safe helper to get or create nodes - no more unwrap()!
        let from_node =
            self.get_or_create_node(&relationship.from_entity, EntityType::Concept, timestamp);

        let to_node =
            self.get_or_create_node(&relationship.to_entity, EntityType::Concept, timestamp);

        // Add edge to graph with full EdgeData (includes context)
        let edge_data = EdgeData {
            relation_type: relationship.relation_type,
            context: relationship.context,
        };
        self.graph.add_edge(from_node, to_node, edge_data);
    }

    /// Merge all entities and relationships from another graph into this one.
    ///
    /// Entities are added with `add_or_update_entity` (so counts accumulate for
    /// entities that appear in multiple sessions). Edges are copied as-is.
    pub fn merge_from(&mut self, other: &SimpleEntityGraph) {
        // Merge entities first so all nodes exist before we add edges
        for (name, data) in &other.entities {
            self.add_or_update_entity(
                name.clone(),
                data.entity_type.clone(),
                data.last_mentioned,
                data.description.as_deref().unwrap_or(""),
            );
        }

        // Merge entity mention lists
        for (name, mentions) in &other.entity_mentions {
            let entry = self.entity_mentions.entry(name.clone()).or_default();
            for id in mentions {
                if !entry.contains(id) {
                    entry.push(*id);
                }
            }
        }

        // Merge edges
        for edge in other.graph.edge_references() {
            let from_name = match other.graph.node_weight(edge.source()) {
                Some(n) => n.clone(),
                None => continue,
            };
            let to_name = match other.graph.node_weight(edge.target()) {
                Some(n) => n.clone(),
                None => continue,
            };
            let edge_data = edge.weight().clone();

            use crate::core::context_update::EntityRelationship;
            self.add_relationship(EntityRelationship {
                from_entity: from_name,
                to_entity: to_name,
                relation_type: edge_data.relation_type,
                context: edge_data.context,
            });
        }
    }

    /// Find related entities - now O(degree) instead of O(n)!
    /// Uses BTreeSet for automatic sorting, avoiding explicit sort() call
    /// Supports case-insensitive lookup with fallback
    pub fn find_related_entities(&self, entity_name: &str) -> Vec<String> {
        use std::collections::BTreeSet;

        // First try exact match (fast O(1))
        let node = if let Some(&node) = self.entity_to_node.get(entity_name) {
            node
        } else {
            // Fallback: case-insensitive lookup (O(n) but rare)
            let entity_lower = entity_name.to_lowercase();
            match self
                .entity_to_node
                .iter()
                .find(|(k, _)| k.to_lowercase() == entity_lower)
            {
                Some((_, &node)) => node,
                None => return Vec::new(),
            }
        };

        // BTreeSet maintains sorted order automatically
        let mut related = BTreeSet::new();

        // Get outgoing neighbors
        for edge in self.graph.edges_directed(node, Direction::Outgoing) {
            if let Some(target_name) = self.node_to_entity.get(&edge.target()) {
                related.insert(target_name.clone());
            }
        }

        // Get incoming neighbors
        for edge in self.graph.edges_directed(node, Direction::Incoming) {
            if let Some(source_name) = self.node_to_entity.get(&edge.source()) {
                related.insert(source_name.clone());
            }
        }

        // BTreeSet iterator is already sorted
        related.into_iter().collect()
    }

    /// Find related entities by specific relation type - also now O(degree) instead of O(n)
    /// Uses BTreeSet for automatic sorting, avoiding explicit sort() call
    /// Supports case-insensitive lookup with fallback
    pub fn find_related_entities_by_type(
        &self,
        entity_name: &str,
        relation_type: &RelationType,
    ) -> Vec<String> {
        use std::collections::BTreeSet;

        // First try exact match (fast O(1))
        let node = if let Some(&node) = self.entity_to_node.get(entity_name) {
            node
        } else {
            // Fallback: case-insensitive lookup (O(n) but rare)
            let entity_lower = entity_name.to_lowercase();
            match self
                .entity_to_node
                .iter()
                .find(|(k, _)| k.to_lowercase() == entity_lower)
            {
                Some((_, &node)) => node,
                None => return Vec::new(),
            }
        };

        // BTreeSet maintains sorted order automatically
        let mut related = BTreeSet::new();

        // Check outgoing edges
        for edge in self.graph.edges_directed(node, Direction::Outgoing) {
            if &edge.weight().relation_type == relation_type
                && let Some(target_name) = self.node_to_entity.get(&edge.target())
            {
                related.insert(target_name.clone());
            }
        }

        // Check incoming edges
        for edge in self.graph.edges_directed(node, Direction::Incoming) {
            if &edge.weight().relation_type == relation_type
                && let Some(source_name) = self.node_to_entity.get(&edge.source())
            {
                related.insert(source_name.clone());
            }
        }

        // BTreeSet iterator is already sorted
        related.into_iter().collect()
    }

    /// Get entities by type - unchanged from original
    pub fn get_entities_by_type(&self, entity_type: &EntityType) -> Vec<&EntityData> {
        self.entities
            .values()
            .filter(|entity| entity.entity_type == *entity_type)
            .collect()
    }

    /// Get most important entities - unchanged from original
    pub fn get_most_important_entities(&self, limit: usize) -> Vec<&EntityData> {
        let mut entities: Vec<&EntityData> = self.entities.values().collect();
        entities.sort_by(|a, b| b.importance_score.partial_cmp(&a.importance_score).unwrap());
        entities.into_iter().take(limit).collect()
    }

    /// Get recently mentioned entities - unchanged from original
    pub fn get_recently_mentioned_entities(&self, limit: usize) -> Vec<&EntityData> {
        let mut entities: Vec<&EntityData> = self.entities.values().collect();
        entities.sort_by(|a, b| b.last_mentioned.cmp(&a.last_mentioned));
        entities.into_iter().take(limit).collect()
    }

    /// Search entities by name or description (case-insensitive)
    pub fn search_entities(&self, query: &str) -> Vec<&EntityData> {
        let query_lower = query.to_lowercase();

        // Fast path: exact key lookup
        if let Some(entity) = self.entities.get(query) {
            return vec![entity];
        }

        // Case-insensitive key lookup
        if let Some(entity) = self.entities.get(&query_lower) {
            return vec![entity];
        }

        // Fallback: substring search
        self.entities
            .values()
            .filter(|entity| {
                entity.name.to_lowercase().contains(&query_lower)
                    || entity
                        .description
                        .as_ref()
                        .is_some_and(|desc| desc.to_lowercase().contains(&query_lower))
            })
            .collect()
    }

    /// Get entity context with improved relationship lookup
    pub fn get_entity_context(&self, entity_name: &str) -> Option<String> {
        let entity = self.entities.get(entity_name)?;
        let related = self.find_related_entities(entity_name);

        let mut context = format!(
            "Entity: {} (Type: {:?}, Mentions: {}, Importance: {:.2})",
            entity.name, entity.entity_type, entity.mention_count, entity.importance_score
        );

        if let Some(desc) = &entity.description {
            context.push_str(&format!("\nDescription: {}", desc));
        }

        if !related.is_empty() {
            context.push_str(&format!("\nRelated entities: {}", related.join(", ")));
        }

        // Add relationship details using efficient petgraph lookups
        let relationships = self.get_entity_relationships(entity_name);
        if !relationships.is_empty() {
            context.push_str("\nRelationships:");
            for (related_entity, relation_type, relation_context) in relationships {
                context.push_str(&format!(
                    "\n  - {} ({:?}): {}",
                    related_entity, relation_type, relation_context
                ));
            }
        }

        Some(context)
    }

    /// Get entity relationships - now uses efficient petgraph edge iteration
    /// Returns (entity_name, relation_type, context) tuples
    pub fn get_entity_relationships(
        &self,
        entity_name: &str,
    ) -> Vec<(String, RelationType, String)> {
        let node = match self.entity_to_node.get(entity_name) {
            Some(&node) => node,
            None => return Vec::new(),
        };

        let mut relationships = Vec::new();

        // Outgoing relationships
        for edge in self.graph.edges_directed(node, Direction::Outgoing) {
            if let Some(target_name) = self.node_to_entity.get(&edge.target()) {
                let edge_data = edge.weight();
                relationships.push((
                    target_name.clone(),
                    edge_data.relation_type.clone(),
                    edge_data.context.clone(), // Now returns stored context!
                ));
            }
        }

        // Incoming relationships
        for edge in self.graph.edges_directed(node, Direction::Incoming) {
            if let Some(source_name) = self.node_to_entity.get(&edge.source()) {
                let edge_data = edge.weight();
                relationships.push((
                    source_name.clone(),
                    edge_data.relation_type.clone(),
                    edge_data.context.clone(), // Now returns stored context!
                ));
            }
        }

        relationships
    }

    /// Trace entity relationships using efficient BFS
    pub fn trace_entity_relationships(
        &self,
        start_entity: &str,
        max_depth: usize,
    ) -> Vec<(String, String, String)> {
        let start_node = match self.entity_to_node.get(start_entity) {
            Some(&node) => node,
            None => return Vec::new(),
        };

        // Pre-allocate with reasonable capacity estimate
        let estimated_capacity = self.graph.edge_count().min(max_depth * 10);
        let mut visited = HashSet::with_capacity(estimated_capacity);
        let mut trace = Vec::with_capacity(estimated_capacity);
        let mut queue = VecDeque::with_capacity(estimated_capacity);

        queue.push_back((start_node, 0));
        visited.insert(start_node);

        while let Some((current_node, depth)) = queue.pop_front() {
            if depth >= max_depth {
                continue;
            }

            let current_name = match self.node_to_entity.get(&current_node) {
                Some(name) => name,
                None => continue,
            };

            // Process all neighbors efficiently
            for edge in self.graph.edges_directed(current_node, Direction::Outgoing) {
                let target_node = edge.target();
                if !visited.contains(&target_node) {
                    visited.insert(target_node);
                    queue.push_back((target_node, depth + 1));

                    if let Some(target_name) = self.node_to_entity.get(&target_node) {
                        trace.push((
                            current_name.clone(),
                            format!("{:?}", edge.weight().relation_type),
                            target_name.clone(),
                        ));
                    }
                }
            }

            // Also process incoming edges
            for edge in self.graph.edges_directed(current_node, Direction::Incoming) {
                let source_node = edge.source();
                if !visited.contains(&source_node) {
                    visited.insert(source_node);
                    queue.push_back((source_node, depth + 1));

                    if let Some(source_name) = self.node_to_entity.get(&source_node) {
                        trace.push((
                            source_name.clone(),
                            format!("inverse_{:?}", edge.weight().relation_type),
                            current_name.clone(),
                        ));
                    }
                }
            }
        }

        trace
    }

    /// Get entity network using efficient graph traversal
    pub fn get_entity_network(&self, center_entity: &str, max_depth: usize) -> EntityNetwork {
        let mut network = EntityNetwork {
            center: center_entity.to_string(),
            entities: BTreeMap::new(),
            relationships: Vec::new(),
        };

        let start_node = match self.entity_to_node.get(center_entity) {
            Some(&node) => node,
            None => return network,
        };

        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();
        // O(1) duplicate check for relationships instead of O(n) iter().any()
        let mut seen_relationships: HashSet<(String, String, RelationType)> = HashSet::new();

        // Add center entity
        if let Some(entity) = self.entities.get(center_entity) {
            network
                .entities
                .insert(center_entity.to_string(), entity.clone());
            queue.push_back((start_node, 0));
            visited.insert(start_node);
        }

        while let Some((current_node, depth)) = queue.pop_front() {
            if depth >= max_depth {
                continue;
            }

            // Process outgoing edges
            for edge in self.graph.edges_directed(current_node, Direction::Outgoing) {
                let target_node = edge.target();
                if !visited.contains(&target_node) {
                    visited.insert(target_node);
                    queue.push_back((target_node, depth + 1));

                    // Add entity to network
                    if let Some(target_name) = self.node_to_entity.get(&target_node)
                        && let Some(entity_data) = self.entities.get(target_name)
                    {
                        network
                            .entities
                            .insert(target_name.clone(), entity_data.clone());
                    }
                }

                // Add relationship to network (with O(1) duplicate check)
                if let (Some(from_name), Some(to_name)) = (
                    self.node_to_entity.get(&current_node),
                    self.node_to_entity.get(&target_node),
                ) && network.entities.contains_key(from_name)
                    && network.entities.contains_key(to_name)
                {
                    let edge_data = edge.weight();
                    let rel_key = (
                        from_name.clone(),
                        to_name.clone(),
                        edge_data.relation_type.clone(),
                    );
                    if seen_relationships.insert(rel_key) {
                        network.relationships.push(EntityRelationship {
                            from_entity: from_name.clone(),
                            to_entity: to_name.clone(),
                            relation_type: edge_data.relation_type.clone(),
                            context: edge_data.context.clone(), // Now uses stored context!
                        });
                    }
                }
            }

            // Process incoming edges
            for edge in self.graph.edges_directed(current_node, Direction::Incoming) {
                let source_node = edge.source();
                if !visited.contains(&source_node) {
                    visited.insert(source_node);
                    queue.push_back((source_node, depth + 1));

                    // Add entity to network
                    if let Some(source_name) = self.node_to_entity.get(&source_node)
                        && let Some(entity_data) = self.entities.get(source_name)
                    {
                        network
                            .entities
                            .insert(source_name.clone(), entity_data.clone());
                    }
                }

                // Add relationship to network (with O(1) duplicate check)
                if let (Some(from_name), Some(to_name)) = (
                    self.node_to_entity.get(&source_node),
                    self.node_to_entity.get(&current_node),
                ) && network.entities.contains_key(from_name)
                    && network.entities.contains_key(to_name)
                {
                    let edge_data = edge.weight();
                    let rel_key = (
                        from_name.clone(),
                        to_name.clone(),
                        edge_data.relation_type.clone(),
                    );
                    if seen_relationships.insert(rel_key) {
                        network.relationships.push(EntityRelationship {
                            from_entity: from_name.clone(),
                            to_entity: to_name.clone(),
                            relation_type: edge_data.relation_type.clone(),
                            context: edge_data.context.clone(), // Now uses stored context!
                        });
                    }
                }
            }
        }

        network
    }

    /// Analyze entity importance with graph metrics
    pub fn analyze_entity_importance(&self) -> Vec<EntityAnalysis> {
        // Pre-allocate capacity to avoid reallocations
        let mut analyses = Vec::with_capacity(self.entities.len());

        for (name, entity) in &self.entities {
            let node = self.entity_to_node.get(name);
            let relationship_count = match node {
                Some(&n) => {
                    self.graph.edges_directed(n, Direction::Outgoing).count()
                        + self.graph.edges_directed(n, Direction::Incoming).count()
                }
                None => {
                    warn!(
                        "Entity '{}' exists in entities map but has no graph node - possible data inconsistency",
                        name
                    );
                    0
                }
            };

            let mentions_in_updates = self
                .entity_mentions
                .get(name)
                .map(|mentions| mentions.len())
                .unwrap_or(0);

            // Enhanced importance with graph centrality
            let degree_centrality = relationship_count as f32;
            let enhanced_importance = entity.importance_score + (degree_centrality * 0.1);

            let entity_analysis = EntityAnalysis {
                entity_name: name.clone(),
                importance_score: enhanced_importance,
                mention_count: entity.mention_count,
                relationship_count,
                update_references: mentions_in_updates,
                first_seen: entity.first_mentioned,
                last_seen: entity.last_mentioned,
            };

            analyses.push(entity_analysis);
        }

        // Sort by enhanced importance score
        analyses.sort_by(|a, b| b.importance_score.partial_cmp(&a.importance_score).unwrap());
        analyses
    }

    /// New petgraph-specific methods
    ///
    /// Find shortest path between two entities using A* algorithm
    /// Returns the complete path including start and end nodes
    pub fn find_shortest_path(&self, from: &str, to: &str) -> Option<Vec<String>> {
        use petgraph::algo::astar;

        // Case-insensitive node lookup helper
        let find_node = |name: &str| -> Option<&NodeIndex> {
            self.entity_to_node.get(name).or_else(|| {
                let lower = name.to_lowercase();
                self.entity_to_node
                    .iter()
                    .find(|(k, _)| k.to_lowercase() == lower)
                    .map(|(_, v)| v)
            })
        };

        let from_node = find_node(from)?;
        let to_node = find_node(to)?;

        // Use A* with uniform edge cost (1) and zero heuristic (becomes Dijkstra)
        let result = astar(
            &self.graph,
            *from_node,
            |node| node == *to_node,
            |_| 1, // All edges have cost 1
            |_| 0, // No heuristic (equivalent to Dijkstra)
        );

        result.map(|(_, path)| {
            path.into_iter()
                .filter_map(|node| self.node_to_entity.get(&node).cloned())
                .collect()
        })
    }

    /// Get graph statistics
    pub fn get_graph_stats(&self) -> GraphStats {
        GraphStats {
            node_count: self.graph.node_count(),
            edge_count: self.graph.edge_count(),
            entity_count: self.entities.len(),
            avg_degree: if self.graph.node_count() > 0 {
                (self.graph.edge_count() * 2) as f64 / self.graph.node_count() as f64
            } else {
                0.0
            },
        }
    }

    /// Find strongly connected components
    pub fn find_communities(&self) -> Vec<Vec<String>> {
        use petgraph::algo::kosaraju_scc;

        let components = kosaraju_scc(&self.graph);

        components
            .into_iter()
            .map(|component| {
                component
                    .into_iter()
                    .filter_map(|node| self.node_to_entity.get(&node).cloned())
                    .collect()
            })
            .filter(|component: &Vec<String>| component.len() > 1) // Only return actual communities
            .collect()
    }

    /// Get all relationships in the graph as EntityRelationship structs
    /// Used for migration to SurrealDB native graph format
    pub fn get_all_relationships(&self) -> Vec<EntityRelationship> {
        self.graph
            .edge_references()
            .filter_map(|edge| {
                let source_name = self.node_to_entity.get(&edge.source())?;
                let target_name = self.node_to_entity.get(&edge.target())?;
                let edge_data = edge.weight();
                Some(EntityRelationship {
                    from_entity: source_name.clone(),
                    to_entity: target_name.clone(),
                    relation_type: edge_data.relation_type.clone(),
                    context: edge_data.context.clone(),
                })
            })
            .collect()
    }

    /// Get all entities as a vector (convenience method for migration)
    pub fn get_all_entities(&self) -> Vec<EntityData> {
        self.entities.values().cloned().collect()
    }
}

/// Entity network structure - uses BTreeMap for deterministic serialization
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EntityNetwork {
    /// Name of the central entity
    pub center: String,
    /// All entities reachable within the traversal depth
    pub entities: BTreeMap<String, EntityData>,
    /// Relationships connecting the entities in this network
    pub relationships: Vec<EntityRelationship>,
}

/// Entity analysis structure - unchanged for compatibility
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EntityAnalysis {
    /// Name of the analyzed entity
    pub entity_name: String,
    /// Combined importance score including graph centrality
    pub importance_score: f32,
    /// Total number of times the entity was mentioned
    pub mention_count: u32,
    /// Number of graph edges connected to this entity
    pub relationship_count: usize,
    /// Number of context updates referencing this entity
    pub update_references: usize,
    /// Timestamp when the entity was first seen
    pub first_seen: DateTime<Utc>,
    /// Timestamp when the entity was last referenced
    pub last_seen: DateTime<Utc>,
}

/// Summary statistics for the entity graph
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GraphStats {
    /// Number of nodes in the petgraph
    pub node_count: usize,
    /// Number of directed edges in the petgraph
    pub edge_count: usize,
    /// Number of entities in the metadata map
    pub entity_count: usize,
    /// Average degree (edges × 2 / nodes)
    pub avg_degree: f64,
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;

    fn create_test_graph() -> SimpleEntityGraph {
        let mut graph = SimpleEntityGraph::new();

        // Add some entities
        graph.add_or_update_entity(
            "rust".to_string(),
            EntityType::Technology,
            Utc::now(),
            "Systems programming language",
        );

        graph.add_or_update_entity(
            "petgraph".to_string(),
            EntityType::Technology,
            Utc::now(),
            "Graph data structure library",
        );

        graph.add_or_update_entity(
            "performance".to_string(),
            EntityType::Concept,
            Utc::now(),
            "System performance optimization",
        );

        // Add relationships
        graph.add_relationship(EntityRelationship {
            from_entity: "rust".to_string(),
            to_entity: "petgraph".to_string(),
            relation_type: RelationType::Implements,
            context: "Rust implements petgraph".to_string(),
        });

        graph.add_relationship(EntityRelationship {
            from_entity: "petgraph".to_string(),
            to_entity: "performance".to_string(),
            relation_type: RelationType::Implements,
            context: "Petgraph improves performance".to_string(),
        });

        graph
    }

    #[test]
    fn test_petgraph_find_related_entities() {
        let graph = create_test_graph();

        let related = graph.find_related_entities("rust");
        assert!(related.contains(&"petgraph".to_string()));

        let related = graph.find_related_entities("petgraph");
        assert!(related.contains(&"rust".to_string()));
        assert!(related.contains(&"performance".to_string()));
    }

    #[test]
    fn test_petgraph_find_related_by_type() {
        let graph = create_test_graph();

        let related = graph.find_related_entities_by_type("rust", &RelationType::Implements);
        assert!(related.contains(&"petgraph".to_string()));

        let related = graph.find_related_entities_by_type("petgraph", &RelationType::Implements);
        assert!(related.contains(&"performance".to_string()));
    }

    #[test]
    fn test_graph_stats() {
        let graph = create_test_graph();
        let stats = graph.get_graph_stats();

        assert_eq!(stats.node_count, 3);
        assert_eq!(stats.edge_count, 2);
        assert_eq!(stats.entity_count, 3);
        assert!(stats.avg_degree > 0.0);
    }

    #[test]
    fn test_shortest_path() {
        let graph = create_test_graph();

        let path = graph.find_shortest_path("rust", "performance");
        assert!(path.is_some());

        let path = graph.find_shortest_path("rust", "nonexistent");
        assert!(path.is_none());
    }

    #[test]
    fn test_trace_relationships_petgraph() {
        let graph = create_test_graph();

        let trace = graph.trace_entity_relationships("rust", 2);
        assert!(!trace.is_empty());

        // Should find path rust -> petgraph -> performance
        let has_rust_to_petgraph = trace
            .iter()
            .any(|(from, _, to)| from == "rust" && to == "petgraph");
        assert!(has_rust_to_petgraph);
    }

    #[test]
    fn test_entity_network_petgraph() {
        let graph = create_test_graph();

        let network = graph.get_entity_network("petgraph", 2);
        assert_eq!(network.center, "petgraph");
        assert!(network.entities.contains_key("petgraph"));
        assert!(network.entities.contains_key("rust"));
        assert!(network.entities.contains_key("performance"));
        assert!(!network.relationships.is_empty());
    }

    #[test]
    fn test_enhanced_importance_scoring() {
        let mut graph = create_test_graph();

        // Add more relationships to petgraph to increase its importance
        graph.add_or_update_entity(
            "optimization".to_string(),
            EntityType::Concept,
            Utc::now(),
            "Code optimization",
        );

        graph.add_relationship(EntityRelationship {
            from_entity: "petgraph".to_string(),
            to_entity: "optimization".to_string(),
            relation_type: RelationType::LeadsTo,
            context: "Petgraph leads to optimization".to_string(),
        });

        let analysis = graph.analyze_entity_importance();

        // Petgraph should have higher importance due to more connections
        let petgraph_analysis = analysis
            .iter()
            .find(|a| a.entity_name == "petgraph")
            .unwrap();

        let rust_analysis = analysis.iter().find(|a| a.entity_name == "rust").unwrap();

        assert!(petgraph_analysis.importance_score >= rust_analysis.importance_score);
        assert_eq!(petgraph_analysis.relationship_count, 3); // Connected to rust, performance, optimization
    }

    #[test]
    fn test_communities_detection() {
        let mut graph = SimpleEntityGraph::new();

        // Create a more complex graph with communities
        let entities = vec!["a", "b", "c", "d", "e"];
        for entity in &entities {
            graph.add_or_update_entity(entity.to_string(), EntityType::Concept, Utc::now(), "");
        }

        // Create some cycles for strongly connected components
        graph.add_relationship(EntityRelationship {
            from_entity: "a".to_string(),
            to_entity: "b".to_string(),
            relation_type: RelationType::RelatedTo,
            context: "test".to_string(),
        });

        graph.add_relationship(EntityRelationship {
            from_entity: "b".to_string(),
            to_entity: "c".to_string(),
            relation_type: RelationType::RelatedTo,
            context: "test".to_string(),
        });

        graph.add_relationship(EntityRelationship {
            from_entity: "c".to_string(),
            to_entity: "a".to_string(),
            relation_type: RelationType::RelatedTo,
            context: "test".to_string(),
        });

        let communities = graph.find_communities();

        // Should find at least one community
        if !communities.is_empty() {
            let first_community = &communities[0];
            assert!(first_community.len() >= 2);
        }
    }

    #[test]
    fn test_graph_serialization_persistence() {
        // Create a complex graph with multiple entities and relationships
        let mut original_graph = SimpleEntityGraph::new();

        // Add entities
        original_graph.add_or_update_entity(
            "rust".to_string(),
            EntityType::Technology,
            Utc::now(),
            "Systems programming language",
        );
        original_graph.add_or_update_entity(
            "petgraph".to_string(),
            EntityType::Technology,
            Utc::now(),
            "Graph library",
        );
        original_graph.add_or_update_entity(
            "performance".to_string(),
            EntityType::Concept,
            Utc::now(),
            "Performance optimization",
        );
        original_graph.add_or_update_entity(
            "lock-free".to_string(),
            EntityType::Concept,
            Utc::now(),
            "Lock-free data structures",
        );

        // Add relationships
        original_graph.add_relationship(EntityRelationship {
            from_entity: "rust".to_string(),
            to_entity: "petgraph".to_string(),
            relation_type: RelationType::Implements,
            context: "Rust implements petgraph".to_string(),
        });

        original_graph.add_relationship(EntityRelationship {
            from_entity: "petgraph".to_string(),
            to_entity: "performance".to_string(),
            relation_type: RelationType::LeadsTo,
            context: "Petgraph improves performance".to_string(),
        });

        original_graph.add_relationship(EntityRelationship {
            from_entity: "lock-free".to_string(),
            to_entity: "performance".to_string(),
            relation_type: RelationType::LeadsTo,
            context: "Lock-free improves performance".to_string(),
        });

        original_graph.add_relationship(EntityRelationship {
            from_entity: "rust".to_string(),
            to_entity: "lock-free".to_string(),
            relation_type: RelationType::Implements,
            context: "Rust supports lock-free".to_string(),
        });

        // Get original state
        let original_stats = original_graph.get_graph_stats();
        let original_rust_related = original_graph.find_related_entities("rust");
        let original_petgraph_related = original_graph.find_related_entities("petgraph");
        let original_trace = original_graph.trace_entity_relationships("rust", 3);
        let original_network = original_graph.get_entity_network("rust", 2);

        // Serialize
        let serialized = serde_json::to_string(&original_graph).expect("Serialization failed");

        // Deserialize
        let deserialized_graph: SimpleEntityGraph =
            serde_json::from_str(&serialized).expect("Deserialization failed");

        // Verify graph stats match
        let deserialized_stats = deserialized_graph.get_graph_stats();
        assert_eq!(
            original_stats.node_count, deserialized_stats.node_count,
            "Node count mismatch"
        );
        assert_eq!(
            original_stats.edge_count, deserialized_stats.edge_count,
            "Edge count mismatch"
        );
        assert_eq!(
            original_stats.entity_count, deserialized_stats.entity_count,
            "Entity count mismatch"
        );

        // Verify relationships are preserved
        let deserialized_rust_related = deserialized_graph.find_related_entities("rust");
        assert_eq!(
            original_rust_related.len(),
            deserialized_rust_related.len(),
            "Rust relationships count mismatch"
        );
        assert!(
            deserialized_rust_related.contains(&"petgraph".to_string()),
            "Missing rust->petgraph relationship"
        );
        assert!(
            deserialized_rust_related.contains(&"lock-free".to_string()),
            "Missing rust->lock-free relationship"
        );

        let deserialized_petgraph_related = deserialized_graph.find_related_entities("petgraph");
        assert_eq!(
            original_petgraph_related.len(),
            deserialized_petgraph_related.len(),
            "Petgraph relationships count mismatch"
        );

        // Verify graph traversal works
        let deserialized_trace = deserialized_graph.trace_entity_relationships("rust", 3);
        assert_eq!(
            original_trace.len(),
            deserialized_trace.len(),
            "Trace length mismatch"
        );

        // Verify entity network works
        let deserialized_network = deserialized_graph.get_entity_network("rust", 2);
        assert_eq!(
            original_network.entities.len(),
            deserialized_network.entities.len(),
            "Network entities count mismatch"
        );
        assert_eq!(
            original_network.relationships.len(),
            deserialized_network.relationships.len(),
            "Network relationships count mismatch"
        );

        // Verify entity_to_node and node_to_entity mappings are rebuilt
        assert_eq!(
            deserialized_graph.entity_to_node.len(),
            4,
            "entity_to_node mapping not rebuilt"
        );
        assert_eq!(
            deserialized_graph.node_to_entity.len(),
            4,
            "node_to_entity mapping not rebuilt"
        );

        // Verify all entities have valid node mappings
        for entity_name in ["rust", "petgraph", "performance", "lock-free"] {
            assert!(
                deserialized_graph.entity_to_node.contains_key(entity_name),
                "Missing entity_to_node mapping for {}",
                entity_name
            );
            let node_idx = deserialized_graph.entity_to_node[entity_name];
            assert_eq!(
                deserialized_graph.node_to_entity[&node_idx], entity_name,
                "Inconsistent bidirectional mapping for {}",
                entity_name
            );
        }

        // Verify specific relationship queries work
        let rust_implements =
            deserialized_graph.find_related_entities_by_type("rust", &RelationType::Implements);
        assert_eq!(
            rust_implements.len(),
            2,
            "Should have 2 'Implements' relationships"
        );
        assert!(rust_implements.contains(&"petgraph".to_string()));
        assert!(rust_implements.contains(&"lock-free".to_string()));

        // Verify shortest path still works
        let path = deserialized_graph.find_shortest_path("rust", "performance");
        assert!(path.is_some(), "Should find path from rust to performance");

        println!(
            "✓ Graph serialization/deserialization preserves all relationships and functionality"
        );
    }

    #[test]
    fn test_empty_graph_serialization() {
        let original = SimpleEntityGraph::new();
        let serialized = serde_json::to_string(&original).expect("Serialization failed");
        let deserialized: SimpleEntityGraph =
            serde_json::from_str(&serialized).expect("Deserialization failed");

        assert_eq!(deserialized.get_graph_stats().node_count, 0);
        assert_eq!(deserialized.get_graph_stats().edge_count, 0);
        assert_eq!(deserialized.entity_to_node.len(), 0);
        assert_eq!(deserialized.node_to_entity.len(), 0);
    }

    #[test]
    fn test_graph_with_orphan_entities() {
        // Test graph with entities that have no relationships
        let mut graph = SimpleEntityGraph::new();

        graph.add_or_update_entity(
            "orphan1".to_string(),
            EntityType::Concept,
            Utc::now(),
            "Orphan entity",
        );
        graph.add_or_update_entity(
            "orphan2".to_string(),
            EntityType::Concept,
            Utc::now(),
            "Another orphan",
        );
        graph.add_or_update_entity(
            "connected1".to_string(),
            EntityType::Concept,
            Utc::now(),
            "Connected entity",
        );
        graph.add_or_update_entity(
            "connected2".to_string(),
            EntityType::Concept,
            Utc::now(),
            "Another connected",
        );

        graph.add_relationship(EntityRelationship {
            from_entity: "connected1".to_string(),
            to_entity: "connected2".to_string(),
            relation_type: RelationType::RelatedTo,
            context: "test".to_string(),
        });

        let serialized = serde_json::to_string(&graph).expect("Serialization failed");
        let deserialized: SimpleEntityGraph =
            serde_json::from_str(&serialized).expect("Deserialization failed");

        // All entities should be preserved
        assert_eq!(deserialized.entities.len(), 4);
        assert_eq!(deserialized.get_graph_stats().node_count, 4);
        assert_eq!(deserialized.get_graph_stats().edge_count, 1);

        // Verify orphan entities are accessible
        assert!(deserialized.entities.contains_key("orphan1"));
        assert!(deserialized.entities.contains_key("orphan2"));

        // Verify connected entities maintain their relationship
        let connected1_related = deserialized.find_related_entities("connected1");
        assert!(connected1_related.contains(&"connected2".to_string()));
    }

    // ==================== NEW TESTS FOR RECENT FIXES ====================

    #[test]
    fn test_shortest_path_returns_full_path() {
        // Create a longer chain: a -> b -> c -> d
        let mut graph = SimpleEntityGraph::new();

        for entity in ["a", "b", "c", "d"] {
            graph.add_or_update_entity(entity.to_string(), EntityType::Concept, Utc::now(), "");
        }

        graph.add_relationship(EntityRelationship {
            from_entity: "a".to_string(),
            to_entity: "b".to_string(),
            relation_type: RelationType::LeadsTo,
            context: "".to_string(),
        });
        graph.add_relationship(EntityRelationship {
            from_entity: "b".to_string(),
            to_entity: "c".to_string(),
            relation_type: RelationType::LeadsTo,
            context: "".to_string(),
        });
        graph.add_relationship(EntityRelationship {
            from_entity: "c".to_string(),
            to_entity: "d".to_string(),
            relation_type: RelationType::LeadsTo,
            context: "".to_string(),
        });

        let path = graph.find_shortest_path("a", "d");
        assert!(path.is_some(), "Path should exist from a to d");

        let path = path.unwrap();
        // Should return full path: [a, b, c, d], not just [a, d]
        assert_eq!(path.len(), 4, "Path should have 4 nodes: a -> b -> c -> d");
        assert_eq!(path[0], "a");
        assert_eq!(path[1], "b");
        assert_eq!(path[2], "c");
        assert_eq!(path[3], "d");
    }

    #[test]
    fn test_shortest_path_finds_shortest() {
        // Create graph with two paths: a -> b -> d (length 2) and a -> c -> e -> d (length 3)
        let mut graph = SimpleEntityGraph::new();

        for entity in ["a", "b", "c", "d", "e"] {
            graph.add_or_update_entity(entity.to_string(), EntityType::Concept, Utc::now(), "");
        }

        // Short path: a -> b -> d
        graph.add_relationship(EntityRelationship {
            from_entity: "a".to_string(),
            to_entity: "b".to_string(),
            relation_type: RelationType::LeadsTo,
            context: "".to_string(),
        });
        graph.add_relationship(EntityRelationship {
            from_entity: "b".to_string(),
            to_entity: "d".to_string(),
            relation_type: RelationType::LeadsTo,
            context: "".to_string(),
        });

        // Long path: a -> c -> e -> d
        graph.add_relationship(EntityRelationship {
            from_entity: "a".to_string(),
            to_entity: "c".to_string(),
            relation_type: RelationType::LeadsTo,
            context: "".to_string(),
        });
        graph.add_relationship(EntityRelationship {
            from_entity: "c".to_string(),
            to_entity: "e".to_string(),
            relation_type: RelationType::LeadsTo,
            context: "".to_string(),
        });
        graph.add_relationship(EntityRelationship {
            from_entity: "e".to_string(),
            to_entity: "d".to_string(),
            relation_type: RelationType::LeadsTo,
            context: "".to_string(),
        });

        let path = graph.find_shortest_path("a", "d").unwrap();
        // Should find the shorter path (length 3: a, b, d)
        assert_eq!(path.len(), 3, "Should find shortest path with 3 nodes");
        assert_eq!(path[0], "a");
        assert_eq!(path[2], "d");
    }

    #[test]
    fn test_relation_type_ordering() {
        // Verify RelationType implements Ord correctly
        assert!(RelationType::RequiredBy < RelationType::LeadsTo);
        assert!(RelationType::LeadsTo < RelationType::RelatedTo);
        assert!(RelationType::RelatedTo < RelationType::ConflictsWith);
        assert!(RelationType::ConflictsWith < RelationType::DependsOn);
        assert!(RelationType::DependsOn < RelationType::Implements);
        assert!(RelationType::Implements < RelationType::CausedBy);
        assert!(RelationType::CausedBy < RelationType::Solves);

        // Test that sorting works
        let mut types = [
            RelationType::Solves,
            RelationType::RequiredBy,
            RelationType::LeadsTo,
        ];
        types.sort();
        assert_eq!(types[0], RelationType::RequiredBy);
        assert_eq!(types[1], RelationType::LeadsTo);
        assert_eq!(types[2], RelationType::Solves);
    }

    #[test]
    fn test_serialization_determinism() {
        // Verify that serializing the same graph twice produces identical JSON
        // With BTreeMap, the entire JSON should be deterministic
        let mut graph = SimpleEntityGraph::new();

        // Add entities in non-alphabetical order
        for entity in ["zebra", "apple", "mango", "banana"] {
            graph.add_or_update_entity(entity.to_string(), EntityType::Concept, Utc::now(), "");
        }

        // Add relationships
        graph.add_relationship(EntityRelationship {
            from_entity: "zebra".to_string(),
            to_entity: "apple".to_string(),
            relation_type: RelationType::RelatedTo,
            context: "".to_string(),
        });
        graph.add_relationship(EntityRelationship {
            from_entity: "apple".to_string(),
            to_entity: "mango".to_string(),
            relation_type: RelationType::LeadsTo,
            context: "".to_string(),
        });

        // Serialize twice - should be identical with BTreeMap
        let json1 = serde_json::to_string(&graph).unwrap();
        let json2 = serde_json::to_string(&graph).unwrap();
        assert_eq!(json1, json2, "Serialization should be deterministic");

        // Parse and verify structure
        let parsed: serde_json::Value = serde_json::from_str(&json1).unwrap();

        // Verify entities are sorted alphabetically (BTreeMap)
        let entities_keys: Vec<&str> = parsed["entities"]
            .as_object()
            .unwrap()
            .keys()
            .map(|s| s.as_str())
            .collect();
        assert_eq!(entities_keys, vec!["apple", "banana", "mango", "zebra"]);

        // Verify graph nodes are sorted alphabetically
        let nodes = parsed["graph"]["nodes"].as_array().unwrap();
        assert_eq!(nodes[0], "apple");
        assert_eq!(nodes[1], "banana");
        assert_eq!(nodes[2], "mango");
        assert_eq!(nodes[3], "zebra");

        // Verify edges are sorted (by from_entity, then to_entity)
        let edges = parsed["graph"]["edges"].as_array().unwrap();
        assert_eq!(edges[0][0], "apple"); // apple -> mango comes before zebra -> apple
        assert_eq!(edges[1][0], "zebra");

        // Deserialize and re-serialize - should produce identical JSON
        let deserialized: SimpleEntityGraph = serde_json::from_str(&json1).unwrap();
        let json3 = serde_json::to_string(&deserialized).unwrap();
        assert_eq!(
            json1, json3,
            "Re-serialization should produce identical JSON"
        );

        // Verify functionality preserved
        assert_eq!(deserialized.entities.len(), 4);
        assert_eq!(deserialized.get_graph_stats().edge_count, 2);

        // Verify relationships work after deserialization
        let related = deserialized.find_related_entities("apple");
        assert!(related.contains(&"zebra".to_string()));
        assert!(related.contains(&"mango".to_string()));
    }

    #[test]
    fn test_find_related_entities_returns_sorted() {
        let mut graph = SimpleEntityGraph::new();

        // Add entities
        for entity in ["center", "zebra", "apple", "mango", "banana"] {
            graph.add_or_update_entity(entity.to_string(), EntityType::Concept, Utc::now(), "");
        }

        // Connect all to center (in non-alphabetical order)
        for entity in ["zebra", "apple", "mango", "banana"] {
            graph.add_relationship(EntityRelationship {
                from_entity: "center".to_string(),
                to_entity: entity.to_string(),
                relation_type: RelationType::RelatedTo,
                context: "".to_string(),
            });
        }

        let related = graph.find_related_entities("center");

        // Verify results are sorted alphabetically
        assert_eq!(related.len(), 4);
        assert_eq!(related[0], "apple");
        assert_eq!(related[1], "banana");
        assert_eq!(related[2], "mango");
        assert_eq!(related[3], "zebra");
    }

    #[test]
    fn test_entity_network_no_duplicate_relationships() {
        let mut graph = SimpleEntityGraph::new();

        // Create a small network
        for entity in ["a", "b", "c"] {
            graph.add_or_update_entity(entity.to_string(), EntityType::Concept, Utc::now(), "");
        }

        // Create bidirectional relationships (could cause duplicates)
        graph.add_relationship(EntityRelationship {
            from_entity: "a".to_string(),
            to_entity: "b".to_string(),
            relation_type: RelationType::RelatedTo,
            context: "".to_string(),
        });
        graph.add_relationship(EntityRelationship {
            from_entity: "b".to_string(),
            to_entity: "c".to_string(),
            relation_type: RelationType::RelatedTo,
            context: "".to_string(),
        });
        graph.add_relationship(EntityRelationship {
            from_entity: "c".to_string(),
            to_entity: "a".to_string(),
            relation_type: RelationType::RelatedTo,
            context: "".to_string(),
        });

        let network = graph.get_entity_network("a", 3);

        // Count occurrences of each relationship
        let mut rel_counts: std::collections::HashMap<(String, String, RelationType), usize> =
            std::collections::HashMap::new();
        for rel in &network.relationships {
            let key = (
                rel.from_entity.clone(),
                rel.to_entity.clone(),
                rel.relation_type.clone(),
            );
            *rel_counts.entry(key).or_insert(0) += 1;
        }

        // Verify no duplicates (all counts should be 1)
        for ((from, to, _), count) in rel_counts {
            assert_eq!(
                count, 1,
                "Relationship {}->{} should appear exactly once, found {}",
                from, to, count
            );
        }
    }

    #[test]
    fn test_analyze_entity_importance_capacity_preallocation() {
        // Test that analyze_entity_importance works correctly with many entities
        let mut graph = SimpleEntityGraph::new();

        // Add many entities to test capacity pre-allocation
        for i in 0..100 {
            graph.add_or_update_entity(
                format!("entity_{}", i),
                EntityType::Concept,
                Utc::now(),
                "",
            );
        }

        // Add some relationships
        for i in 0..50 {
            graph.add_relationship(EntityRelationship {
                from_entity: format!("entity_{}", i),
                to_entity: format!("entity_{}", i + 50),
                relation_type: RelationType::RelatedTo,
                context: "".to_string(),
            });
        }

        let analysis = graph.analyze_entity_importance();

        // Should have all 100 entities analyzed
        assert_eq!(analysis.len(), 100);

        // Verify it's sorted by importance (descending)
        for i in 1..analysis.len() {
            assert!(
                analysis[i - 1].importance_score >= analysis[i].importance_score,
                "Analysis should be sorted by importance descending"
            );
        }
    }
}