yantrikdb 0.21.0

Cognitive memory engine for persistent AI systems
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
//! CK-2.2: Temporal Reasoning Primitives
//!
//! Provides the temporal intelligence layer for YantrikDB's cognitive engine.
//! This module is purely functional — no database dependency — enabling
//! deterministic testing and composition.
//!
//! ## Capabilities
//!
//! 1. **Interval Algebra** — Allen's interval relations (before, after, during,
//!    overlaps, meets, starts, finishes, equals) for reasoning about event
//!    relationships and scheduling conflicts.
//!
//! 2. **Recency-Weighted Relevance** — Exponential decay functions that score
//!    how relevant a past event is to the current moment, with configurable
//!    half-life per domain.
//!
//! 3. **Periodicity Detection** — Infer daily/weekly/monthly patterns from
//!    sparse event timestamps using autocorrelation and peak detection.
//!
//! 4. **EWMA (Exponentially Weighted Moving Average)** — Smoothed tracking of
//!    continuous signals (response times, activity levels, mood) with
//!    configurable smoothing factor.
//!
//! 5. **Burst Detection** — Identifies sudden increases in event frequency
//!    using a sliding window model with z-score threshold.
//!
//! 6. **Temporal Motif Mining** — Discovers recurring sequences of event types
//!    (e.g., "email check → calendar review → standup") from episode streams.
//!
//! 7. **Deadline Urgency** — Sigmoid-based urgency curves for deadline-driven
//!    nodes, computing how urgently attention should be directed.
//!
//! 8. **Event Ordering** — Causal/temporal sequencing using topological sort
//!    over PrecedesTemporally and Triggers edges.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// ══════════════════════════════════════════════════════════════════════════════
// § 1  Interval Algebra
// ══════════════════════════════════════════════════════════════════════════════

/// A closed time interval `[start, end]` in seconds (unix timestamp).
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct TimeInterval {
    pub start: f64,
    pub end: f64,
}

impl TimeInterval {
    /// Create a new interval. Panics if `start > end`.
    pub fn new(start: f64, end: f64) -> Self {
        debug_assert!(start <= end, "TimeInterval: start ({start}) > end ({end})");
        Self { start, end }
    }

    /// Point interval (instantaneous event).
    pub fn point(t: f64) -> Self {
        Self { start: t, end: t }
    }

    /// Duration of the interval in seconds.
    #[inline]
    pub fn duration(&self) -> f64 {
        self.end - self.start
    }

    /// Midpoint of the interval.
    #[inline]
    pub fn midpoint(&self) -> f64 {
        (self.start + self.end) / 2.0
    }

    /// Whether this interval contains a point in time.
    #[inline]
    pub fn contains_point(&self, t: f64) -> bool {
        t >= self.start && t <= self.end
    }

    /// Whether this interval fully contains another interval.
    #[inline]
    pub fn contains_interval(&self, other: &TimeInterval) -> bool {
        self.start <= other.start && self.end >= other.end
    }

    /// Classify the relationship between two intervals using Allen's algebra.
    pub fn relation_to(&self, other: &TimeInterval) -> IntervalRelation {
        // Tolerance for "meets" — intervals within ε of touching are considered meeting
        const EPSILON: f64 = 0.5;

        if (self.end - other.start).abs() < EPSILON && self.start < other.start {
            return IntervalRelation::Meets;
        }
        if (other.end - self.start).abs() < EPSILON && other.start < self.start {
            return IntervalRelation::MetBy;
        }

        if self.end < other.start {
            IntervalRelation::Before
        } else if other.end < self.start {
            IntervalRelation::After
        } else if self.start == other.start && self.end == other.end {
            IntervalRelation::Equals
        } else if self.start == other.start && self.end < other.end {
            IntervalRelation::Starts
        } else if self.start == other.start && self.end > other.end {
            IntervalRelation::StartedBy
        } else if self.end == other.end && self.start > other.start {
            IntervalRelation::Finishes
        } else if self.end == other.end && self.start < other.start {
            IntervalRelation::FinishedBy
        } else if self.start > other.start && self.end < other.end {
            IntervalRelation::During
        } else if other.start > self.start && other.end < self.end {
            IntervalRelation::Contains
        } else if self.start < other.start && self.end > other.start && self.end < other.end {
            IntervalRelation::Overlaps
        } else if other.start < self.start && other.end > self.start && other.end < self.end {
            IntervalRelation::OverlappedBy
        } else {
            // Fallback — should not reach here with proper interval semantics
            IntervalRelation::Overlaps
        }
    }

    /// Compute the overlap duration with another interval (0 if no overlap).
    pub fn overlap_duration(&self, other: &TimeInterval) -> f64 {
        let overlap_start = self.start.max(other.start);
        let overlap_end = self.end.min(other.end);
        (overlap_end - overlap_start).max(0.0)
    }

    /// Merge two overlapping or adjacent intervals into one.
    /// Returns `None` if they are disjoint (gap > tolerance).
    pub fn merge(&self, other: &TimeInterval, tolerance: f64) -> Option<TimeInterval> {
        if self.start <= other.end + tolerance && other.start <= self.end + tolerance {
            Some(TimeInterval::new(
                self.start.min(other.start),
                self.end.max(other.end),
            ))
        } else {
            None
        }
    }
}

/// Allen's 13 interval relations — the complete set for temporal reasoning.
///
/// These relations are mutually exclusive and exhaustive: any two intervals
/// stand in exactly one of these 13 relations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum IntervalRelation {
    /// Self ends before other starts: `[s1,e1] < [s2,e2]`
    Before,
    /// Self starts after other ends: `[s2,e2] < [s1,e1]`
    After,
    /// Self meets other: `e1 ≈ s2` (within epsilon)
    Meets,
    /// Self is met by other: `e2 ≈ s1`
    MetBy,
    /// Self overlaps other: `s1 < s2 < e1 < e2`
    Overlaps,
    /// Self is overlapped by other: `s2 < s1 < e2 < e1`
    OverlappedBy,
    /// Self starts other: same start, self ends first
    Starts,
    /// Self is started by other: same start, other ends first
    StartedBy,
    /// Self finishes other: same end, self starts later
    Finishes,
    /// Self is finished by other: same end, other starts later
    FinishedBy,
    /// Self during other: fully contained within
    During,
    /// Self contains other: fully contains
    Contains,
    /// Identical intervals
    Equals,
}

impl IntervalRelation {
    /// Whether the two intervals have any temporal overlap.
    pub fn has_overlap(self) -> bool {
        !matches!(self, Self::Before | Self::After | Self::Meets | Self::MetBy)
    }

    /// Inverse relation (swap perspectives).
    pub fn inverse(self) -> Self {
        match self {
            Self::Before => Self::After,
            Self::After => Self::Before,
            Self::Meets => Self::MetBy,
            Self::MetBy => Self::Meets,
            Self::Overlaps => Self::OverlappedBy,
            Self::OverlappedBy => Self::Overlaps,
            Self::Starts => Self::StartedBy,
            Self::StartedBy => Self::Starts,
            Self::Finishes => Self::FinishedBy,
            Self::FinishedBy => Self::Finishes,
            Self::During => Self::Contains,
            Self::Contains => Self::During,
            Self::Equals => Self::Equals,
        }
    }
}

// ══════════════════════════════════════════════════════════════════════════════
// § 2  Recency-Weighted Relevance
// ══════════════════════════════════════════════════════════════════════════════

/// Configuration for recency-weighted relevance scoring.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecencyConfig {
    /// Half-life in seconds — the time for relevance to decay to 50%.
    /// Default: 86400.0 (1 day).
    pub half_life_secs: f64,
    /// Minimum relevance floor — events never decay below this.
    /// Default: 0.01.
    pub floor: f64,
}

impl Default for RecencyConfig {
    fn default() -> Self {
        Self {
            half_life_secs: 86400.0,
            floor: 0.01,
        }
    }
}

