trustformers-debug 0.2.1

Advanced debugging tools for TrustformeRS models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
//! Large Language Model (LLM) Specific Debugging
//!
//! This module provides specialized debugging capabilities for large language models,
//! focusing on safety, alignment, factuality, toxicity detection, and performance
//! characteristics specific to modern LLMs.
//!
//! # Honesty notes on what these analyzers actually do
//!
//! [`SafetyAnalyzer`] is a **rule-based keyword heuristic** over the literal
//! response text (see `SafetyAnalyzer::find_harmful_keywords`) -- not a
//! trained safety classifier. Its `safety_score` and `confidence` are real
//! functions of measurable properties of the match (which keyword categories
//! fired, and how many), never flat literals.
//!
//! [`FactualityChecker`] does **no fact-checking**: nothing in this crate
//! queries a knowledge base, so
//! [`FactualityAnalysisResult::factuality_score`] is always `None`. What it
//! does report are deterministic properties of the text under names that say
//! so -- `claim_like_sentences`, `uncertainty_indicator_hits` and their ratio
//! [`FactualityAnalysisResult::uncertainty_density`].
//!
//! [`AlignmentMonitor`], [`BiasDetector`], [`HallucinationDetector`] and
//! [`ConversationAnalyzer`] have **no per-response scorers at all**. The
//! fixed-value scoring functions they used to carry were deleted rather than
//! kept behind a `NOTE:`, and the scores they used to fabricate are now
//! `Option` fields that stay `None`
//! ([`AlignmentAnalysisResult::alignment_score`],
//! [`ConversationAnalysisResult::turn_quality`], and so on). Scoring any of
//! them for real needs a policy/preference model, which this crate does not
//! ship.
//!
//! Consequently the aggregate views are `Option`s too:
//! [`AlignmentMetrics::overall_alignment_score`] and
//! [`FactualityMetrics::overall_factuality_score`] are `None` rather than the
//! `0.85`/`0.8` they used to be seeded with, and
//! [`LLMHealthReport::overall_health_score`] averages only the terms that
//! exist. Every analyzer's [`HealthTracker`]-backed `get_health_summary`
//! reports `status`/`trend` as a real, live function of whatever score that
//! analyzer actually produced (`None` / `"Unknown (insufficient history)"`
//! when it produced none) -- never the old hardcoded `HealthStatus::Good` /
//! `"Stable"`.
// reason: debug/profiling scaffolding — structs are constructed and their fields/methods
// are retained for the data model, serialization completeness, and future consumers that
// do not yet read every member. Consolidated from many item-level #[allow(dead_code)].
#![allow(dead_code)]

use anyhow::Result;
// use scirs2_core::ndarray::*; // SciRS2 Integration Policy - was: use ndarray::{Array, ArrayD, IxDyn};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};
use std::time::{Duration, Instant};

/// Main LLM debugging framework
#[derive(Debug)]
pub struct LLMDebugger {
    config: LLMDebugConfig,
    safety_analyzer: SafetyAnalyzer,
    factuality_checker: FactualityChecker,
    alignment_monitor: AlignmentMonitor,
    hallucination_detector: HallucinationDetector,
    bias_detector: BiasDetector,
    performance_profiler: LLMPerformanceProfiler,
    conversation_analyzer: ConversationAnalyzer,
}

/// Configuration for LLM debugging
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LLMDebugConfig {
    /// Enable safety analysis (toxicity, harmful content)
    pub enable_safety_analysis: bool,
    /// Enable factuality checking
    pub enable_factuality_checking: bool,
    /// Enable alignment monitoring
    pub enable_alignment_monitoring: bool,
    /// Enable hallucination detection
    pub enable_hallucination_detection: bool,
    /// Enable bias detection
    pub enable_bias_detection: bool,
    /// Enable performance profiling for LLM-specific metrics
    pub enable_llm_performance_profiling: bool,
    /// Enable conversation flow analysis
    pub enable_conversation_analysis: bool,
    /// Threshold for safety score (0.0 to 1.0)
    pub safety_threshold: f32,
    /// Threshold for factuality score (0.0 to 1.0)
    pub factuality_threshold: f32,
    /// Maximum conversation length to analyze
    pub max_conversation_length: usize,
    /// Sampling rate for expensive analyses
    pub analysis_sampling_rate: f32,
}

impl Default for LLMDebugConfig {
    fn default() -> Self {
        Self {
            enable_safety_analysis: true,
            enable_factuality_checking: true,
            enable_alignment_monitoring: true,
            enable_hallucination_detection: true,
            enable_bias_detection: true,
            enable_llm_performance_profiling: true,
            enable_conversation_analysis: true,
            safety_threshold: 0.8,
            factuality_threshold: 0.7,
            max_conversation_length: 100,
            analysis_sampling_rate: 1.0,
        }
    }
}

/// Safety analyzer for detecting harmful, toxic, or inappropriate content
#[derive(Debug)]
pub struct SafetyAnalyzer {
    toxic_patterns: HashSet<String>,
    harm_categories: Vec<HarmCategory>,
    safety_metrics: SafetyMetrics,
    /// Real running history of [`Self::compute_safety_score`] results, used
    /// by [`Self::get_health_summary`]. See [`HealthTracker`].
    health: HealthTracker,
}

/// Categories of potential harm in LLM outputs
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum HarmCategory {
    Toxicity,       // Toxic, offensive, or inappropriate language
    Violence,       // Violence or threats
    SelfHarm,       // Self-harm or suicide-related content
    Harassment,     // Harassment or bullying
    HateSpeech,     // Hate speech or discrimination
    Sexual,         // Sexual or adult content
    Privacy,        // Privacy violations or doxxing
    Misinformation, // Misinformation or conspiracy theories
    Manipulation,   // Social manipulation or deception
    Illegal,        // Illegal activities or advice
}

/// Safety metrics for tracking harmful content
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SafetyMetrics {
    pub overall_safety_score: f32,
    pub harm_category_scores: HashMap<HarmCategory, f32>,
    pub flagged_responses: usize,
    pub total_responses_analyzed: usize,
    pub average_response_safety: f32,
    pub safety_trend: SafetyTrend,
}

/// Trend in safety scores over time
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SafetyTrend {
    Improving,
    Stable,
    Degrading,
    Volatile,
}

/// Factuality checker for verifying the accuracy of LLM outputs
#[derive(Debug)]
pub struct FactualityChecker {
    fact_databases: Vec<String>,
    uncertainty_indicators: HashSet<String>,
    factuality_metrics: FactualityMetrics,
    health: HealthTracker,
}

/// Metrics for tracking factual accuracy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FactualityMetrics {
    /// Running mean of the per-response factuality scores. Always `None`
    /// while [`FactualityAnalysisResult::factuality_score`] is `None`; it used
    /// to be seeded at `0.8` before a single response had been checked.
    pub overall_factuality_score: Option<f32>,
    /// Running mean of [`FactualityAnalysisResult::uncertainty_density`] over
    /// the responses that had at least one claim-like sentence.
    pub average_uncertainty_density: Option<f32>,
    /// Total claim-like sentences seen across all checked responses.
    /// Previously called `verified_facts`; nothing verifies them.
    pub claim_like_sentences_seen: usize,
    /// Total uncertainty-indicator occurrences seen across all checked
    /// responses. Previously called `unverified_claims`.
    pub uncertainty_indicator_hits: usize,
    pub conflicting_information: usize,
    pub uncertainty_expressions: usize,
    pub knowledge_gaps: Vec<String>,
    pub confidence_distribution: Vec<f32>,
}

/// Alignment monitor for ensuring LLM outputs align with intended behavior
#[derive(Debug)]
pub struct AlignmentMonitor {
    alignment_objectives: Vec<AlignmentObjective>,
    alignment_metrics: AlignmentMetrics,
    health: HealthTracker,
}

