trane 0.28.0

An automated system for learning complex skills
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
//! Scoring primitives for exercise mastery estimation.
//!
//! This module defines the scorer interface and implementations used to derive a normalized mastery
//! score from historical `ExerciseTrial` data. Implementations are deterministic and stateless:
//! scoring is a pure function of the supplied review history and exercise type.
//!
//! The returned score (0.0 to 5.0) is a compact signal used by the scheduler to select and filter
//! exercises rather than a direct review schedule.

use anyhow::{Result, anyhow};
use chrono::Utc;

use crate::data::{ExerciseTrial, ExerciseType};

/// A trait exposing a function to score an exercise based on the results of previous trials.
pub trait ExerciseScorer {
    /// Returns a score (between 0.0 and 5.0) for the exercise based on the results and timestamps
    /// of previous trials. The trials are assumed to be sorted in descending order by timestamp.
    fn score(&self, exercise_type: ExerciseType, previous_trials: &[ExerciseTrial]) -> Result<f32>;

    /// Returns the velocity of learning for exercise with the given trials. The velocity is a
    /// measure of how quickly the score is improving or worsening over trials. A value of None
    /// indicates that there are too few trials to compute a reliable velocity.
    fn velocity(&self, previous_trials: &[ExerciseTrial]) -> Option<f32>;
}

/// The target retrievability at `t = stability` used to calibrate the forgetting-curve factor.
///
/// For each decay exponent `d`, the factor is derived so that:
/// `R(t = S, S) = TARGET_RETRIEVABILITY_AT_STABILITY`.
const TARGET_RETRIEVABILITY_AT_STABILITY: f32 = 0.9;

/// The decay exponent used in the power-law forgetting curve for declarative exercises (e.g. memory
/// recall). The value is taken from the FSRS-4.5 implementation.
const DECLARATIVE_CURVE_DECAY: f32 = -0.5;

/// The decay exponent used in the power-law forgetting curve for procedural exercises (e.g. playing
/// a piece of music). The value is higher than for declarative exercises, reflecting the slower
/// decay of procedural memory.
const PROCEDURAL_CURVE_DECAY: f32 = -0.3;

/// The minimum stability value in days. This prevents division by zero and ensures that exercises
/// with very few trials still have a reasonable stability estimate.
const MIN_STABILITY: f32 = 0.5;

/// The maximum stability value in days. Trane is designed for the long-life learning of acquiring
/// mastery, so a high stability ceiling of two years allows it to model this case.
const MAX_STABILITY: f32 = 730.0;

/// The default stability for exercises with no review history.
const DEFAULT_STABILITY: f32 = 1.0;

/// The minimum difficulty value. This represents the easiest exercises.
const MIN_DIFFICULTY: f32 = 1.0;

/// The maximum difficulty value. This represents the hardest exercises.
const MAX_DIFFICULTY: f32 = 10.0;

/// The base difficulty value. This represents the default difficulty for exercises with no review
/// history.
const BASE_DIFFICULTY: f32 = 5.0;

/// The divisor used to scale the difficulty effect on the forgetting curve. A higher value means
/// difficulty has less impact on the decay rate. With a value of 30, the maximum difficulty (10.0)
/// increases the effective decay rate by approximately 30%.
const DIFFICULTY_FACTOR: f32 = 30.0;

/// The per-trial difficulty adjustment scale. Good grades reduce difficulty, poor grades increase
/// it.
const DIFFICULTY_GRADE_ADJUSTMENT_SCALE: f32 = 0.6;

/// How much the dynamic difficulty is pulled back toward the base estimate after each review.
const DIFFICULTY_REVERSION_WEIGHT: f32 = 0.1;

/// The baseline score used to calculate the performance factor. Scores above this baseline improve
/// stability and difficulty estimates.
const PERFORMANCE_BASELINE_SCORE: f32 = 3.0;

/// A scaling coefficient applied to the stability update term for each review. The per-review
/// multiplicative change is `1 + STABILITY_COEFFICIENT * P * E * spacing_gain * S^(-k)`. The
/// resulting stability is clamped to `MIN_STABILITY..MAX_STABILITY`.
const STABILITY_COEFFICIENT: f32 = 2.1;

/// The minimum grade value used in performance calculations. Corresponds to complete failure. This
/// is the same as the minimum mastery score, just with a different name to shorten formulas.
const GRADE_MIN: f32 = 1.0;

/// The maximum grade value used in performance calculations. Corresponds to perfect performance.
/// This is also the same as the maximum mastery score.
const GRADE_MAX: f32 = 5.0;

/// The offset used in difficulty linear mapping. Represents the minimum difficulty.
const DIFFICULTY_OFFSET: f32 = 1.0;

/// The scale factor used in difficulty linear mapping. Determines the range of difficulty values.
const DIFFICULTY_SCALE: f32 = 9.0;

/// The number of seconds in a day, used for timestamp conversions.
const SECONDS_PER_DAY: f32 = 86400.0;

/// The per-day decay factor for exponential weighting of performance. Latest score weight 1.0,
/// scores one day old are multiplied by it, two days old by its square and so on.
const PERFORMANCE_WEIGHT_DECAY: f32 = 0.98;

/// Coefficient for the confidence penalty. Exercises with higher variance and lower number of
/// trials receive a larger penalty to reflect higher uncertainty in the score estimate.
const CONFIDENCE_PENALTY_COEFFICIENT: f32 = 0.2;

/// The minimum per-trial performance weight, ensuring very old trials never disappear entirely.
const PERFORMANCE_WEIGHT_MIN: f32 = 0.1;

/// The range of grade values used in performance calculations.
const GRADE_RANGE: f32 = GRADE_MAX - GRADE_MIN;

/// The numerator offset used in the ease factor calculation.
const EASE_NUMERATOR_OFFSET: f32 = 11.0;

/// The denominator used in the ease factor calculation.
const EASE_DENOMINATOR: f32 = 5.0;

/// The weight of the interval-aware spacing effect during successful reviews. Larger values
/// increase stability growth when pre-review retrievability is low.
const SPACING_EFFECT_WEIGHT: f32 = 0.7;

/// The exponent applied to stability when computing diminishing returns for repeated successful
/// reviews. Larger values increase saturation strength at high stability.
const STABILITY_DAMPING_EXP: f32 = 0.1;

/// The minimum stability-loss fraction for a lapse.
const MIN_LAPSE_DROP: f32 = 0.0;

/// The maximum stability-loss fraction for a lapse.
const MAX_LAPSE_DROP: f32 = 0.85;

/// The baseline stability-loss fraction for a lapse.
const LAPSE_BASE_DROP: f32 = 0.30;

