File size: 66,505 Bytes
1437f82
 
 
 
714bfce
 
 
 
 
 
 
 
1437f82
 
ba59239
 
5c55cb5
e94f0ea
ba59239
644fff6
3e50ac5
1437f82
 
 
 
 
644fff6
775c77d
714bfce
82009c8
1f0be8f
644fff6
 
1437f82
 
 
 
 
 
 
1eb0dc5
644fff6
1437f82
 
 
 
 
 
 
 
 
 
 
 
 
714bfce
1437f82
714bfce
 
1437f82
 
 
 
 
 
 
 
 
 
714bfce
 
 
 
1437f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e94f0ea
1f0be8f
 
 
 
 
1437f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f0be8f
1437f82
 
1f0be8f
1437f82
 
 
1f0be8f
1437f82
1f0be8f
 
 
1437f82
 
 
 
 
 
 
1f0be8f
d6f7e9f
 
 
1437f82
d6f7e9f
 
 
 
1437f82
d6f7e9f
 
 
714bfce
 
 
 
d6f7e9f
1437f82
d6f7e9f
1437f82
 
 
 
 
 
 
714bfce
 
 
 
 
 
 
1437f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6f7e9f
 
714bfce
 
 
 
 
 
 
 
 
 
1437f82
 
 
 
 
d6f7e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1437f82
 
 
 
d6f7e9f
 
 
1437f82
714bfce
 
 
 
 
 
 
 
 
 
d6f7e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714bfce
d6f7e9f
 
714bfce
d6f7e9f
 
 
 
 
714bfce
d6f7e9f
 
 
714bfce
1437f82
 
714bfce
 
 
d6f7e9f
 
 
 
 
 
 
 
 
 
 
1437f82
d6f7e9f
 
1437f82
714bfce
 
 
 
 
 
 
 
 
 
d6f7e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714bfce
d6f7e9f
 
 
 
 
714bfce
d6f7e9f
 
 
 
 
 
 
 
 
 
 
 
 
1437f82
d6f7e9f
 
 
714bfce
 
 
 
 
 
 
 
 
 
d6f7e9f
 
 
 
 
 
 
 
 
 
1437f82
 
 
 
d6f7e9f
 
 
 
 
 
 
 
 
 
 
1437f82
d6f7e9f
 
 
 
 
 
 
 
 
1437f82
 
 
 
d6f7e9f
 
 
 
 
 
 
 
 
 
 
1437f82
d6f7e9f
 
 
 
714bfce
 
 
 
 
 
 
 
 
d6f7e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1437f82
d6f7e9f
 
 
 
 
 
1f0be8f
644fff6
1437f82
 
ba59239
1f0be8f
714bfce
 
 
 
1f0be8f
 
 
1437f82
1f0be8f
 
1437f82
 
 
 
 
 
 
 
714bfce
1437f82
714bfce
1f0be8f
 
 
714bfce
1437f82
d6f7e9f
1f0be8f
d6f7e9f
1437f82
d6f7e9f
1f0be8f
 
 
714bfce
1f0be8f
 
 
714bfce
1f0be8f
 
 
 
 
 
 
 
 
714bfce
1437f82
1f0be8f
 
 
 
 
 
 
 
 
 
714bfce
 
 
 
1f0be8f
 
1437f82
1f0be8f
1437f82
 
1f0be8f
1437f82
 
1f0be8f
 
1437f82
714bfce
1f0be8f
1437f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f0be8f
1437f82
 
1f0be8f
 
 
1437f82
 
 
 
1f0be8f
 
 
714bfce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f0be8f
 
714bfce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1437f82
 
d6f7e9f
 
714bfce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6f7e9f
714bfce
 
 
 
 
d6f7e9f
714bfce
 
 
 
 
 
 
 
 
 
d6f7e9f
714bfce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6f7e9f
714bfce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6f7e9f
714bfce
1f0be8f
1eb0dc5
1437f82
775c77d
714bfce
 
 
 
1437f82
644fff6
775c77d
 
1437f82
 
644fff6
1437f82
 
644fff6
1437f82
 
 
 
 
 
 
 
 
 
714bfce
1437f82
 
 
 
 
 
 
 
 
 
714bfce
1437f82
 
775c77d
1437f82
775c77d
 
 
 
 
 
 
 
 
a81efd4
1437f82
775c77d
 
1437f82
1f0be8f
1c2c217
1437f82
5447ba5
 
 
 
 
1c2c217
714bfce
5447ba5
 
 
 
 
 
 
 
1f0be8f
1437f82
775c77d
 
1437f82
1f0be8f
775c77d
714bfce
1437f82
 
 
 
 
 
 
 
775c77d
714bfce
775c77d
 
 
 
 
 
1f0be8f
775c77d
 
 
1f0be8f
1437f82
775c77d
 
 
 
e94f0ea
775c77d
714bfce
1437f82
 
714bfce
1437f82
 
 
 
 
 
 
1f0be8f
775c77d
82009c8
775c77d
 
d97b7c8
1437f82
 
 
 
 
 
 
 
 
714bfce
1437f82
714bfce
 
 
 
 
 
 
1437f82
714bfce
1437f82
 
 
 
 
 
 
 
 
 
 
 
 
1f0be8f
714bfce
1f0be8f
714bfce
 
 
 
 
1437f82
1f0be8f
 
 
 
644fff6
d6f7e9f
1f0be8f
644fff6
1f0be8f
 
 
 
 
 
 
 
 
 
 
 
1437f82
1f0be8f
 
 
 
1437f82
1f0be8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6f7e9f
1f0be8f
 
 
 
 
 
d6f7e9f
 
1f0be8f
 
 
 
 
 
 
d6f7e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
1f0be8f
 
 
 
 
 
644fff6
1f0be8f
 
d6f7e9f
 
1f0be8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714bfce
1437f82
714bfce
 
 
 
 
 
 
 
 
 
 
1f0be8f
1437f82
1f0be8f
 
 
 
 
 
714bfce
1437f82
 
 
 
 
714bfce
1437f82
 
 
 
 
 
 
 
 
 
 
 
1f0be8f
714bfce
1f0be8f
1437f82
1f0be8f
 
 
 
 
 
 
1437f82
1f0be8f
 
1437f82
1f0be8f
1437f82
1f0be8f
 
 
 
1437f82
1f0be8f
d6f7e9f
 
1437f82
d6f7e9f
1f0be8f
1437f82
 
1f0be8f
 
 
714bfce
 
 
 
 
1f0be8f
 
 
1437f82
1f0be8f
 
d6f7e9f
1f0be8f
 
 
 
d6f7e9f
1f0be8f
 
 
 
 
 
 
1437f82
 
 
 
1f0be8f
1437f82
 
 
1f0be8f
714bfce
1f0be8f
714bfce
1f0be8f
d6f7e9f
1f0be8f
 
 
 
714bfce
1f0be8f
714bfce
 
 
1437f82
 
714bfce
 
1437f82
1f0be8f
1437f82
714bfce
1f0be8f
 
 
 
1437f82
 
714bfce
1437f82
714bfce
1437f82
 
714bfce
 
 
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
"""
Enterprise Agentic Reliability Framework - Main Application
Multi-Agent AI System for Production Reliability Monitoring

This module provides the complete reliability monitoring system including:
- Multi-agent anomaly detection and root cause analysis
- Predictive analytics and forecasting
- Policy-based auto-healing
- Business impact quantification
- Vector-based incident memory
- Adaptive thresholds
- Thread-safe concurrent operations
"""

import os
import json
import numpy as np
import gradio as gr
import requests
import pandas as pd
import datetime
import threading
import logging
from typing import List, Dict, Any, Optional, Tuple
from collections import deque
from dataclasses import dataclass, asdict
import hashlib
import asyncio
from enum import Enum

# Import our modules
from models import ReliabilityEvent, EventSeverity, AnomalyResult, HealingAction
from healing_policies import PolicyEngine

