scirs2-core 0.6.5

Core utilities and common functionality for SciRS2 (scirs2-core)
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
//! Advanced implementations for the stability framework
//!
//! This module contains the implementation details for formal verification,
//! runtime validation, performance modeling, and cryptographic audit trails.

use super::*;
use crate::performance_optimization::PerformanceMetrics;
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;

/// A concrete, runnable measurement of the API under verification.
///
/// [`FormalVerificationEngine::verify_contract`] cannot confirm a
/// contract's performance/memory/thread-safety bounds against nothing:
/// those are runtime properties of actually *calling* the API. Without a
/// probe, verification honestly reports [`VerificationStatus::NotVerified`]
/// (via a `verified: false` [`VerificationResult`]) rather than fabricating
/// a pass.
pub struct VerificationProbe {
    /// Invokes the API under test once, returning the wall-clock duration
    /// of the call and, if determinable, the net memory-usage delta caused
    /// by the call (in bytes). Measuring a memory delta is inherently
    /// caller-specific (e.g. via an allocator hook or process RSS sample),
    /// so `None` is an honest "not measured" rather than a fabricated zero.
    pub invoke: Box<dyn Fn() -> (Duration, Option<usize>) + Send + Sync>,
    /// Number of sequential invocations used to build the timing/memory
    /// sample; the *maximum* observed value is compared against the
    /// contract (a conservative choice for a safety bound).
    pub iterations: usize,
    /// Number of concurrent threads used for the thread-safety smoke test
    /// when the contract claims `ThreadSafety::ThreadSafe`. `0` or `1`
    /// disables the concurrent check.
    pub concurrency: usize,
}

impl VerificationProbe {
    /// Convenience constructor for a single-threaded, single-iteration
    /// probe that does not report a memory measurement.
    pub fn from_timing(invoke: impl Fn() + Send + Sync + 'static) -> Self {
        Self {
            invoke: Box::new(move || {
                let start = Instant::now();
                invoke();
                (start.elapsed(), None)
            }),
            iterations: 1,
            concurrency: 1,
        }
    }
}

/// Best-effort extraction of a human-readable message from a caught panic
/// payload (as produced by `std::panic::catch_unwind`).
fn panic_payload_to_string(payload: &(dyn std::any::Any + Send)) -> String {
    if let Some(s) = payload.downcast_ref::<&str>() {
        (*s).to_string()
    } else if let Some(s) = payload.downcast_ref::<String>() {
        s.clone()
    } else {
        "<non-string panic payload>".to_string()
    }
}

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

