car-memgine 0.14.0

Memgine — graph-based memory engine for Common Agent Runtime
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
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
//! The memory graph — nodes are memory entries, edges are relationships.
//!
//! Replaces ImmutableStore + LayerState + 6 HashMaps with a single graph.

use chrono::{DateTime, Utc};
use petgraph::stable_graph::{NodeIndex, StableGraph};
use petgraph::visit::EdgeRef;
use petgraph::Direction;
use std::collections::{HashMap, HashSet, VecDeque};

pub type Layer = u8;

/// Namespace for memory nodes. Keeps external/foreign code knowledge from
/// polluting the project's own memory graph during spreading activation.
///
/// Stored in a side-table keyed by `NodeIndex` so adding partitions does not
/// require rewriting every `MemNode { .. }` literal. Absence from the side
/// table means `Project`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Partition {
    /// The host project's own memory. Default for all legacy call sites.
    Project,
    /// Knowledge harvested from an external repository at a specific commit.
    /// Keyed by (source_repo, commit) for idempotent re-ingestion.
    Foreign { source_repo: String, commit: String },
}

impl Partition {
    pub fn is_project(&self) -> bool {
        matches!(self, Partition::Project)
    }
    pub fn is_foreign(&self) -> bool {
        matches!(self, Partition::Foreign { .. })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CodeLanguage {
    Rust,
    Python,
    JavaScript,
    TypeScript,
    Go,
    Java,
    Cpp,
    Shell,
    Sql,
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ContentType {
    #[default]
    NaturalLanguage,
    Code(CodeLanguage),
    StructuredData,
}

impl ContentType {
    /// Check if this is any code variant regardless of language.
    pub fn is_code(&self) -> bool {
        matches!(self, ContentType::Code(_))
    }

    /// Get the language if this is a code type.
    pub fn language(&self) -> Option<CodeLanguage> {
        match self {
            ContentType::Code(lang) => Some(*lang),
            _ => None,
        }
    }

    /// Parse from a string label (as used in JSON serialization).
    /// Unknown strings default to `NaturalLanguage`.
    pub fn from_label(s: &str) -> Self {
        match s {
            "code" => ContentType::Code(CodeLanguage::Unknown),
            "structured_data" => ContentType::StructuredData,
            _ => ContentType::NaturalLanguage,
        }
    }

    /// Convert to a string label for JSON serialization.
    pub fn as_label(&self) -> &'static str {
        match self {
            ContentType::NaturalLanguage => "natural_language",
            ContentType::Code(_) => "code",
            ContentType::StructuredData => "structured_data",
        }
    }
}

/// Detect content type from text using heuristics.
/// Identifies language for code content via language-specific markers.
pub fn detect_content_type(text: &str) -> ContentType {
    let trimmed = text.trim();

    // Structured data: starts with JSON/YAML/TOML markers
    if trimmed.starts_with('{') || trimmed.starts_with('[') {
        if trimmed.ends_with('}') || trimmed.ends_with(']') {
            return ContentType::StructuredData;
        }
    }
    if trimmed.starts_with("---") {
        return ContentType::StructuredData;
    }
    // TOML/INI-style: majority of lines are key = value or key: value
    let lines: Vec<&str> = trimmed.lines().collect();
    if lines.len() >= 2 {
        let kv_count = lines
            .iter()
            .filter(|l| {
                let l = l.trim();
                if l.is_empty() || l.starts_with('#') || l.starts_with("//") {
                    return false;
                }
                if let Some(pos) = l.find(" = ") {
                    return pos < 30 && !l[..pos].contains(' ');
                }
                if let Some(pos) = l.find(": ") {
                    return pos < 30 && !l[..pos].contains(' ');
                }
                false
            })
            .count();
        if kv_count as f32 / lines.len() as f32 > 0.6 {
            return ContentType::StructuredData;
        }
    }

    // Code detection with language identification
    if let Some(lang) = detect_code_language(text) {
        return ContentType::Code(lang);
    }

    ContentType::NaturalLanguage
}

/// Detect whether text is code and identify the language.
/// Returns None if the text is not code.
fn detect_code_language(text: &str) -> Option<CodeLanguage> {
    // Language-specific strong markers (unique to one language)
    let rust_markers: &[&str] = &[
        "fn ", "pub fn", "impl ", "trait ", "mod ", "#[", "let mut ", "::",
    ];
    let python_markers: &[&str] = &[
        "def ",
        "async def",
        "elif ",
        "self.",
        "import ",
        "__init__",
        "None",
        "True",
        "False",
    ];
    let js_markers: &[&str] = &[
        "function ",
        "const ",
        "let ",
        "var ",
        "=> ",
        "===",
        "!==",
        "require(",
        "console.",
    ];
    let ts_markers: &[&str] = &[
        "interface ",
        ": string",
        ": number",
        ": boolean",
        "readonly ",
        "as const",
    ];
    let go_markers: &[&str] = &["func ", "package ", "go func", ":= ", "fmt."];
    let java_markers: &[&str] = &[
        "public class",
        "private ",
        "protected ",
        "System.out",
        "@Override",
    ];
    let cpp_markers: &[&str] = &["#include", "std::", "cout", "nullptr", "template<"];
    let shell_markers: &[&str] = &["#!/bin", "echo ", "fi\n", "done\n", "esac", "$(", "export "];
    let sql_markers: &[&str] = &[
        "SELECT ",
        "INSERT ",
        "UPDATE ",
        "DELETE ",
        "CREATE TABLE",
        "ALTER TABLE",
        "JOIN ",
    ];

    let count = |markers: &[&str]| markers.iter().filter(|m| text.contains(**m)).count();

    let rust = count(rust_markers);
    let python = count(python_markers);
    let js = count(js_markers);
    let ts = count(ts_markers);
    let go = count(go_markers);
    let java = count(java_markers);
    let cpp = count(cpp_markers);
    let shell = count(shell_markers);
    let sql = count(sql_markers);

    // Find the best match
    let scores = [
        (rust, CodeLanguage::Rust),
        (python, CodeLanguage::Python),
        (js, CodeLanguage::JavaScript),
        (ts, CodeLanguage::TypeScript),
        (go, CodeLanguage::Go),
        (java, CodeLanguage::Java),
        (cpp, CodeLanguage::Cpp),
        (shell, CodeLanguage::Shell),
        (sql, CodeLanguage::Sql),
    ];

    let (best_score, best_lang) = scores
        .iter()
        .max_by_key(|(score, _)| *score)
        .copied()
        .unwrap();

    if best_score >= 2 {
        return Some(best_lang);
    }

    // Fallback: check generic code markers (language-agnostic)
    let has_braces = text.contains('{') && text.contains('}');
    let has_parens_semi = text.contains('(') && text.contains(';');
    let structural = has_braces as usize + has_parens_semi as usize;

    // Generic strong markers
    let generic_strong: &[&str] = &["fn ", "def ", "impl ", "struct ", "enum ", "=>", "->"];
    let generic_weak: &[&str] = &[
        "function ",
        "class ",
        "import ",
        "from ",
        "const ",
        "let ",
        "var ",
        "export ",
        "pub ",
        "use ",
    ];
    let strong = count(generic_strong);
    let weak = count(generic_weak);
    let total = strong + weak;

    let is_code = (strong >= 2)
        || (strong >= 1 && (structural > 0 || weak >= 1))
        || (weak >= 2 && structural > 0)
        || (total >= 3 && structural > 0);

    if is_code {
        // We know it's code but not which language — pick best guess or Unknown
        if best_score >= 1 {
            Some(best_lang)
        } else {
            Some(CodeLanguage::Unknown)
        }
    } else {
        None
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MemKind {
    Identity,
    Fact,
    FactSuperseded,
    /// Derived knowledge citing premise Facts. Automatically invalidated
    /// when any cited premise is superseded (cascade invalidation).
    Conclusion,
    /// A Conclusion whose premises changed — needs recalculation.
    ConclusionInvalidated,
    Skill,
    SkillDeprecated,
    Conversation,
    /// Compacted summary of older conversation turns.
    ConversationSummary,
    Environment,
    /// Model performance profile — tracks observed quality/latency per model.
    Model,
    /// Parsed code symbol — function, struct, class, etc. from tree-sitter AST.
    /// Value is JSON-serialized `CodeSymbolMeta`. Key is `file_path::symbol_name`.
    CodeSymbol,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EdgeKind {
    Supersedes,   // new → old ("I replace you")
    DependsOn,    // dependent → dependency ("I need you")
    RelatedTo,    // semantic similarity (bidirectional by convention)
    Triggers,     // skill → trigger context ("I fire when this matches")
    TemporalNext, // conversation ordering
    Calls,        // CodeSymbol → CodeSymbol ("I call you")
    DefinedIn,    // CodeSymbol → CodeSymbol (method → impl/class)
    Imports,      // CodeSymbol → CodeSymbol (file → dependency)
    CitesPremise, // Conclusion → Fact ("I am derived from this premise")
}

/// Metadata for a CodeSymbol graph node.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CodeSymbolMeta {
    pub name: String,
    pub kind: String, // "Function", "Method", "Struct", "Class", etc.
    pub signature: String,
    pub file_path: String,
    pub start_line: u32,
    pub end_line: u32,
    pub doc_comment: Option<String>,
    pub parent: Option<String>,
}

impl CodeSymbolMeta {
    pub fn from_node(node: &MemNode) -> Option<Self> {
        if node.kind != MemKind::CodeSymbol {
            return None;
        }
        serde_json::from_str(&node.value).ok()
    }
    pub fn encode(&self) -> String {
        serde_json::to_string(self).expect("CodeSymbolMeta serializable")
    }
}

// --- Skill types ---

/// Skill scope — general (applies everywhere) or domain-specific.
/// Follows SkillRL's SkillBank = S_g ∪ ∪S_k structure.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum SkillScope {
    /// Universal strategy applicable across all tasks.
    Global,
    /// Task-specific skill scoped to a domain (persona, task type, etc).
    Domain(String),
}

impl Default for SkillScope {
    fn default() -> Self {
        SkillScope::Global
    }
}

/// Trigger metadata for a stored skill (Parslee-ai/car#181).
///
/// The original shape (`persona`, `url_pattern`, `task_keywords`)
/// stays load-bearing for the web-task automation case — `find_skill`
/// keys on those fields today. The new optional `structured` field
/// opens the trigger to non-web cases (intent signatures, IDE
/// contexts, audio-classifier outputs, anything an agent wants to
/// match a stored skill on without keyword scoring).
///
/// Backward-compatible: existing serialized skills lack `structured`
/// and load with `structured = None`.
///
/// **Matcher dispatch is deferred.** Today `find_skill` keys on the
/// keyword-shaped fields only. A skill whose `structured` is `Some`
/// and whose web-task fields are empty WILL NOT be returned by
/// `find_skill` for a structured query — agents storing such
/// skills today should enumerate via `list_skills` and apply
/// their own matcher until the dispatch lands. Naming this
/// upfront so callers don't read `Some(structured)` as
/// "find_skill will match this."
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default, PartialEq)]
pub struct SkillTrigger {
    #[serde(default)]
    pub persona: String,
    #[serde(default)]
    pub url_pattern: String,
    #[serde(default)]
    pub task_keywords: Vec<String>,
    /// Structured trigger payload for non-web-task skills. The
    /// `kind` identifies the matcher (`"intent_signature"`,
    /// `"ide_context"`, etc.); `signature` is opaque JSON the
    /// matcher knows how to compare. Both fields are optional;
    /// `None` means "use web-task keyword matching."
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub structured: Option<StructuredTrigger>,
}

/// Discriminated structured trigger for matcher plugins.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct StructuredTrigger {
    pub kind: String,
    pub signature: serde_json::Value,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default, PartialEq)]