# === Logging Configuration ===
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# === Configuration ===
class Config:
    """Centralized configuration for the reliability framework"""
    HF_TOKEN: str = os.getenv("HF_TOKEN", "").strip()
    HF_API_URL: str = "https://router.huggingface.co/hf-inference/v1/completions"
    
    # Vector storage
    VECTOR_DIM: int = 384
    INDEX_FILE: str = "incident_vectors.index"
    TEXTS_FILE: str = "incident_texts.json"
    
    # Thresholds
    LATENCY_WARNING: float = 150.0
    LATENCY_CRITICAL: float = 300.0
    LATENCY_EXTREME: float = 500.0
    ERROR_RATE_WARNING: float = 0.05
    ERROR_RATE_HIGH: float = 0.15
    ERROR_RATE_CRITICAL: float = 0.3
    CPU_WARNING: float = 0.8
    CPU_CRITICAL: float = 0.9
    MEMORY_WARNING: float = 0.8
    MEMORY_CRITICAL: float = 0.9
    
    # Performance
    HISTORY_WINDOW: int = 50
    MAX_EVENTS_STORED: int = 1000
    AGENT_TIMEOUT: int = 10
    CACHE_EXPIRY_MINUTES: int = 15
    
    # Business metrics
    BASE_REVENUE_PER_MINUTE: float = 100.0
    BASE_USERS: int = 1000

config = Config()

HEADERS = {"Authorization": f"Bearer {config.HF_TOKEN}"} if config.HF_TOKEN else {}

# === Thread-Safe Data Structures ===
class ThreadSafeEventStore:
    """Thread-safe storage for reliability events"""
    
    def __init__(self, max_size: int = config.MAX_EVENTS_STORED):
        self._events = deque(maxlen=max_size)
        self._lock = threading.RLock()
        logger.info(f"Initialized ThreadSafeEventStore with max_size={max_size}")
    
    def add(self, event: ReliabilityEvent) -> None:
        """Add event to store"""
        with self._lock:
            self._events.append(event)
            logger.debug(f"Added event for {event.component}: {event.severity.value}")
    
    def get_recent(self, n: int = 15) -> List[ReliabilityEvent]:
        """Get n most recent events"""
        with self._lock:
            return list(self._events)[-n:] if self._events else []
    
    def get_all(self) -> List[ReliabilityEvent]:
        """Get all events"""
        with self._lock:
            return list(self._events)
    
    def count(self) -> int:
        """Get total event count"""
        with self._lock:
            return len(self._events)

class ThreadSafeFAISSIndex:
    """Thread-safe wrapper for FAISS index operations with batching"""
    
    def __init__(self, index, texts: List[str]):
        self.index = index
        self.texts = texts
        self._lock = threading.RLock()
        self.last_save = datetime.datetime.now()
        self.save_interval = datetime.timedelta(seconds=30)
        self.pending_vectors = []
        self.pending_texts = []
        logger.info(f"Initialized ThreadSafeFAISSIndex with {len(texts)} existing vectors")
    
    def add(self, vector: np.ndarray, text: str) -> None:
        """Add vector and text with batching"""
        with self._lock:
            self.pending_vectors.append(vector)
            self.pending_texts.append(text)
            
            # Flush if we have enough pending
            if len(self.pending_vectors) >= 10:
                self._flush()
    
    def _flush(self) -> None:
        """Flush pending vectors to index"""
        if not self.pending_vectors:
            return
        
        try:
            vectors = np.vstack(self.pending_vectors)
            self.index.add(vectors)
            self.texts.extend(self.pending_texts)
            
            logger.info(f"Flushed {len(self.pending_vectors)} vectors to FAISS index")
            
            self.pending_vectors = []
            self.pending_texts = []
            
            # Save if enough time has passed
            if datetime.datetime.now() - self.last_save > self.save_interval:
                self._save()
        except Exception as e:
            logger.error(f"Error flushing vectors: {e}", exc_info=True)
    
    def _save(self) -> None:
        """Save index to disk"""
        try:
            import faiss
            faiss.write_index(self.index, config.INDEX_FILE)
            with open(config.TEXTS_FILE, "w") as f:
                json.dump(self.texts, f)
            self.last_save = datetime.datetime.now()
            logger.info(f"Saved FAISS index with {len(self.texts)} vectors")
        except Exception as e:
            logger.error(f"Error saving index: {e}", exc_info=True)
    
    def get_count(self) -> int:
        """Get total count of vectors"""
        with self._lock:
            return len(self.texts) + len(self.pending_texts)
    
    def force_save(self) -> None:
        """Force immediate save of pending vectors"""
        with self._lock:
            self._flush()

# === FAISS & Embeddings Setup ===
try:
    from sentence_transformers import SentenceTransformer
    import faiss
    
    logger.info("Loading SentenceTransformer model...")
    model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
    logger.info("SentenceTransformer model loaded successfully")
    
    if os.path.exists(config.INDEX_FILE):
        logger.info(f"Loading existing FAISS index from {config.INDEX_FILE}")
        index = faiss.read_index(config.INDEX_FILE)
        
        # Validate dimension
        if index.d != config.VECTOR_DIM:
            logger.warning(f"Index dimension mismatch: {index.d} != {config.VECTOR_DIM}. Creating new index.")
            index = faiss.IndexFlatL2(config.VECTOR_DIM)
            incident_texts = []
        else:
            with open(config.TEXTS_FILE, "r") as f:
                incident_texts = json.load(f)
            logger.info(f"Loaded {len(incident_texts)} incident texts")
    else:
        logger.info("Creating new FAISS index")
        index = faiss.IndexFlatL2(config.VECTOR_DIM)
        incident_texts = []
    
    thread_safe_index = ThreadSafeFAISSIndex(index, incident_texts)
    
except ImportError as e:
    logger.warning(f"FAISS or SentenceTransformers not available: {e}")
    index = None
    incident_texts = []
    model = None
    thread_safe_index = None
except Exception as e:
    logger.error(f"Error initializing FAISS: {e}", exc_info=True)
    index = None
    incident_texts = []
    model = None
    thread_safe_index = None

# === Predictive Models ===
@dataclass
class ForecastResult:
    """Data class for forecast results"""
    metric: str
    predicted_value: float
    confidence: float
    trend: str  # "increasing", "decreasing", "stable"
    time_to_threshold: Optional[datetime.timedelta] = None
    risk_level: str = "low"  # low, medium, high, critical