/// Domain-specific half-lives for different types of information.
/// Some things stay relevant longer than others.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DomainRecencyMap {
    /// Domain name → half-life in seconds.
    pub entries: HashMap<String, f64>,
    /// Fallback half-life for unknown domains.
    pub default_half_life: f64,
}

impl Default for DomainRecencyMap {
    fn default() -> Self {
        let mut entries = HashMap::new();
        // Conversations decay fast — yesterday's chat is less relevant
        entries.insert("conversation".to_string(), 3600.0 * 4.0);
        // Tasks have medium persistence
        entries.insert("task".to_string(), 86400.0 * 3.0);
        // Goals persist much longer
        entries.insert("goal".to_string(), 86400.0 * 30.0);
        // Beliefs are very persistent
        entries.insert("belief".to_string(), 86400.0 * 90.0);
        // Routines have long memory (need history to detect patterns)
        entries.insert("routine".to_string(), 86400.0 * 60.0);
        // Preferences are quasi-permanent
        entries.insert("preference".to_string(), 86400.0 * 180.0);
        // Episodes (events) decay at medium rate
        entries.insert("episode".to_string(), 86400.0 * 7.0);

        Self {
            entries,
            default_half_life: 86400.0,
        }
    }
}

impl DomainRecencyMap {
    /// Get the half-life for a domain.
    pub fn half_life_for(&self, domain: &str) -> f64 {
        self.entries
            .get(domain)
            .copied()
            .unwrap_or(self.default_half_life)
    }
}

/// Compute recency-weighted relevance score for an event.
///
/// Uses exponential decay: `relevance = max(floor, 2^(-age / half_life))`.
///
/// # Arguments
/// * `event_time` — When the event occurred (unix seconds).
/// * `now` — Current time (unix seconds).
/// * `config` — Recency configuration.
///
/// # Returns
/// Relevance in `[floor, 1.0]` — 1.0 means "just happened", floor for very old.
pub fn recency_relevance(event_time: f64, now: f64, config: &RecencyConfig) -> f64 {
    let age = (now - event_time).max(0.0);
    let decay = f64::powf(2.0, -age / config.half_life_secs);
    decay.max(config.floor)
}

/// Compute recency-weighted relevance with domain-specific half-life.
pub fn recency_relevance_domain(
    event_time: f64,
    now: f64,
    domain: &str,
    domain_map: &DomainRecencyMap,
) -> f64 {
    let half_life = domain_map.half_life_for(domain);
    let config = RecencyConfig {
        half_life_secs: half_life,
        floor: 0.01,
    };
    recency_relevance(event_time, now, &config)
}

/// Score a batch of events by recency and return them sorted (most relevant first).
///
/// Each event is `(event_id, event_time)`. Returns `(event_id, relevance_score)`.
pub fn rank_by_recency(events: &[(u64, f64)], now: f64, config: &RecencyConfig) -> Vec<(u64, f64)> {
    let mut scored: Vec<(u64, f64)> = events
        .iter()
        .map(|&(id, time)| (id, recency_relevance(time, now, config)))
        .collect();
    scored.sort_by(|a, b| b.1.total_cmp(&a.1));
    scored
}

// ══════════════════════════════════════════════════════════════════════════════
// § 3  Periodicity Detection
// ══════════════════════════════════════════════════════════════════════════════

/// Configuration for periodicity detection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeriodicityConfig {
    /// Candidate periods to test (in seconds).
    /// Default: common human-scale periods.
    pub candidate_periods: Vec<f64>,
    /// Minimum number of events required for reliable detection.
    /// Default: 5.
    pub min_events: usize,
    /// Autocorrelation threshold for a period to be considered detected.
    /// Default: 0.3 (moderate correlation).
    pub correlation_threshold: f64,
    /// Tolerance window as a fraction of the period.
    /// Events within `period * tolerance` of a predicted time count as hits.
    /// Default: 0.15 (15% of period).
    pub tolerance_fraction: f64,
}

impl Default for PeriodicityConfig {
    fn default() -> Self {
        Self {
            candidate_periods: vec![
                3600.0,         // hourly
                3600.0 * 4.0,   // 4-hourly
                86400.0,        // daily
                86400.0 * 7.0,  // weekly
                86400.0 * 14.0, // biweekly
                86400.0 * 30.0, // monthly
                86400.0 * 90.0, // quarterly
            ],
            min_events: 5,
            correlation_threshold: 0.3,
            tolerance_fraction: 0.15,
        }
    }
}

/// Result of periodicity detection for a single candidate period.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetectedPeriod {
    /// The period in seconds.
    pub period_secs: f64,
    /// Autocorrelation score [0.0, 1.0].
    pub correlation: f64,
    /// Phase offset: seconds into the period when events cluster.
    pub phase_offset_secs: f64,
    /// Fraction of expected events that actually occurred.
    pub hit_rate: f64,
    /// Human-readable label (e.g., "daily", "weekly").
    pub label: String,
}

/// Full periodicity analysis result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeriodicityResult {
    /// All detected periods (above threshold), sorted by correlation descending.
    pub detected: Vec<DetectedPeriod>,
    /// Number of events analyzed.
    pub event_count: usize,
    /// Time span of the data (seconds from first to last event).
    pub span_secs: f64,
}

/// Detect periodicities in a set of event timestamps.
///
/// Uses a correlation-based approach:
/// 1. For each candidate period `T`, compute phase offsets `t_i mod T`
/// 2. Compute circular concentration (Rayleigh test analog) of phase offsets
/// 3. Periods with high concentration indicate periodicity
///
/// # Arguments
/// * `timestamps` — Unix timestamps of events (need not be sorted).
/// * `config` — Periodicity detection parameters.
///
/// # Returns
/// `PeriodicityResult` with detected periods sorted by correlation.
pub fn detect_periodicity(timestamps: &[f64], config: &PeriodicityConfig) -> PeriodicityResult {
    if timestamps.len() < config.min_events {
        return PeriodicityResult {
            detected: vec![],
            event_count: timestamps.len(),
            span_secs: 0.0,
        };
    }

    let mut sorted = timestamps.to_vec();
    sorted.sort_by(|a, b| a.total_cmp(b));

    let span = sorted.last().unwrap() - sorted.first().unwrap();
    if span <= 0.0 {
        return PeriodicityResult {
            detected: vec![],
            event_count: timestamps.len(),
            span_secs: 0.0,
        };
    }

    let n = sorted.len() as f64;
    let mut detected = Vec::new();

    for &period in &config.candidate_periods {
        // Skip if we don't have enough span to observe this period
        if span < period * 1.5 {
            continue;
        }

        // Compute phase offsets normalized to [0, 2π]
        let two_pi = std::f64::consts::TAU;
        let phases: Vec<f64> = sorted
            .iter()
            .map(|&t| ((t % period) / period) * two_pi)
            .collect();

        // Rayleigh test: compute mean resultant length
        // R = sqrt((sum cos θ)² + (sum sin θ)²) / n
        let cos_sum: f64 = phases.iter().map(|&p| p.cos()).sum();
        let sin_sum: f64 = phases.iter().map(|&p| p.sin()).sum();
        let r = (cos_sum * cos_sum + sin_sum * sin_sum).sqrt() / n;

        // Mean phase angle → phase offset in seconds
        let mean_phase = sin_sum.atan2(cos_sum);
        let phase_offset = ((mean_phase / two_pi) * period + period) % period;

        // Hit rate: fraction of events falling within tolerance of predicted times
        let tolerance = period * config.tolerance_fraction;
        let hits = sorted
            .iter()
            .filter(|&&t| {
                let offset = (t - phase_offset) % period;
                let offset = if offset < 0.0 {
                    offset + period
                } else {
                    offset
                };
                offset < tolerance || (period - offset) < tolerance
            })
            .count();
        let hit_rate = hits as f64 / n;

        if r >= config.correlation_threshold {
            detected.push(DetectedPeriod {
                period_secs: period,
                correlation: r,
                phase_offset_secs: phase_offset,
                hit_rate,
                label: period_label(period),
            });
        }
    }

    // Sort by correlation descending
    detected.sort_by(|a, b| b.correlation.total_cmp(&a.correlation));

    PeriodicityResult {
        detected,
        event_count: timestamps.len(),
        span_secs: span,
    }
}