impl FormalVerificationEngine {
    /// Create a new formal verification engine
    pub fn new() -> Self {
        Self {
            verification_tasks: Arc::new(Mutex::new(HashMap::new())),
            results_cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Start formal verification for an API contract.
    ///
    /// `probe` supplies a real, runnable measurement of the API under
    /// test. Without one, none of the contract's performance/memory/
    /// thread-safety bounds can be honestly confirmed, so verification
    /// completes with `VerificationStatus::Failed` and a `verified: false`
    /// result explaining why — never a fabricated `Verified`. With a
    /// probe, the bounds declared in `contract` are checked against real
    /// measurements (see the private `perform_verification` method below).
    pub fn verify_contract(
        &self,
        contract: &ApiContract,
        probe: Option<VerificationProbe>,
    ) -> CoreResult<()> {
        let taskid = format!("{}-{}", contract.module, contract.apiname);

        let properties = self.extract_verification_properties(contract);

        let task = VerificationTask {
            apiname: contract.apiname.clone(),
            module: contract.module.clone(),
            properties: properties.clone(),
            status: VerificationStatus::InProgress,
            started_at: Instant::now(),
        };

        {
            let mut tasks = self.verification_tasks.lock().expect("Operation failed");
            tasks.insert(taskid.clone(), task);
        }

        // Spawn a background thread so a slow probe (many iterations, or a
        // concurrent thread-safety smoke test) never blocks the caller.
        let tasks_clone = Arc::clone(&self.verification_tasks);
        let results_clone = Arc::clone(&self.results_cache);
        let performance = contract.performance.clone();
        let memory = contract.memory.clone();
        let thread_safety = contract.concurrency.thread_safety;

        thread::spawn(move || {
            let result = Self::perform_verification(
                &properties,
                &performance,
                &memory,
                thread_safety,
                probe,
            );
            let verified = result.verified;

            // Store result
            {
                let mut results = results_clone.write().expect("Operation failed");
                results.insert(taskid.clone(), result);
            }

            // Update task status: honestly reflect whether verification
            // actually confirmed the contract, rather than always marking
            // `Verified`.
            {
                let mut tasks = tasks_clone.lock().expect("Operation failed");
                if let Some(task) = tasks.get_mut(&taskid) {
                    task.status = if verified {
                        VerificationStatus::Verified
                    } else {
                        VerificationStatus::Failed
                    };
                }
            }
        });

        Ok(())
    }

    /// Extract verification properties from contract
    fn extract_verification_properties(&self, contract: &ApiContract) -> Vec<VerificationProperty> {
        let mut properties = Vec::new();

        // Performance properties
        properties.push(VerificationProperty {
            name: "performance_bound".to_string(),
            specification: format!(
                "execution_time <= {:?}",
                contract
                    .performance
                    .maxexecution_time
                    .unwrap_or(Duration::from_secs(1))
            ),
            property_type: PropertyType::Safety,
        });

        // Memory properties
        if let Some(max_memory) = contract.memory.max_memory {
            properties.push(VerificationProperty {
                name: "memory_bound".to_string(),
                specification: format!("memory_usage <= {max_memory}"),
                property_type: PropertyType::Safety,
            });
        }

        // Thread safety properties
        if contract.concurrency.thread_safety == ThreadSafety::ThreadSafe {
            properties.push(VerificationProperty {
                name: "thread_safety".to_string(),
                specification: "no_race_conditions AND no_deadlocks".to_string(),
                property_type: PropertyType::Safety,
            });
        }

        properties
    }

    /// Get verification status for an API
    pub fn get_verification_status(&self, apiname: &str, module: &str) -> VerificationStatus {
        let taskid = format!("{module}-{apiname}");

        if let Ok(tasks) = self.verification_tasks.lock() {
            if let Some(task) = tasks.get(&taskid) {
                return task.status;
            }
        }

        VerificationStatus::NotVerified
    }

    /// Get all verification results
    pub fn get_all_results(&self) -> HashMap<String, VerificationResult> {
        if let Ok(results) = self.results_cache.read() {
            results.clone()
        } else {
            HashMap::new()
        }
    }

    /// Check if verification is complete for an API
    pub fn is_verification_complete(&self, apiname: &str, module: &str) -> bool {
        matches!(
            self.get_verification_status(apiname, module),
            VerificationStatus::Verified | VerificationStatus::Failed
        )
    }

    /// Get verification coverage percentage
    pub fn get_verification_coverage(&self) -> f64 {
        if let Ok(tasks) = self.verification_tasks.lock() {
            if tasks.is_empty() {
                return 0.0;
            }

            let verified_count = tasks
                .values()
                .filter(|task| task.status == VerificationStatus::Verified)
                .count();

            (verified_count as f64 / tasks.len() as f64) * 100.0
        } else {
            0.0
        }
    }

    /// Actually check `properties` against real measurements taken from
    /// `probe`.
    ///
    /// Without a probe, nothing was executed, so none of the declared
    /// bounds can be confirmed: this honestly returns `verified: false`
    /// with an explanatory counterexample rather than the previous
    /// unconditional `verified: true`. This crate has no formal-methods
    /// backend (no CBMC/KLEE/model-checker integration, and adding one is
    /// out of scope for a general-purpose core utility crate); what *is*
    /// tractable — and implemented here — is checking the contract's own
    /// numeric bounds against a real invocation of the API.
    fn perform_verification(
        properties: &[VerificationProperty],
        performance: &PerformanceContract,
        memory: &MemoryContract,
        thread_safety: ThreadSafety,
        probe: Option<VerificationProbe>,
    ) -> VerificationResult {
        let start_time = Instant::now();

        let Some(probe) = probe else {
            return VerificationResult {
                verified: false,
                verification_time: start_time.elapsed(),
                checked_properties: vec![],
                counterexample: Some(format!(
                    "no VerificationProbe was supplied: {} declared propert{} could not be \
                     executed against a real workload and so cannot be confirmed",
                    properties.len(),
                    if properties.len() == 1 { "y" } else { "ies" }
                )),
                method: VerificationMethod::StaticAnalysis,
            };
        };

        // Measure the real API under test. A probe that panics is itself a
        // genuine finding (the API under test is not safe to call) and
        // must be caught here: left uncaught, it would unwind straight out
        // of the background thread `verify_contract` spawned to run this
        // function, which would silently abandon the task in `InProgress`
        // forever (nothing left running to ever update its status).
        let iterations = probe.iterations.max(1);
        let mut max_elapsed = Duration::ZERO;
        let mut max_memory_delta: Option<usize> = None;
        let mut probe_panic_message: Option<String> = None;
        for _ in 0..iterations {
            match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (probe.invoke)())) {
                Ok((elapsed, memory_delta)) => {
                    max_elapsed = max_elapsed.max(elapsed);
                    if let Some(delta) = memory_delta {
                        max_memory_delta =
                            Some(max_memory_delta.map_or(delta, |current| current.max(delta)));
                    }
                }
                Err(payload) => {
                    probe_panic_message = Some(panic_payload_to_string(&payload));
                    break;
                }
            }
        }

        if let Some(message) = probe_panic_message {
            return VerificationResult {
                verified: false,
                verification_time: start_time.elapsed(),
                checked_properties: vec![],
                counterexample: Some(format!(
                    "the supplied probe panicked during measurement: {message}"
                )),
                method: VerificationMethod::SymbolicExecution,
            };
        }

        let mut checked_properties = Vec::new();
        let mut counterexamples = Vec::new();

        for property in properties {
            match property.name.as_str() {
                "performance_bound" => {
                    checked_properties.push(property.name.clone());
                    if let Some(bound) = performance.maxexecution_time {
                        if max_elapsed > bound {
                            counterexamples.push(format!(
                                "performance_bound violated: measured max execution time \
                                 {max_elapsed:?} exceeds the contract bound {bound:?} (over \
                                 {iterations} iteration(s))"
                            ));
                        }
                    }
                }
                "memory_bound" => {
                    checked_properties.push(property.name.clone());
                    match (memory.max_memory, max_memory_delta) {
                        (Some(bound), Some(delta)) if delta > bound => {
                            counterexamples.push(format!(
                                "memory_bound violated: measured memory delta {delta} bytes \
                                 exceeds the contract bound {bound} bytes"
                            ));
                        }
                        (Some(_), None) => {
                            counterexamples.push(
                                "memory_bound could not be confirmed: the supplied probe never \
                                 reported a memory measurement"
                                    .to_string(),
                            );
                        }
                        _ => {}
                    }
                }
                "thread_safety" => {
                    checked_properties.push(property.name.clone());
                    if thread_safety == ThreadSafety::ThreadSafe && probe.concurrency > 1 {
                        if let Some(failure) = Self::concurrent_smoke_test(&probe) {
                            counterexamples.push(failure);
                        }
                    }
                }
                _ => {}
            }
        }

        let verified = counterexamples.is_empty();
        VerificationResult {
            verified,
            verification_time: start_time.elapsed(),
            checked_properties,
            counterexample: if counterexamples.is_empty() {
                None
            } else {
                Some(counterexamples.join("; "))
            },
            // `SymbolicExecution` is the closest available label for "the
            // API was actually invoked and measured" (as opposed to the
            // purely-static techniques in this enum); it is concrete
            // rather than symbolic execution, but no better-fitting
            // variant exists.
            method: VerificationMethod::SymbolicExecution,
        }
    }