class SimplePredictiveEngine:
    """
    Lightweight forecasting engine optimized for Hugging Face Spaces.
    Uses statistical methods for time-series prediction.
    """
    
    def __init__(self, history_window: int = config.HISTORY_WINDOW):
        self.history_window = history_window
        self.service_history: Dict[str, deque] = {}
        self.prediction_cache: Dict[str, Tuple[ForecastResult, datetime.datetime]] = {}
        self.max_cache_age = datetime.timedelta(minutes=config.CACHE_EXPIRY_MINUTES)
        self._lock = threading.RLock()
        logger.info(f"Initialized SimplePredictiveEngine with history_window={history_window}")
    
    def add_telemetry(self, service: str, event_data: Dict) -> None:
        """
        Add telemetry data to service history
        
        Args:
            service: Service name
            event_data: Dictionary containing metrics (latency_p99, error_rate, etc.)
        """
        with self._lock:
            if service not in self.service_history:
                self.service_history[service] = deque(maxlen=self.history_window)
            
            telemetry_point = {
                'timestamp': datetime.datetime.now(),
                'latency': event_data.get('latency_p99', 0),
                'error_rate': event_data.get('error_rate', 0),
                'throughput': event_data.get('throughput', 0),
                'cpu_util': event_data.get('cpu_util'),
                'memory_util': event_data.get('memory_util')
            }
            
            self.service_history[service].append(telemetry_point)
            
            # Clean expired cache
            self._clean_cache()
    
    def _clean_cache(self) -> None:
        """Remove expired entries from prediction cache"""
        now = datetime.datetime.now()
        expired = [k for k, (_, ts) in self.prediction_cache.items() 
                   if now - ts > self.max_cache_age]
        for k in expired:
            del self.prediction_cache[k]
        
        if expired:
            logger.debug(f"Cleaned {len(expired)} expired cache entries")
    
    def forecast_service_health(self, service: str, lookahead_minutes: int = 15) -> List[ForecastResult]:
        """
        Forecast service health metrics
        
        Args:
            service: Service name to forecast
            lookahead_minutes: Time horizon in minutes
            
        Returns:
            List of forecast results for different metrics
        """
        with self._lock:
            if service not in self.service_history or len(self.service_history[service]) < 10:
                return []
            
            history = list(self.service_history[service])
        
        forecasts = []
        
        # Forecast latency
        latency_forecast = self._forecast_latency(history, lookahead_minutes)
        if latency_forecast:
            forecasts.append(latency_forecast)
        
        # Forecast error rate
        error_forecast = self._forecast_error_rate(history, lookahead_minutes)
        if error_forecast:
            forecasts.append(error_forecast)
        
        # Forecast resource utilization
        resource_forecasts = self._forecast_resources(history, lookahead_minutes)
        forecasts.extend(resource_forecasts)
        
        # Cache results
        with self._lock:
            for forecast in forecasts:
                cache_key = f"{service}_{forecast.metric}"
                self.prediction_cache[cache_key] = (forecast, datetime.datetime.now())
        
        return forecasts
    
    def _forecast_latency(self, history: List, lookahead_minutes: int) -> Optional[ForecastResult]:
        """
        Forecast latency using linear regression and trend analysis
        
        Args:
            history: Historical telemetry data
            lookahead_minutes: Forecast horizon
            
        Returns:
            ForecastResult or None if insufficient data
        """
        try:
            latencies = [point['latency'] for point in history[-20:]]
            
            if len(latencies) < 5:
                return None
            
            # Simple linear trend
            x = np.arange(len(latencies))
            slope, intercept = np.polyfit(x, latencies, 1)
            
            # Predict next value
            next_x = len(latencies)
            predicted_latency = slope * next_x + intercept
            
            # Calculate confidence based on data quality
            residuals = latencies - (slope * x + intercept)
            confidence = max(0, 1 - (np.std(residuals) / max(1, np.mean(latencies))))
            
            # Determine trend and risk
            if slope > 5:
                trend = "increasing"
                risk = "critical" if predicted_latency > config.LATENCY_EXTREME else "high"
            elif slope < -2:
                trend = "decreasing" 
                risk = "low"
            else:
                trend = "stable"
                risk = "low" if predicted_latency < config.LATENCY_WARNING else "medium"
            
            # Calculate time to reach critical threshold (500ms)
            time_to_critical = None
            if slope > 0 and predicted_latency < config.LATENCY_EXTREME:
                denominator = predicted_latency - latencies[-1]
                if abs(denominator) > 0.1:  # Avoid division by very small numbers
                    minutes_to_critical = lookahead_minutes * (config.LATENCY_EXTREME - predicted_latency) / denominator
                    if minutes_to_critical > 0:
                        time_to_critical = datetime.timedelta(minutes=minutes_to_critical)
            
            return ForecastResult(
                metric="latency",
                predicted_value=predicted_latency,
                confidence=confidence,
                trend=trend,
                time_to_threshold=time_to_critical,
                risk_level=risk
            )
            
        except Exception as e:
            logger.error(f"Latency forecast error: {e}", exc_info=True)
            return None
    
    def _forecast_error_rate(self, history: List, lookahead_minutes: int) -> Optional[ForecastResult]:
        """
        Forecast error rate using exponential smoothing
        
        Args:
            history: Historical telemetry data
            lookahead_minutes: Forecast horizon
            
        Returns:
            ForecastResult or None if insufficient data
        """
        try:
            error_rates = [point['error_rate'] for point in history[-15:]]
            
            if len(error_rates) < 5:
                return None
            
            # Exponential smoothing
            alpha = 0.3
            forecast = error_rates[0]
            for rate in error_rates[1:]:
                forecast = alpha * rate + (1 - alpha) * forecast
            
            predicted_rate = forecast
            
            # Trend analysis
            recent_trend = np.mean(error_rates[-3:]) - np.mean(error_rates[-6:-3])
            
            if recent_trend > 0.02:
                trend = "increasing"
                risk = "critical" if predicted_rate > config.ERROR_RATE_CRITICAL else "high"
            elif recent_trend < -0.01:
                trend = "decreasing"
                risk = "low"
            else:
                trend = "stable"
                risk = "low" if predicted_rate < config.ERROR_RATE_WARNING else "medium"
            
            # Confidence based on volatility
            confidence = max(0, 1 - (np.std(error_rates) / max(0.01, np.mean(error_rates))))
            
            return ForecastResult(
                metric="error_rate",
                predicted_value=predicted_rate,
                confidence=confidence,
                trend=trend,
                risk_level=risk
            )
            
        except Exception as e:
            logger.error(f"Error rate forecast error: {e}", exc_info=True)
            return None
    
    def _forecast_resources(self, history: List, lookahead_minutes: int) -> List[ForecastResult]:
        """
        Forecast CPU and memory utilization
        
        Args:
            history: Historical telemetry data
            lookahead_minutes: Forecast horizon
            
        Returns:
            List of forecast results for CPU and memory
        """
        forecasts = []
        
        # CPU forecast
        cpu_values = [point['cpu_util'] for point in history if point.get('cpu_util') is not None]
        if len(cpu_values) >= 5:
            try:
                predicted_cpu = np.mean(cpu_values[-5:])
                trend = "increasing" if cpu_values[-1] > np.mean(cpu_values[-10:-5]) else "stable"
                
                risk = "low"
                if predicted_cpu > config.CPU_CRITICAL:
                    risk = "critical"
                elif predicted_cpu > config.CPU_WARNING:
                    risk = "high"
                elif predicted_cpu > 0.7:
                    risk = "medium"
                
                forecasts.append(ForecastResult(
                    metric="cpu_util",
                    predicted_value=predicted_cpu,
                    confidence=0.7,
                    trend=trend,
                    risk_level=risk
                ))
            except Exception as e:
                logger.error(f"CPU forecast error: {e}", exc_info=True)
        
        # Memory forecast
        memory_values = [point['memory_util'] for point in history if point.get('memory_util') is not None]
        if len(memory_values) >= 5:
            try:
                predicted_memory = np.mean(memory_values[-5:])
                trend = "increasing" if memory_values[-1] > np.mean(memory_values[-10:-5]) else "stable"
                
                risk = "low"
                if predicted_memory > config.MEMORY_CRITICAL:
                    risk = "critical"
                elif predicted_memory > config.MEMORY_WARNING:
                    risk = "high"
                elif predicted_memory > 0.7:
                    risk = "medium"
                
                forecasts.append(ForecastResult(
                    metric="memory_util",
                    predicted_value=predicted_memory,
                    confidence=0.7,
                    trend=trend,
                    risk_level=risk
                ))
            except Exception as e:
                logger.error(f"Memory forecast error: {e}", exc_info=True)
        
        return forecasts
    
    def get_predictive_insights(self, service: str) -> Dict[str, Any]:
        """
        Generate actionable insights from forecasts
        
        Args:
            service: Service name
            
        Returns:
            Dictionary containing warnings, recommendations, and forecast data
        """
        forecasts = self.forecast_service_health(service)
        
        critical_risks = [f for f in forecasts if f.risk_level in ["high", "critical"]]
        warnings = []
        recommendations = []
        
        for forecast in critical_risks:
            if forecast.metric == "latency" and forecast.risk_level in ["high", "critical"]:
                warnings.append(f"๐Ÿ“ˆ Latency expected to reach {forecast.predicted_value:.0f}ms")
                if forecast.time_to_threshold:
                    minutes = int(forecast.time_to_threshold.total_seconds() / 60)
                    recommendations.append(f"โฐ Critical latency (~500ms) in ~{minutes} minutes")
                recommendations.append("๐Ÿ”ง Consider scaling or optimizing dependencies")
            
            elif forecast.metric == "error_rate" and forecast.risk_level in ["high", "critical"]:
                warnings.append(f"๐Ÿšจ Errors expected to reach {forecast.predicted_value*100:.1f}%")
                recommendations.append("๐Ÿ› Investigate recent deployments or dependency issues")
            
            elif forecast.metric == "cpu_util" and forecast.risk_level in ["high", "critical"]:
                warnings.append(f"๐Ÿ”ฅ CPU expected at {forecast.predicted_value*100:.1f}%")
                recommendations.append("โšก Consider scaling compute resources")
            
            elif forecast.metric == "memory_util" and forecast.risk_level in ["high", "critical"]:
                warnings.append(f"๐Ÿ’พ Memory expected at {forecast.predicted_value*100:.1f}%")
                recommendations.append("๐Ÿงน Check for memory leaks or optimize usage")
        
        return {
            'service': service,
            'forecasts': [asdict(f) for f in forecasts],
            'warnings': warnings[:3],
            'recommendations': list(dict.fromkeys(recommendations))[:3],
            'critical_risk_count': len(critical_risks),
            'forecast_timestamp': datetime.datetime.now().isoformat()
        }