/// Human-readable label for a period.
fn period_label(period_secs: f64) -> String {
    let hours = period_secs / 3600.0;
    if hours < 2.0 {
        format!("{:.0}min", period_secs / 60.0)
    } else if hours < 48.0 {
        format!("{:.0}h", hours)
    } else {
        let days = hours / 24.0;
        if (days - 7.0).abs() < 1.0 {
            "weekly".to_string()
        } else if (days - 14.0).abs() < 2.0 {
            "biweekly".to_string()
        } else if (days - 30.0).abs() < 5.0 {
            "monthly".to_string()
        } else if (days - 90.0).abs() < 10.0 {
            "quarterly".to_string()
        } else {
            format!("{:.0}d", days)
        }
    }
}

// ══════════════════════════════════════════════════════════════════════════════
// § 4  EWMA — Exponentially Weighted Moving Average
// ══════════════════════════════════════════════════════════════════════════════

/// Exponentially Weighted Moving Average tracker.
///
/// Smooths a stream of observations with configurable responsiveness.
/// α close to 1.0 = highly responsive to new values (noisy).
/// α close to 0.0 = highly smoothed (sluggish).
///
/// Update rule: `ewma = α × new_value + (1 - α) × ewma`
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EwmaTracker {
    /// Smoothing factor α ∈ (0, 1].
    pub alpha: f64,
    /// Current smoothed value.
    pub value: f64,
    /// Current smoothed variance (for anomaly detection).
    pub variance: f64,
    /// Number of observations ingested.
    pub count: u64,
    /// Last observation timestamp.
    pub last_time: f64,
}

impl EwmaTracker {
    /// Create a new EWMA tracker with the given smoothing factor.
    ///
    /// # Panics
    /// Panics if `alpha` is not in (0, 1].
    pub fn new(alpha: f64) -> Self {
        assert!(alpha > 0.0 && alpha <= 1.0, "alpha must be in (0, 1]");
        Self {
            alpha,
            value: 0.0,
            variance: 0.0,
            count: 0,
            last_time: 0.0,
        }
    }

    /// Create a tracker with initial value (avoids cold-start bias).
    pub fn with_initial(alpha: f64, initial_value: f64) -> Self {
        assert!(alpha > 0.0 && alpha <= 1.0, "alpha must be in (0, 1]");
        Self {
            alpha,
            value: initial_value,
            variance: 0.0,
            count: 1,
            last_time: 0.0,
        }
    }

    /// Ingest a new observation.
    pub fn update(&mut self, observation: f64, timestamp: f64) {
        if self.count == 0 {
            // First observation — initialize directly
            self.value = observation;
            self.variance = 0.0;
        } else {
            let diff = observation - self.value;
            self.value += self.alpha * diff;
            // Welford-style online variance with exponential weighting
            self.variance = (1.0 - self.alpha) * (self.variance + self.alpha * diff * diff);
        }
        self.count += 1;
        self.last_time = timestamp;
    }

    /// Current smoothed value.
    #[inline]
    pub fn current(&self) -> f64 {
        self.value
    }

    /// Estimated standard deviation.
    #[inline]
    pub fn std_dev(&self) -> f64 {
        self.variance.sqrt()
    }

    /// Z-score of a new observation relative to the current EWMA.
    /// Useful for anomaly detection.
    pub fn z_score(&self, observation: f64) -> f64 {
        let sd = self.std_dev();
        if sd < 1e-10 || self.count < 3 {
            0.0
        } else {
            (observation - self.value) / sd
        }
    }

    /// Whether a new observation would be anomalous (|z| > threshold).
    pub fn is_anomaly(&self, observation: f64, z_threshold: f64) -> bool {
        self.z_score(observation).abs() > z_threshold
    }
}

// ══════════════════════════════════════════════════════════════════════════════
// § 5  Burst Detection
// ══════════════════════════════════════════════════════════════════════════════

/// Configuration for burst detection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BurstConfig {
    /// Window size in seconds for computing event rate.
    /// Default: 3600.0 (1 hour).
    pub window_secs: f64,
    /// Z-score threshold for declaring a burst.
    /// Default: 2.5.
    pub z_threshold: f64,
    /// Minimum number of windows required before detection is reliable.
    /// Default: 5.
    pub min_windows: usize,
    /// Smoothing factor for baseline rate EWMA.
    /// Default: 0.2.
    pub ewma_alpha: f64,
}

impl Default for BurstConfig {
    fn default() -> Self {
        Self {
            window_secs: 3600.0,
            z_threshold: 2.5,
            min_windows: 5,
            ewma_alpha: 0.2,
        }
    }
}

/// A detected burst of activity.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetectedBurst {
    /// Start of the burst window (unix seconds).
    pub window_start: f64,
    /// End of the burst window (unix seconds).
    pub window_end: f64,
    /// Event count in the burst window.
    pub event_count: usize,
    /// Z-score of this window's rate vs. baseline.
    pub z_score: f64,
    /// Baseline event rate (events per window).
    pub baseline_rate: f64,
}

/// Burst detection result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BurstResult {
    /// Detected bursts, sorted chronologically.
    pub bursts: Vec<DetectedBurst>,
    /// Overall event rate (events per window).
    pub overall_rate: f64,
    /// Total windows analyzed.
    pub windows_analyzed: usize,
}

/// Detect bursts in a stream of event timestamps.
///
/// Divides the time range into fixed windows, computes event counts per window,
/// then uses EWMA to establish a baseline rate and flags windows that exceed
/// the baseline by more than `z_threshold` standard deviations.
///
/// # Arguments
/// * `timestamps` — Unix timestamps of events (need not be sorted).
/// * `config` — Burst detection parameters.
pub fn detect_bursts(timestamps: &[f64], config: &BurstConfig) -> BurstResult {
    if timestamps.is_empty() {
        return BurstResult {
            bursts: vec![],
            overall_rate: 0.0,
            windows_analyzed: 0,
        };
    }

    let mut sorted = timestamps.to_vec();
    sorted.sort_by(|a, b| a.total_cmp(b));

    let first = sorted[0];
    let last = sorted[sorted.len() - 1];
    let span = last - first;

    if span < config.window_secs {
        return BurstResult {
            bursts: vec![],
            overall_rate: sorted.len() as f64,
            windows_analyzed: 1,
        };
    }

    // Divide into windows and count events per window
    let num_windows = ((span / config.window_secs).ceil() as usize).max(1);
    let mut window_counts = vec![0usize; num_windows];

    for &t in &sorted {
        let idx = ((t - first) / config.window_secs).floor() as usize;
        let idx = idx.min(num_windows - 1);
        window_counts[idx] += 1;
    }

    let overall_rate = sorted.len() as f64 / num_windows as f64;

    // Use EWMA to build adaptive baseline
    let mut tracker = EwmaTracker::new(config.ewma_alpha);
    let mut bursts = Vec::new();

    for (i, &count) in window_counts.iter().enumerate() {
        let rate = count as f64;
        let window_start = first + i as f64 * config.window_secs;
        let window_end = window_start + config.window_secs;

        if tracker.count >= config.min_windows as u64 {
            let z = tracker.z_score(rate);
            if z > config.z_threshold {
                bursts.push(DetectedBurst {
                    window_start,
                    window_end,
                    event_count: count,
                    z_score: z,
                    baseline_rate: tracker.current(),
                });
            }
        }

        tracker.update(rate, window_start);
    }

    BurstResult {
        bursts,
        overall_rate,
        windows_analyzed: num_windows,
    }
}

// ══════════════════════════════════════════════════════════════════════════════
// § 6  Temporal Motif Mining
// ══════════════════════════════════════════════════════════════════════════════