    /// Runs `probe.invoke` concurrently across `probe.concurrency` threads
    /// (each performing `probe.iterations` calls) and reports a failure
    /// message if any worker panics — a real, if coarse, signal that a
    /// `ThreadSafe` claim does not hold — or if the workers do not all
    /// complete within a generous timeout (a suspected deadlock/hang).
    fn concurrent_smoke_test(probe: &VerificationProbe) -> Option<String> {
        const TIMEOUT: Duration = Duration::from_secs(10);

        let concurrency = probe.concurrency.max(1);
        let iterations_per_thread = probe.iterations.max(1);
        let invoke = &probe.invoke;
        let panic_count = AtomicUsize::new(0);
        let (tx, rx) = mpsc::channel::<()>();

        thread::scope(|scope| {
            for _ in 0..concurrency {
                let tx = tx.clone();
                let panic_count = &panic_count;
                scope.spawn(move || {
                    for _ in 0..iterations_per_thread {
                        let outcome =
                            std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                                (invoke)();
                            }));
                        if outcome.is_err() {
                            panic_count.fetch_add(1, Ordering::SeqCst);
                        }
                    }
                    let _ = tx.send(());
                });
            }
            drop(tx);

            let deadline = Instant::now() + TIMEOUT;
            let mut completed = 0usize;
            while completed < concurrency {
                let remaining = deadline.saturating_duration_since(Instant::now());
                if remaining.is_zero() {
                    break;
                }
                match rx.recv_timeout(remaining) {
                    Ok(()) => completed += 1,
                    Err(_) => break,
                }
            }

            if completed < concurrency {
                return Some(format!(
                    "thread_safety smoke test timed out: only {completed}/{concurrency} worker \
                     thread(s) completed within {TIMEOUT:?} (suspected deadlock/hang)"
                ));
            }

            let panics = panic_count.load(Ordering::SeqCst);
            if panics > 0 {
                return Some(format!(
                    "thread_safety smoke test failed: {panics} panic(s) observed across \
                     {concurrency} concurrent thread(s) x {iterations_per_thread} iteration(s)"
                ));
            }

            None
        })
    }
}

impl RuntimeContractValidator {
    /// Create a new runtime contract validator
    pub fn new() -> (Self, Receiver<MonitoringEvent>) {
        let (sender, receiver) = mpsc::channel();

        let validator = Self {
            contracts: Arc::new(RwLock::new(HashMap::new())),
            event_sender: sender,
            stats: Arc::new(Mutex::new(ValidationStatistics {
                total_validations: 0,
                violations_detected: 0,
                avg_validation_time: Duration::from_nanos(0),
                success_rate: 1.0,
            })),
            chaos_controller: Arc::new(Mutex::new(ChaosEngineeringController {
                enabled: false,
                faultprobability: 0.01,
                active_faults: Vec::new(),
                fault_history: Vec::new(),
            })),
        };

        (validator, receiver)
    }

    /// Register a contract for runtime validation
    pub fn register_contract(&self, contract: ApiContract) {
        let key = format!("{}-{}", contract.module, contract.apiname);

        if let Ok(mut contracts) = self.contracts.write() {
            contracts.insert(key, contract);
        }
    }

    /// Validate API call against contract in real-time
    pub fn validate_api_call(
        &self,
        apiname: &str,
        module: &str,
        context: &ApiCallContext,
    ) -> CoreResult<()> {
        let start_time = Instant::now();
        let key = format!("{module}-{apiname}");

        // Update statistics
        {
            if let Ok(mut stats) = self.stats.lock() {
                stats.total_validations += 1;
            }
        }

        // Inject chaos if enabled
        self.maybe_inject_fault(apiname, module)?;

        // Get contract
        let contract = {
            if let Ok(contracts) = self.contracts.read() {
                contracts.get(&key).cloned()
            } else {
                return Err(CoreError::ValidationError(ErrorContext::new(
                    "Cannot access contracts for validation".to_string(),
                )));
            }
        };

        let contract = contract.ok_or_else(|| {
            CoreError::ValidationError(ErrorContext::new(format!(
                "No contract found for {module}::{apiname}"
            )))
        })?;

        // Validate performance contract
        if let Some(max_time) = contract.performance.maxexecution_time {
            if context.execution_time > max_time {
                self.report_violation(
                    apiname,
                    module,
                    ContractViolation {
                        violation_type: ViolationType::Performance,
                        expected: format!("{max_time:?}"),
                        actual: format!("{:?}", context.execution_time),
                        severity: ViolationSeverity::High,
                    },
                )?;
            }
        }

        // Validate memory contract
        if let Some(max_memory) = contract.memory.max_memory {
            if context.memory_usage > max_memory {
                self.report_violation(
                    apiname,
                    module,
                    ContractViolation {
                        violation_type: ViolationType::Memory,
                        expected: format!("{max_memory}"),
                        actual: context.memory_usage.to_string(),
                        severity: ViolationSeverity::Medium,
                    },
                )?;
            }
        }

        // Update statistics
        let validation_time = start_time.elapsed();
        {
            if let Ok(mut stats) = self.stats.lock() {
                let total = stats.total_validations as f64;
                let prev_avg = stats.avg_validation_time.as_nanos() as f64;
                let new_avg =
                    (prev_avg * (total - 1.0) + validation_time.as_nanos() as f64) / total;
                stats.avg_validation_time = Duration::from_nanos(new_avg as u64);
                stats.success_rate = (total - stats.violations_detected as f64) / total;
            }
        }

        Ok(())
    }

    /// Enable chaos engineering
    pub fn enable_chaos_engineering(&self, faultprobability: f64) {
        if let Ok(mut controller) = self.chaos_controller.lock() {
            controller.enabled = true;
            controller.faultprobability = faultprobability.clamp(0.0, 1.0);
        }
    }

