Datasets:
Tasks:
Tabular Classification
Formats:
parquet
Languages:
English
Size:
< 1K
Tags:
economics
quantitative-finance
causal-inference
macroeconomics
housing-economics
market-microstructure
License:
File size: 79,687 Bytes
ebcde1f | 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 | """Command-line interface for research data, reproduction, and reporting."""
from __future__ import annotations
import argparse
import asyncio
import base64
import hashlib
import json
import os
import sys
import tempfile
import time
from collections.abc import AsyncIterator, Callable, Iterator, Mapping, Sequence
from contextlib import suppress
from dataclasses import asdict, dataclass
from decimal import Decimal
from pathlib import Path
from typing import Any, Literal, cast
import pyarrow as pa # type: ignore[import-untyped]
from microstructure import __version__
from microstructure.config import ProjectConfig, datetime_to_ns, load_config
from microstructure.data.binance import (
BinanceLiveDepthCollector,
BinancePublicClient,
CapturedDepth,
RawDepthFrame,
)
from microstructure.data.book import BookSnapshot, IncrementalBookReconstructor
from microstructure.data.quality import IncrementalQualityValidator, ValidationReport
from microstructure.data.schemas import get_schema, table_from_records
from microstructure.data.storage import write_capture_parquet, write_source_manifest
from microstructure.data.synthetic import generate_synthetic_market
from microstructure.ingestion import (
IngestionResult,
ingest_from_config,
validate_configured_input,
)
from microstructure.m8_acquisition import (
M8AcquisitionFailureResult,
M8AcquisitionResult,
acquire_m8_archives,
)
from microstructure.m8_config import load_m8_config
from microstructure.m8_l2_analysis_config import (
M8L2AnalysisConfig,
load_m8_l2_analysis_config,
)
from microstructure.m8_l2_binance import BinanceM8L2Capture
from microstructure.m8_l2_capture import (
M8L2SessionBundle,
capture_m8_l2_session,
verify_m8_l2_session_bundle,
)
from microstructure.m8_l2_config import M8L2StudyConfig, load_m8_l2_config
from microstructure.m8_l2_development import (
L2DevelopmentInputVerifier,
L2DevelopmentLockResult,
lock_m8_l2_development,
verify_m8_l2_development_lock,
)
from microstructure.m8_l2_inputs import (
L2CampaignRuntimeIdentity,
L2SessionFileAuthority,
verify_m8_l2_development_input,
)
from microstructure.m8_l2_pipeline import (
L2StudySessionAuthority,
M8L2StudyRunResult,
load_m8_l2_report_data,
reproduce_m8_l2_study,
verify_m8_l2_study_run,
)
from microstructure.m8_pipeline import M8RunResult, reproduce_m8, verify_m8_result
from microstructure.pipeline import reproduce
from microstructure.provenance import read_json, sha256_file, utc_now_iso, write_json
from microstructure.reporting import (
canonical_report_data_sha256,
load_run_bundle,
verify_checksums,
write_l2_report_set,
write_report_set,
)
def _json_default(value: object) -> object:
if isinstance(value, Path):
return str(value)
if isinstance(value, Decimal):
return str(value)
raise TypeError(f"cannot serialize {type(value).__name__}")
def _print_json(payload: object) -> None:
print(json.dumps(payload, indent=2, sort_keys=True, default=_json_default))
def _ingestion_payload(result: IngestionResult) -> dict[str, Any]:
return {
"mode": result.mode,
"evidence_tier": result.evidence_tier,
"output_root": result.output_root,
"ingestion_manifest": result.ingestion_manifest_path,
"ingestion_manifest_sha256": result.ingestion_manifest_sha256,
"rows": result.rows,
"datasets": [
{
"schema": dataset.schema_name,
"rows": dataset.rows,
"manifest": dataset.storage.manifest_path,
"manifest_sha256": dataset.storage.manifest_sha256,
"quality_errors": dataset.validation.error_count,
"quality_warnings": dataset.validation.warning_count,
}
for dataset in result.datasets
],
"raw_artifacts": len(result.raw_artifacts),
"symbols": [
{
"symbol": item.symbol,
"rows": item.rows,
"complete_range": item.complete_range,
"tick_size": item.metadata.tick_size,
"lot_size": item.metadata.lot_size,
}
for item in result.symbols
],
"quality": {
"passed": result.validation.passed,
"rows_checked": result.validation.rows_checked,
"errors": result.validation.error_count,
"warnings": result.validation.warning_count,
},
}
def _cmd_ingest(args: argparse.Namespace) -> int:
config = load_config(args.config)
output_root = (
Path(args.output_root).resolve() if args.output_root else config.data.partition_root.parent
)
result = ingest_from_config(config, output_root)
_print_json(_ingestion_payload(result))
return 0
def _m8_acquisition_payload(result: M8AcquisitionResult) -> dict[str, Any]:
return {
"status": "acquired",
"scope": "raw_only",
"output_root": result.output_root,
"raw_manifest": result.manifest_path,
"raw_manifest_sha256": result.manifest_sha256,
"metadata_responses": result.metadata_count,
"archives": result.archive_count,
"total_raw_evidence_bytes": result.total_raw_evidence_bytes,
"csv_members_opened": False,
"economic_fields_inspected": False,
}
def _m8_acquisition_failure_payload(result: M8AcquisitionFailureResult) -> dict[str, Any]:
failed_date = result.failed_date
return {
"status": "INSUFFICIENT_DATA",
"scope": "raw_only",
"output_root": result.output_root,
"attempt_dir": result.attempt_dir,
"failure_manifest": result.attempt_manifest_path,
"failure_manifest_sha256": result.attempt_manifest_sha256,
"checksums": result.checksums_path,
"checksums_sha256": result.checksums_sha256,
"terminal_marker": result.terminal_path,
"reason_code": result.reason_code,
"diagnostic": result.diagnostic,
"failed_symbol": result.failed_symbol,
"failed_date": None if failed_date is None else failed_date.isoformat(),
"failed_role": result.failed_role,
"completed_steps": result.completed_count,
"remaining_steps": result.remaining_count,
"retained_inventory_sha256": result.retained_inventory_sha256,
"retained_artifacts": result.retained_artifact_count,
"total_raw_evidence_bytes": result.total_raw_evidence_bytes,
"csv_members_opened": False,
"economic_fields_inspected": False,
}
def _cmd_acquire_m8(args: argparse.Namespace) -> int:
config = load_m8_config(args.config)
result = acquire_m8_archives(config, Path(args.output_root).resolve())
if isinstance(result, M8AcquisitionFailureResult):
_print_json(_m8_acquisition_failure_payload(result))
return 1
_print_json(_m8_acquisition_payload(result))
return 0
def _synthetic_tables(config: ProjectConfig) -> dict[str, Any]:
events = config.data.events_per_symbol
if events is None:
raise ValueError("synthetic validation requires data.events_per_symbol")
generated = generate_synthetic_market(
symbols=config.data.symbols,
events_per_symbol=events,
start_ts_ns=datetime_to_ns(config.data.start),
seed=config.run.seed,
)
return {"trades": generated.trades, "book_observations": generated.book_observations}
def _cmd_validate(args: argparse.Namespace) -> int:
config = load_config(args.config)
tables = _synthetic_tables(config) if config.data.mode == "synthetic" else None
summary = validate_configured_input(config, tables=tables)
_print_json(
{
"passed": summary.passed,
"rows_checked": summary.rows_checked,
"errors": summary.error_count,
"warnings": summary.warning_count,
"reports": [
{
"dataset": report.dataset,
"rows_checked": report.rows_checked,
"errors": report.error_count,
"warnings": report.warning_count,
}
for report in summary.reports
],
"mutation_policy": "validation did not repair or replace observations",
}
)
return 0 if summary.passed else 1
def _cmd_reproduce(args: argparse.Namespace) -> int:
config = load_config(args.config)
output = reproduce(
config,
Path(args.run_dir),
ingestion_manifest_path=args.ingestion_manifest,
ingestion_manifest_sha256=args.ingestion_manifest_sha256,
)
bundle = load_run_bundle(output)
_print_json(
{
"run_dir": output,
"run_id": bundle.run_id,
"evidence_tier": bundle.evidence_tier,
"observed_start_utc": bundle.observed_start_utc,
"observed_end_utc": bundle.observed_end_utc,
"status": "complete",
}
)
return 0
def _cmd_reproduce_m8(args: argparse.Namespace) -> int:
config = load_m8_config(args.config)
result = reproduce_m8(
config,
Path(args.run_dir),
raw_manifest_path=Path(args.raw_manifest),
raw_manifest_sha256=str(args.raw_manifest_sha256),
)
if result.status == "INSUFFICIENT_DATA":
_print_json(_m8_run_result_payload(result))
return 1
bundle = load_run_bundle(result.path)
_print_json(
{
"run_dir": result.path,
"run_id": bundle.run_id,
"evidence_tier": bundle.evidence_tier,
"observed_start_utc": bundle.observed_start_utc,
"observed_end_utc": bundle.observed_end_utc,
"status": result.status,
"raw_manifest_sha256": result.raw_manifest_sha256,
"normalized_manifest_sha256": result.normalized_manifest_sha256,
}
)
return 0
def _m8_run_result_payload(result: M8RunResult) -> dict[str, Any]:
return {
"run_dir": result.path,
"status": result.status,
"raw_manifest_sha256": result.raw_manifest_sha256,
"normalized_manifest_sha256": result.normalized_manifest_sha256,
}
def _cmd_verify_m8(args: argparse.Namespace) -> int:
config = load_m8_config(args.config)
result = verify_m8_result(
args.run_dir,
config,
raw_manifest_path=args.raw_manifest,
raw_manifest_sha256=args.raw_manifest_sha256,
)
payload = _m8_run_result_payload(result)
payload.update(
{
"integrity": "verified",
"protected_files": verify_checksums(result.path),
}
)
_print_json(payload)
return 0
def _external_report_dir(run_root: Path, requested: Path | None) -> Path:
frozen_root = run_root.resolve()
output = (
requested.resolve()
if requested is not None
else frozen_root.with_name(f"{frozen_root.name}-reports")
)
if output == frozen_root or frozen_root in output.parents:
raise ValueError("report output directory must be outside the immutable run bundle")
return output
def _atomic_text(path: Path, content: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
descriptor, temporary_name = tempfile.mkstemp(
dir=path.parent,
prefix=f".{path.name}.",
suffix=".tmp",
text=True,
)
temporary = Path(temporary_name)
try:
with os.fdopen(descriptor, "w", encoding="utf-8", newline="\n") as handle:
handle.write(content)
handle.flush()
os.fsync(handle.fileno())
os.replace(temporary, path)
directory = os.open(path.parent, os.O_RDONLY)
try:
os.fsync(directory)
finally:
os.close(directory)
except BaseException:
temporary.unlink(missing_ok=True)
raise
def _report_mapping(value: object, label: str) -> Mapping[str, Any]:
if not isinstance(value, Mapping):
raise ValueError(f"verified M8 {label} is not a JSON object")
return cast(Mapping[str, Any], value)
def _report_value(value: object) -> str:
if isinstance(value, (dict, list, tuple)):
rendered = json.dumps(value, sort_keys=True, separators=(",", ":"))
elif value is None:
rendered = "null"
elif value is True:
rendered = "true"
elif value is False:
rendered = "false"
else:
rendered = str(value)
return " ".join(rendered.replace("`", "'").split())
def _m8_failure_period(failure: Mapping[str, Any]) -> tuple[str, str]:
dates: list[str] = []
for key in ("completed_normalizations", "stopped_before"):
rows = failure.get(key)
if isinstance(rows, list):
dates.extend(
str(row["date"])
for row in rows
if isinstance(row, Mapping)
and isinstance(row.get("date"), str)
and len(str(row["date"])) == 10
)
failed_date = failure.get("failed_date")
if isinstance(failed_date, str) and len(failed_date) == 10:
dates.append(failed_date)
if not dates:
raise ValueError("verified M8 failure does not declare its study period")
return min(dates), max(dates)
def _render_m8_insufficient_report(run_root: Path) -> str:
failure = _report_mapping(read_json(run_root / "failure.json"), "failure record")
provenance = _report_mapping(read_json(run_root / "provenance.json"), "provenance")
manifest = _report_mapping(read_json(run_root / "run_manifest.json"), "run manifest")
research = _report_mapping(manifest.get("research"), "run research section")
execution = _report_mapping(
manifest.get("execution_assumptions"),
"execution-assumptions section",
)
git = _report_mapping(provenance.get("git"), "Git identity")
period_start, period_end = _m8_failure_period(failure)
completed_symbols = failure.get("selection_completed_symbols")
evaluated_symbols = failure.get("endpoint_evaluation_completed_symbols")
heldout_member = failure.get("held_out_member_opened", "not separately recorded")
return f"""# M8 study result: INSUFFICIENT_DATA
> VERIFIED TERMINAL TRADE-ONLY RESEARCH RESULT — NO DATE REPLACEMENT, LIVE TRADING, OR PERFORMANCE CLAIM
## Frozen scope and failure
- Observed/attempted archive date span: `{period_start}` through `{period_end}` (UTC daily archives)
- Failed coordinate: symbol=`{_report_value(failure.get("failed_symbol"))}`, date=`{_report_value(failure.get("failed_date"))}`, role=`{_report_value(failure.get("failed_role"))}`
- Failure stage: `{_report_value(failure.get("failure_stage"))}`
- Reason code: `{_report_value(failure.get("reason_code"))}`
- Diagnostic: `{_report_value(failure.get("reason"))}`
- Replacement date selected: `{_report_value(failure.get("replacement_date_selected"))}`; reselection performed: `{_report_value(failure.get("reselection_performed"))}`
## Immutable authority
- Config semantic SHA-256: `{_report_value(failure.get("config_sha256"))}`
- Config source SHA-256: `{_report_value(failure.get("config_source_sha256"))}`
- Raw acquisition manifest SHA-256: `{_report_value(failure.get("raw_acquisition_manifest_sha256"))}`
- Bundled raw acquisition manifest SHA-256: `{_report_value(failure.get("bundled_raw_acquisition_manifest_sha256"))}`
- Protocol SHA-256: `{_report_value(failure.get("protocol_sha256"))}`
- Git commit: `{_report_value(git.get("commit"))}`
- Git dirty: `{_report_value(git.get("dirty"))}`
- Source-tree SHA-256: `{_report_value(git.get("source_tree_sha256"))}`
## Terminal analysis states
- Candidate selection started: `{_report_value(failure.get("selection_started"))}`; completed symbols: `{_report_value(completed_symbols)}`
- Aggregate analysis lock committed: `{_report_value(failure.get("aggregate_lock_committed"))}`
- Held-out member opened: `{_report_value(heldout_member)}`
- Endpoint evaluation started: `{_report_value(failure.get("endpoint_evaluation_started"))}`; completed: `{_report_value(failure.get("endpoint_evaluation_completed"))}`; completed symbols: `{_report_value(evaluated_symbols)}`
- Predictions published: `{_report_value(failure.get("predictions_published"))}`; endpoint artifacts published: `{_report_value(failure.get("endpoint_artifacts_published"))}`
- Research endpoint status: `{_report_value(research.get("endpoint_status"))}`
- Execution status: `{_report_value(execution.get("status"))}`; fills calculated: `{_report_value(execution.get("fills_calculated"))}`; P&L calculated: `{_report_value(execution.get("pnl_calculated"))}`; capacity calculated: `{_report_value(execution.get("capacity_calculated"))}`
This terminal result preserves the frozen calendar and failure evidence. It authorizes no execution, profitability, capacity, statistical-significance, or persistent-alpha claim.
"""
def _cmd_report_m8(args: argparse.Namespace) -> int:
config = load_m8_config(args.config)
result = verify_m8_result(
args.run_dir,
config,
raw_manifest_path=args.raw_manifest,
raw_manifest_sha256=args.raw_manifest_sha256,
)
output = _external_report_dir(result.path, args.output_dir)
if result.status == "COMPLETE":
bundle = load_run_bundle(result.path)
paths = write_report_set(bundle, output)
_print_json(
{
**_m8_run_result_payload(result),
"output_dir": output,
"reports_regenerated": True,
**asdict(paths),
}
)
else:
rendered = _render_m8_insufficient_report(result.path)
confirmed = verify_m8_result(
args.run_dir,
config,
raw_manifest_path=args.raw_manifest,
raw_manifest_sha256=args.raw_manifest_sha256,
)
if (
confirmed.status != "INSUFFICIENT_DATA"
or confirmed.path.resolve() != result.path.resolve()
or confirmed.raw_manifest_sha256 != result.raw_manifest_sha256
or confirmed.normalized_manifest_sha256 is not None
):
raise ValueError("M8 failure authority changed while rendering its report")
failure_report = output / "insufficient_data.md"
_atomic_text(failure_report, rendered)
_print_json(
{
**_m8_run_result_payload(result),
"output_dir": output,
"report": failure_report,
"report_sha256": sha256_file(failure_report),
"reports_regenerated": True,
"source_bundle_modified": False,
}
)
return 0
def _lowercase_sha256(value: str) -> str:
if len(value) != 64 or any(character not in "0123456789abcdef" for character in value):
raise argparse.ArgumentTypeError("must be a 64-character lowercase SHA-256 digest")
return value
def _cmd_verify(args: argparse.Namespace) -> int:
bundle = load_run_bundle(args.run_dir)
protected = verify_checksums(args.run_dir)
_print_json(
{
"run_dir": bundle.root,
"run_id": bundle.run_id,
"evidence_tier": bundle.evidence_tier,
"protected_files": protected,
"integrity": "verified",
}
)
return 0
def _cmd_report(args: argparse.Namespace) -> int:
bundle = load_run_bundle(args.run_dir)
output = (
Path(args.output_dir).resolve()
if args.output_dir
else bundle.root.with_name(f"{bundle.root.name}-reports")
)
paths = write_report_set(bundle, output)
_print_json({"run_id": bundle.run_id, "output_dir": output, **asdict(paths)})
return 0
_LIVE_BATCH_ROWS = 1_024
_MAX_LIVE_RAW_MESSAGE_BYTES = 1 * 1024 * 1024
_LIVE_BATCH_ESTIMATED_BYTES = 16 * 1024 * 1024
_VARIABLE_RECORD_OVERHEAD_FACTOR = 8
@dataclass(frozen=True, slots=True)
class DepthCaptureResult:
symbol: str
messages: int
continuity_epochs: int
reconstruction_status: Literal["LIVE", "GAPPED", "INVALID"]
book_observations: int
sequence_gaps: int
stale_events: int
excluded_messages: int
final_update_id: int
quality_errors: int
quality_warnings: int
raw_path: Path
raw_manifest_path: Path
raw_manifest_sha256: str
summary_path: Path
completion_reason: str
requested_duration_seconds: float | None
elapsed_monotonic_seconds: float
receipt_coverage_seconds: float
max_continuity_epoch_seconds: float
@dataclass(slots=True)
class _DepthCaptureStop:
reason: str = "not_started"
elapsed_monotonic_seconds: float = 0.0
@dataclass(slots=True)
class _DepthEpochCoverage:
continuity_id: str
snapshot_id: str
first_received_ns: int
last_received_ns: int
messages: int = 0
book_observations: int = 0
excluded_messages: int = 0
sequence_gaps: int = 0
reconstruction_status: Literal["LIVE", "GAPPED", "INVALID"] = "LIVE"
final_update_id: int = 0
@property
def duration_seconds(self) -> float:
return max(0.0, (self.last_received_ns - self.first_received_ns) / 1_000_000_000.0)
def to_dict(self) -> dict[str, object]:
return {
"continuity_id": self.continuity_id,
"snapshot_id": self.snapshot_id,
"first_received_ns": self.first_received_ns,
"last_received_ns": self.last_received_ns,
"duration_seconds": self.duration_seconds,
"messages": self.messages,
"book_observations": self.book_observations,
"excluded_messages": self.excluded_messages,
"sequence_gaps": self.sequence_gaps,
"reconstruction_status": self.reconstruction_status,
"final_update_id": self.final_update_id,
}
async def _bounded_depth_items(
collector: BinanceLiveDepthCollector,
*,
max_messages: int,
duration_seconds: float | None,
stop: _DepthCaptureStop,
) -> AsyncIterator[CapturedDepth]:
"""Yield a live stream until its safety cap or a graceful duration deadline."""
started = time.monotonic()
yielded = 0
iterator = collector.stream(max_messages=max_messages).__aiter__()
try:
if duration_seconds is None:
async for item in iterator:
yielded += 1
yield item
stop.reason = "message_limit" if yielded == max_messages else "stream_ended_early"
return
deadline = started + duration_seconds
while True:
remaining = deadline - time.monotonic()
if remaining <= 0:
stop.reason = "duration_elapsed"
return
try:
item = await asyncio.wait_for(anext(iterator), timeout=remaining)
except TimeoutError:
stop.reason = "duration_elapsed"
return
except StopAsyncIteration:
stop.reason = (
"message_safety_ceiling" if yielded == max_messages else "stream_ended_early"
)
return
yielded += 1
yield item
finally:
stop.elapsed_monotonic_seconds = max(0.0, time.monotonic() - started)
with suppress(BaseException):
closer = getattr(iterator, "aclose", None)
if callable(closer):
await closer()
@dataclass(frozen=True, slots=True)
class _PublishedRawCapture:
path: Path
sha256: str
manifest_path: Path
manifest_sha256: str
class _ArrowBatchSpool:
"""Bounded record buffer backed by a temporary Arrow IPC stream."""
def __init__(
self,
*,
root: Path,
schema_name: str,
batch_rows: int,
max_buffer_bytes: int,
on_batch: Callable[[pa.RecordBatch], None] | None = None,
) -> None:
if batch_rows < 1:
raise ValueError("batch_rows must be positive")
if max_buffer_bytes < 1:
raise ValueError("max_buffer_bytes must be positive")
self.schema_name = schema_name
self.batch_rows = batch_rows
self.max_buffer_bytes = max_buffer_bytes
self.path = root / f"{schema_name}.arrow"
self._handle = self.path.open("wb")
self._writer = pa.ipc.new_stream(self._handle, get_schema(schema_name))
self._on_batch = on_batch
self._records: list[Mapping[str, object]] = []
self.rows = 0
self.max_buffered_rows = 0
self.max_buffered_estimated_bytes = 0
self._buffered_estimated_bytes = 0
self._closed = False
def append(self, record: Mapping[str, object], *, estimated_bytes: int) -> None:
if self._closed:
raise RuntimeError("cannot append to a closed Arrow spool")
if estimated_bytes < 1:
raise ValueError("estimated_bytes must be positive")
if estimated_bytes > self.max_buffer_bytes:
raise RuntimeError(f"one {self.schema_name} record exceeds the bounded batch estimate")
if (
self._records
and self._buffered_estimated_bytes + estimated_bytes > self.max_buffer_bytes
):
self._flush()
self._records.append(record)
self._buffered_estimated_bytes += estimated_bytes
self.max_buffered_rows = max(self.max_buffered_rows, len(self._records))
self.max_buffered_estimated_bytes = max(
self.max_buffered_estimated_bytes,
self._buffered_estimated_bytes,
)
if len(self._records) >= self.batch_rows:
self._flush()
def _flush(self) -> None:
if not self._records:
return
table = table_from_records(self.schema_name, self._records)
batches = table.to_batches(max_chunksize=self.batch_rows)
if len(batches) != 1 or batches[0].num_rows > self.batch_rows:
raise RuntimeError(f"failed to construct one bounded {self.schema_name} batch")
batch = batches[0]
if self._on_batch is not None:
self._on_batch(batch)
self._writer.write_batch(batch)
self.rows += batch.num_rows
self._records.clear()
self._buffered_estimated_bytes = 0
def close(self) -> None:
if self._closed:
return
self._flush()
self._writer.close()
if not self._handle.closed:
self._handle.flush()
os.fsync(self._handle.fileno())
self._handle.close()
self._closed = True
def iter_batches(self) -> Iterator[pa.RecordBatch]:
if not self._closed:
raise RuntimeError("Arrow spool must be closed before it can be read")
with self.path.open("rb") as handle:
reader = pa.ipc.open_stream(handle)
for batch in reader:
if batch.num_rows > self.batch_rows:
raise RuntimeError(
f"spooled {self.schema_name} batch exceeds {self.batch_rows} rows"
)
yield batch
class _RawMessageSpool:
"""Incrementally persist an exact, typed live-capture journal."""
def __init__(self, *, root: Path, symbol: str, source_uri: str) -> None:
self.root = root
self.symbol = symbol
self.source_uri = source_uri
self.directory = root / "raw" / "binance_spot" / "depth_stream" / symbol
self.directory.mkdir(parents=True, exist_ok=True)
descriptor, temporary_name = tempfile.mkstemp(
dir=self.directory,
prefix=".capture-",
suffix=".ndjson.tmp",
text=True,
)
self._temporary_path = Path(temporary_name)
self._handle = os.fdopen(descriptor, "w", encoding="utf-8", newline="\n")
self.messages = 0
self.snapshot_anchors = 0
self.first_received_ns: int | None = None
self.last_received_ns: int | None = None
self._closed = False
self.published_path: Path | None = None
self._last_frame_identity: tuple[str, int, int, str] | None = None
@property
def evidence_path(self) -> Path:
"""Return the durable path, or the fsynced temporary path after publish failure."""
return self.published_path or self._temporary_path
def _write_event(self, event: Mapping[str, object]) -> None:
if self._closed:
raise RuntimeError("cannot append to a closed raw capture")
json.dump(event, self._handle, sort_keys=True, separators=(",", ":"))
self._handle.write("\n")
def append_frame(self, frame: RawDepthFrame) -> int:
"""Journal one exact frame before any UTF-8 or JSON parsing."""
payload_size = len(frame.payload)
payload_sha256 = hashlib.sha256(frame.payload).hexdigest()
self._write_event(
{
"capture_seq": frame.capture_seq,
"continuity_id": frame.continuity_id,
"event_kind": "websocket_frame",
"payload_base64": base64.b64encode(frame.payload).decode("ascii"),
"payload_bytes": payload_size,
"payload_sha256": payload_sha256,
"received_ts_ns": frame.received_ts_ns,
"websocket_message_type": "text" if frame.was_text else "binary",
}
)
self.messages += 1
if self.first_received_ns is None:
self.first_received_ns = frame.received_ts_ns
self.last_received_ns = frame.received_ts_ns
self._last_frame_identity = (
frame.continuity_id,
frame.capture_seq,
frame.received_ts_ns,
payload_sha256,
)
# The oversize frame is intentionally journaled before capture fails.
if payload_size > _MAX_LIVE_RAW_MESSAGE_BYTES:
raise RuntimeError(
f"live depth message exceeds {_MAX_LIVE_RAW_MESSAGE_BYTES} raw bytes"
)
return payload_size
def append_captured(self, item: CapturedDepth) -> int:
"""Verify callback lineage, with a fallback for injected legacy collectors."""
received_ts_ns = item.delta.received_ts_ns
capture_seq = item.delta.capture_seq
if received_ts_ns is None or capture_seq is None:
raise RuntimeError("captured depth messages require receipt time and capture sequence")
payload = item.raw_payload.encode("utf-8")
payload_sha256 = hashlib.sha256(payload).hexdigest()
identity = (
item.delta.continuity_id,
capture_seq,
received_ts_ns,
payload_sha256,
)
if self._last_frame_identity != identity:
self.append_frame(
RawDepthFrame(
payload=payload,
was_text=True,
received_ts_ns=received_ts_ns,
capture_seq=capture_seq,
continuity_id=item.delta.continuity_id,
)
)
if item.delta.source_artifact_id != payload_sha256:
raise RuntimeError("normalized depth delta is not bound to its raw frame SHA-256")
return len(payload)
def append_snapshot(self, snapshot: BookSnapshot) -> None:
"""Bind one REST snapshot raw artifact into the capture journal."""
raw_path = (
self.root
/ "raw"
/ "binance_spot"
/ "depth_snapshots"
/ snapshot.symbol
/ f"{snapshot.source_artifact_id}.json"
)
if not raw_path.is_file() or sha256_file(raw_path) != snapshot.source_artifact_id:
raise RuntimeError("book snapshot is not bound to its preserved raw response")
manifest_path: Path | None = None
for candidate in raw_path.parent.glob(f"{raw_path.name}.manifest-*.json"):
payload = read_json(candidate)
if (
isinstance(payload, dict)
and payload.get("path") == raw_path.name
and isinstance(payload.get("checksum"), dict)
and payload["checksum"].get("value") == snapshot.source_artifact_id
and (manifest_path is None or candidate.name > manifest_path.name)
):
manifest_path = candidate
if manifest_path is None:
raise RuntimeError("book snapshot raw response has no valid source manifest")
self._write_event(
{
"continuity_id": snapshot.continuity_id,
"event_kind": "rest_snapshot_anchor",
"last_update_id": snapshot.last_update_id,
"raw_manifest_path": str(manifest_path),
"raw_manifest_sha256": sha256_file(manifest_path),
"raw_path": str(raw_path),
"raw_sha256": snapshot.source_artifact_id,
"received_ts_ns": snapshot.received_ts_ns,
"snapshot_id": snapshot.snapshot_id,
}
)
self.snapshot_anchors += 1
def _close(self) -> None:
if self._closed:
return
self._handle.flush()
os.fsync(self._handle.fileno())
self._handle.close()
self._closed = True
def publish(
self,
*,
status: str,
error: BaseException | None = None,
) -> _PublishedRawCapture:
self._close()
if self.published_path is not None:
destination = self.published_path
digest = sha256_file(destination)
else:
digest = sha256_file(self._temporary_path)
prefix = "capture" if status == "raw_capture_complete" else "capture-failed"
destination = self.directory / f"{prefix}-{digest}.ndjson"
if destination.exists():
if sha256_file(destination) != digest:
raise RuntimeError(f"raw depth capture collision at {destination}")
self._temporary_path.unlink(missing_ok=True)
else:
os.replace(self._temporary_path, destination)
self.published_path = destination
response_headers = {
"x-local-capture-status": status,
"x-local-journal-format": "typed-base64-frames-v1",
"x-local-message-count": str(self.messages),
"x-local-snapshot-anchor-count": str(self.snapshot_anchors),
}
if error is not None:
response_headers["x-local-error-type"] = type(error).__name__
response_headers["x-local-error"] = str(error)[:512]
manifest_path, manifest_sha = write_source_manifest(
destination,
source="binance_spot_public_live_capture_journal",
source_uri=self.source_uri,
downloaded_at_utc=utc_now_iso(),
requested_start_ns=self.first_received_ns,
requested_end_ns=(
self.last_received_ns + 1 if self.last_received_ns is not None else None
),
response_headers=response_headers,
)
return _PublishedRawCapture(
path=destination,
sha256=digest,
manifest_path=manifest_path,
manifest_sha256=manifest_sha,
)
def close_without_deleting(self) -> None:
self._close()
def _status_max(
current: Literal["LIVE", "GAPPED", "INVALID"],
observed: Literal["LIVE", "GAPPED", "INVALID"],
) -> Literal["LIVE", "GAPPED", "INVALID"]:
rank = {"LIVE": 0, "GAPPED": 1, "INVALID": 2}
return observed if rank[observed] > rank[current] else current
def _failure_record(
*,
output_root: Path,
capture_id: str,
symbol: str,
raw_spool: _RawMessageSpool,
raw_evidence: _PublishedRawCapture | None,
error: BaseException,
) -> None:
write_json(
output_root / "quality" / f"live_depth_capture.{capture_id}.failed.json",
{
"generated_at_utc": utc_now_iso(),
"capture_id": capture_id,
"capture_status": "FAILED",
"symbol": symbol,
"messages_preserved": raw_spool.messages,
"raw_path": str(
raw_evidence.path if raw_evidence is not None else raw_spool.evidence_path
),
"raw_manifest": (str(raw_evidence.manifest_path) if raw_evidence is not None else None),
"error_type": type(error).__name__,
"error": str(error),
"completion_manifest_published": False,
},
)
async def _capture_depth(
*,
symbol: str,
max_messages: int,
output_root: Path,
duration_seconds: float | None = None,
) -> DepthCaptureResult:
if max_messages < 1:
raise ValueError("max_messages must be positive")
if duration_seconds is not None and duration_seconds <= 0:
raise ValueError("duration_seconds must be positive when supplied")
output_root.mkdir(parents=True, exist_ok=True)
capture_id = f"{symbol.lower()}-{time.time_ns()}"
client = BinancePublicClient()
metadata = client.fetch_exchange_info(symbol=symbol, raw_root=output_root / "raw")
raw_spool_holder: list[_RawMessageSpool] = []
def preserve_raw_frame(frame: RawDepthFrame) -> None:
if not raw_spool_holder: # pragma: no cover - collector cannot run during construction
raise RuntimeError("raw capture journal is not initialized")
raw_spool_holder[0].append_frame(frame)
collector = BinanceLiveDepthCollector(
symbols=(symbol,),
tick_size=metadata.tick_size,
lot_size=metadata.lot_size,
on_raw_frame=preserve_raw_frame,
)
raw_spool = _RawMessageSpool(root=output_root, symbol=symbol, source_uri=collector.url)
raw_spool_holder.append(raw_spool)
raw_evidence: _PublishedRawCapture | None = None
delta_validator = IncrementalQualityValidator(
"depth_deltas",
row_chunk_size=_LIVE_BATCH_ROWS,
)
observation_validator = IncrementalQualityValidator(
"book_observations",
row_chunk_size=_LIVE_BATCH_ROWS,
)
validators_finished = False
with tempfile.TemporaryDirectory(
dir=output_root,
prefix=f".{capture_id}.spool-",
) as temporary_root_name:
temporary_root = Path(temporary_root_name)
spools = {
"book_snapshots": _ArrowBatchSpool(
root=temporary_root,
schema_name="book_snapshots",
batch_rows=_LIVE_BATCH_ROWS,
max_buffer_bytes=_LIVE_BATCH_ESTIMATED_BYTES,
),
"depth_deltas": _ArrowBatchSpool(
root=temporary_root,
schema_name="depth_deltas",
batch_rows=_LIVE_BATCH_ROWS,
max_buffer_bytes=_LIVE_BATCH_ESTIMATED_BYTES,
on_batch=delta_validator.update,
),
"book_observations": _ArrowBatchSpool(
root=temporary_root,
schema_name="book_observations",
batch_rows=_LIVE_BATCH_ROWS,
max_buffer_bytes=_LIVE_BATCH_ESTIMATED_BYTES,
on_batch=observation_validator.update,
),
"sequence_gaps": _ArrowBatchSpool(
root=temporary_root,
schema_name="sequence_gaps",
batch_rows=_LIVE_BATCH_ROWS,
max_buffer_bytes=_LIVE_BATCH_ESTIMATED_BYTES,
),
}
current_continuity_id: str | None = None
reconstructor: IncrementalBookReconstructor | None = None
continuity_epochs = 0
status: Literal["LIVE", "GAPPED", "INVALID"] = "LIVE"
stale_events = 0
excluded_messages = 0
final_update_id = 0
capture_stop = _DepthCaptureStop()
epoch_coverage: list[_DepthEpochCoverage] = []
current_epoch_coverage: _DepthEpochCoverage | None = None
try:
async for item in _bounded_depth_items(
collector,
max_messages=max_messages,
duration_seconds=duration_seconds,
stop=capture_stop,
):
raw_message_bytes = raw_spool.append_captured(item)
if item.delta.continuity_id != current_continuity_id:
snapshot = client.fetch_depth_snapshot(
symbol=symbol,
raw_root=output_root / "raw",
continuity_id=item.delta.continuity_id,
tick_size=metadata.tick_size,
lot_size=metadata.lot_size,
)
raw_spool.append_snapshot(snapshot)
snapshot_estimated_bytes = max(
4_096,
128 * (len(snapshot.bids) + len(snapshot.asks)),
)
spools["book_snapshots"].append(
snapshot.to_record(),
estimated_bytes=snapshot_estimated_bytes,
)
reconstructor = IncrementalBookReconstructor(snapshot)
current_continuity_id = item.delta.continuity_id
continuity_epochs += 1
received_ts_ns = item.delta.received_ts_ns
if received_ts_ns is None: # pragma: no cover - append_captured validates
raise RuntimeError("live depth delta has no receipt timestamp")
current_epoch_coverage = _DepthEpochCoverage(
continuity_id=item.delta.continuity_id,
snapshot_id=snapshot.snapshot_id,
first_received_ns=received_ts_ns,
last_received_ns=received_ts_ns,
)
epoch_coverage.append(current_epoch_coverage)
if reconstructor is None: # pragma: no cover - guarded by epoch creation
raise RuntimeError("live depth epoch has no reconstructor")
if current_epoch_coverage is None: # pragma: no cover - guarded by epoch creation
raise RuntimeError("live depth epoch has no coverage tracker")
spools["depth_deltas"].append(
item.delta.to_record(),
estimated_bytes=max(
4_096,
raw_message_bytes * _VARIABLE_RECORD_OVERHEAD_FACTOR,
),
)
step = reconstructor.update(item.delta)
if step.observation is not None:
spools["book_observations"].append(
step.observation,
estimated_bytes=4_096,
)
else:
excluded_messages += 1
if step.gap is not None:
spools["sequence_gaps"].append(
step.gap.to_record(),
estimated_bytes=2_048,
)
if step.outcome == "STALE":
stale_events += 1
status = _status_max(status, reconstructor.status)
final_update_id = reconstructor.final_update_id
received_ts_ns = item.delta.received_ts_ns
if received_ts_ns is None: # pragma: no cover - append_captured validates
raise RuntimeError("live depth delta has no receipt timestamp")
current_epoch_coverage.last_received_ns = received_ts_ns
current_epoch_coverage.messages += 1
current_epoch_coverage.book_observations += int(step.observation is not None)
current_epoch_coverage.excluded_messages += int(step.observation is None)
current_epoch_coverage.sequence_gaps += int(step.gap is not None)
current_epoch_coverage.reconstruction_status = reconstructor.status
current_epoch_coverage.final_update_id = reconstructor.final_update_id
if duration_seconds is None:
if raw_spool.messages != max_messages or capture_stop.reason != "message_limit":
raise RuntimeError(
f"live depth stream ended after {raw_spool.messages} of "
f"{max_messages} requested messages"
)
else:
if capture_stop.reason == "message_safety_ceiling":
raise RuntimeError(
"live depth message safety ceiling was reached before the requested "
"capture duration elapsed"
)
if capture_stop.reason != "duration_elapsed":
raise RuntimeError(
"live depth stream ended before the requested capture duration "
f"({capture_stop.reason})"
)
if raw_spool.messages == 0:
raise RuntimeError("duration-bounded live depth capture received no messages")
if raw_spool.snapshot_anchors != continuity_epochs:
raise RuntimeError("not every continuity epoch has a raw snapshot anchor")
for spool in spools.values():
spool.close()
if spools["depth_deltas"].rows != raw_spool.messages:
raise RuntimeError("captured and normalized depth-message counts diverged")
if spools["book_observations"].rows + excluded_messages != raw_spool.messages:
raise RuntimeError("not every normalized depth message has an explicit outcome")
delta_quality = delta_validator.finish()
observation_quality = observation_validator.finish()
validators_finished = True
raw_evidence = raw_spool.publish(status="raw_capture_complete")
normalized_root = output_root / "normalized" / "captures" / capture_id
time_columns = {
"book_snapshots": "received_ts_ns",
"depth_deltas": "event_ts_ns",
"book_observations": "event_ts_ns",
"sequence_gaps": "detected_ts_ns",
}
dataset_manifests: dict[str, dict[str, object]] = {}
for schema_name, spool in spools.items():
stored = write_capture_parquet(
spool.iter_batches(),
root=normalized_root,
dataset=schema_name,
schema_name=schema_name,
venue="binance_spot",
symbol=symbol,
capture_id=capture_id,
source="binance_spot_public_live_capture_journal",
source_uri=str(raw_evidence.path),
source_checksum_sha256=raw_evidence.sha256,
requested_start_ns=raw_spool.first_received_ns,
requested_end_ns=(
raw_spool.last_received_ns + 1
if raw_spool.last_received_ns is not None
else None
),
time_column=time_columns[schema_name],
max_input_batch_rows=_LIVE_BATCH_ROWS,
)
if stored.rows != spool.rows:
raise RuntimeError(
f"stored {schema_name} row count does not match its verified spool"
)
dataset_manifests[schema_name] = {
"data_path": str(stored.data_path) if stored.data_path is not None else None,
"data_sha256": stored.data_sha256,
"manifest_path": str(stored.manifest_path),
"manifest_sha256": stored.manifest_sha256,
"rows": stored.rows,
}
quality_reports: tuple[ValidationReport, ...] = (
delta_quality,
observation_quality,
)
quality_errors = sum(report.error_count for report in quality_reports)
quality_warnings = sum(report.warning_count for report in quality_reports)
quality_root = output_root / "quality"
quality_root.mkdir(parents=True, exist_ok=True)
quality_report_paths: dict[str, str] = {}
for report in quality_reports:
report_path = quality_root / f"live_{report.dataset}.{capture_id}.validation.json"
report.write_json(report_path)
quality_report_paths[report.dataset] = str(report_path)
summary_path = quality_root / f"live_depth_capture.{capture_id}.summary.json"
receipt_coverage_seconds = (
(raw_spool.last_received_ns - raw_spool.first_received_ns) / 1_000_000_000.0
if raw_spool.first_received_ns is not None
and raw_spool.last_received_ns is not None
else 0.0
)
max_continuity_epoch_seconds = max(
(epoch.duration_seconds for epoch in epoch_coverage),
default=0.0,
)
summary_payload = {
"generated_at_utc": utc_now_iso(),
"capture_id": capture_id,
"capture_status": "COMPLETE",
"messages": raw_spool.messages,
"continuity_epochs": continuity_epochs,
"normalized_messages": spools["depth_deltas"].rows,
"book_observations": spools["book_observations"].rows,
"sequence_gaps": spools["sequence_gaps"].rows,
"stale_events": stale_events,
"excluded_messages": excluded_messages,
"reconstruction_status": status,
"quality_errors": quality_errors,
"quality_warnings": quality_warnings,
"completion_reason": capture_stop.reason,
"requested_duration_seconds": duration_seconds,
"message_safety_ceiling": max_messages,
"elapsed_monotonic_seconds": capture_stop.elapsed_monotonic_seconds,
"receipt_coverage_seconds": receipt_coverage_seconds,
"continuity_epoch_coverage": [epoch.to_dict() for epoch in epoch_coverage],
"max_continuity_epoch_seconds": max_continuity_epoch_seconds,
"quality_reports": quality_report_paths,
"raw_path": str(raw_evidence.path),
"raw_manifest": str(raw_evidence.manifest_path),
"raw_manifest_sha256": raw_evidence.manifest_sha256,
"normalized_dataset_manifests": dataset_manifests,
"max_buffered_rows_per_dataset": {
name: spool.max_buffered_rows for name, spool in spools.items()
},
"max_buffered_estimated_bytes_per_dataset": {
name: spool.max_buffered_estimated_bytes for name, spool in spools.items()
},
"policy": (
"every continuity transition receives a fresh snapshot; every captured "
"delta is normalized, and non-observed deltas are counted or gap-audited"
),
}
# The capture-ID-specific completion marker is authoritative and published last.
write_json(summary_path, summary_payload)
with suppress(BaseException):
write_json(
quality_root / "live_depth_capture.summary.json",
{
**summary_payload,
"capture_status": "LATEST_POINTER",
"latest_capture_status": summary_payload["capture_status"],
"authoritative_summary_path": str(summary_path),
"authoritative_summary_sha256": sha256_file(summary_path),
},
)
return DepthCaptureResult(
symbol=symbol,
messages=raw_spool.messages,
continuity_epochs=continuity_epochs,
reconstruction_status=status,
book_observations=spools["book_observations"].rows,
sequence_gaps=spools["sequence_gaps"].rows,
stale_events=stale_events,
excluded_messages=excluded_messages,
final_update_id=final_update_id,
quality_errors=quality_errors,
quality_warnings=quality_warnings,
raw_path=raw_evidence.path,
raw_manifest_path=raw_evidence.manifest_path,
raw_manifest_sha256=raw_evidence.manifest_sha256,
summary_path=summary_path,
completion_reason=capture_stop.reason,
requested_duration_seconds=duration_seconds,
elapsed_monotonic_seconds=capture_stop.elapsed_monotonic_seconds,
receipt_coverage_seconds=receipt_coverage_seconds,
max_continuity_epoch_seconds=max_continuity_epoch_seconds,
)
except BaseException as error:
if raw_evidence is None:
try:
raw_evidence = raw_spool.publish(
status="incomplete_capture_failure",
error=error,
)
except BaseException:
raw_spool.close_without_deleting()
else:
with suppress(BaseException):
raw_evidence = raw_spool.publish(
status="normalization_failure",
error=error,
)
with suppress(BaseException):
_failure_record(
output_root=output_root,
capture_id=capture_id,
symbol=symbol,
raw_spool=raw_spool,
raw_evidence=raw_evidence,
error=error,
)
raise
finally:
for spool in spools.values():
with suppress(BaseException):
spool.close()
if not validators_finished:
delta_validator.close()
observation_validator.close()
def _cmd_collect_l2(args: argparse.Namespace) -> int:
output_root = Path(args.output_root).resolve()
result = asyncio.run(
_capture_depth(
symbol=str(args.symbol).upper(),
max_messages=int(args.max_messages),
output_root=output_root,
duration_seconds=(
float(args.duration_seconds) if args.duration_seconds is not None else None
),
)
)
_print_json(
{
"symbol": result.symbol,
"messages": result.messages,
"reconstruction_status": result.reconstruction_status,
"book_observations": result.book_observations,
"sequence_gaps": result.sequence_gaps,
"stale_events": result.stale_events,
"excluded_messages": result.excluded_messages,
"final_update_id": result.final_update_id,
"quality_errors": result.quality_errors,
"quality_warnings": result.quality_warnings,
"raw_manifest": result.raw_manifest_path,
"raw_manifest_sha256": result.raw_manifest_sha256,
"capture_summary": result.summary_path,
"completion_reason": result.completion_reason,
"requested_duration_seconds": result.requested_duration_seconds,
"elapsed_monotonic_seconds": result.elapsed_monotonic_seconds,
"receipt_coverage_seconds": result.receipt_coverage_seconds,
"max_continuity_epoch_seconds": result.max_continuity_epoch_seconds,
"output_root": output_root,
"live_trading": False,
}
)
return 0 if result.reconstruction_status == "LIVE" and result.quality_errors == 0 else 1
def _m8_l2_session_payload(result: M8L2SessionBundle) -> dict[str, object]:
return {
"status": result.status,
"session_id": result.session_id,
"session_date": result.session_date,
"role": result.role,
"output_root": result.root,
"session_manifest": result.manifest_path,
"session_manifest_sha256": result.manifest_sha256,
"checksums": result.checksum_path,
"terminal_marker": result.marker_path,
"reason_codes": list(getattr(result, "reason_codes", ())),
"source": "binance_spot_public_live_diff_depth",
"live_trading": False,
}
def _cmd_capture_m8_l2_session(args: argparse.Namespace) -> int:
config = load_m8_l2_config(args.config)
result = asyncio.run(
capture_m8_l2_session(
config,
str(args.date),
Path(args.output_root).resolve(),
BinanceM8L2Capture(),
)
)
_print_json(_m8_l2_session_payload(result))
return 0 if result.status == "COMPLETE" else 1
def _cmd_verify_m8_l2_session(args: argparse.Namespace) -> int:
config = load_m8_l2_config(args.config)
result = verify_m8_l2_session_bundle(args.bundle_dir, expected_config=config)
payload = _m8_l2_session_payload(result)
payload["integrity"] = "verified"
_print_json(payload)
return 0
def _m8_l2_development_payload(
result: L2DevelopmentLockResult,
*,
integrity: str | None = None,
) -> dict[str, object]:
payload: dict[str, object] = {
"status": getattr(result, "status", "LOCKED"),
"development_lock_dir": result.root,
"development_lock": result.aggregate_path,
"development_lock_sha256": result.aggregate_sha256,
"terminal_marker": result.marker_path,
"created_at_utc": result.created_at_utc,
"children": [
{
"symbol": child.symbol,
"endpoint": child.endpoint,
"lock": child.path,
"lock_sha256": child.sha256,
"selection_lock_sha256": child.selection_lock_sha256,
"fitted_state_sha256": child.fitted_state_sha256,
}
for child in result.children
],
"reason_codes": list(getattr(result, "reason_codes", ())),
"heldout_accessed": False,
"source": "binance_spot_public_live_diff_depth",
"live_trading": False,
}
if integrity is not None:
payload["integrity"] = integrity
return payload
def _verify_m8_l2_session_authority(
bundle_dir: Path,
*,
expected_config: M8L2StudyConfig,
expected_date: str,
expected_role: str,
manifest_sha256: str,
checksums_sha256: str,
) -> M8L2SessionBundle:
bundle = verify_m8_l2_session_bundle(bundle_dir, expected_config=expected_config)
if bundle.session_date != expected_date or bundle.role != expected_role:
raise ValueError(
f"explicit L2 session coordinate differs from {expected_date} {expected_role}"
)
if bundle.manifest_sha256 != manifest_sha256:
raise ValueError(f"explicit {expected_role} session manifest differs from expected SHA-256")
if sha256_file(bundle.checksum_path) != checksums_sha256:
raise ValueError(f"explicit {expected_role} session checksums differ from expected SHA-256")
return bundle
def _m8_l2_insufficient_development_payload(
sessions: Sequence[M8L2SessionBundle],
) -> dict[str, object]:
return {
"status": "INSUFFICIENT_DATA",
"stage": "development_lock",
"reason_code": "DEVELOPMENT_SESSION_NOT_COMPLETE",
"sessions": [
{
"session_id": session.session_id,
"session_date": session.session_date,
"role": session.role,
"status": session.status,
"session_manifest": session.manifest_path,
"session_manifest_sha256": session.manifest_sha256,
"checksums": session.checksum_path,
"checksums_sha256": sha256_file(session.checksum_path),
"reason_codes": list(session.reason_codes),
}
for session in sessions
],
"heldout_accessed": False,
"source": "binance_spot_public_live_diff_depth",
"live_trading": False,
}
def _development_session_authorities(
args: argparse.Namespace,
) -> dict[str, L2SessionFileAuthority]:
return {
"2026-08-10": L2SessionFileAuthority(
manifest_sha256=args.train_manifest_sha256,
checksums_sha256=args.train_checksums_sha256,
),
"2026-08-11": L2SessionFileAuthority(
manifest_sha256=args.validation_manifest_sha256,
checksums_sha256=args.validation_checksums_sha256,
),
}
def _explicit_development_input_loader(
authorities: Mapping[str, L2SessionFileAuthority],
) -> L2DevelopmentInputVerifier:
def load(
bundle_dir: str | Path,
*,
expected_config: M8L2StudyConfig,
expected_date: str,
expected_role: str,
expected_file_authority: object | None = None,
expected_campaign: object | None = None,
) -> Any:
authority = authorities.get(expected_date)
if authority is None:
raise ValueError("development input date is outside the explicit authority set")
if expected_role not in ("train", "validation"):
raise ValueError("development input role is outside the explicit authority set")
if expected_file_authority is not None and expected_file_authority != authority:
raise ValueError("development input authority differs from the explicit CLI authority")
return verify_m8_l2_development_input(
bundle_dir,
expected_config=expected_config,
expected_date=expected_date,
expected_role=cast(Literal["train", "validation"], expected_role),
expected_file_authority=authority,
expected_campaign=cast(L2CampaignRuntimeIdentity | None, expected_campaign),
)
return load
def _load_explicit_development_sessions(
args: argparse.Namespace,
capture_config: M8L2StudyConfig,
) -> tuple[M8L2SessionBundle, M8L2SessionBundle]:
train = _verify_m8_l2_session_authority(
args.train_bundle_dir.absolute(),
expected_config=capture_config,
expected_date="2026-08-10",
expected_role="train",
manifest_sha256=args.train_manifest_sha256,
checksums_sha256=args.train_checksums_sha256,
)
validation = _verify_m8_l2_session_authority(
args.validation_bundle_dir.absolute(),
expected_config=capture_config,
expected_date="2026-08-11",
expected_role="validation",
manifest_sha256=args.validation_manifest_sha256,
checksums_sha256=args.validation_checksums_sha256,
)
return train, validation
def _cmd_lock_m8_l2_development(args: argparse.Namespace) -> int:
capture_config = load_m8_l2_config(args.capture_config)
analysis_config = load_m8_l2_analysis_config(args.analysis_config)
_load_explicit_development_sessions(args, capture_config)
result = lock_m8_l2_development(
capture_config,
analysis_config,
args.train_bundle_dir.absolute(),
args.validation_bundle_dir.absolute(),
args.lock_dir.absolute(),
input_loader=_explicit_development_input_loader(_development_session_authorities(args)),
expected_session_file_authorities=_development_session_authorities(args),
)
_print_json(_m8_l2_development_payload(result))
return 0 if getattr(result, "status", "LOCKED") == "LOCKED" else 1
def _cmd_verify_m8_l2_development_lock(args: argparse.Namespace) -> int:
capture_config = load_m8_l2_config(args.capture_config)
analysis_config = load_m8_l2_analysis_config(args.analysis_config)
_load_explicit_development_sessions(args, capture_config)
result = verify_m8_l2_development_lock(
capture_config,
analysis_config,
args.train_bundle_dir.absolute(),
args.validation_bundle_dir.absolute(),
args.lock_dir.absolute(),
expected_lock_sha256=args.development_lock_sha256,
)
_print_json(_m8_l2_development_payload(result, integrity="verified"))
return 0 if getattr(result, "status", "LOCKED") == "LOCKED" else 1
def _m8_l2_study_authorities(
args: argparse.Namespace,
) -> tuple[
L2StudySessionAuthority,
L2StudySessionAuthority,
L2StudySessionAuthority,
L2StudySessionAuthority,
]:
def authority(role: str) -> L2StudySessionAuthority:
return L2StudySessionAuthority(
bundle_path=getattr(args, f"{role}_bundle_dir").absolute(),
manifest_sha256=getattr(args, f"{role}_manifest_sha256"),
checksums_sha256=getattr(args, f"{role}_checksums_sha256"),
)
return (
authority("train"),
authority("validation"),
authority("primary"),
authority("replication"),
)
def _m8_l2_study_payload(
result: M8L2StudyRunResult,
*,
integrity: str | None = None,
) -> dict[str, object]:
payload: dict[str, object] = {
"status": result.status,
"run_dir": result.root,
"run_manifest": result.manifest_path,
"run_manifest_sha256": result.manifest_sha256,
"checksums": result.checksum_path,
"checksums_sha256": result.checksum_sha256,
"terminal_marker": result.marker_path,
"reason_codes": list(result.reason_codes),
"source": "binance_spot_public_live_diff_depth",
"live_trading": False,
}
if integrity is not None:
payload["integrity"] = integrity
return payload
def _m8_l2_study_arguments(
args: argparse.Namespace,
capture_config: M8L2StudyConfig,
analysis_config: M8L2AnalysisConfig,
) -> tuple[
M8L2StudyConfig,
M8L2AnalysisConfig,
L2StudySessionAuthority,
L2StudySessionAuthority,
Path,
str,
L2StudySessionAuthority,
L2StudySessionAuthority,
Path,
]:
train, validation, primary, replication = _m8_l2_study_authorities(args)
return (
capture_config,
analysis_config,
train,
validation,
args.development_lock_dir.absolute(),
args.development_lock_sha256,
primary,
replication,
args.run_dir.absolute(),
)
def _cmd_reproduce_m8_l2(args: argparse.Namespace) -> int:
capture_config = load_m8_l2_config(args.capture_config)
analysis_config = load_m8_l2_analysis_config(args.analysis_config)
result = reproduce_m8_l2_study(*_m8_l2_study_arguments(args, capture_config, analysis_config))
_print_json(_m8_l2_study_payload(result))
return 0 if result.status == "COMPLETE" else 1
def _verify_m8_l2_study_from_args(
args: argparse.Namespace,
*,
capture_config: M8L2StudyConfig,
analysis_config: M8L2AnalysisConfig,
) -> M8L2StudyRunResult:
return verify_m8_l2_study_run(
*_m8_l2_study_arguments(args, capture_config, analysis_config),
expected_manifest_sha256=args.run_manifest_sha256,
expected_checksums_sha256=args.run_checksums_sha256,
)
def _cmd_verify_m8_l2_run(args: argparse.Namespace) -> int:
capture_config = load_m8_l2_config(args.capture_config)
analysis_config = load_m8_l2_analysis_config(args.analysis_config)
result = _verify_m8_l2_study_from_args(
args,
capture_config=capture_config,
analysis_config=analysis_config,
)
_print_json(_m8_l2_study_payload(result, integrity="verified"))
return 0 if result.status == "COMPLETE" else 1
def _cmd_report_m8_l2(args: argparse.Namespace) -> int:
capture_config = load_m8_l2_config(args.capture_config)
analysis_config = load_m8_l2_analysis_config(args.analysis_config)
positional = _m8_l2_study_arguments(args, capture_config, analysis_config)
result = verify_m8_l2_study_run(
*positional,
expected_manifest_sha256=args.run_manifest_sha256,
expected_checksums_sha256=args.run_checksums_sha256,
)
output = _external_report_dir(result.root, args.output_dir)
report_data = load_m8_l2_report_data(
*positional,
expected_manifest_sha256=args.run_manifest_sha256,
expected_checksums_sha256=args.run_checksums_sha256,
)
technical, memo, comparison = write_l2_report_set(output, report_data)
payload = _m8_l2_study_payload(result, integrity="verified")
payload.update(
{
"output_dir": output,
"technical_report": technical,
"executive_memo": memo,
"model_comparison": comparison,
"report_inputs_sha256": canonical_report_data_sha256(report_data),
"source_bundle_modified": False,
}
)
_print_json(payload)
return 0 if result.status == "COMPLETE" else 1
def _add_m8_l2_development_authority_args(parser: argparse.ArgumentParser) -> None:
parser.add_argument("--capture-config", required=True, type=Path)
parser.add_argument("--analysis-config", required=True, type=Path)
parser.add_argument("--train-bundle-dir", required=True, type=Path)
parser.add_argument(
"--train-manifest-sha256",
required=True,
type=_lowercase_sha256,
)
parser.add_argument(
"--train-checksums-sha256",
required=True,
type=_lowercase_sha256,
)
parser.add_argument("--validation-bundle-dir", required=True, type=Path)
parser.add_argument(
"--validation-manifest-sha256",
required=True,
type=_lowercase_sha256,
)
parser.add_argument(
"--validation-checksums-sha256",
required=True,
type=_lowercase_sha256,
)
parser.add_argument("--lock-dir", required=True, type=Path)
def _add_m8_l2_study_authority_args(
parser: argparse.ArgumentParser,
*,
require_run_authority: bool,
) -> None:
parser.add_argument("--capture-config", required=True, type=Path)
parser.add_argument("--analysis-config", required=True, type=Path)
for role in ("train", "validation", "primary", "replication"):
parser.add_argument(f"--{role}-bundle-dir", required=True, type=Path)
parser.add_argument(
f"--{role}-manifest-sha256",
required=True,
type=_lowercase_sha256,
)
parser.add_argument(
f"--{role}-checksums-sha256",
required=True,
type=_lowercase_sha256,
)
parser.add_argument("--development-lock-dir", required=True, type=Path)
parser.add_argument(
"--development-lock-sha256",
required=True,
type=_lowercase_sha256,
)
parser.add_argument("--run-dir", required=True, type=Path)
if require_run_authority:
parser.add_argument(
"--run-manifest-sha256",
required=True,
type=_lowercase_sha256,
)
parser.add_argument(
"--run-checksums-sha256",
required=True,
type=_lowercase_sha256,
)
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="microstructure",
description="Research-only event-driven market-microstructure system",
)
parser.add_argument("--version", action="version", version=__version__)
subparsers = parser.add_subparsers(dest="command", required=True)
ingest = subparsers.add_parser("ingest", help="ingest configured synthetic or public data")
ingest.add_argument("--config", required=True, type=Path)
ingest.add_argument("--output-root", type=Path)
ingest.set_defaults(handler=_cmd_ingest)
acquire_m8 = subparsers.add_parser(
"acquire-m8",
help="acquire raw M8 evidence without opening any archive CSV member",
)
acquire_m8.add_argument("--config", required=True, type=Path)
acquire_m8.add_argument("--output-root", required=True, type=Path)
acquire_m8.set_defaults(handler=_cmd_acquire_m8)
validate = subparsers.add_parser("validate", help="run non-mutating data validation")
validate.add_argument("--config", required=True, type=Path)
validate.set_defaults(handler=_cmd_validate)
reproduce_parser = subparsers.add_parser(
"reproduce", help="produce or verify an immutable end-to-end sample run"
)
reproduce_parser.add_argument("--config", required=True, type=Path)
reproduce_parser.add_argument("--run-dir", required=True, type=Path)
reproduce_parser.add_argument(
"--ingestion-manifest",
type=Path,
help="explicit public ingestion manifest; required with its SHA-256 for public mode",
)
reproduce_parser.add_argument(
"--ingestion-manifest-sha256",
help="SHA-256 of --ingestion-manifest; required for public mode",
)
reproduce_parser.set_defaults(handler=_cmd_reproduce)
reproduce_m8_parser = subparsers.add_parser(
"reproduce-m8",
help="produce the frozen M8 study from one explicit raw acquisition authority",
)
reproduce_m8_parser.add_argument("--config", required=True, type=Path)
reproduce_m8_parser.add_argument("--run-dir", required=True, type=Path)
reproduce_m8_parser.add_argument("--raw-manifest", required=True, type=Path)
reproduce_m8_parser.add_argument(
"--raw-manifest-sha256",
required=True,
type=_lowercase_sha256,
)
reproduce_m8_parser.set_defaults(handler=_cmd_reproduce_m8)
verify_m8_parser = subparsers.add_parser(
"verify-m8",
help="verify a complete or INSUFFICIENT_DATA M8 result and its raw authority",
)
verify_m8_parser.add_argument("--config", required=True, type=Path)
verify_m8_parser.add_argument("--run-dir", required=True, type=Path)
verify_m8_parser.add_argument("--raw-manifest", required=True, type=Path)
verify_m8_parser.add_argument(
"--raw-manifest-sha256",
required=True,
type=_lowercase_sha256,
)
verify_m8_parser.set_defaults(handler=_cmd_verify_m8)
report_m8_parser = subparsers.add_parser(
"report-m8",
help="render a complete M8 result or expose its frozen failure report",
)
report_m8_parser.add_argument("--config", required=True, type=Path)
report_m8_parser.add_argument("--run-dir", required=True, type=Path)
report_m8_parser.add_argument("--raw-manifest", required=True, type=Path)
report_m8_parser.add_argument(
"--raw-manifest-sha256",
required=True,
type=_lowercase_sha256,
)
report_m8_parser.add_argument("--output-dir", type=Path)
report_m8_parser.set_defaults(handler=_cmd_report_m8)
verify = subparsers.add_parser("verify", help="verify a frozen run and all checksums")
verify.add_argument("--run-dir", required=True, type=Path)
verify.set_defaults(handler=_cmd_verify)
report = subparsers.add_parser("report", help="render reports from a frozen run")
report.add_argument("--run-dir", required=True, type=Path)
report.add_argument("--output-dir", type=Path)
report.set_defaults(handler=_cmd_report)
collect = subparsers.add_parser(
"collect-l2", help="capture and reconstruct public live L2 data; never place orders"
)
collect.add_argument("--symbol", choices=("BTCUSDT", "ETHUSDT"), required=True)
collect.add_argument("--max-messages", type=int, default=1_000)
collect.add_argument(
"--duration-seconds",
type=float,
help=(
"gracefully complete after this wall duration; max-messages remains a safety "
"ceiling and fails the capture if reached first"
),
)
collect.add_argument("--output-root", type=Path, default=Path("data"))
collect.set_defaults(handler=_cmd_collect_l2)
capture_m8_l2 = subparsers.add_parser(
"capture-m8-l2-session",
help="capture one frozen concurrent BTCUSDT/ETHUSDT prospective L2 session",
)
capture_m8_l2.add_argument("--config", required=True, type=Path)
capture_m8_l2.add_argument("--date", required=True)
capture_m8_l2.add_argument("--output-root", required=True, type=Path)
capture_m8_l2.set_defaults(handler=_cmd_capture_m8_l2_session)
verify_m8_l2 = subparsers.add_parser(
"verify-m8-l2-session",
help="verify a complete or INSUFFICIENT_DATA frozen L2 session bundle",
)
verify_m8_l2.add_argument("--config", required=True, type=Path)
verify_m8_l2.add_argument("--bundle-dir", required=True, type=Path)
verify_m8_l2.set_defaults(handler=_cmd_verify_m8_l2_session)
lock_m8_l2_development_parser = subparsers.add_parser(
"lock-m8-l2-development",
help="fit and freeze the Aug 8/9 L2 development state before held-out access",
)
_add_m8_l2_development_authority_args(lock_m8_l2_development_parser)
lock_m8_l2_development_parser.set_defaults(handler=_cmd_lock_m8_l2_development)
verify_m8_l2_development_parser = subparsers.add_parser(
"verify-m8-l2-development-lock",
help="verify the frozen L2 development lock and its explicit session authorities",
)
_add_m8_l2_development_authority_args(verify_m8_l2_development_parser)
verify_m8_l2_development_parser.add_argument(
"--development-lock-sha256",
required=True,
type=_lowercase_sha256,
)
verify_m8_l2_development_parser.set_defaults(handler=_cmd_verify_m8_l2_development_lock)
reproduce_m8_l2_parser = subparsers.add_parser(
"reproduce-m8-l2",
help="produce the frozen four-session prospective live-L2 study",
)
_add_m8_l2_study_authority_args(
reproduce_m8_l2_parser,
require_run_authority=False,
)
reproduce_m8_l2_parser.set_defaults(handler=_cmd_reproduce_m8_l2)
verify_m8_l2_run_parser = subparsers.add_parser(
"verify-m8-l2-run",
help="verify a terminal live-L2 study and all external authorities",
)
_add_m8_l2_study_authority_args(
verify_m8_l2_run_parser,
require_run_authority=True,
)
verify_m8_l2_run_parser.set_defaults(handler=_cmd_verify_m8_l2_run)
report_m8_l2_parser = subparsers.add_parser(
"report-m8-l2",
help="render verified live-L2 report inputs outside the immutable run bundle",
)
_add_m8_l2_study_authority_args(
report_m8_l2_parser,
require_run_authority=True,
)
report_m8_l2_parser.add_argument("--output-dir", required=True, type=Path)
report_m8_l2_parser.set_defaults(handler=_cmd_report_m8_l2)
return parser
def main(argv: Sequence[str] | None = None) -> int:
parser = build_parser()
args = parser.parse_args(argv)
if getattr(args, "max_messages", 1) < 1:
parser.error("--max-messages must be positive")
duration_seconds = getattr(args, "duration_seconds", None)
if duration_seconds is not None and duration_seconds <= 0:
parser.error("--duration-seconds must be positive")
handler = args.handler
try:
return int(handler(args))
except (KeyboardInterrupt, asyncio.CancelledError):
print("operation canceled", file=sys.stderr)
return 130
except Exception as error:
print(f"error: {error}", file=sys.stderr)
return 2
if __name__ == "__main__": # pragma: no cover
raise SystemExit(main())
|