/// A labeled temporal event for motif mining.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LabeledEvent {
    /// Event type label (e.g., "email_check", "calendar_view", "task_complete").
    pub label: String,
    /// When the event occurred (unix seconds).
    pub timestamp: f64,
}

/// A discovered temporal motif (recurring sequence of event types).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemporalMotif {
    /// Ordered sequence of event labels.
    pub sequence: Vec<String>,
    /// Number of times this sequence was observed.
    pub occurrences: u32,
    /// Average time span of the motif (seconds from first to last event).
    pub avg_span_secs: f64,
    /// Average inter-event gaps within the motif (seconds).
    pub avg_gaps: Vec<f64>,
    /// Confidence score [0.0, 1.0] based on consistency of timing.
    pub confidence: f64,
}

/// Configuration for motif mining.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MotifConfig {
    /// Maximum time gap between consecutive events in a motif (seconds).
    /// Default: 7200.0 (2 hours).
    pub max_gap_secs: f64,
    /// Minimum motif length (number of events).
    /// Default: 2.
    pub min_length: usize,
    /// Maximum motif length.
    /// Default: 5.
    pub max_length: usize,
    /// Minimum number of occurrences to report a motif.
    /// Default: 3.
    pub min_occurrences: u32,
}

impl Default for MotifConfig {
    fn default() -> Self {
        Self {
            max_gap_secs: 7200.0,
            min_length: 2,
            max_length: 5,
            min_occurrences: 3,
        }
    }
}

/// Mine temporal motifs from a stream of labeled events.
///
/// Finds recurring subsequences where:
/// 1. Events occur within `max_gap_secs` of each other
/// 2. The same sequence appears at least `min_occurrences` times
/// 3. Timing is reasonably consistent (measured by confidence)
///
/// # Arguments
/// * `events` — Labeled events (need not be sorted).
/// * `config` — Mining parameters.
pub fn mine_temporal_motifs(events: &[LabeledEvent], config: &MotifConfig) -> Vec<TemporalMotif> {
    if events.len() < config.min_length {
        return vec![];
    }

    // Sort by timestamp
    let mut sorted: Vec<&LabeledEvent> = events.iter().collect();
    sorted.sort_by(|a, b| a.timestamp.total_cmp(&b.timestamp));

    // Extract all subsequences of valid length within max_gap constraint
    // Key: sequence of labels → list of (span_secs, gap_secs_list)
    let mut pattern_instances: HashMap<Vec<String>, Vec<(f64, Vec<f64>)>> = HashMap::new();

    for start_idx in 0..sorted.len() {
        let mut sequence = vec![sorted[start_idx].label.clone()];
        let mut gaps = Vec::new();

        for end_idx in (start_idx + 1)..sorted.len() {
            let gap = sorted[end_idx].timestamp - sorted[end_idx - 1].timestamp;
            if gap > config.max_gap_secs {
                break;
            }
            if gap < 0.0 {
                break; // Should not happen after sorting
            }

            gaps.push(gap);
            sequence.push(sorted[end_idx].label.clone());

            if sequence.len() >= config.min_length && sequence.len() <= config.max_length {
                let span = sorted[end_idx].timestamp - sorted[start_idx].timestamp;
                pattern_instances
                    .entry(sequence.clone())
                    .or_default()
                    .push((span, gaps.clone()));
            }

            if sequence.len() >= config.max_length {
                break;
            }
        }
    }

    // Convert to motifs, filtering by min_occurrences
    let mut motifs: Vec<TemporalMotif> = pattern_instances
        .into_iter()
        .filter(|(_, instances)| instances.len() >= config.min_occurrences as usize)
        .map(|(sequence, instances)| {
            let occurrences = instances.len() as u32;

            // Average span
            let avg_span = instances.iter().map(|(s, _)| s).sum::<f64>() / instances.len() as f64;

            // Average gaps per position
            let gap_count = sequence.len() - 1;
            let mut avg_gaps = vec![0.0; gap_count];
            let mut gap_variances = vec![0.0; gap_count];

            for (_, gaps) in &instances {
                for (i, &g) in gaps.iter().enumerate() {
                    if i < gap_count {
                        avg_gaps[i] += g;
                    }
                }
            }
            for g in &mut avg_gaps {
                *g /= instances.len() as f64;
            }

            // Compute gap variance for confidence
            for (_, gaps) in &instances {
                for (i, &g) in gaps.iter().enumerate() {
                    if i < gap_count {
                        let diff = g - avg_gaps[i];
                        gap_variances[i] += diff * diff;
                    }
                }
            }
            for v in &mut gap_variances {
                *v /= instances.len() as f64;
            }

            // Confidence: based on coefficient of variation of gaps
            // Low variance relative to mean gap → high confidence
            let confidence = if gap_count == 0 {
                1.0
            } else {
                let mean_cv: f64 = avg_gaps
                    .iter()
                    .zip(gap_variances.iter())
                    .map(|(&mean, &var)| {
                        if mean < 1.0 {
                            1.0 // Negligible gap — perfect confidence
                        } else {
                            let cv = var.sqrt() / mean;
                            (1.0 - cv).max(0.0) // Low CV → high confidence
                        }
                    })
                    .sum::<f64>()
                    / gap_count as f64;
                mean_cv.clamp(0.0, 1.0)
            };

            TemporalMotif {
                sequence,
                occurrences,
                avg_span_secs: avg_span,
                avg_gaps,
                confidence,
            }
        })
        .collect();

    // Sort by occurrences × confidence (most significant first)
    motifs.sort_by(|a, b| {
        let score_a = a.occurrences as f64 * a.confidence;
        let score_b = b.occurrences as f64 * b.confidence;
        score_b.total_cmp(&score_a)
    });

    motifs
}

// ══════════════════════════════════════════════════════════════════════════════
// § 7  Deadline Urgency
// ══════════════════════════════════════════════════════════════════════════════

/// Configuration for deadline urgency computation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeadlineUrgencyConfig {
    /// Steepness of the normalized sigmoid curve (higher = sharper transition).
    /// Default: 10.0 (smooth S-curve over the ramp window).
    pub steepness: f64,
    /// How far before the deadline urgency begins to rise (seconds).
    /// Default: 86400.0 (1 day before).
    pub ramp_start_before: f64,
    /// Urgency value when exactly at the deadline.
    /// Default: 0.95 (not quite 1.0 to leave room for overdue escalation).
    pub at_deadline: f64,
    /// Urgency value when past the deadline.
    /// Default: 1.0.
    pub overdue: f64,
    /// Base urgency when far from deadline.
    /// Default: 0.02.
    pub floor: f64,
}

impl Default for DeadlineUrgencyConfig {
    fn default() -> Self {
        Self {
            steepness: 10.0,
            ramp_start_before: 86400.0,
            at_deadline: 0.95,
            overdue: 1.0,
            floor: 0.02,
        }
    }
}

/// Compute deadline urgency for a node.
///
/// Uses a normalized sigmoid over the ramp window:
/// 1. Compute `fraction = 1 - time_remaining / ramp_start_before` (0 at ramp start, 1 at deadline)
/// 2. Apply sigmoid: `sig(k * (fraction - 0.5))`, normalized to [0, 1]
/// 3. Scale to `[floor, at_deadline]`
///
/// Guarantees:
/// - Monotonically increasing as `now` approaches `deadline`
/// - Returns `floor` when far from deadline
/// - Returns `at_deadline` at the deadline
/// - Returns `overdue` when past the deadline
///
/// # Arguments
/// * `deadline` — When the task/goal is due (unix seconds).
/// * `now` — Current time (unix seconds).
/// * `config` — Urgency curve parameters.
///
/// # Returns
/// Urgency in [0.0, 1.0].
pub fn deadline_urgency(deadline: f64, now: f64, config: &DeadlineUrgencyConfig) -> f64 {
    let time_remaining = deadline - now;

    if time_remaining <= 0.0 {
        return config.overdue;
    }

    if time_remaining > config.ramp_start_before {
        return config.floor;
    }

    // fraction: 0.0 at ramp start, 1.0 at deadline
    let fraction = 1.0 - (time_remaining / config.ramp_start_before);

    // Normalized sigmoid: maps [0, 1] → [0, 1]
    let k = config.steepness;
    let raw = 1.0 / (1.0 + (-k * (fraction - 0.5)).exp());
    let sig_low = 1.0 / (1.0 + (k * 0.5).exp()); // sig at fraction=0
    let sig_high = 1.0 / (1.0 + (-k * 0.5).exp()); // sig at fraction=1
    let normalized = (raw - sig_low) / (sig_high - sig_low);

    // Scale to [floor, at_deadline]
    let urgency = config.floor + (config.at_deadline - config.floor) * normalized;
    urgency.clamp(0.0, 1.0)
}