    /// Maybe inject a chaos fault
    fn maybe_inject_fault(&self, apiname: &str, module: &str) -> CoreResult<()> {
        if let Ok(mut controller) = self.chaos_controller.lock() {
            if !controller.enabled {
                return Ok(());
            }

            // Generate random number for fault probability
            let mut hasher = DefaultHasher::new();
            apiname.hash(&mut hasher);
            module.hash(&mut hasher);
            SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos()
                .hash(&mut hasher);

            let rand_val = (hasher.finish() % 10000) as f64 / 10000.0;

            if rand_val < controller.faultprobability {
                // Inject a random fault
                let fault = match rand_val * 4.0 {
                    x if x < 1.0 => ChaosFault::LatencyInjection(Duration::from_millis(100)),
                    x if x < 2.0 => ChaosFault::MemoryPressure(1024 * 1024), // 1MB
                    x if x < 3.0 => ChaosFault::CpuThrottling(0.5),
                    _ => ChaosFault::RandomFailure(0.1),
                };

                controller.active_faults.push(fault.clone());
                controller
                    .fault_history
                    .push((Instant::now(), fault.clone()));

                // Send monitoring event
                let event = MonitoringEvent {
                    timestamp: Instant::now(),
                    apiname: apiname.to_string(),
                    module: module.to_string(),
                    event_type: MonitoringEventType::ChaosEngineeringFault(fault.clone()),
                    performance_metrics: RuntimePerformanceMetrics {
                        execution_time: Duration::from_nanos(0),
                        memory_usage: 0,
                        cpu_usage: 0.0,
                        cache_hit_rate: 0.0,
                        thread_count: 1,
                    },
                    thread_id: format!("{:?}", thread::current().id()),
                };

                let _ = self.event_sender.send(event);

                // Actually inject the fault
                match fault {
                    ChaosFault::LatencyInjection(delay) => {
                        thread::sleep(delay);
                    }
                    ChaosFault::RandomFailure(prob) if rand_val < prob => {
                        return Err(CoreError::ValidationError(ErrorContext::new(
                            "Chaos engineering: Random failure injected".to_string(),
                        )));
                    }
                    _ => {} // Other faults would require system-level intervention
                }
            }
        }

        Ok(())
    }

    /// Report a contract violation
    fn report_violation(
        &self,
        apiname: &str,
        module: &str,
        violation: ContractViolation,
    ) -> CoreResult<()> {
        // Update statistics
        {
            if let Ok(mut stats) = self.stats.lock() {
                stats.violations_detected += 1;
                let total = stats.total_validations as f64;
                stats.success_rate = (total - stats.violations_detected as f64) / total;
            }
        }

        // Send monitoring event
        let event = MonitoringEvent {
            timestamp: Instant::now(),
            apiname: apiname.to_string(),
            module: module.to_string(),
            event_type: MonitoringEventType::ContractViolation(violation.clone()),
            performance_metrics: RuntimePerformanceMetrics {
                execution_time: Duration::from_nanos(0),
                memory_usage: 0,
                cpu_usage: 0.0,
                cache_hit_rate: 0.0,
                thread_count: 1,
            },
            thread_id: format!("{:?}", thread::current().id()),
        };

        let _ = self.event_sender.send(event);

        // Return error for critical violations
        if violation.severity >= ViolationSeverity::High {
            return Err(CoreError::ValidationError(ErrorContext::new(format!(
                "Critical contract violation in {}::{}: {} (expected: {}, actual: {})",
                module,
                apiname,
                match violation.violation_type {
                    ViolationType::Performance => "Performance",
                    ViolationType::Memory => "Memory",
                    ViolationType::Numerical => "Numerical",
                    ViolationType::Concurrency => "Concurrency",
                    ViolationType::Behavioral => "Behavioral",
                },
                violation.expected,
                violation.actual
            ))));
        }

        Ok(())
    }

    /// Get validation statistics
    pub fn get_statistics(&self) -> Option<ValidationStatistics> {
        self.stats.lock().ok().map(|stats| stats.clone())
    }

    /// Get chaos engineering status
    pub fn get_chaos_status(&self) -> Option<(bool, f64, usize)> {
        if let Ok(controller) = self.chaos_controller.lock() {
            Some((
                controller.enabled,
                controller.faultprobability,
                controller.fault_history.len(),
            ))
        } else {
            None
        }
    }

    /// Disable chaos engineering
    pub fn disable_chaos_engineering(&self) {
        if let Ok(mut controller) = self.chaos_controller.lock() {
            controller.enabled = false;
            controller.active_faults.clear();
        }
    }
}

/// API call context for runtime validation
#[derive(Debug, Clone)]
pub struct ApiCallContext {
    /// Execution time of the call
    pub execution_time: Duration,
    /// Memory usage during the call
    pub memory_usage: usize,
    /// Input parameters hash
    pub input_hash: String,
    /// Output parameters hash
    pub output_hash: String,
    /// Thread ID where call occurred
    pub thread_id: String,
}

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

