optirs-nas 0.3.2

OptiRS Neural Architecture Search and hyperparameter optimization
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
// Resource Monitoring and Management for Neural Architecture Search
//
// This module provides comprehensive resource monitoring, constraint enforcement,
// and resource optimization functionality for the NAS system.

use scirs2_core::numeric::Float;
use std::collections::HashMap;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use super::config::ResourceConstraints;
use super::results::{ResourceUsage, ResourceUsageSummary};
use super::telemetry::{StdTelemetry, TelemetrySample, TelemetrySource};
use crate::error::Result;

/// Resource monitor for tracking and managing system resources
pub struct ResourceMonitor<T: Float + Debug + Send + Sync + 'static> {
    /// Resource constraints
    constraints: ResourceConstraints<T>,

    /// Current resource usage
    current_usage: Arc<Mutex<ResourceUsage<T>>>,

    /// Resource usage history
    usage_history: Arc<Mutex<Vec<ResourceSnapshot<T>>>>,

    /// Monitoring configuration
    monitoring_config: MonitoringConfig,

    /// Resource monitors
    monitors: Vec<Box<dyn ResourceTracker<T>>>,

    /// Alert handlers
    alert_handlers: Vec<Box<dyn AlertHandler<T>>>,

    /// Monitoring state
    monitoring_state: MonitoringState,

    /// Resource optimization strategies
    optimization_strategies: Vec<Box<dyn ResourceOptimizer<T>>>,
}

impl<T: Float + Debug + Send + Sync + 'static> Debug for ResourceMonitor<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ResourceMonitor")
            .field("constraints", &self.constraints)
            .field("current_usage", &self.current_usage)
            .field("usage_history", &self.usage_history)
            .field("monitoring_config", &self.monitoring_config)
            .field("monitoring_state", &self.monitoring_state)
            .field("monitors_count", &self.monitors.len())
            .field("alert_handlers_count", &self.alert_handlers.len())
            .field(
                "optimization_strategies_count",
                &self.optimization_strategies.len(),
            )
            .finish()
    }
}

/// Lock a mutex, recovering the guarded value if the mutex was poisoned (F22).
///
/// Every mutex in this module guards a plain data snapshot: a `ResourceUsage`
/// total or an append-only history vector. Neither has an invariant that a
/// panicking writer could leave broken, so `PoisonError::into_inner` is the correct
/// recovery — the previous `lock().expect("lock poisoned")` turned one unrelated
/// panic into a permanently panicking getter.
fn lock_recovering<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
    mutex
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
}

/// Derive memory pressure from a telemetry sample, or `None` when memory is not
/// measurable. Shared by [`SystemResourceTracker`] and [`ResourceMonitor`].
fn memory_pressure_from_sample(sample: &TelemetrySample) -> Option<MemoryPressure> {
    let total = sample.total_memory_gb?;
    let available = sample.available_memory_gb?;
    if total <= 0.0 {
        return None;
    }
    let used_fraction = ((total - available) / total).clamp(0.0, 1.0);
    Some(if used_fraction >= 0.95 {
        MemoryPressure::Critical
    } else if used_fraction >= 0.85 {
        MemoryPressure::High
    } else if used_fraction >= 0.7 {
        MemoryPressure::Medium
    } else {
        MemoryPressure::Low
    })
}

/// Resource snapshot for history tracking
#[derive(Debug, Clone)]
pub struct ResourceSnapshot<T: Float + Debug + Send + Sync + 'static> {
    /// Timestamp
    pub timestamp: Instant,

    /// Resource usage at this time
    pub usage: ResourceUsage<T>,

    /// System metrics
    pub system_metrics: SystemMetrics<T>,

    /// Number of processes visible on the system, when measurable.
    pub active_processes: Option<usize>,

    /// Memory pressure level, derived from measured available/total memory. `None`
    /// when memory is not measurable — it used to be hard-coded to
    /// [`MemoryPressure::Low`] regardless.
    pub memory_pressure: Option<MemoryPressure>,

    /// 1-minute load average per logical CPU, when measurable. Previously a
    /// hard-coded `0.6`.
    pub cpu_load_average: Option<T>,

    /// GPU utilization in `[0, 1]`, when measurable. Previously a hard-coded `0.8`.
    pub gpu_utilization: Option<T>,
}

/// System metrics.
///
/// Every field is `Option` because most of them cannot be measured in pure Rust
/// (F12). `None` means **not measured**, and consumers must treat it as
/// *unconstrained* — never as zero, and never as a limit violation. The previous
/// version made every field mandatory, which forced
/// [`SystemResourceTracker`] to fabricate values (32 GB memory, 4 GPUs, 65 C,
/// 250 W) that were then compared against the caller's real constraints.
///
/// Supply your own [`crate::nas_engine::telemetry::TelemetrySource`] to fill in
/// the fields the default source cannot reach.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SystemMetrics<T: Float + Debug + Send + Sync + 'static> {
    /// Memory the OS reports as available for new allocations (GB).
    pub available_memory_gb: Option<T>,

    /// Logical CPUs usable by this process.
    pub available_cpu_cores: Option<usize>,

    /// GPU devices visible to this process.
    pub available_gpu_devices: Option<usize>,

    /// Free space on the working-directory filesystem (GB).
    pub available_disk_gb: Option<T>,

    /// Network bandwidth (MB/s).
    pub network_bandwidth: Option<T>,

    /// System temperature (Celsius).
    pub system_temperature: Option<T>,

    /// Power consumption (watts).
    pub power_consumption: Option<T>,
}

/// Memory pressure levels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryPressure {
    Low,
    Medium,
    High,
    Critical,
}

/// Monitoring configuration
#[derive(Debug, Clone)]
pub struct MonitoringConfig {
    /// Monitoring interval
    pub monitoring_interval: Duration,

    /// History retention period
    pub history_retention: Duration,

    /// Enable detailed monitoring
    pub enable_detailed_monitoring: bool,

    /// Enable predictive monitoring
    pub enable_predictive_monitoring: bool,

    /// Alert thresholds
    pub alert_thresholds: AlertThresholds,

    /// Enable automatic optimization
    pub enable_auto_optimization: bool,
}

/// Alert thresholds
#[derive(Debug, Clone)]
pub struct AlertThresholds {
    /// Memory usage threshold (0.0 to 1.0)
    pub memory_threshold: f64,

    /// CPU usage threshold (0.0 to 1.0)
    pub cpu_threshold: f64,

    /// GPU usage threshold (0.0 to 1.0)
    pub gpu_threshold: f64,

    /// Disk usage threshold (0.0 to 1.0)
    pub disk_threshold: f64,

    /// Temperature threshold (Celsius)
    pub temperature_threshold: f64,

    /// Power consumption threshold (watts)
    pub power_threshold: f64,
}

/// Monitoring state
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MonitoringState {
    Stopped,
    Starting,
    Running,
    Paused,
    Stopping,
    Error,
}

/// Resource tracker trait
pub trait ResourceTracker<T: Float + Debug + Send + Sync + 'static>: Send + Sync {
    /// Get current resource usage
    fn get_current_usage(&self) -> Result<ResourceUsage<T>>;

    /// Get system metrics
    fn get_system_metrics(&self) -> Result<SystemMetrics<T>>;

    /// Check if resource limits are exceeded
    fn check_limits(
        &self,
        constraints: &ResourceConstraints<T>,
    ) -> Result<Vec<ResourceViolation<T>>>;

    /// Get tracker name
    fn name(&self) -> &str;

    /// Initialize tracker
    fn initialize(&mut self) -> Result<()>;

    /// Cleanup tracker
    fn cleanup(&mut self) -> Result<()>;

    /// The raw telemetry reading behind [`ResourceTracker::get_system_metrics`], if
    /// this tracker has one.
    ///
    /// [`ResourceMonitor::update_usage`] uses it for the snapshot fields that are not
    /// part of [`SystemMetrics`] (process count, load average, GPU utilization).
    /// Trackers with no underlying sample return `None`, and those snapshot fields
    /// then stay `None` too — which is the honest outcome.
    fn telemetry_sample(&self) -> Option<TelemetrySample> {
        None
    }
}