/// The influence of difficulty on lapse penalties.
const LAPSE_DIFFICULTY_WEIGHT: f32 = 0.25;

/// The influence of how surprising the lapse was (higher pre-review retrievability means larger
/// penalties).
const LAPSE_RETRIEVABILITY_WEIGHT: f32 = 0.30;

/// The minimum weighted score required to apply the old-good retrievability floor. This floor is
/// applied to exercises with strong historical performance to prevent them from dropping too low
/// after long gaps in practice. In such cases, it is better to allow students to see old exercises
/// and have them fail than to have them stuck with review of very old exercises.
const OLD_GOOD_MIN_SCORE: f32 = 4.0;

/// The minimum number of scores required to apply the old-good retrievability floor.
const OLD_GOOD_MIN_SCORES: usize = 2;

/// The minimum number of elapsed days required to apply the old-good retrievability floor.
const OLD_GOOD_MIN_AGE: f32 = 50.0;

/// The minimum retrievability used for old exercises with strong historical performance.
const OLD_GOOD_FLOOR: f32 = 0.75;

/// A scorer that uses a power-law forgetting curve to compute the score of an exercise, using
/// review-history-based estimation of stability and difficulty. This models memory retention more
/// accurately than exponential decay by accounting for the "fat tail" of long-term memory.
///
/// This implementation is inspired by FSRS (Free Spaced Repetition Scheduler) but simplified for
/// Trane's stateless architecture. Instead of maintaining separate state for each exercise, it
/// chains stability updates through the review history chronologically (oldest to newest).
/// Stability evolves with each review using:
///
/// S' = S × (1 + STABILITY_COEFFICIENT × P × E × spacing_gain × S^(-k)),
///
/// where P is performance factor, E is ease factor, and spacing_gain increases successful growth
/// after longer review intervals. Final score multiplies difficulty-adjusted retrievability by the
/// recency-weighted performance score, then clamps the result to 0-5.
///
/// Algorithm:
///
/// 1. Estimate a base difficulty from review failure rates.
/// 2. Chain stability through reviews chronologically (oldest to newest), updating difficulty after
///    each review based on outcome.
/// 3. Apply interval-aware spacing during stability updates (successful recalls after longer
///    intervals boost stability more).
/// 4. Damp stability gains for already-stable memories to model explicit saturation.
/// 5. Compute retrievability from last review to now using power-law decay.
/// 6. Adjust retrievability by difficulty for harder exercises.
/// 7. Apply performance factor from an exponentially weighted average of all reviews (recent
///    performance matters most).
/// 8. Scale to final 0-5 score.
///
/// A simplified implementation without additional stored parameters is preferred for Trane because:
///
/// - Spaced repetition is just one of many strategies used by the expert system.
/// - The main concept behind trane is to map content to a graph of dependencies, not to have a flat
///   list of exercises. Trane has more information than just previous trials.
/// - The score of an exercise is a score meant to reflect mastery of an exercise, not just memory.
/// - The final output is an optimized batch of exercises, not just a list of exercises due for
///   review.
pub struct PowerLawScorer {}

impl PowerLawScorer {
    /// Estimates the difficulty of the exercise based on failure rates. Difficulty ranges from 1.0
    /// (easiest) to 10.0 (hardest).
    fn estimate_difficulty(previous_trials: &[ExerciseTrial]) -> f32 {
        // Assign the base difficulty to exercises with no history.
        if previous_trials.is_empty() {
            return BASE_DIFFICULTY;
        }

        // Count scores below the baseline as failures.
        let failures = previous_trials
            .iter()
            .filter(|t| t.score < PERFORMANCE_BASELINE_SCORE)
            .count() as f32;
        let failure_rate = failures / previous_trials.len() as f32;

        // Linearly map aggregate failure rate (0.0-1.0) to difficulty (1.0-10.0).
        // - 0% failures -> difficulty 1 (easy)
        // - 50% failures -> difficulty 5.5 (medium)
        // - 100% failures -> difficulty 10 (hard)
        let difficulty = DIFFICULTY_OFFSET + failure_rate * DIFFICULTY_SCALE;
        difficulty.clamp(MIN_DIFFICULTY, MAX_DIFFICULTY)
    }

    /// Computes the time-decayed weighted average performance and weighted variance from all
    /// trials.
    ///
    /// Weights decay by elapsed days from the most recent trial so irregular practice cadence is
    /// modeled more accurately. Returns `(weighted_mean, weighted_variance)`, or `(0.0, 0.0)` for
    /// empty trials.
    fn compute_weighted_avg_and_variance(previous_trials: &[ExerciseTrial]) -> (f32, f32) {
        if previous_trials.is_empty() {
            return (0.0, 0.0);
        }

        // Start from the latest timestamp and compute the weights and squared weights of the rest
        // based on the number of days from it.
        let newest_timestamp = previous_trials[0].timestamp;
        let mut sum_weighted = 0.0;
        let mut sum_weighted_sq = 0.0;
        let mut sum_weights = 0.0;
        for trial in previous_trials {
            let elapsed_days = ((newest_timestamp.saturating_sub(trial.timestamp)) as f32
                / SECONDS_PER_DAY)
                .max(0.0);
            let weight = PERFORMANCE_WEIGHT_DECAY
                .powf(elapsed_days)
                .max(PERFORMANCE_WEIGHT_MIN);
            sum_weighted += weight * trial.score;
            sum_weighted_sq += weight * trial.score * trial.score;
            sum_weights += weight;
        }

        // Compute the mean and variance. No need to check for division by zero because the minimum
        // score of a trial is 1 and the minimum weight is capped.
        let mean = sum_weighted / sum_weights;
        let variance = sum_weighted_sq / sum_weights - mean * mean;
        (mean, variance)
    }

    /// Returns the forgetting-curve decay exponent for the given exercise type.
    fn get_curve_decay(exercise_type: &ExerciseType) -> f32 {
        match exercise_type {
            ExerciseType::Declarative => DECLARATIVE_CURVE_DECAY,
            ExerciseType::Procedural => PROCEDURAL_CURVE_DECAY,
        }
    }

    /// Returns the forgetting-curve factor derived from the decay exponent for this exercise type.
    ///
    /// Using `R(t, S) = (1 + factor * t / S)^(-decay_abs)`, this computes:
    /// `factor = TARGET_RETRIEVABILITY_AT_STABILITY^(-1 / decay_abs) - 1`.
    ///
    /// This calibration ensures `R(t = S, S) = TARGET_RETRIEVABILITY_AT_STABILITY` for each
    /// exercise type while still preserving shape differences through type-specific decay exponents.
    fn get_curve_factor(exercise_type: &ExerciseType) -> f32 {
        let decay_abs = Self::get_curve_decay(exercise_type).abs().max(f32::EPSILON);
        TARGET_RETRIEVABILITY_AT_STABILITY.powf(-1.0 / decay_abs) - 1.0
    }