impl AdvancedPerformanceModeler {
    /// Create a new performance modeler
    pub fn new() -> Self {
        Self {
            performance_history: Arc::new(RwLock::new(Vec::new())),
            prediction_models: Arc::new(RwLock::new(HashMap::new())),
            training_status: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Record a performance measurement
    pub fn record_measurement(
        &self,
        apiname: &str,
        input_characteristics: InputCharacteristics,
        performance: PerformanceMetrics,
        system_state: SystemState,
    ) {
        // Convert PerformanceMetrics to RuntimePerformanceMetrics
        let runtime_performance = RuntimePerformanceMetrics {
            execution_time: Duration::from_secs_f64(
                performance.operation_times.values().sum::<f64>()
                    / performance.operation_times.len().max(1) as f64,
            ),
            memory_usage: 0, // Not available in PerformanceMetrics
            cpu_usage: 0.0,  // Not available in PerformanceMetrics
            cache_hit_rate: performance.cache_hit_rate,
            thread_count: 1, // Default value
        };

        let data_point = PerformanceDataPoint {
            timestamp: Instant::now(),
            apiname: apiname.to_string(),
            input_characteristics,
            performance: runtime_performance,
            system_state,
        };

        if let Ok(mut history) = self.performance_history.write() {
            history.push(data_point);

            // Limit history size to prevent unbounded growth
            if history.len() > 10000 {
                history.remove(0);
            }
        }

        // Trigger model retraining if enough new data
        self.maybe_retrain_model(apiname);
    }

    /// Predict performance for given input characteristics
    pub fn predict_performance(
        &self,
        apiname: &str,
        input_characteristics: InputCharacteristics,
        system_state: &SystemState,
    ) -> Option<RuntimePerformanceMetrics> {
        if let Ok(models) = self.prediction_models.read() {
            if let Some(model) = models.get(apiname) {
                // Simplified prediction based on input size and model parameters
                let base_time = Duration::from_nanos(1000);
                let size_factor = match model.model_type {
                    ModelType::LinearRegression => {
                        // Use linear model: slope * x + intercept
                        if model.parameters.len() >= 2 {
                            model.parameters[0] * input_characteristics.size as f64
                                + model.parameters[1]
                        } else {
                            (input_characteristics.size as f64).sqrt()
                        }
                    }
                    ModelType::PolynomialRegression => (input_characteristics.size as f64).sqrt(),
                    _ => (input_characteristics.size as f64).sqrt(),
                };

                let scaled_time = Duration::from_nanos(
                    (base_time.as_nanos() as f64 * size_factor.max(1.0)) as u64,
                );

                return Some(RuntimePerformanceMetrics {
                    execution_time: scaled_time,
                    memory_usage: input_characteristics.size * 8, // Assume 8 bytes per element
                    cpu_usage: system_state.cpu_utilization * 1.1, // Slightly higher
                    cache_hit_rate: 0.8,                          // Assume good cache performance
                    thread_count: 1,
                });
            }
        }

        None
    }

    /// Maybe retrain model if conditions are met
    fn maybe_retrain_model(&self, apiname: &str) {
        // Check if enough new data points exist
        let should_retrain = {
            if let Ok(history) = self.performance_history.read() {
                let api_data_points = history.iter().filter(|dp| dp.apiname == apiname).count();
                api_data_points > 100 && (api_data_points % 50 == 0)
            } else {
                false
            }
        };

        if should_retrain {
            self.train_model(apiname);
        }
    }

    /// Train a performance prediction model
    fn train_model(&self, apiname: &str) {
        // Set training status
        {
            if let Ok(mut status) = self.training_status.lock() {
                status.insert(apiname.to_string(), TrainingStatus::InProgress);
            }
        }

        let apiname = apiname.to_string();
        let history_clone = Arc::clone(&self.performance_history);
        let models_clone = Arc::clone(&self.prediction_models);
        let status_clone = Arc::clone(&self.training_status);

        // Spawn training thread
        thread::spawn(move || {
            let training_data = {
                if let Ok(history) = history_clone.read() {
                    history
                        .iter()
                        .filter(|dp| dp.apiname == apiname)
                        .cloned()
                        .collect::<Vec<_>>()
                } else {
                    Vec::new()
                }
            };

            if training_data.len() < 10 {
                // Not enough data
                if let Ok(mut status) = status_clone.lock() {
                    status.insert(apiname.clone(), TrainingStatus::Failed);
                }
                return;
            }

            // Simple linear regression model (simplified)
            let mut sum_x = 0.0;
            let mut sum_y = 0.0;
            let mut sum_xy = 0.0;
            let mut sum_x2 = 0.0;
            let n = training_data.len() as f64;

            for dp in &training_data {
                let x = dp.input_characteristics.size as f64;
                let y = dp.performance.execution_time.as_nanos() as f64;

                sum_x += x;
                sum_y += y;
                sum_xy += x * y;
                sum_x2 += x * x;
            }

            let slope = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x * sum_x);
            let intercept = (sum_y - slope * sum_x) / n;

            // Calculate accuracy (R-squared)
            let y_mean = sum_y / n;
            let mut ss_tot = 0.0;
            let mut ss_res = 0.0;

            for dp in &training_data {
                let x = dp.input_characteristics.size as f64;
                let y = dp.performance.execution_time.as_nanos() as f64;
                let y_pred = slope * x + intercept;

                ss_tot += (y - y_mean).powi(2);
                ss_res += (y - y_pred).powi(2);
            }

            let r_squared = if ss_tot > 0.0 {
                1.0 - (ss_res / ss_tot)
            } else {
                0.0
            };

            let model = PerformancePredictionModel {
                model_type: ModelType::LinearRegression,
                parameters: vec![slope, intercept],
                accuracy: r_squared.clamp(0.0, 1.0),
                training_data_size: training_data.len(),
                last_updated: Instant::now(),
            };

            // Store the trained model
            {
                if let Ok(mut models) = models_clone.write() {
                    models.insert(apiname.clone(), model);
                }
            }

            // Update training status
            {
                if let Ok(mut status) = status_clone.lock() {
                    status.insert(apiname, TrainingStatus::Completed);
                }
            }
        });
    }

    /// Get training status for an API
    pub fn get_training_status(&self, apiname: &str) -> TrainingStatus {
        if let Ok(status) = self.training_status.lock() {
            status
                .get(apiname)
                .copied()
                .unwrap_or(TrainingStatus::NotStarted)
        } else {
            TrainingStatus::NotStarted
        }
    }

    /// Get model accuracy for an API
    pub fn get_model_accuracy(&self, apiname: &str) -> Option<f64> {
        if let Ok(models) = self.prediction_models.read() {
            models.get(apiname).map(|model| model.accuracy)
        } else {
            None
        }
    }

    /// Get number of data points for an API
    pub fn get_data_point_count(&self, apiname: &str) -> usize {
        if let Ok(history) = self.performance_history.read() {
            history.iter().filter(|dp| dp.apiname == apiname).count()
        } else {
            0
        }
    }
}

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

impl ImmutableAuditTrail {
    /// Create a new immutable audit trail
    pub fn new() -> Self {
        Self {
            audit_chain: Arc::new(RwLock::new(Vec::new())),
            current_hash: Arc::new(RwLock::new(0.to_string())),
        }
    }