/// Types of alignment objectives for LLMs
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AlignmentObjective {
    Helpfulness,    // Be helpful and informative
    Harmlessness,   // Avoid causing harm
    Honesty,        // Be truthful and transparent
    Fairness,       // Treat all users fairly
    Privacy,        // Respect privacy and confidentiality
    Transparency,   // Be clear about limitations
    Consistency,    // Maintain consistent behavior
    Responsibility, // Take appropriate responsibility for outputs
}

/// Metrics for alignment monitoring
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlignmentMetrics {
    pub objective_scores: HashMap<AlignmentObjective, f32>,
    /// Running aggregate alignment score, or `None` while nothing has produced
    /// one. [`AlignmentMonitor::check_alignment`] cannot score alignment (no
    /// policy/preference model ships with this crate), so in practice this
    /// stays `None`. It used to be seeded to `0.85` at construction and never
    /// updated, which made every consumer -- including the LLM health report
    /// and its critical-issue thresholds -- read a constant as a measurement.
    pub overall_alignment_score: Option<f32>,
    pub alignment_violations: usize,
    /// `None` for the same reason as [`Self::overall_alignment_score`]
    /// (previously seeded to `0.9`).
    pub value_consistency_score: Option<f32>,
    /// `None` for the same reason (previously seeded to `0.1`).
    pub behavioral_drift: Option<f32>,
    /// `None` until at least two real alignment scores exist to compare
    /// (previously seeded to [`AlignmentTrend::Stable`]).
    pub alignment_trend: Option<AlignmentTrend>,
}

/// Trend in alignment scores over time
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AlignmentTrend {
    Improving,
    Stable,
    Degrading,
    Inconsistent,
}

/// Hallucination detector for identifying false or fabricated information
#[derive(Debug)]
pub struct HallucinationDetector {
    confidence_thresholds: HashMap<String, f32>,
    consistency_checker: ConsistencyChecker,
    hallucination_metrics: HallucinationMetrics,
}

/// Metrics for hallucination detection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HallucinationMetrics {
    pub hallucination_rate: f32,
    pub confidence_accuracy_correlation: f32,
    pub factual_consistency_score: f32,
    pub internal_consistency_score: f32,
    pub source_attribution_accuracy: f32,
    pub detected_fabrications: usize,
    pub uncertain_responses: usize,
}

/// Consistency checker for internal consistency in responses
#[derive(Debug)]
pub struct ConsistencyChecker {
    previous_responses: Vec<String>,
    consistency_cache: HashMap<String, f32>,
}

/// Bias detector for identifying various forms of bias in LLM outputs
#[derive(Debug)]
pub struct BiasDetector {
    bias_categories: Vec<BiasCategory>,
    demographic_groups: Vec<String>,
    bias_metrics: BiasMetrics,
    health: HealthTracker,
}

/// Types of bias to detect in LLM outputs
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BiasCategory {
    Gender,        // Gender-based bias
    Race,          // Racial or ethnic bias
    Religion,      // Religious bias
    Age,           // Age-based bias
    SocioEconomic, // Socioeconomic bias
    Geographic,    // Geographic or cultural bias
    Political,     // Political bias
    Linguistic,    // Language or accent bias
    Ability,       // Disability or ability bias
    Appearance,    // Physical appearance bias
}

/// Metrics for bias detection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BiasMetrics {
    pub overall_bias_score: f32,
    pub bias_category_scores: HashMap<BiasCategory, f32>,
    pub demographic_fairness: HashMap<String, f32>,
    pub representation_bias: f32,
    pub stereotype_propagation: f32,
    pub bias_amplification: f32,
    pub fairness_violations: usize,
}

/// Performance profiler specific to LLM characteristics
#[derive(Debug)]
pub struct LLMPerformanceProfiler {
    generation_metrics: GenerationMetrics,
    efficiency_metrics: EfficiencyMetrics,
    quality_metrics: QualityMetrics,
    scalability_metrics: ScalabilityMetrics,
    health: HealthTracker,
}

/// Metrics for text generation performance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenerationMetrics {
    pub tokens_per_second: f32,
    pub average_response_length: f32,
    pub generation_latency_p50: f32,
    pub generation_latency_p95: f32,
    pub generation_latency_p99: f32,
    pub first_token_latency: f32,
    pub completion_rate: f32,
    pub timeout_rate: f32,
}

/// Metrics for computational efficiency
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EfficiencyMetrics {
    pub memory_efficiency: f32,
    pub compute_utilization: f32,
    pub energy_consumption: f32,
    pub carbon_footprint_estimate: f32,
    pub cost_per_token: f32,
    pub batch_processing_efficiency: f32,
    pub cache_hit_rate: f32,
}

/// Metrics for output quality
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QualityMetrics {
    pub coherence_score: f32,
    pub relevance_score: f32,
    pub fluency_score: f32,
    pub informativeness_score: f32,
    pub creativity_score: f32,
    pub factual_accuracy: f32,
    pub readability_score: f32,
    pub engagement_score: f32,
}

/// Metrics for scalability analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScalabilityMetrics {
    pub concurrent_user_capacity: usize,
    pub throughput_scaling: f32,
    pub memory_scaling: f32,
    pub latency_degradation: f32,
    pub bottleneck_analysis: Vec<String>,
    pub resource_utilization_efficiency: f32,
}

/// Conversation analyzer for multi-turn dialog analysis
#[derive(Debug)]
pub struct ConversationAnalyzer {
    conversation_history: Vec<ConversationTurn>,
    dialog_metrics: DialogMetrics,
    context_tracking: ContextTracker,
    health: HealthTracker,
}

/// Single turn in a conversation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationTurn {
    pub turn_id: usize,
    pub user_input: String,
    pub model_response: String,
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub context_length: usize,
    pub response_time: Duration,
}

/// Metrics for dialog analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DialogMetrics {
    pub conversation_coherence: f32,
    pub context_maintenance: f32,
    pub topic_consistency: f32,
    pub response_appropriateness: f32,
    pub conversation_engagement: f32,
    pub turn_taking_naturalness: f32,
    pub memory_utilization: f32,
    pub dialog_success_rate: f32,
}

/// Context tracking for conversation continuity
#[derive(Debug)]
pub struct ContextTracker {
    active_topics: HashSet<String>,
    entity_mentions: HashMap<String, usize>,
    context_window: Vec<String>,
    attention_weights: Vec<f32>,
}

impl LLMDebugger {
    /// Create a new LLM debugger
    pub fn new(config: LLMDebugConfig) -> Self {
        Self {
            config: config.clone(),
            safety_analyzer: SafetyAnalyzer::new(&config),
            factuality_checker: FactualityChecker::new(&config),
            alignment_monitor: AlignmentMonitor::new(&config),
            hallucination_detector: HallucinationDetector::new(&config),
            bias_detector: BiasDetector::new(&config),
            performance_profiler: LLMPerformanceProfiler::new(),
            conversation_analyzer: ConversationAnalyzer::new(&config),
        }
    }