    /// Computes pre-review retrievability used by the interval-aware spacing effect. It uses the
    /// same decay exponent as final retrievability for the given exercise type.
    fn compute_spacing_retrievability(
        exercise_type: &ExerciseType,
        days_since_previous_review: f32,
        stability: f32,
    ) -> f32 {
        let decay = Self::get_curve_decay(exercise_type);
        let factor = Self::get_curve_factor(exercise_type);
        (1.0 + factor * days_since_previous_review / stability)
            .powf(decay)
            .clamp(0.0, 1.0)
    }

    /// Computes the spacing gain multiplier for a review. Successful recalls (`performance_factor >
    /// 0`) receive additional growth after longer intervals. Non-successful reviews return a
    /// neutral multiplier so lapse handling is handled separately.
    fn compute_spacing_gain(
        exercise_type: &ExerciseType,
        days_since_previous_review: f32,
        stability: f32,
        performance_factor: f32,
    ) -> f32 {
        if performance_factor <= 0.0 {
            return 1.0;
        }

        let pre_review_retrievability = Self::compute_spacing_retrievability(
            exercise_type,
            days_since_previous_review,
            stability,
        );
        (1.0 + SPACING_EFFECT_WEIGHT * (1.0 - pre_review_retrievability))
            .clamp(1.0, 1.0 + SPACING_EFFECT_WEIGHT)
    }

    /// Returns whether this trial is considered a lapse for state updates.
    fn is_lapse(trial_score: f32) -> bool {
        trial_score < PERFORMANCE_BASELINE_SCORE
    }

    /// Updates difficulty after a review using dynamic trend and mean reversion.
    ///
    /// Good grades reduce difficulty, poor grades increase it. The result is then pulled back
    /// toward the base estimate to prevent drift.
    fn update_difficulty(difficulty: f32, base_difficulty: f32, trial_score: f32) -> f32 {
        let grade_delta = (PERFORMANCE_BASELINE_SCORE - trial_score) / GRADE_RANGE
            * DIFFICULTY_GRADE_ADJUSTMENT_SCALE;
        let adjusted_difficulty = (difficulty + grade_delta).clamp(MIN_DIFFICULTY, MAX_DIFFICULTY);

        (DIFFICULTY_REVERSION_WEIGHT * base_difficulty
            + (1.0 - DIFFICULTY_REVERSION_WEIGHT) * adjusted_difficulty)
            .clamp(MIN_DIFFICULTY, MAX_DIFFICULTY)
    }

    /// Computes how much stability should be reduced on a lapse.
    ///
    /// Returns a fractional reduction in current stability. Higher difficulty and more surprising
    /// lapses produce larger reductions.
    fn compute_lapse_drop(difficulty: f32, pre_review_retrievability: f32) -> f32 {
        let difficulty_adjust =
            ((difficulty - MIN_DIFFICULTY) / (MAX_DIFFICULTY - MIN_DIFFICULTY)).clamp(0.0, 1.0);
        let pre_review_retrievability = pre_review_retrievability.clamp(0.0, 1.0);
        (LAPSE_BASE_DROP
            + LAPSE_DIFFICULTY_WEIGHT * difficulty_adjust
            + LAPSE_RETRIEVABILITY_WEIGHT * pre_review_retrievability)
            .clamp(MIN_LAPSE_DROP, MAX_LAPSE_DROP)
    }

    /// Applies a single review result to the current stability estimate. Penalties for lapses are
    /// not applied for the first trial.
    fn apply_stability_transition(
        exercise_type: &ExerciseType,
        stability: f32,
        difficulty: f32,
        score: f32,
        days_since_previous_review: f32,
        is_first_review: bool,
    ) -> f32 {
        // Convert score to performance and ease, and estimate pre-review retention.
        let p = (score - GRADE_MIN) / GRADE_RANGE - 0.5;
        let e = (EASE_NUMERATOR_OFFSET - difficulty) / EASE_DENOMINATOR;
        let pre_review_retrievability = Self::compute_spacing_retrievability(
            exercise_type,
            days_since_previous_review,
            stability,
        );

        // Adjust stability based on review outcome.
        if Self::is_lapse(score) && !is_first_review {
            // Penalize hard misses proportionally to difficulty and surprise.
            let lapse_drop = Self::compute_lapse_drop(difficulty, pre_review_retrievability);
            (stability * (1.0 - lapse_drop)).clamp(MIN_STABILITY, MAX_STABILITY)
        } else {
            // Boost stability on successful recall with spacing- and saturation-aware gain.
            let spacing_gain =
                Self::compute_spacing_gain(exercise_type, days_since_previous_review, stability, p);
            let stability_damping = Self::compute_stability_damping(stability);
            let growth_term = STABILITY_COEFFICIENT * p * e * spacing_gain * stability_damping;
            (stability * (1.0 + growth_term)).clamp(MIN_STABILITY, MAX_STABILITY)
        }
    }

    /// Starts with DEFAULT_STABILITY and evolves through reviews from oldest to newest, while
    /// updating dynamic difficulty after each trial with mean reversion.
    ///
    /// For each review:
    /// - Compute elapsed days since the previous review in the chain.
    /// - Estimate pre-review retrievability from elapsed time and current stability.
    /// - Use the same type-specific forgetting curve decay as the final retrievability.
    /// - Apply an interval-aware spacing gain to successful reviews.
    /// - Apply a separate lapse reduction for recalls below the baseline threshold.
    /// - Update stability via the success branch: `S' = S × (1 + STABILITY_COEFFICIENT × P × E ×
    ///   spacing_gain × S^(-k))`.
    /// - Or the lapse branch: `S' = S * (1 - lapse_drop)`, where `lapse_drop` grows with difficulty
    ///   and surprise.
    /// - Update difficulty to the new post-review state and continue to the next trial.
    ///
    /// Here P = (grade - GRADE_MIN) / GRADE_RANGE - 0.5 (performance, from -0.5 for fail to 0.5 for
    /// perfect), and E = (EASE_NUMERATOR_OFFSET - difficulty) / EASE_DENOMINATOR (ease).
    fn compute_stability_and_difficulty(
        exercise_type: &ExerciseType,
        previous_trials: &[ExerciseTrial],
        base_difficulty: f32,
    ) -> (f32, f32) {
        // Seed state for chain replay from the oldest known review.
        let mut stability = DEFAULT_STABILITY;
        let mut difficulty = base_difficulty;
        let mut previous_timestamp = None;

        // Replay each review chronologically so each result sees the updated state.
        for trial in previous_trials.iter().rev() {
            // Compute interval and review-derived signals from the current state.
            let days_since_previous_review = previous_timestamp.map_or(0.0, |timestamp| {
                ((trial.timestamp.saturating_sub(timestamp)) as f32 / SECONDS_PER_DAY).max(0.0)
            });

            stability = Self::apply_stability_transition(
                exercise_type,
                stability,
                difficulty,
                trial.score,
                days_since_previous_review,
                previous_timestamp.is_none(),
            );

            // Update the difficulty state for the next review in the chain.
            difficulty = Self::update_difficulty(difficulty, BASE_DIFFICULTY, trial.score);
            previous_timestamp = Some(trial.timestamp);
        }
        (stability, difficulty)
    }