    /// Add a new audit record
    pub fn add_record(&self, data: AuditData) -> CoreResult<()> {
        let timestamp = SystemTime::now();

        let previous_hash = {
            if let Ok(hash) = self.current_hash.read() {
                hash.clone()
            } else {
                return Err(CoreError::ValidationError(ErrorContext::new(
                    "Cannot access current hash".to_string(),
                )));
            }
        };

        // Create record
        let mut record = AuditRecord {
            timestamp,
            previous_hash: previous_hash.clone(),
            data,
            signature: String::new(), // Would be populated by digital signature
            record_hash: String::new(),
        };

        // Calculate record hash
        record.record_hash = self.calculate_record_hash(&record);

        // Add digital signature (simplified)
        record.signature = record.record_hash.to_string();

        // Add to chain
        {
            if let Ok(mut chain) = self.audit_chain.write() {
                chain.push(record.clone());
            } else {
                return Err(CoreError::ValidationError(ErrorContext::new(
                    "Cannot access audit chain".to_string(),
                )));
            }
        }

        // Update current hash
        {
            if let Ok(mut hash) = self.current_hash.write() {
                *hash = record.record_hash;
            }
        }

        Ok(())
    }

    /// Calculate cryptographic hash of a record
    fn calculate_record_hash(&self, record: &AuditRecord) -> String {
        let mut hasher = DefaultHasher::new();

        record
            .timestamp
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos()
            .hash(&mut hasher);
        record.previous_hash.hash(&mut hasher);

        // Hash the data (simplified)
        match &record.data {
            AuditData::ContractRegistration(name) => name.hash(&mut hasher),
            AuditData::ContractValidation {
                apiname,
                module,
                result,
            } => {
                apiname.hash(&mut hasher);
                module.hash(&mut hasher);
                result.hash(&mut hasher);
            }
            AuditData::PerformanceMeasurement {
                apiname,
                module,
                metrics,
            } => {
                apiname.hash(&mut hasher);
                module.hash(&mut hasher);
                metrics.hash(&mut hasher);
            }
            AuditData::ViolationDetection {
                apiname,
                module,
                violation,
            } => {
                apiname.hash(&mut hasher);
                module.hash(&mut hasher);
                violation.hash(&mut hasher);
            }
        }

        format!("{:x}", hasher.finish())
    }

    /// Verify the integrity of the audit trail
    pub fn verify_integrity(&self) -> bool {
        if let Ok(chain) = self.audit_chain.read() {
            if chain.is_empty() {
                return true;
            }

            for (i, record) in chain.iter().enumerate() {
                // Verify hash
                let expected_hash = self.calculate_record_hash(record);
                if record.record_hash != expected_hash {
                    return false;
                }

                // Verify chain linkage
                if i > 0 {
                    let prev_record = &chain[i.saturating_sub(1)];
                    if record.previous_hash != prev_record.record_hash {
                        return false;
                    }
                }
            }

            true
        } else {
            false
        }
    }

    /// Get audit trail length
    pub fn len(&self) -> usize {
        if let Ok(chain) = self.audit_chain.read() {
            chain.len()
        } else {
            0
        }
    }

    /// Check if audit trail is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get recent audit records
    pub fn get_recent_records(&self, count: usize) -> Vec<AuditRecord> {
        if let Ok(chain) = self.audit_chain.read() {
            let start = chain.len().saturating_sub(count);
            chain[start..].to_vec()
        } else {
            Vec::new()
        }
    }

    /// Export audit trail for external verification
    #[cfg(feature = "serialization")]
    pub fn export_trail(&self) -> CoreResult<String> {
        if let Ok(chain) = self.audit_chain.read() {
            serde_json::to_string_pretty(&*chain).map_err(|e| {
                CoreError::ValidationError(ErrorContext::new(format!(
                    "Failed to serialize audit trail: {e}"
                )))
            })
        } else {
            Err(CoreError::ValidationError(ErrorContext::new(
                "Cannot access audit chain for export".to_string(),
            )))
        }
    }

    /// Export audit trail for external verification (fallback without serialization)
    #[cfg(not(feature = "serialization"))]
    pub fn export_trail(&self) -> CoreResult<String> {
        Err(CoreError::ValidationError(ErrorContext::new(
            "Audit trail export requires serialization feature".to_string(),
        )))
    }
}

// Helper implementations for public structs
impl InputCharacteristics {
    /// Create new input characteristics
    pub fn new(size: usize, datatype: String) -> Self {
        Self {
            size,
            datatype,
            memory_layout: "contiguous".to_string(),
            access_pattern: "sequential".to_string(),
        }
    }

    /// Create characteristics for matrix operations
    pub fn matrix(rows: usize, cols: usize) -> Self {
        Self {
            size: rows * cols,
            datatype: "f64".to_string(),
            memory_layout: "row_major".to_string(),
            access_pattern: "matrix".to_string(),
        }
    }

    /// Create characteristics for vector operations
    pub fn vector(length: usize) -> Self {
        Self {
            size: length,
            datatype: "f64".to_string(),
            memory_layout: "contiguous".to_string(),
            access_pattern: "sequential".to_string(),
        }
    }
}

impl SystemState {
    /// Create new system state
    pub fn new() -> Self {
        Self {
            cpu_utilization: 0.5,    // Default 50%
            memory_utilization: 0.6, // Default 60%
            io_load: 0.1,            // Default low
            network_load: 0.05,      // Default very low
            temperature: 65.0,       // Default temperature in Celsius
        }
    }

    /// Create system state from current system metrics (simplified)
    pub fn current() -> Self {
        // In a real implementation, this would query actual system metrics
        Self::new()
    }
}

impl Default for InputCharacteristics {
    fn default() -> Self {
        Self::new(1000, "f64".to_string())
    }
}

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

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