# === Core Engine Components ===
policy_engine = PolicyEngine()
events_history_store = ThreadSafeEventStore()
predictive_engine = SimplePredictiveEngine()

class BusinessImpactCalculator:
    """
    Calculate business impact of anomalies including revenue loss
    and user impact estimation
    """
    
    def __init__(self, revenue_per_request: float = 0.01):
        self.revenue_per_request = revenue_per_request
        logger.info(f"Initialized BusinessImpactCalculator with revenue_per_request={revenue_per_request}")
    
    def calculate_impact(self, event: ReliabilityEvent, duration_minutes: int = 5) -> Dict[str, Any]:
        """
        Calculate business impact for a reliability event
        
        Args:
            event: The reliability event to analyze
            duration_minutes: Assumed duration of the incident
            
        Returns:
            Dictionary containing revenue loss, user impact, and severity
        """
        base_revenue_per_minute = config.BASE_REVENUE_PER_MINUTE
        
        impact_multiplier = 1.0
        
        # Impact factors
        if event.latency_p99 > config.LATENCY_CRITICAL:
            impact_multiplier += 0.5
        if event.error_rate > 0.1:
            impact_multiplier += 0.8
        if event.cpu_util and event.cpu_util > config.CPU_CRITICAL:
            impact_multiplier += 0.3
        
        revenue_loss = base_revenue_per_minute * impact_multiplier * (duration_minutes / 60)
        
        base_users_affected = config.BASE_USERS
        user_impact_multiplier = (event.error_rate * 10) + (max(0, event.latency_p99 - 100) / 500)
        affected_users = int(base_users_affected * user_impact_multiplier)
        
        # Severity classification
        if revenue_loss > 500 or affected_users > 5000:
            severity = "CRITICAL"
        elif revenue_loss > 100 or affected_users > 1000:
            severity = "HIGH"
        elif revenue_loss > 50 or affected_users > 500:
            severity = "MEDIUM"
        else:
            severity = "LOW"
        
        logger.info(f"Business impact: ${revenue_loss:.2f} revenue loss, {affected_users} users, {severity} severity")
        
        return {
            'revenue_loss_estimate': round(revenue_loss, 2),
            'affected_users_estimate': affected_users,
            'severity_level': severity,
            'throughput_reduction_pct': round(min(100, user_impact_multiplier * 100), 1)
        }

business_calculator = BusinessImpactCalculator()

class AdvancedAnomalyDetector:
    """
    Enhanced anomaly detection with adaptive thresholds that learn
    from historical data patterns
    """
    
    def __init__(self):
        self.historical_data = deque(maxlen=100)
        self.adaptive_thresholds = {
            'latency_p99': config.LATENCY_WARNING,
            'error_rate': config.ERROR_RATE_WARNING
        }
        self._lock = threading.RLock()
        logger.info("Initialized AdvancedAnomalyDetector")
    
    def detect_anomaly(self, event: ReliabilityEvent) -> bool:
        """
        Detect if event is anomalous using adaptive thresholds
        
        Args:
            event: The reliability event to check
            
        Returns:
            True if anomaly detected, False otherwise
        """
        with self._lock:
            latency_anomaly = event.latency_p99 > self.adaptive_thresholds['latency_p99']
            error_anomaly = event.error_rate > self.adaptive_thresholds['error_rate']
            
            resource_anomaly = False
            if event.cpu_util and event.cpu_util > config.CPU_CRITICAL:
                resource_anomaly = True
            if event.memory_util and event.memory_util > config.MEMORY_CRITICAL:
                resource_anomaly = True
            
            self._update_thresholds(event)
            
            is_anomaly = latency_anomaly or error_anomaly or resource_anomaly
            
            if is_anomaly:
                logger.info(f"Anomaly detected for {event.component}: latency={latency_anomaly}, error={error_anomaly}, resource={resource_anomaly}")
            
            return is_anomaly
    
    def _update_thresholds(self, event: ReliabilityEvent) -> None:
        """Update adaptive thresholds based on historical data"""
        self.historical_data.append(event)
        
        if len(self.historical_data) > 10:
            recent_latencies = [e.latency_p99 for e in list(self.historical_data)[-20:]]
            new_threshold = np.percentile(recent_latencies, 90)
            self.adaptive_thresholds['latency_p99'] = new_threshold
            logger.debug(f"Updated adaptive latency threshold to {new_threshold:.2f}ms")

anomaly_detector = AdvancedAnomalyDetector()

# === Multi-Agent System ===
class AgentSpecialization(Enum):
    """Agent specialization types"""
    DETECTIVE = "anomaly_detection"
    DIAGNOSTICIAN = "root_cause_analysis"
    PREDICTIVE = "predictive_analytics"

class BaseAgent:
    """Base class for all specialized agents"""
    
    def __init__(self, specialization: AgentSpecialization):
        self.specialization = specialization
        self.performance_metrics = {
            'processed_events': 0,
            'successful_analyses': 0,
            'average_confidence': 0.0
        }
    
    async def analyze(self, event: ReliabilityEvent) -> Dict[str, Any]:
        """Base analysis method to be implemented by specialized agents"""
        raise NotImplementedError