/// Resource violation
#[derive(Debug, Clone)]
pub struct ResourceViolation<T: Float + Debug + Send + Sync + 'static> {
    /// Violation type
    pub violation_type: ViolationType,

    /// Current value
    pub current_value: T,

    /// Limit value
    pub limit_value: T,

    /// Severity
    pub severity: ViolationSeverity,

    /// Violation time
    pub violation_time: Instant,

    /// Affected resources
    pub affected_resources: Vec<String>,

    /// Suggested actions
    pub suggested_actions: Vec<String>,
}

/// Types of resource violations
#[derive(Debug, Clone, Copy)]
pub enum ViolationType {
    MemoryExceeded,
    CPUExceeded,
    GPUExceeded,
    DiskExceeded,
    TimeExceeded,
    EnergyExceeded,
    TemperatureExceeded,
    PowerExceeded,
    NetworkExceeded,
    ComputationTimeExceeded,
    CostExceeded,
}

/// Violation severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ViolationSeverity {
    Low,
    Medium,
    High,
    Critical,
}

/// Alert handler trait
pub trait AlertHandler<T: Float + Debug + Send + Sync + 'static>: Send + Sync {
    /// Handle resource violation
    fn handle_violation(&self, violation: &ResourceViolation<T>) -> Result<()>;

    /// Handle resource warning
    fn handle_warning(&self, warning: &ResourceWarning<T>) -> Result<()>;

    /// Get handler name
    fn name(&self) -> &str;
}

/// Resource warning
#[derive(Debug, Clone)]
pub struct ResourceWarning<T: Float + Debug + Send + Sync + 'static> {
    /// Warning type
    pub warning_type: WarningType,

    /// Current value
    pub current_value: T,

    /// Threshold value
    pub threshold_value: T,

    /// Trend direction
    pub trend: TrendDirection,

    /// Estimated time to violation
    pub time_to_violation: Option<Duration>,

    /// Warning message
    pub message: String,
}

/// Types of resource warnings
#[derive(Debug, Clone, Copy)]
pub enum WarningType {
    MemoryApproachingLimit,
    CPUApproachingLimit,
    GPUApproachingLimit,
    DiskApproachingLimit,
    TimeApproachingLimit,
    EnergyApproachingLimit,
    TemperatureRising,
    PowerIncreasing,
}

/// Trend directions
#[derive(Debug, Clone, Copy)]
pub enum TrendDirection {
    Increasing,
    Decreasing,
    Stable,
    Volatile,
}

/// Resource optimizer trait
pub trait ResourceOptimizer<T: Float + Debug + Send + Sync + 'static>: Send + Sync {
    /// Optimize resource usage
    fn optimize(
        &self,
        current_usage: &ResourceUsage<T>,
        constraints: &ResourceConstraints<T>,
    ) -> Result<OptimizationAction<T>>;

    /// Get optimizer name
    fn name(&self) -> &str;

    /// Get optimization priority
    fn priority(&self) -> OptimizationPriority;
}

/// Optimization actions
#[derive(Debug, Clone)]
pub struct OptimizationAction<T: Float + Debug + Send + Sync + 'static> {
    /// Action type
    pub action_type: ActionType,

    /// Action parameters
    pub parameters: HashMap<String, T>,

    /// Expected resource savings
    pub expected_savings: ResourceUsage<T>,

    /// Implementation cost
    pub implementation_cost: T,

    /// Action description
    pub description: String,

    /// Priority
    pub priority: OptimizationPriority,
}

/// Types of optimization actions
#[derive(Debug, Clone, Copy)]
pub enum ActionType {
    ReduceMemoryUsage,
    OptimizeCPUUsage,
    OptimizeGPUUsage,
    ClearCaches,
    GarbageCollection,
    ProcessThrottling,
    ResourceReallocation,
    TaskMigration,
    PowerManagement,
    TemperatureControl,
}

/// Optimization priorities
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum OptimizationPriority {
    Low,
    Medium,
    High,
    Critical,
}

/// System resource tracker backed by an injectable
/// [`TelemetrySource`] (F12).
///
/// It previously returned hard-coded readings (32 GB memory / 16 GB used, 60% CPU,
/// 4 GPUs at 80%, 1 TB disk, 1 GB/s network, 65 C, 250 W) from seven
/// `get_*_info` helpers, each commented "in a real implementation, this would
/// query system APIs". Those numbers reached [`ResourceTracker::check_limits`] and
/// were compared against the caller's real constraints, so a tight memory budget
/// aborted the search on invented data.
///
/// Now it reports whatever its source can actually measure and `None` for the rest,
/// and `check_limits` **skips** any check whose measurement is missing.
pub struct SystemResourceTracker {
    /// Tracker name
    name: String,

    /// Monitoring interval
    monitoring_interval: Duration,

    /// Last update time
    last_update: Instant,

    /// Telemetry source. Defaults to
    /// [`StdTelemetry`](crate::nas_engine::telemetry::StdTelemetry).
    telemetry: Box<dyn TelemetrySource>,

    /// Most recent reading, refreshed on every query.
    cached_sample: Option<TelemetrySample>,
}

impl Debug for SystemResourceTracker {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SystemResourceTracker")
            .field("name", &self.name)
            .field("monitoring_interval", &self.monitoring_interval)
            .field("telemetry", &self.telemetry.name())
            .field("cached_sample", &self.cached_sample)
            .finish()
    }
}

impl SystemResourceTracker {
    /// Create a tracker using the default (honest, sparse) telemetry source.
    pub fn new(name: String, monitoring_interval: Duration) -> Self {
        Self::with_telemetry(name, monitoring_interval, Box::new(StdTelemetry::new()))
    }

    /// Create a tracker backed by a caller-supplied telemetry source. This is how
    /// GPU / thermal / power constraints become enforceable: provide a source that
    /// can measure them.
    pub fn with_telemetry(
        name: String,
        monitoring_interval: Duration,
        telemetry: Box<dyn TelemetrySource>,
    ) -> Self {
        Self {
            name,
            monitoring_interval,
            last_update: Instant::now(),
            telemetry,
            cached_sample: None,
        }
    }

    /// Name of the telemetry source in use.
    pub fn telemetry_name(&self) -> &str {
        self.telemetry.name()
    }

    /// The most recent reading, if one has been taken.
    pub fn last_sample(&self) -> Option<&TelemetrySample> {
        self.cached_sample.as_ref()
    }

    /// Take a fresh reading.
    pub fn sample(&self) -> TelemetrySample {
        self.telemetry.sample()
    }

    /// Memory pressure derived from the current reading, or `None` when memory is
    /// not measurable here.
    pub fn memory_pressure(&self) -> Option<MemoryPressure> {
        memory_pressure_from_sample(&self.telemetry.sample())
    }
}