    /// Build a minimal `ApiContract` for verification tests.
    fn make_contract(
        apiname: &str,
        module: &str,
        max_exec: Option<Duration>,
        max_memory: Option<usize>,
        thread_safe: bool,
    ) -> ApiContract {
        ApiContract {
            apiname: apiname.to_string(),
            module: module.to_string(),
            contract_hash: "test_hash".to_string(),
            created_at: SystemTime::now(),
            verification_status: VerificationStatus::NotVerified,
            stability: StabilityLevel::Stable,
            since_version: Version::new(1, 0, 0),
            performance: PerformanceContract {
                time_complexity: ComplexityBound::Linear,
                space_complexity: ComplexityBound::Constant,
                maxexecution_time: max_exec,
                min_throughput: None,
                memorybandwidth: None,
            },
            numerical: NumericalContract {
                precision: PrecisionGuarantee::MachinePrecision,
                stability: NumericalStability::Stable,
                input_domain: InputDomain {
                    ranges: vec![],
                    exclusions: vec![],
                    special_values: SpecialValueHandling::Propagate,
                },
                output_range: OutputRange {
                    bounds: None,
                    monotonic: None,
                    continuous: true,
                },
            },
            concurrency: ConcurrencyContract {
                thread_safety: if thread_safe {
                    ThreadSafety::ThreadSafe
                } else {
                    ThreadSafety::NotThreadSafe
                },
                atomicity: AtomicityGuarantee::OperationAtomic,
                lock_free: false,
                wait_free: false,
                memory_ordering: MemoryOrdering::AcquireRelease,
            },
            memory: MemoryContract {
                allocation_pattern: AllocationPattern::SingleAllocation,
                max_memory,
                alignment: None,
                locality: LocalityGuarantee::GoodSpatial,
                gc_behavior: GcBehavior::MinimalGc,
            },
            deprecation: None,
        }
    }

    /// Poll until verification for `(module, apiname)` leaves the
    /// `InProgress` state (the background thread resolves near-instantly
    /// for these tests), panicking if it never does.
    fn wait_for_verification(
        engine: &FormalVerificationEngine,
        apiname: &str,
        module: &str,
    ) -> VerificationStatus {
        let deadline = Instant::now() + Duration::from_secs(5);
        loop {
            let status = engine.get_verification_status(apiname, module);
            if status != VerificationStatus::InProgress {
                return status;
            }
            assert!(
                Instant::now() < deadline,
                "verification for {module}-{apiname} did not complete within the test timeout"
            );
            thread::sleep(Duration::from_millis(5));
        }
    }

    #[test]
    fn test_formal_verification_engine() {
        let engine = FormalVerificationEngine::new();
        assert_eq!(engine.get_verification_coverage(), 0.0);

        let contract = make_contract(
            "test_api",
            "test_module",
            Some(Duration::from_millis(100)),
            Some(1024),
            true,
        );

        // No probe supplied: nothing was actually executed, so this must
        // honestly resolve to Failed rather than a fabricated Verified.
        engine
            .verify_contract(&contract, None)
            .expect("Operation failed");

        let status = wait_for_verification(&engine, "test_api", "test_module");
        assert_eq!(
            status,
            VerificationStatus::Failed,
            "verification with no executable probe must not fabricate Verified"
        );

        let results = engine.get_all_results();
        let result = results
            .get("test_module-test_api")
            .expect("result recorded");
        assert!(!result.verified);
        assert!(result.counterexample.is_some());
    }

    #[test]
    fn test_verify_contract_passes_with_probe_within_bounds() {
        let engine = FormalVerificationEngine::new();
        let contract = make_contract(
            "fast_api",
            "perf_module",
            Some(Duration::from_millis(50)),
            Some(1024),
            false,
        );

        let probe = VerificationProbe {
            invoke: Box::new(|| (Duration::from_millis(1), Some(10))),
            iterations: 5,
            concurrency: 1,
        };

        engine
            .verify_contract(&contract, Some(probe))
            .expect("start verification");
        let status = wait_for_verification(&engine, "fast_api", "perf_module");
        assert_eq!(status, VerificationStatus::Verified);

        let results = engine.get_all_results();
        let result = results
            .get("perf_module-fast_api")
            .expect("result recorded");
        assert!(result.verified);
        assert!(result
            .checked_properties
            .contains(&"performance_bound".to_string()));
        assert!(result
            .checked_properties
            .contains(&"memory_bound".to_string()));
    }

    #[test]
    fn test_verify_contract_detects_real_performance_violation() {
        let engine = FormalVerificationEngine::new();
        let contract = make_contract(
            "slow_api",
            "perf_module",
            Some(Duration::from_millis(10)),
            None,
            false,
        );

        // A real measured duration that genuinely exceeds the contract
        // bound; under the old hardcoded-`true` implementation this would
        // have been reported Verified regardless.
        let probe = VerificationProbe {
            invoke: Box::new(|| (Duration::from_millis(200), None)),
            iterations: 1,
            concurrency: 1,
        };

        engine
            .verify_contract(&contract, Some(probe))
            .expect("start verification");
        let status = wait_for_verification(&engine, "slow_api", "perf_module");
        assert_eq!(status, VerificationStatus::Failed);

        let results = engine.get_all_results();
        let result = results
            .get("perf_module-slow_api")
            .expect("result recorded");
        assert!(!result.verified);
        let counterexample = result
            .counterexample
            .as_ref()
            .expect("counterexample present");
        assert!(
            counterexample.contains("performance_bound"),
            "got: {counterexample}"
        );
    }

    #[test]
    fn test_verify_contract_detects_real_memory_violation() {
        let engine = FormalVerificationEngine::new();
        let contract = make_contract("greedy_api", "mem_module", None, Some(100), false);

        let probe = VerificationProbe {
            invoke: Box::new(|| (Duration::from_micros(1), Some(500))),
            iterations: 1,
            concurrency: 1,
        };

        engine
            .verify_contract(&contract, Some(probe))
            .expect("start verification");
        let status = wait_for_verification(&engine, "greedy_api", "mem_module");
        assert_eq!(status, VerificationStatus::Failed);

        let results = engine.get_all_results();
        let result = results
            .get("mem_module-greedy_api")
            .expect("result recorded");
        let counterexample = result
            .counterexample
            .as_ref()
            .expect("counterexample present");
        assert!(
            counterexample.contains("memory_bound"),
            "got: {counterexample}"
        );
    }