pub struct SkillStats {
    pub success_count: u64,
    pub fail_count: u64,
    pub degraded: bool,
    pub broken_for_repair: bool,
    /// Continuous success metric for telemetry feeds where success/
    /// failure isn't binary (Parslee-ai/car#181). Range [0.0, 1.0]
    /// or `None` if the consumer hasn't reported one. Distinct from
    /// `success_ratio()` which is a derived aggregate.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub completion_rate: Option<f64>,
    /// User-reported complaint count — the next layer below
    /// `fail_count`. A correct-looking but user-disliked outcome
    /// increments this without flipping `fail_count`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub complaint_count: Option<u64>,
    /// Last time this skill was applied (success or fail). Drives
    /// recency-aware decay in the matcher.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_used_at: Option<DateTime<Utc>>,
}

impl SkillStats {
    pub fn should_degrade(&self, threshold: u64) -> bool {
        self.fail_count > self.success_count + threshold
    }
    pub fn success_ratio(&self) -> f64 {
        let total = self.success_count + self.fail_count;
        if total == 0 {
            return 0.5;
        }
        self.success_count as f64 / total as f64
    }
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum SkillOutcome {
    Success,
    Fail,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct SkillMeta {
    pub name: String,
    pub code: String,     // opaque to Rust — Playwright body, shell script, etc.
    pub platform: String, // "playwright", "node", "shell", "python"
    pub description: String,
    pub trigger: SkillTrigger,
    /// Scope: Global (general strategy) or Domain(name) (task-specific).
    #[serde(default)]
    pub scope: SkillScope,
    /// When this skill should be applied — human-readable condition.
    #[serde(default)]
    pub when_to_apply: String,
    #[serde(default)]
    pub stats: SkillStats,
    #[serde(default = "default_skill_version")]
    pub version: u64,
    /// Multi-tenant isolation tag (Parslee-ai/car#187 phase 3-D).
    /// When set, the skill belongs to a specific tenant and only
    /// surfaces in retrievals scoped to that tenant. `None` means
    /// the skill is unscoped — the legacy / single-tenant default.
    ///
    /// Important: scoped retrievals do NOT see unscoped skills.
    /// Strict isolation by design — see the `ScopedMemgineView` doc
    /// for the rationale and the rebootstrap-per-tenant pattern.
    #[serde(default)]
    pub tenant_id: Option<String>,
}

fn default_skill_version() -> u64 {
    1
}

impl SkillMeta {
    pub fn from_node(node: &MemNode) -> Option<Self> {
        if !matches!(node.kind, MemKind::Skill | MemKind::SkillDeprecated) {
            return None;
        }
        serde_json::from_str(&node.value).ok()
    }
    pub fn encode(&self) -> String {
        serde_json::to_string(self).expect("SkillMeta serializable")
    }
}

/// Provenance record — where a fact came from.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct Provenance {
    pub source: String, // "user", "system", "consolidation", "reflection", "compaction"
    pub reference: String, // file path, URL, conversation turn, etc.
    #[serde(default)]
    pub date: Option<DateTime<Utc>>,
}

/// Structured metadata for knowledge nodes (inspired by metaswarm's JSONL schema).
/// Tracks provenance, confidence, usage signals, and file affinity.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct FactMetadata {
    /// Confidence level: "high", "medium", "low", "derived".
    #[serde(default)]
    pub confidence: String,
    /// Where this fact came from (may have multiple sources).
    #[serde(default)]
    pub provenance: Vec<Provenance>,
    /// File paths this fact is relevant to (glob patterns allowed).
    #[serde(default)]
    pub affected_files: Vec<String>,
    /// Free-form tags for filtering and retrieval.
    #[serde(default)]
    pub tags: Vec<String>,
    /// Fact category: "fact", "gotcha", "anti_pattern", "decision", "pattern".
    #[serde(default)]
    pub category: String,
    /// How many times this fact was included in context.
    #[serde(default)]
    pub usage_count: u64,
    /// How many times usage of this fact correlated with a successful outcome.
    #[serde(default)]
    pub helpful_count: u64,
    /// How many times this fact has been flagged as potentially outdated.
    #[serde(default)]
    pub outdated_reports: u64,
    /// Multi-tenant isolation tag (Parslee-ai/car#187 phase 3-D).
    /// When set, the fact belongs to a specific tenant and only
    /// surfaces in retrievals scoped to that tenant. `None` means
    /// the fact is unscoped — the legacy / single-tenant default.
    ///
    /// Important: scoped retrievals do NOT see unscoped facts.
    /// Strict isolation by design — operators bootstrap each tenant's
    /// memgine with whatever seeded facts they want; there's no
    /// "global facts visible to everyone" fallthrough because that
    /// would let any tenant pollute the shared namespace.
    #[serde(default)]
    pub tenant_id: Option<String>,
}

impl FactMetadata {
    pub fn is_empty(&self) -> bool {
        self.confidence.is_empty()
            && self.provenance.is_empty()
            && self.affected_files.is_empty()
            && self.tags.is_empty()
            && self.category.is_empty()
    }

    /// Staleness signal: facts with high outdated_reports relative to usage are suspect.
    pub fn staleness_ratio(&self) -> f64 {
        if self.usage_count == 0 {
            return 0.0;
        }
        self.outdated_reports as f64 / self.usage_count as f64
    }

