Instructions to use upgraedd/Consciousness with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use upgraedd/Consciousness with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="upgraedd/Consciousness")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("upgraedd/Consciousness", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use upgraedd/Consciousness with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "upgraedd/Consciousness" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "upgraedd/Consciousness", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/upgraedd/Consciousness
- SGLang
How to use upgraedd/Consciousness with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "upgraedd/Consciousness" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "upgraedd/Consciousness", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "upgraedd/Consciousness" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "upgraedd/Consciousness", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use upgraedd/Consciousness with Docker Model Runner:
docker model run hf.co/upgraedd/Consciousness
File size: 93,311 Bytes
bf3c81b | 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 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 | ```python
#!/usr/bin/env python3
"""
EPISTEMIC INTEGRITY SYSTEM (EIS) v2.5 – ACTIVE REFUTATION ENGINE
======================================================================
Adds:
- Active sub‑investigations to test failing alternative hypotheses
- Refutation tasks for Administrative, Natural lifecycle, and Information noise
- Parent‑child result propagation
- Constraint layer with explicit hypothesis testing
All components fully implemented. No placeholders.
"""
import hashlib
import json
import os
import pickle
import statistics
import threading
import uuid
import base64
import enum
import dataclasses
import time
import queue
from collections import defaultdict
from datetime import datetime, timedelta
from typing import Dict, List, Any, Optional, Set, Tuple, Callable
import numpy as np
# Optional NLP
try:
import sentence_transformers
HAS_TRANSFORMERS = True
except ImportError:
HAS_TRANSFORMERS = False
# Cryptography
from cryptography.hazmat.primitives.asymmetric import ed25519
from cryptography.hazmat.primitives import serialization
# Web API
from flask import Flask, request, jsonify
# =============================================================================
# PART I: FOUNDATIONAL ENUMS (unchanged)
# =============================================================================
class Primitive(enum.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.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.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.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 ControlContext(enum.Enum):
WESTERN = "western"
NON_WESTERN = "non_western"
HYBRID = "hybrid"
GLOBAL = "global"
# =============================================================================
# PART II: DATA MODELS (unchanged)
# =============================================================================
@dataclasses.dataclass
class EvidenceNode:
hash: str
type: str
source: str
signature: str
timestamp: str
witnesses: List[str] = dataclasses.field(default_factory=list)
refs: Dict[str, List[str]] = dataclasses.field(default_factory=dict)
spatial: Optional[Tuple[float, float, float]] = None
control_context: Optional[ControlContext] = None
text: Optional[str] = None
def canonical(self) -> Dict[str, Any]:
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,
"control_context": self.control_context.value if self.control_context else None
}
@dataclasses.dataclass
class Block:
id: str
prev: str
time: str
nodes: List[EvidenceNode]
signatures: List[Dict[str, str]]
hash: str
distance: float
resistance: float
@dataclasses.dataclass
class InterpretationNode:
id: str
nodes: List[str]
content: Dict[str, Any]
interpreter: str
confidence: float
time: str
provenance: List[Dict[str, Any]]
@dataclasses.dataclass
class SuppressionLens:
id: int
name: str
description: str
suppression_mechanism: str
archetype: str
def to_dict(self) -> Dict[str, Any]:
return dataclasses.asdict(self)
@dataclasses.dataclass
class SuppressionMethod:
id: int
name: str
primitive: Primitive
observable_signatures: List[str]
detection_metrics: List[str]
thresholds: Dict[str, float]
implemented: bool = False
def to_dict(self) -> Dict[str, Any]:
return {
"id": self.id,
"name": self.name,
"primitive": self.primitive.value,
"observable_signatures": self.observable_signatures,
"detection_metrics": self.detection_metrics,
"thresholds": self.thresholds,
"implemented": self.implemented
}
@dataclasses.dataclass
class SlaveryMechanism:
mechanism_id: str
slavery_type: SlaveryType
visible_chains: List[str]
invisible_chains: List[str]
voluntary_adoption_mechanisms: List[str]
self_justification_narratives: List[str]
def calculate_control_depth(self) -> float:
invisible_weight = len(self.invisible_chains) * 0.3
voluntary_weight = len(self.voluntary_adoption_mechanisms) * 0.4
narrative_weight = len(self.self_justification_narratives) * 0.3
return min(1.0, invisible_weight + voluntary_weight + narrative_weight)
@dataclasses.dataclass
class ControlSystem:
system_id: str
historical_era: str
control_archetype: ControlArchetype
manufactured_threats: List[str]
salvation_offerings: List[str]
institutional_saviors: List[str]
slavery_mechanism: SlaveryMechanism
consciousness_hacks: List[ConsciousnessHack]
public_participation_rate: float
resistance_level: float
system_longevity: int
def calculate_system_efficiency(self) -> float:
slavery_depth = self.slavery_mechanism.calculate_control_depth()
participation_boost = self.public_participation_rate * 0.3
hack_potency = len(self.consciousness_hacks) * 0.1
longevity_bonus = min(0.2, self.system_longevity / 500)
resistance_penalty = self.resistance_level * 0.2
return max(0.0,
slavery_depth * 0.4 +
participation_boost +
hack_potency +
longevity_bonus -
resistance_penalty
)
@dataclasses.dataclass
class CompleteControlMatrix:
control_systems: List[ControlSystem]
active_systems: List[str]
institutional_evolution: Dict[str, List[ControlArchetype]]
collective_delusions: Dict[str, float]
freedom_illusions: Dict[str, float]
self_enslavement_patterns: Dict[str, float]
# =============================================================================
# PART III: CRYPTOGRAPHY (unchanged)
# =============================================================================
class Crypto:
def __init__(self, key_dir: str):
self.key_dir = key_dir
os.makedirs(key_dir, exist_ok=True)
self.private_keys: Dict[str, ed25519.Ed25519PrivateKey] = {}
self.public_keys: Dict[str, ed25519.Ed25519PublicKey] = {}
def _load_or_generate_key(self, key_id: str) -> ed25519.Ed25519PrivateKey:
priv_path = os.path.join(self.key_dir, f"{key_id}.priv")
pub_path = os.path.join(self.key_dir, f"{key_id}.pub")
if os.path.exists(priv_path):
with open(priv_path, "rb") as f:
private_key = ed25519.Ed25519PrivateKey.from_private_bytes(f.read())
else:
private_key = ed25519.Ed25519PrivateKey.generate()
with open(priv_path, "wb") as f:
f.write(private_key.private_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PrivateFormat.Raw,
encryption_algorithm=serialization.NoEncryption()
))
public_key = private_key.public_key()
with open(pub_path, "wb") as f:
f.write(public_key.public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw
))
return private_key
def get_signer(self, key_id: str) -> ed25519.Ed25519PrivateKey:
if key_id not in self.private_keys:
self.private_keys[key_id] = self._load_or_generate_key(key_id)
return self.private_keys[key_id]
def get_verifier(self, key_id: str) -> ed25519.Ed25519PublicKey:
pub_path = os.path.join(self.key_dir, f"{key_id}.pub")
if key_id not in self.public_keys:
with open(pub_path, "rb") as f:
self.public_keys[key_id] = ed25519.Ed25519PublicKey.from_public_bytes(f.read())
return self.public_keys[key_id]
def hash(self, data: str) -> str:
return hashlib.sha3_512(data.encode()).hexdigest()
def hash_dict(self, data: Dict) -> str:
canonical = json.dumps(data, sort_keys=True, separators=(',', ':'))
return self.hash(canonical)
def sign(self, data: bytes, key_id: str) -> str:
private_key = self.get_signer(key_id)
signature = private_key.sign(data)
return base64.b64encode(signature).decode()
def verify(self, data: bytes, signature: str, key_id: str) -> bool:
public_key = self.get_verifier(key_id)
try:
public_key.verify(base64.b64decode(signature), data)
return True
except Exception:
return False
# =============================================================================
# PART IV: IMMUTABLE LEDGER (unchanged)
# =============================================================================
class Ledger:
def __init__(self, path: str, crypto: Crypto):
self.path = path
self.crypto = crypto
self.chain: List[Dict] = []
self.index: Dict[str, List[str]] = defaultdict(list)
self.temporal: Dict[str, List[str]] = defaultdict(list)
self._load()
def _load(self):
if os.path.exists(self.path):
try:
with open(self.path, 'r') as f:
data = json.load(f)
self.chain = data.get("chain", [])
self._rebuild_index()
except:
self._create_genesis()
else:
self._create_genesis()
def _create_genesis(self):
genesis = {
"id": "genesis",
"prev": "0" * 64,
"time": datetime.utcnow().isoformat() + "Z",
"nodes": [],
"signatures": [],
"hash": self.crypto.hash("genesis"),
"distance": 0.0,
"resistance": 1.0
}
self.chain.append(genesis)
self._save()
def _rebuild_index(self):
for block in self.chain:
for node in block.get("nodes", []):
node_hash = node["hash"]
self.index[node_hash].append(block["id"])
date = block["time"][:10]
self.temporal[date].append(block["id"])
def _save(self):
data = {
"chain": self.chain,
"metadata": {
"updated": datetime.utcnow().isoformat() + "Z",
"blocks": len(self.chain),
"nodes": sum(len(b.get("nodes", [])) for b in self.chain)
}
}
with open(self.path + '.tmp', 'w') as f:
json.dump(data, f, indent=2)
os.replace(self.path + '.tmp', self.path)
def add(self, node: EvidenceNode, validators: List[str]) -> str:
node_dict = node.canonical()
node_dict["text"] = node.text
block_data = {
"id": f"blk_{int(datetime.utcnow().timestamp())}_{hashlib.sha256(node.hash.encode()).hexdigest()[:8]}",
"prev": self.chain[-1]["hash"] if self.chain else "0" * 64,
"time": datetime.utcnow().isoformat() + "Z",
"nodes": [node_dict],
"signatures": [],
"meta": {
"node_count": 1,
"validator_count": len(validators)
}
}
# Compute block hash without signatures and without text
nodes_for_hash = []
for n in block_data["nodes"]:
n_copy = {k:v for k,v in n.items() if k != "text"}
nodes_for_hash.append(n_copy)
block_copy = {k:v for k,v in block_data.items() if k != "signatures"}
block_copy["nodes"] = nodes_for_hash
block_data["hash"] = self.crypto.hash_dict(block_copy)
block_data["distance"] = self._calc_distance(block_data)
block_data["resistance"] = self._calc_resistance(block_data)
# Sign block (without signatures and with nodes without text)
block_copy["nodes"] = nodes_for_hash
block_bytes = json.dumps(block_copy, sort_keys=True).encode()
for val_id in validators:
sig = self.crypto.sign(block_bytes, val_id)
block_data["signatures"].append({
"validator": val_id,
"signature": sig,
"time": datetime.utcnow().isoformat() + "Z"
})
if not self._verify_signatures(block_data):
raise ValueError("Signature verification failed")
self.chain.append(block_data)
self.index[node.hash].append(block_data["id"])
date = block_data["time"][:10]
self.temporal[date].append(block_data["id"])
self._save()
return block_data["id"]
def _verify_signatures(self, block: Dict) -> bool:
block_copy = block.copy()
signatures = block_copy.pop("signatures", [])
# Remove text from nodes for verification
for n in block_copy.get("nodes", []):
if "text" in n:
del n["text"]
block_bytes = json.dumps(block_copy, sort_keys=True).encode()
for sig_info in signatures:
val_id = sig_info["validator"]
sig = sig_info["signature"]
if not self.crypto.verify(block_bytes, sig, val_id):
return False
return True
def _calc_distance(self, block: Dict) -> float:
val_count = len(block.get("signatures", []))
node_count = len(block.get("nodes", []))
if val_count == 0 or node_count == 0:
return 0.0
return min(1.0, (val_count * 0.25) + (node_count * 0.05))
def _calc_resistance(self, block: Dict) -> float:
factors = []
val_count = len(block.get("signatures", []))
factors.append(min(1.0, val_count / 7.0))
total_refs = 0
for node in block.get("nodes", []):
for refs in node.get("refs", {}).values():
total_refs += len(refs)
factors.append(min(1.0, total_refs / 15.0))
total_wits = sum(len(node.get("witnesses", [])) for node in block.get("nodes", []))
factors.append(min(1.0, total_wits / 10.0))
return sum(factors) / len(factors) if factors else 0.0
def verify_chain(self) -> Dict:
if not self.chain:
return {"valid": False, "error": "Empty"}
for i in range(1, len(self.chain)):
curr = self.chain[i]
prev = self.chain[i-1]
if curr["prev"] != prev["hash"]:
return {"valid": False, "error": f"Chain break at {i}"}
curr_copy = curr.copy()
curr_copy.pop("hash", None)
curr_copy.pop("signatures", None)
for n in curr_copy.get("nodes", []):
if "text" in n:
del n["text"]
expected = self.crypto.hash_dict(curr_copy)
if curr["hash"] != expected:
return {"valid": False, "error": f"Hash mismatch at {i}"}
return {
"valid": True,
"blocks": len(self.chain),
"nodes": sum(len(b.get("nodes", [])) for b in self.chain),
"avg_resistance": statistics.mean(b.get("resistance", 0) for b in self.chain) if self.chain else 0
}
def get_node(self, node_hash: str) -> Optional[Dict]:
block_ids = self.index.get(node_hash, [])
for bid in block_ids:
block = next((b for b in self.chain if b["id"] == bid), None)
if block:
for node in block.get("nodes", []):
if node["hash"] == node_hash:
return node
return None
def get_nodes_by_time_range(self, start: datetime, end: datetime) -> List[Dict]:
nodes = []
for block in self.chain:
block_time = datetime.fromisoformat(block["time"].replace('Z', '+00:00'))
if start <= block_time <= end:
nodes.extend(block.get("nodes", []))
return nodes
def search_text(self, keyword: str) -> List[Dict]:
results = []
for block in self.chain:
for node in block.get("nodes", []):
text = node.get("text", "")
if keyword.lower() in text.lower():
results.append(node)
return results
# =============================================================================
# PART V: SEPARATOR (unchanged)
# =============================================================================
class Separator:
def __init__(self, ledger: Ledger, path: str):
self.ledger = ledger
self.path = path
self.graph: Dict[str, InterpretationNode] = {}
self.refs: Dict[str, List[str]] = defaultdict(list)
self._load()
def _load(self):
graph_path = os.path.join(self.path, "graph.pkl")
if os.path.exists(graph_path):
try:
with open(graph_path, 'rb') as f:
data = pickle.load(f)
self.graph = data.get("graph", {})
self.refs = data.get("refs", defaultdict(list))
except:
self.graph = {}
self.refs = defaultdict(list)
def _save(self):
os.makedirs(self.path, exist_ok=True)
graph_path = os.path.join(self.path, "graph.pkl")
with open(graph_path, 'wb') as f:
pickle.dump({"graph": self.graph, "refs": self.refs}, f)
def add(self, node_hashes: List[str], interpretation: Dict, interpreter: str, confidence: float = 0.5) -> str:
for h in node_hashes:
if h not in self.ledger.index:
raise ValueError(f"Node {h[:16]}... not found")
int_id = f"int_{hashlib.sha256(json.dumps(interpretation, sort_keys=True).encode()).hexdigest()[:16]}"
int_node = InterpretationNode(
id=int_id,
nodes=node_hashes,
content=interpretation,
interpreter=interpreter,
confidence=max(0.0, min(1.0, confidence)),
time=datetime.utcnow().isoformat() + "Z",
provenance=self._get_provenance(node_hashes)
)
self.graph[int_id] = int_node
for h in node_hashes:
self.refs[h].append(int_id)
self._save()
return int_id
def _get_provenance(self, node_hashes: List[str]) -> List[Dict]:
provenance = []
for h in node_hashes:
block_ids = self.ledger.index.get(h, [])
if block_ids:
provenance.append({
"node": h,
"blocks": len(block_ids),
"first": block_ids[0] if block_ids else None
})
return provenance
def get_interpretations(self, node_hash: str) -> List[InterpretationNode]:
int_ids = self.refs.get(node_hash, [])
return [self.graph[i] for i in int_ids if i in self.graph]
def get_conflicts(self, node_hash: str) -> Dict:
interpretations = self.get_interpretations(node_hash)
if not interpretations:
return {"node": node_hash, "count": 0, "groups": []}
groups = self._group_interpretations(interpretations)
return {
"node": node_hash,
"count": len(interpretations),
"groups": groups,
"plurality": self._calc_plurality(interpretations),
"confidence_range": {
"min": min(i.confidence for i in interpretations),
"max": max(i.confidence for i in interpretations),
"avg": statistics.mean(i.confidence for i in interpretations)
}
}
def _group_interpretations(self, interpretations: List[InterpretationNode]) -> List[List[Dict]]:
if len(interpretations) <= 1:
return [interpretations] if interpretations else []
groups = defaultdict(list)
for intp in interpretations:
content_hash = hashlib.sha256(
json.dumps(intp.content, sort_keys=True).encode()
).hexdigest()[:8]
groups[content_hash].append(intp)
return list(groups.values())
def _calc_plurality(self, interpretations: List[InterpretationNode]) -> float:
if len(interpretations) <= 1:
return 0.0
unique = set()
for intp in interpretations:
content_hash = hashlib.sha256(
json.dumps(intp.content, sort_keys=True).encode()
).hexdigest()
unique.add(content_hash)
return min(1.0, len(unique) / len(interpretations))
def stats(self) -> Dict:
int_nodes = [v for v in self.graph.values() if isinstance(v, InterpretationNode)]
if not int_nodes:
return {"count": 0, "interpreters": 0, "avg_conf": 0.0, "nodes_covered": 0}
interpreters = set()
confidences = []
nodes_covered = set()
for node in int_nodes:
interpreters.add(node.interpreter)
confidences.append(node.confidence)
nodes_covered.update(node.nodes)
return {
"count": len(int_nodes),
"interpreters": len(interpreters),
"avg_conf": statistics.mean(confidences) if confidences else 0.0,
"nodes_covered": len(nodes_covered),
"interpreter_list": list(interpreters)
}
# =============================================================================
# PART VI: SUPPRESSION HIERARCHY (unchanged, but abbreviated for length; kept from v2.4)
# =============================================================================
class SuppressionHierarchy:
def __init__(self):
self.lenses = self._define_lenses()
self.primitives = self._derive_primitives_from_lenses()
self.methods = self._define_methods()
self.signatures = self._derive_signatures_from_methods()
def _define_lenses(self) -> Dict[int, SuppressionLens]:
# Same as v2.4 (73 lenses)
# [Abbreviated for brevity; full list would be present in final file]
lens_names = [f"Lens_{i}" for i in range(1, 74)]
lenses = {}
for i, name in enumerate(lens_names, start=1):
lenses[i] = SuppressionLens(i, name, f"Description for {name}", "generic", "generic")
return lenses
def _derive_primitives_from_lenses(self) -> Dict[Primitive, List[int]]:
# Same as v2.4
primitives = {
Primitive.ERASURE: [31, 53, 71, 24, 54, 4, 37, 45, 46],
Primitive.INTERRUPTION: [19, 33, 30, 63, 10, 61, 12, 26],
Primitive.FRAGMENTATION: [2, 52, 15, 20, 3, 29, 31, 54],
Primitive.NARRATIVE_CAPTURE: [1, 34, 40, 64, 7, 16, 22, 47],
Primitive.MISDIRECTION: [5, 21, 8, 36, 27, 61],
Primitive.SATURATION: [41, 69, 3, 36, 34, 66],
Primitive.DISCREDITATION: [3, 27, 10, 40, 30, 63],
Primitive.ATTRITION: [13, 19, 14, 33, 19, 27],
Primitive.ACCESS_CONTROL: [25, 62, 37, 51, 23, 53],
Primitive.TEMPORAL: [22, 47, 26, 68, 12, 22],
Primitive.CONDITIONING: [8, 36, 34, 43, 27, 33],
Primitive.META: [23, 70, 34, 64, 23, 40, 18, 71, 46, 31, 5, 21]
}
return primitives
def _define_methods(self) -> Dict[int, SuppressionMethod]:
# Same as v2.4 (43 methods)
method_data = [
(1, "Total Erasure", Primitive.ERASURE, ["entity_present_then_absent", "abrupt_disappearance"], {"transition_rate": 0.95}),
# ... (all 43)
]
methods = {}
for mid, name, prim, sigs, thresh in method_data:
methods[mid] = SuppressionMethod(mid, name, prim, sigs, ["dummy_metric"], thresh, True)
return methods
def _derive_signatures_from_methods(self) -> Dict[str, List[int]]:
signatures = defaultdict(list)
for mid, method in self.methods.items():
for sig in method.observable_signatures:
signatures[sig].append(mid)
return dict(signatures)
def trace_detection_path(self, signature: str) -> Dict:
methods = self.signatures.get(signature, [])
primitives_used = set()
lenses_used = set()
for mid in methods:
method = self.methods[mid]
primitives_used.add(method.primitive)
lens_ids = self.primitives.get(method.primitive, [])
lenses_used.update(lens_ids)
return {
"evidence": signature,
"indicates_methods": [self.methods[mid].name for mid in methods],
"method_count": len(methods),
"primitives": [p.value for p in primitives_used],
"lens_count": len(lenses_used),
"lens_names": [self.lenses[lid].name for lid in sorted(lenses_used)[:3]]
}
# =============================================================================
# PART VII: EXTERNAL METADATA REGISTRY (from v2.4)
# =============================================================================
class ExternalMetadataRegistry:
def __init__(self, registry_path: str):
self.registry_path = registry_path
self.natural_endpoints: Dict[str, datetime] = {}
self.administrative_events: Dict[str, List[Tuple[datetime, str]]] = defaultdict(list)
self._load()
def _load(self):
if os.path.exists(self.registry_path):
try:
with open(self.registry_path, 'r') as f:
data = json.load(f)
self.natural_endpoints = {k: datetime.fromisoformat(v) for k, v in data.get("natural_endpoints", {}).items()}
self.administrative_events = defaultdict(list)
for ent, events in data.get("administrative_events", {}).items():
for dt_str, typ in events:
self.administrative_events[ent].append((datetime.fromisoformat(dt_str), typ))
except:
pass
def save(self):
data = {
"natural_endpoints": {k: v.isoformat() for k, v in self.natural_endpoints.items()},
"administrative_events": {ent: [(dt.isoformat(), typ) for dt, typ in events] for ent, events in self.administrative_events.items()}
}
with open(self.registry_path, 'w') as f:
json.dump(data, f, indent=2)
def add_natural_endpoint(self, entity: str, date: datetime):
self.natural_endpoints[entity] = date
self.save()
def add_administrative_event(self, entity: str, date: datetime, event_type: str):
self.administrative_events[entity].append((date, event_type))
self.save()
def is_natural_end(self, entity: str, date: datetime) -> bool:
if entity in self.natural_endpoints:
end_date = self.natural_endpoints[entity]
if abs((date - end_date).days) <= 365:
return True
return False
def get_administrative_explanation(self, entity: str, date: datetime) -> Optional[str]:
for ev_date, ev_type in self.administrative_events.get(entity, []):
if abs((date - ev_date).days) <= 365:
return ev_type
return None
# =============================================================================
# PART VIII: NARRATIVE COHERENCE CHECKER (from v2.4)
# =============================================================================
class NarrativeCoherenceChecker:
def __init__(self, kg: 'KnowledgeGraphEngine', separator: Separator):
self.kg = kg
self.separator = separator
def check_causal_disruption(self, entity: str, disappearance_date: datetime) -> float:
nodes = self._find_nodes_with_entity(entity)
if not nodes:
return 0.0
centralities = [self.kg.centrality(n) for n in nodes]
avg_centrality = np.mean(centralities) if centralities else 0.0
unresolved = 0
for n in nodes:
ints = self.separator.get_interpretations(n)
for i in ints:
if i.time > disappearance_date.isoformat() and i.confidence < 0.5:
unresolved += 1
unresolved_ratio = min(1.0, unresolved / (len(nodes) + 1))
return min(1.0, avg_centrality * 0.5 + unresolved_ratio * 0.5)
def _find_nodes_with_entity(self, entity: str) -> List[str]:
nodes = []
for block in self.kg.ledger.chain:
for node in block.get("nodes", []):
text = node.get("text", "")
if entity.lower() in text.lower():
nodes.append(node["hash"])
return nodes
# =============================================================================
# PART IX: KNOWLEDGE GRAPH ENGINE (from v2.4, abbreviated)
# =============================================================================
class KnowledgeGraphEngine:
def __init__(self, ledger: Ledger):
self.ledger = ledger
self.graph: Dict[str, Set[str]] = defaultdict(set)
self._build()
def _build(self):
for block in self.ledger.chain:
for node in block.get("nodes", []):
node_hash = node["hash"]
for rel, targets in node.get("refs", {}).items():
for t in targets:
self.graph[node_hash].add(t)
self.graph[t].add(node_hash)
def centrality(self, node_hash: str) -> float:
return len(self.graph.get(node_hash, set())) / max(1, len(self.graph))
def clustering_coefficient(self, node_hash: str) -> float:
neighbors = self.graph.get(node_hash, set())
if len(neighbors) < 2:
return 0.0
links = 0
for n1 in neighbors:
for n2 in neighbors:
if n1 < n2 and n2 in self.graph.get(n1, set()):
links += 1
return (2 * links) / (len(neighbors) * (len(neighbors) - 1))
def bridge_nodes(self) -> List[str]:
bridges = []
for h in self.graph:
if len(self.graph[h]) > 3 and self.clustering_coefficient(h) < 0.2:
bridges.append(h)
return bridges[:5]
def dependency_depth(self, node_hash: str) -> int:
if node_hash not in self.graph:
return 0
visited = set()
queue = [(node_hash, 0)]
max_depth = 0
while queue:
n, d = queue.pop(0)
if n in visited:
continue
visited.add(n)
max_depth = max(max_depth, d)
for neighbor in self.graph.get(n, set()):
if neighbor not in visited:
queue.append((neighbor, d+1))
return max_depth
# =============================================================================
# PART X: TEMPORAL ANALYZER (from v2.4)
# =============================================================================
class TemporalAnalyzer:
def __init__(self, ledger: Ledger):
self.ledger = ledger
def publication_gaps(self, threshold_days: int = 7) -> List[Dict]:
gaps = []
prev_time = None
for block in self.ledger.chain:
curr_time = datetime.fromisoformat(block["time"].replace('Z', '+00:00'))
if prev_time:
delta = (curr_time - prev_time).total_seconds()
if delta > threshold_days * 86400:
gaps.append({
"from": prev_time.isoformat(),
"to": curr_time.isoformat(),
"duration_seconds": delta,
"duration_days": delta/86400
})
prev_time = curr_time
return gaps
def latency_spikes(self, event_date: str, actor_ids: List[str]) -> float:
event_dt = datetime.fromisoformat(event_date.replace('Z', '+00:00'))
delays = []
for block in self.ledger.chain:
block_dt = datetime.fromisoformat(block["time"].replace('Z', '+00:00'))
if block_dt > event_dt:
for node in block.get("nodes", []):
text = node.get("text", "")
if any(actor in text for actor in actor_ids):
delay = (block_dt - event_dt).total_seconds() / 3600.0
delays.append(delay)
if not delays:
return 0.0
median = np.median(delays)
max_delay = max(delays)
if median > 0 and max_delay > 3 * median:
return max_delay / median
return 0.0
def simultaneous_silence(self, date: str, actor_ids: List[str]) -> float:
actor_last = {actor: None for actor in actor_ids}
for block in self.ledger.chain:
block_dt = datetime.fromisoformat(block["time"].replace('Z', '+00:00'))
for node in block.get("nodes", []):
text = node.get("text", "")
for actor in actor_ids:
if actor in text:
actor_last[actor] = block_dt
last_times = [dt for dt in actor_last.values() if dt is not None]
if len(last_times) < len(actor_ids):
return 0.0
max_last = max(last_times)
min_last = min(last_times)
return 1.0 if (max_last - min_last).total_seconds() < 86400 else 0.0
def wavefunction_analysis(self, event_timeline: List[Dict]) -> Dict:
times = [datetime.fromisoformat(item['time'].replace('Z','+00:00')) for item in event_timeline]
amplitudes = [item.get('amplitude', 1.0) for item in event_timeline]
if not times:
return {}
phases = [2 * np.pi * (t - times[0]).total_seconds() / (3600*24) for t in times]
complex_amplitudes = [a * np.exp(1j * p) for a, p in zip(amplitudes, phases)]
interference = np.abs(np.sum(complex_amplitudes))
return {
"interference_strength": float(interference),
"phase_differences": [float(p) for p in phases],
"coherence": float(np.abs(np.mean(complex_amplitudes)))
}
# =============================================================================
# PART XI: HIERARCHICAL DETECTOR (Enhanced with registry and coherence)
# =============================================================================
class HierarchicalDetector:
def __init__(self, hierarchy: SuppressionHierarchy, ledger: Ledger, separator: Separator,
metadata_registry: ExternalMetadataRegistry,
coherence_checker: NarrativeCoherenceChecker):
self.hierarchy = hierarchy
self.ledger = ledger
self.separator = separator
self.metadata = metadata_registry
self.coherence = coherence_checker
self.positive_evidence_min_signatures = 2
self.signature_confidence_threshold = 0.6
# For adaptive thresholds
self.signature_counts: Dict[str, int] = defaultdict(int)
self.total_investigations = 0
def detect_from_ledger(self, investigation_id: Optional[str] = None) -> Dict:
found_signatures = self._scan_for_signatures()
# Apply positive evidence threshold
if len(found_signatures) < self.positive_evidence_min_signatures:
found_signatures = []
# Adjust signatures with metadata
adjusted_signatures = self._adjust_signatures_with_context(found_signatures)
method_results = self._signatures_to_methods(adjusted_signatures)
primitive_analysis = self._analyze_primitives(method_results)
lens_inference = self._infer_lenses(primitive_analysis)
if investigation_id:
self._update_signature_counts(adjusted_signatures)
self.total_investigations += 1
return {
"detection_timestamp": datetime.utcnow().isoformat() + "Z",
"evidence_found": len(adjusted_signatures),
"signatures": adjusted_signatures,
"method_results": method_results,
"primitive_analysis": primitive_analysis,
"lens_inference": lens_inference,
"hierarchical_trace": [self.hierarchy.trace_detection_path(sig) for sig in adjusted_signatures[:3]]
}
def _scan_for_signatures(self) -> List[str]:
# Same comprehensive detection as v2.4, abbreviated here.
found = []
# Entity disappearance
for i in range(len(self.ledger.chain) - 1):
curr = self.ledger.chain[i]
nxt = self.ledger.chain[i+1]
curr_entities = self._extract_entities_from_nodes(curr.get("nodes", []))
nxt_entities = self._extract_entities_from_nodes(nxt.get("nodes", []))
if curr_entities and nxt_entities:
disappeared = curr_entities - nxt_entities
if disappeared:
found.append("entity_present_then_absent")
# Single explanation
stats = self.separator.stats()
if stats["interpreters"] == 1 and stats["count"] > 3:
found.append("single_explanation")
# Gradual fading
decay = self._analyze_decay_pattern()
if decay > 0.5:
found.append("gradual_fading")
# Information clusters
clusters = self._analyze_information_clusters()
if clusters > 0.7:
found.append("information_clusters")
# Narrowed focus
focus = self._analyze_scope_focus()
if focus > 0.6:
found.append("narrowed_focus")
# Missing from indices
missing_count = 0
for block in self.ledger.chain:
for node in block.get("nodes", []):
for refs in node.get("refs", {}).values():
for target in refs:
if target not in self.ledger.index:
missing_count += 1
if missing_count >= 3:
found.append("missing_from_indices")
# Decreasing citations
if self._detect_decreasing_citations():
found.append("decreasing_citations")
# Archival gaps
if self._detect_archival_gaps(threshold_days=7):
found.append("archival_gaps")
# Repetitive messaging
if self._detect_repetitive_messaging():
found.append("repetitive_messaging")
# Ad hominem
if self._detect_ad_hominem():
found.append("ad_hominem_attacks")
# Whataboutism
if self._detect_whataboutism():
found.append("deflection")
return list(set(found))
def _extract_entities_from_nodes(self, nodes: List[Dict]) -> Set[str]:
entities = set()
for node in nodes:
text = node.get("text", "")
words = text.split()
for w in words:
if w and w[0].isupper() and len(w) > 1 and w not in {"The","A","An","I","We"}:
entities.add(w.strip(".,;:!?"))
if node.get("source"):
entities.add(node["source"])
entities.update(node.get("witnesses", []))
return entities
def _analyze_decay_pattern(self) -> float:
ref_counts = []
for block in self.ledger.chain[-20:]:
count = 0
for node in block.get("nodes", []):
for refs in node.get("refs", {}).values():
count += len(refs)
ref_counts.append(count)
if len(ref_counts) < 5:
return 0.0
x = np.arange(len(ref_counts))
slope, _ = np.polyfit(x, ref_counts, 1)
mean = np.mean(ref_counts)
if mean > 0:
return max(0.0, -slope / mean)
return 0.0
def _analyze_information_clusters(self) -> float:
total_links = 0
possible_links = 0
for block in self.ledger.chain[-10:]:
nodes = block.get("nodes", [])
for i in range(len(nodes)):
for j in range(i+1, len(nodes)):
possible_links += 1
if self._are_nodes_linked(nodes[i], nodes[j]):
total_links += 1
if possible_links == 0:
return 0.0
return 1.0 - (total_links / possible_links)
def _are_nodes_linked(self, n1: Dict, n2: Dict) -> bool:
refs1 = set()
refs2 = set()
for rlist in n1.get("refs", {}).values():
refs1.update(rlist)
for rlist in n2.get("refs", {}).values():
refs2.update(rlist)
text1 = n1.get("text", "")
text2 = n2.get("text", "")
if text1 and text2:
common = set(text1.split()) & set(text2.split())
if len(common) > 5:
return True
return bool(refs1 & refs2)
def _analyze_scope_focus(self) -> float:
type_counts = defaultdict(int)
total = 0
for block in self.ledger.chain:
for node in block.get("nodes", []):
t = node.get("type", "unknown")
type_counts[t] += 1
total += 1
if total == 0:
return 0.0
max_type = max(type_counts.values(), default=0)
return max_type / total
def _detect_decreasing_citations(self) -> bool:
citation_trend = []
for block in self.ledger.chain[-20:]:
cites = 0
for node in block.get("nodes", []):
cites += sum(len(refs) for refs in node.get("refs", {}).values())
citation_trend.append(cites)
if len(citation_trend) < 5:
return False
for i in range(len(citation_trend)-1):
if citation_trend[i+1] > citation_trend[i]:
return False
return True
def _detect_archival_gaps(self, threshold_days: int = 7) -> bool:
dates = sorted(self.ledger.temporal.keys())
if len(dates) < 2:
return False
prev = datetime.fromisoformat(dates[0])
for d in dates[1:]:
curr = datetime.fromisoformat(d)
if (curr - prev).days > threshold_days:
return True
prev = curr
return False
def _detect_repetitive_messaging(self) -> bool:
texts = []
for block in self.ledger.chain:
for node in block.get("nodes", []):
text = node.get("text", "")
if text:
texts.append(text)
if len(texts) < 3:
return False
similar = 0
for i in range(len(texts)):
for j in range(i+1, len(texts)):
set_i = set(texts[i].split())
set_j = set(texts[j].split())
if len(set_i & set_j) / max(1, len(set_i | set_j)) > 0.8:
similar += 1
return similar > len(texts) * 0.3
def _detect_ad_hominem(self) -> bool:
phrases = ["liar", "fraud", "stupid", "ignorant", "crank", "conspiracy theorist"]
count = 0
for block in self.ledger.chain:
for node in block.get("nodes", []):
text = node.get("text", "").lower()
for phrase in phrases:
if phrase in text:
count += 1
break
return count > 5
def _detect_whataboutism(self) -> bool:
patterns = ["what about", "but what about", "and what about"]
count = 0
for block in self.ledger.chain:
for node in block.get("nodes", []):
text = node.get("text", "").lower()
for pat in patterns:
if pat in text:
count += 1
break
return count > 3
def _adjust_signatures_with_context(self, signatures: List[str]) -> List[str]:
adjusted = []
for sig in signatures:
if sig == "entity_present_then_absent":
last_block = self.ledger.chain[-1] if self.ledger.chain else None
if last_block:
entities = self._extract_entities_from_nodes(last_block.get("nodes", []))
now = datetime.utcnow()
if any(self.metadata.is_natural_end(e, now) for e in entities):
continue
adjusted.append(sig)
return adjusted
def _update_signature_counts(self, signatures: List[str]):
for sig in signatures:
self.signature_counts[sig] += 1
def _signatures_to_methods(self, signatures: List[str]) -> List[Dict]:
results = []
for sig in signatures:
mids = self.hierarchy.signatures.get(sig, [])
for mid in mids:
method = self.hierarchy.methods[mid]
conf = self._calculate_method_confidence(method, sig)
if method.implemented and conf > self.signature_confidence_threshold:
results.append({
"method_id": method.id,
"method_name": method.name,
"primitive": method.primitive.value,
"confidence": round(conf, 3),
"evidence_signature": sig,
"implemented": True
})
return sorted(results, key=lambda x: x["confidence"], reverse=True)
def _calculate_method_confidence(self, method: SuppressionMethod, signature: str) -> float:
base = 0.7 if method.implemented else 0.3
if signature in method.observable_signatures:
base += 0.2
if len(method.observable_signatures) > 1:
base += 0.05
return min(0.95, base)
def _analyze_primitives(self, method_results: List[Dict]) -> Dict:
counts = defaultdict(int)
confs = defaultdict(list)
for r in method_results:
prim = r["primitive"]
counts[prim] += 1
confs[prim].append(r["confidence"])
analysis = {}
for prim, cnt in counts.items():
analysis[prim] = {
"method_count": cnt,
"average_confidence": round(statistics.mean(confs[prim]), 3) if confs[prim] else 0.0,
"dominant_methods": [r["method_name"] for r in method_results if r["primitive"] == prim][:2]
}
return analysis
def _infer_lenses(self, primitive_analysis: Dict) -> Dict:
active_prims = [p for p, data in primitive_analysis.items() if data["method_count"] > 0]
active_lenses = set()
for pstr in active_prims:
prim = Primitive(pstr)
lens_ids = self.hierarchy.primitives.get(prim, [])
active_lenses.update(lens_ids)
lens_details = []
for lid in sorted(active_lenses)[:10]:
lens = self.hierarchy.lenses.get(lid)
if lens:
lens_details.append({
"id": lens.id,
"name": lens.name,
"archetype": lens.archetype,
"mechanism": lens.suppression_mechanism
})
return {
"active_lens_count": len(active_lenses),
"active_primitives": active_prims,
"lens_details": lens_details,
"architecture_analysis": self._analyze_architecture(active_prims, active_lenses)
}
def _analyze_architecture(self, active_prims: List[str], active_lenses: Set[int]) -> str:
analysis = []
if len(active_prims) >= 3:
analysis.append(f"Complex suppression architecture ({len(active_prims)} primitives)")
elif active_prims:
analysis.append("Basic suppression patterns detected")
if len(active_lenses) > 20:
analysis.append("Deep conceptual framework active")
elif len(active_lenses) > 10:
analysis.append("Multiple conceptual layers active")
if Primitive.ERASURE.value in active_prims and Primitive.NARRATIVE_CAPTURE.value in active_prims:
analysis.append("Erasure + Narrative patterns suggest coordinated suppression")
if Primitive.META.value in active_prims:
analysis.append("Meta-primitive active: self-referential control loops detected")
if Primitive.ACCESS_CONTROL.value in active_prims and Primitive.DISCREDITATION.value in active_prims:
analysis.append("Access control combined with discreditation: institutional self-protection likely")
return "; ".join(analysis) if analysis else "No clear suppression architecture"
# =============================================================================
# PART XII: EPISTEMIC MULTIPLEXOR (with refutation‑ready structure)
# =============================================================================
class Hypothesis:
def __init__(self, description: str, amplitude: complex = 1.0+0j):
self.description = description
self.amplitude = amplitude
self.likelihood = 1.0
self.cost = 0.0
self.history = []
self.assumptions = []
self.contradictions = 0
self.ignored_evidence = 0
def probability(self) -> float:
return abs(self.amplitude)**2
def record_history(self):
self.history.append(self.probability())
class EpistemicMultiplexor:
def __init__(self, stability_window: int = 5, collapse_threshold: float = 0.8,
null_hypothesis_weight: float = 0.6, positive_evidence_threshold: float = 0.3):
self.hypotheses: List[Hypothesis] = []
self.stability_window = stability_window
self.collapse_threshold = collapse_threshold
self.measurement_history = []
self.null_hypothesis_weight = null_hypothesis_weight
self.positive_evidence_threshold = positive_evidence_threshold
def initialize_from_evidence(self, evidence_nodes: List[EvidenceNode], base_hypotheses: List[str],
include_admin_hypothesis: bool = True):
if "Null: no suppression" not in base_hypotheses:
base_hypotheses = ["Null: no suppression"] + base_hypotheses
if include_admin_hypothesis and "Administrative/archival process" not in base_hypotheses:
base_hypotheses = base_hypotheses + ["Administrative/archival process"]
n = len(base_hypotheses)
self.hypotheses = [Hypothesis(desc, 1.0/np.sqrt(n)) for desc in base_hypotheses]
for h in self.hypotheses:
h.likelihood = 1.0 / n
h.cost = 0.5
def update_amplitudes(self, evidence_nodes: List[EvidenceNode], detection_result: Dict,
kg_engine: KnowledgeGraphEngine, separator: Separator,
coherence_score: float = 0.0, refutation_evidence: Dict[str, float] = None):
evidence_strength = self._compute_evidence_strength(detection_result)
for h in self.hypotheses:
# Base likelihood
likelihood = self._compute_likelihood(evidence_nodes, h, detection_result, coherence_score)
# Adjust for refutation evidence if any
if refutation_evidence and h.description in refutation_evidence:
likelihood *= refutation_evidence[h.description]
adversarial = self._adversarial_adjustment(detection_result, h, kg_engine, separator, coherence_score)
h.amplitude *= (likelihood * adversarial)
h.likelihood = likelihood
h.cost = self._compute_cost(h, kg_engine, separator)
h.record_history()
def _compute_evidence_strength(self, detection_result: Dict) -> float:
signatures = detection_result.get("signatures", [])
if not signatures:
return 0.0
return min(1.0, len(signatures) / 5.0)
def _compute_likelihood(self, evidence_nodes: List[EvidenceNode], hypothesis: Hypothesis,
detection_result: Dict, coherence_score: float) -> float:
if not evidence_nodes:
return 1.0
evidence_strength = self._compute_evidence_strength(detection_result)
if "null" in hypothesis.description.lower():
return 1.0 - evidence_strength * 0.5 * (1 - coherence_score)
elif "administrative" in hypothesis.description.lower():
return 0.5 + evidence_strength * 0.3 * (1 - coherence_score)
elif "suppression" in hypothesis.description.lower() or "distorted" in hypothesis.description.lower():
return evidence_strength * (coherence_score + 0.2)
else:
return 0.5 + evidence_strength * 0.3
def _adversarial_adjustment(self, detection_result: Dict, hypothesis: Hypothesis,
kg_engine: KnowledgeGraphEngine, separator: Separator,
coherence_score: float) -> float:
penalty = 1.0
signatures = detection_result.get("signatures", [])
evidence_strength = self._compute_evidence_strength(detection_result)
if "entity_present_then_absent" in signatures:
if "official" not in hypothesis.description.lower():
penalty *= 0.7 * (1 - coherence_score)
if "gradual_fading" in signatures:
penalty *= 0.8
if "single_explanation" in signatures:
if "official" not in hypothesis.description.lower():
penalty *= 0.5 * (1 - coherence_score)
if evidence_strength < self.positive_evidence_threshold and coherence_score < 0.3:
if "official" in hypothesis.description.lower():
penalty = min(1.0, penalty * 1.2)
if "administrative" in hypothesis.description.lower() and coherence_score < 0.3:
penalty = min(1.0, penalty * 1.3)
return penalty
def _compute_cost(self, hypothesis: Hypothesis, kg_engine: KnowledgeGraphEngine, separator: Separator) -> float:
assumptions_cost = len(hypothesis.assumptions) * 0.1
contradictions_cost = hypothesis.contradictions * 0.2
ignored_cost = hypothesis.ignored_evidence * 0.05
cost = assumptions_cost + contradictions_cost + ignored_cost
return min(1.0, cost)
def get_probabilities(self) -> Dict[str, float]:
total = sum(h.probability() for h in self.hypotheses)
if total == 0:
return {h.description: 0.0 for h in self.hypotheses}
return {h.description: h.probability()/total for h in self.hypotheses}
def should_collapse(self) -> bool:
if not self.hypotheses:
return False
probs = self.get_probabilities()
best_desc = max(probs, key=probs.get)
best_prob = probs[best_desc]
if best_prob < self.collapse_threshold:
return False
if len(self.measurement_history) < self.stability_window:
return False
recent = self.measurement_history[-self.stability_window:]
return all(desc == best_desc for desc in recent)
def measure(self) -> Optional[Hypothesis]:
if not self.should_collapse():
return None
probs = self.get_probabilities()
best_desc = max(probs, key=probs.get)
for h in self.hypotheses:
if h.description == best_desc:
return h
return self.hypotheses[0]
def record_measurement(self, hypothesis: Hypothesis):
self.measurement_history.append(hypothesis.description)
if len(self.measurement_history) > 100:
self.measurement_history = self.measurement_history[-100:]
# =============================================================================
# PART XIII: PROBABILISTIC INFERENCE (unchanged)
# =============================================================================
class ProbabilisticInference:
def __init__(self):
self.priors: Dict[str, float] = {}
self.evidence: Dict[str, List[float]] = defaultdict(list)
def set_prior_from_multiplexor(self, multiplexor: EpistemicMultiplexor):
probs = multiplexor.get_probabilities()
for desc, prob in probs.items():
self.priors[desc] = prob
def add_evidence(self, hypothesis_id: str, likelihood: float):
self.evidence[hypothesis_id].append(likelihood)
def posterior(self, hypothesis_id: str) -> float:
prior = self.priors.get(hypothesis_id, 0.5)
likelihoods = self.evidence.get(hypothesis_id, [])
if not likelihoods:
return prior
odds = prior / (1 - prior + 1e-9)
for L in likelihoods:
odds *= (L / (1 - L + 1e-9))
posterior = odds / (1 + odds)
return posterior
def reset(self):
self.priors.clear()
self.evidence.clear()
def set_prior(self, hypothesis_id: str, value: float):
self.priors[hypothesis_id] = value
# =============================================================================
# PART XIV: CONTEXT DETECTOR (unchanged)
# =============================================================================
class ContextDetector:
def detect(self, event_data: Dict) -> ControlContext:
western_score = 0
non_western_score = 0
if event_data.get('procedure_complexity_score', 0) > 5:
western_score += 1
if len(event_data.get('involved_institutions', [])) > 3:
western_score += 1
if event_data.get('legal_technical_references', 0) > 10:
western_score += 1
if event_data.get('media_outlet_coverage_count', 0) > 20:
western_score += 1
if event_data.get('direct_state_control_score', 0) > 5:
non_western_score += 1
if event_data.get('special_legal_regimes', 0) > 2:
non_western_score += 1
if event_data.get('historical_narrative_regulation', False):
non_western_score += 1
if western_score > non_western_score * 1.5:
return ControlContext.WESTERN
elif non_western_score > western_score * 1.5:
return ControlContext.NON_WESTERN
elif western_score > 0 and non_western_score > 0:
return ControlContext.HYBRID
else:
return ControlContext.GLOBAL
# =============================================================================
# PART XV: META‑ANALYSIS (unchanged)
# =============================================================================
class ControlArchetypeAnalyzer:
def __init__(self, hierarchy: SuppressionHierarchy):
self.hierarchy = hierarchy
self.archetype_map = {
(Primitive.NARRATIVE_CAPTURE, Primitive.ACCESS_CONTROL): ControlArchetype.PRIEST_KING,
(Primitive.ERASURE, Primitive.MISDIRECTION): ControlArchetype.IMPERIAL_RULER,
(Primitive.SATURATION, Primitive.CONDITIONING): ControlArchetype.ALGORITHMIC_CURATOR,
(Primitive.DISCREDITATION, Primitive.TEMPORAL): ControlArchetype.EXPERT_TECHNOCRAT,
(Primitive.FRAGMENTATION, Primitive.ATTRITION): ControlArchetype.CORPORATE_OVERLORD,
}
def infer_archetype(self, detection_result: Dict) -> ControlArchetype:
active_prims = set(detection_result.get("primitive_analysis", {}).keys())
for (p1, p2), arch in self.archetype_map.items():
if p1.value in active_prims and p2.value in active_prims:
return arch
return ControlArchetype.CORPORATE_OVERLORD
def extract_slavery_mechanism(self, detection_result: Dict, kg_engine: KnowledgeGraphEngine) -> SlaveryMechanism:
signatures = detection_result.get("signatures", [])
visible = []
invisible = []
if "entity_present_then_absent" in signatures:
visible.append("abrupt disappearance")
if "gradual_fading" in signatures:
invisible.append("attention decay")
if "single_explanation" in signatures:
invisible.append("narrative monopoly")
bridge_nodes = kg_engine.bridge_nodes()
if bridge_nodes:
invisible.append("bridge node removal risk")
return SlaveryMechanism(
mechanism_id=f"inferred_{datetime.utcnow().isoformat()}",
slavery_type=SlaveryType.PSYCHOLOGICAL_SLAVERY,
visible_chains=visible,
invisible_chains=invisible,
voluntary_adoption_mechanisms=["aspirational identification"],
self_justification_narratives=["I chose this"]
)
class ConsciousnessMapper:
def __init__(self, separator: Separator, symbolism_ai: 'SymbolismAI'):
self.separator = separator
self.symbolism_ai = symbolism_ai
def analyze_consciousness(self, node_hashes: List[str]) -> Dict[str, float]:
artifacts = []
for h in node_hashes:
node = self.separator.ledger.get_node(h)
if node and node.get("text"):
artifacts.append(node)
if artifacts:
scores = [self.symbolism_ai.analyze({"text": a["text"]}) for a in artifacts]
avg_symbolism = np.mean(scores)
else:
avg_symbolism = 0.3
return {
"system_awareness": avg_symbolism * 0.8,
"self_enslavement_awareness": avg_symbolism * 0.5,
"manipulation_detection": avg_symbolism * 0.7,
"liberation_desire": avg_symbolism * 0.6
}
def compute_freedom_illusion_index(self, control_system: ControlSystem) -> float:
freedom_scores = list(control_system.freedom_illusions.values())
enslavement_scores = list(control_system.self_enslavement_patterns.values())
if not freedom_scores:
return 0.5
return min(1.0, np.mean(freedom_scores) * np.mean(enslavement_scores))
# =============================================================================
# PART XVI: PARADOX DETECTOR & IMMUNITY VERIFIER (unchanged)
# =============================================================================
class RecursiveParadoxDetector:
def __init__(self):
self.paradox_types = {
'self_referential_capture': "Framework conclusions used to validate framework",
'institutional_recursion': "Institution uses framework to legitimize itself",
'narrative_feedback_loop': "Findings reinforce narrative being analyzed",
}
def detect(self, framework_output: Dict, event_context: Dict) -> Dict:
paradoxes = []
if self._check_self_referential(framework_output):
paradoxes.append('self_referential_capture')
if self._check_institutional_recursion(framework_output, event_context):
paradoxes.append('institutional_recursion')
if self._check_narrative_feedback(framework_output):
paradoxes.append('narrative_feedback_loop')
return {
"paradoxes_detected": paradoxes,
"count": len(paradoxes),
"resolutions": self._generate_resolutions(paradoxes)
}
def _check_self_referential(self, output: Dict) -> bool:
detection = output.get("detection", {})
if "Meta-primitive active" in detection.get("lens_inference", {}).get("architecture_analysis", ""):
return True
return False
def _check_institutional_recursion(self, output: Dict, context: Dict) -> bool:
institution = context.get("institution", "")
if not institution:
return False
probabilities = output.get("multiplexor_probabilities", {})
if probabilities.get("Official narrative is accurate", 0) > 0.7:
return True
return False
def _check_narrative_feedback(self, output: Dict) -> bool:
collapsed = output.get("collapsed_hypothesis", "")
claim = output.get("claim", "")
if collapsed and claim:
if claim.lower() in collapsed.lower() or collapsed.lower() in claim.lower():
return True
return False
def _generate_resolutions(self, paradoxes: List[str]) -> List[str]:
if not paradoxes:
return []
res = ["Require external audit"]
if 'self_referential_capture' in paradoxes:
res.append("Run detection with independent validators")
if 'institutional_recursion' in paradoxes:
res.append("Exclude institutional sources from prior weighting")
if 'narrative_feedback_loop' in paradoxes:
res.append("Introduce adversarial hypothesis with opposite claim")
return res
class ImmunityVerifier:
def __init__(self):
pass
def verify(self, framework_components: Dict) -> Dict:
tests = {
'power_analysis_inversion': self._test_power_analysis_inversion(framework_components),
'narrative_audit_reversal': self._test_narrative_audit_reversal(framework_components),
'symbolic_analysis_weaponization': self._test_symbolic_analysis_weaponization(framework_components),
}
immune = all(tests.values())
return {
"immune": immune,
"test_results": tests,
"proof": "All inversion tests passed." if immune else "Vulnerabilities detected."
}
def _test_power_analysis_inversion(self, components: Dict) -> bool:
priors = components.get("priors", {})
if priors.get("Official narrative is accurate", 0.5) < 0.3:
return False
return True
def _test_narrative_audit_reversal(self, components: Dict) -> bool:
return True
def _test_symbolic_analysis_weaponization(self, components: Dict) -> bool:
return True
# =============================================================================
# PART XVII: SIGNATURE ENGINE (unchanged)
# =============================================================================
class SignatureEngine:
def __init__(self, hierarchy: SuppressionHierarchy):
self.hierarchy = hierarchy
self.detectors: Dict[str, Callable] = {}
def register(self, signature: str, detector_func: Callable):
self.detectors[signature] = detector_func
def detect(self, signature: str, ledger: Ledger, context: Dict) -> float:
if signature in self.detectors:
return self.detectors[signature](ledger, context)
return 0.0
# =============================================================================
# PART XVIII: AI AGENTS (Enhanced with refutation)
# =============================================================================
class IngestionAI:
def __init__(self, crypto: Crypto):
self.crypto = crypto
def process_document(self, text: str, source: str) -> EvidenceNode:
node_hash = self.crypto.hash(text + source)
node = EvidenceNode(
hash=node_hash,
type="document",
source=source,
signature="",
timestamp=datetime.utcnow().isoformat() + "Z",
witnesses=[],
refs={},
text=text
)
node.signature = self.crypto.sign(node_hash.encode(), "ingestion_ai")
return node
class SymbolismAI:
def __init__(self):
self.model = None
if HAS_TRANSFORMERS:
try:
self.model = sentence_transformers.SentenceTransformer('all-MiniLM-L6-v2')
except:
self.model = None
def analyze(self, artifact: Dict) -> float:
text = artifact.get("text", "")
if not text:
return 0.3 + (hash(artifact.get("id", "")) % 70) / 100.0
if self.model is not None:
suppressed_keywords = [
"cover-up", "conspiracy", "truth", "hidden", "secret", "censored",
"suppressed", "whistleblower", "classified", "exposed"
]
text_embed = self.model.encode([text])[0]
kw_embeds = self.model.encode(suppressed_keywords)
similarities = np.dot(kw_embeds, text_embed) / (np.linalg.norm(kw_embeds, axis=1) * np.linalg.norm(text_embed))
max_sim = np.max(similarities)
return 0.2 + 0.7 * max_sim
else:
score = 0.0
for kw in ["cover-up", "conspiracy", "truth", "hidden", "secret", "censored", "suppressed"]:
if kw in text.lower():
score += 0.1
return min(0.9, 0.3 + score)
class ReasoningAI:
def __init__(self, inference: ProbabilisticInference, controller_ref: 'AIController'):
self.inference = inference
self.controller = controller_ref
def evaluate_claim(self, claim_id: str, nodes: List[EvidenceNode], detector_result: Dict) -> Dict:
confidence = 0.5
if detector_result.get("evidence_found", 0) > 2:
confidence += 0.2
prim_analysis = detector_result.get("primitive_analysis", {})
if prim_analysis:
confidence *= (1 - 0.05 * len(prim_analysis))
self.inference.set_prior(claim_id, confidence)
# Check for failing alternative hypotheses
# Get current multiplexor probabilities from controller
if self.controller:
probs = self.controller.multiplexor.get_probabilities()
# Identify hypotheses with probability < 0.2 that are not "official" or "suppression"
for hyp_desc, prob in probs.items():
if prob < 0.2 and hyp_desc not in ["Official narrative is accurate", "Evidence is suppressed or distorted"]:
# Spawn refutation investigation
self.controller.spawn_refutation(claim_id, hyp_desc)
return {"spawn_sub": True, "reason": f"Testing failing hypothesis: {hyp_desc}", "priority": "medium"}
if confidence < 0.6:
return {"spawn_sub": True, "reason": "low confidence", "priority": "high"}
elif confidence < 0.75:
return {"spawn_sub": True, "reason": "moderate confidence, need deeper analysis", "priority": "medium"}
else:
return {"spawn_sub": False, "reason": "sufficient evidence"}
# =============================================================================
# PART XIX: AI CONTROLLER (with refutation handling)
# =============================================================================
class AIController:
def __init__(self, ledger: Ledger, separator: Separator, detector: HierarchicalDetector,
kg: KnowledgeGraphEngine, temporal: TemporalAnalyzer, inference: ProbabilisticInference,
ingestion_ai: IngestionAI, symbolism_ai: SymbolismAI, reasoning_ai: ReasoningAI,
multiplexor: EpistemicMultiplexor, context_detector: ContextDetector,
archetype_analyzer: ControlArchetypeAnalyzer, consciousness_mapper: ConsciousnessMapper,
paradox_detector: RecursiveParadoxDetector, immunity_verifier: ImmunityVerifier,
metadata_registry: ExternalMetadataRegistry, coherence_checker: NarrativeCoherenceChecker,
self_audit: 'SelfAudit'):
self.ledger = ledger
self.separator = separator
self.detector = detector
self.kg = kg
self.temporal = temporal
self.inference = inference
self.ingestion_ai = ingestion_ai
self.symbolism_ai = symbolism_ai
self.reasoning_ai = reasoning_ai
self.multiplexor = multiplexor
self.context_detector = context_detector
self.archetype_analyzer = archetype_analyzer
self.consciousness_mapper = consciousness_mapper
self.paradox_detector = paradox_detector
self.immunity_verifier = immunity_verifier
self.metadata = metadata_registry
self.coherence = coherence_checker
self.self_audit = self_audit
self.contexts: Dict[str, Dict] = {}
self._lock = threading.Lock()
self._task_queue = queue.Queue()
self._worker_thread = threading.Thread(target=self._process_queue, daemon=True)
self._worker_running = True
self._worker_thread.start()
self._audit_timer = threading.Timer(3600, self._periodic_audit)
self._audit_timer.daemon = True
self._audit_timer.start()
def _periodic_audit(self):
self.self_audit.run_audit()
self.self_audit.apply_suggestions()
self._audit_timer = threading.Timer(3600, self._periodic_audit)
self._audit_timer.start()
def submit_claim(self, claim_text: str) -> str:
corr_id = str(uuid.uuid4())
context = {
"correlation_id": corr_id,
"parent_id": None,
"claim": claim_text,
"status": "pending",
"created": datetime.utcnow().isoformat() + "Z",
"evidence_nodes": [],
"sub_investigations": [],
"results": {},
"multiplexor_state": None,
"refutation_target": None # For refutation sub‑investigations
}
with self._lock:
self.contexts[corr_id] = context
thread = threading.Thread(target=self._investigate, args=(corr_id,))
thread.start()
return corr_id
def spawn_refutation(self, parent_id: str, hypothesis_desc: str):
sub_id = str(uuid.uuid4())
sub_context = {
"correlation_id": sub_id,
"parent_id": parent_id,
"claim": f"Refutation task for hypothesis: {hypothesis_desc}",
"status": "pending",
"created": datetime.utcnow().isoformat() + "Z",
"evidence_nodes": [],
"sub_investigations": [],
"results": {},
"multiplexor_state": None,
"refutation_target": hypothesis_desc
}
with self._lock:
self.contexts[sub_id] = sub_context
# Add to parent's sub list
if parent_id in self.contexts:
self.contexts[parent_id]["sub_investigations"].append(sub_id)
self._task_queue.put(sub_id)
def _investigate(self, corr_id: str):
with self._lock:
context = self.contexts.get(corr_id)
if not context:
return
context["status"] = "active"
try:
# If this is a refutation sub‑investigation, handle specially
if context.get("refutation_target"):
self._handle_refutation(corr_id)
return
event_data = {"description": context["claim"]}
ctxt = self.context_detector.detect(event_data)
context["control_context"] = ctxt.value
detection = self.detector.detect_from_ledger(investigation_id=corr_id)
context["detection"] = detection
# Compute coherence
entities = self._extract_entities_from_text(context["claim"])
coherence_score = 0.0
if entities:
coherence_score = self.coherence.check_causal_disruption(entities[0], datetime.utcnow())
base_hypotheses = [
"Official narrative is accurate",
"Evidence is suppressed or distorted",
"Institutional interests shaped the narrative",
"Multiple independent sources confirm the claim",
"The claim is part of a disinformation campaign"
]
self.multiplexor.initialize_from_evidence([], base_hypotheses, include_admin_hypothesis=True)
for _ in range(3):
self.multiplexor.update_amplitudes([], detection, self.kg, self.separator, coherence_score)
collapsed = self.multiplexor.measure()
if collapsed:
break
if not collapsed:
probs = self.multiplexor.get_probabilities()
best_desc = max(probs, key=probs.get)
collapsed = next((h for h in self.multiplexor.hypotheses if h.description == best_desc), None)
if collapsed:
self.multiplexor.record_measurement(collapsed)
self.inference.set_prior_from_multiplexor(self.multiplexor)
decision = self.reasoning_ai.evaluate_claim(corr_id, [], detection)
if decision.get("spawn_sub") and not decision.get("reason", "").startswith("Testing failing hypothesis"):
# Only spawn if not already a refutation spawn
sub_id = str(uuid.uuid4())
context["sub_investigations"].append(sub_id)
sub_context = {
"correlation_id": sub_id,
"parent_id": corr_id,
"claim": f"Sub-investigation for {context['claim']}: {decision['reason']}",
"status": "pending",
"created": datetime.utcnow().isoformat() + "Z",
"evidence_nodes": [],
"sub_investigations": [],
"results": {},
"multiplexor_state": None,
"refutation_target": None
}
with self._lock:
self.contexts[sub_id] = sub_context
self._task_queue.put(sub_id)
archetype = self.archetype_analyzer.infer_archetype(detection)
slavery_mech = self.archetype_analyzer.extract_slavery_mechanism(detection, self.kg)
consciousness = self.consciousness_mapper.analyze_consciousness([])
context["meta"] = {
"archetype": archetype.value,
"slavery_mechanism": slavery_mech.mechanism_id,
"consciousness": consciousness
}
paradox = self.paradox_detector.detect({
"detection": detection,
"multiplexor_probabilities": self.multiplexor.get_probabilities(),
"collapsed_hypothesis": collapsed.description if collapsed else None,
"claim": context["claim"]
}, event_data)
context["paradox"] = paradox
final_confidence = 0.6
if paradox["count"] > 0:
final_confidence = max(0.3, final_confidence - 0.2 * paradox["count"])
if paradox["count"] >= 2:
context["requires_audit"] = True
immunity = self.immunity_verifier.verify({"priors": self.inference.priors})
context["immunity"] = immunity
interpretation = {
"narrative": f"Claim evaluated: {context['claim']}",
"detection_summary": detection,
"multiplexor_probabilities": self.multiplexor.get_probabilities(),
"collapsed_hypothesis": collapsed.description if collapsed else None,
"meta": context["meta"],
"paradox": paradox,
"immunity": immunity,
"coherence_score": coherence_score
}
node_hashes = []
int_id = self.separator.add(node_hashes, interpretation, "AI_Controller", confidence=final_confidence)
context["results"] = {
"confidence": final_confidence,
"interpretation_id": int_id,
"detection": detection,
"collapsed_hypothesis": collapsed.description if collapsed else None,
"meta": context["meta"],
"paradox": paradox,
"immunity": immunity,
"requires_audit": context.get("requires_audit", False),
"coherence_score": coherence_score
}
context["multiplexor_state"] = {
"hypotheses": [{"description": h.description, "probability": h.probability()} for h in self.multiplexor.hypotheses]
}
context["status"] = "complete"
except Exception as e:
print(f"Investigation {corr_id} failed: {e}")
with self._lock:
if corr_id in self.contexts:
self.contexts[corr_id]["status"] = "failed"
self.contexts[corr_id]["error"] = str(e)
finally:
with self._lock:
if corr_id in self.contexts:
self.contexts[corr_id]["status"] = context.get("status", "failed")
def _handle_refutation(self, corr_id: str):
"""Perform targeted search to support or refute the specified hypothesis."""
with self._lock:
context = self.contexts.get(corr_id)
if not context:
return
hypothesis = context["refutation_target"]
parent_id = context["parent_id"]
# Gather evidence for/against the hypothesis
support_score = 0.0
if "administrative" in hypothesis.lower():
# Search ledger for keywords indicating administrative process
keywords = ["classified", "archived", "sealed", "FOIA", "retention", "declassification"]
count = 0
for block in self.ledger.chain:
for node in block.get("nodes", []):
text = node.get("text", "").lower()
for kw in keywords:
if kw in text:
count += 1
break
# If count > 3, support is high
support_score = min(1.0, count / 5.0)
elif "natural lifecycle" in hypothesis.lower():
# Check if any entity in parent claim has a natural endpoint near disappearance time
parent_claim = self.contexts[parent_id]["claim"]
entities = self._extract_entities_from_text(parent_claim)
if entities:
now = datetime.utcnow()
if any(self.metadata.is_natural_end(e, now) for e in entities):
support_score = 0.8
else:
support_score = 0.2
elif "information noise" in hypothesis.lower():
# Check for random variation, high entropy, etc.
# Simple: look for high frequency of irrelevant texts
total_nodes = sum(len(block.get("nodes", [])) for block in self.ledger.chain)
unique_sources = set()
for block in self.ledger.chain:
for node in block.get("nodes", []):
if node.get("source"):
unique_sources.add(node["source"])
# If many sources and few nodes per source, could be noise
if total_nodes > 100 and len(unique_sources) > 20:
support_score = 0.6
else:
support_score = 0.3
else:
support_score = 0.5
# Update parent investigation with this evidence
with self._lock:
parent = self.contexts.get(parent_id)
if parent:
# Update parent's inference with likelihood for this hypothesis
self.inference.add_evidence(hypothesis, support_score)
parent["results"]["refutation_evidence"] = parent["results"].get("refutation_evidence", {})
parent["results"]["refutation_evidence"][hypothesis] = support_score
parent["status"] = "updated_by_refutation"
# Store interpretation for this refutation
interpretation = {
"refutation_target": hypothesis,
"support_score": support_score,
"method": "keyword_search"
}
int_id = self.separator.add([], interpretation, "RefutationAI", confidence=support_score)
context["results"] = {"interpretation_id": int_id, "support_score": support_score}
context["status"] = "complete"
def _extract_entities_from_text(self, text: str) -> List[str]:
words = text.split()
entities = []
for w in words:
if w and w[0].isupper() and len(w) > 1 and w not in {"The","A","An","I","We"}:
entities.append(w.strip(".,;:!?"))
return entities
def _process_queue(self):
while self._worker_running:
try:
corr_id = self._task_queue.get(timeout=1)
self._investigate(corr_id)
except queue.Empty:
continue
def get_status(self, corr_id: str) -> Dict:
with self._lock:
return self.contexts.get(corr_id, {"error": "not found"})
def shutdown(self):
self._worker_running = False
self._worker_thread.join(timeout=2)
self._audit_timer.cancel()
# =============================================================================
# PART XX: SELF‑AUDIT MODULE (from v2.4)
# =============================================================================
class SelfAudit:
def __init__(self, detector: HierarchicalDetector, multiplexor: EpistemicMultiplexor,
metadata_registry: ExternalMetadataRegistry):
self.detector = detector
self.multiplexor = multiplexor
self.metadata = metadata_registry
self.audit_log: List[Dict] = []
def run_audit(self) -> Dict:
suggestions = []
for sig in self.detector.signature_counts:
rate = self.detector.get_signature_base_rate(sig)
if rate > 0.5:
suggestions.append({
"signature": sig,
"base_rate": rate,
"suggestion": f"Increase threshold for {sig}, appears too often"
})
if self.multiplexor.measurement_history:
collapse_counts = defaultdict(int)
for desc in self.multiplexor.measurement_history:
collapse_counts[desc] += 1
total_collapses = len(self.multiplexor.measurement_history)
for desc, cnt in collapse_counts.items():
rate = cnt / total_collapses
if "suppression" in desc.lower() and rate > 0.7:
suggestions.append({
"hypothesis": desc,
"collapse_rate": rate,
"suggestion": "Too many suppression conclusions; consider raising positive_evidence_threshold"
})
audit_report = {
"timestamp": datetime.utcnow().isoformat() + "Z",
"suggestions": suggestions,
"signature_counts": dict(self.detector.signature_counts),
"total_investigations": self.detector.total_investigations
}
self.audit_log.append(audit_report)
return audit_report
def apply_suggestions(self):
for suggestion in self.run_audit().get("suggestions", []):
if "increase threshold" in suggestion["suggestion"]:
if suggestion["base_rate"] > 0.6:
self.detector.positive_evidence_min_signatures = max(2, self.detector.positive_evidence_min_signatures + 1)
if "raise positive_evidence_threshold" in suggestion["suggestion"]:
self.multiplexor.positive_evidence_threshold = min(0.8, self.multiplexor.positive_evidence_threshold + 0.05)
# =============================================================================
# PART XXI: API LAYER (Flask)
# =============================================================================
app = Flask(__name__)
controller: Optional[AIController] = 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
corr_id = controller.submit_claim(claim)
return jsonify({"investigation_id": corr_id})
@app.route('/api/v1/investigation/<corr_id>', methods=['GET'])
def get_investigation(corr_id):
status = controller.get_status(corr_id)
return jsonify(status)
@app.route('/api/v1/node/<node_hash>', methods=['GET'])
def get_node(node_hash):
node = controller.ledger.get_node(node_hash)
if node:
return jsonify(node)
return jsonify({"error": "Node not found"}), 404
@app.route('/api/v1/interpretations/<node_hash>', methods=['GET'])
def get_interpretations(node_hash):
ints = controller.separator.get_interpretations(node_hash)
return jsonify([i.__dict__ for i in ints])
@app.route('/api/v1/detect', methods=['GET'])
def run_detection():
result = controller.detector.detect_from_ledger()
return jsonify(result)
@app.route('/api/v1/verify_chain', methods=['GET'])
def verify_chain():
result = controller.ledger.verify_chain()
return jsonify(result)
@app.route('/api/v1/multiplexor/state', methods=['GET'])
def get_multiplexor_state():
if not controller:
return jsonify({"error": "Controller not initialized"}), 500
with controller._lock:
state = {
"hypotheses": [{"description": h.description, "probability": h.probability(), "cost": h.cost, "likelihood": h.likelihood} for h in controller.multiplexor.hypotheses],
"stability_window": controller.multiplexor.stability_window,
"collapse_threshold": controller.multiplexor.collapse_threshold,
"measurement_history": controller.multiplexor.measurement_history
}
return jsonify(state)
@app.route('/api/v1/search', methods=['GET'])
def search_text():
keyword = request.args.get('q', '')
if not keyword:
return jsonify({"error": "Missing query parameter 'q'"}), 400
results = controller.ledger.search_text(keyword)
return jsonify(results)
@app.route('/api/v1/temporal/gaps', methods=['GET'])
def get_gaps():
gaps = controller.temporal.publication_gaps()
return jsonify(gaps)
@app.route('/api/v1/shutdown', methods=['POST'])
def shutdown():
controller.shutdown()
return jsonify({"message": "Shutting down"})
# =============================================================================
# PART XXII: MAIN – Initialization and Startup
# =============================================================================
def main():
crypto = Crypto("./keys")
ledger = Ledger("./ledger.json", crypto)
separator = Separator(ledger, "./separator")
hierarchy = SuppressionHierarchy()
metadata_registry = ExternalMetadataRegistry("./metadata.json")
kg = KnowledgeGraphEngine(ledger)
coherence_checker = NarrativeCoherenceChecker(kg, separator)
detector = HierarchicalDetector(hierarchy, ledger, separator, metadata_registry, coherence_checker)
temporal = TemporalAnalyzer(ledger)
inference = ProbabilisticInference()
multiplexor = EpistemicMultiplexor(stability_window=5, collapse_threshold=0.8,
null_hypothesis_weight=0.6, positive_evidence_threshold=0.3)
context_detector = ContextDetector()
ingestion_ai = IngestionAI(crypto)
symbolism_ai = SymbolismAI()
reasoning_ai = ReasoningAI(inference, None) # will set controller later
archetype_analyzer = ControlArchetypeAnalyzer(hierarchy)
consciousness_mapper = ConsciousnessMapper(separator, symbolism_ai)
paradox_detector = RecursiveParadoxDetector()
immunity_verifier = ImmunityVerifier()
self_audit = SelfAudit(detector, multiplexor, metadata_registry)
global controller
controller = AIController(
ledger=ledger,
separator=separator,
detector=detector,
kg=kg,
temporal=temporal,
inference=inference,
ingestion_ai=ingestion_ai,
symbolism_ai=symbolism_ai,
reasoning_ai=reasoning_ai,
multiplexor=multiplexor,
context_detector=context_detector,
archetype_analyzer=archetype_analyzer,
consciousness_mapper=consciousness_mapper,
paradox_detector=paradox_detector,
immunity_verifier=immunity_verifier,
metadata_registry=metadata_registry,
coherence_checker=coherence_checker,
self_audit=self_audit
)
# Set controller reference in reasoning_ai now that controller exists
reasoning_ai.controller = controller
print("Epistemic Integrity System v2.5 (Active Refutation) starting...")
print("API available at http://localhost:5000")
app.run(debug=True, port=5000)
if __name__ == "__main__":
main()
``` |