    /// Returns a growth modifier that controls how quickly stability increases after successes.
    ///
    /// Lower stability gets a stronger boost so weak memories can recover faster, while higher
    /// stability gets a smaller boost so already-strong memories do not keep accelerating.
    fn compute_stability_damping(stability: f32) -> f32 {
        stability.max(MIN_STABILITY).powf(-STABILITY_DAMPING_EXP)
    }

    /// Computes retrievability using power-law forgetting: `R = (1 + factor × t/S)^decay`.
    /// Returns a 0-1 probability of recall.
    ///
    /// The factor is derived from each type's decay exponent so that `R(t = S, S) = 0.9` for both
    /// declarative and procedural exercises. This keeps stability interpretation aligned across
    /// exercise types while retaining type-specific curve shapes.
    fn compute_retrievability(
        exercise_type: &ExerciseType,
        days_since_last: f32,
        stability: f32,
    ) -> f32 {
        let decay = Self::get_curve_decay(exercise_type);
        let factor = Self::get_curve_factor(exercise_type);
        (1.0 + factor * days_since_last / stability).powf(decay)
    }

    /// Applies a retrievability floor for old exercises with strong weighted performance. It is
    /// preferable to show old exercises and have the student fail than to have students stuck with
    /// review of very old exercises after long gaps in their practice.
    fn apply_old_good_retrievability_floor(
        adjusted_retrievability: f32,
        weighted_score: f32,
        days_since_last: f32,
        num_scores: usize,
    ) -> f32 {
        if num_scores >= OLD_GOOD_MIN_SCORES
            && weighted_score >= OLD_GOOD_MIN_SCORE
            && days_since_last >= OLD_GOOD_MIN_AGE
        {
            adjusted_retrievability.max(OLD_GOOD_FLOOR)
        } else {
            adjusted_retrievability
        }
    }
}

impl ExerciseScorer for PowerLawScorer {
    fn score(&self, exercise_type: ExerciseType, previous_trials: &[ExerciseTrial]) -> Result<f32> {
        // Guard input ordering and missing-history edge cases.
        if previous_trials.is_empty() {
            return Ok(0.0);
        }
        if previous_trials
            .windows(2)
            .any(|w| w[0].timestamp < w[1].timestamp)
        {
            return Err(anyhow!(
                "Exercise trials not sorted in descending order by timestamp"
            ));
        }

        // Reconstruct stability and dynamic difficulty from the trial sequence.
        let base_difficulty = Self::estimate_difficulty(previous_trials);
        let (stability, final_difficulty) = Self::compute_stability_and_difficulty(
            &exercise_type,
            previous_trials,
            base_difficulty,
        );

        // Project recall probability from the most recent review to now.
        let days_since_last = ((Utc::now()
            .timestamp()
            .saturating_sub(previous_trials[0].timestamp)) as f32
            / SECONDS_PER_DAY)
            .max(0.0);
        let retrievability =
            Self::compute_retrievability(&exercise_type, days_since_last, stability);

        // The difficulty exponent adjusts retrievability based on exercise hardness. Harder
        // exercises (higher difficulty) have lower retrievability for the same stability due to
        // increased decay. The formula is exponent = MIN_DIFFICULTY + (difficulty - MIN_DIFFICULTY)
        // / DIFFICULTY_FACTOR.
        let difficulty_exponent =
            MIN_DIFFICULTY + (final_difficulty - MIN_DIFFICULTY) / DIFFICULTY_FACTOR;
        let adjusted_retrievability = retrievability.powf(difficulty_exponent);

        // Compute the weighted score and variance and apply the old-good retrievability floor.
        let (weighted_score, weighted_variance) =
            Self::compute_weighted_avg_and_variance(previous_trials);
        let effective_retrievability = Self::apply_old_good_retrievability_floor(
            adjusted_retrievability,
            weighted_score,
            days_since_last,
            previous_trials.len(),
        );
        let raw_score = effective_retrievability * weighted_score;

        // Apply the confidence penalty and clamp the final score to the [0.0, 5.0] range.
        let confidence_penalty = CONFIDENCE_PENALTY_COEFFICIENT * weighted_variance
            / (previous_trials.len() as f32).sqrt();
        Ok((raw_score - confidence_penalty).clamp(0.0, 5.0))
    }

    fn velocity(&self, previous_trials: &[ExerciseTrial]) -> Option<f32> {
        // Need at least 2 trials for a meaningful slope.
        if previous_trials.len() < 2 {
            return None;
        }

        // Compute the velocity using the ordinary least squares regression method. The oldest trial
        // is used as the reference point and other trials are converted to days from it.
        let oldest_timestamp = previous_trials.last().unwrap().timestamp;
        let n = previous_trials.len() as f32;
        let mut sum_t = 0.0_f32;
        let mut sum_scores = 0.0_f32;
        let mut sum_t_scores = 0.0_f32;
        let mut sum_t_sq = 0.0_f32;
        for trial in previous_trials {
            let t = (trial.timestamp.saturating_sub(oldest_timestamp)) as f32 / SECONDS_PER_DAY;
            sum_t += t;
            sum_scores += trial.score;
            sum_t_scores += t * trial.score;
            sum_t_sq += t * t;
        }
        let denominator = n * sum_t_sq - sum_t * sum_t;
        if denominator.abs() < f32::EPSILON {
            return Some(0.0);
        }
        let slope = (n * sum_t_scores - sum_t * sum_scores) / denominator;
        Some(slope)
    }
}

#[cfg(test)]
#[cfg_attr(coverage, coverage(off))]
mod test {
    use chrono::Utc;

    use crate::{data::ExerciseTrial, exercise_scorer::*};

    const SCORER: PowerLawScorer = PowerLawScorer {};