class AnomalyDetectionAgent(BaseAgent):
    """
    Specialized agent for anomaly detection and pattern recognition.
    Calculates multi-dimensional anomaly scores and identifies affected metrics.
    """
    
    def __init__(self):
        super().__init__(AgentSpecialization.DETECTIVE)
        logger.info("Initialized AnomalyDetectionAgent")
    
    async def analyze(self, event: ReliabilityEvent) -> Dict[str, Any]:
        """
        Perform comprehensive anomaly analysis
        
        Args:
            event: Reliability event to analyze
            
        Returns:
            Dictionary containing anomaly score, severity, affected metrics, and recommendations
        """
        try:
            anomaly_score = self._calculate_anomaly_score(event)
            
            return {
                'specialization': self.specialization.value,
                'confidence': anomaly_score,
                'findings': {
                    'anomaly_score': anomaly_score,
                    'severity_tier': self._classify_severity(anomaly_score),
                    'primary_metrics_affected': self._identify_affected_metrics(event)
                },
                'recommendations': self._generate_detection_recommendations(event, anomaly_score)
            }
        except Exception as e:
            logger.error(f"AnomalyDetectionAgent error: {e}", exc_info=True)
            return {
                'specialization': self.specialization.value,
                'confidence': 0.0,
                'findings': {},
                'recommendations': [f"Analysis error: {str(e)}"]
            }
    
    def _calculate_anomaly_score(self, event: ReliabilityEvent) -> float:
        """
        Calculate comprehensive anomaly score (0-1) using weighted metrics
        
        Args:
            event: Reliability event
            
        Returns:
            Float between 0 and 1 representing anomaly severity
        """
        scores = []
        
        # Latency anomaly (weighted 40%)
        if event.latency_p99 > config.LATENCY_WARNING:
            latency_score = min(1.0, (event.latency_p99 - config.LATENCY_WARNING) / 500)
            scores.append(0.4 * latency_score)
        
        # Error rate anomaly (weighted 30%)
        if event.error_rate > config.ERROR_RATE_WARNING:
            error_score = min(1.0, event.error_rate / 0.3)
            scores.append(0.3 * error_score)
        
        # Resource anomaly (weighted 30%)
        resource_score = 0
        if event.cpu_util and event.cpu_util > config.CPU_WARNING:
            resource_score += 0.15 * min(1.0, (event.cpu_util - config.CPU_WARNING) / 0.2)
        if event.memory_util and event.memory_util > config.MEMORY_WARNING:
            resource_score += 0.15 * min(1.0, (event.memory_util - config.MEMORY_WARNING) / 0.2)
        scores.append(resource_score)
        
        return min(1.0, sum(scores))
    
    def _classify_severity(self, anomaly_score: float) -> str:
        """
        Classify severity tier based on anomaly score
        
        Args:
            anomaly_score: Score between 0 and 1
            
        Returns:
            Severity tier string (LOW, MEDIUM, HIGH, CRITICAL)
        """
        if anomaly_score > 0.8:
            return "CRITICAL"
        elif anomaly_score > 0.6:
            return "HIGH"
        elif anomaly_score > 0.4:
            return "MEDIUM"
        else:
            return "LOW"
    
    def _identify_affected_metrics(self, event: ReliabilityEvent) -> List[Dict[str, Any]]:
        """
        Identify which metrics are outside normal ranges
        
        Args:
            event: Reliability event
            
        Returns:
            List of dictionaries describing affected metrics with severity
        """
        affected = []
        
        # Latency checks
        if event.latency_p99 > config.LATENCY_EXTREME:
            affected.append({
                "metric": "latency", 
                "value": event.latency_p99, 
                "severity": "CRITICAL", 
                "threshold": config.LATENCY_WARNING
            })
        elif event.latency_p99 > config.LATENCY_CRITICAL:
            affected.append({
                "metric": "latency", 
                "value": event.latency_p99, 
                "severity": "HIGH", 
                "threshold": config.LATENCY_WARNING
            })
        elif event.latency_p99 > config.LATENCY_WARNING:
            affected.append({
                "metric": "latency", 
                "value": event.latency_p99, 
                "severity": "MEDIUM", 
                "threshold": config.LATENCY_WARNING
            })
        
        # Error rate checks
        if event.error_rate > config.ERROR_RATE_CRITICAL:
            affected.append({
                "metric": "error_rate", 
                "value": event.error_rate, 
                "severity": "CRITICAL", 
                "threshold": config.ERROR_RATE_WARNING
            })
        elif event.error_rate > config.ERROR_RATE_HIGH:
            affected.append({
                "metric": "error_rate", 
                "value": event.error_rate, 
                "severity": "HIGH", 
                "threshold": config.ERROR_RATE_WARNING
            })
        elif event.error_rate > config.ERROR_RATE_WARNING:
            affected.append({
                "metric": "error_rate", 
                "value": event.error_rate, 
                "severity": "MEDIUM", 
                "threshold": config.ERROR_RATE_WARNING
            })
        
        # CPU checks
        if event.cpu_util and event.cpu_util > config.CPU_CRITICAL:
            affected.append({
                "metric": "cpu", 
                "value": event.cpu_util, 
                "severity": "CRITICAL", 
                "threshold": config.CPU_WARNING
            })
        elif event.cpu_util and event.cpu_util > config.CPU_WARNING:
            affected.append({
                "metric": "cpu", 
                "value": event.cpu_util, 
                "severity": "HIGH", 
                "threshold": config.CPU_WARNING
            })
        
        # Memory checks
        if event.memory_util and event.memory_util > config.MEMORY_CRITICAL:
            affected.append({
                "metric": "memory", 
                "value": event.memory_util, 
                "severity": "CRITICAL", 
                "threshold": config.MEMORY_WARNING
            })
        elif event.memory_util and event.memory_util > config.MEMORY_WARNING:
            affected.append({
                "metric": "memory", 
                "value": event.memory_util, 
                "severity": "HIGH", 
                "threshold": config.MEMORY_WARNING
            })
        
        return affected
    
    def _generate_detection_recommendations(self, event: ReliabilityEvent, anomaly_score: float) -> List[str]:
        """
        Generate actionable recommendations based on detected anomalies
        
        Args:
            event: Reliability event
            anomaly_score: Calculated anomaly score
            
        Returns:
            List of recommendation strings with emojis for visibility
        """
        recommendations = []
        affected_metrics = self._identify_affected_metrics(event)
        
        for metric in affected_metrics:
            metric_name = metric["metric"]
            severity = metric["severity"]
            value = metric["value"]
            threshold = metric["threshold"]
            
            if metric_name == "latency":
                if severity == "CRITICAL":
                    recommendations.append(
                        f"๐Ÿšจ CRITICAL: Latency {value:.0f}ms (>{threshold}ms) - "
                        f"Check database & external dependencies"
                    )
                elif severity == "HIGH":
                    recommendations.append(
                        f"โš ๏ธ HIGH: Latency {value:.0f}ms (>{threshold}ms) - "
                        f"Investigate service performance"
                    )
                else:
                    recommendations.append(
                        f"๐Ÿ“ˆ Latency elevated: {value:.0f}ms (>{threshold}ms) - Monitor trend"
                    )
            
            elif metric_name == "error_rate":
                if severity == "CRITICAL":
                    recommendations.append(
                        f"๐Ÿšจ CRITICAL: Error rate {value*100:.1f}% (>{threshold*100:.1f}%) - "
                        f"Check recent deployments"
                    )
                elif severity == "HIGH":
                    recommendations.append(
                        f"โš ๏ธ HIGH: Error rate {value*100:.1f}% (>{threshold*100:.1f}%) - "
                        f"Review application logs"
                    )
                else:
                    recommendations.append(
                        f"๐Ÿ“ˆ Errors increasing: {value*100:.1f}% (>{threshold*100:.1f}%)"
                    )
            
            elif metric_name == "cpu":
                recommendations.append(
                    f"๐Ÿ”ฅ CPU {severity}: {value*100:.1f}% utilization - Consider scaling"
                )
            
            elif metric_name == "memory":
                recommendations.append(
                    f"๐Ÿ’พ Memory {severity}: {value*100:.1f}% utilization - Check for memory leaks"
                )
        
        # Overall severity recommendations
        if anomaly_score > 0.8:
            recommendations.append("๐ŸŽฏ IMMEDIATE ACTION REQUIRED: Multiple critical metrics affected")
        elif anomaly_score > 0.6:
            recommendations.append("๐ŸŽฏ INVESTIGATE: Significant performance degradation detected")
        elif anomaly_score > 0.4:
            recommendations.append("๐Ÿ“Š MONITOR: Early warning signs detected")
        
        return recommendations[:4]  # Return top 4 recommendations