    /// Helpfulness signal: facts that correlate with success.
    pub fn helpfulness_ratio(&self) -> f64 {
        if self.usage_count == 0 {
            return 0.5;
        } // neutral prior
        self.helpful_count as f64 / self.usage_count as f64
    }
}

#[derive(Debug, Clone)]
pub struct MemNode {
    pub kind: MemKind,
    pub layer: Layer,
    pub key: String,
    pub value: String,
    pub fact_id: Option<String>,
    pub scope: String,
    pub authority: String,
    pub is_constraint: bool,
    pub created_at: DateTime<Utc>,
    pub expires_at: Option<DateTime<Utc>>,
    pub content_type: ContentType,
    /// Structured metadata for knowledge quality signals (optional, defaults empty).
    pub metadata: FactMetadata,
}

impl MemNode {
    pub fn token_estimate(&self) -> usize {
        std::cmp::max(1, self.value.len() / 4)
    }

    pub fn is_valid(&self) -> bool {
        !matches!(
            self.kind,
            MemKind::FactSuperseded | MemKind::SkillDeprecated | MemKind::ConclusionInvalidated
        )
    }
}

#[derive(Debug, Clone)]
pub struct MemEdge {
    pub kind: EdgeKind,
    pub weight: f32,
    pub created_at: DateTime<Utc>,
}

#[derive(Debug, Clone)]
pub struct RetrievalHit {
    pub node_ix: NodeIndex,
    pub activation: f32,
    pub hops: usize,
}

/// The memory graph. Nodes are entries, edges are relationships.
pub struct MemoryGraph {
    pub inner: StableGraph<MemNode, MemEdge>,
    by_fact_id: HashMap<String, NodeIndex>,
    by_key: HashMap<String, Vec<NodeIndex>>,
    by_layer: [Vec<NodeIndex>; 4],
    last_conversation: Option<NodeIndex>,
    /// Partition assignments. Only non-`Project` entries are stored —
    /// absence means `Project`.
    partitions: HashMap<NodeIndex, Partition>,
    /// Idempotency index for foreign nodes, keyed by `fact_id`.
    /// `fact_id` is already globally unique (Indexer encodes
    /// `foreign::{repo}::{commit}::{path}::{symbol}` — distinguishes
    /// overloads and impl blocks that share a short `key`). A node without
    /// a `fact_id` is NOT idempotency-tracked and will duplicate on
    /// re-insert — callers that want dedupe must populate `fact_id`.
    foreign_by_fact_id: HashMap<String, NodeIndex>,
}

impl MemoryGraph {
    pub fn new() -> Self {
        Self {
            inner: StableGraph::new(),
            by_fact_id: HashMap::new(),
            by_key: HashMap::new(),
            by_layer: [Vec::new(), Vec::new(), Vec::new(), Vec::new()],
            last_conversation: None,
            partitions: HashMap::new(),
            foreign_by_fact_id: HashMap::new(),
        }
    }

    /// Partition a node belongs to. Returns `Partition::Project` for any node
    /// not explicitly assigned (the legacy default).
    pub fn partition_of(&self, nix: NodeIndex) -> Partition {
        self.partitions
            .get(&nix)
            .cloned()
            .unwrap_or(Partition::Project)
    }

    /// Insert a node into the given `Partition`. For `Project`, equivalent
    /// to `insert`. For `Foreign`, enforces idempotency by `(repo, commit, key)`
    /// — re-inserting the same triple returns the existing `NodeIndex`
    /// rather than creating a duplicate.
    pub fn insert_partitioned(&mut self, node: MemNode, partition: Partition) -> NodeIndex {
        if let Partition::Foreign { .. } = &partition {
            if let Some(fid) = node.fact_id.as_deref() {
                if let Some(&existing) = self.foreign_by_fact_id.get(fid) {
                    return existing;
                }
            }
            let fid = node.fact_id.clone();
            let nix = self.insert(node);
            if let Some(fid) = fid {
                self.foreign_by_fact_id.insert(fid, nix);
            }
            self.partitions.insert(nix, partition);
            return nix;
        }
        self.insert(node)
    }

    /// Convenience: insert a node under `Partition::Foreign { source_repo, commit }`.
    pub fn insert_foreign(
        &mut self,
        source_repo: impl Into<String>,
        commit: impl Into<String>,
        node: MemNode,
    ) -> NodeIndex {
        self.insert_partitioned(
            node,
            Partition::Foreign {
                source_repo: source_repo.into(),
                commit: commit.into(),
            },
        )
    }

    /// Insert a node and index it.
    pub fn insert(&mut self, node: MemNode) -> NodeIndex {
        let layer_idx = (node.layer as usize).saturating_sub(1).min(3);
        let fact_id = node.fact_id.clone();
        let key = node.key.clone();
        let is_conv = matches!(
            node.kind,
            MemKind::Conversation | MemKind::ConversationSummary
        );

        let nix = self.inner.add_node(node);

        if let Some(fid) = fact_id {
            self.by_fact_id.insert(fid, nix);
        }
        self.by_key.entry(key).or_default().push(nix);
        self.by_layer[layer_idx].push(nix);

        // Auto-link conversation nodes temporally
        if is_conv {
            if let Some(prev) = self.last_conversation {
                self.inner.add_edge(
                    prev,
                    nix,
                    MemEdge {
                        kind: EdgeKind::TemporalNext,
                        weight: 1.0,
                        created_at: Utc::now(),
                    },
                );
            }
            self.last_conversation = Some(nix);
        }

        nix
    }

    /// Add a typed edge between two nodes.
    pub fn link(&mut self, from: NodeIndex, to: NodeIndex, kind: EdgeKind, weight: f32) {
        self.inner.add_edge(
            from,
            to,
            MemEdge {
                kind,
                weight,
                created_at: Utc::now(),
            },
        );
    }

    /// Supersede: mark old as FactSuperseded, add Supersedes edge,
    /// and cascade invalidation to any Conclusions citing this premise.
    pub fn supersede(&mut self, new_nix: NodeIndex, old_fact_id: &str) -> HashSet<NodeIndex> {
        let mut invalidated = HashSet::new();

        if let Some(&old_nix) = self.by_fact_id.get(old_fact_id) {
            // Mark old as superseded
            if let Some(old_node) = self.inner.node_weight_mut(old_nix) {
                old_node.kind = MemKind::FactSuperseded;
            }
            // Add supersedes edge
            self.link(new_nix, old_nix, EdgeKind::Supersedes, 1.0);
            invalidated.insert(old_nix);

            // Find dependents (nodes with DependsOn edges pointing to old_nix)
            let dependents: Vec<NodeIndex> = self
                .inner
                .neighbors_directed(old_nix, Direction::Incoming)
                .filter(|&n| {
                    self.inner
                        .edges_connecting(n, old_nix)
                        .any(|e| e.weight().kind == EdgeKind::DependsOn)
                })
                .collect();

            for dep in dependents {
                invalidated.insert(dep);
            }

            // CASCADE: invalidate Conclusions that cite this premise
            let citing_conclusions: Vec<NodeIndex> = self
                .inner
                .neighbors_directed(old_nix, Direction::Incoming)
                .filter(|&n| {
                    self.inner
                        .edges_connecting(n, old_nix)
                        .any(|e| e.weight().kind == EdgeKind::CitesPremise)
                })
                .collect();

            for conc_nix in citing_conclusions {
                if let Some(conc_node) = self.inner.node_weight_mut(conc_nix) {
                    if conc_node.kind == MemKind::Conclusion {
                        conc_node.kind = MemKind::ConclusionInvalidated;
                        invalidated.insert(conc_nix);
                    }
                }
            }
        }

        invalidated
    }

    /// Lookup by fact_id.
    pub fn get_by_fact_id(&self, fact_id: &str) -> Option<(NodeIndex, &MemNode)> {
        self.by_fact_id
            .get(fact_id)
            .and_then(|&nix| self.inner.node_weight(nix).map(|n| (nix, n)))
    }

    /// Get all valid fact nodes.
    pub fn valid_facts(&self) -> Vec<(NodeIndex, &MemNode)> {
        self.by_layer[1]
            .iter() // layer 2 = index 1
            .filter_map(|&nix| {
                self.inner
                    .node_weight(nix)
                    .filter(|n| n.kind == MemKind::Fact)
                    .map(|n| (nix, n))
            })
            .collect()
    }

    /// Get all constraint fact nodes.
    pub fn constraints(&self) -> Vec<(NodeIndex, &MemNode)> {
        self.valid_facts()
            .into_iter()
            .filter(|(_, n)| n.is_constraint)
            .collect()
    }