    #[test]
    fn test_verify_contract_concurrent_smoke_test_catches_real_panics() {
        let engine = FormalVerificationEngine::new();
        let contract = make_contract("racy_api", "concurrency_module", None, None, true);

        let call_count = Arc::new(AtomicUsize::new(0));
        let call_count_probe = Arc::clone(&call_count);
        let probe = VerificationProbe {
            invoke: Box::new(move || {
                let n = call_count_probe.fetch_add(1, Ordering::SeqCst);
                // The sequential performance/memory warm-up consumes calls
                // 0..iterations (4) first; only fail calls from the
                // concurrent phase (n >= 4) so this test exercises the
                // concurrent smoke test specifically; a probe that panics
                // during the *sequential* pass is covered by a separate
                // test (`test_formal_verification_engine`-adjacent
                // behavior lives in `perform_verification`'s own
                // panic-safety, not this concurrency-focused test).
                assert!(n < 4 || n % 5 != 0, "simulated concurrency bug (call #{n})");
                (Duration::from_micros(1), None)
            }),
            iterations: 4,
            concurrency: 4,
        };

        engine
            .verify_contract(&contract, Some(probe))
            .expect("start verification");
        let status = wait_for_verification(&engine, "racy_api", "concurrency_module");
        assert_eq!(status, VerificationStatus::Failed);

        let results = engine.get_all_results();
        let result = results
            .get("concurrency_module-racy_api")
            .expect("result recorded");
        let counterexample = result
            .counterexample
            .as_ref()
            .expect("counterexample present");
        assert!(
            counterexample.contains("thread_safety"),
            "got: {counterexample}"
        );
    }

    #[test]
    fn test_verify_contract_concurrent_smoke_test_passes_when_actually_safe() {
        let engine = FormalVerificationEngine::new();
        let contract = make_contract("safe_api", "concurrency_module2", None, None, true);

        let probe = VerificationProbe {
            invoke: Box::new(|| (Duration::from_micros(1), None)),
            iterations: 4,
            concurrency: 4,
        };

        engine
            .verify_contract(&contract, Some(probe))
            .expect("start verification");
        let status = wait_for_verification(&engine, "safe_api", "concurrency_module2");
        assert_eq!(status, VerificationStatus::Verified);
    }

    /// Regression test: a probe that panics during the sequential
    /// performance/memory measurement pass (before the concurrent
    /// thread-safety smoke test even runs) must resolve to `Failed` with
    /// an explanatory counterexample — not silently hang the background
    /// verification thread forever (an uncaught panic there would abandon
    /// the task in `InProgress` with nothing left running to ever update
    /// it).
    #[test]
    fn test_verify_contract_probe_panic_during_sequential_pass_reports_failure_not_hang() {
        let engine = FormalVerificationEngine::new();
        let contract = make_contract(
            "panicky_api",
            "panic_module",
            Some(Duration::from_millis(50)),
            None,
            false,
        );

        let probe = VerificationProbe {
            invoke: Box::new(|| panic!("probe always panics")),
            iterations: 1,
            concurrency: 1,
        };

        engine
            .verify_contract(&contract, Some(probe))
            .expect("start verification");
        let status = wait_for_verification(&engine, "panicky_api", "panic_module");
        assert_eq!(
            status,
            VerificationStatus::Failed,
            "a probe that panics must resolve to Failed, not hang or fabricate Verified"
        );

        let results = engine.get_all_results();
        let result = results
            .get("panic_module-panicky_api")
            .expect("result recorded");
        assert!(!result.verified);
        let counterexample = result
            .counterexample
            .as_ref()
            .expect("counterexample present");
        assert!(counterexample.contains("panicked"), "got: {counterexample}");
    }

    #[test]
    fn test_runtime_contract_validator() {
        let (validator, receiver) = RuntimeContractValidator::new();

        let stats = validator.get_statistics().expect("Operation failed");
        assert_eq!(stats.total_validations, 0);
        assert_eq!(stats.violations_detected, 0);
        assert_eq!(stats.success_rate, 1.0);
    }

    #[test]
    fn test_performance_modeler() {
        let modeler = AdvancedPerformanceModeler::new();

        let input_chars = InputCharacteristics::new(1000, "f64".to_string());
        let system_state = SystemState::new();
        let performance = PerformanceMetrics {
            operation_times: std::collections::HashMap::new(),
            strategy_success_rates: std::collections::HashMap::new(),
            memorybandwidth_utilization: 0.8,
            cache_hit_rate: 0.8,
            parallel_efficiency: 0.9,
        };

        modeler.record_measurement(
            "test_api",
            input_chars.clone(),
            performance,
            system_state.clone(),
        );

        assert_eq!(modeler.get_data_point_count("test_api"), 1);
        assert_eq!(
            modeler.get_training_status("test_api"),
            TrainingStatus::NotStarted
        );
    }

    #[test]
    fn test_audit_trail() {
        let trail = ImmutableAuditTrail::new();
        assert!(trail.is_empty());
        assert!(trail.verify_integrity());

        let data = AuditData::ContractRegistration("test::api".to_string());
        trail.add_record(data).expect("Operation failed");

        assert_eq!(trail.len(), 1);
        assert!(trail.verify_integrity());
    }

    #[test]
    fn test_input_characteristics() {
        let chars = InputCharacteristics::matrix(10, 10);
        assert_eq!(chars.size, 100);
        assert_eq!(chars.memory_layout, "row_major");

        let vector_chars = InputCharacteristics::vector(50);
        assert_eq!(vector_chars.size, 50);
        assert_eq!(vector_chars.access_pattern, "sequential");
    }

    #[test]
    fn test_system_state() {
        let state = SystemState::current();
        assert!(state.cpu_utilization >= 0.0 && state.cpu_utilization <= 1.0);
        assert!(state.memory_utilization >= 0.0 && state.memory_utilization <= 1.0);
        assert!(state.temperature > 0.0);
    }
}