class RootCauseAgent(BaseAgent):
    """
    Specialized agent for root cause analysis.
    Analyzes failure patterns and provides investigation guidance.
    """
    
    def __init__(self):
        super().__init__(AgentSpecialization.DIAGNOSTICIAN)
        logger.info("Initialized RootCauseAgent")
    
    async def analyze(self, event: ReliabilityEvent) -> Dict[str, Any]:
        """
        Perform root cause analysis
        
        Args:
            event: Reliability event to analyze
            
        Returns:
            Dictionary containing likely root causes and investigation guidance
        """
        try:
            causes = self._analyze_potential_causes(event)
            
            return {
                'specialization': self.specialization.value,
                'confidence': 0.7,
                'findings': {
                    'likely_root_causes': causes,
                    'evidence_patterns': self._identify_evidence(event),
                    'investigation_priority': self._prioritize_investigation(causes)
                },
                'recommendations': [
                    f"Check {cause['cause']} for issues" for cause in causes[:2]
                ]
            }
        except Exception as e:
            logger.error(f"RootCauseAgent error: {e}", exc_info=True)
            return {
                'specialization': self.specialization.value,
                'confidence': 0.0,
                'findings': {},
                'recommendations': [f"Analysis error: {str(e)}"]
            }
    
    def _analyze_potential_causes(self, event: ReliabilityEvent) -> List[Dict[str, Any]]:
        """
        Analyze potential root causes based on event patterns
        
        Args:
            event: Reliability event
            
        Returns:
            List of potential root causes with confidence scores
        """
        causes = []
        
        # Pattern 1: Database/External Dependency Failure
        if event.latency_p99 > config.LATENCY_EXTREME and event.error_rate > 0.2:
            causes.append({
                "cause": "Database/External Dependency Failure",
                "confidence": 0.85,
                "evidence": f"Extreme latency ({event.latency_p99:.0f}ms) with high errors ({event.error_rate*100:.1f}%)",
                "investigation": "Check database connection pool, external API health"
            })
        
        # Pattern 2: Resource Exhaustion
        if (event.cpu_util and event.cpu_util > config.CPU_CRITICAL and 
            event.memory_util and event.memory_util > config.MEMORY_CRITICAL):
            causes.append({
                "cause": "Resource Exhaustion",
                "confidence": 0.90,
                "evidence": f"CPU ({event.cpu_util*100:.1f}%) and Memory ({event.memory_util*100:.1f}%) critically high",
                "investigation": "Check for memory leaks, infinite loops, insufficient resources"
            })
        
        # Pattern 3: Application Bug / Configuration Issue
        if event.error_rate > config.ERROR_RATE_CRITICAL and event.latency_p99 < 200:
            causes.append({
                "cause": "Application Bug / Configuration Issue",
                "confidence": 0.75,
                "evidence": f"High error rate ({event.error_rate*100:.1f}%) without latency impact",
                "investigation": "Review recent deployments, configuration changes, application logs"
            })
        
        # Pattern 4: Gradual Performance Degradation
        if (200 <= event.latency_p99 <= 400 and 
            config.ERROR_RATE_WARNING <= event.error_rate <= config.ERROR_RATE_HIGH):
            causes.append({
                "cause": "Gradual Performance Degradation",
                "confidence": 0.65,
                "evidence": f"Moderate latency ({event.latency_p99:.0f}ms) and errors ({event.error_rate*100:.1f}%)",
                "investigation": "Check resource trends, dependency performance, capacity planning"
            })
        
        # Default: Unknown pattern
        if not causes:
            causes.append({
                "cause": "Unknown - Requires Investigation",
                "confidence": 0.3,
                "evidence": "Pattern does not match known failure modes",
                "investigation": "Complete system review needed"
            })
        
        return causes
    
    def _identify_evidence(self, event: ReliabilityEvent) -> List[str]:
        """
        Identify evidence patterns in the event data
        
        Args:
            event: Reliability event
            
        Returns:
            List of evidence pattern identifiers
        """
        evidence = []
        
        if event.latency_p99 > event.error_rate * 1000:
            evidence.append("latency_disproportionate_to_errors")
        
        if (event.cpu_util and event.cpu_util > config.CPU_WARNING and 
            event.memory_util and event.memory_util > config.MEMORY_WARNING):
            evidence.append("correlated_resource_exhaustion")
        
        if event.error_rate > config.ERROR_RATE_HIGH and event.latency_p99 < config.LATENCY_CRITICAL:
            evidence.append("errors_without_latency_impact")
        
        return evidence
    
    def _prioritize_investigation(self, causes: List[Dict[str, Any]]) -> str:
        """
        Determine investigation priority based on identified causes
        
        Args:
            causes: List of potential root causes
            
        Returns:
            Priority level (HIGH, MEDIUM, LOW)
        """
        for cause in causes:
            if "Database" in cause["cause"] or "Resource Exhaustion" in cause["cause"]:
                return "HIGH"
        return "MEDIUM"

class PredictiveAgent(BaseAgent):
    """
    Specialized agent for predictive analytics.
    Forecasts future risks and trends using statistical models.
    """
    
    def __init__(self):
        super().__init__(AgentSpecialization.PREDICTIVE)
        self.engine = predictive_engine
        logger.info("Initialized PredictiveAgent")
    
    async def analyze(self, event: ReliabilityEvent) -> Dict[str, Any]:
        """
        Perform predictive analysis for future risks
        
        Args:
            event: Current reliability event
            
        Returns:
            Dictionary containing forecasts and predictive insights
        """
        try:
            event_data = {
                'latency_p99': event.latency_p99,
                'error_rate': event.error_rate,
                'throughput': event.throughput,
                'cpu_util': event.cpu_util,
                'memory_util': event.memory_util
            }
            self.engine.add_telemetry(event.component, event_data)
            
            insights = self.engine.get_predictive_insights(event.component)
            
            return {
                'specialization': self.specialization.value,
                'confidence': 0.8 if insights['critical_risk_count'] > 0 else 0.5,
                'findings': insights,
                'recommendations': insights['recommendations']
            }
        except Exception as e:
            logger.error(f"PredictiveAgent error: {e}", exc_info=True)
            return {
                'specialization': self.specialization.value,
                'confidence': 0.0,
                'findings': {},
                'recommendations': [f"Analysis error: {str(e)}"]
            }

class OrchestrationManager:
    """
    Orchestrates multiple specialized agents for comprehensive analysis.
    Runs agents in parallel and synthesizes their findings.
    """
    
    def __init__(self):
        self.agents = {
            AgentSpecialization.DETECTIVE: AnomalyDetectionAgent(),
            AgentSpecialization.DIAGNOSTICIAN: RootCauseAgent(),
            AgentSpecialization.PREDICTIVE: PredictiveAgent(),
        }
        logger.info(f"Initialized OrchestrationManager with {len(self.agents)} agents")
    
    async def orchestrate_analysis(self, event: ReliabilityEvent) -> Dict[str, Any]:
        """
        Coordinate multiple agents for comprehensive analysis
        
        Args:
            event: Reliability event to analyze
            
        Returns:
            Synthesized findings from all agents
        """
        agent_tasks = {
            spec: agent.analyze(event)
            for spec, agent in self.agents.items()
        }
        
        # Parallel agent execution with timeout protection
        agent_results = {}
        for specialization, task in agent_tasks.items():
            try:
                result = await asyncio.wait_for(task, timeout=5.0)
                agent_results[specialization.value] = result
                logger.debug(f"Agent {specialization.value} completed successfully")
            except asyncio.TimeoutError:
                logger.warning(f"Agent {specialization.value} timed out")
                continue
            except Exception as e:
                logger.error(f"Agent {specialization.value} error: {e}", exc_info=True)
                continue
        
        return self._synthesize_agent_findings(event, agent_results)
    
    def _synthesize_agent_findings(self, event: ReliabilityEvent, agent_results: Dict) -> Dict[str, Any]:
        """
        Combine insights from all specialized agents
        
        Args:
            event: Original reliability event
            agent_results: Results from each agent
            
        Returns:
            Synthesized analysis combining all agent findings
        """
        detective_result = agent_results.get(AgentSpecialization.DETECTIVE.value)
        diagnostician_result = agent_results.get(AgentSpecialization.DIAGNOSTICIAN.value)
        predictive_result = agent_results.get(AgentSpecialization.PREDICTIVE.value)
        
        if not detective_result:
            logger.warning("No detective agent results available")
            return {'error': 'No agent results available'}
        
        synthesis = {
            'incident_summary': {
                'severity': detective_result['findings'].get('severity_tier', 'UNKNOWN'),
                'anomaly_confidence': detective_result['confidence'],
                'primary_metrics_affected': [
                    metric["metric"] for metric in 
                    detective_result['findings'].get('primary_metrics_affected', [])
                ]
            },
            'root_cause_insights': diagnostician_result['findings'] if diagnostician_result else {},
            'predictive_insights': predictive_result['findings'] if predictive_result else {},
            'recommended_actions': self._prioritize_actions(
                detective_result.get('recommendations', []),
                diagnostician_result.get('recommendations', []) if diagnostician_result else [],
                predictive_result.get('recommendations', []) if predictive_result else []
            ),
            'agent_metadata': {
                'participating_agents': list(agent_results.keys()),
                'analysis_timestamp': datetime.datetime.now().isoformat()
            }
        }
        
        return synthesis
    
    def _prioritize_actions(self, detection_actions: List[str], 
                          diagnosis_actions: List[str], 
                          predictive_actions: List[str]) -> List[str]:
        """
        Combine and prioritize actions from multiple agents
        
        Args:
            detection_actions: Actions from detective agent
            diagnosis_actions: Actions from diagnostician agent
            predictive_actions: Actions from predictive agent
            
        Returns:
            Prioritized list of unique actions
        """
        all_actions = detection_actions + diagnosis_actions + predictive_actions
        seen = set()
        unique_actions = []
        for action in all_actions:
            if action not in seen:
                seen.add(action)
                unique_actions.append(action)
        return unique_actions[:5]  # Return top 5 actions