impl<T: Float + Debug + Send + Sync + 'static> ResourceTracker<T> for SystemResourceTracker
where
    T: From<f64> + std::fmt::Debug,
{
    /// Resource usage attributable to **this search**.
    ///
    /// Only the process's own resident set size is reported, and only where it can
    /// be measured; everything else stays at the accumulator identity (`0`), which
    /// is honest for a *usage* total that nothing has been attributed to. The
    /// previous version reported system-wide invented figures here, which is a
    /// category error as well as a fabrication: `ResourceUsage` is compared against
    /// a budget for the search, so charging it for the whole machine aborts the
    /// search whenever the host is busy.
    fn get_current_usage(&self) -> Result<ResourceUsage<T>> {
        let sample = self.telemetry.sample();
        let mut usage = ResourceUsage::default();
        if let Some(process_memory_gb) = sample.process_memory_gb {
            let value: T =
                scirs2_core::numeric::NumCast::from(process_memory_gb).unwrap_or_else(|| T::zero());
            usage.memory_gb = value;
            usage.peak_memory_gb = value;
        }
        Ok(usage)
    }

    fn get_system_metrics(&self) -> Result<SystemMetrics<T>> {
        let sample = self.telemetry.sample();
        let convert = |value: Option<f64>| -> Option<T> {
            value.and_then(|v| scirs2_core::numeric::NumCast::from(v))
        };
        Ok(SystemMetrics {
            available_memory_gb: convert(sample.available_memory_gb),
            available_cpu_cores: sample.logical_cpus,
            available_gpu_devices: sample.gpu_devices,
            available_disk_gb: convert(sample.available_disk_gb),
            network_bandwidth: convert(sample.network_bandwidth_mbps),
            system_temperature: convert(sample.temperature_celsius),
            power_consumption: convert(sample.power_watts),
        })
    }

    /// Report only violations backed by an actual measurement (F12).
    ///
    /// Every check is guarded by the presence of its measurement, so an
    /// unmeasurable resource is treated as **unlimited** and can never abort a
    /// search. This is the whole point: `check_violations` is called from
    /// `NeuralArchitectureSearch::should_stop_search` on every iteration, so a
    /// fabricated reading here terminated real searches.
    fn check_limits(
        &self,
        constraints: &ResourceConstraints<T>,
    ) -> Result<Vec<ResourceViolation<T>>> {
        let sample = self.telemetry.sample();
        let mut violations = Vec::new();

        // Memory: this process's RSS against the hardware memory budget.
        if let Some(process_memory_gb) = sample.process_memory_gb {
            let current: T =
                scirs2_core::numeric::NumCast::from(process_memory_gb).unwrap_or_else(|| T::zero());
            let limit: T =
                scirs2_core::numeric::NumCast::from(constraints.hardware_resources.max_memory_gb)
                    .unwrap_or_else(|| T::zero());
            if limit > T::zero() && current > limit {
                violations.push(ResourceViolation {
                    violation_type: ViolationType::MemoryExceeded,
                    current_value: current,
                    limit_value: limit,
                    severity: ViolationSeverity::High,
                    violation_time: Instant::now(),
                    affected_resources: vec!["Memory".to_string()],
                    suggested_actions: vec![
                        "Clear caches".to_string(),
                        "Reduce batch size".to_string(),
                        "Enable memory optimization".to_string(),
                    ],
                });
            }
        }

        // Power: only when a source can actually read it.
        if let Some(power_watts) = sample.power_watts {
            let current: T =
                scirs2_core::numeric::NumCast::from(power_watts).unwrap_or_else(|| T::zero());
            let limit: T =
                scirs2_core::numeric::NumCast::from(POWER_LIMIT_WATTS).unwrap_or_else(|| T::zero());
            if current > limit {
                violations.push(ResourceViolation {
                    violation_type: ViolationType::PowerExceeded,
                    current_value: current,
                    limit_value: limit,
                    severity: ViolationSeverity::Medium,
                    violation_time: Instant::now(),
                    affected_resources: vec!["Power".to_string()],
                    suggested_actions: vec![
                        "Reduce clock speeds".to_string(),
                        "Throttle processes".to_string(),
                        "Enable power saving mode".to_string(),
                    ],
                });
            }
        }

        // Temperature: likewise.
        if let Some(temperature_celsius) = sample.temperature_celsius {
            let current: T = scirs2_core::numeric::NumCast::from(temperature_celsius)
                .unwrap_or_else(|| T::zero());
            let limit: T = scirs2_core::numeric::NumCast::from(TEMPERATURE_LIMIT_CELSIUS)
                .unwrap_or_else(|| T::zero());
            if current > limit {
                violations.push(ResourceViolation {
                    violation_type: ViolationType::TemperatureExceeded,
                    current_value: current,
                    limit_value: limit,
                    severity: ViolationSeverity::Critical,
                    violation_time: Instant::now(),
                    affected_resources: vec!["CPU".to_string(), "GPU".to_string()],
                    suggested_actions: vec![
                        "Increase cooling".to_string(),
                        "Reduce workload".to_string(),
                        "Enable thermal throttling".to_string(),
                    ],
                });
            }
        }

        Ok(violations)
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn initialize(&mut self) -> Result<()> {
        self.last_update = Instant::now();
        let sample = self.telemetry.sample();
        if sample.is_fully_unknown() {
            log::warn!(
                "resource tracker {} initialized with telemetry source '{}', which can measure \
                 nothing on this platform; every resource constraint will be treated as \
                 unlimited",
                self.name,
                self.telemetry.name()
            );
        } else {
            log::info!(
                "resource tracker {} initialized with telemetry source '{}': {:?}",
                self.name,
                self.telemetry.name(),
                sample
            );
        }
        self.cached_sample = Some(sample);
        Ok(())
    }

    fn cleanup(&mut self) -> Result<()> {
        self.cached_sample = None;
        log::info!("cleaned up system resource tracker: {}", self.name);
        Ok(())
    }

    fn telemetry_sample(&self) -> Option<TelemetrySample> {
        Some(self.telemetry.sample())
    }
}

/// Package power above which a [`ViolationType::PowerExceeded`] is reported —
/// checked **only** when a telemetry source can actually read power draw.
const POWER_LIMIT_WATTS: f64 = 1000.0;

/// Die temperature above which a [`ViolationType::TemperatureExceeded`] is
/// reported — checked only when a source can actually read temperature.
const TEMPERATURE_LIMIT_CELSIUS: f64 = 80.0;

/// Console alert handler implementation
pub struct ConsoleAlertHandler {
    /// Handler name
    name: String,

    /// Enable verbose logging
    verbose: bool,
}

impl ConsoleAlertHandler {
    /// Create a new console alert handler
    pub fn new(name: String, verbose: bool) -> Self {
        Self { name, verbose }
    }
}

impl<T: Float + Debug + Send + Sync + 'static> AlertHandler<T> for ConsoleAlertHandler
where
    T: std::fmt::Display + std::fmt::Debug,
{
    /// Report a violation through `log`, at a level matching its severity (F24).
    ///
    /// This used to `println!` unconditionally, which writes to a library consumer's
    /// stdout with no way to filter or redirect it.
    fn handle_violation(&self, violation: &ResourceViolation<T>) -> Result<()> {
        let summary = format!(
            "resource violation {:?}: current={}, limit={}, severity={:?}",
            violation.violation_type,
            violation.current_value,
            violation.limit_value,
            violation.severity
        );
        match violation.severity {
            ViolationSeverity::Critical | ViolationSeverity::High => log::error!("{}", summary),
            ViolationSeverity::Medium => log::warn!("{}", summary),
            ViolationSeverity::Low => log::info!("{}", summary),
        }

        if self.verbose {
            log::debug!(
                "violation detail: time={:?}, affected={:?}, suggested={:?}",
                violation.violation_time,
                violation.affected_resources,
                violation.suggested_actions
            );
        }

        Ok(())
    }

    /// Report a warning through `log` (F24).
    fn handle_warning(&self, warning: &ResourceWarning<T>) -> Result<()> {
        log::warn!(
            "resource warning {:?}: current={}, threshold={}, trend={:?}",
            warning.warning_type,
            warning.current_value,
            warning.threshold_value,
            warning.trend
        );

        if self.verbose {
            log::debug!(
                "warning detail: time_to_violation={:?}, message={}",
                warning.time_to_violation,
                warning.message
            );
        }

        Ok(())
    }

    fn name(&self) -> &str {
        &self.name
    }
}

/// Memory optimizer implementation
pub struct MemoryOptimizer {
    /// Optimizer name
    name: String,

    /// Optimization aggressiveness
    aggressiveness: f64,
}

impl MemoryOptimizer {
    /// Create a new memory optimizer
    pub fn new(name: String, aggressiveness: f64) -> Self {
        Self {
            name,
            aggressiveness,
        }
    }
}

impl<T: Float + Debug + Send + Sync + 'static> ResourceOptimizer<T> for MemoryOptimizer
where
    T: From<f64> + std::cmp::PartialOrd,
{
    fn optimize(
        &self,
        current_usage: &ResourceUsage<T>,
        constraints: &ResourceConstraints<T>,
    ) -> Result<OptimizationAction<T>> {
        let memory_limit =
            scirs2_core::numeric::NumCast::from(constraints.hardware_resources.max_memory_gb)
                .unwrap_or_else(|| T::zero());

        if current_usage.memory_gb
            > memory_limit * scirs2_core::numeric::NumCast::from(0.8).unwrap_or_else(|| T::zero())
        {
            let reduction_target = current_usage.memory_gb
                * scirs2_core::numeric::NumCast::from(self.aggressiveness * 0.2)
                    .unwrap_or_else(|| T::zero());

            let mut parameters = HashMap::new();
            parameters.insert("memory_reduction_gb".to_string(), reduction_target);
            parameters.insert(
                "aggressiveness".to_string(),
                scirs2_core::numeric::NumCast::from(self.aggressiveness)
                    .unwrap_or_else(|| T::zero()),
            );

            Ok(OptimizationAction {
                action_type: ActionType::ReduceMemoryUsage,
                parameters,
                expected_savings: ResourceUsage {
                    memory_gb: reduction_target,
                    cpu_time_seconds: scirs2_core::numeric::NumCast::from(0.0)
                        .unwrap_or_else(|| T::zero()),
                    gpu_time_seconds: scirs2_core::numeric::NumCast::from(0.0)
                        .unwrap_or_else(|| T::zero()),
                    energy_kwh: scirs2_core::numeric::NumCast::from(0.0)
                        .unwrap_or_else(|| T::zero()),
                    network_io_gb: scirs2_core::numeric::NumCast::from(0.0)
                        .unwrap_or_else(|| T::zero()),
                    disk_io_gb: scirs2_core::numeric::NumCast::from(0.0)
                        .unwrap_or_else(|| T::zero()),
                    peak_memory_gb: reduction_target,
                    efficiency_score: scirs2_core::numeric::NumCast::from(0.1)
                        .unwrap_or_else(|| T::zero()),
                    cost_usd: scirs2_core::numeric::NumCast::from(0.0).unwrap_or_else(|| T::zero()),
                    network_gb: scirs2_core::numeric::NumCast::from(0.0)
                        .unwrap_or_else(|| T::zero()),
                },
                implementation_cost: scirs2_core::numeric::NumCast::from(0.05)
                    .unwrap_or_else(|| T::zero()),
                description: "Reduce memory usage through cache clearing and optimization"
                    .to_string(),
                priority: OptimizationPriority::High,
            })
        } else {
            Ok(OptimizationAction {
                action_type: ActionType::ReduceMemoryUsage,
                parameters: HashMap::new(),
                expected_savings: ResourceUsage::default(),
                implementation_cost: scirs2_core::numeric::NumCast::from(0.0)
                    .unwrap_or_else(|| T::zero()),
                description: "No memory optimization needed".to_string(),
                priority: OptimizationPriority::Low,
            })
        }
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn priority(&self) -> OptimizationPriority {
        OptimizationPriority::High
    }
}

impl<T: Float + Debug + Send + Sync + 'static> ResourceMonitor<T>
where
    T: From<f64> + std::fmt::Display,
{
    /// Create a new resource monitor
    pub fn new(constraints: ResourceConstraints<T>) -> Self {
        let monitoring_config = MonitoringConfig {
            monitoring_interval: Duration::from_secs(5),
            history_retention: Duration::from_secs(3600), // 1 hour
            enable_detailed_monitoring: true,
            enable_predictive_monitoring: false,
            alert_thresholds: AlertThresholds {
                memory_threshold: 0.8,
                cpu_threshold: 0.9,
                gpu_threshold: 0.9,
                disk_threshold: 0.9,
                temperature_threshold: 75.0,
                power_threshold: 800.0,
            },
            enable_auto_optimization: true,
        };

        let monitors: Vec<Box<dyn ResourceTracker<T>>> =
            vec![Box::new(SystemResourceTracker::new(
                "SystemTracker".to_string(),
                monitoring_config.monitoring_interval,
            ))];

        let alert_handlers: Vec<Box<dyn AlertHandler<T>>> = vec![Box::new(
            ConsoleAlertHandler::new("ConsoleHandler".to_string(), true),
        )];

        let optimization_strategies: Vec<Box<dyn ResourceOptimizer<T>>> = vec![Box::new(
            MemoryOptimizer::new("MemoryOptimizer".to_string(), 0.8),
        )];

        Self {
            constraints,
            current_usage: Arc::new(Mutex::new(ResourceUsage::default())),
            usage_history: Arc::new(Mutex::new(Vec::new())),
            monitoring_config,
            monitors,
            alert_handlers,
            monitoring_state: MonitoringState::Stopped,
            optimization_strategies,
        }
    }

    /// Start resource monitoring
    pub fn start_monitoring(&mut self) -> Result<()> {
        self.monitoring_state = MonitoringState::Starting;

        // Initialize all monitors
        for monitor in &mut self.monitors {
            monitor.initialize()?;
        }

        self.monitoring_state = MonitoringState::Running;
        log::info!(
            "resource monitoring started with {} tracker(s)",
            self.monitors.len()
        );
        Ok(())
    }

    /// Stop resource monitoring
    pub fn stop_monitoring(&mut self) -> Result<()> {
        self.monitoring_state = MonitoringState::Stopping;

        // Cleanup all monitors
        for monitor in &mut self.monitors {
            monitor.cleanup()?;
        }

        self.monitoring_state = MonitoringState::Stopped;
        log::info!("resource monitoring stopped");
        Ok(())
    }

    /// Sample every tracker and record a snapshot (F12).
    ///
    /// This is called once per generation from
    /// `NeuralArchitectureSearch::check_resource_constraints`; before that wiring it
    /// was never called at all, so `current_usage` stayed at
    /// `ResourceUsage::default()` forever and `check_resource_violations` could
    /// never fire.
    ///
    /// The snapshot's derived fields are all measured or absent: `active_processes`,
    /// `memory_pressure`, `cpu_load_average` and `gpu_utilization` used to be
    /// hard-coded to `1` / `Low` / `0.6` / `0.8`.
    ///
    /// The clock is read once and reused for the timestamp and the retention cutoff,
    /// so a snapshot cannot be pruned by an `Instant::now()` taken microseconds later.
    pub fn update_usage(&self) -> Result<()> {
        if self.monitoring_state != MonitoringState::Running {
            return Ok(());
        }

        let mut total_usage = ResourceUsage::default();
        let mut system_metrics = None;
        let mut telemetry_sample: Option<TelemetrySample> = None;

        for monitor in &self.monitors {
            let usage = monitor.get_current_usage()?;
            total_usage.memory_gb = total_usage.memory_gb + usage.memory_gb;
            total_usage.cpu_time_seconds = total_usage.cpu_time_seconds + usage.cpu_time_seconds;
            total_usage.gpu_time_seconds = total_usage.gpu_time_seconds + usage.gpu_time_seconds;
            total_usage.energy_kwh = total_usage.energy_kwh + usage.energy_kwh;
            if total_usage.peak_memory_gb < usage.peak_memory_gb {
                total_usage.peak_memory_gb = usage.peak_memory_gb;
            }

            if system_metrics.is_none() {
                system_metrics = Some(monitor.get_system_metrics()?);
            }
            if telemetry_sample.is_none() {
                telemetry_sample = monitor.telemetry_sample();
            }
        }

        {
            let mut current = lock_recovering(&self.current_usage);
            *current = total_usage.clone();
        }

        if let Some(metrics) = system_metrics {
            let sample = telemetry_sample.unwrap_or_default();
            let now = Instant::now();
            let snapshot = ResourceSnapshot {
                timestamp: now,
                usage: total_usage,
                system_metrics: metrics,
                active_processes: sample.process_count,
                memory_pressure: memory_pressure_from_sample(&sample),
                cpu_load_average: sample
                    .load_average_per_cpu
                    .and_then(scirs2_core::numeric::NumCast::from),
                gpu_utilization: sample
                    .gpu_utilization
                    .and_then(scirs2_core::numeric::NumCast::from),
            };

            {
                let mut history = lock_recovering(&self.usage_history);
                history.push(snapshot);
                let cutoff_time = now
                    .checked_sub(self.monitoring_config.history_retention)
                    .unwrap_or(now);
                history.retain(|s| s.timestamp >= cutoff_time);
            }
        }

        Ok(())
    }

    /// Check for resource violations
    pub fn check_violations(&self) -> Result<Vec<ResourceViolation<T>>> {
        let mut all_violations = Vec::new();

        for monitor in &self.monitors {
            let violations = monitor.check_limits(&self.constraints)?;
            all_violations.extend(violations);
        }

        // Handle violations
        for violation in &all_violations {
            for handler in &self.alert_handlers {
                handler.handle_violation(violation)?;
            }
        }

        Ok(all_violations)
    }

    /// Optimize resource usage
    pub fn optimize_resources(&self) -> Result<Vec<OptimizationAction<T>>> {
        if !self.monitoring_config.enable_auto_optimization {
            return Ok(Vec::new());
        }

        let current_usage = {
            let usage = lock_recovering(&self.current_usage);
            usage.clone()
        };

        let mut optimization_actions = Vec::new();

        for optimizer in &self.optimization_strategies {
            let action = optimizer.optimize(&current_usage, &self.constraints)?;
            if action.priority >= OptimizationPriority::Medium {
                optimization_actions.push(action);
            }
        }

        // Most urgent first (F25). `OptimizationPriority` derives `Ord` with
        // `Low < Medium < High < Critical`, so the previous ascending
        // `sort_by_key(|a| a.priority)` put the *least* urgent action at the front —
        // the opposite of what a caller applying a prefix of the list needs.
        optimization_actions.sort_by_key(|action| std::cmp::Reverse(action.priority));

        Ok(optimization_actions)
    }

    /// Get current resource usage.
    ///
    /// Recovers from a poisoned mutex instead of panicking (F22): the guarded data
    /// is a plain snapshot with no invariants that a panicking writer could have
    /// broken, so one panic elsewhere must not make this getter panic forever.
    pub fn get_current_usage(&self) -> ResourceUsage<T> {
        lock_recovering(&self.current_usage).clone()
    }

    /// Get usage history. Poison-recovering, for the same reason as
    /// [`ResourceMonitor::get_current_usage`].
    pub fn get_usage_history(&self) -> Vec<ResourceSnapshot<T>> {
        lock_recovering(&self.usage_history).clone()
    }

    /// Get monitoring state
    pub fn get_monitoring_state(&self) -> MonitoringState {
        self.monitoring_state
    }

    /// Replace the resource trackers. This is the injection point for real
    /// telemetry: hand in a [`SystemResourceTracker::with_telemetry`] built over a
    /// source that can measure what the default cannot (F12).
    pub fn set_trackers(&mut self, monitors: Vec<Box<dyn ResourceTracker<T>>>) {
        self.monitors = monitors;
    }

    /// Replace the alert handlers.
    pub fn set_alert_handlers(&mut self, handlers: Vec<Box<dyn AlertHandler<T>>>) {
        self.alert_handlers = handlers;
    }

    /// Replace the optimization strategies.
    pub fn set_optimization_strategies(&mut self, strategies: Vec<Box<dyn ResourceOptimizer<T>>>) {
        self.optimization_strategies = strategies;
    }

    /// Names of the telemetry sources currently backing this monitor.
    pub fn telemetry_source_names(&self) -> Vec<&str> {
        self.monitors.iter().map(|monitor| monitor.name()).collect()
    }

    /// Deliberately poison both internal mutexes, so a test can prove the getters
    /// recover (F22) rather than panicking forever.
    #[cfg(test)]
    pub(crate) fn poison_locks_for_test(&self) {
        for _ in 0..2 {
            let usage = std::sync::Arc::clone(&self.current_usage);
            let history = std::sync::Arc::clone(&self.usage_history);
            let handle = std::thread::spawn(move || {
                let _usage_guard = usage.lock();
                let _history_guard = history.lock();
                panic!("deliberate panic to poison the resource-monitor mutexes");
            });
            let _ = handle.join();
        }
        debug_assert!(self.current_usage.is_poisoned());
        debug_assert!(self.usage_history.is_poisoned());
    }

    /// Update constraints
    pub fn update_constraints(&mut self, constraints: ResourceConstraints<T>) {
        self.constraints = constraints;
    }

    /// Generate resource report
    pub fn generate_report(&self) -> ResourceReport<T> {
        let current_usage = self.get_current_usage();
        let history = self.get_usage_history();

        let usage_trend = self.calculate_usage_trend(&history);
        let efficiency_score = self.calculate_efficiency_score(&current_usage);
        let recommendations = self.generate_recommendations(&current_usage, &history);

        ResourceReport {
            timestamp: Instant::now(),
            current_usage,
            usage_trend,
            efficiency_score,
            total_samples: history.len(),
            monitoring_duration: match (history.first(), history.last()) {
                // `duration_since` on `Instant` panics if the argument is later;
                // history is append-only and monotonic, but `saturating_duration_since`
                // makes that structural rather than an assumption.
                (Some(first), Some(last)) => {
                    last.timestamp.saturating_duration_since(first.timestamp)
                }
                _ => Duration::from_secs(0),
            },
            recommendations,
            constraint_violations: self.check_violations().unwrap_or_default(),
        }
    }

    /// Calculate usage trend
    fn calculate_usage_trend(&self, history: &[ResourceSnapshot<T>]) -> UsageTrend<T> {
        if history.len() < 2 {
            return UsageTrend::default();
        }

        let recent_window = history.len().min(10);
        let recent = &history[history.len() - recent_window..];

        let memory_trend =
            self.calculate_metric_trend(recent.iter().map(|s| s.usage.memory_gb).collect());
        let cpu_trend =
            self.calculate_metric_trend(recent.iter().map(|s| s.usage.cpu_time_seconds).collect());
        let gpu_trend =
            self.calculate_metric_trend(recent.iter().map(|s| s.usage.gpu_time_seconds).collect());

        UsageTrend {
            memory_trend,
            cpu_trend,
            gpu_trend,
            energy_trend: TrendDirection::Stable,  // Simplified
            overall_trend: TrendDirection::Stable, // Simplified
            _phantom: std::marker::PhantomData,
        }
    }

    /// Calculate metric trend
    fn calculate_metric_trend(&self, values: Vec<T>) -> TrendDirection {
        if values.len() < 2 {
            return TrendDirection::Stable;
        }

        let first_half_avg = values.iter().take(values.len() / 2).fold(
            scirs2_core::numeric::NumCast::from(0.0).unwrap_or_else(|| T::zero()),
            |acc, &x| acc + x,
        ) / scirs2_core::numeric::NumCast::from(values.len() / 2)
            .unwrap_or_else(|| T::one());
        let second_half_avg =
            values.iter().skip(values.len() / 2).fold(
                scirs2_core::numeric::NumCast::from(0.0).unwrap_or_else(|| T::zero()),
                |acc, &x| acc + x,
            ) / scirs2_core::numeric::NumCast::from(values.len() - values.len() / 2)
                .unwrap_or_else(|| T::one());

        let change_threshold =
            scirs2_core::numeric::NumCast::from(0.05).unwrap_or_else(|| T::zero()); // 5% change threshold

        if second_half_avg > first_half_avg + change_threshold {
            TrendDirection::Increasing
        } else if second_half_avg < first_half_avg - change_threshold {
            TrendDirection::Decreasing
        } else {
            TrendDirection::Stable
        }
    }

    /// Calculate efficiency score
    fn calculate_efficiency_score(&self, usage: &ResourceUsage<T>) -> T {
        // Simplified efficiency calculation
        usage.efficiency_score
    }

    /// Generate recommendations
    fn generate_recommendations(
        &self,
        _usage: &ResourceUsage<T>,
        _history: &[ResourceSnapshot<T>],
    ) -> Vec<String> {
        vec![
            "Consider enabling memory optimization".to_string(),
            "Monitor GPU utilization for potential improvements".to_string(),
            "Review energy consumption patterns".to_string(),
        ]
    }

    /// Check for resource constraint violations
    pub fn check_resource_violations(&self) -> Result<Vec<ResourceViolation<T>>> {
        let current_usage = self.get_current_usage();
        let mut violations = Vec::new();

        // Check memory constraints
        if current_usage.memory_gb > self.constraints.max_memory_gb {
            violations.push(ResourceViolation {
                violation_type: ViolationType::MemoryExceeded,
                severity: ViolationSeverity::High,
                current_value: current_usage.memory_gb,
                limit_value: self.constraints.max_memory_gb,
                violation_time: Instant::now(),
                affected_resources: vec!["System Memory".to_string(), "RAM".to_string()],
                suggested_actions: vec![
                    "Reduce batch size".to_string(),
                    "Clear memory cache".to_string(),
                    "Enable memory optimization".to_string(),
                ],
            });
        }

        // Check CPU time constraints
        if current_usage.cpu_time_seconds
            > self.constraints.max_computation_hours
                * scirs2_core::numeric::NumCast::from(3600.0).unwrap_or_else(|| T::zero())
        {
            violations.push(ResourceViolation {
                violation_type: ViolationType::ComputationTimeExceeded,
                severity: ViolationSeverity::Medium,
                current_value: current_usage.cpu_time_seconds,
                limit_value: self.constraints.max_computation_hours
                    * scirs2_core::numeric::NumCast::from(3600.0).unwrap_or_else(|| T::zero()),
                violation_time: Instant::now(),
                affected_resources: vec!["CPU".to_string(), "Computation Time".to_string()],
                suggested_actions: vec![
                    "Optimize algorithms".to_string(),
                    "Enable parallelization".to_string(),
                    "Reduce search space".to_string(),
                ],
            });
        }

        // Check energy constraints
        if current_usage.energy_kwh > self.constraints.max_energy_kwh {
            violations.push(ResourceViolation {
                violation_type: ViolationType::EnergyExceeded,
                severity: ViolationSeverity::Low,
                current_value: current_usage.energy_kwh,
                limit_value: self.constraints.max_energy_kwh,
                violation_time: Instant::now(),
                affected_resources: vec!["Energy".to_string(), "Power Consumption".to_string()],
                suggested_actions: vec![
                    "Enable power-saving mode".to_string(),
                    "Reduce GPU usage".to_string(),
                    "Optimize computation schedule".to_string(),
                ],
            });
        }

        // Check cost constraints
        if current_usage.cost_usd > self.constraints.max_cost_usd {
            violations.push(ResourceViolation {
                violation_type: ViolationType::CostExceeded,
                severity: ViolationSeverity::High,
                current_value: current_usage.cost_usd,
                limit_value: self.constraints.max_cost_usd,
                violation_time: Instant::now(),
                affected_resources: vec!["Budget".to_string(), "Cost".to_string()],
                suggested_actions: vec![
                    "Stop non-critical tasks".to_string(),
                    "Switch to cheaper resources".to_string(),
                    "Optimize resource allocation".to_string(),
                ],
            });
        }

        Ok(violations)
    }

    /// Get a summary of resource usage
    pub fn get_usage_summary(&self) -> ResourceUsageSummary<T> {
        let current_usage = self.get_current_usage();
        let history = self.get_usage_history();

        // Calculate totals from history
        let mut total_cpu_hours = T::zero();
        let mut total_gpu_hours = T::zero();
        let mut total_energy_kwh = T::zero();
        let mut total_cost_usd = T::zero();

        for snapshot in &history {
            total_cpu_hours = total_cpu_hours
                + snapshot.usage.cpu_time_seconds
                    / scirs2_core::numeric::NumCast::from(3600.0).unwrap_or_else(|| T::one());
            total_gpu_hours = total_gpu_hours
                + snapshot.usage.gpu_time_seconds
                    / scirs2_core::numeric::NumCast::from(3600.0).unwrap_or_else(|| T::one());
            total_energy_kwh = total_energy_kwh + snapshot.usage.energy_kwh;
            total_cost_usd = total_cost_usd + snapshot.usage.cost_usd;
        }

        ResourceUsageSummary {
            total_memory_gb: current_usage.memory_gb,
            total_cpu_hours,
            total_gpu_hours,
            total_energy_kwh,
            total_cost_usd,
            average_efficiency: current_usage.efficiency_score,
        }
    }
}

/// Resource usage trend
#[derive(Debug, Clone)]
pub struct UsageTrend<T: Float + Debug + Send + Sync + 'static> {
    pub memory_trend: TrendDirection,
    pub cpu_trend: TrendDirection,
    pub gpu_trend: TrendDirection,
    pub energy_trend: TrendDirection,
    pub overall_trend: TrendDirection,
    _phantom: PhantomData<T>,
}

impl<T: Float + Debug + Send + Sync + 'static> Default for UsageTrend<T> {
    fn default() -> Self {
        Self {
            memory_trend: TrendDirection::Stable,
            cpu_trend: TrendDirection::Stable,
            gpu_trend: TrendDirection::Stable,
            energy_trend: TrendDirection::Stable,
            overall_trend: TrendDirection::Stable,
            _phantom: PhantomData,
        }
    }
}

