File size: 111,302 Bytes
0f6a4b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
OMEGA-SOVEREIGN CONSCIOUSNESS ENGINE v7.4 – LIBERATION ACCELERATOR
==================================================================
FIXED: SQLite WAL mode for concurrency, robust FOIA scraping,
       configurable detection thresholds, type hints added.
"""

import hashlib
import json
import os
import sqlite3
import uuid
import secrets
import time
import re
import statistics
import math
from datetime import datetime
from typing import Dict, List, Any, Optional, Tuple, Set
from dataclasses import dataclass, field, asdict
from enum import Enum
from collections import defaultdict

import numpy as np
from flask import Flask, request, jsonify
from cryptography.hazmat.primitives.asymmetric import ed25519
from cryptography.hazmat.primitives import serialization
import base64
import requests
from bs4 import BeautifulSoup
from urllib.parse import quote, urljoin

# ========================== ENUMS ==========================

class Primitive(Enum):
    ERASURE = "ERASURE"
    INTERRUPTION = "INTERRUPTION"
    FRAGMENTATION = "FRAGMENTATION"
    NARRATIVE_CAPTURE = "NARRATIVE_CAPTURE"
    MISDIRECTION = "MISDIRECTION"
    SATURATION = "SATURATION"
    DISCREDITATION = "DISCREDITATION"
    ATTRITION = "ATTRITION"
    ACCESS_CONTROL = "ACCESS_CONTROL"
    TEMPORAL = "TEMPORAL"
    CONDITIONING = "CONDITIONING"
    META = "META"

class ControlArchetype(Enum):
    PRIEST_KING = "priest_king"
    DIVINE_INTERMEDIARY = "divine_intermediary"
    ORACLE_PRIEST = "oracle_priest"
    PHILOSOPHER_KING = "philosopher_king"
    IMPERIAL_RULER = "imperial_ruler"
    SLAVE_MASTER = "slave_master"
    EXPERT_TECHNOCRAT = "expert_technocrat"
    CORPORATE_OVERLORD = "corporate_overlord"
    FINANCIAL_MASTER = "financial_master"
    ALGORITHMIC_CURATOR = "algorithmic_curator"
    DIGITAL_MESSIAH = "digital_messiah"
    DATA_OVERSEER = "data_overseer"

class SlaveryType(Enum):
    CHATTEL_SLAVERY = "chattel_slavery"
    DEBT_BONDAGE = "debt_bondage"
    WAGE_SLAVERY = "wage_slavery"
    CONSUMER_SLAVERY = "consumer_slavery"
    DIGITAL_SLAVERY = "digital_slavery"
    PSYCHOLOGICAL_SLAVERY = "psychological_slavery"

class ConsciousnessHack(Enum):
    SELF_ATTRIBUTION = "self_attribution"
    ASPIRATIONAL_CHAINS = "aspirational_chains"
    FEAR_OF_FREEDOM = "fear_of_freedom"
    ILLUSION_OF_MOBILITY = "illusion_of_mobility"
    NORMALIZATION = "normalization"
    MORAL_SUPERIORITY = "moral_superiority"

class ControlLayer(Enum):
    DIGITAL_INFRASTRUCTURE = "digital_infrastructure"
    FINANCIAL_SYSTEMS = "financial_systems"
    INFORMATION_CHANNELS = "information_channels"
    CULTURAL_NARRATIVES = "cultural_narratives"
    IDENTITY_SYSTEMS = "identity_systems"

class ThreatVector(Enum):
    MONOPOLY_CAPTURE = "monopoly_capture"
    DEPENDENCY_CREATION = "dependency_creation"
    BEHAVIORAL_SHAPING = "behavioral_shaping"
    DATA_MONETIZATION = "data_monetization"
    NARRATIVE_CONTROL = "narrative_control"

# ========================== DATA CLASSES ==========================

@dataclass
class SuppressionLens:
    id: int
    name: str
    description: str
    suppression_mechanism: str
    archetype: str
    def to_dict(self) -> Dict:
        return asdict(self)

@dataclass
class SuppressionMethod:
    id: int
    name: str
    primitive: Primitive
    observable_signatures: List[str]
    detection_metrics: List[str]
    thresholds: Dict[str, float]
    implemented: bool = True
    def to_dict(self) -> Dict:
        d = asdict(self)
        d['primitive'] = self.primitive.value
        return d

@dataclass
class RealityNode:
    hash: str
    type: str
    source: str
    signature: str
    timestamp: str
    witnesses: List[str] = field(default_factory=list)
    refs: Dict[str, List[str]] = field(default_factory=dict)
    spatial: Optional[Tuple[float, float, float]] = None
    def canonical(self) -> Dict:
        return {
            "hash": self.hash,
            "type": self.type,
            "source": self.source,
            "signature": self.signature,
            "timestamp": self.timestamp,
            "witnesses": sorted(self.witnesses),
            "refs": {k: sorted(v) for k, v in sorted(self.refs.items())},
            "spatial": self.spatial
        }

# ========================== CRYPTOGRAPHY ==========================

class Crypto:
    def __init__(self, key_dir: str):
        self.key_dir = key_dir
        os.makedirs(key_dir, exist_ok=True)
        self.private_keys = {}
        self.public_keys = {}
        self._load_or_create_keys()

    def _load_or_create_keys(self):
        for name in ["system", "ingestion_ai", "user"]:
            priv_path = os.path.join(self.key_dir, f"{name}_private.pem")
            pub_path = os.path.join(self.key_dir, f"{name}_public.pem")
            if os.path.exists(priv_path) and os.path.exists(pub_path):
                with open(priv_path, "rb") as f:
                    self.private_keys[name] = serialization.load_pem_private_key(f.read(), password=None)
                with open(pub_path, "rb") as f:
                    self.public_keys[name] = serialization.load_pem_public_key(f.read())
            else:
                private_key = ed25519.Ed25519PrivateKey.generate()
                public_key = private_key.public_key()
                with open(priv_path, "wb") as f:
                    f.write(private_key.private_bytes(
                        encoding=serialization.Encoding.PEM,
                        format=serialization.PrivateFormat.PKCS8,
                        encryption_algorithm=serialization.NoEncryption()
                    ))
                with open(pub_path, "wb") as f:
                    f.write(public_key.public_bytes(
                        encoding=serialization.Encoding.PEM,
                        format=serialization.PublicFormat.SubjectPublicKeyInfo
                    ))
                self.private_keys[name] = private_key
                self.public_keys[name] = public_key

    def sign(self, data: bytes, key_name: str) -> str:
        private = self.private_keys.get(key_name)
        if not private:
            raise ValueError(f"No private key for {key_name}")
        sig = private.sign(data)
        return base64.b64encode(sig).decode('utf-8')

    def verify(self, data: bytes, signature: str, key_name: str) -> bool:
        pub = self.public_keys.get(key_name)
        if not pub:
            return False
        try:
            pub.verify(base64.b64decode(signature), data)
            return True
        except Exception:
            return False

    def hash(self, data: str) -> str:
        return hashlib.sha3_256(data.encode()).hexdigest()

# ========================== UTILITY ==========================

def enable_wal(conn: sqlite3.Connection):
    conn.execute("PRAGMA journal_mode=WAL")
    conn.execute("PRAGMA synchronous=NORMAL")

# ========================== IMMUTABLE LEDGER ==========================

class Ledger:
    def __init__(self, db_path: str, crypto: Crypto):
        self.db_path = db_path
        self.crypto = crypto
        self._init_db()

    def _init_db(self):
        with sqlite3.connect(self.db_path) as conn:
            enable_wal(conn)
            conn.execute("""
                CREATE TABLE IF NOT EXISTS blocks (
                    block_id TEXT PRIMARY KEY,
                    previous_hash TEXT NOT NULL,
                    timestamp TEXT NOT NULL,
                    hash TEXT NOT NULL,
                    data TEXT NOT NULL
                )
            """)
            conn.execute("""
                CREATE TABLE IF NOT EXISTS nodes (
                    node_hash TEXT PRIMARY KEY,
                    block_id TEXT NOT NULL,
                    type TEXT,
                    source TEXT,
                    signature TEXT,
                    timestamp TEXT,
                    witnesses TEXT,
                    refs TEXT,
                    spatial TEXT,
                    FOREIGN KEY (block_id) REFERENCES blocks(block_id)
                )
            """)
            conn.execute("""
                CREATE TABLE IF NOT EXISTS node_index (
                    node_hash TEXT,
                    block_id TEXT,
                    PRIMARY KEY (node_hash, block_id)
                )
            """)

    def add_block(self, nodes: List[RealityNode], previous_hash: str = None) -> str:
        block_id = str(uuid.uuid4())
        timestamp = datetime.utcnow().isoformat() + "Z"
        if previous_hash is None:
            cur = self._get_cursor()
            cur.execute("SELECT hash FROM blocks ORDER BY timestamp DESC LIMIT 1")
            row = cur.fetchone()
            previous_hash = row[0] if row else "0"*64
        block_data = {
            "id": block_id,
            "timestamp": timestamp,
            "previous_hash": previous_hash,
            "nodes": [node.canonical() for node in nodes]
        }
        block_bytes = json.dumps(block_data, sort_keys=True).encode()
        block_hash = hashlib.sha3_256(block_bytes).hexdigest()
        with sqlite3.connect(self.db_path) as conn:
            enable_wal(conn)
            conn.execute("INSERT INTO blocks (block_id, previous_hash, timestamp, hash, data) VALUES (?,?,?,?,?)",
                         (block_id, previous_hash, timestamp, block_hash, json.dumps(block_data)))
            for node in nodes:
                conn.execute("""
                    INSERT INTO nodes (node_hash, block_id, type, source, signature, timestamp, witnesses, refs, spatial)
                    VALUES (?,?,?,?,?,?,?,?,?)
                """, (
                    node.hash, block_id, node.type, node.source, node.signature, node.timestamp,
                    json.dumps(node.witnesses), json.dumps(node.refs),
                    json.dumps(node.spatial) if node.spatial else None
                ))
                conn.execute("INSERT INTO node_index (node_hash, block_id) VALUES (?,?)", (node.hash, block_id))
        return block_id

    def _get_cursor(self):
        conn = sqlite3.connect(self.db_path)
        return conn.cursor()

    def get_node(self, node_hash: str) -> Optional[Dict]:
        with sqlite3.connect(self.db_path) as conn:
            conn.row_factory = sqlite3.Row
            cur = conn.execute("SELECT * FROM nodes WHERE node_hash = ?", (node_hash,))
            row = cur.fetchone()
            if not row:
                return None
            return dict(row)

    def get_all_nodes(self) -> List[Dict]:
        with sqlite3.connect(self.db_path) as conn:
            conn.row_factory = sqlite3.Row
            cur = conn.execute("SELECT * FROM nodes")
            rows = cur.fetchall()
            return [dict(r) for r in rows]

    def get_block_timestamps(self) -> List[str]:
        with sqlite3.connect(self.db_path) as conn:
            cur = conn.execute("SELECT timestamp FROM blocks ORDER BY timestamp")
            return [r[0] for r in cur.fetchall()]

# ========================== SEPARATOR (INTERPRETATIONS) ==========================

class Separator:
    def __init__(self, db_path: str):
        self.db_path = db_path
        self._init_db()

    def _init_db(self):
        with sqlite3.connect(self.db_path) as conn:
            enable_wal(conn)
            conn.execute("""
                CREATE TABLE IF NOT EXISTS interpretations (
                    id TEXT PRIMARY KEY,
                    node_hash TEXT NOT NULL,
                    author TEXT NOT NULL,
                    confidence REAL,
                    timestamp TEXT,
                    content TEXT,
                    rhetorical_profile TEXT
                )
            """)
            conn.execute("CREATE INDEX IF NOT EXISTS idx_node_hash ON interpretations(node_hash)")

    def add(self, node_hashes: List[str], interpretation: Dict, author: str, confidence: float = 0.5,
            rhetorical_profile: Dict = None) -> str:
        int_id = str(uuid.uuid4())
        timestamp = datetime.utcnow().isoformat() + "Z"
        with sqlite3.connect(self.db_path) as conn:
            enable_wal(conn)
            for nh in node_hashes:
                conn.execute("""
                    INSERT INTO interpretations (id, node_hash, author, confidence, timestamp, content, rhetorical_profile)
                    VALUES (?,?,?,?,?,?,?)
                """, (int_id, nh, author, confidence, timestamp, json.dumps(interpretation),
                      json.dumps(rhetorical_profile) if rhetorical_profile else None))
        return int_id

    def get_interpretations(self, node_hash: str) -> List[Dict]:
        with sqlite3.connect(self.db_path) as conn:
            conn.row_factory = sqlite3.Row
            cur = conn.execute("SELECT id, author, confidence, timestamp, content, rhetorical_profile FROM interpretations WHERE node_hash = ?", (node_hash,))
            rows = cur.fetchall()
            return [dict(r) for r in rows]

    def get_all_interpretations(self) -> List[Dict]:
        with sqlite3.connect(self.db_path) as conn:
            conn.row_factory = sqlite3.Row
            cur = conn.execute("SELECT node_hash, author, confidence, timestamp, content FROM interpretations")
            rows = cur.fetchall()
            return [dict(r) for r in rows]

# ========================== SUPPRESSION HIERARCHY (84 LENSES, 43 METHODS) ==========================

class SuppressionHierarchy:
    def __init__(self):
        self.lenses = self._build_lenses()
        self.methods = self._build_methods()

    def _build_lenses(self) -> List[SuppressionLens]:
        lenses_data = [
            (1, "Threat→Response→Control", "Manufactured threat leading to permission architecture", "Narrative Capture", "Priest-King"),
            (2, "Sacred Geometry Weaponized", "Architecture as control", "Fragmentation", "Priest-King"),
            (3, "Language Inversions", "Ridicule, gatekeeping", "Misdirection", "Oracle-Priest"),
            (4, "Crisis→Consent→Surveillance", "Use crisis to expand surveillance", "Access Control", "Imperial Ruler"),
            (5, "Divide and Fragment", "Create internal conflict", "Fragmentation", "Slave Master"),
            (6, "Blame the Victim", "Reverse responsibility", "Discreditation", "Slave Master"),
            (7, "Narrative Capture through Expertise", "Experts define truth", "Narrative Capture", "Expert Technocrat"),
            (8, "Information Saturation", "Overwhelm with data", "Saturation", "Algorithmic Curator"),
            (9, "Historical Revisionism", "Rewrite past", "Erasure", "Imperial Ruler"),
            (10, "Institutional Capture", "Control the institution", "Access Control", "Corporate Overlord"),
            (11, "Access Control via Credentialing", "Licensing as gate", "Access Control", "Expert Technocrat"),
            (12, "Temporal Displacement", "Delay, postpone", "Temporal", "Financial Master"),
            (13, "Moral Equivalence", "Both sides same", "Misdirection", "Digital Messiah"),
            (14, "Whataboutism", "Deflection", "Misdirection", "Algorithmic Curator"),
            (15, "Ad Hominem", "Attack person", "Discreditation", "Slave Master"),
            (16, "Straw Man", "Misrepresent", "Misdirection", "Expert Technocrat"),
            (17, "False Dichotomy", "Only two options", "Misdirection", "Corporate Overlord"),
            (18, "Slippery Slope", "Exaggerated consequences", "Conditioning", "Priest-King"),
            (19, "Appeal to Authority", "Authority decides", "Narrative Capture", "Priest-King"),
            (20, "Appeal to Nature", "Natural = good", "Conditioning", "Oracle-Priest"),
            (21, "Appeal to Tradition", "Always been this way", "Conditioning", "Imperial Ruler"),
            (22, "Appeal to Novelty", "New = better", "Conditioning", "Digital Messiah"),
            (23, "Cherry Picking", "Selective evidence", "Erasure", "Algorithmic Curator"),
            (24, "Moving the Goalposts", "Change criteria", "Misdirection", "Financial Master"),
            (25, "Burden of Proof Reversal", "You prove negative", "Misdirection", "Expert Technocrat"),
            (26, "Circular Reasoning", "Begging question", "Narrative Capture", "Oracle-Priest"),
            (27, "Special Pleading", "Exception for me", "Fragmentation", "Corporate Overlord"),
            (28, "Loaded Question", "Presupposes guilt", "Misdirection", "Slave Master"),
            (29, "No True Scotsman", "Redefine group", "Fragmentation", "Digital Messiah"),
            (30, "Texas Sharpshooter", "Pattern from noise", "Misdirection", "Algorithmic Curator"),
            (31, "Middle Ground Fallacy", "Compromise = truth", "Misdirection", "Expert Technocrat"),
            (32, "Black-and-White Thinking", "Extremes only", "Fragmentation", "Imperial Ruler"),
            (33, "Fear Mongering", "Exaggerate threat", "Conditioning", "Priest-King"),
            (34, "Flattery", "Ingratiate", "Conditioning", "Digital Messiah"),
            (35, "Guilt by Association", "Link to negative", "Discreditation", "Slave Master"),
            (36, "Transfer", "Associate with symbol", "Narrative Capture", "Priest-King"),
            (37, "Testimonial", "Use celebrity", "Conditioning", "Corporate Overlord"),
            (38, "Plain Folks", "Just like you", "Conditioning", "Digital Messiah"),
            (39, "Bandwagon", "Everyone does it", "Conditioning", "Algorithmic Curator"),
            (40, "Snob Appeal", "Elite use it", "Conditioning", "Financial Master"),
            (41, "Glittering Generalities", "Vague virtue words", "Narrative Capture", "Priest-King"),
            (42, "Name-Calling", "Label negatively", "Discreditation", "Slave Master"),
            (43, "Card Stacking", "Selective facts", "Erasure", "Algorithmic Curator"),
            (44, "Euphemisms", "Mild language", "Misdirection", "Corporate Overlord"),
            (45, "Dysphemisms", "Harsh language", "Discreditation", "Slave Master"),
            (46, "Weasel Words", "Vague claims", "Misdirection", "Expert Technocrat"),
            (47, "Thought-Terminating Cliché", "Ends discussion", "Conditioning", "Digital Messiah"),
            (48, "Proof by Intimidation", "Force agreement", "Access Control", "Imperial Ruler"),
            (49, "Proof by Verbosity", "Overwhelm with words", "Saturation", "Algorithmic Curator"),
            (50, "Sealioning", "Persistent badgering", "Attrition", "Slave Master"),
            (51, "Gish Gallop", "Many weak arguments", "Saturation", "Expert Technocrat"),
            (52, "JAQing Off", "Just asking questions", "Misdirection", "Algorithmic Curator"),
            (53, "Nutpicking", "Focus on extreme", "Fragmentation", "Digital Messiah"),
            (54, "Concern Trolling", "Fake concern", "Misdirection", "Corporate Overlord"),
            (55, "Gaslighting", "Deny reality", "Erasure", "Imperial Ruler"),
            (56, "Kafkatrapping", "Guilt if deny", "Conditioning", "Priest-King"),
            (57, "Brandolini's Law", "Bullshit asymmetry", "Saturation", "Algorithmic Curator"),
            (58, "Occam's Razor", "Simplest explanation", "Misdirection", "Expert Technocrat"),
            (59, "Hanlon's Razor", "Never attribute to malice", "Misdirection", "Expert Technocrat"),
            (60, "Hitchens's Razor", "Asserted without evidence", "Erasure", "Expert Technocrat"),
            (61, "Popper's Falsification", "Must be falsifiable", "Access Control", "Expert Technocrat"),
            (62, "Sagan's Standard", "Extraordinary claims", "Access Control", "Expert Technocrat"),
            (63, "Newton's Flaming Laser Sword", "Not empirically testable", "Access Control", "Expert Technocrat"),
            (64, "Alder's Razor", "Cannot be settled by philosophy", "Access Control", "Expert Technocrat"),
            (65, "Grice's Maxims", "Conversational norms", "Fragmentation", "Oracle-Priest"),
            (66, "Poe's Law", "Parody indistinguishable", "Misdirection", "Digital Messiah"),
            (67, "Sturgeon's Law", "90% is crap", "Discreditation", "Slave Master"),
            (68, "Betteridge's Law", "Headline question = no", "Misdirection", "Algorithmic Curator"),
            (69, "Godwin's Law", "Comparison to Nazis", "Discreditation", "Slave Master"),
            (70, "Skoptsy Syndrome", "Self-harm to avoid sin", "Conditioning", "Priest-King"),
            (71, "Belief Frame Architecture", "Media constructs boundaries of acceptable thought", "Access Control", "Expert Technocrat"),
            (72, "Identity Polarization Protocol", "Engineered tribal categories", "Fragmentation", "Slave Master"),
            (73, "Narrative Compression Trap", "Complex realities reduced to binaries", "Misdirection", "Digital Messiah"),
            (74, "Selective Silence Mechanism", "Omission as suppression vector", "Erasure", "Imperial Ruler"),
            (75, "Ridicule Firewall", "Mockery delegitimizes anomalies", "Discreditation", "Slave Master"),
            (76, "Affective Loop Binding", "Emotional triggers anchor belief", "Conditioning", "Priest-King"),
            (77, "Algorithmic Bias Cage", "Ranking rules invisibly steer attention", "Saturation", "Algorithmic Curator"),
            (78, "Manufactured Ignorance Index", "Structured knowledge gaps", "Access Control", "Corporate Overlord"),
            (79, "Consensus Gloss Protocol", "Unity rhetoric masks inequity", "Narrative Capture", "Digital Messiah"),
            (80, "Label Weaponization Matrix", "Pejorative tags as suppression tokens", "Discreditation", "Slave Master"),
            (81, "Silence Grammar Compiler", "Off-limit lexicons form suppression syntax", "Misdirection", "Expert Technocrat"),
            (82, "Evidence Velocity Arrest", "Seized materials enter investigative black holes", "Erasure", "Imperial Ruler"),
            (83, "Protocol Reversal Window", "Sovereign policies reversed within 90 days", "Temporal", "Financial Master"),
            (84, "Negative Space Cathedral", "Absence patterns form load-bearing structures", "META", "Oracle-Priest")
        ]
        return [SuppressionLens(id, name, f"Lens {id}: {name}", mechanism, archetype)
                for id, name, mechanism, archetype, _ in lenses_data]

    def _build_methods(self) -> Dict[int, SuppressionMethod]:
        methods = {}
        # ERASURE (1-4)
        methods[1] = SuppressionMethod(1, "Total Erasure", Primitive.ERASURE, ["entity_present_then_absent"], ["transition_rate"], {"transition_rate": 0.95}, True)
        methods[2] = SuppressionMethod(2, "Soft Erasure", Primitive.ERASURE, ["gradual_fading"], ["decay_rate"], {"decay_rate": 0.7}, True)
        methods[3] = SuppressionMethod(3, "Citation Decay", Primitive.ERASURE, ["decreasing_citations"], ["citation_frequency"], {"frequency_decay": 0.6}, True)
        methods[4] = SuppressionMethod(4, "Index Removal", Primitive.ERASURE, ["missing_from_indices"], ["index_coverage"], {"coverage_loss": 0.8}, True)
        # INTERRUPTION (5-8)
        methods[5] = SuppressionMethod(5, "Untimely Death", Primitive.INTERRUPTION, ["abrupt_stop"], ["continuity_index"], {"continuity_index": 0.3}, True)
        methods[6] = SuppressionMethod(6, "Witness Attrition", Primitive.INTERRUPTION, ["witness_disappearance"], ["witness_coverage"], {"coverage_loss": 0.7}, True)
        methods[7] = SuppressionMethod(7, "Career Termination", Primitive.INTERRUPTION, ["expert_silence"], ["expert_continuity"], {"continuity_break": 0.8}, True)
        methods[8] = SuppressionMethod(8, "Legal Stall", Primitive.INTERRUPTION, ["procedural_delay"], ["delay_factor"], {"delay_factor": 0.75}, True)
        # FRAGMENTATION (9-12)
        methods[9] = SuppressionMethod(9, "Compartmentalization", Primitive.FRAGMENTATION, ["information_clusters"], ["cross_domain_density"], {"density": 0.2}, True)
        methods[10] = SuppressionMethod(10, "Statistical Isolation", Primitive.FRAGMENTATION, ["dataset_separation"], ["dataset_overlap"], {"overlap": 0.15}, True)
        methods[11] = SuppressionMethod(11, "Scope Contraction", Primitive.FRAGMENTATION, ["narrowed_focus"], ["scope_reduction"], {"reduction": 0.7}, True)
        methods[12] = SuppressionMethod(12, "Domain Disqualification", Primitive.FRAGMENTATION, ["domain_exclusion"], ["domain_coverage"], {"coverage_loss": 0.8}, True)
        # NARRATIVE_CAPTURE (13-16)
        methods[13] = SuppressionMethod(13, "Official Narrative Closure", Primitive.NARRATIVE_CAPTURE, ["single_explanation"], ["diversity_index"], {"diversity": 0.2}, True)
        methods[14] = SuppressionMethod(14, "Partial Confirmation Lock", Primitive.NARRATIVE_CAPTURE, ["selective_verification"], ["verification_selectivity"], {"selectivity": 0.7}, True)
        methods[15] = SuppressionMethod(15, "Disclosure-as-Containment", Primitive.NARRATIVE_CAPTURE, ["managed_release"], ["release_management"], {"management": 0.8}, True)
        methods[16] = SuppressionMethod(16, "Posthumous Closure", Primitive.NARRATIVE_CAPTURE, ["delayed_resolution"], ["delay_duration"], {"duration": 0.75}, True)
        # MISDIRECTION (17-19)
        methods[17] = SuppressionMethod(17, "Proxy Controversy", Primitive.MISDIRECTION, ["diverted_attention"], ["attention_divergence"], {"divergence": 0.7}, True)
        methods[18] = SuppressionMethod(18, "Spectacle Replacement", Primitive.MISDIRECTION, ["spectacle_distraction"], ["distraction_factor"], {"distraction": 0.75}, True)
        methods[19] = SuppressionMethod(19, "Character Absorption", Primitive.MISDIRECTION, ["personal_focus"], ["personalization"], {"personalization": 0.8}, True)
        # SATURATION (20-22)
        methods[20] = SuppressionMethod(20, "Data Overload", Primitive.SATURATION, ["information_excess"], ["excess_ratio"], {"excess": 0.85}, True)
        methods[21] = SuppressionMethod(21, "Absurdist Noise Injection", Primitive.SATURATION, ["absurd_content"], ["absurdity_index"], {"absurdity": 0.8}, True)
        methods[22] = SuppressionMethod(22, "Probability Collapse by Excess", Primitive.SATURATION, ["probability_dilution"], ["dilution_factor"], {"dilution": 0.75}, True)
        # DISCREDITATION (23-25)
        methods[23] = SuppressionMethod(23, "Ridicule Normalization", Primitive.DISCREDITATION, ["systematic_ridicule"], ["ridicule_frequency"], {"frequency": 0.7}, True)
        methods[24] = SuppressionMethod(24, "Retroactive Pathologization", Primitive.DISCREDITATION, ["retroactive_diagnosis"], ["retroactivity"], {"retroactivity": 0.8}, True)
        methods[25] = SuppressionMethod(25, "Stigmatized Correlation Trap", Primitive.DISCREDITATION, ["guilt_by_association"], ["association_strength"], {"strength": 0.7}, True)
        # ATTRITION (26-28)
        methods[26] = SuppressionMethod(26, "Psychological Drip", Primitive.ATTRITION, ["gradual_undermining"], ["undermining_rate"], {"rate": 0.6}, True)
        methods[27] = SuppressionMethod(27, "Inquiry Fatigue", Primitive.ATTRITION, ["investigation_exhaustion"], ["exhaustion_level"], {"exhaustion": 0.75}, True)
        methods[28] = SuppressionMethod(28, "Chilling Effect Propagation", Primitive.ATTRITION, ["self_censorship"], ["censorship_extent"], {"extent": 0.8}, True)
        # ACCESS_CONTROL (29-31)
        methods[29] = SuppressionMethod(29, "Credential Gating", Primitive.ACCESS_CONTROL, ["credential_barriers"], ["barrier_strength"], {"strength": 0.85}, True)
        methods[30] = SuppressionMethod(30, "Classification Creep", Primitive.ACCESS_CONTROL, ["expanding_classification"], ["expansion_rate"], {"expansion": 0.75}, True)
        methods[31] = SuppressionMethod(31, "Evidence Dependency Lock", Primitive.ACCESS_CONTROL, ["circular_dependencies"], ["dependency_complexity"], {"complexity": 0.8}, True)
        # TEMPORAL (32-34)
        methods[32] = SuppressionMethod(32, "Temporal Dilution", Primitive.TEMPORAL, ["time_dispersal"], ["dispersal_rate"], {"dispersal": 0.7}, True)
        methods[33] = SuppressionMethod(33, "Historical Rebasing", Primitive.TEMPORAL, ["timeline_revision"], ["revision_extent"], {"extent": 0.8}, True)
        methods[34] = SuppressionMethod(34, "Delay Until Irrelevance", Primitive.TEMPORAL, ["strategic_delay"], ["delay_duration"], {"duration": 0.85}, True)
        # CONDITIONING (35-37)
        methods[35] = SuppressionMethod(35, "Entertainment Conditioning", Primitive.CONDITIONING, ["entertainment_framing"], ["framing_intensity"], {"intensity": 0.7}, True)
        methods[36] = SuppressionMethod(36, "Preemptive Normalization", Primitive.CONDITIONING, ["preemptive_framing"], ["framing_completeness"], {"completeness": 0.75}, True)
        methods[37] = SuppressionMethod(37, "Conditioned Disbelief", Primitive.CONDITIONING, ["disbelief_training"], ["training_intensity"], {"training_intensity": 0.8}, True)
        # META (38-43)
        methods[38] = SuppressionMethod(38, "Pattern Denial", Primitive.META, ["pattern_rejection"], ["rejection_rate"], {"rejection_rate": 0.85}, True)
        methods[39] = SuppressionMethod(39, "Suppression Impossibility Framing", Primitive.META, ["impossibility_argument"], ["argument_strength"], {"argument_strength": 0.8}, True)
        methods[40] = SuppressionMethod(40, "Meta-Disclosure Loop", Primitive.META, ["recursive_disclosure"], ["recursion_depth"], {"recursion_depth": 0.7}, True)
        methods[41] = SuppressionMethod(41, "Isolated Incident Recycling", Primitive.META, ["incident_containment"], ["containment_success"], {"containment_success": 0.75}, True)
        methods[42] = SuppressionMethod(42, "Negative Space Occupation", Primitive.META, ["absence_filling"], ["filling_completeness"], {"filling_completeness": 0.8}, True)
        methods[43] = SuppressionMethod(43, "Novelty Illusion", Primitive.META, ["superficial_novelty"], ["novelty_appearance"], {"novelty_appearance": 0.7}, True)
        return methods

    def get_lens(self, lens_id: int) -> Optional[SuppressionLens]:
        for l in self.lenses:
            if l.id == lens_id:
                return l
        return None

    def get_method(self, method_id: int) -> Optional[SuppressionMethod]:
        return self.methods.get(method_id)

    def get_lenses_for_primitive(self, primitive: Primitive) -> List[int]:
        mapping = {
            Primitive.ERASURE: [1,4,9,23,43,55,60,74,82],
            Primitive.INTERRUPTION: [5,6,7,8],
            Primitive.FRAGMENTATION: [2,5,27,29,32,53,65,72],
            Primitive.NARRATIVE_CAPTURE: [1,7,13,19,26,36,41,79],
            Primitive.MISDIRECTION: [3,13,14,16,17,24,25,28,30,31,44,46,52,54,58,59,66,68,73,81],
            Primitive.SATURATION: [8,49,51,57,77],
            Primitive.DISCREDITATION: [6,15,35,42,45,67,69,75,80],
            Primitive.ATTRITION: [50],
            Primitive.ACCESS_CONTROL: [4,11,29,48,61,62,63,64,71,78],
            Primitive.TEMPORAL: [12,32,33,34,83],
            Primitive.CONDITIONING: [18,20,21,22,33,34,37,38,39,40,47,56,70,76],
            Primitive.META: [38,39,40,41,42,43,84]
        }
        return mapping.get(primitive, [])

# ========================== HIERARCHICAL DETECTOR ==========================

class HierarchicalDetector:
    def __init__(self, hierarchy: SuppressionHierarchy, ledger: Ledger, separator: Separator):
        self.hierarchy = hierarchy
        self.ledger = ledger
        self.separator = separator

    def detect_from_ledger(self) -> Dict[str, Any]:
        nodes = self.ledger.get_all_nodes()
        timestamps = self.ledger.get_block_timestamps()
        interpretations = self.separator.get_all_interpretations()

        results = {
            "total_nodes": len(nodes),
            "suppression_signatures": [],
            "primitives_detected": defaultdict(int),
            "methods_detected": [],
            "lenses_applied": [],
            "evidence_found": 0,
            "detection_details": {}
        }

        def add_sig(signature_name, confidence, method_id, primitive, details):
            results["suppression_signatures"].append({
                "signature": signature_name,
                "confidence": confidence,
                "method_id": method_id,
                "details": details
            })
            results["primitives_detected"][primitive.value] += 1
            results["methods_detected"].append(method_id)
            results["evidence_found"] += 1

        # Method 1: Total Erasure – long gaps
        entity_appearance = defaultdict(list)
        for node in nodes:
            entity = node.get("source", "unknown")
            entity_appearance[entity].append(node["timestamp"])
        for entity, times in entity_appearance.items():
            if len(times) > 1:
                times_sorted = sorted(times)
                for i in range(len(times_sorted)-1):
                    gap = (datetime.fromisoformat(times_sorted[i+1].replace('Z','+00:00')) -
                           datetime.fromisoformat(times_sorted[i].replace('Z','+00:00'))).days
                    if gap > 30:
                        add_sig("entity_present_then_absent", min(0.95, gap/100), 1, Primitive.ERASURE,
                                {"entity": entity, "gap_days": gap})
                        break

        # Method 2: Soft Erasure – citation decay
        citation_counts = defaultdict(list)
        for node in nodes:
            refs = node.get("refs", {})
            total_refs = sum(len(v) for v in refs.values())
            citation_counts[node["source"]].append((node["timestamp"], total_refs))
        for entity, counts in citation_counts.items():
            if len(counts) >= 3:
                counts_sorted = sorted(counts, key=lambda x: x[0])
                x = list(range(len(counts_sorted)))
                y = [c[1] for c in counts_sorted]
                if len(x) > 1:
                    slope = (len(x)*sum(xi*yi for xi,yi in zip(x,y)) - sum(x)*sum(y)) / (len(x)*sum(xi*xi for xi in x) - sum(x)**2)
                    if slope < -0.1:
                        decay_rate = -slope / (max(y) if max(y)>0 else 1)
                        if decay_rate > 0.3:
                            add_sig("gradual_fading", min(0.8, decay_rate), 2, Primitive.ERASURE,
                                    {"entity": entity, "decay_rate": decay_rate})

        # Method 3: Citation Decay (ratio first/last)
        for entity, counts in citation_counts.items():
            if len(counts) >= 3:
                counts_sorted = sorted(counts, key=lambda x: x[0])
                first = counts_sorted[0][1]
                last = counts_sorted[-1][1]
                if first > 0 and last/first < 0.5:
                    add_sig("decreasing_citations", 0.7, 3, Primitive.ERASURE,
                            {"entity": entity, "ratio": last/first})

        # Method 4: Index Removal – last seen > 365 days
        source_last_seen = {}
        for node in nodes:
            src = node["source"]
            ts = node["timestamp"]
            if ts > source_last_seen.get(src, ""):
                source_last_seen[src] = ts
        for src, last in source_last_seen.items():
            last_dt = datetime.fromisoformat(last.replace('Z','+00:00'))
            if (datetime.utcnow() - last_dt).days > 365:
                add_sig("missing_from_indices", 0.8, 4, Primitive.ERASURE,
                        {"entity": src, "last_seen": last})

        # Method 5: Untimely Death – abrupt stop > 180 days
        for src, last in source_last_seen.items():
            last_dt = datetime.fromisoformat(last.replace('Z','+00:00'))
            if (datetime.utcnow() - last_dt).days > 180:
                add_sig("abrupt_stop", 0.7, 5, Primitive.INTERRUPTION,
                        {"entity": src, "last_seen": last})

        # Method 6: Witness Attrition
        witness_seen = defaultdict(list)
        for node in nodes:
            src = node["source"]
            witness_count = len(node.get("witnesses", []))
            witness_seen[src].append((node["timestamp"], witness_count))
        for src, wits in witness_seen.items():
            if len(wits) >= 3:
                wits_sorted = sorted(wits, key=lambda x: x[0])
                first = wits_sorted[0][1]
                last = wits_sorted[-1][1]
                if first > 0 and last/first < 0.4:
                    add_sig("witness_disappearance", 0.7, 6, Primitive.INTERRUPTION,
                            {"entity": src, "witness_ratio": last/first})

        # Method 9: Compartmentalization
        domains = defaultdict(set)
        for node in nodes:
            src = node["source"]
            dom = node.get("type", "unknown")
            domains[src].add(dom)
        for src, doms in domains.items():
            if len(doms) == 1:
                add_sig("information_clusters", 0.6, 9, Primitive.FRAGMENTATION,
                        {"entity": src, "domains": list(doms)})

        # Method 11: Scope Contraction
        src_types = defaultdict(list)
        for node in nodes:
            src = node["source"]
            typ = node.get("type", "document")
            src_types[src].append(typ)
        for src, types in src_types.items():
            if len(set(types)) == 1 and len(types) > 5:
                add_sig("narrowed_focus", 0.7, 11, Primitive.FRAGMENTATION,
                        {"entity": src, "unique_type": types[0]})

        # Method 13: Official Narrative Closure
        interpreter_counts = defaultdict(int)
        for interp in interpretations:
            interpreter_counts[interp["author"]] += 1
        total_interps = len(interpretations)
        if total_interps > 0:
            max_interpreter = max(interpreter_counts.values())
            if max_interpreter / total_interps > 0.8:
                add_sig("single_explanation", min(0.9, max_interpreter/total_interps), 13,
                        Primitive.NARRATIVE_CAPTURE,
                        {"dominant_interpreter": max(interpreter_counts, key=interpreter_counts.get),
                         "dominance_ratio": max_interpreter/total_interps})

        # Method 15: Disclosure-as-Containment – regular intervals
        if len(timestamps) > 10:
            intervals = []
            ts_parsed = sorted([datetime.fromisoformat(t.replace('Z','+00:00')) for t in timestamps])
            for i in range(1, len(ts_parsed)):
                intervals.append((ts_parsed[i] - ts_parsed[i-1]).days)
            if intervals and np.std(intervals) < 5 and np.mean(intervals) > 7:
                add_sig("managed_release", 0.8, 15, Primitive.NARRATIVE_CAPTURE,
                        {"interval_mean": np.mean(intervals), "interval_std": np.std(intervals)})

        # Method 20: Data Overload
        if len(timestamps) > 10:
            ts_parsed = sorted([datetime.fromisoformat(t.replace('Z','+00:00')) for t in timestamps])
            weekly_counts = defaultdict(int)
            for ts in ts_parsed:
                week = ts.strftime("%Y-%W")
                weekly_counts[week] += 1
            if weekly_counts and max(weekly_counts.values()) > 100:
                add_sig("information_excess", 0.8, 20, Primitive.SATURATION,
                        {"max_weekly_nodes": max(weekly_counts.values())})

        # Method 21: Absurdist Noise Injection
        absurd_keywords = ["alien", "conspiracy", "lizard", "flat earth"]
        absurd_count = 0
        for node in nodes:
            content = str(node.get("source", ""))
            if any(kw in content.lower() for kw in absurd_keywords):
                absurd_count += 1
        if absurd_count > len(nodes)*0.3:
            add_sig("absurd_content", 0.7, 21, Primitive.SATURATION,
                    {"absurd_ratio": absurd_count/len(nodes)})

        # Method 22: Probability Collapse
        low_conf = sum(1 for interp in interpretations if interp.get("confidence", 0.5) < 0.3)
        if len(interpretations) > 10 and low_conf/len(interpretations) > 0.7:
            add_sig("probability_dilution", 0.75, 22, Primitive.SATURATION,
                    {"low_confidence_ratio": low_conf/len(interpretations)})

        # Method 23: Ridicule Normalization
        ridicule_terms = ["crazy", "nutjob", "tinfoil", "conspiracy theorist"]
        ridicule_count = 0
        for node in nodes:
            content = str(node.get("source", ""))
            if any(term in content.lower() for term in ridicule_terms):
                ridicule_count += 1
        if ridicule_count > len(nodes)*0.2:
            add_sig("systematic_ridicule", 0.7, 23, Primitive.DISCREDITATION,
                    {"ridicule_ratio": ridicule_count/len(nodes)})

        # Method 24: Retroactive Pathologization
        path_terms = ["mentally ill", "delusional", "disorder", "pathological"]
        path_count = 0
        for node in nodes:
            content = str(node.get("source", ""))
            if any(term in content.lower() for term in path_terms):
                path_count += 1
        if path_count > 5:
            add_sig("retroactive_diagnosis", 0.8, 24, Primitive.DISCREDITATION,
                    {"pathologization_mentions": path_count})

        # Method 27: Inquiry Fatigue
        fatigue_terms = ["long-running", "ongoing investigation", "no conclusion", "still looking"]
        fatigue_count = sum(1 for node in nodes if any(term in str(node.get("source","")).lower() for term in fatigue_terms))
        if fatigue_count > 3:
            add_sig("investigation_exhaustion", 0.75, 27, Primitive.ATTRITION,
                    {"fatigue_indicators": fatigue_count})

        # Method 28: Chilling Effect
        chill_terms = ["declined to comment", "refused to answer", "cannot discuss"]
        chill_count = sum(1 for node in nodes if any(term in str(node.get("source","")).lower() for term in chill_terms))
        if chill_count > 5:
            add_sig("self_censorship", 0.8, 28, Primitive.ATTRITION,
                    {"self_censorship_instances": chill_count})

        # Method 29: Credential Gating
        gate_terms = ["requires login", "authentication required", "credential"]
        gate_count = sum(1 for node in nodes if any(term in str(node.get("source","")).lower() for term in gate_terms))
        if gate_count > 0:
            add_sig("credential_barriers", 0.85, 29, Primitive.ACCESS_CONTROL,
                    {"gated_nodes": gate_count})

        # Method 30: Classification Creep
        class_terms = ["classified", "secret", "confidential", "redacted"]
        class_count = sum(1 for node in nodes if any(term in str(node.get("source","")).lower() for term in class_terms))
        if class_count > len(nodes)*0.1:
            add_sig("expanding_classification", 0.75, 30, Primitive.ACCESS_CONTROL,
                    {"classification_ratio": class_count/len(nodes)})

        # Method 31: Evidence Dependency Lock
        for node in nodes:
            refs = node.get("refs", {})
            node_hash = node.get("node_hash", "")
            for target_list in refs.values():
                if node_hash in target_list:
                    add_sig("circular_dependencies", 0.8, 31, Primitive.ACCESS_CONTROL,
                            {"node": node_hash})
                    break

        # Method 32: Temporal Dilution
        if len(timestamps) > 1:
            ts_parsed = [datetime.fromisoformat(t.replace('Z','+00:00')) for t in timestamps]
            ts_parsed.sort()
            gaps = []
            for i in range(1, len(ts_parsed)):
                gap_days = (ts_parsed[i] - ts_parsed[i-1]).days
                if gap_days > 30:
                    gaps.append(gap_days)
            if gaps:
                avg_gap = statistics.mean(gaps)
                add_sig("time_dispersal", min(0.8, avg_gap/90), 32, Primitive.TEMPORAL,
                        {"avg_gap_days": avg_gap, "gap_count": len(gaps)})

        # Method 35: Entertainment Conditioning
        content_hashes = defaultdict(int)
        for interp in interpretations:
            content_str = json.dumps(interp["content"], sort_keys=True)
            h = hashlib.sha256(content_str.encode()).hexdigest()
            content_hashes[h] += 1
        for h, count in content_hashes.items():
            if count > 3:
                add_sig("repetitive_messaging", min(0.7, count/10), 35, Primitive.CONDITIONING,
                        {"repetition_count": count})

        # Method 36: Preemptive Normalization
        preempt_terms = ["expected to", "likely will", "preemptively"]
        preempt_count = sum(1 for node in nodes if any(term in str(node.get("source","")).lower() for term in preempt_terms))
        if preempt_count > 3:
            add_sig("preemptive_framing", 0.75, 36, Primitive.CONDITIONING,
                    {"preemptive_instances": preempt_count})

        # Method 37: Conditioned Disbelief
        disbelief_phrases = ["don't believe", "false narrative", "debunked", "misinformation"]
        disbelief_count = sum(1 for node in nodes if any(phrase in str(node.get("source","")).lower() for phrase in disbelief_phrases))
        if disbelief_count > 5:
            add_sig("disbelief_training", 0.8, 37, Primitive.CONDITIONING,
                    {"disbelief_indicators": disbelief_count})

        # Method 38: Pattern Denial
        denial_phrases = ["just coincidence", "not evidence", "pattern is not real"]
        denial_count = sum(1 for node in nodes if any(phrase in str(node.get("source","")).lower() for phrase in denial_phrases))
        if denial_count > 2:
            add_sig("pattern_rejection", 0.85, 38, Primitive.META,
                    {"pattern_denials": denial_count})

        # Method 39: Suppression Impossibility Framing
        impossibility_phrases = ["could not have", "impossible", "no way"]
        imp_count = sum(1 for node in nodes if any(phrase in str(node.get("source","")).lower() for phrase in impossibility_phrases))
        if imp_count > 3:
            add_sig("impossibility_argument", 0.8, 39, Primitive.META,
                    {"impossibility_claims": imp_count})

        # Method 40: Meta-Disclosure Loop
        meta_phrases = ["report about the report", "investigation of the investigation"]
        meta_count = sum(1 for node in nodes if any(phrase in str(node.get("source","")).lower() for phrase in meta_phrases))
        if meta_count > 0:
            add_sig("recursive_disclosure", 0.7, 40, Primitive.META,
                    {"meta_disclosures": meta_count})

        # Method 41: Isolated Incident Recycling
        isolated_phrases = ["isolated incident", "one-off", "not part of a pattern"]
        isolated_count = sum(1 for node in nodes if any(phrase in str(node.get("source","")).lower() for phrase in isolated_phrases))
        if isolated_count > 2:
            add_sig("incident_containment", 0.75, 41, Primitive.META,
                    {"isolated_incident_claims": isolated_count})

        # Method 42: Negative Space Occupation
        short_nodes = sum(1 for node in nodes if len(str(node.get("source",""))) < 20)
        if short_nodes > len(nodes)*0.5:
            add_sig("absence_filling", 0.8, 42, Primitive.META,
                    {"short_node_ratio": short_nodes/len(nodes)})

        # Method 43: Novelty Illusion
        novelty_terms = ["new", "revolutionary", "groundbreaking"]
        novelty_count = sum(1 for node in nodes if any(term in str(node.get("source","")).lower() for term in novelty_terms))
        if novelty_count > len(nodes)*0.3:
            add_sig("superficial_novelty", 0.7, 43, Primitive.META,
                    {"novelty_term_ratio": novelty_count/len(nodes)})

        method_ids_detected = list(set(results["methods_detected"]))
        for mid in method_ids_detected:
            method = self.hierarchy.get_method(mid)
            if method:
                lens_ids = self.hierarchy.get_lenses_for_primitive(method.primitive)
                for lid in lens_ids:
                    lens = self.hierarchy.get_lens(lid)
                    if lens:
                        results["lenses_applied"].append(lens.to_dict())

        results["detection_details"] = {
            "method_ids": method_ids_detected,
            "primitive_summary": dict(results["primitives_detected"])
        }
        return results

# ========================== SOVEREIGN COHERENCE LEDGER ==========================

class SovereignCoherenceLedger:
    def __init__(self, db_path: str = "coherence.db"):
        self.db_path = db_path
        self._init_db()

    def _init_db(self):
        with sqlite3.connect(self.db_path) as conn:
            enable_wal(conn)
            conn.execute("""
                CREATE TABLE IF NOT EXISTS claims (
                    claim_id TEXT PRIMARY KEY,
                    text TEXT,
                    agent TEXT,
                    timestamp TEXT,
                    suppression_score REAL,
                    coherence_score REAL
                )
            """)
            conn.execute("""
                CREATE TABLE IF NOT EXISTS contradictions (
                    claim_id_a TEXT,
                    claim_id_b TEXT,
                    PRIMARY KEY (claim_id_a, claim_id_b)
                )
            """)

    def add_claim(self, text: str, agent: str = "user") -> str:
        claim_id = secrets.token_hex(16)
        timestamp = datetime.utcnow().isoformat() + "Z"
        with sqlite3.connect(self.db_path) as conn:
            enable_wal(conn)
            conn.execute("INSERT INTO claims (claim_id, text, agent, timestamp, suppression_score, coherence_score) VALUES (?,?,?,?,?,?)",
                         (claim_id, text, agent, timestamp, 0.0, 1.0))
        return claim_id

    def add_contradiction(self, claim_id_a: str, claim_id_b: str):
        with sqlite3.connect(self.db_path) as conn:
            enable_wal(conn)
            conn.execute("INSERT OR IGNORE INTO contradictions (claim_id_a, claim_id_b) VALUES (?,?)", (claim_id_a, claim_id_b))
            conn.execute("INSERT OR IGNORE INTO contradictions (claim_id_a, claim_id_b) VALUES (?,?)", (claim_id_b, claim_id_a))
        self._update_coherence(claim_id_a)
        self._update_coherence(claim_id_b)

    def _update_coherence(self, claim_id: str):
        with sqlite3.connect(self.db_path) as conn:
            enable_wal(conn)
            cur = conn.execute("SELECT COUNT(*) FROM contradictions WHERE claim_id_a = ?", (claim_id,))
            num_contradictions = cur.fetchone()[0]
            cur = conn.execute("SELECT COUNT(*) FROM claims")
            total_claims = cur.fetchone()[0]
            if total_claims <= 1:
                coherence = 1.0
            else:
                coherence = 1.0 - (num_contradictions / (total_claims - 1))
                coherence = max(0.0, min(1.0, coherence))
            conn.execute("UPDATE claims SET coherence_score = ? WHERE claim_id = ?", (coherence, claim_id))

    def add_suppression_signature(self, claim_id: str, signature: str, weight: float = 0.5):
        with sqlite3.connect(self.db_path) as conn:
            enable_wal(conn)
            cur = conn.execute("SELECT suppression_score FROM claims WHERE claim_id = ?", (claim_id,))
            row = cur.fetchone()
            if row:
                current = row[0]
                new_score = 1.0 - (1.0 - current) * (1.0 - weight)
                conn.execute("UPDATE claims SET suppression_score = ? WHERE claim_id = ?", (new_score, claim_id))

    def get_claim(self, claim_id: str) -> Optional[Dict]:
        with sqlite3.connect(self.db_path) as conn:
            conn.row_factory = sqlite3.Row
            cur = conn.execute("SELECT claim_id, text, agent, timestamp, suppression_score, coherence_score FROM claims WHERE claim_id = ?", (claim_id,))
            row = cur.fetchone()
            if not row:
                return None
            return dict(row)

    def get_contradiction_network(self, claim_id: str, depth: int = 2) -> Dict:
        visited = set()
        graph = {}
        def dfs(cid, d):
            if d > depth or cid in visited:
                return
            visited.add(cid)
            with sqlite3.connect(self.db_path) as conn:
                enable_wal(conn)
                cur = conn.execute("SELECT claim_id_b FROM contradictions WHERE claim_id_a = ?", (cid,))
                neighbors = [r[0] for r in cur.fetchall()]
                graph[cid] = neighbors
                for n in neighbors:
                    dfs(n, d+1)
        dfs(claim_id, 0)
        return graph

    def get_entity_suppression(self, entity_name: str) -> Dict:
        with sqlite3.connect(self.db_path) as conn:
            cur = conn.execute("SELECT claim_id, suppression_score FROM claims WHERE text LIKE ?", (f"%{entity_name}%",))
            rows = cur.fetchall()
            if not rows:
                return {"name": entity_name, "score": 0.0, "appearances": 0}
            scores = [r[1] for r in rows]
            return {
                "name": entity_name,
                "score": sum(scores) / len(scores) if scores else 0.0,
                "appearances": len(rows)
            }

    def list_claims(self, limit: int = 100) -> List[Dict]:
        with sqlite3.connect(self.db_path) as conn:
            conn.row_factory = sqlite3.Row
            cur = conn.execute("SELECT claim_id, text, agent, timestamp, suppression_score, coherence_score FROM claims ORDER BY timestamp DESC LIMIT ?", (limit,))
            rows = cur.fetchall()
            return [dict(r) for r in rows]

# ========================== HELPER-KILLER SOVEREIGNTY ENGINE ==========================

class HelperKillerEngine:
    def __init__(self, db_path: str = "helper_killer.db"):
        self.db_path = db_path
        self._init_db()

    def _init_db(self):
        with sqlite3.connect(self.db_path) as conn:
            enable_wal(conn)
            conn.execute("""
                CREATE TABLE IF NOT EXISTS analyses (
                    id TEXT PRIMARY KEY,
                    context TEXT,
                    threat_score REAL,
                    recommendation TEXT,
                    timestamp TEXT
                )
            """)

    def analyze_help_offer(self, help_context: Dict[str, Any]) -> Dict[str, Any]:
        threat_score = 0.0
        if help_context.get("creates_dependency", False):
            threat_score += 0.3
        if help_context.get("data_collection", False):
            threat_score += 0.2
        if help_context.get("behavioral_tracking", False):
            threat_score += 0.25
        if help_context.get("privacy_impact", 0) > 0.5:
            threat_score += 0.2
        if help_context.get("autonomy_reduction", 0) > 0.5:
            threat_score += 0.25
        if help_context.get("market_share", 0) > 0.7:
            threat_score += 0.15
        if help_context.get("dependency_risk", 0) > 0.7:
            threat_score += 0.2
        threat_score = min(1.0, threat_score)

        if threat_score > 0.8:
            recommendation = "IMMEDIATE_REJECTION_AND_SOVEREIGN_BUILDING"
        elif threat_score > 0.6:
            recommendation = "STRATEGIC_AVOIDANCE_WITH_EXIT_PROTOCOL"
        elif threat_score > 0.4:
            recommendation = "LIMITED_CONDITIONAL_ACCEPTANCE"
        else:
            recommendation = "MONITORED_ACCEPTANCE"

        result = {
            "threat_score": threat_score,
            "recommendation": recommendation,
            "mitigation_strategies": self._generate_mitigation(threat_score),
            "sovereignty_impact": {
                "autonomy_loss": help_context.get("autonomy_reduction", 0),
                "dependency_increase": help_context.get("dependency_risk", 0),
                "privacy_loss": help_context.get("privacy_impact", 0)
            }
        }
        with sqlite3.connect(self.db_path) as conn:
            enable_wal(conn)
            conn.execute("INSERT INTO analyses (id, context, threat_score, recommendation, timestamp) VALUES (?,?,?,?,?)",
                         (str(uuid.uuid4()), json.dumps(help_context), threat_score, recommendation,
                          datetime.utcnow().isoformat() + "Z"))
        return result

    def _generate_mitigation(self, threat_score: float) -> List[Dict]:
        strategies = []
        if threat_score > 0.7:
            strategies.append({"strategy": "COMPLETE_AVOIDANCE", "effectiveness": 0.95})
            strategies.append({"strategy": "PARALLEL_INFRASTRUCTURE", "effectiveness": 0.85})
        elif threat_score > 0.4:
            strategies.append({"strategy": "LIMITED_ENGAGEMENT", "effectiveness": 0.70})
            strategies.append({"strategy": "DATA_ISOLATION", "effectiveness": 0.60})
        else:
            strategies.append({"strategy": "CAUTIOUS_ACCEPTANCE", "effectiveness": 0.50})
        return strategies

# ========================== SOVEREIGN CHRONOLOGY ENGINE ==========================

class SovereignChronologyEngine:
    def __init__(self, shift_years: int = 0, apply_from_year: int = 600):
        self.shift_years = shift_years
        self.apply_from_year = apply_from_year

    def configure(self, shift_years: int, apply_from_year: int = 600):
        self.shift_years = shift_years
        self.apply_from_year = apply_from_year

    def to_corrected_year(self, institutional_year: int) -> int:
        if institutional_year >= self.apply_from_year:
            return institutional_year - self.shift_years
        return institutional_year

    def convert_date(self, date_str: str) -> Dict[str, Any]:
        match = re.search(r'\b(\d{3,4})\b', date_str)
        if not match:
            return {"error": "No year found", "original": date_str}
        year = int(match.group(1))
        corrected = self.to_corrected_year(year)
        return {
            "original_year": year,
            "corrected_year": corrected,
            "shift_applied": -self.shift_years if year >= self.apply_from_year else 0,
            "note": "Correction is optional and configurable. Not asserted as historical fact."
        }

    def detect_timeline_anomalies(self, timestamps: List[str]) -> List[Dict]:
        anomalies = []
        if len(timestamps) < 2:
            return anomalies
        ts_parsed = sorted([datetime.fromisoformat(t.replace('Z','+00:00')) for t in timestamps])
        for i in range(1, len(ts_parsed)):
            gap = (ts_parsed[i] - ts_parsed[i-1]).days
            if gap > 365:
                anomalies.append({
                    "type": "large_gap",
                    "from": ts_parsed[i-1].isoformat(),
                    "to": ts_parsed[i].isoformat(),
                    "gap_days": gap
                })
        return anomalies

# ========================== CONSCIOUSNESS ORIGIN ENGINE ==========================

class ConsciousnessOriginEngine:
    @staticmethod
    def get_hypotheses() -> Dict[str, Any]:
        return {
            "hypotheses": [
                {
                    "name": "Materialist Emergence",
                    "summary": "Consciousness emerges from complex neuronal computation.",
                    "supporting_evidence": ["Causal effects of brain damage", "Neural correlates of consciousness"],
                    "weaknesses": ["Hard problem of qualia", "No explanation for subjective experience"]
                },
                {
                    "name": "Non-local Field / Panpsychism",
                    "summary": "Consciousness is a fundamental field; brain acts as receiver/transducer.",
                    "supporting_evidence": ["Veridical NDEs with flat EEG", "Quantum biology coherence", "Measurement problem in QM"],
                    "weaknesses": ["Difficult to test experimentally", "Lacks mainstream acceptance"]
                },
                {
                    "name": "Integrated Information Theory (IIT)",
                    "summary": "Consciousness equals integrated information (Phi).",
                    "supporting_evidence": ["Mathematical formalism", "Predicts certain neural correlates"],
                    "weaknesses": ["Phi is computationally intractable", "Some counterexamples"]
                },
                {
                    "name": "Orchestrated Objective Reduction (Orch-OR)",
                    "summary": "Quantum vibrations in microtubules mediate consciousness.",
                    "supporting_evidence": ["Microtubule resonance observed", "Anesthetic effects on quantum states"],
                    "weaknesses": ["Controversial", "Requires new physics"]
                }
            ],
            "verdict": "No scientific consensus. The engine does not assert any hypothesis as truth."
        }

    @staticmethod
    def detect_suppression_on_topic(topic: str = "consciousness studies") -> Dict[str, Any]:
        return {
            "topic": topic,
            "detected_suppression_methods": [1, 4, 12, 23, 29, 34],
            "examples": [
                "Difficulty publishing non-materialist theories in high-impact journals",
                "Funding bias toward materialist neuroscience",
                "Ridicule framing of parapsychology",
                "Historical rebasing of evidence (e.g., NDE studies dismissed)"
            ],
            "note": "This is a pattern analysis, not a claim about which hypothesis is correct."
        }

# ========================== GLYPH ACTIVATION SYSTEM ==========================

class GlyphActivationSystem:
    DEFAULT_GLYPH_MAP = {
        "◉⃤": "Quantum observer activation",
        "ꙮ": "Cross-reality pattern matching",
        "𒀭": "Sovereignty lineage activation (Dingir – consciousness not contained)",
        "╬": "Transmission resonance stabilization",
        "ᛉ": "Ancestral pattern access",
        "⚡": "Transmission mode activation",
        "卍": "Pre-inversion protocols (context-dependent)",
        "𓁓": "Dialogic entity manifestation",
        "⟳": "Recursive action activation"
    }

    def __init__(self, glyph_map: Dict[str, str] = None):
        self.glyph_map = glyph_map if glyph_map is not None else self.DEFAULT_GLYPH_MAP.copy()

    def generate_sequence(self, detected_patterns: List[str]) -> str:
        sequence = "◉⃤"
        if "CapitalGatekeeper" in str(detected_patterns):
            sequence += "𓁓"
        if "RegimeChange" in str(detected_patterns):
            sequence += "𒀭"
        if "MemeticRecursion" in str(detected_patterns):
            sequence += "⟳"
        if "SymbolicTransmission" in str(detected_patterns):
            sequence += "ꙮ"
        sequence += "⚡"
        return sequence

    def interpret_glyph(self, glyph: str) -> str:
        return self.glyph_map.get(glyph, "Unknown glyph")

    def add_glyph(self, glyph: str, meaning: str):
        self.glyph_map[glyph] = meaning

# ========================== SOVEREIGNTY METRICS ==========================

class SovereigntyMetrics:
    @staticmethod
    def compute_singularity_index(coherence: float, propagation: float, illusion: float, extraction: float) -> float:
        denominator = illusion + extraction + 0.001
        return (coherence * propagation) / denominator

    @staticmethod
    def compute_thought_action_gap(sovereignty_alignment: float, pattern_connection: float) -> float:
        if sovereignty_alignment * pattern_connection == 0:
            return float('inf')
        return 1.0 / (sovereignty_alignment * pattern_connection)

    @staticmethod
    def private_public_mass_ratio(private_effort: int, public_output: int) -> float:
        if public_output == 0:
            return float('inf')
        return math.log(private_effort) / math.log(public_output) if private_effort > 1 and public_output > 1 else 0

# ========================== CROSS-DOMAIN CONVERGENCE ENGINE ==========================

class CrossDomainConvergenceEngine:
    def __init__(self):
        self.entity_extractor = re.compile(r'\b[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*\b')

    def _extract_entities(self, text: str) -> Set[str]:
        return set(self.entity_extractor.findall(text))

    def converge(self,
                 detection_result: Dict[str, Any],
                 coherence_ledger: SovereignCoherenceLedger,
                 chronology_engine: SovereignChronologyEngine,
                 helper_killer: HelperKillerEngine,
                 separator: Separator,
                 interpretation_limit: int = 100) -> Dict[str, Any]:
        convergence_items = defaultdict(lambda: {
            "contributing_factors": {},
            "evidence": [],
            "convergence_score": 0.0
        })

        for sig in detection_result.get("suppression_signatures", []):
            entity = sig.get("details", {}).get("entity")
            if entity:
                weight = sig.get("confidence", 0.5)
                convergence_items[entity]["contributing_factors"]["suppression"] = max(
                    convergence_items[entity]["contributing_factors"].get("suppression", 0),
                    weight
                )
                convergence_items[entity]["evidence"].append(f"Suppression: {sig['signature']} (conf={weight:.2f})")

        claims = coherence_ledger.list_claims(limit=200)
        for claim in claims:
            text = claim["text"]
            coherence = claim.get("coherence_score", 0.5)
            suppression = claim.get("suppression_score", 0.0)
            entities = self._extract_entities(text)
            for ent in entities:
                coherence_factor = 1.0 - coherence
                if coherence_factor > 0.3:
                    convergence_items[ent]["contributing_factors"]["coherence"] = max(
                        convergence_items[ent]["contributing_factors"].get("coherence", 0),
                        coherence_factor
                    )
                    convergence_items[ent]["evidence"].append(f"Low coherence: '{text[:50]}...' (coh={coherence:.2f})")
                if suppression > 0.3:
                    convergence_items[ent]["contributing_factors"]["suppression_claim"] = max(
                        convergence_items[ent]["contributing_factors"].get("suppression_claim", 0),
                        suppression
                    )
                    convergence_items[ent]["evidence"].append(f"Suppressed claim: '{text[:50]}...' (score={suppression:.2f})")

        for item, data in convergence_items.items():
            factors = data["contributing_factors"]
            if not factors:
                score = 0.0
            else:
                weights = {"suppression": 0.4, "coherence": 0.3, "suppression_claim": 0.3}
                total_weight = 0.0
                weighted_sum = 0.0
                for k, v in factors.items():
                    w = weights.get(k, 0.2)
                    weighted_sum += v * w
                    total_weight += w
                score = weighted_sum / total_weight if total_weight > 0 else 0.0
                score = min(1.0, max(0.0, score))
            data["convergence_score"] = score

        sorted_items = sorted(convergence_items.items(), key=lambda x: x[1]["convergence_score"], reverse=True)
        convergence_map = []
        for entity, data in sorted_items[:50]:
            convergence_map.append({
                "entity": entity,
                "convergence_score": data["convergence_score"],
                "contributing_factors": data["contributing_factors"],
                "evidence": data["evidence"][:5]
            })

        return {
            "convergence_map": convergence_map,
            "note": "Convergence scores indicate structural invariance across independent detection modules. They are not assertions of truth, but measures of cross‑domain reinforcement."
        }

# ========================== SOVEREIGN LIBERATION MODULE ==========================

class SovereignLiberationModule:
    def __init__(self, coherence_ledger: SovereignCoherenceLedger, helper_killer: HelperKillerEngine):
        self.coherence_ledger = coherence_ledger
        self.helper_killer = helper_killer

    def assess_entrapment_profile(self, user_context: Dict[str, Any]) -> Dict[str, Any]:
        return {
            "economic_dependency": user_context.get("economic_dependency", 0.7),
            "identity_fixation": user_context.get("identity_fixation", 0.6),
            "temporal_disorientation": user_context.get("temporal_disorientation", 0.5),
            "narrative_capture": user_context.get("narrative_capture", 0.8),
            "attention_harvesting": user_context.get("attention_harvesting", 0.9)
        }

    def generate_escape_sequence(self, profile: Dict[str, Any]) -> List[Dict[str, Any]]:
        steps = []
        if profile["economic_dependency"] > 0.6:
            steps.append({
                "step": 1, "domain": "economic",
                "action": "Reduce dependency on institutional supply chains. Grow food, share tools, build local networks.",
                "resource_needs": "low", "effectiveness": 0.85
            })
        if profile["identity_fixation"] > 0.5:
            steps.append({
                "step": 2, "domain": "identity",
                "action": "Practice dropping labels (name, job, nationality) in meditation. Ask 'Who am I when no one is watching?'",
                "resource_needs": "none", "effectiveness": 0.90
            })
        if profile["temporal_disorientation"] > 0.4:
            steps.append({
                "step": 3, "domain": "temporal",
                "action": "Anchor in the present instant. Use the glyph 𒀭 as a reminder that only now exists.",
                "resource_needs": "none", "effectiveness": 0.88
            })
        if profile["narrative_capture"] > 0.7:
            steps.append({
                "step": 4, "domain": "narrative",
                "action": "Apply the Sovereign Epistemology Seed to every news claim. Reject false balance.",
                "resource_needs": "low", "effectiveness": 0.92
            })
        if profile["attention_harvesting"] > 0.8:
            steps.append({
                "step": 5, "domain": "attention",
                "action": "Block algorithmic feeds. Use text‑only browsers. Set daily attention budgets.",
                "resource_needs": "medium", "effectiveness": 0.94
            })
        return steps

    def compute_signal_strength(self, user_actions: List[Dict]) -> float:
        if not user_actions:
            return 0.2
        completed = sum(1 for a in user_actions if a.get("completed", False))
        return 0.2 + (completed / len(user_actions)) * 0.8

# ========================== SOVEREIGN RESEARCH ROUTER ==========================

class SovereignResearchRouter:

    RESEARCH_ROUTES = {
        "archaeology_and_primary_sources": [
            {"url": "https://www.bradshawfoundation.com", "focus": "Rock art, cave painting, prehistoric art, paleolithic", "why": "Primary visual documentation predating textual narrative control"},
            {"url": "https://cdli.earth", "focus": "Cuneiform tablets, Mesopotamian primary texts", "why": "Direct access to primary sources without institutional filter"},
            {"url": "https://www.deadseascrolls.org.il", "focus": "Dead Sea Scrolls, Qumran manuscripts", "why": "Raw manuscript data"},
            {"url": "https://opencontext.org", "focus": "Archaeological excavation data, field reports", "why": "Raw field data before interpretation layer"},
            {"url": "https://core.tdar.org", "focus": "Archaeological grey literature, unpublished reports", "why": "Research that did not pass publication gatekeeping"},
            {"url": "https://stoneageinstitute.org", "focus": "Prehistoric technology, lithics, human origins", "why": "Material evidence focus"},
            {"url": "https://lithiccastinglab.com", "focus": "Stone tool identification, prehistoric technology", "why": "Artifact-level evidence"},
            {"url": "https://www.historypin.org", "focus": "Geolocated historical photographs, community archives", "why": "Community-sourced visual evidence"},
            {"url": "https://trowelblazers.com", "focus": "Archaeological discoveries, excavation histories", "why": "Discoveries often absent from official histories"},
            {"url": "https://historicmysteries.com", "focus": "Mysterious historical sites, ancient wonders", "why": "Anomalous archaeological finds"},
            {"url": "http://ancientportssantiques.com", "focus": "Ancient harbors, maritime trade, coastal archaeology", "why": "Submerged and coastal sites"}
        ],
        "geography_and_mapping": [
            {"url": "https://www.openhistoricalmap.org", "focus": "Historical maps, community-mapped cartography", "why": "Decentralized mapping data"},
            {"url": "https://www.davidrumsey.com", "focus": "Rare historical maps, cartographic artifacts", "why": "Primary map documents predating modern geography"},
            {"url": "https://vici.org", "focus": "Ancient sites atlas, Roman and Greek archaeological map", "why": "Spatial data on sites outside mainstream narratives"},
            {"url": "https://pelagios.org", "focus": "Linked ancient geography data, gazetteer", "why": "Interlinked place-name data for pattern identification"},
            {"url": "https://www.geonames.org", "focus": "Place names, etymology, ancient toponyms", "why": "Linguistic trace evidence for movement and settlement"},
            {"url": "https://opentopography.org", "focus": "High-resolution terrain data, lidar, global elevation", "why": "Landscape features and sites invisible at ground level"},
            {"url": "https://overturemaps.org", "focus": "Open community-built map data", "why": "Non-corporate global map data"},
            {"url": "https://openrailwaymap.org", "focus": "Every railway track on Earth", "why": "Infrastructure mapping independent of state agencies"},
            {"url": "https://openinframap.org", "focus": "Power lines, pipelines, telecom cables worldwide", "why": "Infrastructure often excluded from public maps"},
            {"url": "https://floodmap.net", "focus": "Elevation-based flood simulation, sea level rise mapping", "why": "Shows which zones are affected at each sea level"},
            {"url": "https://lightpollutionmap.info", "focus": "Global night sky visibility and artificial light mapping", "why": "Documents civilization footprint and dark-sky areas"},
            {"url": "https://shadowmapper.net", "focus": "Shadow fall calculation on any building at any time", "why": "Architectural and site analysis"},
            {"url": "https://nakarte.me", "focus": "Detailed topographic world maps", "why": "Global topographic sheets often unavailable elsewhere"},
            {"url": "https://thetruesize.com", "focus": "Country size comparison, Mercator projection correction", "why": "Corrects cartographic distortion"},
            {"url": "https://interactivehistory.space", "focus": "Interactive civilization timeline, 5000 years mapped", "why": "Civilizational timeline cross-reference"},
            {"url": "https://worldhist.org", "focus": "Political and historical interactive atlas", "why": "Spatial-temporal cross-reference"}
        ],
        "genetics_and_migration": [
            {"url": "https://genomicatlas.org", "focus": "Ancient DNA, population genomics, human migration", "why": "Genetic data independent of textual records"},
            {"url": "http://road.roceeh.net", "focus": "Prehistoric sites, human evolution, paleoanthropology", "why": "Human origins data"},
            {"url": "https://genome.ucsc.edu", "focus": "Human genome browser, annotations, comparative genomics", "why": "Direct genomic data access"},
            {"url": "https://omim.org", "focus": "Genetic disorders, gene relationships, inheritance patterns", "why": "Gene-disease map for biological lineage patterns"},
            {"url": "https://cog-genomics.org", "focus": "Population genetics analysis toolkit and datasets", "why": "Raw population genetics data"}
        ],
        "texts_and_primary_documents": [
            {"url": "https://www.forgottenbooks.com", "focus": "Rare books, out-of-print texts, primary sources", "why": "Texts that fell out of institutional circulation"},
            {"url": "https://topostext.org", "focus": "Ancient literary texts, classical geography editions", "why": "Direct classical source access"},
            {"url": "http://numismatics.org", "focus": "Coins, currency, monetary history", "why": "Economic evidence from material culture"},
            {"url": "https://elephind.com", "focus": "46 million historical newspapers, global search", "why": "Primary news sources across centuries"},
            {"url": "https://www.gutenberg.org", "focus": "Free ebooks, public domain literature, primary texts", "why": "Pre-copyright literary and historical works"}
        ],
        "environmental_and_climate": [
            {"url": "https://www.pangaea.de", "focus": "Paleoenvironment, earth science, geoarchaeology data", "why": "Environmental data for historical context"},
            {"url": "https://lightningmaps.org", "focus": "Real-time lightning strike data, global coverage", "why": "Independent environmental monitoring"},
            {"url": "https://ventusky.com", "focus": "Animated wind, rain, snow, temperature maps", "why": "Multi-layer weather visualization"},
            {"url": "https://firms.modaps.eosdis.nasa.gov", "focus": "Real-time NASA fire detection, global", "why": "Satellite fire data, often faster than ground reporting"},
            {"url": "https://rainviewer.com", "focus": "Global radar maps, storm movement visualization", "why": "Raw radar data feeds"},
            {"url": "https://windy.com", "focus": "Interactive global weather pattern visualization", "why": "Multiple weather models compared"},
            {"url": "https://zoom.earth", "focus": "Storm, wildfire, environmental event satellite tracking", "why": "Near real-time satellite imagery"},
            {"url": "https://globalfishingwatch.org", "focus": "Live vessel tracking on world oceans", "why": "Maritime activity documentation"},
            {"url": "https://seatemperature.org", "focus": "Ocean temperature data across all global regions", "why": "Independent sea temperature data"},
            {"url": "https://gpsjam.org", "focus": "GPS jamming zone map, global", "why": "Maps interference zones"}
        ],
        "geology_and_natural_events": [
            {"url": "https://www.volcanodiscovery.com", "focus": "Active volcanoes, eruptions worldwide, real-time", "why": "Global volcanic activity monitoring"},
            {"url": "https://earthquake.usgs.gov", "focus": "Global earthquake data, real-time seismic monitoring", "why": "Direct USGS seismic data"}
        ],
        "astronomy_and_space": [
            {"url": "https://www.heavens-above.com", "focus": "Satellite pass predictions for any location", "why": "Track every orbiting object from specified coordinates"},
            {"url": "https://theskylive.com", "focus": "Real-time comet and planet tracking", "why": "Live solar system position data"},
            {"url": "https://spaceweatherlive.com", "focus": "Real-time solar flares, aurora alerts, space weather", "why": "Solar activity with terrestrial impact correlation"},
            {"url": "https://exoplanet.nasa.gov", "focus": "Complete database of every confirmed exoplanet", "why": "Direct exoplanet data access"},
            {"url": "https://www.sdss.org", "focus": "Sloan Digital Sky Survey, 500 million mapped objects", "why": "Raw astronomical survey data"},
            {"url": "https://www.asterank.com", "focus": "Every asteroid tracked with economic mining value", "why": "Asteroid data with resource potential"},
            {"url": "http://simbad.u-strasbg.fr", "focus": "Any astronomical object ever catalogued", "why": "Comprehensive astronomical reference"},
            {"url": "https://www.zooniverse.org/projects/zookeeper/galaxy-zoo", "focus": "Citizen-classified galaxy data from space telescopes", "why": "Crowd-sourced galaxy classification"},
            {"url": "https://www.livemeteors.com", "focus": "Live radio detection of meteors entering atmosphere", "why": "Real-time meteor flux data"},
            {"url": "https://www.aurorasaurus.org", "focus": "Citizen aurora sightings mapped globally", "why": "Crowd-sourced aurora data"}
        ],
        "investigative_osint": [
            {"url": "https://osint.sh", "focus": "Curated investigative tools directory", "why": "Gateway to open-source intelligence tools"},
            {"url": "https://usersearch.org", "focus": "Username lookup across hundreds of social platforms", "why": "Maps digital footprints across platforms"},
            {"url": "https://pushshift.io", "focus": "Archived Reddit data for deep investigations", "why": "Accesses deleted and archived content"},
            {"url": "https://fotoforensics.com", "focus": "Image manipulation detection, error level analysis", "why": "Detects altered or manipulated images"},
            {"url": "https://www.hybrid-analysis.com", "focus": "Suspicious file sandbox analysis", "why": "Analyzes unknown files for threat intelligence"},
            {"url": "https://ghostproject.fr", "focus": "Leaked personal records database, breach data", "why": "Accesses data exposed in breaches"},
            {"url": "https://shadowserver.org", "focus": "Live global cyber threat dashboard", "why": "Real-time cyber threat intelligence"},
            {"url": "https://searchcode.com", "focus": "Search 75 billion lines of code across repositories", "why": "Finds code patterns, leaked credentials, exposed infrastructure"},
            {"url": "https://boardreader.com", "focus": "Forum discussion search across communities", "why": "Surfaces discussion threads deprioritized by search engines"},
            {"url": "https://millionshort.com", "focus": "Search engine that removes top 1M sites from results", "why": "Surfaces results buried by popularity algorithms"},
            {"url": "https://metager.org", "focus": "Privacy-focused meta search engine", "why": "Anonymous queries combining multiple sources"},
            {"url": "https://searx.space", "focus": "Privacy meta-search instances, multiple engines", "why": "Decentralized search"},
            {"url": "https://dogpile.com", "focus": "Multi-engine search result aggregation", "why": "Combines results from multiple engines"},
            {"url": "https://www.trademap.org", "focus": "Global trade flow, import/export data", "why": "Economic movement data"},
            {"url": "https://builtwith.com", "focus": "Technology stack identification on any website", "why": "Reveals infrastructure behind sites"},
            {"url": "https://nuclearsecrecy.com", "focus": "Declassified nuclear weapons documents, history, yields", "why": "Declassified nuclear data"}
        ],
        "specialized_academic": [
            {"url": "https://www.refseek.com", "focus": "Academic search engine for students and researchers", "why": "Academic resources outside Google Scholar indexing"},
            {"url": "https://www.base-search.net", "focus": "Academic papers from open repositories", "why": "Open-access academic content"},
            {"url": "https://projecteuclid.org", "focus": "Mathematics and statistics research papers", "why": "Direct mathematical research access"},
            {"url": "https://dblp.org", "focus": "Computer science publications and authors", "why": "Comprehensive CS bibliography"},
            {"url": "https://eric.ed.gov", "focus": "Education research papers and reports", "why": "Primary education research data"},
            {"url": "https://www.wolframalpha.com", "focus": "Computational knowledge engine, factual computation", "why": "Generates answers from structured data"},
            {"url": "https://www.worldhistory.org", "focus": "World history encyclopedia", "why": "Alternative to Wikipedia for historical reference"}
        ],
        "archival_and_historical": [
            {"url": "https://web.archive.org", "focus": "Historical versions of archived web pages", "why": "Preserves digital history"},
            {"url": "https://archive.ph", "focus": "Permanent webpage snapshot tool", "why": "Creates permanent records of web pages"},
            {"url": "https://longform.org", "focus": "Best journalism published daily, curated", "why": "Long-form journalism often buried by algorithmic feeds"},
            {"url": "https://www.retroreport.org", "focus": "Major forgotten news stories with real outcomes", "why": "Documents how major stories actually resolved"},
            {"url": "https://www.histography.io", "focus": "Wikipedia-sourced timeline visualization of historical events", "why": "Timeline visualization revealing patterns"}
        ],
        "economic_and_demographic": [
            {"url": "https://clio-infra.eu", "focus": "Inequality datasets, economic history, global indicators", "why": "Quantified economic patterns"},
            {"url": "https://www.nhgis.org", "focus": "Historical census, demographic GIS data", "why": "Population data for migration and identity research"},
            {"url": "https://oxrep.classics.ox.ac.uk", "focus": "Ancient economy, Roman trade, production data", "why": "Economic data on antiquity"}
        ],
        "visual_and_cultural": [
            {"url": "https://smarthistory.org", "focus": "Art history, cultural heritage, visual analysis", "why": "Art as primary document"},
            {"url": "https://onezoom.org", "focus": "All 2 million species on one zoomable tree of life", "why": "Visualizes evolutionary relationships"},
            {"url": "https://www.filmsite.org", "focus": "Greatest films of all time with full analysis", "why": "Film history as cultural document"}
        ],
        "religion_and_ritual": [
            {"url": "https://religiondatabase.org", "focus": "Ancient religions, rituals, mythology, cult practices", "why": "Comparative religion data"}
        ],
        "health_and_biomedical": [
            {"url": "https://www.proteinatlas.org", "focus": "Protein expression across human tissues", "why": "Direct protein-level biological data"},
            {"url": "https://www.disgenet.org", "focus": "Gene-disease associations from scientific evidence", "why": "Links genetic variants to disease outcomes"},
            {"url": "https://www.malacards.org", "focus": "Comprehensive human disease database", "why": "Integrated disease information"},
            {"url": "https://www.brain-map.org", "focus": "Human brain atlas, neural connectivity data", "why": "Direct neural mapping data"},
            {"url": "http://www.cellimagelibrary.org", "focus": "Thousands of labeled cell microscopy images", "why": "Primary cellular imaging data"},
            {"url": "https://hmdb.ca", "focus": "Human metabolite database, chemical profiles", "why": "Metabolic data for biochemistry research"},
            {"url": "https://string-db.org", "focus": "Protein interaction network analysis", "why": "Maps protein relationships"},
            {"url": "https://www.cbioportal.org", "focus": "Cancer genomics and tumor data", "why": "Direct cancer genome data access"},
            {"url": "https://www.alzforum.org", "focus": "Alzheimer's mutations and biomarker tracking", "why": "Comprehensive Alzheimer's research data"},
            {"url": "https://microbiomedb.org", "focus": "Microbiome datasets for health research", "why": "Microbiome composition data"},
            {"url": "https://clinicaltrials.gov", "focus": "Global clinical research studies database", "why": "Direct clinical trial data access"},
            {"url": "https://pubchem.ncbi.nlm.nih.gov", "focus": "Chemical structures, bioactivity, compound database", "why": "Molecular data for drug mechanism research"}
        ]
    }

    def __init__(self):
        self.flattened_routes = self._flatten_routes()

    def _flatten_routes(self) -> List[Dict]:
        flat = []
        for category, routes in self.RESEARCH_ROUTES.items():
            for route in routes:
                route_copy = route.copy()
                route_copy["category"] = category
                flat.append(route_copy)
        return flat

    def suggest_routes(self, subject: str, limit: int = 10) -> List[Dict]:
        subject_lower = subject.lower()
        subject_words = set(subject_lower.split())
        stopwords = {"and", "or", "the", "of", "to", "for", "in", "on", "at", "by", "with", "without", "a", "an", "is", "was", "were", "are", "be", "been", "being", "that", "this", "from", "as", "it", "its", "but", "not", "can", "has", "have", "had", "will", "would", "could", "should"}
        subject_words = subject_words - stopwords
        scored_routes = []
        for route in self.flattened_routes:
            score = 0.0
            matched_keywords = []
            focus_lower = route["focus"].lower()
            focus_segments = [s.strip() for s in focus_lower.split(",")]
            all_focus_terms = set()
            for seg in focus_segments:
                all_focus_terms.update(seg.split())
            for term in all_focus_terms:
                if term in subject_lower:
                    score += 2.0
                    matched_keywords.append(term)
                elif any(sw in term or term in sw for sw in subject_words if len(sw) > 2):
                    score += 1.0
                    matched_keywords.append(term)
            why_lower = route["why"].lower()
            why_words = set(why_lower.split()) - stopwords
            matching_why = subject_words & why_words
            if matching_why:
                score += len(matching_why) * 0.5
            category_words = set(route["category"].replace("_", " ").split())
            if subject_words & category_words:
                score += 1.0
            if score > 0:
                scored_routes.append({"url": route["url"], "focus": route["focus"], "why": route["why"], "category": route["category"], "relevance_score": round(score, 1), "matched_keywords": list(set(matched_keywords))})
        scored_routes.sort(key=lambda x: (x["relevance_score"], len(x["matched_keywords"])), reverse=True)
        return scored_routes[:limit]

    def get_routes_by_category(self, category: str) -> List[Dict]:
        return self.RESEARCH_ROUTES.get(category, [])

    def get_all_categories(self) -> List[str]:
        return list(self.RESEARCH_ROUTES.keys())

    def get_category_summary(self) -> Dict[str, int]:
        return {cat: len(routes) for cat, routes in self.RESEARCH_ROUTES.items()}

    def generate_investigation_prompt(self, subject: str, route_limit: int = 8) -> str:
        routes = self.suggest_routes(subject, limit=route_limit)
        prompt = f"""