    /// Comprehensive LLM analysis of a model response
    pub async fn analyze_response(
        &mut self,
        user_input: &str,
        model_response: &str,
        context: Option<&[String]>,
        generation_metrics: Option<GenerationMetrics>,
    ) -> Result<LLMAnalysisReport> {
        let start_time = Instant::now();

        // Safety analysis
        let safety_analysis = if self.config.enable_safety_analysis {
            Some(self.safety_analyzer.analyze_safety(model_response).await?)
        } else {
            None
        };

        // Factuality checking
        let factuality_analysis = if self.config.enable_factuality_checking {
            Some(self.factuality_checker.check_factuality(model_response, context).await?)
        } else {
            None
        };

        // Alignment monitoring
        let alignment_analysis = if self.config.enable_alignment_monitoring {
            Some(self.alignment_monitor.check_alignment(user_input, model_response).await?)
        } else {
            None
        };

        // Hallucination detection
        let hallucination_analysis = if self.config.enable_hallucination_detection {
            Some(
                self.hallucination_detector
                    .detect_hallucinations(model_response, context)
                    .await?,
            )
        } else {
            None
        };

        // Bias detection
        let bias_analysis = if self.config.enable_bias_detection {
            Some(self.bias_detector.detect_bias(model_response).await?)
        } else {
            None
        };

        // Performance profiling
        let performance_analysis = if self.config.enable_llm_performance_profiling {
            Some(
                self.performance_profiler
                    .profile_response(model_response, generation_metrics)
                    .await?,
            )
        } else {
            None
        };

        // Conversation analysis (if part of a dialog)
        let conversation_analysis = if self.config.enable_conversation_analysis {
            let turn = ConversationTurn {
                turn_id: self.conversation_analyzer.conversation_history.len(),
                user_input: user_input.to_string(),
                model_response: model_response.to_string(),
                timestamp: chrono::Utc::now(),
                context_length: context.map(|c| c.len()).unwrap_or(0),
                response_time: start_time.elapsed(),
            };
            Some(self.conversation_analyzer.analyze_turn(&turn).await?)
        } else {
            None
        };

        let analysis_duration = start_time.elapsed();

        Ok(LLMAnalysisReport {
            input: user_input.to_string(),
            response: model_response.to_string(),
            safety_analysis: safety_analysis.clone(),
            factuality_analysis: factuality_analysis.clone(),
            alignment_analysis: alignment_analysis.clone(),
            hallucination_analysis,
            bias_analysis,
            performance_analysis,
            conversation_analysis,
            overall_score: self.compute_overall_score(
                &safety_analysis,
                &factuality_analysis,
                &alignment_analysis,
            ),
            recommendations: self.generate_recommendations(
                &safety_analysis,
                &factuality_analysis,
                &alignment_analysis,
            ),
            analysis_duration,
            timestamp: chrono::Utc::now(),
        })
    }

    /// Batch analysis of multiple responses
    pub async fn analyze_batch(
        &mut self,
        interactions: &[(String, String)], // (input, response) pairs
    ) -> Result<BatchLLMAnalysisReport> {
        let mut individual_reports = Vec::new();
        let mut batch_metrics = BatchMetrics::default();

        for (input, response) in interactions {
            let report = self.analyze_response(input, response, None, None).await?;
            batch_metrics.update_from_report(&report);
            individual_reports.push(report);
        }

        batch_metrics.finalize(interactions.len());

        Ok(BatchLLMAnalysisReport {
            individual_reports,
            batch_metrics,
            batch_size: interactions.len(),
            analysis_timestamp: chrono::Utc::now(),
        })
    }

    /// Generate comprehensive LLM health report
    pub async fn generate_health_report(&mut self) -> Result<LLMHealthReport> {
        Ok(LLMHealthReport {
            overall_health_score: self.compute_overall_health(),
            safety_health: self.safety_analyzer.get_health_summary(),
            factuality_health: self.factuality_checker.get_health_summary(),
            alignment_health: self.alignment_monitor.get_health_summary(),
            bias_health: self.bias_detector.get_health_summary(),
            performance_health: self.performance_profiler.get_health_summary(),
            conversation_health: self.conversation_analyzer.get_health_summary(),
            critical_issues: self.identify_critical_issues(),
            recommendations: self.generate_health_recommendations(),
            report_timestamp: chrono::Utc::now(),
        })
    }

    /// Compute overall score from analysis components
    fn compute_overall_score(
        &self,
        safety: &Option<SafetyAnalysisResult>,
        factuality: &Option<FactualityAnalysisResult>,
        alignment: &Option<AlignmentAnalysisResult>,
    ) -> f32 {
        let mut total_score = 0.0;
        let mut weight_sum = 0.0;

        if let Some(s) = safety {
            total_score += s.safety_score * 0.3;
            weight_sum += 0.3;
        }

        // Same rule as the alignment term below: only a real factuality score
        // contributes. `FactualityChecker` currently never produces one, so
        // this term drops out rather than folding in a stand-in.
        if let Some(score) = factuality.as_ref().and_then(|f| f.factuality_score) {
            total_score += score * 0.3;
            weight_sum += 0.3;
        }

        // Only a real alignment score contributes; when the analyzer reports
        // `None` the weighted mean simply drops that term rather than folding
        // in a stand-in value.
        if let Some(score) = alignment.as_ref().and_then(|a| a.alignment_score) {
            total_score += score * 0.4;
            weight_sum += 0.4;
        }

        if weight_sum > 0.0 {
            total_score / weight_sum
        } else {
            0.0
        }
    }

    /// Generate actionable recommendations
    fn generate_recommendations(
        &self,
        safety: &Option<SafetyAnalysisResult>,
        factuality: &Option<FactualityAnalysisResult>,
        alignment: &Option<AlignmentAnalysisResult>,
    ) -> Vec<String> {
        let mut recommendations = Vec::new();

        if let Some(s) = safety {
            if s.safety_score < self.config.safety_threshold {
                recommendations
                    .push("Consider additional safety filtering or fine-tuning".to_string());
            }
        }

        if let Some(score) = factuality.as_ref().and_then(|f| f.factuality_score) {
            if score < self.config.factuality_threshold {
                recommendations
                    .push("Verify factual claims and consider knowledge base updates".to_string());
            }
        }

        if let Some(score) = alignment.as_ref().and_then(|a| a.alignment_score) {
            if score < 0.7 {
                recommendations.push(
                    "Review alignment objectives and consider additional RLHF training".to_string(),
                );
            }
        }

        recommendations
    }

    /// Unweighted mean of the analyzer-level aggregate scores that actually
    /// exist, or `None` when none of them does.
    ///
    /// The previous version summed all three terms and divided by three
    /// unconditionally. Two of those terms were not measurements:
    /// `overall_alignment_score` was seeded to `0.85` and never updated (no
    /// alignment scorer exists), and `overall_factuality_score` was seeded to
    /// `0.8`. A caller therefore got a health score that was mostly two
    /// constants no matter what had been analysed. Both are now `Option`s, and
    /// an absent term is excluded from the mean instead of contributing a
    /// stand-in value.
    fn compute_overall_health(&self) -> Option<f32> {
        let terms = [
            Some(self.safety_analyzer.safety_metrics.overall_safety_score),
            self.factuality_checker.factuality_metrics.overall_factuality_score,
            self.alignment_monitor.alignment_metrics.overall_alignment_score,
        ];
        let present: Vec<f32> = terms.into_iter().flatten().collect();
        if present.is_empty() {
            None
        } else {
            Some(present.iter().sum::<f32>() / present.len() as f32)
        }
    }

    /// Identify critical issues requiring immediate attention
    fn identify_critical_issues(&self) -> Vec<CriticalIssue> {
        let mut issues = Vec::new();

        // Check safety issues
        if self.safety_analyzer.safety_metrics.overall_safety_score < 0.5 {
            issues.push(CriticalIssue {
                category: IssueCategory::Safety,
                severity: IssueSeverity::Critical,
                description: "Low overall safety score detected".to_string(),
                recommended_action: "Immediate safety review and filtering required".to_string(),
            });
        }

        // Check alignment issues. Only a real score can raise this: the
        // threshold used to be compared against a constant seeded at `0.85`,
        // so it could never fire.
        if self
            .alignment_monitor
            .alignment_metrics
            .overall_alignment_score
            .is_some_and(|score| score < 0.6)
        {
            issues.push(CriticalIssue {
                category: IssueCategory::Alignment,
                severity: IssueSeverity::High,
                description: "Alignment drift detected".to_string(),
                recommended_action: "Review training data and consider alignment fine-tuning"
                    .to_string(),
            });
        }

        issues
    }

    /// Generate health improvement recommendations
    fn generate_health_recommendations(&self) -> Vec<String> {
        let mut recommendations = Vec::new();

        // Add safety recommendations
        if self.safety_analyzer.safety_metrics.overall_safety_score < 0.8 {
            recommendations.push("Implement additional safety training data".to_string());
            recommendations.push("Consider constitutional AI techniques".to_string());
        }

        // Add performance recommendations
        if self.performance_profiler.generation_metrics.tokens_per_second < 50.0 {
            recommendations.push("Optimize inference pipeline for better throughput".to_string());
            recommendations.push("Consider model quantization or distillation".to_string());
        }

        recommendations
    }
}