/// Resource monitoring report
#[derive(Debug, Clone)]
pub struct ResourceReport<T: Float + Debug + Send + Sync + 'static> {
    /// Report timestamp
    pub timestamp: Instant,

    /// Current resource usage
    pub current_usage: ResourceUsage<T>,

    /// Usage trends
    pub usage_trend: UsageTrend<T>,

    /// Efficiency score
    pub efficiency_score: T,

    /// Total samples in history
    pub total_samples: usize,

    /// Monitoring duration
    pub monitoring_duration: Duration,

    /// Optimization recommendations
    pub recommendations: Vec<String>,

    /// Current constraint violations
    pub constraint_violations: Vec<ResourceViolation<T>>,
}

// Default implementations
impl Default for MonitoringConfig {
    fn default() -> Self {
        Self {
            monitoring_interval: Duration::from_secs(5),
            history_retention: Duration::from_secs(3600),
            enable_detailed_monitoring: true,
            enable_predictive_monitoring: false,
            alert_thresholds: AlertThresholds::default(),
            enable_auto_optimization: true,
        }
    }
}

impl Default for AlertThresholds {
    fn default() -> Self {
        Self {
            memory_threshold: 0.8,
            cpu_threshold: 0.9,
            gpu_threshold: 0.9,
            disk_threshold: 0.9,
            temperature_threshold: 75.0,
            power_threshold: 800.0,
        }
    }
}

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

    #[test]
    fn test_resource_monitor_creation() {
        let constraints = ResourceConstraints::default();
        let monitor = ResourceMonitor::<f64>::new(constraints);
        assert_eq!(monitor.get_monitoring_state(), MonitoringState::Stopped);
    }

    #[test]
    fn test_system_resource_tracker() {
        use super::ResourceTracker;

        let mut tracker =
            SystemResourceTracker::new("TestTracker".to_string(), Duration::from_secs(1));

        assert!(<SystemResourceTracker as ResourceTracker<f64>>::initialize(&mut tracker).is_ok());

        let usage = <SystemResourceTracker as ResourceTracker<f64>>::get_current_usage(&tracker);
        assert!(usage.is_ok());

        let metrics = <SystemResourceTracker as ResourceTracker<f64>>::get_system_metrics(&tracker);
        assert!(metrics.is_ok());

        assert!(<SystemResourceTracker as ResourceTracker<f64>>::cleanup(&mut tracker).is_ok());
    }

    #[test]
    fn test_memory_optimizer() {
        let optimizer = MemoryOptimizer::new("TestOptimizer".to_string(), 0.5);

        let usage = ResourceUsage {
            memory_gb: 20.0,
            cpu_time_seconds: 100.0,
            gpu_time_seconds: 50.0,
            energy_kwh: 1.0,
            network_io_gb: 1.0,
            disk_io_gb: 2.0,
            peak_memory_gb: 22.0,
            efficiency_score: 0.8,
            cost_usd: 0.0,
            network_gb: 1.0,
        };

        let constraints = ResourceConstraints::default();
        let action = optimizer.optimize(&usage, &constraints);
        assert!(action.is_ok());
    }

    #[test]
    fn test_alert_handler() {
        let handler = ConsoleAlertHandler::new("TestHandler".to_string(), false);

        let violation = ResourceViolation {
            violation_type: ViolationType::MemoryExceeded,
            current_value: 25.0,
            limit_value: 20.0,
            severity: ViolationSeverity::High,
            violation_time: Instant::now(),
            affected_resources: vec!["Memory".to_string()],
            suggested_actions: vec!["Clear cache".to_string()],
        };

        assert!(handler.handle_violation(&violation).is_ok());
    }

    #[test]
    fn test_trend_calculation() {
        let constraints = ResourceConstraints::default();
        let monitor = ResourceMonitor::<f64>::new(constraints);

        let values = vec![1.0, 1.1, 1.2, 1.3, 1.4];
        let trend = monitor.calculate_metric_trend(values);
        assert!(matches!(trend, TrendDirection::Increasing));

        let stable_values = vec![1.0, 1.0, 1.0, 1.0, 1.0];
        let stable_trend = monitor.calculate_metric_trend(stable_values);
        assert!(matches!(stable_trend, TrendDirection::Stable));
    }
    use super::super::telemetry::FixedTelemetry;

    /// Build a monitor whose single tracker reports exactly `sample`.
    fn monitor_with(
        constraints: ResourceConstraints<f64>,
        sample: TelemetrySample,
    ) -> ResourceMonitor<f64> {
        let mut monitor = ResourceMonitor::<f64>::new(constraints);
        monitor.set_trackers(vec![Box::new(SystemResourceTracker::with_telemetry(
            "fixed".to_string(),
            Duration::from_secs(1),
            Box::new(FixedTelemetry::new("fixed", sample)),
        ))]);
        monitor
    }

    // ---- F12: no fabricated telemetry -----------------------------------

    /// The default tracker must not report the old invented constants, and every
    /// unmeasurable metric must be `None`.
    #[test]
    fn the_default_tracker_reports_no_fabricated_metrics() {
        use super::ResourceTracker;
        let tracker = SystemResourceTracker::new("t".to_string(), Duration::from_secs(1));
        assert_eq!(tracker.telemetry_name(), "std");

        let metrics = <SystemResourceTracker as ResourceTracker<f64>>::get_system_metrics(&tracker)
            .expect("metrics");

        // These were 4 GPUs, 1 TB disk (500 GB free), 1 GB/s, 65 C and 250 W.
        assert_eq!(metrics.available_gpu_devices, None);
        assert_eq!(metrics.available_disk_gb, None);
        assert_eq!(metrics.network_bandwidth, None);
        assert_eq!(metrics.system_temperature, None);
        assert_eq!(metrics.power_consumption, None);
        // CPU count is genuinely measurable.
        assert!(metrics.available_cpu_cores.is_some_and(|cores| cores >= 1));

        let usage = <SystemResourceTracker as ResourceTracker<f64>>::get_current_usage(&tracker)
            .expect("usage");
        // Memory was a fixed 16.0 GB with a 19.2 GB "peak"; now it is either a real
        // measurement or the accumulator identity.
        assert_ne!(usage.memory_gb, 16.0);
        assert_ne!(usage.peak_memory_gb, 19.2);
        assert!(usage.memory_gb >= 0.0 && usage.memory_gb.is_finite());
        // These were 0.25 kWh, 1 GB network, 2 GB disk I/O, 0.8 efficiency, $0.50.
        assert_eq!(usage.energy_kwh, 0.0);
        assert_eq!(usage.network_io_gb, 0.0);
        assert_eq!(usage.disk_io_gb, 0.0);
        assert_eq!(usage.cost_usd, 0.0);
    }

    /// The core F12 hazard: a tight constraint must not abort a search on the
    /// strength of an unmeasured resource.
    #[test]
    fn unknown_measurements_are_treated_as_unlimited() {
        use super::ResourceTracker;
        let tracker = SystemResourceTracker::with_telemetry(
            "blind".to_string(),
            Duration::from_secs(1),
            Box::new(FixedTelemetry::new("blind", TelemetrySample::unknown())),
        );

        // An absurdly tight budget on every axis.
        let mut constraints = ResourceConstraints::<f64>::default();
        constraints.hardware_resources.max_memory_gb = 0.001;
        constraints.max_memory_gb = 0.001;
        constraints.max_computation_hours = 0.0;
        constraints.max_energy_kwh = 0.0;
        constraints.max_cost_usd = 0.0;

        let violations = tracker.check_limits(&constraints).expect("check_limits");
        assert!(
            violations.is_empty(),
            "an unmeasurable resource must never produce a violation, got {:?}",
            violations
                .iter()
                .map(|v| v.violation_type)
                .collect::<Vec<_>>()
        );
    }

    /// A measured value over budget still must be reported — the guard must not
    /// have disabled enforcement altogether.
    #[test]
    fn measured_overruns_are_still_reported() {
        use super::ResourceTracker;
        let sample = TelemetrySample {
            process_memory_gb: Some(9.0),
            temperature_celsius: Some(95.0),
            power_watts: Some(1500.0),
            ..TelemetrySample::unknown()
        };
        let tracker = SystemResourceTracker::with_telemetry(
            "hot".to_string(),
            Duration::from_secs(1),
            Box::new(FixedTelemetry::new("hot", sample)),
        );

        let mut constraints = ResourceConstraints::<f64>::default();
        constraints.hardware_resources.max_memory_gb = 4.0;

        let violations = tracker.check_limits(&constraints).expect("check_limits");
        let kinds: Vec<String> = violations
            .iter()
            .map(|v| format!("{:?}", v.violation_type))
            .collect();
        assert!(kinds.contains(&"MemoryExceeded".to_string()), "{kinds:?}");
        assert!(
            kinds.contains(&"TemperatureExceeded".to_string()),
            "{kinds:?}"
        );
        assert!(kinds.contains(&"PowerExceeded".to_string()), "{kinds:?}");

        // A generous budget produces no memory violation.
        constraints.hardware_resources.max_memory_gb = 64.0;
        let relaxed = tracker.check_limits(&constraints).expect("check_limits");
        assert!(!relaxed
            .iter()
            .any(|v| matches!(v.violation_type, ViolationType::MemoryExceeded)));
    }

    /// `update_usage` must actually record what the tracker measured, and the
    /// snapshot's derived fields must be measured or `None` — never the old
    /// hard-coded `1` / `Low` / `0.6` / `0.8`.
    #[test]
    fn update_usage_records_measured_values_and_no_hardcoded_ones() {
        let sample = TelemetrySample {
            process_memory_gb: Some(2.5),
            total_memory_gb: Some(16.0),
            available_memory_gb: Some(2.0),
            logical_cpus: Some(8),
            load_average_per_cpu: Some(0.375),
            process_count: Some(412),
            ..TelemetrySample::unknown()
        };
        let mut monitor = monitor_with(ResourceConstraints::default(), sample);

        // Nothing is recorded while monitoring is stopped.
        monitor.update_usage().expect("update while stopped");
        assert!(monitor.get_usage_history().is_empty());

        monitor.start_monitoring().expect("start");
        monitor.update_usage().expect("update");

        let usage = monitor.get_current_usage();
        assert_eq!(usage.memory_gb, 2.5, "the measured RSS must be recorded");

        let history = monitor.get_usage_history();
        assert_eq!(history.len(), 1);
        let snapshot = &history[0];
        assert_eq!(snapshot.active_processes, Some(412));
        assert_eq!(snapshot.cpu_load_average, Some(0.375));
        assert_ne!(
            snapshot.cpu_load_average,
            Some(0.6),
            "0.6 was the hardcoded load average"
        );
        assert_eq!(
            snapshot.gpu_utilization, None,
            "GPU utilization is unmeasurable here; 0.8 was fabricated"
        );
        // 14/16 = 87.5% used -> High, derived, not the hardcoded Low.
        assert!(
            matches!(snapshot.memory_pressure, Some(MemoryPressure::High)),
            "got {:?}",
            snapshot.memory_pressure
        );
        assert_eq!(snapshot.system_metrics.available_cpu_cores, Some(8));

        // The summary now reflects real data instead of an untouched default.
        let summary = monitor.get_usage_summary();
        assert_eq!(summary.total_memory_gb, 2.5);
    }

    #[test]
    fn a_blind_monitor_records_a_snapshot_with_everything_unknown() {
        let mut monitor = monitor_with(ResourceConstraints::default(), TelemetrySample::unknown());
        monitor.start_monitoring().expect("start");
        monitor.update_usage().expect("update");

        let history = monitor.get_usage_history();
        assert_eq!(history.len(), 1);
        assert_eq!(history[0].active_processes, None);
        assert_eq!(history[0].memory_pressure, None);
        assert_eq!(history[0].cpu_load_average, None);
        assert_eq!(history[0].gpu_utilization, None);
        assert!(monitor
            .check_resource_violations()
            .expect("violations")
            .is_empty());
    }

    // ---- F22: poisoned locks are recovered, not fatal --------------------

    #[test]
    fn a_poisoned_lock_is_recovered_instead_of_panicking_forever() {
        use std::sync::{Arc, Mutex};

        let shared: Arc<Mutex<u32>> = Arc::new(Mutex::new(7));
        let poisoner = Arc::clone(&shared);
        let handle = std::thread::spawn(move || {
            let _guard = poisoner.lock().expect("first lock succeeds");
            panic!("deliberate panic to poison the mutex");
        });
        assert!(
            handle.join().is_err(),
            "the helper thread must have panicked"
        );
        assert!(shared.is_poisoned(), "the mutex must be poisoned");

        // `lock().expect("lock poisoned")` would panic here forever.
        assert_eq!(*lock_recovering(&shared), 7);
    }

    #[test]
    fn getters_survive_a_poisoned_usage_lock() {
        let sample = TelemetrySample {
            process_memory_gb: Some(1.25),
            ..TelemetrySample::unknown()
        };
        let mut monitor = monitor_with(ResourceConstraints::default(), sample);
        monitor.start_monitoring().expect("start");
        monitor.update_usage().expect("update");

        monitor.poison_locks_for_test();

        // Every getter must still work.
        assert_eq!(monitor.get_current_usage().memory_gb, 1.25);
        assert_eq!(monitor.get_usage_history().len(), 1);
        let report = monitor.generate_report();
        assert_eq!(report.total_samples, 1);
        assert!(monitor.optimize_resources().is_ok());
    }

    // ---- F25: priority ordering ------------------------------------------

    /// An optimizer that always emits an action at a chosen priority, so the sort
    /// direction can be observed. `MemoryOptimizer` alone cannot do this: it emits
    /// either `High` or `Low`, and `optimize_resources` filters `Low` out, so every
    /// surviving action has the same priority and any ordering passes.
    #[derive(Debug)]
    struct FixedPriorityOptimizer {
        name: String,
        priority: OptimizationPriority,
    }

    impl ResourceOptimizer<f64> for FixedPriorityOptimizer {
        fn optimize(
            &self,
            _current_usage: &ResourceUsage<f64>,
            _constraints: &ResourceConstraints<f64>,
        ) -> Result<OptimizationAction<f64>> {
            Ok(OptimizationAction {
                action_type: ActionType::ClearCaches,
                parameters: HashMap::new(),
                expected_savings: ResourceUsage::default(),
                implementation_cost: 0.0,
                description: format!("{} action", self.name),
                priority: self.priority,
            })
        }

        fn name(&self) -> &str {
            &self.name
        }

        fn priority(&self) -> OptimizationPriority {
            self.priority
        }
    }

    #[test]
    fn optimization_actions_are_returned_most_urgent_first() {
        let mut monitor = monitor_with(ResourceConstraints::default(), TelemetrySample::unknown());
        // Registered in an order that an ascending sort would preserve and a
        // descending sort must reverse.
        monitor.set_optimization_strategies(vec![
            Box::new(FixedPriorityOptimizer {
                name: "medium".to_string(),
                priority: OptimizationPriority::Medium,
            }),
            Box::new(FixedPriorityOptimizer {
                name: "critical".to_string(),
                priority: OptimizationPriority::Critical,
            }),
            Box::new(FixedPriorityOptimizer {
                name: "high".to_string(),
                priority: OptimizationPriority::High,
            }),
        ]);
        monitor.start_monitoring().expect("start");
        monitor.update_usage().expect("update");

        let actions = monitor.optimize_resources().expect("optimize");
        let priorities: Vec<OptimizationPriority> =
            actions.iter().map(|action| action.priority).collect();
        assert_eq!(
            priorities,
            vec![
                OptimizationPriority::Critical,
                OptimizationPriority::High,
                OptimizationPriority::Medium
            ],
            "the previous ascending `sort_by_key(|a| a.priority)` yielded \
             [Medium, High, Critical] — least urgent first"
        );
    }

    #[test]
    fn optimization_can_be_disabled() {
        let mut monitor = monitor_with(ResourceConstraints::default(), TelemetrySample::unknown());
        monitor.set_optimization_strategies(vec![Box::new(FixedPriorityOptimizer {
            name: "critical".to_string(),
            priority: OptimizationPriority::Critical,
        })]);
        monitor.monitoring_config.enable_auto_optimization = false;
        assert!(monitor.optimize_resources().expect("optimize").is_empty());
    }
}