    /// Get nodes by layer.
    pub fn nodes_by_layer(&self, layer: Layer) -> Vec<(NodeIndex, &MemNode)> {
        let idx = (layer as usize).saturating_sub(1).min(3);
        self.by_layer[idx]
            .iter()
            .filter_map(|&nix| self.inner.node_weight(nix).map(|n| (nix, n)))
            .collect()
    }

    /// Personalized PageRank retrieval from seed nodes (HippoRAG-inspired).
    ///
    /// PPR propagates relevance from seed nodes through the graph with a damping
    /// factor that controls the balance between following edges and teleporting
    /// back to seeds. Converges to a stationary distribution in ~10-20 iterations.
    ///
    /// Results are filtered to seeds' partitions by default. Use
    /// `retrieve_ppr_cross_partition` to traverse across the Project/Foreign
    /// boundary.
    pub fn retrieve_ppr(
        &self,
        seeds: &[NodeIndex],
        seed_weights: Option<&[f32]>,
        damping: f32,
        max_results: usize,
    ) -> Vec<RetrievalHit> {
        self.retrieve_ppr_inner(seeds, seed_weights, damping, max_results, true)
    }

    /// Like `retrieve_ppr`, but does NOT filter by partition — foreign nodes
    /// can be reached from project seeds and vice versa. Caller opts in.
    pub fn retrieve_ppr_cross_partition(
        &self,
        seeds: &[NodeIndex],
        seed_weights: Option<&[f32]>,
        damping: f32,
        max_results: usize,
    ) -> Vec<RetrievalHit> {
        self.retrieve_ppr_inner(seeds, seed_weights, damping, max_results, false)
    }