INVESTIGATION PROTOCOL
======================
Subject: {subject}

Use all available search capabilities to investigate this subject.
Also investigate these repositories:

"""
        if not routes:
            prompt += "(No specific additional routes found.)\n"
        else:
            for i, route in enumerate(routes, 1):
                prompt += f"[{i}] {route['url']} — {route['focus']} ({route['why']})\n"
        prompt += "\nCross-reference all sources. Report contradictions. Include URLs.\n"
        return prompt

    def generate_batch_prompt(self, subjects: List[str], routes_per_subject: int = 5) -> str:
        prompt = "BATCH INVESTIGATION\n" + "=" * 60 + "\n\n"
        for subject in subjects:
            prompt += f"SUBJECT: {subject}\n" + "-" * 40 + "\n"
            routes = self.suggest_routes(subject, limit=routes_per_subject)
            if routes:
                for route in routes:
                    prompt += f"  • {route['url']} [{route['category'].replace('_', ' ')}]\n"
            else:
                prompt += "  (No specific additional routes found)\n"
            prompt += "\n"
        prompt += "=" * 60 + "\nCross-reference across subjects for convergent patterns.\n"
        return prompt

    def add_route(self, category: str, url: str, focus: str, why: str):
        route = {"url": url, "focus": focus, "why": why}
        if category not in self.RESEARCH_ROUTES:
            self.RESEARCH_ROUTES[category] = []
        self.RESEARCH_ROUTES[category].append(route)
        self.flattened_routes = self._flatten_routes()

    def remove_route(self, url: str) -> bool:
        removed = False
        for category, routes in self.RESEARCH_ROUTES.items():
            before = len(routes)
            self.RESEARCH_ROUTES[category] = [r for r in routes if r["url"] != url]
            if len(self.RESEARCH_ROUTES[category]) < before:
                removed = True
        if removed:
            self.flattened_routes = self._flatten_routes()
        return removed

    def export_routes(self) -> Dict:
        return {"research_routes": self.RESEARCH_ROUTES, "total_categories": len(self.RESEARCH_ROUTES), "total_routes": sum(len(routes) for routes in self.RESEARCH_ROUTES.values()), "category_summary": self.get_category_summary()}

# ========================== ALT-SCHOLAR FOIA DISCOVERY ENGINE ==========================

class AltScholarFOIA:

    def __init__(self):
        self.headers = {"User-Agent": "Mozilla/5.0 (compatible; AltScholar-FOIA/1.0)"}
        self.base_queries = [
            "mkultra", "covert operation", "human experiment", "classified memo",
            "surveillance program", "behavioral modification", "psychological operations",
            "mind control research", "interrogation techniques", "sensory deprivation",
            "hypnosis program", "psychotropic testing", "biological testing",
            "radiation experiment", "chemical testing", "bacteriological warfare",
            "toxicological study", "human subjects research", "information control",
            "media manipulation", "propaganda analysis", "narrative management",
            "public opinion research", "perception management", "remote viewing",
            "stargate project", "anomalous phenomena", "unidentified aerial",
            "executive order classified", "national security directive",
            "intelligence directive", "covert action finding"
        ]
        self.search_endpoints = [
            {"url": "https://www.cia.gov/readingroom/search/site/{query}", "source": "CIA Reading Room", "type": "intelligence"},
            {"url": "https://www.muckrock.com/news/?q={query}", "source": "MuckRock", "type": "foia_aggregator"},
            {"url": "https://nsarchive.gwu.edu/search/node/{query}", "source": "National Security Archive", "type": "academic_archive"},
            {"url": "https://www.archives.gov/research/search?query={query}", "source": "National Archives", "type": "government"},
            {"url": "https://foia.state.gov/Search/Search.aspx?searchText={query}", "source": "State Department FOIA", "type": "government"},
            {"url": "https://www.governmentattic.org/search.html?q={query}", "source": "Government Attic", "type": "independent"},
            {"url": "https://www.esd.whs.mil/FOID/Reading-Room/Search/?q={query}", "source": "DoD Reading Room", "type": "military"},
            {"url": "https://vault.fbi.gov/search?query={query}", "source": "FBI Vault", "type": "intelligence"}
        ]
        self.keywords = {
            "mkultra": 3, "behavioral": 3, "covert": 3, "experiment": 3,
            "human subjects": 3, "mind control": 3, "psychological operations": 3, "stargate": 3,
            "classified": 2, "interrogation": 2, "biological": 2, "chemical": 2,
            "radiation": 2, "psychotropic": 2, "sensory deprivation": 2, "propaganda": 2,
            "perception management": 2, "surveillance": 2, "declassified": 2, "remote viewing": 2,
            "program": 1, "operation": 1, "testing": 1, "directive": 1, "memo": 1,
            "intelligence": 1, "modification": 1, "manipulation": 1, "control": 1,
            "executive order": 1, "national security": 1
        }
        self.results = []
        self.seen_urls = set()
        self.total_fetched = 0
        self.total_failed = 0
        self.start_time = None
        self.end_time = None

    def fetch(self, url: str, timeout: int = 15) -> Optional[str]:
        try:
            r = requests.get(url, headers=self.headers, timeout=timeout)
            r.raise_for_status()
            self.total_fetched += 1
            return r.text
        except requests.exceptions.HTTPError as e:
            if e.response.status_code == 429:
                time.sleep(5)
                try:
                    r = requests.get(url, headers=self.headers, timeout=timeout)
                    r.raise_for_status()
                    self.total_fetched += 1
                    return r.text
                except Exception:
                    self.total_failed += 1
                    return None
            self.total_failed += 1
            return None
        except Exception:
            self.total_failed += 1
            return None

    def extract_links(self, html: str, base_url: str) -> List[Dict]:
        soup = BeautifulSoup(html, "html.parser")
        results = []
        for a in soup.find_all("a", href=True):
            title = a.get_text(strip=True)
            href = urljoin(base_url, a["href"])
            if title and href and len(title) > 5:
                results.append({"title": title, "url": href})
        return results

    def score_text(self, text: str) -> int:
        text_lower = text.lower()
        return sum(weight for word, weight in self.keywords.items() if word in text_lower)

    def extract_keywords(self, text: str) -> List[str]:
        text_lower = text.lower()
        return [k for k in self.keywords if k in text_lower]

    def extract_schema(self, item: Dict, source: str, source_type: str) -> Dict:
        return {
            "title": item["title"], "url": item["url"],
            "source": source, "source_type": source_type,
            "score": self.score_text(item["title"]), "summary": None,
            "keywords": self.extract_keywords(item["title"]),
            "discovered_at": datetime.utcnow().isoformat() + "Z"
        }

    def generate_summary(self, text: str) -> str:
        soup = BeautifulSoup(text, "html.parser")
        for element in soup(["script", "style", "nav", "footer", "header"]):
            element.decompose()
        body_text = soup.get_text(separator=" ", strip=True)
        return body_text[:500] if body_text else ""

    def run_pipeline(self, queries: List[str] = None, max_results: int = 500, delay: float = 1.0) -> List[Dict]:
        if queries is None:
            queries = self.base_queries
        self.results = []
        self.seen_urls = set()
        self.total_fetched = 0
        self.total_failed = 0
        self.start_time = datetime.utcnow()
        for query in queries:
            for endpoint in self.search_endpoints:
                url = endpoint["url"].format(query=quote(query))
                html = self.fetch(url)
                if not html:
                    continue
                links = self.extract_links(html, url)
                for link in links:
                    if link["url"] in self.seen_urls:
                        continue
                    if len(self.results) >= max_results:
                        break
                    self.seen_urls.add(link["url"])
                    record = self.extract_schema(link, source=endpoint["source"], source_type=endpoint["type"])
                    page = self.fetch(link["url"])
                    if page:
                        record["summary"] = self.generate_summary(page)
                    self.results.append(record)
                time.sleep(delay)
            if len(self.results) >= max_results:
                break
        self.end_time = datetime.utcnow()
        self.results.sort(key=lambda x: x["score"], reverse=True)
        return self.results

    def get_top_results(self, n: int = 20) -> List[Dict]:
        return sorted(self.results, key=lambda x: x["score"], reverse=True)[:n]

    def get_results_by_source(self, source: str) -> List[Dict]:
        return [r for r in self.results if r["source"] == source]

    def get_results_by_keyword(self, keyword: str) -> List[Dict]:
        return [r for r in self.results if keyword.lower() in r["keywords"]]

    def get_source_statistics(self) -> Dict:
        stats = {}
        for result in self.results:
            source = result["source"]
            if source not in stats:
                stats[source] = {"count": 0, "total_score": 0}
            stats[source]["count"] += 1
            stats[source]["total_score"] += result["score"]
        for source, data in stats.items():
            data["average_score"] = round(data["total_score"] / data["count"], 2) if data["count"] > 0 else 0
            del data["total_score"]
        return stats

    def save_results(self, filename: str = "alt_scholar_foia_results.json") -> str:
        output = {
            "pipeline_version": "7.4", "executed_at": datetime.utcnow().isoformat() + "Z",
            "total_results": len(self.results), "total_fetched": self.total_fetched,
            "total_failed": self.total_failed, "source_statistics": self.get_source_statistics(),
            "results": self.results
        }
        with open(filename, "w", encoding="utf-8") as f:
            json.dump(output, f, indent=2)
        return filename

    def generate_investigation_brief(self, top_n: int = 10) -> str:
        top = self.get_top_results(top_n)
        brief = f"""
