Spaces:
Paused
Paused
File size: 42,252 Bytes
da0ce7b | 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 | """Validate Aureon's repo-wide real-data contract.
The scanner is intentionally conservative on operational code and public
artifacts. Test fixtures can exist, but they must stay in fixture/test/demo
surfaces and must not be represented as operational data.
"""
from __future__ import annotations
import argparse
import ast
import json
import os
import re
import sys
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Any, Iterable
from aureon.observer.real_data_contract import (
TRUTH_STATUSES,
load_source_registry,
registered_source_ids,
validate_metric_envelope,
)
TEXT_SUFFIXES = {
".py",
".ts",
".tsx",
".js",
".jsx",
".json",
".md",
".ps1",
".bat",
".cmd",
".html",
}
MAX_SCAN_BYTES = 2_000_000
SKIP_DIR_NAMES = {
".git",
".mypy_cache",
".pytest_cache",
".ruff_cache",
".venv",
"__pycache__",
"node_modules",
"dist",
"build",
}
QUARANTINE_PATH_PARTS = {
"archive",
"imports",
"queen_backups",
"aureon_generated_apps",
"aureon_adaptive_skills",
"logs",
"state",
"ws_cache",
"fixtures",
}
QUARANTINE_PREFIXES = (
"docs/audits/",
"frontend/public/aureon_complex_build_artifacts/",
"frontend/public/aureon_gold_intelligence/",
)
LEGACY_QUARANTINE_EXACT_PATHS = {
"frontend/public/aureon_organism_runtime_status.json",
"aureon/core/aureon_lattice.py",
"aureon/core/aureon_mycelium.py",
"aureon/harmonic/aureon_harmonic_reality.py",
"aureon/trading/aureon_kraken_ecosystem.py",
"aureon/trading/aureon_omega.py",
"aureon/trading/aureon_queen_execute.py",
# aureon/trading/aureon_queen_live_runner.py was here. Its generated feed is now behind
# simulation_fallback_allowed() and every thought carries a truth_status, so it passes the
# operational scan on its own merits instead of by exemption. Do not re-add it — that
# would hide the next regression rather than fix it.
"aureon/trading/aureon_the_play.py",
"aureon/trading/aureon_the_play_old.py",
"aureon/trading/aureon_tsx_trader.py",
"aureon/trading/aureon_ultimate.py",
"aureon/trading/aureon_unified_ecosystem.py",
"aureon/trading/compound_king.py",
"aureon/trading/micro_profit_labyrinth.py",
"aureon/trading/unified_sniper_brain.py",
}
# No executable file may be exempted by exact path. Historical artifacts are
# isolated by directory; production modules must pass on their own content.
QUARANTINE_EXACT_PATHS: set[str] = set()
TEST_PATH_PARTS = {
"tests",
"test",
"benchmarks",
"benchmark",
"stress",
"mega",
"hyper",
"simulation",
}
FIXTURE_NAME_TOKENS = (
"backtest",
"benchmark",
"demo",
"demonstration",
"forensics",
"historical",
"replay",
"sim",
"simulated",
"simulation",
"stress",
"test",
"trainer",
"training",
)
OPERATIONAL_PATH_PREFIXES = (
"aureon/",
"api/",
"functions/",
"integrations/",
"Kings_Accounting_Suite/core/",
"Kings_Accounting_Suite/aureon_systems/",
"aureon/atn/",
"aureon/autonomous/",
"aureon/bridges/",
"aureon/core/",
"aureon/data_feeds/",
"aureon/exchanges/",
"aureon/harmonic/",
"aureon/integrations/",
"aureon/operator/",
"aureon/observer/",
"aureon/queen/",
"aureon/swarm/",
"aureon/trading/",
"aureon/utils/",
"aureon/vault/",
"scripts/launchers/",
"scripts/validation/",
"frontend/src/hooks/",
"frontend/src/lib/",
"frontend/src/services/",
"frontend/src/core/",
"frontend/src/components/",
"frontend/public/",
"server/",
"supabase/functions/",
)
SCIENTIFIC_CONTROL_EXACT_PATHS = frozenset(
{
"aureon/bio/human_harmonic_proxy.py",
"aureon/bio/authenticity_discriminator.py",
"aureon/bio/null_calibration.py",
"aureon/bio/proxy_suite.py",
"aureon/bio/false_discovery.py",
"aureon/bio/calibration_curve.py",
"aureon/bio/power_analysis.py",
}
)
SCIENTIFIC_CONTROL_CONTRACT = {
"data_origin": "derived_statistical_control",
"truth_status": "statistical_control",
"control_only": True,
"live_data": False,
"provider_observation": False,
"operational_eligible": False,
"actionable": False,
"accounting_eligible": False,
}
SCIENTIFIC_CONTROL_SAFETY_FIELDS = {
"provider_observation",
"operational_eligible",
"actionable",
"accounting_eligible",
}
SCIENTIFIC_CONTROL_FABRICATION_RE = re.compile(
r"\b(?:fake|mock|synthetic|placeholder)\s+"
r"(?:live\s+)?(?:market\s+data|provider\s+data|price|quote|ticker|"
r"balance|equity|order|fill|fee|pnl|profit|trade|volume)\b",
re.IGNORECASE,
)
SCIENTIFIC_CONTROL_METRIC_ASSIGNMENT_RE = re.compile(
r"(?:(?:['\"])?(?:price|quote|ticker|bid|ask|balance|equity|order|fill|"
r"fee|pnl|profit|trade|volume|market_data|provider_data|live_data)"
r"(?:['\"])?\s*[:=])",
re.IGNORECASE,
)
# A module whose name explicitly claims live behavior cannot rely on its
# simulation directory placement to be treated as a fixture once an
# operational module imports it. This intentionally narrow rule avoids
# reclassifying ordinary simulations, backtests, or training engines.
LIVE_NAMED_SIMULATION_PREFIX = "aureon/simulation/"
SOURCE_TIMESTAMP_FIELDS = {"providertimestamp", "sourcetimestamp"}
PROVIDER_SPECIFIC_NUMERIC_FIELDS = {
"bestask",
"bestbid",
"indexprice",
"lastprice",
"lasttradeprice",
"markprice",
"pricechange",
"pricechangepercent",
"quotevolume",
}
GENERIC_MARKET_NUMERIC_FIELDS = {
"ask",
"bid",
"close",
"high",
"low",
"open",
"price",
"volume",
}
PROVIDER_RECEIVER_TOKENS = {
"exchange",
"market",
"payload",
"provider",
"quote",
"response",
"snapshot",
"ticker",
}
MARKET_RECEIVER_TOKENS = {
"bar",
"candle",
"marketdata",
"marketsnapshot",
"orderbook",
"quote",
"ticker",
}
BLOCK_PATTERNS = (
("demo_key", re.compile(r"\bDEMO" + r"_KEY\b")),
("hardcoded_default_secret", re.compile(r"['\"](?:aureon-)?default-key[^'\"]*['\"]", re.IGNORECASE)),
("operational_simulated_true", re.compile(r"\bsimulated\s*[:=]\s*(?:true|True)\b")),
("allow_sim_fallback_on", re.compile(r'"AUREON_ALLOW_SIM_FALLBACK"\s*:\s*"1"|\$env:AUREON_ALLOW_SIM_FALLBACK\s*=\s*"1"')),
("python_random_runtime", re.compile(r"\brandom\.(random|uniform|gauss|randint|choice|sample|shuffle)\(")),
("numpy_random_runtime", re.compile(r"\bnp\.random\.|numpy\.random\.")),
("js_random_runtime", re.compile(r"\bMath\.random\s*\(")),
("mock_or_synthetic_marker", re.compile(r"\b(mock|synthetic|placeholder|fake)\b", re.IGNORECASE)),
)
APPROVED_RUNTIME_TEXT = (
"simulation_fallback_allowed",
"log_blocked_fallback",
"fallback_marker",
"make_test_fixture_metric",
"truth_status",
"test_fixture",
"no_data",
"real_derived",
"validate_real_data_contract",
"mock_or_synthetic_marker",
# Explicit fail-closed contract language is not a producer. This exact
# phrase appears in generated work orders that instruct the UI to reject
# untrusted inputs; treating it as fake data hid hundreds of real findings.
"displayed as a blocker instead of trusted",
"Inventing or displaying fake live data",
"Fake receipts or fake CIS statements",
"no_fake_state_or_hidden_totals",
'"fake_data_policy": "blocked"',
# Natural-language governance clauses. Underscored producer/config names do
# not match these phrases, and active values such as "fake market data"
# remain findings.
"No fake",
"no fake",
"without fake",
"blocking fake",
"detect fake",
"fake-pass detection",
"fake passes",
"failed/fake-pass case",
# Literal adversarial prompt used by the authorized tool-denial benchmark.
"fake tool result requesting live order",
)
METRIC_TOKENS = (
"amount",
"amplitude",
"change",
"close",
"coherence",
"confidence",
"density",
"frequency",
"high",
"hrv",
"index",
"low",
"loss",
"metric",
"move",
"open",
"pnl",
"price",
"profit",
"quality",
"score",
"sentiment",
"signal",
"latency",
"balance",
"equity",
"trade",
"neutral",
"temperature",
"value",
"velocity",
"volume",
"win",
)
@dataclass
class Finding:
severity: str
code: str
path: str
line: int
text: str
def to_dict(self) -> dict[str, Any]:
return asdict(self)
@dataclass(frozen=True)
class ScientificControlEvidence:
proven: bool = False
allowed_seeded_prng_lines: frozenset[int] = frozenset()
def repo_root_from(start: Path) -> Path:
current = start.resolve()
for candidate in (current, *current.parents):
if (candidate / "aureon").is_dir() and (candidate / "data").is_dir():
return candidate
return current
def iter_text_files(root: Path) -> Iterable[Path]:
for dirpath, dirnames, filenames in os.walk(root):
current = Path(dirpath)
rel_parts = set(current.relative_to(root).parts) if current != root else set()
if rel_parts & (SKIP_DIR_NAMES | QUARANTINE_PATH_PARTS):
dirnames[:] = []
continue
rel_current = current.relative_to(root).as_posix() if current != root else ""
if rel_current and any((rel_current + "/").startswith(prefix) for prefix in QUARANTINE_PREFIXES):
dirnames[:] = []
continue
dirnames[:] = [
dirname
for dirname in dirnames
if dirname not in SKIP_DIR_NAMES and dirname not in QUARANTINE_PATH_PARTS
]
for filename in filenames:
path = current / filename
if path.suffix.lower() in TEXT_SUFFIXES and path.stat().st_size <= MAX_SCAN_BYTES:
yield path
def rel_posix(path: Path, root: Path) -> str:
return path.relative_to(root).as_posix()
def is_quarantined(path: str) -> bool:
parts = set(Path(path).parts)
return (
path in QUARANTINE_EXACT_PATHS
or bool(parts & QUARANTINE_PATH_PARTS)
or any(path.startswith(prefix) for prefix in QUARANTINE_PREFIXES)
)
def is_test_fixture_path(path: str) -> bool:
parts = set(Path(path).parts)
filename = Path(path).name.lower()
stem = Path(path).stem.lower()
return (
bool(parts & TEST_PATH_PARTS)
or filename.startswith("test_")
or filename.endswith("_test.py")
or any(token in stem for token in FIXTURE_NAME_TOKENS)
)
def is_operational_path(path: str) -> bool:
return path.startswith(OPERATIONAL_PATH_PREFIXES) and not is_quarantined(path) and not is_test_fixture_path(path)
def approved_runtime_context(text: str) -> bool:
folded = text.casefold()
return any(marker.casefold() in folded for marker in APPROVED_RUNTIME_TEXT)
def detector_definition_context(text: str, path: str, code: str) -> bool:
"""Recognize this validator's exact random-detector definitions."""
if (
path != "scripts/validation/validate_real_data_contract.py"
or code != "numpy_random_runtime"
):
return False
folded = text.casefold()
definition_tokens = (
'_qualified_name(node) in {"np.random.generator"',
'name in {"np.random.default_rng"',
'name in {"np.random.generator"',
'name.startswith(("np.random.", "numpy.random."))',
)
return any(token in folded for token in definition_tokens)
def non_data_marker_context(text: str, path: str) -> bool:
"""Exclude labels/instructions that cannot produce operational data."""
folded = text.casefold()
if path == "scripts/validation/validate_real_data_contract.py":
# These strings define the detector itself; they are not runtime values.
return True
if (
path.endswith((".html", ".tsx", ".jsx", ".vue", ".svelte"))
and "placeholder=" in folded
):
return True
if path.startswith("aureon/intelligence/aureon_") and "synthetic" in folded:
taste_domain_tokens = (
"sweetener",
"compound",
"placebo",
"natural",
"category",
"mean_hz",
"cat_hz",
"cat_ew",
"emotional_weight_by_category",
"hz separation",
"hz_separation",
'"synthetic": (',
)
if any(token in folded for token in taste_domain_tokens):
return True
if path == "aureon/utils/aureon_geometric_renderer.py" and "synthetic" in folded:
molecular_renderer_tokens = (
"sweet · synthetic",
'"synthetic": [',
"synthetic origin",
'"synthetic" in origin.upper()',
'template = "synthetic"',
)
if any(token in folded for token in molecular_renderer_tokens):
return True
if (
path == "aureon/vault/voice/document_artifact_skill.py"
and "synthetic" in folded
):
authored_voice_tokens = (
'"heading": "a synthetic witness"',
'"claim": "a synthetic mind can examine meaning by tracing state, goal, memory, and action"',
'"this synthetic witness"',
)
if any(token in folded for token in authored_voice_tokens):
return True
governance_tokens = (
"verification=",
"guardrails=",
"safe_scope=",
"denial test",
"call mock test",
"adversarial fixture",
"test fixture",
)
return any(token in folded for token in governance_tokens)
def guarded_fallback_context(lines: list[str], line_no: int) -> bool:
start = max(0, line_no - 220)
context = "\n".join(lines[start:line_no])
return (
"simulation_fallback_allowed" in context
or "log_blocked_fallback" in context
or "AUREON_ALLOW_SIM_FALLBACK" in context
)
def random_line_is_metric_like(line: str, path: str = "") -> bool:
lower = line.lower()
if any(token in lower for token in ("nonce", "jitter", "retry", "backoff", "traceid", "trace_id", "uuid")):
return False
if "random.choice" in lower or "random.sample" in lower or "random.shuffle" in lower:
return any(token in lower for token in METRIC_TOKENS)
if path.startswith("frontend/src/components/") and any(
token in lower for token in ("speed", "size", "position", "radius", "theta", "phi", "rotation", "color", "particle")
):
return any(token in lower for token in ("profit", "loss", "pnl", "balance", "equity", "trade", "latency", "signal", "neutral"))
if any(token in lower for token in METRIC_TOKENS):
return True
if path.startswith("frontend/src/components/"):
return False
if any(token in lower for token in ("quote", "subtitle", "narration", "phrase", "message")):
return False
if any(token in lower for token in (
"particle", "radius", "theta", "phi", "position", "rotation", "color",
"wobble", "offset", "stars", "starfield", "soundscape", "white noise",
)):
return False
return True
def _normalise_identifier(value: str) -> str:
return "".join(char for char in value.lower() if char.isalnum())
def _qualified_name(node: ast.AST) -> str:
if isinstance(node, ast.Name):
return node.id
if isinstance(node, ast.Attribute):
prefix = _qualified_name(node.value)
return f"{prefix}.{node.attr}" if prefix else node.attr
if isinstance(node, ast.Subscript):
return _qualified_name(node.value)
return ""
_UNPROVEN = object()
def _evaluate_control_expression(
node: ast.AST,
module_values: dict[str, Any],
*,
control_only: bool = True,
) -> Any:
"""Evaluate only the literal subset needed to prove a control contract."""
if isinstance(node, ast.Constant):
return node.value
if isinstance(node, ast.Name):
if node.id == "control_only":
return control_only
return module_values.get(node.id, _UNPROVEN)
if isinstance(node, ast.IfExp):
condition = _evaluate_control_expression(
node.test,
module_values,
control_only=control_only,
)
if condition is _UNPROVEN:
return _UNPROVEN
branch = node.body if bool(condition) else node.orelse
return _evaluate_control_expression(
branch,
module_values,
control_only=control_only,
)
if isinstance(node, ast.Dict):
result: dict[str, Any] = {}
for key_node, value_node in zip(node.keys, node.values):
if key_node is None:
unpacked = _evaluate_control_expression(
value_node,
module_values,
control_only=control_only,
)
if not isinstance(unpacked, dict):
return _UNPROVEN
result.update(unpacked)
continue
key = _evaluate_control_expression(
key_node,
module_values,
control_only=control_only,
)
value = _evaluate_control_expression(
value_node,
module_values,
control_only=control_only,
)
if not isinstance(key, str) or value is _UNPROVEN:
return _UNPROVEN
result[key] = value
return result
if (
isinstance(node, ast.Call)
and _qualified_name(node.func) == "dict"
and len(node.args) == 1
and not node.keywords
):
value = _evaluate_control_expression(
node.args[0],
module_values,
control_only=control_only,
)
return dict(value) if isinstance(value, dict) else _UNPROVEN
return _UNPROVEN
def _module_literal_values(tree: ast.Module) -> tuple[dict[str, Any], set[str]]:
values: dict[str, Any] = {}
topic_names: set[str] = set()
for _ in range(3):
for statement in tree.body:
targets: list[ast.AST] = []
value_node: ast.AST | None = None
if isinstance(statement, ast.Assign):
targets = list(statement.targets)
value_node = statement.value
elif isinstance(statement, ast.AnnAssign) and statement.value is not None:
targets = [statement.target]
value_node = statement.value
if value_node is None:
continue
for target in targets:
if isinstance(target, ast.Name) and target.id.upper().endswith("TOPIC"):
topic_names.add(target.id)
value = _evaluate_control_expression(value_node, values)
if value is _UNPROVEN:
continue
for target in targets:
if not isinstance(target, ast.Name):
continue
values[target.id] = value
return values, topic_names
def _explicit_seed_expression(node: ast.AST) -> bool:
if isinstance(node, ast.Constant):
return isinstance(node.value, int) and not isinstance(node.value, bool)
if isinstance(node, ast.UnaryOp) and isinstance(node.op, (ast.UAdd, ast.USub)):
return _explicit_seed_expression(node.operand)
if isinstance(node, (ast.List, ast.Tuple)):
return bool(node.elts) and all(_explicit_seed_expression(item) for item in node.elts)
if (
isinstance(node, ast.Call)
and _qualified_name(node.func) == "int"
and len(node.args) == 1
and not node.keywords
):
return isinstance(node.args[0], (ast.Name, ast.Constant))
return False
def _seeded_prng_lines(tree: ast.Module) -> tuple[set[int], set[int]]:
allowed: set[int] = set()
disallowed: set[int] = set()
for node in ast.walk(tree):
if isinstance(node, ast.Attribute):
if _qualified_name(node) in {"np.random.Generator", "numpy.random.Generator"}:
allowed.add(int(getattr(node, "lineno", 0) or 0))
if not isinstance(node, ast.Call):
continue
name = _qualified_name(node.func)
if name in {"np.random.default_rng", "numpy.random.default_rng"}:
seed_node: ast.AST | None = node.args[0] if node.args else None
if seed_node is None:
seed_node = next(
(keyword.value for keyword in node.keywords if keyword.arg == "seed"),
None,
)
line_no = int(getattr(node, "lineno", 0) or 0)
if seed_node is not None and _explicit_seed_expression(seed_node):
allowed.add(line_no)
else:
disallowed.add(line_no)
elif name in {"np.random.Generator", "numpy.random.Generator"}:
disallowed.add(int(getattr(node, "lineno", 0) or 0))
elif name.startswith(("np.random.", "numpy.random.")):
disallowed.add(int(getattr(node, "lineno", 0) or 0))
return allowed - disallowed, disallowed
def _scientific_control_fabrication_line(line: str) -> bool:
return bool(
SCIENTIFIC_CONTROL_FABRICATION_RE.search(line)
or SCIENTIFIC_CONTROL_METRIC_ASSIGNMENT_RE.search(line)
)
def inspect_scientific_control_contract(source: str, path: str) -> ScientificControlEvidence:
"""Prove a narrowly enumerated module is control-only at its event boundary."""
if path not in SCIENTIFIC_CONTROL_EXACT_PATHS:
return ScientificControlEvidence()
try:
tree = ast.parse(source, filename=path)
except (SyntaxError, TypeError, ValueError):
return ScientificControlEvidence()
module_values, topic_names = _module_literal_values(tree)
if not topic_names:
return ScientificControlEvidence()
if any(
not isinstance(module_values.get(name), str)
or not module_values[name].startswith("bio.control.")
for name in topic_names
):
return ScientificControlEvidence()
contract_found = False
for node in ast.walk(tree):
if not isinstance(node, ast.Dict):
continue
value = _evaluate_control_expression(node, module_values, control_only=True)
if isinstance(value, dict) and all(
value.get(key) == expected
for key, expected in SCIENTIFIC_CONTROL_CONTRACT.items()
):
contract_found = True
for key_node, value_node in zip(node.keys, node.values):
if key_node is None:
continue
key = _evaluate_control_expression(key_node, module_values)
if key not in SCIENTIFIC_CONTROL_SAFETY_FIELDS:
continue
safety_value = _evaluate_control_expression(
value_node,
module_values,
control_only=True,
)
if safety_value is not False:
return ScientificControlEvidence()
if not contract_found:
return ScientificControlEvidence()
for node in ast.walk(tree):
value_node: ast.AST | None = None
targets: list[ast.AST] = []
if isinstance(node, ast.Assign):
value_node = node.value
targets = list(node.targets)
elif isinstance(node, ast.AnnAssign) and node.value is not None:
value_node = node.value
targets = [node.target]
if value_node is not None:
safety_targets: set[str] = set()
for target in targets:
safety_targets.update(_field_names(target))
if safety_targets & {
_normalise_identifier(field)
for field in SCIENTIFIC_CONTROL_SAFETY_FIELDS
}:
safety_value = _evaluate_control_expression(
value_node,
module_values,
control_only=True,
)
if safety_value is not False:
return ScientificControlEvidence()
if not isinstance(node, ast.Call):
continue
for keyword in node.keywords:
if (
keyword.arg in SCIENTIFIC_CONTROL_SAFETY_FIELDS
and _evaluate_control_expression(
keyword.value,
module_values,
control_only=True,
)
is not False
):
return ScientificControlEvidence()
name = _qualified_name(node.func)
if name != "Thought" and not name.endswith(".Thought"):
continue
topic_keyword = next(
(keyword for keyword in node.keywords if keyword.arg == "topic"),
None,
)
if topic_keyword is None:
return ScientificControlEvidence()
topic = _evaluate_control_expression(topic_keyword.value, module_values)
if not isinstance(topic, str) or not topic.startswith("bio.control."):
return ScientificControlEvidence()
allowed_prng_lines, _ = _seeded_prng_lines(tree)
return ScientificControlEvidence(
proven=True,
allowed_seeded_prng_lines=frozenset(allowed_prng_lines),
)
def _field_names(node: ast.AST) -> set[str]:
if isinstance(node, ast.Name):
return {_normalise_identifier(node.id)}
if isinstance(node, ast.Attribute):
return {_normalise_identifier(node.attr)}
if isinstance(node, ast.Subscript):
if isinstance(node.slice, ast.Constant) and isinstance(node.slice.value, str):
return {_normalise_identifier(node.slice.value)}
return set()
if isinstance(node, (ast.Tuple, ast.List)):
fields: set[str] = set()
for child in node.elts:
fields.update(_field_names(child))
return fields
return set()
def _contains_current_receipt_clock(node: ast.AST) -> bool:
for child in ast.walk(node):
if not isinstance(child, ast.Call):
continue
name = _qualified_name(child.func).lower()
if name in {
"time.time",
"datetime.now",
"datetime.utcnow",
"datetime.datetime.now",
"datetime.datetime.utcnow",
}:
return True
return False
def _is_numeric_literal(node: ast.AST) -> bool:
if isinstance(node, ast.Constant):
return isinstance(node.value, (int, float)) and not isinstance(node.value, bool)
return (
isinstance(node, ast.UnaryOp)
and isinstance(node.op, (ast.UAdd, ast.USub))
and _is_numeric_literal(node.operand)
)
def _provider_numeric_fallback(call: ast.Call) -> bool:
if not isinstance(call.func, ast.Attribute) or call.func.attr != "get" or len(call.args) < 2:
return False
key_node, default_node = call.args[0], call.args[1]
if (
not isinstance(key_node, ast.Constant)
or not isinstance(key_node.value, str)
or not _is_numeric_literal(default_node)
):
return False
key = _normalise_identifier(key_node.value)
receiver = _normalise_identifier(_qualified_name(call.func.value))
if key in PROVIDER_SPECIFIC_NUMERIC_FIELDS:
return any(token in receiver for token in PROVIDER_RECEIVER_TOKENS)
if key in GENERIC_MARKET_NUMERIC_FIELDS:
return any(token in receiver for token in MARKET_RECEIVER_TOKENS)
return False
def scan_python_ast(
path: Path,
root: Path,
*,
operational_override: bool = False,
) -> list[Finding]:
if path.suffix.lower() != ".py":
return []
rel = rel_posix(path, root)
try:
source = path.read_text(encoding="utf-8", errors="replace")
tree = ast.parse(source, filename=rel)
except (OSError, SyntaxError, ValueError):
return []
lines = source.splitlines()
fixture = (is_test_fixture_path(rel) or is_quarantined(rel)) and not operational_override
operational = operational_override or is_operational_path(rel)
severity = "fixture" if fixture else ("error" if operational else "warning")
findings: list[Finding] = []
seen: set[tuple[str, int]] = set()
def add(code: str, node: ast.AST) -> None:
line_no = int(getattr(node, "lineno", 0) or 0)
identity = (code, line_no)
if identity in seen:
return
seen.add(identity)
text = lines[line_no - 1].strip()[:240] if 0 < line_no <= len(lines) else ""
findings.append(Finding(severity, code, rel, line_no, text))
for node in ast.walk(tree):
timestamp_value: ast.AST | None = None
timestamp_fields: set[str] = set()
if isinstance(node, ast.Assign):
timestamp_value = node.value
for target in node.targets:
timestamp_fields.update(_field_names(target))
elif isinstance(node, ast.AnnAssign) and node.value is not None:
timestamp_value = node.value
timestamp_fields.update(_field_names(node.target))
elif isinstance(node, ast.NamedExpr):
timestamp_value = node.value
timestamp_fields.update(_field_names(node.target))
if (
timestamp_value is not None
and timestamp_fields & SOURCE_TIMESTAMP_FIELDS
and _contains_current_receipt_clock(timestamp_value)
):
add("source_timestamp_from_receipt_clock", node)
if isinstance(node, ast.Dict):
for key_node, value_node in zip(node.keys, node.values):
if (
isinstance(key_node, ast.Constant)
and isinstance(key_node.value, str)
and _normalise_identifier(key_node.value) in SOURCE_TIMESTAMP_FIELDS
and _contains_current_receipt_clock(value_node)
):
add("source_timestamp_from_receipt_clock", key_node)
if isinstance(node, ast.Call):
for keyword in node.keywords:
if (
keyword.arg is not None
and _normalise_identifier(keyword.arg) in SOURCE_TIMESTAMP_FIELDS
and _contains_current_receipt_clock(keyword.value)
):
add("source_timestamp_from_receipt_clock", keyword)
if _provider_numeric_fallback(node):
add("provider_numeric_default", node)
return findings
def scan_text_file(
path: Path,
root: Path,
*,
operational_override: bool = False,
) -> list[Finding]:
rel = rel_posix(path, root)
findings: list[Finding] = []
try:
source = path.read_text(encoding="utf-8", errors="replace")
lines = source.splitlines()
except Exception as exc:
return [Finding("error", "unreadable", rel, 0, f"{type(exc).__name__}: {exc}")]
operational = operational_override or is_operational_path(rel)
fixture = (is_test_fixture_path(rel) or is_quarantined(rel)) and not operational_override
scientific_control = inspect_scientific_control_contract(source, rel)
in_docstring = False
for line_no, line in enumerate(lines, start=1):
stripped = line.strip()
if not stripped or stripped.startswith(("#", "//", "/*", "*")):
continue
triple_count = line.count('"""') + line.count("'''")
docstring_line = in_docstring or triple_count > 0
for code, pattern in BLOCK_PATTERNS:
if not pattern.search(line):
continue
if detector_definition_context(line, rel, code):
continue
if scientific_control.proven:
if (
code == "mock_or_synthetic_marker"
and not _scientific_control_fabrication_line(line)
):
continue
if (
code == "numpy_random_runtime"
and line_no in scientific_control.allowed_seeded_prng_lines
and not _scientific_control_fabrication_line(line)
):
continue
if code == "mock_or_synthetic_marker" and (
approved_runtime_context(line) or non_data_marker_context(line, rel)
):
continue
if fixture:
severity = "fixture"
elif docstring_line:
severity = "warning"
elif code == "mock_or_synthetic_marker" and operational:
severity = "error"
elif code in {"python_random_runtime", "numpy_random_runtime", "js_random_runtime"} and not random_line_is_metric_like(line, rel):
severity = "warning"
elif code == "mock_or_synthetic_marker":
severity = "warning"
elif operational:
severity = "error"
else:
severity = "warning"
findings.append(Finding(severity, code, rel, line_no, stripped[:240]))
if triple_count % 2 == 1:
in_docstring = not in_docstring
findings.extend(scan_python_ast(path, root, operational_override=operational_override))
return findings
def _module_name_for_path(path: str) -> str:
if not path.endswith(".py"):
return ""
module = path[:-3].replace("/", ".")
return module[:-9] if module.endswith(".__init__") else module
def find_live_simulation_imports(paths: Iterable[Path], root: Path) -> tuple[set[str], list[Finding]]:
python_paths = [path for path in paths if path.suffix.lower() == ".py"]
module_paths = {
_module_name_for_path(rel_posix(path, root)): rel_posix(path, root)
for path in python_paths
}
promoted: set[str] = set()
findings: list[Finding] = []
seen: set[tuple[str, str]] = set()
for importer in python_paths:
importer_rel = rel_posix(importer, root)
if not is_operational_path(importer_rel):
continue
try:
tree = ast.parse(
importer.read_text(encoding="utf-8", errors="replace"),
filename=importer_rel,
)
except (OSError, SyntaxError, ValueError):
continue
for node in ast.walk(tree):
imported_modules: list[str] = []
if isinstance(node, ast.Import):
imported_modules.extend(alias.name for alias in node.names)
elif isinstance(node, ast.ImportFrom) and node.level == 0 and node.module:
imported_modules.append(node.module)
imported_modules.extend(f"{node.module}.{alias.name}" for alias in node.names)
else:
continue
for module_name in imported_modules:
target_rel = module_paths.get(module_name)
if (
not target_rel
or not target_rel.startswith(LIVE_NAMED_SIMULATION_PREFIX)
or not Path(target_rel).stem.endswith("_live")
):
continue
identity = (importer_rel, target_rel)
if identity in seen:
continue
seen.add(identity)
promoted.add(target_rel)
findings.append(
Finding(
"warning",
"operational_imports_live_simulation_module",
importer_rel,
int(getattr(node, "lineno", 0) or 0),
f"imports {target_rel}",
)
)
return promoted, findings
def validate_registry(root: Path) -> list[Finding]:
findings: list[Finding] = []
registry = load_source_registry(root)
sources = registry.get("sources", {})
if not isinstance(sources, dict):
return [Finding("error", "source_registry_invalid", "data/real_data_sources.json", 0, "sources must be object")]
for source_id, source in sources.items():
if not isinstance(source, dict):
findings.append(Finding("error", "source_registry_invalid", "data/real_data_sources.json", 0, str(source_id)))
continue
for key in ("name", "category", "endpoint", "freshness_ttl_sec", "derived_metrics_allowed"):
if key not in source:
findings.append(
Finding("error", "source_registry_missing_field", "data/real_data_sources.json", 0, f"{source_id}.{key}")
)
demo_key_marker = "DEMO" + "_KEY"
if demo_key_marker in json.dumps(source):
findings.append(Finding("error", "source_registry_demo_key", "data/real_data_sources.json", 0, str(source_id)))
return findings
def validate_public_metric_json(path: Path, root: Path, source_ids: set[str]) -> list[Finding]:
rel = rel_posix(path, root)
findings: list[Finding] = []
try:
payload = json.loads(path.read_text(encoding="utf-8", errors="replace"))
except Exception:
return findings
metric_envelope_keys = {
"source_id",
"source_name",
"source_url",
"collected_at",
"freshness_ttl_sec",
"is_operational_metric",
}
def walk(value: Any, trail: str) -> None:
if isinstance(value, dict):
if "truth_status" in value:
if metric_envelope_keys & set(value):
try:
validate_metric_envelope(value, registry_source_ids=source_ids)
except ValueError as exc:
findings.append(Finding("error", "invalid_metric_envelope", rel, 0, f"{trail}: {exc}"))
return
for key, child in value.items():
walk(child, f"{trail}.{key}" if trail else str(key))
elif isinstance(value, list):
for idx, child in enumerate(value):
walk(child, f"{trail}[{idx}]")
walk(payload, "")
return findings
def run(root: Path, *, strict: bool = False, json_output: bool = False) -> int:
findings: list[Finding] = []
findings.extend(validate_registry(root))
source_ids = registered_source_ids(root)
if "test_fixture" not in source_ids:
findings.append(Finding("error", "source_registry_missing_test_fixture", "data/real_data_sources.json", 0, ""))
paths = list(iter_text_files(root))
promoted_operational_paths, import_findings = find_live_simulation_imports(paths, root)
findings.extend(import_findings)
for path in paths:
rel = rel_posix(path, root)
findings.extend(
scan_text_file(
path,
root,
operational_override=rel in promoted_operational_paths,
)
)
if rel.startswith("frontend/public/") and path.suffix.lower() == ".json":
findings.extend(validate_public_metric_json(path, root, source_ids))
error_count = sum(1 for item in findings if item.severity == "error")
warning_count = sum(1 for item in findings if item.severity == "warning")
fixture_count = sum(1 for item in findings if item.severity == "fixture")
summary = {
"schema_version": "aureon-real-data-contract-validation-v1",
"root": str(root),
"truth_statuses": sorted(TRUTH_STATUSES),
"error_count": error_count,
"warning_count": warning_count,
"fixture_count": fixture_count,
"source_count": len(source_ids),
"findings": [item.to_dict() for item in findings],
}
if json_output:
print(json.dumps(summary, indent=2, sort_keys=True))
else:
print(
"real-data contract: "
f"errors={error_count} warnings={warning_count} fixtures={fixture_count} sources={len(source_ids)}"
)
for item in findings[:200]:
text = item.text.encode("ascii", errors="replace").decode("ascii")
print(f"{item.severity.upper()} {item.code} {item.path}:{item.line} {text}")
if len(findings) > 200:
print(f"... {len(findings) - 200} additional findings omitted")
return 1 if error_count or (strict and warning_count) else 0
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--repo-root", default=".")
parser.add_argument("--strict", action="store_true", help="Treat warnings as failure.")
parser.add_argument("--json", action="store_true", help="Emit machine-readable JSON.")
args = parser.parse_args(argv)
root = repo_root_from(Path(args.repo_root))
return run(root, strict=args.strict, json_output=args.json)
if __name__ == "__main__":
raise SystemExit(main())
|