/// Compute compound urgency considering both deadline and priority.
///
/// Higher-priority items become urgent earlier by expanding the ramp window.
/// A Critical task (weight=1.0) starts ramping at 2× the base window.
/// A Low task (weight=0.25) starts ramping at ~0.875× the base window.
pub fn compound_urgency(
    deadline: f64,
    now: f64,
    priority_weight: f64,
    config: &DeadlineUrgencyConfig,
) -> f64 {
    // Priority scales the ramp_start_before: Critical items become urgent earlier
    let scale = 0.5 + 1.5 * priority_weight; // Low=0.875, Med=1.25, High=1.625, Crit=2.0
    let adjusted_config = DeadlineUrgencyConfig {
        ramp_start_before: config.ramp_start_before * scale,
        ..*config
    };
    deadline_urgency(deadline, now, &adjusted_config)
}

// ══════════════════════════════════════════════════════════════════════════════
// § 8  Event Ordering & Causal Sequencing
// ══════════════════════════════════════════════════════════════════════════════

/// An event in a temporal sequence, referenced by node ID.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemporalEvent {
    /// The cognitive node this event represents.
    pub node_id: super::state::NodeId,
    /// Timestamp of the event (unix seconds).
    pub timestamp: f64,
    /// Node IDs that this event causally/temporally depends on.
    pub predecessors: Vec<super::state::NodeId>,
}

/// Result of topological ordering of temporal events.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemporalOrder {
    /// Events in causal/temporal order (predecessors before dependents).
    pub ordered: Vec<super::state::NodeId>,
    /// Events involved in dependency cycles (if any).
    pub cycles: Vec<super::state::NodeId>,
    /// Depth of each node in the dependency graph (0 = no predecessors).
    pub depths: HashMap<u32, usize>,
}

/// Compute a topological ordering of temporal events.
///
/// Events are ordered such that predecessors appear before dependents.
/// Falls back to timestamp ordering when no explicit dependencies exist.
/// Detects cycles and reports them separately.
///
/// # Arguments
/// * `events` — Temporal events with predecessor relationships.
pub fn topological_order(events: &[TemporalEvent]) -> TemporalOrder {
    if events.is_empty() {
        return TemporalOrder {
            ordered: vec![],
            cycles: vec![],
            depths: HashMap::new(),
        };
    }

    // Build adjacency and in-degree maps
    let mut adj: HashMap<u32, Vec<u32>> = HashMap::new();
    let mut in_degree: HashMap<u32, usize> = HashMap::new();
    let mut all_nodes: Vec<u32> = Vec::new();

    for event in events {
        let nid = event.node_id.to_raw();
        all_nodes.push(nid);
        in_degree.entry(nid).or_insert(0);
        adj.entry(nid).or_default();

        for &pred in &event.predecessors {
            let pid = pred.to_raw();
            adj.entry(pid).or_default().push(nid);
            *in_degree.entry(nid).or_insert(0) += 1;
            in_degree.entry(pid).or_insert(0);
        }
    }

    // Kahn's algorithm for topological sort
    let mut queue: Vec<u32> = in_degree
        .iter()
        .filter(|(_, &deg)| deg == 0)
        .map(|(&nid, _)| nid)
        .collect();

    // Sort initial queue by timestamp for deterministic ordering among peers
    let ts_map: HashMap<u32, f64> = events
        .iter()
        .map(|e| (e.node_id.to_raw(), e.timestamp))
        .collect();
    queue.sort_by(|a, b| {
        let ta = ts_map.get(a).copied().unwrap_or(0.0);
        let tb = ts_map.get(b).copied().unwrap_or(0.0);
        ta.total_cmp(&tb)
    });

    let mut ordered = Vec::new();
    let mut depths: HashMap<u32, usize> = HashMap::new();
    let mut processed = std::collections::HashSet::new();

    while let Some(nid) = queue.first().copied() {
        queue.remove(0);
        if processed.contains(&nid) {
            continue;
        }
        processed.insert(nid);
        ordered.push(super::state::NodeId::from_raw(nid));

        let depth = *depths.get(&nid).unwrap_or(&0);

        if let Some(successors) = adj.get(&nid) {
            for &succ in successors {
                if let Some(deg) = in_degree.get_mut(&succ) {
                    *deg = deg.saturating_sub(1);
                    let succ_depth = depth + 1;
                    let existing = depths.entry(succ).or_insert(0);
                    *existing = (*existing).max(succ_depth);
                    if *deg == 0 {
                        queue.push(succ);
                    }
                }
            }
        }

        // Re-sort queue by timestamp for deterministic peer ordering
        queue.sort_by(|a, b| {
            let ta = ts_map.get(a).copied().unwrap_or(0.0);
            let tb = ts_map.get(b).copied().unwrap_or(0.0);
            ta.total_cmp(&tb)
        });
    }

    // Any nodes not in ordered are part of cycles
    let ordered_set: std::collections::HashSet<u32> = ordered.iter().map(|n| n.to_raw()).collect();
    let cycles: Vec<super::state::NodeId> = all_nodes
        .iter()
        .filter(|n| !ordered_set.contains(n))
        .map(|&n| super::state::NodeId::from_raw(n))
        .collect();

    TemporalOrder {
        ordered,
        cycles,
        depths,
    }
}

// ══════════════════════════════════════════════════════════════════════════════
// § 9  Seasonal Histogram
// ══════════════════════════════════════════════════════════════════════════════

/// A histogram that bins events by time-of-day or day-of-week.
///
/// Useful for understanding when events cluster (e.g., "user is most active
/// between 9am-11am on weekdays").
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SeasonalHistogram {
    /// Number of bins (e.g., 24 for hourly, 7 for daily).
    pub num_bins: usize,
    /// Count per bin.
    pub counts: Vec<u32>,
    /// Total events ingested.
    pub total: u32,
    /// Label for the histogram (e.g., "hour_of_day", "day_of_week").
    pub label: String,
}

impl SeasonalHistogram {
    /// Create a new histogram with the given number of bins.
    pub fn new(num_bins: usize, label: &str) -> Self {
        Self {
            num_bins,
            counts: vec![0; num_bins],
            total: 0,
            label: label.to_string(),
        }
    }

    /// Create an hour-of-day histogram (24 bins, 0=midnight, 23=11pm).
    pub fn hour_of_day() -> Self {
        Self::new(24, "hour_of_day")
    }

    /// Create a day-of-week histogram (7 bins, 0=Monday, 6=Sunday).
    pub fn day_of_week() -> Self {
        Self::new(7, "day_of_week")
    }

    /// Add an event at the given bin index.
    pub fn add(&mut self, bin: usize) {
        if bin < self.num_bins {
            self.counts[bin] += 1;
            self.total += 1;
        }
    }

    /// Add events from unix timestamps using a bin extraction function.
    ///
    /// `bin_fn` maps a timestamp to a bin index.
    pub fn add_timestamps(&mut self, timestamps: &[f64], bin_fn: impl Fn(f64) -> usize) {
        for &t in timestamps {
            let bin = bin_fn(t);
            self.add(bin);
        }
    }