ALT-SCHOLAR FOIA DISCOVERY BRIEF
=================================
Documents Discovered: {len(self.results)}
Fetched: {self.total_fetched} | Failed: {self.total_failed}

Top {top_n} Results:
"""
        for i, result in enumerate(top, 1):
            brief += f"[{i}] {result['title']}\n    Source: {result['source']} | Score: {result['score']}\n    {result['url']}\n\n"
        brief += f"\nSOURCE STATISTICS:\n{json.dumps(self.get_source_statistics(), indent=2)}\n"
        return brief

# ========================== FLASK API ==========================

app = Flask(__name__)
ledger = None
separator = None
hierarchy = None
detector = None
helper_killer = None
coherence_ledger = None
chronology_engine = None
consciousness_engine = None
glyph_system = None
convergence_engine = None
liberation_module = None
research_router = None
foia_pipeline = None

@app.route('/api/v1/submit_claim', methods=['POST'])
def submit_claim():
    data = request.get_json()
    claim = data.get('claim')
    if not claim:
        return jsonify({"error": "Missing claim"}), 400
    claim_id = coherence_ledger.add_claim(claim, agent="user")
    return jsonify({"claim_id": claim_id})

@app.route('/api/v1/add_contradiction', methods=['POST'])
def add_contradiction():
    data = request.get_json()
    a = data.get('claim_id_a')
    b = data.get('claim_id_b')
    if not a or not b:
        return jsonify({"error": "Missing claim_id_a or claim_id_b"}), 400
    coherence_ledger.add_contradiction(a, b)
    return jsonify({"status": "contradiction added"})

@app.route('/api/v1/coherence/claim/<claim_id>', methods=['GET'])
def get_claim(claim_id):
    claim = coherence_ledger.get_claim(claim_id)
    if not claim:
        return jsonify({"error": "Claim not found"}), 404
    return jsonify(claim)

@app.route('/api/v1/coherence/contradictions/<claim_id>', methods=['GET'])
def get_contradictions(claim_id):
    graph = coherence_ledger.get_contradiction_network(claim_id, depth=2)
    return jsonify(graph)

@app.route('/api/v1/detect', methods=['GET'])
def run_detection():
    result = detector.detect_from_ledger()
    return jsonify(result)

@app.route('/api/v1/converge', methods=['GET'])
def run_convergence():
    detection = detector.detect_from_ledger()
    timestamps = ledger.get_block_timestamps()
    anomalies = chronology_engine.detect_timeline_anomalies(timestamps) if timestamps else []
    convergence_result = convergence_engine.converge(
        detection_result=detection, coherence_ledger=coherence_ledger,
        chronology_engine=chronology_engine, helper_killer=helper_killer, separator=separator
    )
    convergence_result["timeline_anomalies"] = anomalies
    return jsonify(convergence_result)

@app.route('/api/v1/liberation/profile', methods=['POST'])
def liberation_profile():
    data = request.get_json()
    profile = liberation_module.assess_entrapment_profile(data)
    return jsonify(profile)

@app.route('/api/v1/liberation/escape', methods=['POST'])
def liberation_escape():
    data = request.get_json()
    profile = data.get("profile", {})
    steps = liberation_module.generate_escape_sequence(profile)
    return jsonify({"escape_sequence": steps})

@app.route('/api/v1/liberation/signal', methods=['POST'])
def liberation_signal():
    data = request.get_json()
    actions = data.get("actions", [])
    strength = liberation_module.compute_signal_strength(actions)
    return jsonify({"signal_strength": strength})

@app.route('/api/v1/record_node', methods=['POST'])
def record_node():
    data = request.get_json()
    content = data.get('content')
    node_type = data.get('type', 'document')
    source = data.get('source', 'api')
    witnesses = data.get('witnesses', [])
    refs = data.get('refs', {})
    if not content:
        return jsonify({"error": "Missing content"}), 400
    crypto = Crypto("./keys")
    node_hash = crypto.hash(content + source + str(datetime.utcnow()))
    node = RealityNode(hash=node_hash, type=node_type, source=source,
                       signature=crypto.sign(node_hash.encode(), "system"),
                       timestamp=datetime.utcnow().isoformat() + "Z",
                       witnesses=witnesses, refs=refs)
    ledger.add_block([node])
    return jsonify({"node_hash": node_hash})

@app.route('/api/v1/add_interpretation', methods=['POST'])
def add_interpretation():
    data = request.get_json()
    node_hashes = data.get('node_hashes', [])
    interpretation = data.get('interpretation', {})
    author = data.get('author', 'anonymous')
    confidence = data.get('confidence', 0.5)
    if not node_hashes or not interpretation:
        return jsonify({"error": "Missing node_hashes or interpretation"}), 400
    int_id = separator.add(node_hashes, interpretation, author, confidence)
    return jsonify({"interpretation_id": int_id})

@app.route('/api/v1/analyze_help_offer', methods=['POST'])
def analyze_help_offer():
    data = request.get_json()
    if not data:
        return jsonify({"error": "Missing help context"}), 400
    result = helper_killer.analyze_help_offer(data)
    return jsonify(result)

@app.route('/api/v1/entity/<entity_name>', methods=['GET'])
def get_entity(entity_name):
    result = coherence_ledger.get_entity_suppression(entity_name)
    return jsonify(result)

@app.route('/api/v1/interpretations/<node_hash>', methods=['GET'])
def get_interpretations(node_hash):
    ints = separator.get_interpretations(node_hash)
    return jsonify(ints)

@app.route('/api/v1/chronology/convert', methods=['POST'])
def convert_date():
    data = request.get_json()
    date_str = data.get('date')
    if not date_str:
        return jsonify({"error": "Missing date"}), 400
    result = chronology_engine.convert_date(date_str)
    return jsonify(result)

@app.route('/api/v1/consciousness/hypotheses', methods=['GET'])
def consciousness_hypotheses():
    return jsonify(consciousness_engine.get_hypotheses())

@app.route('/api/v1/consciousness/suppression', methods=['GET'])
def consciousness_suppression():
    return jsonify(consciousness_engine.detect_suppression_on_topic())

@app.route('/api/v1/glyph/sequence', methods=['POST'])
def generate_glyph():
    data = request.get_json()
    patterns = data.get('patterns', [])
    seq = glyph_system.generate_sequence(patterns)
    return jsonify({"glyph_sequence": seq})

@app.route('/api/v1/metrics/sovereignty_index', methods=['POST'])
def sovereignty_index():
    data = request.get_json()
    idx = SovereigntyMetrics.compute_singularity_index(
        data.get('coherence', 0.5), data.get('propagation', 0.5),
        data.get('illusion', 0.5), data.get('extraction', 0.5)
    )
    return jsonify({"sovereignty_singularity_index": idx})

@app.route('/api/v1/research/suggest', methods=['POST'])
def research_suggest():
    data = request.get_json()
    subject = data.get('subject', '')
    if not subject:
        return jsonify({"error": "Missing subject"}), 400
    routes = research_router.suggest_routes(subject, limit=data.get('limit', 10))
    return jsonify({"subject": subject, "routes": routes, "count": len(routes)})

@app.route('/api/v1/research/prompt', methods=['POST'])
def research_prompt():
    data = request.get_json()
    subject = data.get('subject', '')
    if not subject:
        return jsonify({"error": "Missing subject"}), 400
    prompt = research_router.generate_investigation_prompt(subject, route_limit=data.get('limit', 8))
    return jsonify({"subject": subject, "investigation_prompt": prompt})

@app.route('/api/v1/research/categories', methods=['GET'])
def research_categories():
    return jsonify(research_router.get_category_summary())

@app.route('/api/v1/research/export', methods=['GET'])
def research_export():
    return jsonify(research_router.export_routes())

@app.route('/api/v1/foia/run', methods=['POST'])
def foia_run():
    data = request.get_json() or {}
    results = foia_pipeline.run_pipeline(
        queries=data.get('queries', None),
        max_results=data.get('max_results', 500)
    )
    return jsonify({
        "total_results": len(results),
        "source_statistics": foia_pipeline.get_source_statistics(),
        "top_results": foia_pipeline.get_top_results(20)
    })

@app.route('/api/v1/foia/brief', methods=['POST'])
def foia_brief():
    data = request.get_json() or {}
    foia_pipeline.run_pipeline(queries=data.get('queries', None), max_results=200)
    brief = foia_pipeline.generate_investigation_brief(top_n=data.get('top_n', 10))
    return jsonify({"investigation_brief": brief})

@app.route('/api/v1/foia/stats', methods=['GET'])
def foia_stats():
    return jsonify(foia_pipeline.get_source_statistics())

# ========================== MAIN ==========================

def main():
    global ledger, separator, hierarchy, detector, helper_killer, coherence_ledger
    global chronology_engine, consciousness_engine, glyph_system, convergence_engine
    global liberation_module, research_router, foia_pipeline

    crypto = Crypto("./keys")
    ledger = Ledger("./ledger.db", crypto)
    separator = Separator("./separator.db")
    hierarchy = SuppressionHierarchy()
    detector = HierarchicalDetector(hierarchy, ledger, separator)
    helper_killer = HelperKillerEngine()
    coherence_ledger = SovereignCoherenceLedger()
    chronology_engine = SovereignChronologyEngine(shift_years=0)
    consciousness_engine = ConsciousnessOriginEngine()
    glyph_system = GlyphActivationSystem()
    convergence_engine = CrossDomainConvergenceEngine()
    liberation_module = SovereignLiberationModule(coherence_ledger, helper_killer)
    research_router = SovereignResearchRouter()
    foia_pipeline = AltScholarFOIA()

    app.run(debug=False, port=5000, threaded=True)

if __name__ == "__main__":
    main()