    fn retrieve_ppr_inner(
        &self,
        seeds: &[NodeIndex],
        seed_weights: Option<&[f32]>,
        damping: f32,
        max_results: usize,
        apply_partition_filter: bool,
    ) -> Vec<RetrievalHit> {
        if seeds.is_empty() {
            return Vec::new();
        }

        let all_nodes: Vec<NodeIndex> = self.inner.node_indices().collect();
        let n = all_nodes.len();
        if n == 0 {
            return Vec::new();
        }

        // Build node index → position map for O(1) lookup
        let pos: HashMap<NodeIndex, usize> = all_nodes
            .iter()
            .enumerate()
            .map(|(i, &nix)| (nix, i))
            .collect();

        // Build reset vector (teleportation distribution)
        let mut reset = vec![0.0f32; n];
        let mut total_weight = 0.0f32;
        for (i, &seed) in seeds.iter().enumerate() {
            if let Some(&p) = pos.get(&seed) {
                let w = seed_weights
                    .and_then(|sw| sw.get(i).copied())
                    .unwrap_or(1.0);
                reset[p] = w;
                total_weight += w;
            }
        }
        if total_weight > 0.0 {
            for v in &mut reset {
                *v /= total_weight;
            }
        }

        // Cap any single entry to 0.4 and re-normalize
        let cap = 0.4f32;
        let mut needs_renorm = false;
        for v in &mut reset {
            if *v > cap {
                *v = cap;
                needs_renorm = true;
            }
        }
        if needs_renorm {
            let sum: f32 = reset.iter().sum();
            if sum > 0.0 {
                for v in &mut reset {
                    *v /= sum;
                }
            }
        }

        // Initialize scores to reset vector
        let mut scores = reset.clone();

        // Edge weight multipliers by kind
        let edge_mult = |kind: EdgeKind| -> f32 {
            match kind {
                EdgeKind::Supersedes => 0.3,
                EdgeKind::DependsOn => 0.8,
                EdgeKind::RelatedTo => 0.6,
                EdgeKind::Triggers => 0.9,
                EdgeKind::TemporalNext => 0.5,
                EdgeKind::Calls => 0.85,
                EdgeKind::DefinedIn => 0.9,
                EdgeKind::Imports => 0.4,
                EdgeKind::CitesPremise => 0.85,
            }
        };

        // Precompute outgoing edge weights per node for normalization
        let mut out_weights: Vec<f32> = vec![0.0; n];
        for (i, &nix) in all_nodes.iter().enumerate() {
            let mut total = 0.0f32;
            for e in self.inner.edges(nix) {
                total += e.weight().weight * edge_mult(e.weight().kind);
            }
            // Also count incoming edges we traverse (RelatedTo, DependsOn, Triggers)
            for e in self.inner.edges_directed(nix, Direction::Incoming) {
                let k = e.weight().kind;
                if matches!(
                    k,
                    EdgeKind::RelatedTo
                        | EdgeKind::DependsOn
                        | EdgeKind::Triggers
                        | EdgeKind::Calls
                        | EdgeKind::DefinedIn
                        | EdgeKind::Imports
                ) {
                    total += e.weight().weight * edge_mult(k);
                }
            }
            out_weights[i] = total;
        }

        // PPR iteration
        let max_iters = 30;
        let epsilon = 1e-6;

        let mut new_scores = vec![0.0f32; n];

        for _iter in 0..max_iters {
            new_scores.fill(0.0);

            // Teleportation component
            for i in 0..n {
                new_scores[i] += (1.0 - damping) * reset[i];
            }

            // Propagation component: each node distributes its score to neighbors
            for (i, &nix) in all_nodes.iter().enumerate() {
                if scores[i] < epsilon {
                    continue;
                }
                let out_w = out_weights[i];
                if out_w == 0.0 {
                    continue;
                }

                // Outgoing edges
                for e in self.inner.edges(nix) {
                    if let Some(&j) = pos.get(&e.target()) {
                        let w = e.weight().weight * edge_mult(e.weight().kind);
                        new_scores[j] += damping * scores[i] * w / out_w;
                    }
                }
                // Incoming edges (bidirectional traversal)
                for e in self.inner.edges_directed(nix, Direction::Incoming) {
                    let k = e.weight().kind;
                    if !matches!(
                        k,
                        EdgeKind::RelatedTo
                            | EdgeKind::DependsOn
                            | EdgeKind::Triggers
                            | EdgeKind::Calls
                            | EdgeKind::DefinedIn
                            | EdgeKind::Imports
                    ) {
                        continue;
                    }
                    if let Some(&j) = pos.get(&e.source()) {
                        let w = e.weight().weight * edge_mult(k);
                        new_scores[j] += damping * scores[i] * w / out_w;
                    }
                }
            }

            // Check convergence
            let delta: f32 = scores
                .iter()
                .zip(new_scores.iter())
                .map(|(a, b)| (a - b).abs())
                .sum();
            std::mem::swap(&mut scores, &mut new_scores);
            if delta < epsilon {
                break;
            }
        }

        // Collect results
        let mut hits: Vec<RetrievalHit> = all_nodes
            .iter()
            .enumerate()
            .filter_map(|(i, &nix)| {
                let node = self.inner.node_weight(nix)?;
                if !node.is_valid() {
                    return None;
                }
                if scores[i] < epsilon {
                    return None;
                }
                // Compute approximate hops from nearest seed
                // PPR doesn't have a clean concept of "hops" — set to 0
                let hops = 0;
                Some(RetrievalHit {
                    node_ix: nix,
                    activation: scores[i],
                    hops,
                })
            })
            .collect();

        hits.sort_by(|a, b| {
            b.activation
                .partial_cmp(&a.activation)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        if apply_partition_filter {
            self.filter_same_partition(&mut hits, seeds);
        }
        hits.truncate(max_results);
        hits
    }

    /// Drop hits whose partition doesn't match any seed's partition.
    /// Private helper; `*_cross_partition` variants skip this step.
    fn filter_same_partition(&self, hits: &mut Vec<RetrievalHit>, seeds: &[NodeIndex]) {
        if seeds.is_empty() {
            return;
        }
        // If all seeds are Project and no Foreign partitions exist, fast-path.
        if self.partitions.is_empty() {
            return;
        }
        let seed_parts: HashSet<Partition> = seeds.iter().map(|&n| self.partition_of(n)).collect();
        hits.retain(|h| seed_parts.contains(&self.partition_of(h.node_ix)));
    }

    /// Legacy spreading activation retrieval from seed nodes.
    ///
    /// Results are filtered to seeds' partitions by default. Use
    /// `retrieve_cross_partition` for opt-in cross-partition traversal.
    pub fn retrieve(
        &self,
        seeds: &[NodeIndex],
        max_hops: usize,
        max_results: usize,
        decay: f32,
        min_activation: f32,
    ) -> Vec<RetrievalHit> {
        self.retrieve_inner(seeds, max_hops, max_results, decay, min_activation, true)
    }

    /// Cross-partition variant of `retrieve`.
    pub fn retrieve_cross_partition(
        &self,
        seeds: &[NodeIndex],
        max_hops: usize,
        max_results: usize,
        decay: f32,
        min_activation: f32,
    ) -> Vec<RetrievalHit> {
        self.retrieve_inner(seeds, max_hops, max_results, decay, min_activation, false)
    }

    fn retrieve_inner(
        &self,
        seeds: &[NodeIndex],
        max_hops: usize,
        max_results: usize,
        decay: f32,
        min_activation: f32,
        apply_partition_filter: bool,
    ) -> Vec<RetrievalHit> {
        let mut activations: HashMap<NodeIndex, f32> = HashMap::new();
        let mut hops_map: HashMap<NodeIndex, usize> = HashMap::new();
        let mut queue: VecDeque<(NodeIndex, f32, usize)> = VecDeque::new();

        for &seed in seeds {
            activations.insert(seed, 1.0);
            hops_map.insert(seed, 0);
            queue.push_back((seed, 1.0, 0));
        }

        while let Some((node, activation, hops)) = queue.pop_front() {
            if hops >= max_hops {
                continue;
            }

            // Traverse outgoing edges
            for edge_ref in self.inner.edges(node) {
                let neighbor = edge_ref.target();
                let edge = edge_ref.weight();

                let edge_mult = match edge.kind {
                    EdgeKind::Supersedes => 0.3,
                    EdgeKind::DependsOn => 0.8,
                    EdgeKind::RelatedTo => 0.6,
                    EdgeKind::Triggers => 0.9,
                    EdgeKind::TemporalNext => 0.5,
                    EdgeKind::Calls => 0.85,
                    EdgeKind::DefinedIn => 0.9,
                    EdgeKind::Imports => 0.4,
                    EdgeKind::CitesPremise => 0.85,
                };

                let new_activation = activation * decay * edge.weight * edge_mult;
                if new_activation < min_activation {
                    continue;
                }

                let existing = activations.get(&neighbor).copied().unwrap_or(0.0);
                if new_activation > existing {
                    activations.insert(neighbor, new_activation);
                    hops_map.insert(neighbor, hops + 1);
                    queue.push_back((neighbor, new_activation, hops + 1));
                }
            }

            // Also traverse incoming edges (for bidirectional traversal)
            for edge_ref in self.inner.edges_directed(node, Direction::Incoming) {
                let neighbor = edge_ref.source();
                let edge = edge_ref.weight();

                // Follow RelatedTo, DependsOn, and Triggers backwards
                let edge_mult = match edge.kind {
                    EdgeKind::RelatedTo => 0.6,
                    EdgeKind::DependsOn => 0.5,
                    EdgeKind::Triggers => 0.85, // "what skill fires for this trigger?"
                    EdgeKind::Calls => 0.7,     // "what calls me?"
                    EdgeKind::DefinedIn => 0.8, // "what methods does this type have?"
                    EdgeKind::Imports => 0.3,
                    _ => continue,
                };

                let new_activation = activation * decay * edge.weight * edge_mult;
                if new_activation < min_activation {
                    continue;
                }

                let existing = activations.get(&neighbor).copied().unwrap_or(0.0);
                if new_activation > existing {
                    activations.insert(neighbor, new_activation);
                    hops_map.insert(neighbor, hops + 1);
                    queue.push_back((neighbor, new_activation, hops + 1));
                }
            }
        }

        let mut hits: Vec<RetrievalHit> = activations
            .into_iter()
            .filter_map(|(nix, act)| {
                let node = self.inner.node_weight(nix)?;
                if !node.is_valid() {
                    return None;
                } // skip superseded
                Some(RetrievalHit {
                    node_ix: nix,
                    activation: act,
                    hops: *hops_map.get(&nix).unwrap_or(&0),
                })
            })
            .collect();

        hits.sort_by(|a, b| {
            b.activation
                .partial_cmp(&a.activation)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        if apply_partition_filter {
            self.filter_same_partition(&mut hits, seeds);
        }
        hits.truncate(max_results);
        hits
    }

    /// Find seed nodes by keyword matching with IDF-weighted scores.
    ///
    /// Returns (seeds, weights) where weights incorporate corpus IDF (term rarity),
    /// stemming, synonym expansion, and content-type-aware tokenization.
    /// Use the weights as `seed_weights` in `retrieve_ppr()`.
    pub fn find_seeds_weighted(&self, query: &str, max_seeds: usize) -> (Vec<NodeIndex>, Vec<f32>) {
        let q_tokens = tokenize(query);
        if q_tokens.is_empty() {
            return (Vec::new(), Vec::new());
        }

        // Compute corpus IDF: how rare each term is across all nodes
        let corpus_idf = self.compute_corpus_idf();

        let mut scored: Vec<(f32, NodeIndex)> = Vec::new();

        for nix in self.inner.node_indices() {
            let node = match self.inner.node_weight(nix) {
                Some(n) => n,
                None => continue,
            };
            if !node.is_valid() {
                continue;
            }

            let text = format!("{} {}", node.key, node.value).to_lowercase();
            let n_tokens = tokenize_for_content(&text, node.content_type);
            let n_stems: HashSet<String> = n_tokens.iter().map(|t| stem(t)).collect();

            // Weighted scoring using corpus IDF
            let mut weighted_hits = 0.0f32;
            let mut weighted_total = 0.0f32;

            for qt in &q_tokens {
                let term_idf = corpus_idf.get(qt).copied().unwrap_or(1.0);
                weighted_total += term_idf * 3.0; // max score per term = 3 * idf

                // Exact token match (3x weight)
                if n_tokens.contains(qt) {
                    weighted_hits += term_idf * 3.0;
                    continue;
                }
                // Stem match (2x weight)
                let qt_stem = stem(qt);
                if n_stems.contains(&qt_stem) {
                    weighted_hits += term_idf * 2.0;
                    continue;
                }
                // Synonym match (1.5x weight)
                let qt_syns = synonym_expand(qt);
                if !qt_syns.is_empty()
                    && qt_syns
                        .iter()
                        .any(|s| n_tokens.contains(s) || n_stems.contains(&stem(s)))
                {
                    weighted_hits += term_idf * 1.5;
                    continue;
                }
                // Substring match (1x weight)
                if text.contains(qt.as_str()) {
                    weighted_hits += term_idf;
                }
            }

            if weighted_total > 0.0 {
                let match_score = weighted_hits / weighted_total;
                if match_score > 0.0 {
                    scored.push((match_score, nix));
                }
            }
        }

        scored.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));
        scored.truncate(max_seeds);
        let seeds: Vec<NodeIndex> = scored.iter().map(|(_, nix)| *nix).collect();
        let weights: Vec<f32> = scored.iter().map(|(w, _)| *w).collect();
        (seeds, weights)
    }

    /// Compute corpus IDF: ln(N / (1 + df)) for each term across all valid nodes.
    /// Called once per find_seeds_weighted invocation. O(N × tokens_per_node),
    /// sub-millisecond for expected graph sizes (<10K nodes).
    fn compute_corpus_idf(&self) -> HashMap<String, f32> {
        let total = self.inner.node_count().max(1) as f32;
        let mut doc_freq: HashMap<String, usize> = HashMap::new();

        for nix in self.inner.node_indices() {
            let node = match self.inner.node_weight(nix) {
                Some(n) if n.is_valid() => n,
                _ => continue,
            };
            let text = format!("{} {}", node.key, node.value).to_lowercase();
            let tokens = tokenize_for_content(&text, node.content_type);
            let mut seen = HashSet::new();
            for token in tokens {
                if seen.insert(token.clone()) {
                    *doc_freq.entry(token).or_insert(0) += 1;
                }
            }
        }

        doc_freq
            .into_iter()
            .map(|(term, df)| (term, (total / (1.0 + df as f32)).ln().max(0.1)))
            .collect()
    }

    /// Find seed nodes by keyword matching (unweighted, backward compat).
    pub fn find_seeds(&self, query: &str, max_seeds: usize) -> Vec<NodeIndex> {
        self.find_seeds_weighted(query, max_seeds).0
    }

    /// Total token count across all valid nodes.
    pub fn total_tokens(&self) -> usize {
        self.inner
            .node_indices()
            .filter_map(|nix| self.inner.node_weight(nix))
            .filter(|n| n.is_valid())
            .map(|n| n.token_estimate())
            .sum()
    }

    /// Count of valid fact nodes.
    pub fn valid_fact_count(&self) -> usize {
        self.valid_facts().len()
    }

    pub fn node_count(&self) -> usize {
        self.inner.node_count()
    }

    pub fn edge_count(&self) -> usize {
        self.inner.edge_count()
    }

    /// Garbage-collect superseded/deprecated nodes beyond a retention depth.
    ///
    /// Walks Supersedes chains from each FactSuperseded/SkillDeprecated node.
    /// If the node is more than `max_depth` hops from any active (non-superseded)
    /// ancestor, it is removed. Returns the number of nodes removed.
    pub fn gc_superseded(&mut self, max_depth: usize) -> usize {
        // Find all superseded/deprecated nodes
        let stale: Vec<NodeIndex> = self
            .inner
            .node_indices()
            .filter(|&nix| {
                self.inner
                    .node_weight(nix)
                    .map(|n| {
                        matches!(
                            n.kind,
                            MemKind::FactSuperseded
                                | MemKind::SkillDeprecated
                                | MemKind::ConclusionInvalidated
                        )
                    })
                    .unwrap_or(false)
            })
            .collect();

        // For each stale node, walk Supersedes edges backward (incoming) to find
        // distance to nearest active node. If no active ancestor within max_depth, remove.
        let mut to_remove = Vec::new();
        for nix in &stale {
            let mut depth = 0;
            let mut current = *nix;
            let mut reachable = false;

            // Walk backward: who supersedes me? (incoming Supersedes edges)
            loop {
                let parent: Option<NodeIndex> = self
                    .inner
                    .neighbors_directed(current, Direction::Incoming)
                    .find(|&neighbor| {
                        self.inner
                            .edges_connecting(neighbor, current)
                            .any(|e| e.weight().kind == EdgeKind::Supersedes)
                    });

                match parent {
                    Some(p) => {
                        depth += 1;
                        if let Some(node) = self.inner.node_weight(p) {
                            if node.is_valid() {
                                reachable = true;
                                break;
                            }
                        }
                        if depth > max_depth {
                            break;
                        }
                        current = p;
                    }
                    None => break, // orphaned superseded node
                }
            }

            // Remove if too deep or orphaned (no active ancestor at all)
            if !reachable || depth > max_depth {
                to_remove.push(*nix);
            }
        }

        let count = to_remove.len();
        for nix in to_remove {
            // Capture fields before removal for index cleanup
            let (key, layer, fact_id) = match self.inner.node_weight(nix) {
                Some(node) => (node.key.clone(), node.layer, node.fact_id.clone()),
                None => continue,
            };
            self.remove_from_indexes(nix, &key, layer, fact_id.as_deref());
            self.inner.remove_node(nix);
        }
        count
    }

    /// Remove expired environment nodes.
    pub fn prune_expired(&mut self, now: DateTime<Utc>) {
        let expired: Vec<(NodeIndex, String, Layer, Option<String>)> = self
            .inner
            .node_indices()
            .filter_map(|nix| {
                let node = self.inner.node_weight(nix)?;
                if node.expires_at.map(|e| e <= now).unwrap_or(false) {
                    Some((nix, node.key.clone(), node.layer, node.fact_id.clone()))
                } else {
                    None
                }
            })
            .collect();
        for (nix, key, layer, fact_id) in expired {
            self.remove_from_indexes(nix, &key, layer, fact_id.as_deref());
            self.inner.remove_node(nix);
        }
    }

    /// Remove a set of conversation/summary nodes and repair the temporal chain.
    /// After removal, re-links surviving conversation+summary nodes with TemporalNext edges.
    pub fn remove_conversation_nodes(&mut self, to_remove: &[NodeIndex]) {
        // Clean indexes and remove the nodes
        for &nix in to_remove {
            let (key, layer, fact_id) = match self.inner.node_weight(nix) {
                Some(node) => (node.key.clone(), node.layer, node.fact_id.clone()),
                None => continue,
            };
            self.remove_from_indexes(nix, &key, layer, fact_id.as_deref());
            self.inner.remove_node(nix);
        }

        // Collect surviving conversation/summary nodes, sorted by created_at
        let mut survivors: Vec<(NodeIndex, chrono::DateTime<Utc>)> = self
            .inner
            .node_indices()
            .filter_map(|nix| {
                let n = self.inner.node_weight(nix)?;
                if matches!(n.kind, MemKind::Conversation | MemKind::ConversationSummary) {
                    Some((nix, n.created_at))
                } else {
                    None
                }
            })
            .collect();
        survivors.sort_by_key(|&(_, ts)| ts);

        // Remove all existing TemporalNext edges between conversation nodes
        let temporal_edges: Vec<petgraph::graph::EdgeIndex> = self
            .inner
            .edge_indices()
            .filter(|&eix| {
                self.inner
                    .edge_weight(eix)
                    .map(|e| e.kind == EdgeKind::TemporalNext)
                    .unwrap_or(false)
            })
            .collect();
        for eix in temporal_edges {
            self.inner.remove_edge(eix);
        }

        // Re-link surviving nodes in chronological order
        for window in survivors.windows(2) {
            let (from_nix, _) = window[0];
            let (to_nix, _) = window[1];
            self.inner.add_edge(
                from_nix,
                to_nix,
                MemEdge {
                    kind: EdgeKind::TemporalNext,
                    weight: 1.0,
                    created_at: Utc::now(),
                },
            );
        }

        // Update last_conversation
        self.last_conversation = survivors.last().map(|&(nix, _)| nix);
    }

    /// Remove a node's entries from all secondary indexes.
    fn remove_from_indexes(
        &mut self,
        nix: NodeIndex,
        key: &str,
        layer: Layer,
        fact_id: Option<&str>,
    ) {
        if let Some(fid) = fact_id {
            self.by_fact_id.remove(fid);
        }
        if let Some(entries) = self.by_key.get_mut(key) {
            entries.retain(|&n| n != nix);
            if entries.is_empty() {
                self.by_key.remove(key);
            }
        }
        let layer_idx = (layer as usize).saturating_sub(1).min(3);
        self.by_layer[layer_idx].retain(|&n| n != nix);
    }

    pub fn clear(&mut self) {
        self.inner.clear();
        self.by_fact_id.clear();
        self.by_key.clear();
        for v in &mut self.by_layer {
            v.clear();
        }
        self.last_conversation = None;
    }
}

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

/// Tokenize text on common delimiters for seed matching.
fn tokenize(text: &str) -> HashSet<String> {
    text.split(|c: char| {
        c.is_whitespace() || c == '/' || c == ':' || c == '-' || c == '_' || c == '.' || c == ','
    })
    .map(|s| s.trim().to_lowercase())
    .filter(|s| !s.is_empty())
    .collect()
}

/// Syntax noise tokens common across all languages.
const CODE_NOISE_COMMON: &[&str] = &[
    "return", "if", "else", "for", "while", "true", "false", "new", "this", "static", "void",
    "null", "break", "continue", "try", "catch", "throw",
];

/// Per-language noise tokens — keywords that carry no semantic meaning for retrieval.
fn noise_for_language(lang: CodeLanguage) -> &'static [&'static str] {
    match lang {
        CodeLanguage::Rust => &[
            "fn", "let", "const", "pub", "mut", "use", "mod", "crate", "super", "struct", "enum",
            "impl", "trait", "async", "await", "where", "type", "match", "ref", "move", "dyn",
            "unsafe",
        ],
        CodeLanguage::Python => &[
            "def", "class", "import", "from", "pass", "none", "self", "elif", "except", "finally",
            "yield", "lambda", "nonlocal", "global", "assert", "with", "as", "in", "is", "not",
            "and", "or",
        ],
        CodeLanguage::JavaScript | CodeLanguage::TypeScript => &[
            "function",
            "const",
            "let",
            "var",
            "export",
            "default",
            "async",
            "await",
            "typeof",
            "instanceof",
            "undefined",
            "require",
        ],
        CodeLanguage::Go => &[
            "func",
            "package",
            "import",
            "defer",
            "go",
            "chan",
            "select",
            "range",
            "switch",
            "case",
            "fallthrough",
        ],
        CodeLanguage::Java => &[
            "public",
            "private",
            "protected",
            "class",
            "interface",
            "extends",
            "implements",
            "final",
            "abstract",
            "synchronized",
            "volatile",
            "import",
            "package",
            "throws",
        ],
        CodeLanguage::Cpp => &[
            "include",
            "namespace",
            "using",
            "template",
            "typename",
            "virtual",
            "override",
            "const",
            "auto",
            "inline",
            "extern",
            "typedef",
            "class",
            "struct",
            "enum",
        ],
        CodeLanguage::Shell => &[
            "echo", "fi", "do", "done", "then", "esac", "elif", "local", "export", "readonly",
            "set",
        ],
        CodeLanguage::Sql => &[
            "select", "from", "where", "and", "or", "not", "insert", "into", "update", "set",
            "delete", "create", "alter", "drop", "table", "join", "on", "as", "order", "by",
            "group", "having", "limit",
        ],
        CodeLanguage::Unknown => &[
            "fn", "let", "const", "var", "pub", "mut", "return", "def", "class", "import", "from",
            "function", "export", "default",
        ],
    }
}

/// Split camelCase and PascalCase identifiers into parts.
/// e.g. "verifyJwtToken" → ["verify", "jwt", "token"]
///      "HTMLParser" → ["html", "parser"]
fn split_camel_case(s: &str) -> Vec<String> {
    let mut parts = Vec::new();
    let mut current = String::new();
    let chars: Vec<char> = s.chars().collect();

    for i in 0..chars.len() {
        let c = chars[i];
        if !c.is_alphanumeric() {
            if !current.is_empty() {
                parts.push(std::mem::take(&mut current).to_lowercase());
            }
            continue;
        }
        if c.is_uppercase() {
            // Check if this starts a new word:
            // 1. Previous was lowercase (camelCase boundary)
            // 2. Previous was uppercase but next is lowercase (acronym end: HTMLParser → HTML|Parser)
            let prev_lower = i > 0 && chars[i - 1].is_lowercase();
            let acronym_end = i > 0
                && chars[i - 1].is_uppercase()
                && i + 1 < chars.len()
                && chars[i + 1].is_lowercase();
            if prev_lower || acronym_end {
                if !current.is_empty() {
                    parts.push(std::mem::take(&mut current).to_lowercase());
                }
            }
        }
        current.push(c);
    }
    if !current.is_empty() {
        parts.push(current.to_lowercase());
    }
    parts
}

/// Code-aware tokenizer: splits camelCase/PascalCase, filters language-specific noise.
fn tokenize_code(text: &str, lang: CodeLanguage) -> HashSet<String> {
    let mut noise: HashSet<&str> = CODE_NOISE_COMMON.iter().copied().collect();
    noise.extend(noise_for_language(lang));

    let raw_tokens: Vec<String> = text
        .split(|c: char| {
            c.is_whitespace()
                || c == '/'
                || c == ':'
                || c == '-'
                || c == '_'
                || c == '.'
                || c == ','
                || c == '('
                || c == ')'
                || c == '{'
                || c == '}'
                || c == '['
                || c == ']'
                || c == ';'
                || c == '"'
                || c == '\''
                || c == '<'
                || c == '>'
                || c == '='
                || c == '&'
                || c == '|'
                || c == '!'
                || c == '#'
                || c == '*'
                || c == '+'
        })
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect();

    let mut tokens = HashSet::new();
    for raw in &raw_tokens {
        let parts = split_camel_case(raw);
        for part in parts {
            if part.len() >= 2 && !noise.contains(part.as_str()) {
                tokens.insert(part);
            }
        }
    }
    tokens
}

/// Dispatch tokenization based on content type.
pub fn tokenize_for_content(text: &str, content_type: ContentType) -> HashSet<String> {
    match content_type {
        ContentType::Code(lang) => tokenize_code(text, lang),
        _ => tokenize(text),
    }
}

/// Minimal suffix-stripping stemmer. Returns stem alongside original.
/// Strips common English suffixes, minimum stem length 3.
pub fn stem(word: &str) -> String {
    let w = word.to_lowercase();
    if w.len() < 4 {
        return w;
    }

    // Try longest suffixes first
    let suffixes = &[
        "ation", "tion", "ment", "ness", "able", "ible", "ence", "ance", "ing", "ful", "ous",
        "ive", "ize", "ise", "ify", "ate", "ed", "er", "ly", "al", "es",
    ];
    for suffix in suffixes {
        if let Some(stem) = w.strip_suffix(suffix) {
            if stem.len() >= 3 {
                return stem.to_string();
            }
        }
    }
    // Trailing 's' (but not 'ss')
    if w.ends_with('s') && !w.ends_with("ss") && w.len() > 3 {
        return w[..w.len() - 1].to_string();
    }
    w
}

/// Synonym groups for cross-modal matching.
/// Each group contains terms that should match each other.
const SYNONYM_GROUPS: &[&[&str]] = &[
    &[
        "auth",
        "authenticate",
        "authorization",
        "credential",
        "login",
        "jwt",
        "token",
        "verify",
    ],
    &[
        "db", "database", "sql", "query", "postgres", "sqlite", "mysql",
    ],
    &["err", "error", "exception", "panic", "fail", "failure"],
    &["config", "configuration", "setting", "preference", "option"],
    &["msg", "message", "notification", "alert", "event"],
    &["req", "request", "http", "api", "endpoint", "route"],
    &["resp", "response", "reply", "result", "output"],
    &["mem", "memory", "cache", "buffer", "storage"],
    &["exec", "execute", "run", "invoke", "call", "dispatch"],
    &["parse", "deserialize", "decode", "unmarshal", "extract"],
    &["serial", "serialize", "encode", "marshal", "format"],
    &["nav", "navigate", "redirect", "goto"],
];

/// Find synonym expansions for a token.
pub fn synonym_expand(token: &str) -> HashSet<String> {
    let mut result = HashSet::new();
    let lower = token.to_lowercase();
    let stemmed = stem(&lower);
    for group in SYNONYM_GROUPS {
        let matches = group.iter().any(|&t| t == lower || stem(t) == stemmed);
        if matches {
            for &t in *group {
                result.insert(t.to_string());
            }
        }
    }
    result
}

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