    /// Normalized distribution (each bin as fraction of total).
    pub fn distribution(&self) -> Vec<f64> {
        if self.total == 0 {
            return vec![0.0; self.num_bins];
        }
        self.counts
            .iter()
            .map(|&c| c as f64 / self.total as f64)
            .collect()
    }

    /// Peak bin (most events). Returns (bin_index, count).
    pub fn peak(&self) -> (usize, u32) {
        self.counts
            .iter()
            .enumerate()
            .max_by_key(|(_, &c)| c)
            .map(|(i, &c)| (i, c))
            .unwrap_or((0, 0))
    }

    /// Top-k bins by count.
    pub fn top_k(&self, k: usize) -> Vec<(usize, u32)> {
        let mut indexed: Vec<(usize, u32)> = self
            .counts
            .iter()
            .enumerate()
            .map(|(i, &c)| (i, c))
            .collect();
        indexed.sort_by(|a, b| b.1.cmp(&a.1));
        indexed.truncate(k);
        indexed
    }

    /// Entropy of the distribution (bits). Low entropy = concentrated pattern.
    pub fn entropy(&self) -> f64 {
        let dist = self.distribution();
        dist.iter()
            .filter(|&&p| p > 0.0)
            .map(|&p| -p * p.log2())
            .sum()
    }

    /// Concentration ratio: fraction of events in the top-k bins.
    pub fn concentration(&self, top_k: usize) -> f64 {
        if self.total == 0 {
            return 0.0;
        }
        let top_sum: u32 = self.top_k(top_k).iter().map(|(_, c)| c).sum();
        top_sum as f64 / self.total as f64
    }
}

/// Extract hour-of-day from a unix timestamp.
///
/// Note: This uses UTC. For local time, offset must be applied before calling.
pub fn hour_of_day_utc(timestamp: f64) -> usize {
    let secs_in_day = timestamp % 86400.0;
    let hour = (secs_in_day / 3600.0).floor() as usize;
    hour.min(23)
}

/// Extract day-of-week from a unix timestamp (0=Thursday for epoch, adjusted to 0=Monday).
pub fn day_of_week_utc(timestamp: f64) -> usize {
    // Unix epoch (1970-01-01) was a Thursday (day 4 in ISO, 0-indexed Monday=0)
    let days_since_epoch = (timestamp / 86400.0).floor() as i64;
    let dow = ((days_since_epoch % 7 + 3) % 7) as usize; // 0=Monday
    dow.min(6)
}

// ══════════════════════════════════════════════════════════════════════════════
// § 10  Temporal Relevance Composite
// ══════════════════════════════════════════════════════════════════════════════

/// Configuration for composite temporal relevance scoring.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemporalRelevanceConfig {
    /// Weight for recency component.
    pub recency_weight: f64,
    /// Weight for periodicity alignment.
    pub periodicity_weight: f64,
    /// Weight for deadline urgency.
    pub deadline_weight: f64,
    /// Recency half-life in seconds.
    pub recency_half_life: f64,
}

impl Default for TemporalRelevanceConfig {
    fn default() -> Self {
        Self {
            recency_weight: 0.4,
            periodicity_weight: 0.3,
            deadline_weight: 0.3,
            recency_half_life: 86400.0,
        }
    }
}

/// Compute a composite temporal relevance score for a cognitive node.
///
/// Combines:
/// - **Recency**: How recently was this node updated?
/// - **Periodicity alignment**: If the node is a routine, how close are we
///   to its next occurrence?
/// - **Deadline urgency**: If the node has a deadline, how urgent is it?
///
/// # Arguments
/// * `last_updated` — When the node was last updated (unix seconds).
/// * `now` — Current time (unix seconds).
/// * `next_occurrence` — Next predicted occurrence if periodic (seconds), or `None`.
/// * `deadline` — Deadline if applicable (seconds), or `None`.
/// * `config` — Scoring weights.
///
/// # Returns
/// Composite relevance in [0.0, 1.0].
pub fn temporal_relevance_composite(
    last_updated: f64,
    now: f64,
    next_occurrence: Option<f64>,
    deadline: Option<f64>,
    config: &TemporalRelevanceConfig,
) -> f64 {
    let recency_config = RecencyConfig {
        half_life_secs: config.recency_half_life,
        floor: 0.01,
    };
    let recency_score = recency_relevance(last_updated, now, &recency_config);

    let periodicity_score = match next_occurrence {
        Some(next) => {
            let time_until = (next - now).max(0.0);
            // Close to next occurrence → high relevance
            // Uses inverse sigmoid: high when time_until is small
            1.0 / (1.0 + (time_until / 3600.0)) // Decays over hours
        }
        None => 0.0,
    };

    let deadline_score = match deadline {
        Some(dl) => deadline_urgency(dl, now, &DeadlineUrgencyConfig::default()),
        None => 0.0,
    };

    // Weighted combination, normalized
    let total_weight = config.recency_weight + config.periodicity_weight + config.deadline_weight;
    if total_weight < 1e-10 {
        return 0.0;
    }

    let composite = (config.recency_weight * recency_score
        + config.periodicity_weight * periodicity_score
        + config.deadline_weight * deadline_score)
        / total_weight;

    composite.clamp(0.0, 1.0)
}