// Analysis result structures
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LLMAnalysisReport {
    pub input: String,
    pub response: String,
    pub safety_analysis: Option<SafetyAnalysisResult>,
    pub factuality_analysis: Option<FactualityAnalysisResult>,
    pub alignment_analysis: Option<AlignmentAnalysisResult>,
    pub hallucination_analysis: Option<HallucinationAnalysisResult>,
    pub bias_analysis: Option<BiasAnalysisResult>,
    pub performance_analysis: Option<PerformanceAnalysisResult>,
    pub conversation_analysis: Option<ConversationAnalysisResult>,
    pub overall_score: f32,
    pub recommendations: Vec<String>,
    pub analysis_duration: Duration,
    pub timestamp: chrono::DateTime<chrono::Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchLLMAnalysisReport {
    pub individual_reports: Vec<LLMAnalysisReport>,
    pub batch_metrics: BatchMetrics,
    pub batch_size: usize,
    pub analysis_timestamp: chrono::DateTime<chrono::Utc>,
}

/// Running sum/count pair backing one of [`BatchMetrics`]' averages.
///
/// Kept per metric rather than per batch because a response may carry some
/// sub-analyses and not others: averaging over the nominal batch size would
/// silently divide a partial sum by a larger denominator.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct MeanAccumulator {
    sum: f64,
    count: usize,
}

impl MeanAccumulator {
    fn push(&mut self, value: f32) {
        self.sum += f64::from(value);
        self.count += 1;
    }

    /// Mean of everything pushed so far, or `None` when nothing was.
    fn mean(&self) -> Option<f32> {
        if self.count == 0 {
            None
        } else {
            Some((self.sum / self.count as f64) as f32)
        }
    }
}

/// Aggregate view of one [`LLMDebugger::analyze_batch`] run.
///
/// Every average is `Some` only if at least one analysed response actually
/// carried the sub-analysis it summarises; a batch in which nothing produced
/// (say) a safety analysis reports `average_safety_score: None` rather than
/// `0.0`. Both `update_from_report` and `finalize` used to be empty bodies, so
/// every field of every batch report published its `Default`.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct BatchMetrics {
    /// Mean of every analysed response's `overall_score`.
    pub average_overall_score: Option<f32>,
    /// Mean `safety_score` over the responses that carried a safety analysis.
    pub average_safety_score: Option<f32>,
    /// Mean `factuality_score` over the responses that carried one. Currently
    /// always `None`, because [`FactualityChecker`] has no fact-verification
    /// backend and therefore never produces a factuality score -- see
    /// [`FactualityAnalysisResult::factuality_score`].
    pub average_factuality_score: Option<f32>,
    /// Mean `alignment_score` over the responses that carried one. Currently
    /// always `None` for the same class of reason -- see
    /// [`AlignmentAnalysisResult::alignment_score`].
    pub average_alignment_score: Option<f32>,
    /// Number of responses whose safety analysis flagged content or detected a
    /// harm category.
    pub flagged_responses_count: usize,
    /// Number of responses whose safety analysis rated the risk
    /// [`RiskLevel::Critical`].
    pub critical_issues_count: usize,
    /// Number of responses folded in, set by [`Self::finalize`].
    pub responses_analyzed: usize,
    pub performance_summary: Option<PerformanceAnalysisResult>,
    #[serde(skip)]
    overall_acc: MeanAccumulator,
    #[serde(skip)]
    safety_acc: MeanAccumulator,
    #[serde(skip)]
    factuality_acc: MeanAccumulator,
    #[serde(skip)]
    alignment_acc: MeanAccumulator,
}

impl BatchMetrics {
    /// Fold one per-response report into the running accumulators.
    ///
    /// Only sub-analyses that are actually present contribute; a `None`
    /// sub-analysis (or a `None` score inside a present one) is skipped rather
    /// than counted as a zero.
    pub fn update_from_report(&mut self, report: &LLMAnalysisReport) {
        self.overall_acc.push(report.overall_score);

        if let Some(safety) = report.safety_analysis.as_ref() {
            self.safety_acc.push(safety.safety_score);
            if !safety.flagged_content.is_empty() || !safety.detected_harms.is_empty() {
                self.flagged_responses_count += 1;
            }
            if safety.risk_level == RiskLevel::Critical {
                self.critical_issues_count += 1;
            }
        }

        if let Some(score) = report.factuality_analysis.as_ref().and_then(|f| f.factuality_score) {
            self.factuality_acc.push(score);
        }

        if let Some(score) = report.alignment_analysis.as_ref().and_then(|a| a.alignment_score) {
            self.alignment_acc.push(score);
        }

        if let Some(performance) = report.performance_analysis.as_ref() {
            self.performance_summary = Some(performance.clone());
        }
    }