    fn make_fact(key: &str, value: &str, fact_id: &str) -> MemNode {
        MemNode {
            kind: MemKind::Fact,
            layer: 2,
            key: key.to_string(),
            value: value.to_string(),
            fact_id: Some(fact_id.to_string()),
            scope: "global".to_string(),
            authority: "peer".to_string(),
            is_constraint: false,
            created_at: Utc::now(),
            expires_at: None,
            content_type: ContentType::default(),
            metadata: FactMetadata::default(),
        }
    }

    #[test]
    fn insert_and_lookup() {
        let mut g = MemoryGraph::new();
        let nix = g.insert(make_fact("language", "Rust", "f1"));
        let (found_nix, node) = g.get_by_fact_id("f1").unwrap();
        assert_eq!(found_nix, nix);
        assert_eq!(node.value, "Rust");
    }

    #[test]
    fn supersession() {
        let mut g = MemoryGraph::new();
        g.insert(make_fact("db", "PostgreSQL", "f1"));
        let new_nix = g.insert(make_fact("db", "SQLite", "f2"));
        let invalidated = g.supersede(new_nix, "f1");

        assert!(!invalidated.is_empty());
        let (_, old) = g.get_by_fact_id("f1").unwrap();
        assert_eq!(old.kind, MemKind::FactSuperseded);
        assert_eq!(g.valid_fact_count(), 1);
    }