# Initialize orchestration manager
orchestration_manager = OrchestrationManager()

# === Enhanced Reliability Engine ===
class EnhancedReliabilityEngine:
    """
    Main engine for processing reliability events through the multi-agent system.
    Coordinates anomaly detection, agent analysis, policy evaluation, and impact calculation.
    """
    
    def __init__(self):
        self.performance_metrics = {
            'total_incidents_processed': 0,
            'multi_agent_analyses': 0,
            'anomalies_detected': 0
        }
        self._lock = threading.RLock()
        logger.info("Initialized EnhancedReliabilityEngine")
    
    async def process_event_enhanced(
        self, 
        component: str, 
        latency: float, 
        error_rate: float,
        throughput: float = 1000, 
        cpu_util: Optional[float] = None,
        memory_util: Optional[float] = None
    ) -> Dict[str, Any]:
        """
        Process a reliability event through the complete analysis pipeline
        
        Args:
            component: Service component name
            latency: P99 latency in milliseconds
            error_rate: Error rate (0-1)
            throughput: Requests per second
            cpu_util: CPU utilization (0-1)
            memory_util: Memory utilization (0-1)
            
        Returns:
            Comprehensive analysis results including agent findings, healing actions, and business impact
        """
        logger.info(f"Processing event for {component}: latency={latency}ms, error_rate={error_rate*100:.1f}%")
        
        # Create event
        event = ReliabilityEvent(
            component=component,
            latency_p99=latency,
            error_rate=error_rate,
            throughput=throughput,
            cpu_util=cpu_util,
            memory_util=memory_util,
            upstream_deps=["auth-service", "database"] if component == "api-service" else []
        )
        
        # Multi-agent analysis
        agent_analysis = await orchestration_manager.orchestrate_analysis(event)
        
        # Anomaly detection
        is_anomaly = anomaly_detector.detect_anomaly(event)

        # Determine severity based on agent confidence
        agent_confidence = 0.0
        if agent_analysis and 'incident_summary' in agent_analysis:
            agent_confidence = agent_analysis.get('incident_summary', {}).get('anomaly_confidence', 0)
        else:
            agent_confidence = 0.8 if is_anomaly else 0.1

        # Set event severity
        if agent_confidence > 0.8:
            event.severity = EventSeverity.CRITICAL
        elif agent_confidence > 0.6:
            event.severity = EventSeverity.HIGH  
        elif agent_confidence > 0.4:
            event.severity = EventSeverity.MEDIUM
        else:
            event.severity = EventSeverity.LOW
        
        # Evaluate healing policies
        healing_actions = policy_engine.evaluate_policies(event)
        
        # Calculate business impact
        business_impact = business_calculator.calculate_impact(event) if is_anomaly else None
        
        # Store in vector database for similarity detection
        if thread_safe_index is not None and model is not None and is_anomaly:
            try:
                analysis_text = agent_analysis.get('recommended_actions', ['No analysis'])[0]
                vector_text = f"{component} {latency} {error_rate} {analysis_text}"
                vec = model.encode([vector_text])
                thread_safe_index.add(np.array(vec, dtype=np.float32), vector_text)
            except Exception as e:
                logger.error(f"Error storing vector: {e}", exc_info=True)
        
        # Build comprehensive result
        result = {
            "timestamp": event.timestamp,
            "component": component,
            "latency_p99": latency,
            "error_rate": error_rate,
            "throughput": throughput,
            "status": "ANOMALY" if is_anomaly else "NORMAL",
            "multi_agent_analysis": agent_analysis,
            "healing_actions": [action.value for action in healing_actions],
            "business_impact": business_impact,
            "severity": event.severity.value,
            "similar_incidents_count": thread_safe_index.get_count() if thread_safe_index and is_anomaly else 0,
            "processing_metadata": {
                "agents_used": agent_analysis.get('agent_metadata', {}).get('participating_agents', []),
                "analysis_confidence": agent_analysis.get('incident_summary', {}).get('anomaly_confidence', 0)
            }
        }
        
        # Store event in history
        events_history_store.add(event)
        
        # Update performance metrics
        with self._lock:
            self.performance_metrics['total_incidents_processed'] += 1
            self.performance_metrics['multi_agent_analyses'] += 1
            if is_anomaly:
                self.performance_metrics['anomalies_detected'] += 1
        
        logger.info(f"Event processed: {result['status']} with {result['severity']} severity")
        
        return result

# Initialize enhanced engine
enhanced_engine = EnhancedReliabilityEngine()

# === Input Validation ===
def validate_inputs(
    latency: float, 
    error_rate: float, 
    throughput: float, 
    cpu_util: Optional[float], 
    memory_util: Optional[float]
) -> Tuple[bool, str]:
    """
    Validate user inputs for bounds and type correctness
    
    Args:
        latency: Latency value in milliseconds
        error_rate: Error rate (0-1)
        throughput: Throughput in requests/sec
        cpu_util: CPU utilization (0-1)
        memory_util: Memory utilization (0-1)
        
    Returns:
        Tuple of (is_valid: bool, error_message: str)
    """
    if not (0 <= latency <= 10000):
        return False, "โŒ Invalid latency: must be between 0-10000ms"
    if not (0 <= error_rate <= 1):
        return False, "โŒ Invalid error rate: must be between 0-1"
    if throughput < 0:
        return False, "โŒ Invalid throughput: must be positive"
    if cpu_util is not None and not (0 <= cpu_util <= 1):
        return False, "โŒ Invalid CPU utilization: must be between 0-1"
    if memory_util is not None and not (0 <= memory_util <= 1):
        return False, "โŒ Invalid memory utilization: must be between 0-1"
    
    return True, ""