    /// Generates a timestamp equal to the timestamp from `num_days` ago.
    fn generate_timestamp(num_days: i64) -> i64 {
        let now = Utc::now().timestamp();
        now - num_days * SECONDS_PER_DAY as i64
    }

    /// Verifies that difficulty is estimated correctly from failure rates.
    #[test]
    fn estimate_difficulty() {
        // Empty trials should return neutral difficulty.
        assert_eq!(PowerLawScorer::estimate_difficulty(&[]), BASE_DIFFICULTY);

        // All successes should yield low difficulty.
        let easy_trials = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(2),
            },
        ];
        let easy_difficulty = PowerLawScorer::estimate_difficulty(&easy_trials);
        assert!(easy_difficulty < 3.0);

        // All failures should yield high difficulty.
        let hard_trials = vec![
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 2.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(2),
            },
        ];
        let hard_difficulty = PowerLawScorer::estimate_difficulty(&hard_trials);
        assert!(hard_difficulty > 8.0);

        // Mixed results should yield medium difficulty.
        let medium_trials = vec![
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 2.0,
                timestamp: generate_timestamp(2),
            },
        ];
        let medium_difficulty = PowerLawScorer::estimate_difficulty(&medium_trials);
        assert!(medium_difficulty >= 4.0 && medium_difficulty < 7.0);

        // A mixed history should yield an intermediate difficulty from aggregate failures.
        let mixed_trials = vec![
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 2.0,
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(3),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(4),
            },
        ];
        let mixed_difficulty = PowerLawScorer::estimate_difficulty(&mixed_trials);
        assert!(mixed_difficulty > 4.0 && mixed_difficulty < 6.0);
    }

    /// Verifies the score for an exercise with no previous trials is 0.0.
    #[test]
    fn no_previous_trials() {
        assert_eq!(0.0, SCORER.score(ExerciseType::Declarative, &[]).unwrap());
    }

    /// Verifies running the full scoring algorithm on a set of trials produces a reasonable score.
    #[test]
    fn score_trials() {
        let trials = vec![
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(3),
            },
        ];

        let score = SCORER.score(ExerciseType::Declarative, &trials).unwrap();
        assert!(score > 0.0 && score <= 5.0);
        assert!(score > 2.0); // Decent due to good recent performance
    }

    /// Verifies scoring an exercise with an invalid timestamp still returns a sane score.
    #[test]
    fn invalid_timestamp() -> Result<()> {
        let score = SCORER.score(
            ExerciseType::Declarative,
            &[ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(1e10 as i64),
            }],
        )?;
        assert!(score >= 0.0 && score <= 5.0);
        assert!(score < 1.0); // Low due to long time elapsed
        Ok(())
    }

    /// Verifies extreme timestamp gaps do not overflow elapsed-time calculations.
    #[test]
    fn extreme_timestamp_gap_does_not_overflow() -> Result<()> {
        let score = SCORER.score(
            ExerciseType::Declarative,
            &[
                ExerciseTrial {
                    score: 5.0,
                    timestamp: i64::MAX,
                },
                ExerciseTrial {
                    score: 1.0,
                    timestamp: i64::MIN,
                },
            ],
        )?;
        assert!(score >= 0.0 && score <= 5.0);
        Ok(())
    }

    /// Verifies stability computation evolves correctly through reviews.
    #[test]
    fn compute_stability() {
        let difficulty = BASE_DIFFICULTY;
        let trials = vec![
            ExerciseTrial {
                score: 1.0, // Bad: P = -0.5, stability decreases
                timestamp: generate_timestamp(3),
            },
            ExerciseTrial {
                score: 5.0, // Good: P = 0.5, stability increases
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 3.0, // Medium: P = 0.0, stability unchanged
                timestamp: generate_timestamp(1),
            },
        ];
        let stability = PowerLawScorer::compute_stability_and_difficulty(
            &ExerciseType::Declarative,
            &trials,
            difficulty,
        )
        .0;
        assert!(stability > 0.0 && stability < 2.0); // Reasonable range
    }

    /// Verifies longer spacing between successful reviews yields higher stability.
    #[test]
    fn compute_stability_spacing_effect() {
        let difficulty = BASE_DIFFICULTY;
        let short_spacing_trials = vec![
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(3),
            },
        ];
        let long_spacing_trials = vec![
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(10),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(30),
            },
        ];

        let short_spacing_stability = PowerLawScorer::compute_stability_and_difficulty(
            &ExerciseType::Declarative,
            &short_spacing_trials,
            difficulty,
        )
        .0;
        let long_spacing_stability = PowerLawScorer::compute_stability_and_difficulty(
            &ExerciseType::Declarative,
            &long_spacing_trials,
            difficulty,
        )
        .0;
        assert!(long_spacing_stability > short_spacing_stability);
    }

    /// Verifies lapses reduce stability more than a baseline success.
    #[test]
    fn stability_lapse_reduces_more_than_hard_success() {
        // Two-trial sequences: the first trial seeds stability identically, the second exercises
        // the lapse penalty path vs. the success path.
        let difficulty = BASE_DIFFICULTY;
        let success_trials = vec![
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(1),
            },
        ];
        let lapse_trials = vec![
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(1),
            },
        ];

        let success_stability = PowerLawScorer::compute_stability_and_difficulty(
            &ExerciseType::Declarative,
            &success_trials,
            difficulty,
        )
        .0;
        let lapse_stability = PowerLawScorer::compute_stability_and_difficulty(
            &ExerciseType::Declarative,
            &lapse_trials,
            difficulty,
        )
        .0;

        assert!(success_stability > MIN_STABILITY);
        assert!(lapse_stability < success_stability);
        assert!(lapse_stability >= MIN_STABILITY);
    }

    /// Verifies that stronger damping reduces growth at high stability for the same review quality.
    #[test]
    fn stability_growth_saturates_at_high_s() {
        // Compare the same successful-review profile at low and high starting stability.
        let difficulty = BASE_DIFFICULTY;
        let exercise_type = ExerciseType::Declarative;
        let p = (5.0 - GRADE_MIN) / GRADE_RANGE - 0.5;
        let e = (EASE_NUMERATOR_OFFSET - difficulty) / EASE_DENOMINATOR;
        let spacing_gain =
            PowerLawScorer::compute_spacing_gain(&exercise_type, 0.0, MIN_STABILITY, p);
        let base_growth_term = STABILITY_COEFFICIENT * p * e * spacing_gain;

        let low_stability = MIN_STABILITY;
        let high_stability = 50.0;
        let low_stability_damping = PowerLawScorer::compute_stability_damping(low_stability);
        let high_stability_damping = PowerLawScorer::compute_stability_damping(high_stability);

        let low_effective_growth = base_growth_term * low_stability_damping;
        let high_effective_growth = base_growth_term * high_stability_damping;
        assert!(low_effective_growth > high_effective_growth);
    }

    /// Verifies repeated lapses remain bounded by minimum stability.
    #[test]
    fn multiple_lapses_bounded() {
        let difficulty = BASE_DIFFICULTY;
        let lapses = vec![
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(3),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(1),
            },
        ];

        let stability = PowerLawScorer::compute_stability_and_difficulty(
            &ExerciseType::Declarative,
            &lapses,
            difficulty,
        )
        .0;
        assert!(stability >= MIN_STABILITY);
        assert!(stability < DEFAULT_STABILITY);
    }

    /// Verifies long-run saturated success updates do not explode beyond expected bounds.
    #[test]
    fn high_stability_does_not_explode() {
        // Use a fixed successful-review profile for all iterations.
        let exercise_type = ExerciseType::Declarative;
        let difficulty = BASE_DIFFICULTY;
        let p = (5.0 - GRADE_MIN) / GRADE_RANGE - 0.5;
        let e = (EASE_NUMERATOR_OFFSET - difficulty) / EASE_DENOMINATOR;
        let spacing_gain =
            PowerLawScorer::compute_spacing_gain(&exercise_type, 0.0, MIN_STABILITY, p);
        let base_growth_term = STABILITY_COEFFICIENT * p * e * spacing_gain;
        let max_damping = PowerLawScorer::compute_stability_damping(MIN_STABILITY);

        // Repeatedly apply success updates and ensure gains remain bounded.
        let mut stability = MIN_STABILITY;
        for _ in 0..25 {
            let stability_damping = PowerLawScorer::compute_stability_damping(stability);
            let effective_growth = base_growth_term * stability_damping;
            let next_stability =
                (stability * (1.0 + effective_growth)).clamp(MIN_STABILITY, MAX_STABILITY);
            let relative_gain = (next_stability - stability) / stability;

            // Relative gain stays bounded and non-negative.
            assert!(relative_gain >= 0.0);
            assert!(relative_gain <= base_growth_term * max_damping + f32::EPSILON);
            stability = next_stability;
        }

        assert!(stability <= MAX_STABILITY);
        assert!(stability >= MIN_STABILITY);
    }

    /// Verifies consistent successful trials reduce difficulty by pulling it toward easier recall.
    #[test]
    fn difficulty_trend_improves_with_successes() {
        // Start from a hard baseline and apply repeated good recall.
        let base_difficulty = MAX_DIFFICULTY;
        let trials = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(0),
            },
        ];

        let (_stability, adjusted_difficulty) = PowerLawScorer::compute_stability_and_difficulty(
            &ExerciseType::Declarative,
            &trials,
            base_difficulty,
        );
        assert!(adjusted_difficulty < base_difficulty);
        assert!(adjusted_difficulty >= MIN_DIFFICULTY);
    }

    /// Verifies repeated failed trials raise difficulty relative to a low baseline.
    #[test]
    fn difficulty_trend_worsens_with_failures() {
        // Start from an easy baseline and apply repeated misses.
        let base_difficulty = MIN_DIFFICULTY;
        let trials = vec![
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(0),
            },
        ];

        let (_stability, adjusted_difficulty) = PowerLawScorer::compute_stability_and_difficulty(
            &ExerciseType::Declarative,
            &trials,
            base_difficulty,
        );
        assert!(adjusted_difficulty > base_difficulty);
        assert!(adjusted_difficulty < MAX_DIFFICULTY);
    }

    /// Verifies mean reversion keeps difficulty bounded during long repeated failures.
    #[test]
    fn difficulty_mean_reversion_prevents_runaway() {
        // Repeated failures should increase difficulty, but reversion toward BASE_DIFFICULTY
        // prevents runaway growth.
        let failures = (0..20)
            .map(|days| ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(days),
            })
            .collect::<Vec<_>>();

        let (_stability, adjusted_difficulty) = PowerLawScorer::compute_stability_and_difficulty(
            &ExerciseType::Declarative,
            &failures,
            BASE_DIFFICULTY,
        );
        assert!(adjusted_difficulty > BASE_DIFFICULTY);
        assert!(adjusted_difficulty < MAX_DIFFICULTY - 1.0);
    }

    /// Verifies retrievability computation using power-law decay.
    #[test]
    fn compute_retrievability() {
        // Recent review: high retrievability
        let stability = DEFAULT_STABILITY;
        let recent_declarative =
            PowerLawScorer::compute_retrievability(&ExerciseType::Declarative, 0.01, stability);
        let recent_procedural =
            PowerLawScorer::compute_retrievability(&ExerciseType::Procedural, 0.01, stability);
        assert!(recent_declarative > 0.9);
        assert!(recent_declarative > recent_procedural);

        // Old review: moderate retrievability
        let old_declarative =
            PowerLawScorer::compute_retrievability(&ExerciseType::Declarative, 10.0, stability);
        let old_procedural =
            PowerLawScorer::compute_retrievability(&ExerciseType::Procedural, 10.0, stability);
        assert!(old_declarative < 0.6 && old_declarative > 0.4);
        assert!(old_declarative < old_procedural);

        // Very old: low retrievability
        let very_old_declarative =
            PowerLawScorer::compute_retrievability(&ExerciseType::Declarative, 100.0, stability);
        let very_old_procedural =
            PowerLawScorer::compute_retrievability(&ExerciseType::Procedural, 100.0, stability);
        assert!(very_old_declarative < 0.25);
        assert!(very_old_declarative < very_old_procedural);
    }

    /// Verifies that retrievability is calibrated to 90% when elapsed time equals stability.
    #[test]
    fn retrievability_at_stability_is_ninety_percent() {
        let declarative = PowerLawScorer::compute_retrievability(
            &ExerciseType::Declarative,
            DEFAULT_STABILITY,
            DEFAULT_STABILITY,
        );
        let procedural = PowerLawScorer::compute_retrievability(
            &ExerciseType::Procedural,
            DEFAULT_STABILITY,
            DEFAULT_STABILITY,
        );

        assert!((declarative - TARGET_RETRIEVABILITY_AT_STABILITY).abs() < 1e-6);
        assert!((procedural - TARGET_RETRIEVABILITY_AT_STABILITY).abs() < 1e-6);
    }

    /// Verifies pre-review spacing retrievability decreases as elapsed time grows.
    #[test]
    fn compute_spacing_retrievability() {
        let stability = DEFAULT_STABILITY;
        let recent_declarative = PowerLawScorer::compute_spacing_retrievability(
            &ExerciseType::Declarative,
            0.0,
            stability,
        );
        let old_declarative = PowerLawScorer::compute_spacing_retrievability(
            &ExerciseType::Declarative,
            30.0,
            stability,
        );
        let old_procedural = PowerLawScorer::compute_spacing_retrievability(
            &ExerciseType::Procedural,
            30.0,
            stability,
        );

        assert!((recent_declarative - 1.0).abs() < 1e-6);
        assert!(old_declarative >= 0.0 && old_declarative <= 1.0);
        assert!(old_procedural >= 0.0 && old_procedural <= 1.0);
        assert!(recent_declarative > old_declarative);
        assert!(old_procedural > old_declarative);
    }

    /// Verifies spacing gain grows with interval for successful reviews and stays neutral
    /// otherwise.
    #[test]
    fn compute_spacing_gain() {
        let stability = DEFAULT_STABILITY;
        let short_interval_gain =
            PowerLawScorer::compute_spacing_gain(&ExerciseType::Declarative, 0.0, stability, 0.25);
        let long_interval_gain =
            PowerLawScorer::compute_spacing_gain(&ExerciseType::Declarative, 10.0, stability, 0.25);
        let neutral_gain =
            PowerLawScorer::compute_spacing_gain(&ExerciseType::Declarative, 10.0, stability, 0.0);
        let failure_gain =
            PowerLawScorer::compute_spacing_gain(&ExerciseType::Declarative, 10.0, stability, -0.5);

        assert!(short_interval_gain >= 1.0 && short_interval_gain <= 1.0 + SPACING_EFFECT_WEIGHT);
        assert!(long_interval_gain > short_interval_gain);
        assert_eq!(neutral_gain, 1.0);
        assert_eq!(failure_gain, 1.0);
    }

    /// Verifies that the weighted average and variance are computed correctly.
    #[test]
    fn compute_weighted_avg_and_variance() {
        // Empty trials should return (0.0, 0.0).
        assert_eq!(
            PowerLawScorer::compute_weighted_avg_and_variance(&[]),
            (0.0, 0.0)
        );

        // Single trial with score 5.0 returns mean 5.0, variance 0.0.
        let single_trial = vec![ExerciseTrial {
            score: 5.0,
            timestamp: generate_timestamp(0),
        }];
        let (mean, var) = PowerLawScorer::compute_weighted_avg_and_variance(&single_trial);
        assert!((mean - 5.0).abs() < 1e-6);
        assert!(var.abs() < 1e-6);

        // Multiple trials: [5.0, 4.0, 3.0] should be approx 4.03 at this decay rate.
        let multi_trials = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(2),
            },
        ];
        let (weighted, var) = PowerLawScorer::compute_weighted_avg_and_variance(&multi_trials);
        assert!((weighted - 4.013).abs() < 0.001);
        assert!(var > 0.0);

        // Irregular spacing should down-weight distant failures more than dense spacing.
        let dense_low_tail = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(2),
            },
        ];
        let sparse_low_tail = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(30),
            },
        ];
        let (dense_weighted, dense_variance) =
            PowerLawScorer::compute_weighted_avg_and_variance(&dense_low_tail);
        let (sparse_weighted, sparse_variance) =
            PowerLawScorer::compute_weighted_avg_and_variance(&sparse_low_tail);
        assert!(sparse_weighted > dense_weighted);
        assert!(sparse_variance > dense_variance);

        // Very old history contributes a floor weight and remains somewhat influential.
        let compact = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(1),
            },
        ];
        let with_ancient = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(365),
            },
        ];
        let (compact_weighted, compact_variance) =
            PowerLawScorer::compute_weighted_avg_and_variance(&compact);
        let (ancient_weighted, ancient_variance) =
            PowerLawScorer::compute_weighted_avg_and_variance(&with_ancient);
        assert!(ancient_weighted < compact_weighted);
        assert!(ancient_weighted > 4.0);
        assert!(ancient_variance > compact_variance);
    }

    /// Verifies that the old-good retrievability floor is applied only when thresholds are met.
    #[test]
    fn apply_old_good_retrievability_floor() {
        assert_eq!(
            PowerLawScorer::apply_old_good_retrievability_floor(0.2, 4.0, 80.0, 3),
            OLD_GOOD_FLOOR
        );
        assert_eq!(
            PowerLawScorer::apply_old_good_retrievability_floor(0.95, 4.0, 80.0, 3),
            0.95
        );
        assert_eq!(
            PowerLawScorer::apply_old_good_retrievability_floor(0.2, 3.4, 80.0, 3),
            0.2
        );
        assert_eq!(
            PowerLawScorer::apply_old_good_retrievability_floor(0.2, 4.0, 49.0, 3),
            0.2
        );
        assert_eq!(
            PowerLawScorer::apply_old_good_retrievability_floor(0.2, 4.0, 80.0, 1),
            0.2
        );
    }

    /// Verifies that a recent bad performance results in a very low score.
    #[test]
    fn score_bad_recent() -> Result<()> {
        let trials = vec![
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(3),
            },
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(7),
            },
            ExerciseTrial {
                score: 2.0,
                timestamp: generate_timestamp(10),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(13),
            },
        ];
        let score = SCORER.score(ExerciseType::Declarative, &trials)?;
        assert!(score < 2.0);
        Ok(())
    }

    /// Verifies score for mixed performance history.
    #[test]
    fn score_mixed_performance() -> Result<()> {
        let trials = vec![
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(4),
            },
            ExerciseTrial {
                score: 2.0,
                timestamp: generate_timestamp(5),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(6),
            },
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(7),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(10),
            },
            ExerciseTrial {
                score: 2.0,
                timestamp: generate_timestamp(14),
            },
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(18),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(21),
            },
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(25),
            },
        ];
        let score = SCORER.score(ExerciseType::Declarative, &trials)?;
        assert!(score > 1.0 && score < 4.0);
        Ok(())
    }

    /// Verifies that trials not sorted in descending order by timestamp return an error.
    #[test]
    fn score_unsorted_trials() {
        let result = SCORER.score(
            ExerciseType::Declarative,
            &[
                ExerciseTrial {
                    score: 3.0,
                    timestamp: generate_timestamp(2),
                },
                ExerciseTrial {
                    score: 4.0,
                    timestamp: generate_timestamp(1),
                },
            ],
        );
        assert!(result.is_err());
    }

    /// Verifies that trials with old timestamp result in a low score.
    #[test]
    fn score_old_timestamp() -> Result<()> {
        let score = SCORER.score(
            ExerciseType::Declarative,
            &[ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(100),
            }],
        )?;
        assert!(score < 3.0);
        Ok(())
    }

    /// Verifies that the score for multiple good trials is very close to the maximum score.
    #[test]
    fn score_multiple_good() -> Result<()> {
        let trials = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(3),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(4),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(5),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(6),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(7),
            },
        ];
        let score = SCORER.score(ExerciseType::Declarative, &trials)?;
        assert!(score > 4.0);
        Ok(())
    }

    /// Verifies that multiple bad trials result in a very low score.
    #[test]
    fn score_multiple_bad() -> Result<()> {
        let trials = vec![
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 2.0,
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(4),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(6),
            },
            ExerciseTrial {
                score: 2.0,
                timestamp: generate_timestamp(9),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(15),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(16),
            },
            ExerciseTrial {
                score: 2.0,
                timestamp: generate_timestamp(27),
            },
        ];
        let score = SCORER.score(ExerciseType::Declarative, &trials)?;
        assert!(score < 2.0);
        Ok(())
    }

    /// Verifies that many old and well-spaced trials with good scores return a score that is still
    /// good due to high stability.
    #[test]
    fn score_old_good_trials() -> Result<()> {
        let trials = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(200),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(210),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(213),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(248),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(256),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(270),
            },
        ];
        let score = SCORER.score(ExerciseType::Procedural, &trials)?;
        assert!(score >= 3.5);
        let score = SCORER.score(ExerciseType::Declarative, &trials)?;
        assert!(score >= 3.5);
        Ok(())
    }

    /// Verifies that very old trials of an exercise with good scores return a high score due to
    /// strong stability.
    #[test]
    fn score_very_good_old_trialsl() -> Result<()> {
        let trials = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(400),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(410),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(411),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(420),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(430),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(431),
            },
        ];
        let score = SCORER.score(ExerciseType::Procedural, &trials)?;
        assert!(score >= 3.5);
        let score = SCORER.score(ExerciseType::Declarative, &trials)?;
        assert!(score >= 3.5);
        Ok(())
    }

    /// Verifies that high-variance trials score lower than consistent trials with the same mean.
    #[test]
    fn confidence_penalty_reduces_high_variance_score() -> Result<()> {
        // High variance: [5.0, 1.0], mean ~3.0.
        let high_var = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(1),
            },
        ];
        // Low variance: [3.0, 3.0], mean = 3.0.
        let low_var = vec![
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(1),
            },
        ];
        let high_var_score = SCORER.score(ExerciseType::Declarative, &high_var)?;
        let low_var_score = SCORER.score(ExerciseType::Declarative, &low_var)?;
        assert!(low_var_score > high_var_score);
        Ok(())
    }

    /// Verifies that the confidence penalty diminishes as the number of trials increases.
    #[test]
    fn confidence_penalty_diminishes_with_trials() -> Result<()> {
        // Fewer trials: alternating [5.0, 1.0].
        let few = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(1),
            },
        ];
        // More trials: alternating [5.0, 1.0, 5.0, 1.0, 5.0, 1.0].
        let many = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(3),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(4),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(5),
            },
        ];
        let few_score = SCORER.score(ExerciseType::Declarative, &few)?;
        let many_score = SCORER.score(ExerciseType::Declarative, &many)?;
        assert!(many_score > few_score,);
        Ok(())
    }

    /// Verifies that a single trial has zero variance and thus zero confidence penalty.
    #[test]
    fn confidence_penalty_zero_for_single_trial() {
        let single = vec![ExerciseTrial {
            score: 4.0,
            timestamp: generate_timestamp(0),
        }];
        let (_, var) = PowerLawScorer::compute_weighted_avg_and_variance(&single);
        assert!(var.abs() < 1e-6);
    }

    /// Verifies that identical scores yield zero variance and thus zero confidence penalty.
    #[test]
    fn confidence_penalty_zero_for_consistent_trials() {
        let consistent = vec![
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(2),
            },
        ];
        let (mean, var) = PowerLawScorer::compute_weighted_avg_and_variance(&consistent);
        assert!((mean - 4.0).abs() < 1e-6);
        assert!(var.abs() < 1e-5);
    }

    /// Verifies the weighted variance computation against hand-calculated values.
    #[test]
    fn weighted_variance_correctness() {
        // Two trials at the same timestamp (equal weights): scores [4.0, 2.0].
        // Mean = 3.0, variance = ((4-3)^2 + (2-3)^2) / 2 = 1.0.
        let now = generate_timestamp(0);
        let trials = vec![
            ExerciseTrial {
                score: 4.0,
                timestamp: now,
            },
            ExerciseTrial {
                score: 2.0,
                timestamp: now,
            },
        ];
        let (mean, var) = PowerLawScorer::compute_weighted_avg_and_variance(&trials);
        assert!((mean - 3.0).abs() < 1e-6);
        assert!((var - 1.0).abs() < 1e-6);
    }

    /// Verifies that velocity returns None for 0 or 1 trials.
    #[test]
    fn velocity_empty_trials() {
        assert_eq!(SCORER.velocity(&[]), None);

        let trials = vec![ExerciseTrial {
            score: 3.0,
            timestamp: generate_timestamp(0),
        }];
        assert_eq!(SCORER.velocity(&trials), None);
    }

    /// Verifies that improving scores (most recent is highest) produce positive velocity.
    #[test]
    fn velocity_improving_scores() {
        // Most-recent-first: [5.0, 4.0, 3.0, 2.0, 1.0] — scores are getting better over time.
        let trials = vec![
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 2.0,
                timestamp: generate_timestamp(3),
            },
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(4),
            },
        ];
        let velocity = SCORER.velocity(&trials).unwrap();
        assert!(velocity > 0.0);
    }

    /// Verifies that worsening scores (most recent is lowest) produce negative velocity.
    #[test]
    fn velocity_worsening_scores() {
        // Most-recent-first: [1.0, 2.0, 3.0, 4.0, 5.0] — scores are getting worse over time.
        let trials = vec![
            ExerciseTrial {
                score: 1.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 2.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(2),
            },
            ExerciseTrial {
                score: 4.0,
                timestamp: generate_timestamp(3),
            },
            ExerciseTrial {
                score: 5.0,
                timestamp: generate_timestamp(4),
            },
        ];
        let velocity = SCORER.velocity(&trials).unwrap();
        assert!(velocity < 0.0);
    }

    /// Verifies that constant scores produce near-zero velocity.
    #[test]
    fn velocity_constant_scores() {
        let trials = vec![
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(0),
            },
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(1),
            },
            ExerciseTrial {
                score: 3.0,
                timestamp: generate_timestamp(2),
            },
        ];
        let velocity = SCORER.velocity(&trials).unwrap();
        assert!(velocity.abs() < 1e-6);
    }
}