    #[test]
    fn dependency_cascade() {
        let mut g = MemoryGraph::new();
        let base = g.insert(make_fact("base", "100", "f1"));
        let derived = g.insert(make_fact("derived", "200", "f2"));
        g.link(derived, base, EdgeKind::DependsOn, 1.0);

        let new_base = g.insert(make_fact("base", "150", "f3"));
        let invalidated = g.supersede(new_base, "f1");

        // derived should be in invalidated set (it depends on f1)
        assert!(invalidated.contains(&derived));
    }

    #[test]
    fn seed_finding() {
        let mut g = MemoryGraph::new();
        g.insert(make_fact("navigate /ops/dashboard", "Page loads", "f1"));
        g.insert(make_fact("click button", "Button clicked", "f2"));

        let seeds = g.find_seeds("dashboard", 5);
        assert!(!seeds.is_empty()); // should find f1 via substring match
    }

    #[test]
    fn spreading_activation() {
        let mut g = MemoryGraph::new();
        let a = g.insert(make_fact("project", "CAR runtime", "f1"));
        let b = g.insert(make_fact("language", "Rust", "f2"));
        let c = g.insert(make_fact("testing", "uses proptest", "f3"));
        g.link(a, b, EdgeKind::RelatedTo, 0.8);
        g.link(b, c, EdgeKind::RelatedTo, 0.7);

        let hits = g.retrieve(&[a], 3, 10, 0.7, 0.05);
        // Should find b (1 hop) and c (2 hops) via RelatedTo edges
        assert!(hits.len() >= 2);
        assert!(hits[0].activation > hits[1].activation); // closer = higher
    }