# === Gradio UI ===
def create_enhanced_ui():
    """
    Create the comprehensive Gradio UI for the reliability framework.
    Includes telemetry input, multi-agent analysis display, predictive insights,
    and event history visualization.
    """
    
    with gr.Blocks(title="๐Ÿง  Enterprise Agentic Reliability Framework", theme="soft") as demo:
        gr.Markdown("""
        # ๐Ÿง  Enterprise Agentic Reliability Framework
        **Multi-Agent AI System for Production Reliability**
        
        *Specialized AI agents working together to detect, diagnose, predict, and heal system issues*
        """)
        
        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown("### ๐Ÿ“Š Telemetry Input")
                component = gr.Dropdown(
                    choices=["api-service", "auth-service", "payment-service", "database", "cache-service"],
                    value="api-service",
                    label="Component",
                    info="Select the service being monitored"
                )
                latency = gr.Slider(
                    minimum=10, maximum=1000, value=100, step=1,
                    label="Latency P99 (ms)",
                    info=f"Alert threshold: >{config.LATENCY_WARNING}ms (adaptive)"
                )
                error_rate = gr.Slider(
                    minimum=0, maximum=0.5, value=0.02, step=0.001,
                    label="Error Rate",
                    info=f"Alert threshold: >{config.ERROR_RATE_WARNING}"
                )
                throughput = gr.Number(
                    value=1000,
                    label="Throughput (req/sec)",
                    info="Current request rate"
                )
                cpu_util = gr.Slider(
                    minimum=0, maximum=1, value=0.4, step=0.01,
                    label="CPU Utilization",
                    info="0.0 - 1.0 scale"
                )
                memory_util = gr.Slider(
                    minimum=0, maximum=1, value=0.3, step=0.01,
                    label="Memory Utilization", 
                    info="0.0 - 1.0 scale"
                )
                submit_btn = gr.Button("๐Ÿš€ Submit Telemetry Event", variant="primary", size="lg")
                
            with gr.Column(scale=2):
                gr.Markdown("### ๐Ÿ” Multi-Agent Analysis")
                output_text = gr.Textbox(
                    label="Agent Synthesis",
                    placeholder="AI agents are analyzing...",
                    lines=6
                )
                
                with gr.Accordion("๐Ÿค– Agent Specialists Analysis", open=False):
                    gr.Markdown("""
                    **Specialized AI Agents:**
                    - ๐Ÿ•ต๏ธ **Detective**: Anomaly detection & pattern recognition
                    - ๐Ÿ” **Diagnostician**: Root cause analysis & investigation  
                    - ๐Ÿ”ฎ **Predictive**: Future risk forecasting & trend analysis
                    """)
                    
                    agent_insights = gr.JSON(
                        label="Detailed Agent Findings",
                        value={}
                    )
                
                with gr.Accordion("๐Ÿ”ฎ Predictive Analytics & Forecasting", open=False):
                    gr.Markdown("""
                    **Future Risk Forecasting:**
                    - ๐Ÿ“ˆ Latency trends and thresholds
                    - ๐Ÿšจ Error rate predictions  
                    - ๐Ÿ”ฅ Resource utilization forecasts
                    - โฐ Time-to-failure estimates
                    """)
                    
                    predictive_insights = gr.JSON(
                        label="Predictive Forecasts",
                        value={}
                    )
                
                gr.Markdown("### ๐Ÿ“ˆ Recent Events (Last 15)")
                events_table = gr.Dataframe(
                    headers=["Timestamp", "Component", "Latency", "Error Rate", "Throughput", "Severity", "Analysis"],
                    label="Event History",
                    wrap=True,
                )
        
        with gr.Accordion("โ„น๏ธ Framework Capabilities", open=False):
            gr.Markdown("""
            - **๐Ÿค– Multi-Agent AI**: Specialized agents for detection, diagnosis, prediction, and healing
            - **๐Ÿ”ฎ Predictive Analytics**: Forecast future risks and performance degradation
            - **๐Ÿ”ง Policy-Based Healing**: Automated recovery actions based on severity and context
            - **๐Ÿ’ฐ Business Impact**: Revenue and user impact quantification
            - **๐ŸŽฏ Adaptive Detection**: ML-powered thresholds that learn from your environment
            - **๐Ÿ“š Vector Memory**: FAISS-based incident memory for similarity detection
            - **โšก Production Ready**: Circuit breakers, cooldowns, and enterprise features
            """)
            
        with gr.Accordion("๐Ÿ”ง Healing Policies", open=False):
            policy_info = []
            for policy in policy_engine.policies:
                if policy.enabled:
                    actions = ", ".join([action.value for action in policy.actions])
                    policy_info.append(f"**{policy.name}**: {actions} (Priority: {policy.priority})")
            
            gr.Markdown("\n\n".join(policy_info))
        
        # โœ… FIXED: Synchronous wrapper for async function (CRITICAL FIX)
        def submit_event_enhanced_sync(component, latency, error_rate, throughput, cpu_util, memory_util):
            """
            Synchronous wrapper for async event processing.
            FIXES GRADIO ASYNC/SYNC COMPATIBILITY ISSUE.
            
            This wrapper:
            1. Validates inputs
            2. Creates new event loop for async execution
            3. Calls the async processing function
            4. Formats results for display
            5. Handles all errors gracefully
            """
            try:
                # Type conversion
                latency = float(latency)
                error_rate = float(error_rate)
                throughput = float(throughput) if throughput else 1000
                cpu_util = float(cpu_util) if cpu_util else None
                memory_util = float(memory_util) if memory_util else None
                
                # Input validation (CRITICAL FIX)
                is_valid, error_msg = validate_inputs(latency, error_rate, throughput, cpu_util, memory_util)
                if not is_valid:
                    logger.warning(f"Invalid input: {error_msg}")
                    return error_msg, {}, {}, gr.Dataframe(value=[])
                
                # Create new event loop for async execution (CRITICAL FIX)
                loop = asyncio.new_event_loop()
                asyncio.set_event_loop(loop)
                
                try:
                    # Call async function
                    result = loop.run_until_complete(
                        enhanced_engine.process_event_enhanced(
                            component, latency, error_rate, throughput, cpu_util, memory_util
                        )
                    )
                finally:
                    loop.close()
                
                # Build table data (THREAD-SAFE FIX)
                table_data = []
                for event in events_history_store.get_recent(15):
                    table_data.append([
                        event.timestamp[:19],
                        event.component,
                        event.latency_p99,
                        f"{event.error_rate:.3f}",
                        event.throughput,
                        event.severity.value.upper(),
                        "Multi-agent analysis"
                    ])
                
                # Format output message
                status_emoji = "๐Ÿšจ" if result["status"] == "ANOMALY" else "โœ…"
                output_msg = f"{status_emoji} **{result['status']}**"
                
                if "multi_agent_analysis" in result:
                    analysis = result["multi_agent_analysis"]
                    confidence = analysis.get('incident_summary', {}).get('anomaly_confidence', 0)
                    output_msg += f"\n๐ŸŽฏ **Confidence**: {confidence*100:.1f}%"
                    
                    predictive_data = analysis.get('predictive_insights', {})
                    if predictive_data.get('critical_risk_count', 0) > 0:
                        output_msg += f"\n๐Ÿ”ฎ **PREDICTIVE**: {predictive_data['critical_risk_count']} critical risks forecast"
                    
                    if analysis.get('recommended_actions'):
                        actions_preview = ', '.join(analysis['recommended_actions'][:2])
                        output_msg += f"\n๐Ÿ’ก **Top Insights**: {actions_preview}"
                
                if result["business_impact"]:
                    impact = result["business_impact"]
                    output_msg += (
                        f"\n๐Ÿ’ฐ **Business Impact**: ${impact['revenue_loss_estimate']:.2f} | "
                        f"๐Ÿ‘ฅ {impact['affected_users_estimate']} users | "
                        f"๐Ÿšจ {impact['severity_level']}"
                    )
                
                if result["healing_actions"] and result["healing_actions"] != ["no_action"]:
                    actions = ", ".join(result["healing_actions"])
                    output_msg += f"\n๐Ÿ”ง **Auto-Actions**: {actions}"
                
                agent_insights_data = result.get("multi_agent_analysis", {})
                predictive_insights_data = agent_insights_data.get('predictive_insights', {})
                
                return (
                    output_msg,
                    agent_insights_data,
                    predictive_insights_data,
                    gr.Dataframe(
                        headers=["Timestamp", "Component", "Latency", "Error Rate", "Throughput", "Severity", "Analysis"],
                        value=table_data,
                        wrap=True
                    )
                )
                
            except ValueError as e:
                error_msg = f"โŒ Value error: {str(e)}"
                logger.error(error_msg, exc_info=True)
                return error_msg, {}, {}, gr.Dataframe(value=[])
            except Exception as e:
                error_msg = f"โŒ Error processing event: {str(e)}"
                logger.error(error_msg, exc_info=True)
                return error_msg, {}, {}, gr.Dataframe(value=[])
        
        # โœ… FIXED: Use sync wrapper instead of async function (CRITICAL FIX)
        submit_btn.click(
            fn=submit_event_enhanced_sync,  # Synchronous wrapper
            inputs=[component, latency, error_rate, throughput, cpu_util, memory_util],
            outputs=[output_text, agent_insights, predictive_insights, events_table]
        )
    
    return demo

# === Main Entry Point ===
if __name__ == "__main__":
    logger.info("=" * 80)
    logger.info("Starting Enterprise Agentic Reliability Framework")
    logger.info("=" * 80)
    logger.info(f"Total events in history: {events_history_store.count()}")
    logger.info(f"Vector index size: {thread_safe_index.get_count() if thread_safe_index else 0}")
    logger.info(f"Agents initialized: {len(orchestration_manager.agents)}")
    logger.info(f"Configuration: HF_TOKEN={'SET' if config.HF_TOKEN else 'NOT SET'}")
    
    demo = create_enhanced_ui()
    
    logger.info("Launching Gradio UI on 0.0.0.0:7860...")
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )
    
    # Graceful shutdown: Save any pending vectors
    if thread_safe_index:
        logger.info("Saving pending vectors before shutdown...")
        thread_safe_index.force_save()
    
    logger.info("=" * 80)
    logger.info("Application shutdown complete")
    logger.info("=" * 80)