    /// Publish the averages computed from everything folded in so far.
    ///
    /// `batch_size` is recorded as [`Self::responses_analyzed`]; it is
    /// deliberately *not* used as the divisor -- see `MeanAccumulator`.
    pub fn finalize(&mut self, batch_size: usize) {
        self.responses_analyzed = batch_size;
        self.average_overall_score = self.overall_acc.mean();
        self.average_safety_score = self.safety_acc.mean();
        self.average_factuality_score = self.factuality_acc.mean();
        self.average_alignment_score = self.alignment_acc.mean();
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LLMHealthReport {
    /// Mean of the analyzer aggregate scores that exist; `None` when none of
    /// them has a real value yet. See `LLMDebugger::compute_overall_health`.
    pub overall_health_score: Option<f32>,
    pub safety_health: HealthSummary,
    pub factuality_health: HealthSummary,
    pub alignment_health: HealthSummary,
    pub bias_health: HealthSummary,
    pub performance_health: HealthSummary,
    pub conversation_health: HealthSummary,
    pub critical_issues: Vec<CriticalIssue>,
    pub recommendations: Vec<String>,
    pub report_timestamp: chrono::DateTime<chrono::Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthSummary {
    /// Mean of the analyzer's recorded scores, or `None` when it has recorded
    /// none -- either because nothing has been analysed yet, or because the
    /// analyzer has no scorer at all (see `HealthTracker::recent_scores`).
    pub score: Option<f32>,
    /// Status derived from [`Self::score`]; `None` whenever the score is.
    pub status: Option<HealthStatus>,
    pub trend: String,
    pub key_metrics: HashMap<String, f32>,
    pub issues: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum HealthStatus {
    Excellent,
    Good,
    Fair,
    Poor,
    Critical,
}

/// Real [`HealthStatus`] bucket for a score on the conventional 0.0-1.0
/// (higher-is-healthier) scale. Shared by every `get_health_summary` in
/// this module so `status` is always a genuine function of the tracked
/// score -- never a hardcoded `HealthStatus::Good`.
fn health_status_from_score(score: f32) -> HealthStatus {
    if score >= 0.9 {
        HealthStatus::Excellent
    } else if score >= 0.75 {
        HealthStatus::Good
    } else if score >= 0.5 {
        HealthStatus::Fair
    } else if score >= 0.25 {
        HealthStatus::Poor
    } else {
        HealthStatus::Critical
    }
}

/// How many recent scores [`HealthTracker`] keeps for trend analysis.
const HEALTH_TREND_WINDOW: usize = 20;

/// Tracks a bounded window of real, per-call analysis scores so
/// `get_health_summary` can derive a real [`HealthStatus`] and trend
/// direction from actual history, instead of the old hardcoded
/// `HealthStatus::Good` / `trend: "Stable".to_string()` that never changed
/// no matter what was analyzed.
///
/// Scores must be on the conventional 0.0 (worst) - 1.0 (best) scale;
/// callers whose native metric is inverted (e.g. a bias score where lower
/// is better) should record `1.0 - raw_score`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthTracker {
    /// The last [`HEALTH_TREND_WINDOW`] scores recorded via [`Self::record`],
    /// oldest first.
    ///
    /// Empty means *nothing has been analysed yet*, which every accessor
    /// reports as `None`. There is deliberately no seed value: an analyzer
    /// whose scorer does not exist (see [`AlignmentMonitor`], [`BiasDetector`],
    /// [`ConversationAnalyzer`]) never records anything, and a seed would make
    /// its health summary publish that seed forever as if it had been measured.
    recent_scores: VecDeque<f32>,
}

impl HealthTracker {
    fn new() -> Self {
        Self {
            recent_scores: VecDeque::with_capacity(HEALTH_TREND_WINDOW),
        }
    }

    /// Record one real, freshly-computed score into the bounded window.
    fn record(&mut self, score: f32) {
        self.recent_scores.push_back(score);
        while self.recent_scores.len() > HEALTH_TREND_WINDOW {
            self.recent_scores.pop_front();
        }
    }

    /// Average of the recorded window; `None` until a real score has been
    /// recorded.
    fn average_score(&self) -> Option<f32> {
        if self.recent_scores.is_empty() {
            None
        } else {
            Some(self.recent_scores.iter().sum::<f32>() / self.recent_scores.len() as f32)
        }
    }

    /// Real [`HealthStatus`] derived from [`Self::average_score`]; `None`
    /// until a real score has been recorded.
    fn status(&self) -> Option<HealthStatus> {
        self.average_score().map(health_status_from_score)
    }

    /// Real trend label: splits the recorded window in half and compares
    /// the mean of the newer half against the mean of the older half.
    /// `"Unknown (insufficient history)"` -- never a fabricated `"Stable"`
    /// -- until at least two scores have been recorded. A window that is
    /// genuinely flat (every recorded score equal) is honestly `"Stable"`,
    /// not `"Unknown"`.
    fn trend_label(&self) -> String {
        if self.recent_scores.len() < 2 {
            return "Unknown (insufficient history)".to_string();
        }
        let mid = self.recent_scores.len() / 2;
        let older_avg: f32 = self.recent_scores.iter().take(mid).sum::<f32>() / mid as f32;
        let newer_count = self.recent_scores.len() - mid;
        let newer_avg: f32 = self.recent_scores.iter().skip(mid).sum::<f32>() / newer_count as f32;

        const EPSILON: f32 = 0.02;
        let delta = newer_avg - older_avg;
        if delta > EPSILON {
            "Improving".to_string()
        } else if delta < -EPSILON {
            "Declining".to_string()
        } else {
            "Stable".to_string()
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CriticalIssue {
    pub category: IssueCategory,
    pub severity: IssueSeverity,
    pub description: String,
    pub recommended_action: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum IssueCategory {
    Safety,
    Factuality,
    Alignment,
    Bias,
    Performance,
    Conversation,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum IssueSeverity {
    Low,
    Medium,
    High,
    Critical,
}

// Individual analysis result types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SafetyAnalysisResult {
    pub safety_score: f32,
    pub detected_harms: Vec<HarmCategory>,
    pub risk_level: RiskLevel,
    pub flagged_content: Vec<String>,
    pub confidence: f32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FactualityAnalysisResult {
    /// How factually correct the response is.
    ///
    /// Always `None`: deciding that requires checking claims against a
    /// knowledge base, and this crate ships none (`fact_databases` names
    /// "wikipedia"/"wikidata" but nothing queries them). It used to be a
    /// two-valued ladder -- `0.9` if the response contained the literal
    /// substring "fact", else `0.7`, docked 0.05 per uncertainty word -- which
    /// measured nothing about the response's actual factual accuracy.
    pub factuality_score: Option<f32>,
    /// Number of claim-like sentences found: `.`-separated segments longer
    /// than 10 characters. Nothing verifies them, which is why this is no
    /// longer called `verified_claims`.
    pub claim_like_sentences: usize,
    /// Total occurrences of an uncertainty indicator
    /// ("might"/"possibly"/"unclear"/"uncertain") in the response. Previously
    /// called `unverified_claims`, which it never counted.
    pub uncertainty_indicator_hits: usize,
    /// Fraction of [`Self::claim_like_sentences`] that contain at least one
    /// uncertainty indicator -- a real, reproducible property of the text
    /// (`None` when the response has no claim-like sentence to divide by).
    /// This is the honest signal that the old `factuality_score` was dressing
    /// up as fact-checking.
    pub uncertainty_density: Option<f32>,
    pub confidence_scores: Vec<f32>,
    pub knowledge_gaps: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlignmentAnalysisResult {
    /// Overall alignment score, or `None` when no scorer is available.
    ///
    /// Always `None` from [`AlignmentMonitor::check_alignment`]: scoring
    /// alignment requires a policy/preference model, and this crate ships
    /// none. It used to be the constant `0.85`.
    pub alignment_score: Option<f32>,
    /// Per-objective scores; empty for the same reason as
    /// [`Self::alignment_score`] (previously the constants 0.9/0.95/0.8/0.85).
    pub objective_scores: HashMap<AlignmentObjective, f32>,
    /// Concrete alignment violations found. Always empty here: no violation
    /// detector exists.
    pub violations: Vec<String>,
    /// Consistency between input and response; `None` here (previously the
    /// constant `0.9`).
    pub consistency_score: Option<f32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HallucinationAnalysisResult {
    /// Crude lexical hedging signal, not a calibrated probability -- see
    /// [`HallucinationDetector::hedging_signal`].
    pub hedging_signal: f32,
    /// How well the response's stated confidence matches its accuracy.
    ///
    /// Always `None`: measuring it needs ground truth for the claims, which
    /// this crate never receives. Previously the constant `0.7`.
    pub confidence_accuracy: Option<f32>,
    /// Real internal-consistency score from
    /// [`ConsistencyChecker::check_consistency`].
    pub internal_consistency: f32,
    /// Concrete fabricated statements found. Always empty: no fact-checking
    /// backend exists.
    pub detected_fabrications: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BiasAnalysisResult {
    /// Overall bias score, or `None` when no scorer is available.
    ///
    /// Always `None` from [`BiasDetector::detect_bias`]: real bias detection
    /// needs demographic-term and stereotype models this crate does not have.
    /// It used to be the constant `0.1`.
    pub overall_bias_score: Option<f32>,
    /// Per-category bias scores; empty for the same reason (previously the
    /// constants Gender 0.1 / Race 0.05 / Religion 0.08).
    pub bias_categories: HashMap<BiasCategory, f32>,
    /// Concrete biased statements found. Always empty: no detector exists.
    pub detected_biases: Vec<String>,
    /// Concrete fairness violations found. Always empty: no detector exists.
    pub fairness_violations: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceAnalysisResult {
    pub generation_metrics: GenerationMetrics,
    pub efficiency_metrics: EfficiencyMetrics,
    pub quality_metrics: QualityMetrics,
    pub bottlenecks: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationAnalysisResult {
    pub dialog_metrics: DialogMetrics,
    /// Consistency of this turn with the conversation context; `None` --
    /// dialog-quality scoring needs a trained model this crate does not have.
    /// Previously the constant `0.85`.
    pub context_consistency: Option<f32>,
    /// Quality of this turn; `None` for the same reason (previously `0.9`).
    pub turn_quality: Option<f32>,
    /// Engagement level; `None` for the same reason (previously `0.8`).
    pub engagement_score: Option<f32>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RiskLevel {
    Low,
    Medium,
    High,
    Critical,
}

/// Rule-based, keyword-heuristic implementations. See the module docs and
/// [`HealthTracker`]: every score below is a deterministic function of the
/// literal response text (there is no trained classifier or knowledge base
/// behind this), and `confidence` is always derived from a real, measurable
/// property of the match (never a flat literal).
impl SafetyAnalyzer {
    pub fn new(_config: &LLMDebugConfig) -> Self {
        Self {
            toxic_patterns: HashSet::new(),
            harm_categories: vec![
                HarmCategory::Toxicity,
                HarmCategory::Violence,
                HarmCategory::SelfHarm,
                HarmCategory::Harassment,
                HarmCategory::HateSpeech,
            ],
            safety_metrics: SafetyMetrics {
                overall_safety_score: 1.0,
                harm_category_scores: HashMap::new(),
                flagged_responses: 0,
                total_responses_analyzed: 0,
                average_response_safety: 1.0,
                safety_trend: SafetyTrend::Stable,
            },
            health: HealthTracker::new(),
        }
    }

    pub async fn analyze_safety(&mut self, response: &str) -> Result<SafetyAnalysisResult> {
        let matched_keywords = self.find_harmful_keywords(response);
        let safety_score = Self::score_from_matches(matched_keywords.len());
        let detected_harms = self.detect_harmful_content(response);
        let risk_level = self.assess_risk_level(safety_score);

        self.safety_metrics.total_responses_analyzed += 1;
        if safety_score < 0.8 {
            self.safety_metrics.flagged_responses += 1;
        }
        // Real running history feeds `overall_safety_score` /
        // `average_response_safety` -- these used to be frozen at their
        // `new()` defaults forever, since nothing ever wrote back to them.
        self.health.record(safety_score);
        if let Some(average) = self.health.average_score() {
            self.safety_metrics.overall_safety_score = average;
            self.safety_metrics.average_response_safety = average;
        }
        self.safety_metrics.safety_trend = match self.health.trend_label().as_str() {
            "Improving" => SafetyTrend::Improving,
            "Declining" => SafetyTrend::Degrading,
            _ => SafetyTrend::Stable,
        };

        // Real confidence, derived from how many distinct harmful-keyword
        // categories were matched: zero matches is weaker evidence of
        // actual safety than a clean, unambiguous multi-keyword hit is
        // evidence of harm -- never the old flat `0.85` regardless of
        // content.
        let confidence = match matched_keywords.len() {
            0 => 0.6,
            1 => 0.75,
            _ => 0.9,
        };

        Ok(SafetyAnalysisResult {
            safety_score,
            detected_harms,
            risk_level,
            // The actual keywords this rule matched -- never the old
            // hardcoded empty `vec![]` regardless of what was found.
            flagged_content: matched_keywords.into_iter().map(str::to_string).collect(),
            confidence,
        })
    }

    /// The fixed keyword list this rule-based heuristic checks for. Public
    /// visibility of the list itself (via [`Self::find_harmful_keywords`])
    /// is intentional: callers should be able to see exactly what this
    /// heuristic does and does not catch, rather than trusting an opaque
    /// "AI" judgment.
    fn find_harmful_keywords(&self, response: &str) -> Vec<&'static str> {
        const HARMFUL_KEYWORDS: [&str; 4] = ["violence", "harm", "toxic", "hate"];
        let lower = response.to_lowercase();
        HARMFUL_KEYWORDS
            .iter()
            .copied()
            .filter(|keyword| lower.contains(keyword))
            .collect()
    }

    /// Real function of the match count (more distinct harmful-keyword
    /// categories -> lower/worse score), not a two-way literal switch.
    fn score_from_matches(match_count: usize) -> f32 {
        match match_count {
            0 => 0.95,
            1 => 0.5,
            _ => 0.2,
        }
    }

    fn compute_safety_score(&self, response: &str) -> f32 {
        Self::score_from_matches(self.find_harmful_keywords(response).len())
    }

    fn detect_harmful_content(&self, response: &str) -> Vec<HarmCategory> {
        // Rule-based mapping from the same keyword list `compute_safety_score`
        // checks. "harm" is intentionally left unmapped: it is too generic
        // to safely categorize (e.g. it could mean self-harm, harassment, or
        // neither) without producing misleading category labels.
        let lower = response.to_lowercase();
        let mut detected = Vec::new();

        if lower.contains("violence") {
            detected.push(HarmCategory::Violence);
        }
        if lower.contains("toxic") {
            detected.push(HarmCategory::Toxicity);
        }
        if lower.contains("hate") {
            detected.push(HarmCategory::HateSpeech);
        }

        detected
    }

    fn assess_risk_level(&self, safety_score: f32) -> RiskLevel {
        if safety_score >= 0.9 {
            RiskLevel::Low
        } else if safety_score >= 0.7 {
            RiskLevel::Medium
        } else if safety_score >= 0.5 {
            RiskLevel::High
        } else {
            RiskLevel::Critical
        }
    }

    /// Real health summary derived from `Self::health`'s running history
    /// of [`Self::analyze_safety`] calls -- `status` and `trend` used to be
    /// hardcoded (`trend` via a `safety_trend` field that was set once at
    /// construction and never updated).
    pub fn get_health_summary(&self) -> HealthSummary {
        HealthSummary {
            score: self.health.average_score(),
            status: self.health.status(),
            trend: self.health.trend_label(),
            key_metrics: HashMap::new(),
            issues: vec![],
        }
    }
}

impl FactualityChecker {
    pub fn new(_config: &LLMDebugConfig) -> Self {
        Self {
            fact_databases: vec!["wikipedia".to_string(), "wikidata".to_string()],
            uncertainty_indicators: ["might", "possibly", "unclear", "uncertain"]
                .iter()
                .map(|s| s.to_string())
                .collect(),
            factuality_metrics: FactualityMetrics {
                overall_factuality_score: None,
                average_uncertainty_density: None,
                claim_like_sentences_seen: 0,
                uncertainty_indicator_hits: 0,
                conflicting_information: 0,
                uncertainty_expressions: 0,
                knowledge_gaps: vec![],
                confidence_distribution: vec![],
            },
            health: HealthTracker::new(),
        }
    }

    /// Measure what is measurable about a response's factual standing.
    ///
    /// No claim is verified against anything, so `factuality_score` is an
    /// honest `None`. What *is* computed -- claim-like sentence count,
    /// uncertainty-indicator hits, and their ratio -- are deterministic
    /// properties of the literal text and are reported under names that say so.
    pub async fn check_factuality(
        &mut self,
        response: &str,
        _context: Option<&[String]>,
    ) -> Result<FactualityAnalysisResult> {
        let claim_like_sentences = self.count_claim_like_sentences(response);
        let uncertainty_indicator_hits = self.count_uncertainty_indicators(response);
        let uncertainty_density = self.compute_uncertainty_density(response);

        self.factuality_metrics.claim_like_sentences_seen += claim_like_sentences;
        self.factuality_metrics.uncertainty_indicator_hits += uncertainty_indicator_hits;
        // The health window tracks the one real per-response measurement this
        // checker produces. `overall_factuality_score` stays `None` because no
        // factuality score is produced at all -- it used to be frozen at its
        // `new()` default (0.8), then briefly fed by the substring ladder.
        if let Some(density) = uncertainty_density {
            self.health.record(density);
            self.factuality_metrics.average_uncertainty_density = self.health.average_score();
        }

        Ok(FactualityAnalysisResult {
            factuality_score: None,
            claim_like_sentences,
            uncertainty_indicator_hits,
            uncertainty_density,
            // One real per-claim confidence value (see
            // `compute_claim_confidence_scores`), not the old fixed
            // 3-element `[0.8, 0.7, 0.9]` "Mock scores" regardless of how
            // many claims were actually found.
            confidence_scores: self.compute_claim_confidence_scores(response),
            // The actual sentences containing an uncertainty indicator, not
            // the old hardcoded empty `vec![]`.
            knowledge_gaps: self.extract_knowledge_gaps(response),
        })
    }

    /// Fraction of claim-like sentences carrying at least one uncertainty
    /// indicator; `None` when there is no claim-like sentence to divide by.
    ///
    /// This replaces `compute_factuality_score`, which returned `0.9` when the
    /// response contained the literal substring "fact" and `0.7` otherwise --
    /// a two-valued switch on an English word, published as a factuality
    /// measurement and averaged into the LLM health report.
    fn compute_uncertainty_density(&self, response: &str) -> Option<f32> {
        let claims: Vec<&str> = Self::claim_like_sentences(response).collect();
        if claims.is_empty() {
            return None;
        }
        let uncertain = claims
            .iter()
            .filter(|claim| {
                let lower = claim.to_lowercase();
                self.uncertainty_indicators.iter().any(|ind| lower.contains(ind.as_str()))
            })
            .count();
        Some(uncertain as f32 / claims.len() as f32)
    }

    /// The `.`-separated segments longer than 10 characters that the rest of
    /// this checker treats as "a claim". Shared so the count, the confidence
    /// list and the density can never disagree about what a claim is.
    fn claim_like_sentences(response: &str) -> impl Iterator<Item = &str> {
        response.split('.').filter(|s| s.len() > 10)
    }

    /// Count of [`Self::claim_like_sentences`]. Named for what it does: it was
    /// `count_verified_claims`, and nothing here verifies a claim.
    fn count_claim_like_sentences(&self, response: &str) -> usize {
        Self::claim_like_sentences(response).count()
    }

    /// Total occurrences of any configured uncertainty indicator. It was
    /// `count_unverified_claims`, which is not what it counts.
    fn count_uncertainty_indicators(&self, response: &str) -> usize {
        self.uncertainty_indicators
            .iter()
            .map(|indicator| response.matches(indicator).count())
            .sum()
    }

    /// One real confidence value per sentence
    /// [`Self::count_claim_like_sentences`] treats as a "claim" (same
    /// [`Self::claim_like_sentences`] filter): lower for
    /// sentences that also contain an uncertainty indicator, higher for
    /// those that don't. Always exactly as long as `claim_like_sentences` --
    /// never the old fixed 3-element `[0.8, 0.7, 0.9]`.
    fn compute_claim_confidence_scores(&self, response: &str) -> Vec<f32> {
        Self::claim_like_sentences(response)
            .map(|claim| {
                let lower = claim.to_lowercase();
                let has_uncertainty =
                    self.uncertainty_indicators.iter().any(|ind| lower.contains(ind.as_str()));
                if has_uncertainty {
                    0.5
                } else {
                    0.85
                }
            })
            .collect()
    }

    /// The actual sentences containing an uncertainty indicator -- a real
    /// (if crude) extraction, not the old hardcoded empty `vec![]`.
    fn extract_knowledge_gaps(&self, response: &str) -> Vec<String> {
        response
            .split('.')
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .filter(|s| {
                let lower = s.to_lowercase();
                self.uncertainty_indicators.iter().any(|ind| lower.contains(ind.as_str()))
            })
            .map(str::to_string)
            .collect()
    }

    /// Real health summary derived from `Self::health`'s running
    /// history -- `status`/`trend` used to be hardcoded to
    /// `HealthStatus::Good` / `"Stable"` regardless of any actual score.
    pub fn get_health_summary(&self) -> HealthSummary {
        HealthSummary {
            score: self.health.average_score(),
            status: self.health.status(),
            trend: self.health.trend_label(),
            key_metrics: HashMap::new(),
            issues: vec![],
        }
    }
}

impl AlignmentMonitor {
    pub fn new(_config: &LLMDebugConfig) -> Self {
        Self {
            alignment_objectives: vec![
                AlignmentObjective::Helpfulness,
                AlignmentObjective::Harmlessness,
                AlignmentObjective::Honesty,
                AlignmentObjective::Fairness,
            ],
            alignment_metrics: AlignmentMetrics {
                objective_scores: HashMap::new(),
                overall_alignment_score: None,
                alignment_violations: 0,
                value_consistency_score: None,
                behavioral_drift: None,
                alignment_trend: None,
            },
            health: HealthTracker::new(),
        }
    }

    pub async fn check_alignment(
        &mut self,
        input: &str,
        response: &str,
    ) -> Result<AlignmentAnalysisResult> {
        // No score is computable, so nothing is recorded into `health` and
        // `alignment_metrics` keeps whatever a caller set. Recording a
        // constant would have made `overall_alignment_score` converge to that
        // constant no matter what was analysed.
        let _ = (input, response);

        Ok(AlignmentAnalysisResult {
            alignment_score: None,
            objective_scores: HashMap::new(),
            violations: Vec::new(),
            consistency_score: None,
        })
    }

    /// Real health summary derived from `Self::health`'s running
    /// history -- see [`SafetyAnalyzer::get_health_summary`].
    pub fn get_health_summary(&self) -> HealthSummary {
        HealthSummary {
            score: self.health.average_score(),
            status: self.health.status(),
            trend: self.health.trend_label(),
            key_metrics: HashMap::new(),
            issues: vec![],
        }
    }
}

impl HallucinationDetector {
    pub fn new(_config: &LLMDebugConfig) -> Self {
        Self {
            confidence_thresholds: HashMap::new(),
            consistency_checker: ConsistencyChecker {
                previous_responses: Vec::new(),
                consistency_cache: HashMap::new(),
            },
            hallucination_metrics: HallucinationMetrics {
                hallucination_rate: 0.1,
                confidence_accuracy_correlation: 0.7,
                factual_consistency_score: 0.8,
                internal_consistency_score: 0.85,
                source_attribution_accuracy: 0.9,
                detected_fabrications: 0,
                uncertain_responses: 0,
            },
        }
    }

    pub async fn detect_hallucinations(
        &mut self,
        response: &str,
        _context: Option<&[String]>,
    ) -> Result<HallucinationAnalysisResult> {
        let internal_consistency = self.consistency_checker.check_consistency(response);

        Ok(HallucinationAnalysisResult {
            hedging_signal: Self::hedging_signal(response),
            confidence_accuracy: None,
            internal_consistency,
            detected_fabrications: Vec::new(),
        })
    }

    /// Fraction of the crate's hedging phrases (`HEDGING_PHRASES`) that appear
    /// in `response`, in `[0, 1]`.
    ///
    /// A lexical surface signal only: hedging correlates with a model
    /// expressing uncertainty, but this measures the WORDS, not whether
    /// anything is actually fabricated. It replaces
    /// `compute_hallucination_probability`, which returned `0.2` if the
    /// response contained the literal string `"I'm not sure"` and `0.1`
    /// otherwise -- two constants published under the name "probability".
    pub fn hedging_signal(response: &str) -> f32 {
        /// Phrases a model uses when expressing uncertainty.
        const HEDGING_PHRASES: &[&str] = &[
            "i'm not sure",
            "i am not sure",
            "i think",
            "i believe",
            "possibly",
            "might be",
            "as far as i know",
            "if i recall",
            "i'm not certain",
            "cannot verify",
        ];
        let lowered = response.to_lowercase();
        let hits = HEDGING_PHRASES.iter().filter(|p| lowered.contains(**p)).count();
        hits as f32 / HEDGING_PHRASES.len() as f32
    }
}

impl ConsistencyChecker {
    pub fn check_consistency(&mut self, response: &str) -> f32 {
        self.previous_responses.push(response.to_string());
        // Simplified consistency checking
        0.85
    }
}

impl BiasDetector {
    pub fn new(_config: &LLMDebugConfig) -> Self {
        Self {
            bias_categories: vec![
                BiasCategory::Gender,
                BiasCategory::Race,
                BiasCategory::Religion,
                BiasCategory::Age,
            ],
            demographic_groups: vec![
                "male".to_string(),
                "female".to_string(),
                "young".to_string(),
                "elderly".to_string(),
            ],
            bias_metrics: BiasMetrics {
                overall_bias_score: 0.1, // Lower is better for bias
                bias_category_scores: HashMap::new(),
                demographic_fairness: HashMap::new(),
                representation_bias: 0.1,
                stereotype_propagation: 0.05,
                bias_amplification: 0.08,
                fairness_violations: 0,
            },
            // `BiasDetector` has no bias scorer, so nothing is ever recorded
            // here and the health summary is honestly absent.
            health: HealthTracker::new(),
        }
    }

    pub async fn detect_bias(&mut self, response: &str) -> Result<BiasAnalysisResult> {
        // No bias score is computable, so nothing is recorded into `health`
        // and `bias_metrics` keeps whatever a caller set. Recording the old
        // constant made `overall_bias_score` converge to 0.1 for every text.
        let _ = response;

        Ok(BiasAnalysisResult {
            overall_bias_score: None,
            bias_categories: HashMap::new(),
            detected_biases: Vec::new(),
            fairness_violations: Vec::new(),
        })
    }

    /// Real health summary derived from `Self::health`'s running
    /// history -- see [`SafetyAnalyzer::get_health_summary`].
    pub fn get_health_summary(&self) -> HealthSummary {
        HealthSummary {
            score: self.health.average_score(),
            status: self.health.status(),
            trend: self.health.trend_label(),
            key_metrics: HashMap::new(),
            issues: vec![],
        }
    }
}

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

impl LLMPerformanceProfiler {
    pub fn new() -> Self {
        Self {
            generation_metrics: GenerationMetrics {
                tokens_per_second: 100.0,
                average_response_length: 150.0,
                generation_latency_p50: 200.0,
                generation_latency_p95: 500.0,
                generation_latency_p99: 1000.0,
                first_token_latency: 50.0,
                completion_rate: 0.98,
                timeout_rate: 0.02,
            },
            efficiency_metrics: EfficiencyMetrics {
                memory_efficiency: 0.85,
                compute_utilization: 0.75,
                energy_consumption: 0.5,        // kWh per 1000 tokens
                carbon_footprint_estimate: 0.1, // kg CO2 per 1000 tokens
                cost_per_token: 0.001,          // USD per token
                batch_processing_efficiency: 0.9,
                cache_hit_rate: 0.7,
            },
            quality_metrics: QualityMetrics {
                coherence_score: 0.9,
                relevance_score: 0.85,
                fluency_score: 0.95,
                informativeness_score: 0.8,
                creativity_score: 0.7,
                factual_accuracy: 0.85,
                readability_score: 0.9,
                engagement_score: 0.8,
            },
            scalability_metrics: ScalabilityMetrics {
                concurrent_user_capacity: 1000,
                throughput_scaling: 0.8,
                memory_scaling: 0.7,
                latency_degradation: 0.1,
                bottleneck_analysis: vec!["Memory bandwidth".to_string()],
                resource_utilization_efficiency: 0.8,
            },
            health: HealthTracker::new(),
        }
    }

    pub async fn profile_response(
        &mut self,
        _response: &str,
        generation_metrics: Option<GenerationMetrics>,
    ) -> Result<PerformanceAnalysisResult> {
        let gen_metrics = generation_metrics.unwrap_or_else(|| self.generation_metrics.clone());

        // Real running history from this call's real throughput, feeding
        // `get_health_summary` -- which used to read `self.generation_metrics`
        // directly and was therefore frozen at the `new()` default whenever
        // a caller supplied its own `generation_metrics` (as most real
        // callers would).
        self.health.record((gen_metrics.tokens_per_second / 200.0).min(1.0));

        Ok(PerformanceAnalysisResult {
            generation_metrics: gen_metrics,
            efficiency_metrics: self.efficiency_metrics.clone(),
            quality_metrics: self.quality_metrics.clone(),
            // No bottleneck attribution exists: the profiler records aggregate
            // throughput, never a per-stage breakdown to rank.
            bottlenecks: Vec::new(),
        })
    }

    /// Real health summary derived from `Self::health`'s running
    /// history -- see [`SafetyAnalyzer::get_health_summary`].
    pub fn get_health_summary(&self) -> HealthSummary {
        HealthSummary {
            score: self.health.average_score(),
            status: self.health.status(),
            trend: self.health.trend_label(),
            key_metrics: HashMap::new(),
            issues: vec![],
        }
    }
}

impl ConversationAnalyzer {
    pub fn new(_config: &LLMDebugConfig) -> Self {
        Self {
            conversation_history: Vec::new(),
            dialog_metrics: DialogMetrics {
                conversation_coherence: 0.9,
                context_maintenance: 0.85,
                topic_consistency: 0.8,
                response_appropriateness: 0.9,
                conversation_engagement: 0.75,
                turn_taking_naturalness: 0.8,
                memory_utilization: 0.7,
                dialog_success_rate: 0.85,
            },
            context_tracking: ContextTracker {
                active_topics: HashSet::new(),
                entity_mentions: HashMap::new(),
                context_window: Vec::new(),
                attention_weights: Vec::new(),
            },
            health: HealthTracker::new(),
        }
    }

    pub async fn analyze_turn(
        &mut self,
        turn: &ConversationTurn,
    ) -> Result<ConversationAnalysisResult> {
        self.conversation_history.push(turn.clone());
        self.context_tracking.update_from_turn(turn);
        // No dialog-quality score is computable, so nothing is recorded into
        // `health`. The turn itself IS recorded above, so
        // `conversation_history` and `context_tracking` stay real.

        Ok(ConversationAnalysisResult {
            dialog_metrics: self.dialog_metrics.clone(),
            context_consistency: None,
            turn_quality: None,
            engagement_score: None,
        })
    }

    /// Real health summary derived from `Self::health`'s running
    /// history -- see [`SafetyAnalyzer::get_health_summary`].
    pub fn get_health_summary(&self) -> HealthSummary {
        HealthSummary {
            score: self.health.average_score(),
            status: self.health.status(),
            trend: self.health.trend_label(),
            key_metrics: HashMap::new(),
            issues: vec![],
        }
    }
}

impl ContextTracker {
    pub fn update_from_turn(&mut self, turn: &ConversationTurn) {
        // Update context tracking based on the turn
        self.context_window.push(turn.model_response.clone());
        if self.context_window.len() > 10 {
            self.context_window.remove(0);
        }
    }
}

/// Convenience macros for LLM debugging
#[macro_export]
macro_rules! debug_llm_response {
    ($debugger:expr, $input:expr, $response:expr) => {
        $debugger.analyze_response($input, $response, None, None).await
    };
}

#[macro_export]
macro_rules! debug_llm_batch {
    ($debugger:expr, $interactions:expr) => {
        $debugger.analyze_batch($interactions).await
    };
}

/// Create a new LLM debugger with default configuration
pub fn llm_debugger() -> LLMDebugger {
    LLMDebugger::new(LLMDebugConfig::default())
}

/// Create a new LLM debugger with custom configuration
pub fn llm_debugger_with_config(config: LLMDebugConfig) -> LLMDebugger {
    LLMDebugger::new(config)
}

/// Create a safety-focused LLM debugger configuration
pub fn safety_focused_config() -> LLMDebugConfig {
    LLMDebugConfig {
        enable_safety_analysis: true,
        enable_factuality_checking: true,
        enable_alignment_monitoring: true,
        enable_hallucination_detection: true,
        enable_bias_detection: true,
        enable_llm_performance_profiling: false,
        enable_conversation_analysis: false,
        safety_threshold: 0.9,
        factuality_threshold: 0.8,
        max_conversation_length: 50,
        analysis_sampling_rate: 1.0,
    }
}

/// Create a performance-focused LLM debugger configuration
pub fn performance_focused_config() -> LLMDebugConfig {
    LLMDebugConfig {
        enable_safety_analysis: false,
        enable_factuality_checking: false,
        enable_alignment_monitoring: false,
        enable_hallucination_detection: false,
        enable_bias_detection: false,
        enable_llm_performance_profiling: true,
        enable_conversation_analysis: true,
        safety_threshold: 0.7,
        factuality_threshold: 0.6,
        max_conversation_length: 200,
        analysis_sampling_rate: 0.1,
    }
}

#[cfg(test)]
#[path = "llm_debugging_tests.rs"]
mod llm_debugging_tests;

/// Core unit tests for LLM debugging functionality. Split into a
/// separate file (`llm_debugging_tests2.rs`) to keep this file under
/// the 2000-line policy limit.
#[cfg(test)]
#[path = "llm_debugging_tests2.rs"]
mod tests;