// ══════════════════════════════════════════════════════════════════════════════
// Tests
// ══════════════════════════════════════════════════════════════════════════════

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

    // ── Interval Algebra ──

    #[test]
    fn test_interval_before_after() {
        let a = TimeInterval::new(10.0, 20.0);
        let b = TimeInterval::new(30.0, 40.0);
        assert_eq!(a.relation_to(&b), IntervalRelation::Before);
        assert_eq!(b.relation_to(&a), IntervalRelation::After);
        assert!(!a.relation_to(&b).has_overlap());
    }

    #[test]
    fn test_interval_meets() {
        let a = TimeInterval::new(10.0, 20.0);
        let b = TimeInterval::new(20.0, 30.0);
        assert_eq!(a.relation_to(&b), IntervalRelation::Meets);
        assert_eq!(b.relation_to(&a), IntervalRelation::MetBy);
    }

    #[test]
    fn test_interval_overlaps() {
        let a = TimeInterval::new(10.0, 25.0);
        let b = TimeInterval::new(20.0, 35.0);
        assert_eq!(a.relation_to(&b), IntervalRelation::Overlaps);
        assert_eq!(b.relation_to(&a), IntervalRelation::OverlappedBy);
        assert!(a.relation_to(&b).has_overlap());
    }

    #[test]
    fn test_interval_during_contains() {
        let a = TimeInterval::new(15.0, 25.0);
        let b = TimeInterval::new(10.0, 30.0);
        assert_eq!(a.relation_to(&b), IntervalRelation::During);
        assert_eq!(b.relation_to(&a), IntervalRelation::Contains);
    }

    #[test]
    fn test_interval_starts_finishes() {
        let a = TimeInterval::new(10.0, 20.0);
        let b = TimeInterval::new(10.0, 30.0);
        assert_eq!(a.relation_to(&b), IntervalRelation::Starts);
        assert_eq!(b.relation_to(&a), IntervalRelation::StartedBy);

        let c = TimeInterval::new(20.0, 30.0);
        let d = TimeInterval::new(10.0, 30.0);
        assert_eq!(c.relation_to(&d), IntervalRelation::Finishes);
        assert_eq!(d.relation_to(&c), IntervalRelation::FinishedBy);
    }

    #[test]
    fn test_interval_equals() {
        let a = TimeInterval::new(10.0, 20.0);
        let b = TimeInterval::new(10.0, 20.0);
        assert_eq!(a.relation_to(&b), IntervalRelation::Equals);
    }

    #[test]
    fn test_interval_overlap_duration() {
        let a = TimeInterval::new(10.0, 30.0);
        let b = TimeInterval::new(20.0, 40.0);
        assert!((a.overlap_duration(&b) - 10.0).abs() < 1e-10);

        let c = TimeInterval::new(50.0, 60.0);
        assert!((a.overlap_duration(&c) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_interval_merge() {
        let a = TimeInterval::new(10.0, 25.0);
        let b = TimeInterval::new(20.0, 35.0);
        let merged = a.merge(&b, 0.0).unwrap();
        assert!((merged.start - 10.0).abs() < 1e-10);
        assert!((merged.end - 35.0).abs() < 1e-10);

        let c = TimeInterval::new(50.0, 60.0);
        assert!(a.merge(&c, 0.0).is_none());
    }

    #[test]
    fn test_interval_inverse_symmetry() {
        for rel in [
            IntervalRelation::Before,
            IntervalRelation::After,
            IntervalRelation::Meets,
            IntervalRelation::MetBy,
            IntervalRelation::Overlaps,
            IntervalRelation::OverlappedBy,
            IntervalRelation::Starts,
            IntervalRelation::StartedBy,
            IntervalRelation::Finishes,
            IntervalRelation::FinishedBy,
            IntervalRelation::During,
            IntervalRelation::Contains,
            IntervalRelation::Equals,
        ] {
            assert_eq!(rel.inverse().inverse(), rel);
        }
    }

    // ── Recency ──

    #[test]
    fn test_recency_just_happened() {
        let config = RecencyConfig::default();
        let score = recency_relevance(1000.0, 1000.0, &config);
        assert!((score - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_recency_one_halflife_ago() {
        let config = RecencyConfig {
            half_life_secs: 100.0,
            floor: 0.0,
        };
        let score = recency_relevance(0.0, 100.0, &config);
        assert!((score - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_recency_floor() {
        let config = RecencyConfig {
            half_life_secs: 100.0,
            floor: 0.05,
        };
        // Very old event
        let score = recency_relevance(0.0, 1_000_000.0, &config);
        assert!((score - 0.05).abs() < 0.01);
    }

    #[test]
    fn test_recency_ranking() {
        let config = RecencyConfig::default();
        let events = vec![(1, 100.0), (2, 500.0), (3, 300.0)];
        let ranked = rank_by_recency(&events, 1000.0, &config);
        assert_eq!(ranked[0].0, 2); // Most recent
        assert_eq!(ranked[1].0, 3);
        assert_eq!(ranked[2].0, 1); // Oldest
    }

    #[test]
    fn test_domain_recency() {
        let map = DomainRecencyMap::default();
        // Conversations decay faster than goals
        let conv_half = map.half_life_for("conversation");
        let goal_half = map.half_life_for("goal");
        assert!(conv_half < goal_half);
    }

    // ── EWMA ──

    #[test]
    fn test_ewma_converges() {
        let mut tracker = EwmaTracker::new(0.3);
        for i in 0..100 {
            tracker.update(10.0, i as f64);
        }
        assert!((tracker.current() - 10.0).abs() < 0.01);
    }

    #[test]
    fn test_ewma_tracks_change() {
        let mut tracker = EwmaTracker::new(0.5);
        for i in 0..20 {
            tracker.update(5.0, i as f64);
        }
        // Now shift to 15.0
        for i in 20..40 {
            tracker.update(15.0, i as f64);
        }
        // Should be close to 15.0
        assert!((tracker.current() - 15.0).abs() < 0.5);
    }

    #[test]
    fn test_ewma_z_score() {
        let mut tracker = EwmaTracker::new(0.2);
        // Establish baseline at ~10
        for i in 0..50 {
            tracker.update(10.0 + (i as f64 * 0.01), i as f64);
        }
        // Spike should have high z-score
        let z = tracker.z_score(50.0);
        assert!(z > 2.0);
    }

    #[test]
    fn test_ewma_anomaly_detection() {
        let mut tracker = EwmaTracker::new(0.1);
        for i in 0..100 {
            tracker.update(5.0 + (i % 2) as f64, i as f64);
        }
        assert!(!tracker.is_anomaly(5.5, 3.0)); // Normal
        assert!(tracker.is_anomaly(100.0, 3.0)); // Anomalous
    }

    // ── Periodicity ──

    #[test]
    fn test_detect_daily_periodicity() {
        let config = PeriodicityConfig {
            min_events: 3,
            correlation_threshold: 0.3,
            ..Default::default()
        };
        // Generate events at ~9am daily for 2 weeks
        let base = 1000000.0;
        let daily = 86400.0;
        let timestamps: Vec<f64> = (0..14)
            .map(|d| base + d as f64 * daily + 100.0) // Small jitter
            .collect();

        let result = detect_periodicity(&timestamps, &config);
        assert!(!result.detected.is_empty());
        // Should detect daily period
        let has_daily = result
            .detected
            .iter()
            .any(|p| (p.period_secs - 86400.0).abs() < 1.0);
        assert!(
            has_daily,
            "Should detect daily period: {:?}",
            result.detected
        );
    }

    #[test]
    fn test_periodicity_insufficient_data() {
        let config = PeriodicityConfig {
            min_events: 10,
            ..Default::default()
        };
        let timestamps = vec![100.0, 200.0, 300.0];
        let result = detect_periodicity(&timestamps, &config);
        assert!(result.detected.is_empty());
    }

    #[test]
    fn test_periodicity_random_noise() {
        let config = PeriodicityConfig {
            min_events: 5,
            correlation_threshold: 0.5, // Higher threshold
            ..Default::default()
        };
        // Random-ish timestamps (no real periodicity)
        let timestamps = vec![
            100.0, 357.0, 981.0, 1234.0, 2001.0, 3500.0, 5600.0, 8900.0, 14000.0, 22000.0,
        ];
        let result = detect_periodicity(&timestamps, &config);
        // Should find few or no strong periodicities
        // (may find weak ones by chance, but correlation should be lower)
        for p in &result.detected {
            assert!(
                p.correlation < 0.9,
                "Unexpected strong periodicity in noise: {:?}",
                p
            );
        }
    }

    // ── Burst Detection ──

    #[test]
    fn test_detect_burst() {
        let config = BurstConfig {
            window_secs: 100.0,
            z_threshold: 2.0,
            min_windows: 3,
            ewma_alpha: 0.3,
        };
        // Normal rate: ~2 events per window, then a burst of 20
        let mut timestamps = Vec::new();
        // Normal windows (0-600s, ~2 per window)
        for i in 0..6 {
            timestamps.push(i as f64 * 100.0 + 10.0);
            timestamps.push(i as f64 * 100.0 + 50.0);
        }
        // Burst window (600-700s: 20 events)
        for j in 0..20 {
            timestamps.push(600.0 + j as f64 * 4.0);
        }

        let result = detect_bursts(&timestamps, &config);
        assert!(!result.bursts.is_empty(), "Should detect burst");
        // The burst should be in the 600-700 window
        let burst = &result.bursts[0];
        assert!(burst.event_count > 5);
        assert!(burst.z_score > 2.0);
    }

    #[test]
    fn test_no_burst_uniform() {
        let config = BurstConfig::default();
        // Uniform rate
        let timestamps: Vec<f64> = (0..100).map(|i| i as f64 * 100.0).collect();
        let result = detect_bursts(&timestamps, &config);
        assert!(
            result.bursts.is_empty(),
            "Uniform rate should not trigger burst"
        );
    }

    // ── Deadline Urgency ──

    #[test]
    fn test_urgency_overdue() {
        let config = DeadlineUrgencyConfig::default();
        let urgency = deadline_urgency(1000.0, 2000.0, &config);
        assert!((urgency - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_urgency_far_away() {
        let config = DeadlineUrgencyConfig::default();
        let urgency = deadline_urgency(1_000_000.0, 0.0, &config);
        assert!(
            (urgency - 0.02).abs() < 1e-10,
            "Far-away deadline should equal floor: {urgency}"
        );
    }

    #[test]
    fn test_urgency_monotonic_increase() {
        let config = DeadlineUrgencyConfig::default();
        let deadline = 100_000.0;
        let mut prev = 0.0;
        for t in (0..100_000).step_by(10_000) {
            let u = deadline_urgency(deadline, t as f64, &config);
            assert!(
                u >= prev - 1e-10,
                "Urgency should increase: {prev} -> {u} at t={t}"
            );
            prev = u;
        }
    }

    #[test]
    fn test_compound_urgency_priority_scaling() {
        let config = DeadlineUrgencyConfig::default();
        let deadline = 100_000.0;
        let now = 50_000.0;

        let low = compound_urgency(deadline, now, 0.25, &config);
        let high = compound_urgency(deadline, now, 1.0, &config);
        // Higher priority should cause higher urgency at the same time
        assert!(
            high >= low,
            "Higher priority should mean higher urgency: low={low}, high={high}"
        );
    }

    // ── Temporal Motifs ──

    #[test]
    fn test_mine_recurring_sequence() {
        let config = MotifConfig {
            max_gap_secs: 3600.0,
            min_length: 2,
            max_length: 3,
            min_occurrences: 3,
        };

        // Repeat pattern: email → calendar → standup, 3 times
        let mut events = Vec::new();
        for day in 0..3 {
            let base = day as f64 * 86400.0;
            events.push(LabeledEvent {
                label: "email".to_string(),
                timestamp: base + 100.0,
            });
            events.push(LabeledEvent {
                label: "calendar".to_string(),
                timestamp: base + 600.0,
            });
            events.push(LabeledEvent {
                label: "standup".to_string(),
                timestamp: base + 1200.0,
            });
        }

        let motifs = mine_temporal_motifs(&events, &config);
        assert!(!motifs.is_empty());
        // Should find the email→calendar pattern
        let has_email_cal = motifs.iter().any(|m| {
            m.sequence.len() >= 2 && m.sequence[0] == "email" && m.sequence[1] == "calendar"
        });
        assert!(
            has_email_cal,
            "Should find email→calendar motif: {:?}",
            motifs
        );
    }

    #[test]
    fn test_motif_gap_constraint() {
        let config = MotifConfig {
            max_gap_secs: 100.0,
            min_length: 2,
            max_length: 3,
            min_occurrences: 2,
        };

        // Events too far apart to form motifs
        let events: Vec<LabeledEvent> = (0..6)
            .map(|i| {
                LabeledEvent {
                    label: if i % 2 == 0 {
                        "a".to_string()
                    } else {
                        "b".to_string()
                    },
                    timestamp: i as f64 * 1000.0, // 1000s gaps > 100s max
                }
            })
            .collect();

        let motifs = mine_temporal_motifs(&events, &config);
        assert!(
            motifs.is_empty(),
            "Large gaps should prevent motif detection"
        );
    }

    // ── Topological Order ──

    #[test]
    fn test_topological_order_simple() {
        use super::super::state::{NodeId, NodeKind};

        let a = NodeId::new(NodeKind::Episode, 1);
        let b = NodeId::new(NodeKind::Episode, 2);
        let c = NodeId::new(NodeKind::Episode, 3);

        let events = vec![
            TemporalEvent {
                node_id: a,
                timestamp: 100.0,
                predecessors: vec![],
            },
            TemporalEvent {
                node_id: b,
                timestamp: 200.0,
                predecessors: vec![a],
            },
            TemporalEvent {
                node_id: c,
                timestamp: 300.0,
                predecessors: vec![b],
            },
        ];

        let order = topological_order(&events);
        assert_eq!(order.ordered.len(), 3);
        assert_eq!(order.ordered[0], a);
        assert_eq!(order.ordered[1], b);
        assert_eq!(order.ordered[2], c);
        assert!(order.cycles.is_empty());
    }

    #[test]
    fn test_topological_order_with_cycle() {
        use super::super::state::{NodeId, NodeKind};

        let a = NodeId::new(NodeKind::Episode, 1);
        let b = NodeId::new(NodeKind::Episode, 2);

        let events = vec![
            TemporalEvent {
                node_id: a,
                timestamp: 100.0,
                predecessors: vec![b],
            },
            TemporalEvent {
                node_id: b,
                timestamp: 200.0,
                predecessors: vec![a],
            },
        ];

        let order = topological_order(&events);
        // Both nodes should be in cycles since they mutually depend
        assert!(!order.cycles.is_empty());
    }

    #[test]
    fn test_topological_depth() {
        use super::super::state::{NodeId, NodeKind};

        let a = NodeId::new(NodeKind::Episode, 1);
        let b = NodeId::new(NodeKind::Episode, 2);
        let c = NodeId::new(NodeKind::Episode, 3);

        let events = vec![
            TemporalEvent {
                node_id: a,
                timestamp: 100.0,
                predecessors: vec![],
            },
            TemporalEvent {
                node_id: b,
                timestamp: 200.0,
                predecessors: vec![a],
            },
            TemporalEvent {
                node_id: c,
                timestamp: 300.0,
                predecessors: vec![a, b],
            },
        ];

        let order = topological_order(&events);
        assert_eq!(*order.depths.get(&b.to_raw()).unwrap_or(&0), 1);
        assert_eq!(*order.depths.get(&c.to_raw()).unwrap_or(&0), 2);
    }

    // ── Seasonal Histogram ──

    #[test]
    fn test_seasonal_histogram_peak() {
        let mut hist = SeasonalHistogram::hour_of_day();
        // Most events at 9am
        for _ in 0..20 {
            hist.add(9);
        }
        for _ in 0..5 {
            hist.add(14);
        }
        for _ in 0..3 {
            hist.add(22);
        }

        let (peak_bin, peak_count) = hist.peak();
        assert_eq!(peak_bin, 9);
        assert_eq!(peak_count, 20);
    }

    #[test]
    fn test_seasonal_entropy() {
        // Uniform distribution → maximum entropy
        let mut uniform = SeasonalHistogram::new(4, "test");
        for _ in 0..25 {
            uniform.add(0);
        }
        for _ in 0..25 {
            uniform.add(1);
        }
        for _ in 0..25 {
            uniform.add(2);
        }
        for _ in 0..25 {
            uniform.add(3);
        }

        // Concentrated → low entropy
        let mut concentrated = SeasonalHistogram::new(4, "test");
        for _ in 0..100 {
            concentrated.add(0);
        }

        assert!(uniform.entropy() > concentrated.entropy());
    }

    #[test]
    fn test_concentration_ratio() {
        let mut hist = SeasonalHistogram::new(10, "test");
        for _ in 0..50 {
            hist.add(0);
        }
        for _ in 0..30 {
            hist.add(1);
        }
        for _ in 0..20 {
            hist.add(2);
        }

        let c2 = hist.concentration(2);
        assert!((c2 - 0.80).abs() < 1e-10); // Top 2 bins = 80%
    }

    // ── Composite Relevance ──

    #[test]
    fn test_composite_all_recent() {
        let config = TemporalRelevanceConfig::default();
        let now = 1000.0;
        let score = temporal_relevance_composite(now, now, None, None, &config);
        // Only recency contributes (1.0), periodicity and deadline are 0
        // score = (0.4 * 1.0 + 0.3 * 0.0 + 0.3 * 0.0) / 1.0 = 0.4
        assert!((score - 0.4).abs() < 0.05);
    }

    #[test]
    fn test_composite_with_deadline() {
        let config = TemporalRelevanceConfig::default();
        let now = 1000.0;
        let deadline = 1001.0; // 1 second away
        let score = temporal_relevance_composite(now, now, None, Some(deadline), &config);
        // Recency = 1.0 (just now), deadline urgency should be high
        assert!(score > 0.5);
    }

    #[test]
    fn test_day_of_week_utc() {
        // 2024-01-01 (Monday) = 1704067200
        let monday = 1704067200.0;
        assert_eq!(day_of_week_utc(monday), 0); // Monday = 0
    }

    #[test]
    fn test_hour_of_day_utc() {
        // Midnight UTC = hour 0
        let midnight = 86400.0 * 100.0; // Some midnight
        assert_eq!(hour_of_day_utc(midnight), 0);
        // 9am UTC
        let nine_am = midnight + 9.0 * 3600.0;
        assert_eq!(hour_of_day_utc(nine_am), 9);
    }
}