    #[test]
    fn conversation_temporal_links() {
        let mut g = MemoryGraph::new();
        let c1 = g.insert(MemNode {
            kind: MemKind::Conversation,
            layer: 3,
            key: "user".to_string(),
            value: "How's the project?".to_string(),
            fact_id: None,
            scope: "global".to_string(),
            authority: "peer".to_string(),
            is_constraint: false,
            created_at: Utc::now(),
            expires_at: None,
            content_type: ContentType::NaturalLanguage,
            metadata: FactMetadata::default(),
        });
        let c2 = g.insert(MemNode {
            kind: MemKind::Conversation,
            layer: 3,
            key: "assistant".to_string(),
            value: "Going well".to_string(),
            fact_id: None,
            scope: "global".to_string(),
            authority: "peer".to_string(),
            is_constraint: false,
            created_at: Utc::now(),
            expires_at: None,
            content_type: ContentType::NaturalLanguage,
            metadata: FactMetadata::default(),
        });

        // Should have TemporalNext edge automatically
        assert_eq!(g.edge_count(), 1);
        let edge = g.inner.edges(c1).next().unwrap();
        assert_eq!(edge.weight().kind, EdgeKind::TemporalNext);
        assert_eq!(edge.target(), c2);
    }

    #[test]
    fn constraints_tracked() {
        let mut g = MemoryGraph::new();
        let mut node = make_fact("budget", "Max $500K", "c1");
        node.is_constraint = true;
        g.insert(node);
        g.insert(make_fact("language", "Rust", "f1"));

        assert_eq!(g.constraints().len(), 1);
        assert_eq!(g.valid_fact_count(), 2);
    }

    #[test]
    fn gc_superseded_removes_deep_chains() {
        // Build chain: f4 supersedes f3 supersedes f2 supersedes f1
        let mut g = MemoryGraph::new();
        let _n1 = g.insert(make_fact("db", "v1", "f1"));
        let n2 = g.insert(make_fact("db", "v2", "f2"));
        g.supersede(n2, "f1"); // f1 → FactSuperseded
        let n3 = g.insert(make_fact("db", "v3", "f3"));
        g.supersede(n3, "f2"); // f2 → FactSuperseded
        let n4 = g.insert(make_fact("db", "v4", "f4"));
        g.supersede(n4, "f3"); // f3 → FactSuperseded

        // Before GC: 4 nodes (1 active, 3 superseded)
        assert_eq!(g.node_count(), 4);

        // GC with max_depth=1: keep f3 (1 hop from active f4), remove f1 and f2
        let removed = g.gc_superseded(1);
        assert_eq!(removed, 2);
        assert_eq!(g.node_count(), 2);

        // f4 (active) and f3 (1 hop) should remain
        assert!(g.get_by_fact_id("f4").is_some());
        // f1 and f2 should be gone
        assert!(g.get_by_fact_id("f1").is_none());
        assert!(g.get_by_fact_id("f2").is_none());
    }

    #[test]
    fn gc_superseded_retains_shallow_chain() {
        // f2 supersedes f1 — depth 1, should be retained with max_depth=1
        let mut g = MemoryGraph::new();
        let _n1 = g.insert(make_fact("db", "old", "f1"));
        let n2 = g.insert(make_fact("db", "new", "f2"));
        g.supersede(n2, "f1");

        let removed = g.gc_superseded(1);
        assert_eq!(removed, 0);
        assert_eq!(g.node_count(), 2);
    }

    #[test]
    fn gc_superseded_removes_orphaned() {
        // A superseded node with no active ancestor (orphaned chain)
        let mut g = MemoryGraph::new();
        let mut orphan = make_fact("db", "orphan", "f1");
        orphan.kind = MemKind::FactSuperseded;
        g.insert(orphan);

        let removed = g.gc_superseded(1);
        assert_eq!(removed, 1);
        assert_eq!(g.node_count(), 0);
    }

    #[test]
    fn gc_superseded_noop_on_clean_graph() {
        let mut g = MemoryGraph::new();
        g.insert(make_fact("a", "1", "f1"));
        g.insert(make_fact("b", "2", "f2"));

        let removed = g.gc_superseded(1);
        assert_eq!(removed, 0);
        assert_eq!(g.node_count(), 2);
    }

    #[test]
    fn prune_expired_removes_ttl_nodes() {
        let mut g = MemoryGraph::new();
        let now = Utc::now();
        let mut expired = make_fact("temp", "gone", "e1");
        expired.kind = MemKind::Environment;
        expired.layer = 4;
        expired.expires_at = Some(now - chrono::Duration::hours(1));
        g.insert(expired);

        let mut fresh = make_fact("temp", "here", "e2");
        fresh.kind = MemKind::Environment;
        fresh.layer = 4;
        fresh.expires_at = Some(now + chrono::Duration::hours(1));
        g.insert(fresh);

        g.prune_expired(now);
        assert_eq!(g.node_count(), 1);
    }

    #[test]
    fn prune_expired_cleans_by_key_and_by_layer() {
        let mut g = MemoryGraph::new();
        let now = Utc::now();

        // Create an expired environment node
        let mut expired = make_fact("env_temp", "gone", "e1");
        expired.kind = MemKind::Environment;
        expired.layer = 4;
        expired.expires_at = Some(now - chrono::Duration::hours(1));
        g.insert(expired);

        // Verify indexes are populated before pruning
        assert!(g.by_key.contains_key("env_temp"));
        let layer_idx = (4usize).saturating_sub(1).min(3); // index 3
        assert_eq!(g.by_layer[layer_idx].len(), 1);

        g.prune_expired(now);

        // After pruning, by_key should not contain the key
        assert!(!g.by_key.contains_key("env_temp"));
        // by_layer should be clean
        assert_eq!(g.by_layer[layer_idx].len(), 0);
        // by_fact_id should be clean
        assert!(g.get_by_fact_id("e1").is_none());
    }

    #[test]
    fn gc_superseded_cleans_by_key_and_by_layer() {
        // Build chain: f3 supersedes f2 supersedes f1
        let mut g = MemoryGraph::new();
        let _n1 = g.insert(make_fact("db", "v1", "f1"));
        let n2 = g.insert(make_fact("db", "v2", "f2"));
        g.supersede(n2, "f1");
        let n3 = g.insert(make_fact("db", "v3", "f3"));
        g.supersede(n3, "f2");

        // Before GC: by_key["db"] has 3 entries, by_layer[1] has 3 entries
        assert_eq!(g.by_key.get("db").map(|v| v.len()).unwrap_or(0), 3);
        let layer_idx = (2usize).saturating_sub(1).min(3); // index 1
        let initial_layer_count = g.by_layer[layer_idx].len();
        assert_eq!(initial_layer_count, 3);

        // GC with max_depth=0: only f3 (active) kept, f1 and f2 removed
        let removed = g.gc_superseded(0);
        assert_eq!(removed, 2);

        // by_key["db"] should only have the active node (f3)
        assert_eq!(g.by_key.get("db").map(|v| v.len()).unwrap_or(0), 1);
        // by_layer should only have the active node
        assert_eq!(g.by_layer[layer_idx].len(), 1);
    }

    #[test]
    fn remove_conversation_nodes_cleans_by_key_and_by_layer() {
        let mut g = MemoryGraph::new();
        let c1 = g.insert(MemNode {
            kind: MemKind::Conversation,
            layer: 3,
            key: "user".to_string(),
            value: "Hello".to_string(),
            fact_id: None,
            scope: "global".to_string(),
            authority: "peer".to_string(),
            is_constraint: false,
            created_at: Utc::now(),
            expires_at: None,
            content_type: ContentType::NaturalLanguage,
            metadata: FactMetadata::default(),
        });
        let c2 = g.insert(MemNode {
            kind: MemKind::Conversation,
            layer: 3,
            key: "assistant".to_string(),
            value: "Hi there".to_string(),
            fact_id: None,
            scope: "global".to_string(),
            authority: "peer".to_string(),
            is_constraint: false,
            created_at: Utc::now(),
            expires_at: None,
            content_type: ContentType::NaturalLanguage,
            metadata: FactMetadata::default(),
        });

        // Verify indexes populated
        assert!(g.by_key.contains_key("user"));
        assert!(g.by_key.contains_key("assistant"));
        let layer_idx = (3usize).saturating_sub(1).min(3); // index 2
        assert_eq!(g.by_layer[layer_idx].len(), 2);

        // Remove both conversation nodes
        g.remove_conversation_nodes(&[c1, c2]);

        // by_key should be clean
        assert!(!g.by_key.contains_key("user"));
        assert!(!g.by_key.contains_key("assistant"));
        // by_layer should be clean
        assert_eq!(g.by_layer[layer_idx].len(), 0);
        // Graph should be empty
        assert_eq!(g.node_count(), 0);
    }
}