Datasets:
Tasks:
Tabular Classification
Formats:
parquet
Languages:
English
Size:
< 1K
Tags:
economics
quantitative-finance
causal-inference
macroeconomics
housing-economics
market-microstructure
License:
File size: 137,429 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 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 | """Cross-symbol authority and terminal publication for frozen M8 L2 sessions.
This module deliberately depends on an injected, typed single-symbol capture
producer. It does not import the exploratory CLI collector and contains no
exchange connection or order-entry path.
"""
from __future__ import annotations
import asyncio
import base64
import binascii
import hashlib
import importlib.metadata as importlib_metadata
import json
import os
import platform
import secrets
import shutil
import stat
import subprocess
import sys
import tempfile
import time
from collections.abc import Mapping, Sequence
from contextlib import suppress
from dataclasses import dataclass
from pathlib import Path, PurePosixPath
from typing import Any, Literal, Protocol, cast
import pyarrow.parquet as pq # type: ignore[import-untyped]
from microstructure.data.schemas import SCHEMA_VERSION, get_schema
from microstructure.m8_l2_config import (
M8_L2_CONFIG_SOURCE_SHA256,
M8_L2_FREEZE_COMMIT,
M8_L2_PROTOCOL_SHA256,
M8L2CaptureLimits,
M8L2Session,
M8L2StudyConfig,
load_m8_l2_config,
)
from microstructure.provenance import (
ImportOriginError,
assert_project_module_origins,
sha256_file,
utc_now_iso,
write_json,
)
_SESSION_SCHEMA_VERSION = "m8-live-l2-session-v3"
_CAMPAIGN_SCHEMA_VERSION = "m8-live-l2-campaign-authority-v2"
_RUNTIME_SCHEMA_VERSION = "m8-live-l2-runtime-fingerprint-v1"
_CAMPAIGN_AUTHORITY_NAME = "campaign_authority.json"
_CHECKSUM_NAME = "CHECKSUMS.sha256"
_COMPLETE_MARKER = "_SUCCESS"
_INSUFFICIENT_MARKER = "INSUFFICIENT_DATA"
_COMPLETE_BYTES = b"complete\n"
_INSUFFICIENT_BYTES = b"terminal\n"
_NANOSECONDS_PER_SECOND = 1_000_000_000
_MAX_JSON_AUTHORITY_BYTES = 8 * 1024 * 1024
_MAX_CAMPAIGN_AUTHORITY_BYTES = 16 * 1024
_RUNTIME_DISTRIBUTIONS = (
"duckdb",
"numpy",
"polars",
"pyarrow",
"requests",
"scikit-learn",
"streamlit",
"websockets",
)
_SYMBOL_SUMMARY_SCHEMA_VERSION = "m8-binance-l2-symbol-capture-v1"
_REQUIRED_CAPTURE_ARTIFACT_KINDS = frozenset(
{
"capture_summary",
"normalized_data",
"normalized_manifest",
"quality_report",
"raw_journal",
"raw_journal_manifest",
"raw_snapshot",
"raw_snapshot_manifest",
}
)
SessionStatus = Literal["COMPLETE", "INSUFFICIENT_DATA"]
CaptureStatus = Literal["COMPLETE", "FAILED"]
ReconstructionStatus = Literal["LIVE", "GAPPED", "INVALID", "NOT_STARTED"]
class M8L2CaptureError(RuntimeError):
"""Base class for orchestration, authority, and verification failures."""
class M8L2VerificationError(M8L2CaptureError):
"""Raised when a session bundle is not an exact immutable authority."""
class M8L2CaptureSystemError(M8L2CaptureError):
"""A nonterminal program/I/O failure; captured raw evidence is retained."""
def __init__(self, message: str, *, evidence_root: Path | None = None) -> None:
super().__init__(message)
self.evidence_root = evidence_root
class M8L2DataFailure(M8L2CaptureError):
"""Typed prospective-session failure that may terminalize as insufficient."""
def __init__(
self,
reason_code: str,
*,
phase: str,
message: str,
partial_result: SymbolCaptureResult | None = None,
) -> None:
_validate_code(reason_code, "reason code")
_validate_code(phase, "failure phase")
super().__init__(message)
self.reason_code = reason_code
self.phase = phase
self.partial_result = partial_result
@dataclass(frozen=True, slots=True)
class CaptureSourceIdentity:
"""Clean runtime source identity bound into one prospective session."""
commit: str
source_tree_sha256: str
dirty: bool
@dataclass(frozen=True, slots=True)
class _RuntimeFingerprint:
"""Canonical production interpreter/dependency identity."""
python_implementation: str
python_version: str
platform_system: str
platform_release: str
platform_machine: str
dependencies: tuple[tuple[str, str], ...]
sha256: str
def payload(self) -> dict[str, object]:
return _runtime_payload(
python_implementation=self.python_implementation,
python_version=self.python_version,
platform_system=self.platform_system,
platform_release=self.platform_release,
platform_machine=self.platform_machine,
dependencies=self.dependencies,
)
@dataclass(frozen=True, slots=True)
class _CampaignAuthority:
"""Exact durable campaign identity shared by all four frozen sessions."""
path: Path
sha256: str
raw: bytes
source: CaptureSourceIdentity
runtime: _RuntimeFingerprint
nonce: str
output_root_path: str
output_root_device: int
output_root_inode: int
@dataclass(frozen=True, slots=True)
class ObservedInterval:
"""One continuous interval backed only by OBSERVED reconstructed states."""
continuity_id: str
start_received_ns: int
end_received_ns_exclusive: int
def __post_init__(self) -> None:
if not self.continuity_id:
raise ValueError("observed interval continuity_id must not be empty")
if self.start_received_ns < 0:
raise ValueError("observed interval start must be nonnegative")
if self.end_received_ns_exclusive <= self.start_received_ns:
raise ValueError("observed interval end must be after start")
@property
def duration_ns(self) -> int:
return self.end_received_ns_exclusive - self.start_received_ns
def to_dict(self) -> dict[str, object]:
return {
"continuity_id": self.continuity_id,
"start_received_ns": self.start_received_ns,
"end_received_ns_exclusive": self.end_received_ns_exclusive,
"duration_ns": self.duration_ns,
"duration_seconds": self.duration_ns / _NANOSECONDS_PER_SECOND,
}
@dataclass(frozen=True, slots=True)
class CapturedArtifact:
"""Explicit file coordinate returned by a single-symbol capture producer."""
path: Path
kind: str
sha256: str
@dataclass(frozen=True, slots=True)
class SymbolCaptureResult:
"""Bounded descriptor for one symbol's complete or partial capture evidence."""
symbol: str
capture_id: str
status: CaptureStatus
completion_reason: str
reconstruction_status: ReconstructionStatus
messages: int
normalized_rows: int
reconstructed_rows: int
excluded_rows: int
continuity_epochs: int
snapshot_anchors: int
sequence_gaps: int
quality_errors: int
quality_warnings: int
max_raw_frame_bytes_observed: int
max_arrow_batch_bytes_observed: int
first_raw_received_ns: int | None
last_raw_received_ns: int | None
valid_observed_intervals: tuple[ObservedInterval, ...]
artifacts: tuple[CapturedArtifact, ...]
failure_reason_code: str | None = None
failure_phase: str | None = None
class CaptureOne(Protocol):
"""Injected adapter boundary for one duration- and boundary-aware capture."""
async def __call__(
self,
*,
symbol: str,
scheduled_start_ns: int,
scheduled_end_ns: int,
stage_root: Path,
limits: M8L2CaptureLimits,
session_id: str,
) -> SymbolCaptureResult: ...
class CaptureClock(Protocol):
def time_ns(self) -> int: ...
async def sleep(self, seconds: float) -> None: ...
class _SystemClock:
def time_ns(self) -> int:
return time.time_ns()
async def sleep(self, seconds: float) -> None:
await asyncio.sleep(seconds)
@dataclass(frozen=True, slots=True)
class M8L2SessionBundle:
root: Path
status: SessionStatus
session_id: str
session_date: str
role: str
manifest_path: Path
manifest_sha256: str
checksum_path: Path
marker_path: Path
reason_codes: tuple[str, ...]
def _validate_code(value: str, label: str) -> None:
if not value or any(
character not in "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" for character in value
):
raise ValueError(f"{label} must use uppercase letters, digits, and underscores")
def _is_lower_sha256(value: str) -> bool:
return len(value) == 64 and all(character in "0123456789abcdef" for character in value)
def _stable_sha256(payload: Mapping[str, object]) -> str:
encoded = json.dumps(
dict(payload), sort_keys=True, separators=(",", ":"), allow_nan=False
).encode()
return hashlib.sha256(encoded).hexdigest()
def _run_git_text(project_root: Path, arguments: Sequence[str], *, label: str) -> str:
try:
result = subprocess.run(
["git", *arguments],
cwd=project_root,
check=False,
capture_output=True,
text=True,
env={**os.environ, "GIT_OPTIONAL_LOCKS": "0"},
)
except (OSError, UnicodeError) as error:
raise M8L2CaptureSystemError(f"cannot execute Git {label}") from error
if result.returncode != 0:
raise M8L2CaptureSystemError(f"Git {label} failed with exit {result.returncode}")
return result.stdout
def _run_git_bytes(project_root: Path, arguments: Sequence[str], *, label: str) -> bytes:
try:
result = subprocess.run(
["git", *arguments],
cwd=project_root,
check=False,
capture_output=True,
env={**os.environ, "GIT_OPTIONAL_LOCKS": "0"},
)
except OSError as error:
raise M8L2CaptureSystemError(f"cannot execute Git {label}") from error
if result.returncode != 0:
raise M8L2CaptureSystemError(f"Git {label} failed with exit {result.returncode}")
return result.stdout
def _strict_git_revision(project_root: Path) -> str:
output = _run_git_text(project_root, ("rev-parse", "HEAD"), label="revision lookup")
lines = output.splitlines()
if len(lines) != 1:
raise M8L2CaptureSystemError("Git revision lookup returned a noncanonical result")
return lines[0]
def _strict_git_status(project_root: Path) -> str:
return _run_git_text(
project_root,
("status", "--porcelain=v1", "--untracked-files=normal"),
label="status lookup",
)
def _hash_regular_nofollow(path: Path, metadata: os.stat_result) -> str:
flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)
try:
descriptor = os.open(path, flags)
except OSError as error:
raise M8L2CaptureSystemError(f"cannot open Git source file: {path}") from error
try:
opened = os.fstat(descriptor)
if not stat.S_ISREG(opened.st_mode) or (
opened.st_dev,
opened.st_ino,
opened.st_size,
opened.st_mtime_ns,
opened.st_ctime_ns,
) != (
metadata.st_dev,
metadata.st_ino,
metadata.st_size,
metadata.st_mtime_ns,
metadata.st_ctime_ns,
):
raise M8L2CaptureSystemError(f"Git source file changed before hashing: {path}")
digest = hashlib.sha256()
while chunk := os.read(descriptor, 1024 * 1024):
digest.update(chunk)
after = os.fstat(descriptor)
named = path.lstat()
coordinates = (
opened.st_dev,
opened.st_ino,
opened.st_size,
opened.st_mtime_ns,
opened.st_ctime_ns,
)
if coordinates != (
after.st_dev,
after.st_ino,
after.st_size,
after.st_mtime_ns,
after.st_ctime_ns,
) or coordinates != (
named.st_dev,
named.st_ino,
named.st_size,
named.st_mtime_ns,
named.st_ctime_ns,
):
raise M8L2CaptureSystemError(f"Git source file changed while hashing: {path}")
return digest.hexdigest()
except OSError as error:
raise M8L2CaptureSystemError(f"cannot hash Git source file: {path}") from error
finally:
os.close(descriptor)
def _strict_source_tree_sha256(repository_root: Path, listing: bytes) -> str:
encoded_paths = [item for item in listing.split(b"\0") if item]
if len(encoded_paths) != len(set(encoded_paths)):
raise M8L2CaptureSystemError("Git source-tree listing contains duplicate paths")
digest = hashlib.sha256()
for encoded_relative in sorted(encoded_paths):
relative = Path(os.fsdecode(encoded_relative))
if relative.is_absolute() or not relative.parts or ".." in relative.parts:
raise M8L2CaptureSystemError("Git source-tree listing contains an unsafe path")
path = repository_root / relative
digest.update(len(encoded_relative).to_bytes(8, "big"))
digest.update(encoded_relative)
try:
metadata = path.lstat()
except FileNotFoundError:
digest.update(b"MISSING\0")
continue
except OSError as error:
raise M8L2CaptureSystemError(f"cannot inspect Git source path: {path}") from error
digest.update((metadata.st_mode & 0o7777).to_bytes(4, "big"))
if stat.S_ISLNK(metadata.st_mode):
try:
target = os.readlink(path).encode("utf-8", errors="surrogateescape")
repeated = path.lstat()
except OSError as error:
raise M8L2CaptureSystemError(f"cannot hash Git source symlink: {path}") from error
if (metadata.st_dev, metadata.st_ino, metadata.st_mtime_ns, metadata.st_ctime_ns) != (
repeated.st_dev,
repeated.st_ino,
repeated.st_mtime_ns,
repeated.st_ctime_ns,
):
raise M8L2CaptureSystemError(f"Git source symlink changed while hashing: {path}")
digest.update(b"SYMLINK\0")
digest.update(len(target).to_bytes(8, "big"))
digest.update(target)
elif stat.S_ISREG(metadata.st_mode):
digest.update(b"FILE\0")
digest.update(_hash_regular_nofollow(path, metadata).encode())
else:
digest.update(b"OTHER\0")
return digest.hexdigest()
def _source_identity(project_root: Path) -> CaptureSourceIdentity:
"""Take one fail-closed clean Git snapshot without lenient provenance fallbacks."""
first_revision = _strict_git_revision(project_root)
first_status = _strict_git_status(project_root)
if first_status.strip():
raise M8L2CaptureSystemError(
"prospective live-L2 capture requires a clean runtime source tree"
)
top_level_raw = _run_git_text(
project_root,
("rev-parse", "--show-toplevel"),
label="repository-root lookup",
)
top_level_lines = top_level_raw.splitlines()
if len(top_level_lines) != 1 or not top_level_lines[0]:
raise M8L2CaptureSystemError("Git repository-root lookup returned a noncanonical result")
repository_root = Path(top_level_lines[0]).resolve()
try:
project_root.resolve().relative_to(repository_root)
except ValueError as error:
raise M8L2CaptureSystemError("capture project root escapes the Git repository") from error
listing = _run_git_bytes(
repository_root,
("ls-files", "-z", "--cached", "--others", "--exclude-standard"),
label="source-tree listing",
)
source_tree_sha256 = _strict_source_tree_sha256(repository_root, listing)
second_status = _strict_git_status(project_root)
second_revision = _strict_git_revision(project_root)
if second_status.strip() or first_revision != second_revision:
raise M8L2CaptureSystemError("runtime Git identity changed during source snapshot")
return CaptureSourceIdentity(
commit=first_revision,
source_tree_sha256=source_tree_sha256,
dirty=False,
)
def _runtime_payload(
*,
python_implementation: str,
python_version: str,
platform_system: str,
platform_release: str,
platform_machine: str,
dependencies: tuple[tuple[str, str], ...],
) -> dict[str, object]:
return {
"schema_version": _RUNTIME_SCHEMA_VERSION,
"python": {
"implementation": python_implementation,
"version": python_version,
},
"platform": {
"system": platform_system,
"release": platform_release,
"machine": platform_machine,
},
"dependencies": dict(dependencies),
}
def _runtime_fingerprint() -> _RuntimeFingerprint:
try:
dependencies = tuple(
(distribution, importlib_metadata.version(distribution))
for distribution in _RUNTIME_DISTRIBUTIONS
)
except importlib_metadata.PackageNotFoundError as error:
raise M8L2CaptureSystemError(
f"production runtime dependency is not installed: {error.name}"
) from error
implementation = platform.python_implementation()
version = platform.python_version()
system = platform.system()
release = platform.release()
machine = platform.machine()
payload = _runtime_payload(
python_implementation=implementation,
python_version=version,
platform_system=system,
platform_release=release,
platform_machine=machine,
dependencies=dependencies,
)
return _RuntimeFingerprint(
python_implementation=implementation,
python_version=version,
platform_system=system,
platform_release=release,
platform_machine=machine,
dependencies=dependencies,
sha256=_stable_sha256(payload),
)
def current_m8_l2_runtime_fingerprint_sha256() -> str:
"""Return the canonical production runtime digest used by L2 authorities."""
return _runtime_fingerprint().sha256
def _runtime_from_recorded(value: object, sha256: object) -> _RuntimeFingerprint:
if not isinstance(value, Mapping) or set(value) != {
"schema_version",
"python",
"platform",
"dependencies",
}:
raise M8L2CaptureSystemError("recorded runtime fingerprint payload is not exact")
if value.get("schema_version") != _RUNTIME_SCHEMA_VERSION:
raise M8L2CaptureSystemError("recorded runtime fingerprint schema is unsupported")
python_payload = value.get("python")
platform_payload = value.get("platform")
dependencies_payload = value.get("dependencies")
if not isinstance(python_payload, Mapping) or set(python_payload) != {
"implementation",
"version",
}:
raise M8L2CaptureSystemError("recorded Python runtime identity is not exact")
if not isinstance(platform_payload, Mapping) or set(platform_payload) != {
"system",
"release",
"machine",
}:
raise M8L2CaptureSystemError("recorded platform runtime identity is not exact")
if not isinstance(dependencies_payload, Mapping) or set(dependencies_payload) != set(
_RUNTIME_DISTRIBUTIONS
):
raise M8L2CaptureSystemError("recorded production dependency identity is not exact")
implementation = python_payload.get("implementation")
version = python_payload.get("version")
system = platform_payload.get("system")
release = platform_payload.get("release")
machine = platform_payload.get("machine")
if (
type(implementation) is not str
or not implementation
or type(version) is not str
or not version
or type(system) is not str
or not system
or type(release) is not str
or not release
or type(machine) is not str
or not machine
):
raise M8L2CaptureSystemError("recorded Python runtime identity is malformed")
dependencies: list[tuple[str, str]] = []
for distribution in _RUNTIME_DISTRIBUTIONS:
dependency_version = dependencies_payload.get(distribution)
if type(dependency_version) is not str or not dependency_version:
raise M8L2CaptureSystemError(
f"recorded dependency version is malformed: {distribution}"
)
dependencies.append((distribution, dependency_version))
canonical_payload = _runtime_payload(
python_implementation=implementation,
python_version=version,
platform_system=system,
platform_release=release,
platform_machine=machine,
dependencies=tuple(dependencies),
)
observed_sha256 = _stable_sha256(canonical_payload)
if type(sha256) is not str or not _is_lower_sha256(sha256) or sha256 != observed_sha256:
raise M8L2CaptureSystemError("recorded runtime fingerprint digest is invalid")
if dict(value) != canonical_payload:
raise M8L2CaptureSystemError("recorded runtime fingerprint payload is not canonical")
return _RuntimeFingerprint(
python_implementation=implementation,
python_version=version,
platform_system=system,
platform_release=release,
platform_machine=machine,
dependencies=tuple(dependencies),
sha256=observed_sha256,
)
def _validate_source_identity(identity: CaptureSourceIdentity) -> None:
if identity.dirty:
raise M8L2CaptureSystemError(
"prospective live-L2 capture requires a clean runtime source tree"
)
if len(identity.commit) != 40 or any(
character not in "0123456789abcdef" for character in identity.commit
):
raise M8L2CaptureSystemError("runtime Git commit must be a lowercase 40-character SHA-1")
if not _is_lower_sha256(identity.source_tree_sha256):
raise M8L2CaptureSystemError("runtime source-tree digest is not a lowercase SHA-256")
def _assert_loaded_source_root(project_root: Path) -> None:
try:
assert_project_module_origins(project_root, "microstructure.m8_l2_capture")
except ImportOriginError as error:
raise M8L2CaptureSystemError(
"loaded microstructure code does not come from the hashed capture project root"
) from error
def _assert_production_capture_origin(project_root: Path, capture_one: CaptureOne) -> None:
"""Bind the real network adapter and every critical dependency to one checkout."""
capture_type = type(capture_one)
module_name = getattr(capture_type, "__module__", None)
if (
module_name != "microstructure.m8_l2_binance"
or capture_type.__name__ != "BinanceM8L2Capture"
):
raise M8L2CaptureSystemError(
"production CaptureOne must be the exact BinanceM8L2Capture adapter"
)
adapter_module = sys.modules.get(module_name)
if adapter_module is None or getattr(adapter_module, "BinanceM8L2Capture", None) is not (
capture_type
):
raise M8L2CaptureSystemError(
"production BinanceM8L2Capture class lacks its canonical loaded module"
)
try:
assert_project_module_origins(
project_root,
adapter_module,
"microstructure.data.binance",
"microstructure.data.book",
"microstructure.data.quality",
"microstructure.data.schemas",
"microstructure.data.storage",
)
except ImportOriginError as error:
raise M8L2CaptureSystemError(
"production capture adapter has a foreign or mixed import origin"
) from error
def _revalidate_runtime_authority(
*,
config: M8L2StudyConfig,
protocol: Path,
expected_source: CaptureSourceIdentity,
expected_runtime: _RuntimeFingerprint,
source_identity_was_injected: bool,
) -> None:
if not source_identity_was_injected:
_assert_loaded_source_root(config.path.parent.parent.resolve())
try:
reloaded = load_m8_l2_config(config.path)
except (OSError, ValueError) as error:
raise M8L2CaptureSystemError(
"frozen live-L2 config changed during session orchestration"
) from error
if reloaded != config or reloaded.hash != config.hash:
raise M8L2CaptureSystemError(
"in-memory live-L2 config differs from its exact frozen byte authority"
)
_assert_protocol(protocol)
if _runtime_fingerprint() != expected_runtime:
raise M8L2CaptureSystemError(
"production runtime fingerprint changed during session orchestration"
)
if source_identity_was_injected:
return
observed = _source_identity(config.path.parent.parent.resolve())
_validate_source_identity(observed)
if observed != expected_source:
raise M8L2CaptureSystemError(
"runtime commit/source-tree identity changed during session orchestration"
)
def merge_observed_intervals(intervals: Sequence[ObservedInterval]) -> tuple[ObservedInterval, ...]:
"""Merge overlap only within the same continuity epoch and return time order."""
by_continuity: dict[str, list[ObservedInterval]] = {}
for interval in intervals:
by_continuity.setdefault(interval.continuity_id, []).append(interval)
merged: list[ObservedInterval] = []
for continuity_id, values in by_continuity.items():
ordered = sorted(
values, key=lambda item: (item.start_received_ns, item.end_received_ns_exclusive)
)
current_start: int | None = None
current_end: int | None = None
for item in ordered:
if current_start is None or current_end is None:
current_start = item.start_received_ns
current_end = item.end_received_ns_exclusive
elif item.start_received_ns <= current_end:
current_end = max(current_end, item.end_received_ns_exclusive)
else:
merged.append(ObservedInterval(continuity_id, current_start, current_end))
current_start = item.start_received_ns
current_end = item.end_received_ns_exclusive
if current_start is not None and current_end is not None:
merged.append(ObservedInterval(continuity_id, current_start, current_end))
return tuple(
sorted(
merged,
key=lambda item: (
item.start_received_ns,
item.end_received_ns_exclusive,
item.continuity_id,
),
)
)
def _numeric_union(intervals: Sequence[ObservedInterval]) -> list[tuple[int, int]]:
ordered = sorted(
(
(item.start_received_ns, item.end_received_ns_exclusive)
for item in merge_observed_intervals(intervals)
),
key=lambda item: item,
)
result: list[tuple[int, int]] = []
for start, end in ordered:
if result and start <= result[-1][1]:
result[-1] = (result[-1][0], max(result[-1][1], end))
else:
result.append((start, end))
return result
def overlapping_observed_coverage_ns(
left: Sequence[ObservedInterval], right: Sequence[ObservedInterval]
) -> int:
"""Return exact union-intersection coverage without bridging either feed's gaps."""
left_union = _numeric_union(left)
right_union = _numeric_union(right)
left_index = 0
right_index = 0
total = 0
while left_index < len(left_union) and right_index < len(right_union):
left_start, left_end = left_union[left_index]
right_start, right_end = right_union[right_index]
total += max(0, min(left_end, right_end) - max(left_start, right_start))
if left_end <= right_end:
left_index += 1
else:
right_index += 1
return total
def _session_identity(
*,
config: M8L2StudyConfig,
session: M8L2Session,
source: CaptureSourceIdentity,
campaign_authority_sha256: str,
) -> str:
if not _is_lower_sha256(campaign_authority_sha256):
raise M8L2CaptureSystemError("session campaign authority digest is invalid")
return _stable_sha256(
{
"schema_version": _SESSION_SCHEMA_VERSION,
"config_sha256": config.hash,
"config_source_sha256": config.source_sha256,
"protocol_sha256": M8_L2_PROTOCOL_SHA256,
"protocol_freeze_commit": M8_L2_FREEZE_COMMIT,
"runtime_commit": source.commit,
"runtime_source_tree_sha256": source.source_tree_sha256,
"campaign_authority_sha256": campaign_authority_sha256,
"date": session.date.isoformat(),
"role": session.role,
"scheduled_start_ns": session.start_ns,
"scheduled_end_ns": session.end_ns,
}
)
def _terminal_root(output_root: Path, session: M8L2Session, session_id: str) -> Path:
return output_root / "sessions" / f"{session.date.isoformat()}-{session.role}-{session_id[:20]}"
def _directory_identity(path: Path, *, label: str) -> tuple[int, int]:
try:
metadata = path.lstat()
except OSError as error:
raise M8L2CaptureSystemError(f"cannot inspect {label}: {path}") from error
if not stat.S_ISDIR(metadata.st_mode):
raise M8L2CaptureSystemError(f"{label} is not a non-symlink directory: {path}")
return metadata.st_dev, metadata.st_ino
def _reject_existing_symlink_components(path: Path, *, label: str) -> None:
absolute = path.absolute()
current = Path(absolute.anchor)
for part in absolute.parts[1:]:
current /= part
if not current.exists() and not current.is_symlink():
continue
try:
if stat.S_ISLNK(current.lstat().st_mode):
raise M8L2CaptureSystemError(f"{label} traverses a symlink: {current}")
except OSError as error:
raise M8L2CaptureSystemError(f"cannot inspect {label}: {current}") from error
def _prepare_output_root(output_root: str | Path) -> tuple[Path, tuple[int, int], tuple[int, int]]:
requested = Path(output_root)
_reject_existing_symlink_components(requested, label="live-L2 output root")
requested.mkdir(parents=True, exist_ok=True)
root = requested.resolve()
root_identity = _directory_identity(root, label="live-L2 output root")
sessions = root / "sessions"
if sessions.is_symlink():
raise M8L2CaptureSystemError("live-L2 sessions directory must not be a symlink")
sessions.mkdir(exist_ok=True)
sessions_identity = _directory_identity(sessions, label="live-L2 sessions directory")
return root, root_identity, sessions_identity
def _revalidate_output_layout(
root: Path,
*,
root_identity: tuple[int, int],
sessions_identity: tuple[int, int],
target: Path,
) -> None:
if _directory_identity(root, label="live-L2 output root") != root_identity:
raise M8L2CaptureSystemError("live-L2 output root identity changed")
sessions = root / "sessions"
if _directory_identity(sessions, label="live-L2 sessions directory") != sessions_identity:
raise M8L2CaptureSystemError("live-L2 sessions directory identity changed")
if target.parent != sessions or target.absolute().parent != sessions.absolute():
raise M8L2CaptureSystemError("live-L2 target escapes its sessions directory")
def _assert_protocol(path: Path) -> str:
if path.is_symlink() or not path.is_file():
raise M8L2CaptureSystemError(f"frozen live-L2 protocol does not exist: {path}")
observed = sha256_file(path)
if observed != M8_L2_PROTOCOL_SHA256:
raise M8L2CaptureSystemError("live-L2 protocol bytes differ from the outcome-blind freeze")
return observed
def _assert_no_symlink_components(path: Path, root: Path, label: str) -> None:
try:
relative = path.relative_to(root)
except ValueError as error:
raise M8L2CaptureSystemError(f"{label} escapes its evidence root: {path}") from error
current = root
for part in relative.parts:
current = current / part
try:
mode = current.lstat().st_mode
except OSError as error:
raise M8L2CaptureSystemError(f"cannot inspect {label}: {current}") from error
if stat.S_ISLNK(mode):
raise M8L2CaptureSystemError(f"{label} contains a symlink: {current}")
def _assert_regular_inside(path: Path, root: Path, label: str) -> Path:
absolute_root = root.absolute()
absolute_path = path.absolute()
_assert_no_symlink_components(absolute_path, absolute_root, label)
try:
mode = absolute_path.lstat().st_mode
except OSError as error:
raise M8L2CaptureSystemError(f"cannot inspect {label}: {path}") from error
if not stat.S_ISREG(mode):
raise M8L2CaptureSystemError(f"{label} is not a regular file: {path}")
resolved = absolute_path.resolve()
try:
resolved.relative_to(root.resolve())
except ValueError as error:
raise M8L2CaptureSystemError(f"{label} escapes its symbol stage: {path}") from error
return resolved
def _relative(path: Path, root: Path) -> str:
return path.absolute().relative_to(root.absolute()).as_posix()
def _walk_regular_evidence(root: Path) -> tuple[tuple[Path, ...], tuple[Path, ...]]:
"""Walk evidence without following links and reject every special entry."""
try:
root_mode = root.lstat().st_mode
except OSError as error:
raise M8L2CaptureSystemError(f"cannot inspect session evidence root: {root}") from error
if not stat.S_ISDIR(root_mode):
raise M8L2CaptureSystemError(f"session evidence root is not a regular directory: {root}")
files: list[Path] = []
directories: list[Path] = [root]
pending = [root]
while pending:
directory = pending.pop()
try:
entries = tuple(os.scandir(directory))
except OSError as error:
raise M8L2CaptureSystemError(
f"cannot enumerate session evidence: {directory}"
) from error
for entry in entries:
path = Path(entry.path)
try:
mode = entry.stat(follow_symlinks=False).st_mode
except OSError as error:
raise M8L2CaptureSystemError(f"cannot inspect session evidence: {path}") from error
if stat.S_ISREG(mode):
files.append(path)
elif stat.S_ISDIR(mode):
directories.append(path)
pending.append(path)
else:
raise M8L2CaptureSystemError(
f"session evidence contains a non-regular filesystem entry: {path}"
)
return (
tuple(sorted(files, key=lambda item: _relative(item, root))),
tuple(sorted(directories, key=lambda item: _relative(item, root))),
)
def _all_regular_files(root: Path) -> tuple[Path, ...]:
return _walk_regular_evidence(root)[0]
def _canonical_json_bytes(value: object) -> bytes:
return (json.dumps(value, indent=2, sort_keys=True, allow_nan=False) + "\n").encode()
def _decode_canonical_json(raw: bytes, *, label: str) -> object:
try:
text = raw.decode("utf-8")
def reject_duplicate(pairs: list[tuple[str, object]]) -> dict[str, object]:
result: dict[str, object] = {}
for key, value in pairs:
if key in result:
raise ValueError(f"duplicate JSON key: {key}")
result[key] = value
return result
value = json.loads(
text,
object_pairs_hook=reject_duplicate,
parse_constant=lambda token: (_ for _ in ()).throw(
ValueError(f"invalid JSON number: {token}")
),
)
except (UnicodeError, ValueError, json.JSONDecodeError) as error:
raise M8L2CaptureSystemError(f"cannot parse canonical {label}") from error
if raw != _canonical_json_bytes(value):
raise M8L2CaptureSystemError(f"{label} is not canonical stable JSON")
return value
def _read_bounded_regular_nofollow(
path: Path, *, label: str, maximum_bytes: int = _MAX_JSON_AUTHORITY_BYTES
) -> bytes:
"""Read one stable regular file through a no-follow descriptor."""
flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)
try:
descriptor = os.open(path, flags)
except OSError as error:
raise M8L2CaptureSystemError(
f"cannot open {label} without following links: {path}"
) from error
try:
before = os.fstat(descriptor)
if not stat.S_ISREG(before.st_mode) or before.st_size < 1 or before.st_size > maximum_bytes:
raise M8L2CaptureSystemError(f"{label} is not a bounded nonempty regular file: {path}")
chunks: list[bytes] = []
remaining = maximum_bytes + 1
while remaining:
chunk = os.read(descriptor, min(64 * 1024, remaining))
if not chunk:
break
chunks.append(chunk)
remaining -= len(chunk)
raw = b"".join(chunks)
after = os.fstat(descriptor)
current = path.lstat()
stable_coordinates = (
before.st_dev,
before.st_ino,
before.st_size,
before.st_mtime_ns,
before.st_ctime_ns,
) == (
after.st_dev,
after.st_ino,
after.st_size,
after.st_mtime_ns,
after.st_ctime_ns,
)
path_still_names_descriptor = (
current.st_dev,
current.st_ino,
current.st_size,
) == (
after.st_dev,
after.st_ino,
after.st_size,
) and stat.S_ISREG(current.st_mode)
if (
len(raw) > maximum_bytes
or len(raw) != before.st_size
or not stable_coordinates
or not path_still_names_descriptor
):
raise M8L2CaptureSystemError(f"{label} changed during its bounded read: {path}")
return raw
except OSError as error:
raise M8L2CaptureSystemError(f"cannot read {label}: {path}") from error
finally:
os.close(descriptor)
def _strict_json(path: Path, *, label: str) -> object:
"""Read a bounded canonical JSON authority with duplicate/NaN rejection."""
raw = _read_bounded_regular_nofollow(path, label=label)
return _decode_canonical_json(raw, label=f"{label}: {path}")
def _validate_symbol_result(
result: SymbolCaptureResult,
*,
expected_symbol: str,
symbol_root: Path,
session: M8L2Session,
) -> SymbolCaptureResult:
if result.symbol != expected_symbol:
raise M8L2CaptureSystemError(
f"capture result symbol {result.symbol!r} does not match {expected_symbol!r}"
)
if not result.capture_id or "/" in result.capture_id or "\\" in result.capture_id:
raise M8L2CaptureSystemError("single-symbol capture returned an unsafe capture_id")
for name in (
"messages",
"normalized_rows",
"reconstructed_rows",
"excluded_rows",
"continuity_epochs",
"snapshot_anchors",
"sequence_gaps",
"quality_errors",
"quality_warnings",
"max_raw_frame_bytes_observed",
"max_arrow_batch_bytes_observed",
):
value = getattr(result, name)
if isinstance(value, bool) or not isinstance(value, int) or value < 0:
raise M8L2CaptureSystemError(f"capture result {name} must be a nonnegative integer")
if result.status == "FAILED":
if result.failure_reason_code is None or result.failure_phase is None:
raise M8L2CaptureSystemError("FAILED capture result lacks typed failure coordinates")
_validate_code(result.failure_reason_code, "capture failure reason")
_validate_code(result.failure_phase, "capture failure phase")
elif result.failure_reason_code is not None or result.failure_phase is not None:
raise M8L2CaptureSystemError("COMPLETE capture result cannot carry a failure")
if (result.first_raw_received_ns is None) != (result.last_raw_received_ns is None):
raise M8L2CaptureSystemError("raw receipt bounds must be both null or both present")
if (
result.first_raw_received_ns is not None
and result.last_raw_received_ns is not None
and not (
session.start_ns
<= result.first_raw_received_ns
<= result.last_raw_received_ns
< session.end_ns
)
):
raise M8L2CaptureSystemError(
"raw receipt bounds fall outside the frozen [start, end) session"
)
merged = merge_observed_intervals(result.valid_observed_intervals)
if tuple(result.valid_observed_intervals) != merged:
raise M8L2CaptureSystemError(
"OBSERVED intervals must already be canonical, disjoint, and time ordered"
)
continuity_ids = {item.continuity_id for item in merged}
if len(continuity_ids) > result.continuity_epochs:
raise M8L2CaptureSystemError(
"OBSERVED continuity IDs exceed the declared reconstruction epochs"
)
for interval in merged:
if not (
session.start_ns
<= interval.start_received_ns
< interval.end_received_ns_exclusive
<= session.end_ns
):
raise M8L2CaptureSystemError(
"OBSERVED interval falls outside the frozen [start, end) session"
)
if result.first_raw_received_ns is None or result.last_raw_received_ns is None:
raise M8L2CaptureSystemError("OBSERVED intervals require raw receipt bounds")
if (
interval.start_received_ns < result.first_raw_received_ns
or interval.end_received_ns_exclusive > result.last_raw_received_ns + 1
):
raise M8L2CaptureSystemError("OBSERVED interval exceeds its raw receipt evidence")
declared: dict[str, CapturedArtifact] = {}
for artifact in result.artifacts:
if not artifact.kind:
raise M8L2CaptureSystemError("captured artifact kind must not be empty")
if not _is_lower_sha256(artifact.sha256):
raise M8L2CaptureSystemError("captured artifact digest is not lowercase SHA-256")
path = _assert_regular_inside(artifact.path, symbol_root, "captured artifact")
relative = _relative(path, symbol_root)
if relative in declared:
raise M8L2CaptureSystemError(f"duplicate captured artifact coordinate: {relative}")
if sha256_file(path) != artifact.sha256:
raise M8L2CaptureSystemError(f"captured artifact digest mismatch: {relative}")
declared[relative] = artifact
actual = {_relative(path, symbol_root) for path in _all_regular_files(symbol_root)}
if actual != set(declared):
raise M8L2CaptureSystemError(
"single-symbol capture artifact inventory differs from files on disk "
f"(missing={sorted(actual - set(declared))}, extra={sorted(set(declared) - actual)})"
)
if any(item.kind == "capture_summary" for item in result.artifacts):
_validate_capture_summary(result, symbol_root=symbol_root, session=session)
return result
def _gate(
gates: list[dict[str, object]],
*,
gate_id: str,
passed: bool,
observed: object,
required: object,
symbol: str | None = None,
) -> None:
entry: dict[str, object] = {
"gate_id": gate_id,
"passed": passed,
"observed": observed,
"required": required,
}
if symbol is not None:
entry["symbol"] = symbol
gates.append(entry)
def _symbol_payload(result: SymbolCaptureResult, *, stage_root: Path) -> dict[str, object]:
intervals = merge_observed_intervals(result.valid_observed_intervals)
return {
"symbol": result.symbol,
"capture_id": result.capture_id,
"status": result.status,
"completion_reason": result.completion_reason,
"reconstruction_status": result.reconstruction_status,
"messages": result.messages,
"normalized_rows": result.normalized_rows,
"reconstructed_rows": result.reconstructed_rows,
"excluded_rows": result.excluded_rows,
"continuity_epochs": result.continuity_epochs,
"snapshot_anchors": result.snapshot_anchors,
"sequence_gaps": result.sequence_gaps,
"quality_errors": result.quality_errors,
"quality_warnings": result.quality_warnings,
"max_raw_frame_bytes_observed": result.max_raw_frame_bytes_observed,
"max_arrow_batch_bytes_observed": result.max_arrow_batch_bytes_observed,
"first_raw_received_ns": result.first_raw_received_ns,
"last_raw_received_ns": result.last_raw_received_ns,
"valid_observed_intervals": [item.to_dict() for item in intervals],
"max_valid_continuity_epoch_seconds": max(
(item.duration_ns for item in intervals), default=0
)
/ _NANOSECONDS_PER_SECOND,
"failure_reason_code": result.failure_reason_code,
"failure_phase": result.failure_phase,
"artifacts": [
{
"path": _relative(artifact.path, stage_root),
"kind": artifact.kind,
"sha256": artifact.sha256,
"bytes": artifact.path.stat().st_size,
}
for artifact in sorted(
result.artifacts, key=lambda item: _relative(item.path, stage_root)
)
],
}
def _summary_artifact_map(
result: SymbolCaptureResult, *, symbol_root: Path
) -> dict[str, CapturedArtifact]:
return {
_relative(item.path, symbol_root): item
for item in result.artifacts
if item.kind != "capture_summary"
}
def _require_summary_reference(
value: object,
*,
artifacts: Mapping[str, CapturedArtifact],
kind: str,
label: str,
) -> CapturedArtifact:
if type(value) is not str:
raise M8L2CaptureSystemError(f"capture summary {label} must be a relative path")
relative = _safe_checksum_relative(value)
artifact = artifacts.get(relative)
if artifact is None or artifact.kind != kind:
raise M8L2CaptureSystemError(
f"capture summary {label} does not name a declared {kind} artifact"
)
return artifact
def _load_compact_json_line(raw: bytes, *, label: str) -> Mapping[str, Any]:
try:
text = raw.decode("utf-8")
def reject_duplicate(pairs: list[tuple[str, object]]) -> dict[str, object]:
result: dict[str, object] = {}
for key, value in pairs:
if key in result:
raise ValueError(f"duplicate JSON key: {key}")
result[key] = value
return result
value = json.loads(
text,
object_pairs_hook=reject_duplicate,
parse_constant=lambda token: (_ for _ in ()).throw(
ValueError(f"invalid JSON number: {token}")
),
)
except (UnicodeError, ValueError, json.JSONDecodeError) as error:
raise M8L2CaptureSystemError(f"cannot parse {label}") from error
if not isinstance(value, Mapping) or not all(type(key) is str for key in value):
raise M8L2CaptureSystemError(f"{label} must be a JSON object")
canonical = json.dumps(value, sort_keys=True, separators=(",", ":"), allow_nan=False).encode()
if raw != canonical:
raise M8L2CaptureSystemError(f"{label} is not canonical compact JSON")
return cast(Mapping[str, Any], value)
def _validate_raw_journal(
result: SymbolCaptureResult,
*,
journal: CapturedArtifact,
artifacts: Mapping[str, CapturedArtifact],
session: M8L2Session,
) -> None:
websocket_receipts: list[int] = []
max_frame_bytes = 0
anchor_ids: set[str] = set()
anchor_count = 0
try:
with journal.path.open("rb") as handle:
line_number = 0
while True:
line = handle.readline(2 * _MAX_JSON_AUTHORITY_BYTES + 1)
if not line:
break
line_number += 1
if len(line) > 2 * _MAX_JSON_AUTHORITY_BYTES or not line.endswith(b"\n"):
raise M8L2CaptureSystemError("raw journal contains an oversized/partial line")
entry = _load_compact_json_line(line[:-1], label=f"raw journal line {line_number}")
event_kind = entry.get("event_kind")
if event_kind == "websocket_frame":
received = entry.get("received_ts_ns")
payload_bytes = entry.get("payload_bytes")
payload_base64 = entry.get("payload_base64")
payload_sha256 = entry.get("payload_sha256")
if (
type(received) is not int
or not session.start_ns <= received < session.end_ns
or type(payload_bytes) is not int
or payload_bytes < 0
or type(payload_base64) is not str
or type(payload_sha256) is not str
or not _is_lower_sha256(payload_sha256)
):
raise M8L2CaptureSystemError("raw websocket lineage has invalid bounds")
try:
decoded = base64.b64decode(payload_base64, validate=True)
except (ValueError, binascii.Error) as error:
raise M8L2CaptureSystemError(
"raw websocket lineage has invalid base64 bytes"
) from error
if len(decoded) != payload_bytes or hashlib.sha256(decoded).hexdigest() != (
payload_sha256
):
raise M8L2CaptureSystemError(
"raw websocket lineage size/digest differs from preserved bytes"
)
if websocket_receipts and received < websocket_receipts[-1]:
raise M8L2CaptureSystemError("raw websocket receipts are not FIFO ordered")
websocket_receipts.append(received)
max_frame_bytes = max(max_frame_bytes, payload_bytes)
elif event_kind == "rest_snapshot_anchor":
continuity_id = entry.get("continuity_id")
if type(continuity_id) is not str or not continuity_id:
raise M8L2CaptureSystemError("raw snapshot anchor lacks continuity ID")
if continuity_id in anchor_ids:
raise M8L2CaptureSystemError(
"raw journal repeats a snapshot anchor continuity ID"
)
anchor_ids.add(continuity_id)
anchor_count += 1
raw_snapshot = _require_summary_reference(
entry.get("raw_path"),
artifacts=artifacts,
kind="raw_snapshot",
label="raw journal snapshot path",
)
raw_snapshot_manifest = _require_summary_reference(
entry.get("raw_manifest_path"),
artifacts=artifacts,
kind="raw_snapshot_manifest",
label="raw journal snapshot manifest path",
)
if (
entry.get("raw_sha256") != raw_snapshot.sha256
or entry.get("raw_manifest_sha256") != raw_snapshot_manifest.sha256
):
raise M8L2CaptureSystemError("raw snapshot anchor digest is inconsistent")
else:
raise M8L2CaptureSystemError("raw journal has an unsupported event kind")
except OSError as error:
raise M8L2CaptureSystemError("cannot stream the raw journal authority") from error
if len(websocket_receipts) != result.messages:
raise M8L2CaptureSystemError("raw journal message count differs from returned claims")
observed_first = websocket_receipts[0] if websocket_receipts else None
observed_last = websocket_receipts[-1] if websocket_receipts else None
if (
observed_first != result.first_raw_received_ns
or observed_last != result.last_raw_received_ns
or max_frame_bytes != result.max_raw_frame_bytes_observed
):
raise M8L2CaptureSystemError("raw journal receipt/frame claims are inconsistent")
if anchor_count != result.snapshot_anchors or anchor_count != result.continuity_epochs:
raise M8L2CaptureSystemError("raw journal anchors differ from reconstruction epochs")
if not {item.continuity_id for item in result.valid_observed_intervals}.issubset(anchor_ids):
raise M8L2CaptureSystemError("OBSERVED intervals lack a matching raw snapshot anchor")
def _validate_capture_summary(
result: SymbolCaptureResult, *, symbol_root: Path, session: M8L2Session
) -> None:
summaries = [item for item in result.artifacts if item.kind == "capture_summary"]
if len(summaries) != 1:
raise M8L2CaptureSystemError("capture result must declare exactly one capture summary")
summary_artifact = summaries[0]
if _relative(summary_artifact.path, symbol_root) != "quality/capture.summary.json":
raise M8L2CaptureSystemError("capture summary path is not the canonical coordinate")
payload = _strict_json(summary_artifact.path, label="symbol capture summary")
if not isinstance(payload, Mapping) or not all(type(key) is str for key in payload):
raise M8L2CaptureSystemError("symbol capture summary must be a JSON object")
summary = cast(Mapping[str, Any], payload)
expected_claims: dict[str, object] = {
"schema_version": _SYMBOL_SUMMARY_SCHEMA_VERSION,
"symbol": result.symbol,
"capture_id": result.capture_id,
"capture_status": result.status,
"completion_reason": result.completion_reason,
"reconstruction_status": result.reconstruction_status,
"failure_reason_code": result.failure_reason_code,
"failure_phase": result.failure_phase,
"messages": result.messages,
"normalized_rows": result.normalized_rows,
"reconstructed_rows": result.reconstructed_rows,
"excluded_rows": result.excluded_rows,
"continuity_epochs": result.continuity_epochs,
"snapshot_anchors": result.snapshot_anchors,
"sequence_gaps": result.sequence_gaps,
"quality_errors": result.quality_errors,
"quality_warnings": result.quality_warnings,
"max_raw_frame_bytes_observed": result.max_raw_frame_bytes_observed,
"max_arrow_batch_bytes_observed": result.max_arrow_batch_bytes_observed,
"first_raw_received_ns": result.first_raw_received_ns,
"last_raw_received_ns": result.last_raw_received_ns,
"valid_observed_intervals": [item.to_dict() for item in result.valid_observed_intervals],
"scheduled_range_ns": {
"start": session.start_ns,
"end_exclusive": session.end_ns,
},
}
mismatches = [
name for name, expected in expected_claims.items() if summary.get(name) != expected
]
if mismatches:
raise M8L2CaptureSystemError(
"symbol capture summary differs from returned claims: " + ", ".join(mismatches)
)
artifacts = _summary_artifact_map(result, symbol_root=symbol_root)
inventory_raw = summary.get("artifact_inventory_without_summary")
if not isinstance(inventory_raw, list):
raise M8L2CaptureSystemError(
"symbol capture summary lacks artifact_inventory_without_summary"
)
summary_inventory: dict[str, tuple[str, str, int]] = {}
for index, raw in enumerate(inventory_raw):
if not isinstance(raw, Mapping) or set(raw) != {"path", "kind", "sha256", "bytes"}:
raise M8L2CaptureSystemError(
f"symbol capture summary inventory entry {index} is malformed"
)
relative_value = raw.get("path")
if type(relative_value) is not str:
raise M8L2CaptureSystemError("symbol capture summary inventory path is invalid")
relative = _safe_checksum_relative(relative_value)
kind = raw.get("kind")
digest = raw.get("sha256")
size = raw.get("bytes")
if (
type(kind) is not str
or type(digest) is not str
or not _is_lower_sha256(digest)
or type(size) is not int
or size < 0
or relative in summary_inventory
):
raise M8L2CaptureSystemError("symbol capture summary inventory entry is invalid")
summary_inventory[relative] = (kind, digest, size)
expected_inventory = {
relative: (item.kind, item.sha256, item.path.stat().st_size)
for relative, item in artifacts.items()
}
if summary_inventory != expected_inventory or list(summary_inventory) != sorted(
summary_inventory
):
raise M8L2CaptureSystemError(
"symbol capture summary artifact inventory is not exact and canonical"
)
raw_journal = _require_summary_reference(
summary.get("raw_journal"),
artifacts=artifacts,
kind="raw_journal",
label="raw_journal",
)
raw_manifest = _require_summary_reference(
summary.get("raw_journal_manifest"),
artifacts=artifacts,
kind="raw_journal_manifest",
label="raw_journal_manifest",
)
if (
summary.get("raw_journal_sha256") != raw_journal.sha256
or summary.get("raw_journal_manifest_sha256") != raw_manifest.sha256
):
raise M8L2CaptureSystemError("symbol capture summary raw digests are inconsistent")
_validate_raw_journal(
result,
journal=raw_journal,
artifacts=artifacts,
session=session,
)
raw_manifest_payload = _strict_json(raw_manifest.path, label="raw journal manifest")
if not isinstance(raw_manifest_payload, Mapping):
raise M8L2CaptureSystemError("raw journal manifest must be a JSON object")
raw_checksum = raw_manifest_payload.get("checksum")
raw_range = raw_manifest_payload.get("requested_range_ns")
raw_headers = raw_manifest_payload.get("response_headers")
if (
raw_manifest_payload.get("artifact_kind") != "raw_source"
or not isinstance(raw_checksum, Mapping)
or raw_checksum.get("algorithm") != "sha256"
or raw_checksum.get("value") != raw_journal.sha256
or raw_manifest_payload.get("bytes") != raw_journal.path.stat().st_size
or raw_manifest_payload.get("path") != raw_journal.path.name
or raw_range != {"start": session.start_ns, "end_exclusive": session.end_ns}
or not isinstance(raw_headers, Mapping)
or raw_headers.get("x-local-message-count") != str(result.messages)
or raw_headers.get("x-local-snapshot-anchor-count") != str(result.snapshot_anchors)
):
raise M8L2CaptureSystemError("raw journal manifest differs from capture claims")
datasets_raw = summary.get("normalized_dataset_manifests")
expected_rows = {
"book_snapshots": result.snapshot_anchors,
"depth_deltas": result.normalized_rows,
"book_observations": result.reconstructed_rows,
"sequence_gaps": result.sequence_gaps,
}
if not isinstance(datasets_raw, Mapping) or set(datasets_raw) != set(expected_rows):
raise M8L2CaptureSystemError("symbol capture summary normalized manifests are incomplete")
for dataset, expected_row_count in expected_rows.items():
raw_entry = datasets_raw[dataset]
if not isinstance(raw_entry, Mapping):
raise M8L2CaptureSystemError("normalized dataset summary entry must be an object")
manifest = _require_summary_reference(
raw_entry.get("manifest_path"),
artifacts=artifacts,
kind="normalized_manifest",
label=f"normalized_dataset_manifests.{dataset}.manifest_path",
)
if raw_entry.get("manifest_sha256") != manifest.sha256:
raise M8L2CaptureSystemError("normalized dataset manifest digest is inconsistent")
if raw_entry.get("rows") != expected_row_count:
raise M8L2CaptureSystemError("normalized dataset row count differs from claims")
data_path = raw_entry.get("data_path")
data_sha = raw_entry.get("data_sha256")
if expected_row_count == 0:
if data_path is not None or data_sha is not None:
raise M8L2CaptureSystemError("empty normalized dataset unexpectedly has data")
else:
data = _require_summary_reference(
data_path,
artifacts=artifacts,
kind="normalized_data",
label=f"normalized_dataset_manifests.{dataset}.data_path",
)
if data_sha != data.sha256:
raise M8L2CaptureSystemError("normalized dataset digest is inconsistent")
_validate_normalized_parquet(
data.path,
dataset=dataset,
expected_rows=expected_row_count,
)
normalized_manifest_payload = _strict_json(
manifest.path, label=f"{dataset} normalized manifest"
)
if not isinstance(normalized_manifest_payload, Mapping):
raise M8L2CaptureSystemError("normalized dataset manifest must be an object")
if (
normalized_manifest_payload.get("dataset") != dataset
or normalized_manifest_payload.get("rows") != expected_row_count
or normalized_manifest_payload.get("requested_range_ns")
!= {"start": session.start_ns, "end_exclusive": session.end_ns}
):
raise M8L2CaptureSystemError("normalized manifest essentials differ from claims")
reports_raw = summary.get("quality_reports")
if not isinstance(reports_raw, Mapping) or set(reports_raw) != {
"depth_deltas",
"book_observations",
}:
raise M8L2CaptureSystemError("symbol capture summary quality reports are incomplete")
report_counts = {"errors": 0, "warnings": 0}
for dataset in ("depth_deltas", "book_observations"):
report = _require_summary_reference(
reports_raw[dataset],
artifacts=artifacts,
kind="quality_report",
label=f"quality_reports.{dataset}",
)
report_payload = _strict_json(report.path, label=f"{dataset} quality report")
if not isinstance(report_payload, Mapping) or report_payload.get("dataset") != dataset:
raise M8L2CaptureSystemError("quality report dataset differs from its coordinate")
report_summary = report_payload.get("summary")
if not isinstance(report_summary, Mapping):
raise M8L2CaptureSystemError("quality report lacks its summary counts")
for name in report_counts:
value = report_summary.get(name)
if type(value) is not int or value < 0:
raise M8L2CaptureSystemError("quality report count is invalid")
report_counts[name] += value
if report_counts != {"errors": result.quality_errors, "warnings": result.quality_warnings}:
raise M8L2CaptureSystemError("quality report counts differ from returned claims")
def _validate_normalized_parquet(path: Path, *, dataset: str, expected_rows: int) -> None:
"""Bound footer reads before PyArrow metadata/schema validation."""
try:
size = path.stat().st_size
if size < 12:
raise M8L2CaptureSystemError("normalized data is too short to be Parquet")
flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)
descriptor = os.open(path, flags)
try:
if not stat.S_ISREG(os.fstat(descriptor).st_mode):
raise M8L2CaptureSystemError("normalized data is not a regular file")
if os.pread(descriptor, 4, 0) != b"PAR1":
raise M8L2CaptureSystemError("normalized data lacks the Parquet header")
trailer = os.pread(descriptor, 8, size - 8)
if len(trailer) != 8 or trailer[4:] != b"PAR1":
raise M8L2CaptureSystemError("normalized data lacks the Parquet trailer")
footer_bytes = int.from_bytes(trailer[:4], "little")
if footer_bytes > _MAX_JSON_AUTHORITY_BYTES or footer_bytes + 12 > size:
raise M8L2CaptureSystemError("normalized Parquet footer exceeds its bound")
finally:
os.close(descriptor)
parquet = pq.ParquetFile(path)
if parquet.metadata.num_rows != expected_rows:
raise M8L2CaptureSystemError("Parquet footer row count differs from capture claims")
if parquet.schema_arrow != get_schema(dataset):
raise M8L2CaptureSystemError("normalized Parquet schema differs from the registry")
metadata = parquet.schema_arrow.metadata or {}
if metadata.get(b"schema_version") != SCHEMA_VERSION.encode():
raise M8L2CaptureSystemError("normalized Parquet schema version is not frozen")
except M8L2CaptureSystemError:
raise
except (OSError, ValueError) as error:
raise M8L2CaptureSystemError("cannot validate normalized Parquet metadata") from error
def _evaluate_gates(
results: Mapping[str, SymbolCaptureResult],
*,
config: M8L2StudyConfig,
) -> tuple[list[dict[str, object]], list[str], int]:
limits = config.capture
gates: list[dict[str, object]] = []
reasons: list[str] = []
for symbol in config.study.symbols:
result = results.get(symbol)
if result is None:
reasons.append(f"MISSING_SYMBOL_{symbol}")
_gate(
gates,
gate_id="SYMBOL_RESULT_PRESENT",
passed=False,
observed=False,
required=True,
symbol=symbol,
)
continue
artifact_kinds = {artifact.kind for artifact in result.artifacts}
snapshot_artifact_count = sum(
artifact.kind == "raw_snapshot" for artifact in result.artifacts
)
snapshot_manifest_count = sum(
artifact.kind == "raw_snapshot_manifest" for artifact in result.artifacts
)
checks: tuple[tuple[str, bool, object, object], ...] = (
(
"CAPTURE_STATUS_COMPLETE",
(not limits.require_complete_status) or result.status == "COMPLETE",
result.status,
"COMPLETE",
),
(
"SCHEDULED_END_REACHED",
result.completion_reason == "scheduled_end_reached",
result.completion_reason,
"scheduled_end_reached",
),
(
"RAW_RECEIPT_BOUNDS_PRESENT",
result.first_raw_received_ns is not None
and result.last_raw_received_ns is not None,
{
"first": result.first_raw_received_ns,
"last": result.last_raw_received_ns,
},
"both present inside [start,end)",
),
(
"MESSAGE_CEILING",
result.messages <= limits.max_messages_per_symbol,
result.messages,
{"maximum": limits.max_messages_per_symbol},
),
(
"MESSAGE_NORMALIZATION_RECONCILIATION",
result.messages == result.normalized_rows
and result.normalized_rows == result.reconstructed_rows + result.excluded_rows,
{
"messages": result.messages,
"normalized": result.normalized_rows,
"reconstructed": result.reconstructed_rows,
"excluded": result.excluded_rows,
},
"messages == normalized == reconstructed + excluded",
),
(
"SNAPSHOT_ANCHOR_RECONCILIATION",
result.continuity_epochs > 0
and result.snapshot_anchors == result.continuity_epochs,
{
"epochs": result.continuity_epochs,
"snapshot_anchors": result.snapshot_anchors,
},
"positive epochs and one anchor per epoch",
),
(
"LIVE_RECONSTRUCTION",
(not limits.require_live_reconstruction) or result.reconstruction_status == "LIVE",
result.reconstruction_status,
"LIVE",
),
(
"SEQUENCE_GAPS",
result.sequence_gaps <= limits.max_sequence_gaps,
result.sequence_gaps,
{"maximum": limits.max_sequence_gaps},
),
(
"QUALITY_ERRORS",
result.quality_errors <= limits.max_quality_errors,
result.quality_errors,
{"maximum": limits.max_quality_errors},
),
(
"QUALITY_WARNINGS",
result.quality_warnings <= limits.max_quality_warnings,
result.quality_warnings,
{"maximum": limits.max_quality_warnings},
),
(
"RAW_FRAME_BYTES",
result.max_raw_frame_bytes_observed <= limits.max_raw_frame_bytes,
result.max_raw_frame_bytes_observed,
{"maximum": limits.max_raw_frame_bytes},
),
(
"ARROW_BATCH_BYTES",
result.max_arrow_batch_bytes_observed <= limits.max_arrow_batch_bytes,
result.max_arrow_batch_bytes_observed,
{"maximum": limits.max_arrow_batch_bytes},
),
(
"VALID_CONTINUITY_EPOCH",
max(
(
item.duration_ns
for item in merge_observed_intervals(result.valid_observed_intervals)
),
default=0,
)
>= limits.min_single_continuity_epoch_seconds * _NANOSECONDS_PER_SECOND,
max(
(
item.duration_ns
for item in merge_observed_intervals(result.valid_observed_intervals)
),
default=0,
)
/ _NANOSECONDS_PER_SECOND,
{"minimum_seconds": limits.min_single_continuity_epoch_seconds},
),
(
"CAPTURE_ARTIFACTS_PRESENT",
_REQUIRED_CAPTURE_ARTIFACT_KINDS.issubset(artifact_kinds),
{
"kinds": sorted(artifact_kinds),
"raw_snapshots": snapshot_artifact_count,
"raw_snapshot_manifests": snapshot_manifest_count,
},
{
"required_kinds": sorted(_REQUIRED_CAPTURE_ARTIFACT_KINDS),
"snapshot anchors are cross-bound in the raw journal": True,
},
),
)
if result.failure_reason_code is not None:
reasons.append(result.failure_reason_code)
for gate_id, passed, observed, required in checks:
_gate(
gates,
gate_id=gate_id,
passed=passed,
observed=observed,
required=required,
symbol=symbol,
)
if not passed:
reasons.append(f"GATE_{gate_id}_{symbol}")
overlap_ns = 0
if all(symbol in results for symbol in config.study.symbols):
left = results[config.study.symbols[0]].valid_observed_intervals
right = results[config.study.symbols[1]].valid_observed_intervals
overlap_ns = overlapping_observed_coverage_ns(left, right)
overlap_passed = overlap_ns >= limits.min_overlapping_coverage_seconds * _NANOSECONDS_PER_SECOND
_gate(
gates,
gate_id="CROSS_SYMBOL_OBSERVED_OVERLAP",
passed=overlap_passed,
observed=overlap_ns / _NANOSECONDS_PER_SECOND,
required={"minimum_seconds": limits.min_overlapping_coverage_seconds},
)
if not overlap_passed:
reasons.append("GATE_CROSS_SYMBOL_OBSERVED_OVERLAP")
return gates, sorted(set(reasons)), overlap_ns
def _write_bytes_durable(path: Path, payload: bytes) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
descriptor, temporary_name = tempfile.mkstemp(
dir=path.parent, prefix=f".{path.name}.", suffix=".tmp"
)
try:
with os.fdopen(descriptor, "wb") as handle:
handle.write(payload)
handle.flush()
os.fsync(handle.fileno())
os.replace(temporary_name, path)
_fsync_directory(path.parent)
except BaseException:
Path(temporary_name).unlink(missing_ok=True)
raise
def _fsync_directory(path: Path) -> None:
descriptor = os.open(path, os.O_RDONLY)
try:
os.fsync(descriptor)
finally:
os.close(descriptor)
def _campaign_payload(
*,
config: M8L2StudyConfig,
source: CaptureSourceIdentity,
runtime: _RuntimeFingerprint,
nonce: str,
root: Path,
root_identity: tuple[int, int],
) -> dict[str, object]:
_validate_source_identity(source)
if not _is_lower_sha256(nonce):
raise M8L2CaptureSystemError("live-L2 campaign nonce is not 256-bit lowercase hex")
if runtime != _runtime_from_recorded(runtime.payload(), runtime.sha256):
raise M8L2CaptureSystemError("live-L2 runtime fingerprint is not canonical")
return {
"schema_version": _CAMPAIGN_SCHEMA_VERSION,
"artifact_kind": "m8_prospective_live_l2_campaign_authority",
"campaign_nonce": nonce,
"output_root": {
"canonical_path": str(root),
"device": root_identity[0],
"inode": root_identity[1],
},
"config_sha256": config.hash,
"config_source_sha256": config.source_sha256,
"protocol_sha256": M8_L2_PROTOCOL_SHA256,
"protocol_freeze_commit": M8_L2_FREEZE_COMMIT,
"runtime_commit": source.commit,
"runtime_source_tree_sha256": source.source_tree_sha256,
"runtime_dirty": False,
"runtime_fingerprint": runtime.payload(),
"runtime_fingerprint_sha256": runtime.sha256,
}
def _recorded_output_root(value: object) -> tuple[str, int, int]:
if not isinstance(value, Mapping) or set(value) != {
"canonical_path",
"device",
"inode",
}:
raise M8L2CaptureSystemError("campaign output-root identity is not exact")
canonical_path = value.get("canonical_path")
device = value.get("device")
inode = value.get("inode")
if (
type(canonical_path) is not str
or not canonical_path
or "\x00" in canonical_path
or not PurePosixPath(canonical_path).is_absolute()
or ".." in PurePosixPath(canonical_path).parts
or PurePosixPath(canonical_path).as_posix() != canonical_path
):
raise M8L2CaptureSystemError("campaign output-root path is not canonical")
if type(device) is not int or device < 0 or type(inode) is not int or inode < 0:
raise M8L2CaptureSystemError("campaign output-root device/inode is malformed")
return canonical_path, device, inode
def _validate_campaign_bytes(
raw: bytes,
*,
path: Path,
config: M8L2StudyConfig,
source: CaptureSourceIdentity,
expected_runtime: _RuntimeFingerprint | None = None,
expected_root: Path | None = None,
expected_root_identity: tuple[int, int] | None = None,
expected_sha256: str | None = None,
) -> _CampaignAuthority:
value = _decode_canonical_json(raw, label=f"live-L2 campaign authority: {path}")
if not isinstance(value, Mapping) or set(value) != {
"schema_version",
"artifact_kind",
"campaign_nonce",
"output_root",
"config_sha256",
"config_source_sha256",
"protocol_sha256",
"protocol_freeze_commit",
"runtime_commit",
"runtime_source_tree_sha256",
"runtime_dirty",
"runtime_fingerprint",
"runtime_fingerprint_sha256",
}:
raise M8L2CaptureSystemError("live-L2 campaign authority schema is not exact")
expected_claims = {
"schema_version": _CAMPAIGN_SCHEMA_VERSION,
"artifact_kind": "m8_prospective_live_l2_campaign_authority",
"config_sha256": config.hash,
"config_source_sha256": config.source_sha256,
"protocol_sha256": M8_L2_PROTOCOL_SHA256,
"protocol_freeze_commit": M8_L2_FREEZE_COMMIT,
"runtime_commit": source.commit,
"runtime_source_tree_sha256": source.source_tree_sha256,
"runtime_dirty": False,
}
if any(value.get(name) != expected for name, expected in expected_claims.items()):
raise M8L2CaptureSystemError(
"live-L2 campaign authority differs from the frozen configuration/source identity"
)
nonce = value.get("campaign_nonce")
if type(nonce) is not str or not _is_lower_sha256(nonce):
raise M8L2CaptureSystemError("live-L2 campaign nonce is malformed")
output_path, output_device, output_inode = _recorded_output_root(value.get("output_root"))
if (expected_root is None) != (expected_root_identity is None):
raise M8L2CaptureSystemError("campaign root validation coordinates are incomplete")
if (
expected_root is not None
and expected_root_identity is not None
and (
output_path != str(expected_root)
or (output_device, output_inode) != expected_root_identity
)
):
raise M8L2CaptureSystemError(
"live-L2 campaign authority differs from the current output-root identity"
)
runtime = _runtime_from_recorded(
value.get("runtime_fingerprint"), value.get("runtime_fingerprint_sha256")
)
if expected_runtime is not None and runtime != expected_runtime:
raise M8L2CaptureSystemError(
"live-L2 campaign authority differs from the current production runtime"
)
digest = hashlib.sha256(raw).hexdigest()
if expected_sha256 is not None and digest != expected_sha256:
raise M8L2CaptureSystemError("live-L2 campaign authority digest changed")
return _CampaignAuthority(
path=path,
sha256=digest,
raw=raw,
source=source,
runtime=runtime,
nonce=nonce,
output_root_path=output_path,
output_root_device=output_device,
output_root_inode=output_inode,
)
def _verify_campaign_authority(
root: Path,
*,
root_identity: tuple[int, int],
config: M8L2StudyConfig,
source: CaptureSourceIdentity,
runtime: _RuntimeFingerprint,
expected_sha256: str | None = None,
) -> _CampaignAuthority:
if _directory_identity(root, label="live-L2 output root") != root_identity:
raise M8L2CaptureSystemError("live-L2 output root identity changed")
path = root / _CAMPAIGN_AUTHORITY_NAME
_assert_no_symlink_components(path, root, "live-L2 campaign authority")
raw = _read_bounded_regular_nofollow(
path,
label="live-L2 campaign authority",
maximum_bytes=_MAX_CAMPAIGN_AUTHORITY_BYTES,
)
if _directory_identity(root, label="live-L2 output root") != root_identity:
raise M8L2CaptureSystemError("live-L2 output root changed during campaign verification")
return _validate_campaign_bytes(
raw,
path=path,
config=config,
source=source,
expected_runtime=runtime,
expected_root=root,
expected_root_identity=root_identity,
expected_sha256=expected_sha256,
)
def _create_campaign_authority_once(
root: Path, *, root_identity: tuple[int, int], raw: bytes
) -> None:
"""Atomically link one fsynced authority into place without replacement."""
descriptor, temporary_name = tempfile.mkstemp(
dir=root, prefix=".campaign-authority-", suffix=".tmp"
)
temporary = Path(temporary_name)
root_descriptor = -1
try:
with os.fdopen(descriptor, "wb") as handle:
handle.write(raw)
handle.flush()
os.fsync(handle.fileno())
directory_flags = os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_NOFOLLOW", 0)
root_descriptor = os.open(root, directory_flags)
root_metadata = os.fstat(root_descriptor)
if (root_metadata.st_dev, root_metadata.st_ino) != root_identity:
raise M8L2CaptureSystemError(
"live-L2 output root changed before campaign authority creation"
)
with suppress(FileExistsError):
os.link(
temporary.name,
_CAMPAIGN_AUTHORITY_NAME,
src_dir_fd=root_descriptor,
dst_dir_fd=root_descriptor,
follow_symlinks=False,
)
# This fsync makes either our new link or a concurrent creator's link
# durable before either caller may proceed toward a network connection.
os.fsync(root_descriptor)
os.unlink(temporary.name, dir_fd=root_descriptor)
temporary = Path()
os.fsync(root_descriptor)
finally:
if root_descriptor >= 0:
os.close(root_descriptor)
if temporary != Path():
with suppress(OSError):
temporary.unlink(missing_ok=True)
def _ensure_campaign_authority(
root: Path,
*,
root_identity: tuple[int, int],
config: M8L2StudyConfig,
source: CaptureSourceIdentity,
runtime: _RuntimeFingerprint,
) -> _CampaignAuthority:
path = root / _CAMPAIGN_AUTHORITY_NAME
try:
path.lstat()
except FileNotFoundError:
_create_campaign_authority_once(
root,
root_identity=root_identity,
raw=_canonical_json_bytes(
_campaign_payload(
config=config,
source=source,
runtime=runtime,
nonce=secrets.token_hex(32),
root=root,
root_identity=root_identity,
)
),
)
except OSError as error:
raise M8L2CaptureSystemError(
f"cannot inspect live-L2 campaign authority: {path}"
) from error
return _verify_campaign_authority(
root,
root_identity=root_identity,
config=config,
source=source,
runtime=runtime,
)
def _verify_bundled_campaign_authority(
path: Path,
*,
config: M8L2StudyConfig,
source: CaptureSourceIdentity,
expected_sha256: str,
) -> _CampaignAuthority:
raw = _read_bounded_regular_nofollow(
path,
label="bundled live-L2 campaign authority",
maximum_bytes=_MAX_CAMPAIGN_AUTHORITY_BYTES,
)
return _validate_campaign_bytes(
raw,
path=path,
config=config,
source=source,
expected_sha256=expected_sha256,
)
def _fsync_tree(root: Path) -> None:
files, directories = _walk_regular_evidence(root)
for path in files:
flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)
descriptor = os.open(path, flags)
try:
if not stat.S_ISREG(os.fstat(descriptor).st_mode):
raise M8L2CaptureSystemError(f"cannot fsync non-regular evidence: {path}")
os.fsync(descriptor)
finally:
os.close(descriptor)
for directory in sorted(directories, key=lambda item: len(item.parts), reverse=True):
_fsync_directory(directory)
def _inventory(root: Path, *, exclude: frozenset[str] = frozenset()) -> list[dict[str, object]]:
result: list[dict[str, object]] = []
for path in _all_regular_files(root):
relative = _relative(path, root)
if relative in exclude:
continue
result.append(
{
"path": relative,
"sha256": sha256_file(path),
"bytes": path.stat().st_size,
}
)
return result
def _write_checksums(stage: Path) -> Path:
protected = _inventory(
stage,
exclude=frozenset({_CHECKSUM_NAME, _COMPLETE_MARKER, _INSUFFICIENT_MARKER}),
)
content = "".join(f"{entry['sha256']} {entry['path']}\n" for entry in protected).encode()
path = stage / _CHECKSUM_NAME
_write_bytes_durable(path, content)
return path
def _publish_terminal(
*,
stage: Path,
target: Path,
config: M8L2StudyConfig,
protocol: Path,
session: M8L2Session,
session_id: str,
source: CaptureSourceIdentity,
runtime: _RuntimeFingerprint,
source_identity_was_injected: bool,
campaign: _CampaignAuthority,
status: SessionStatus,
reason_codes: Sequence[str],
phase_ledger: Sequence[Mapping[str, object]],
gates: Sequence[Mapping[str, object]],
results: Mapping[str, SymbolCaptureResult],
barrier_released_ns: int | None,
overlap_ns: int,
capture_finished_ns: int | None,
root_identity: tuple[int, int],
sessions_identity: tuple[int, int],
) -> M8L2SessionBundle:
observed_campaign = _verify_campaign_authority(
target.parent.parent,
root_identity=root_identity,
config=config,
source=source,
runtime=runtime,
expected_sha256=campaign.sha256,
)
if observed_campaign.raw != campaign.raw:
raise M8L2CaptureSystemError("live-L2 campaign authority bytes changed")
_verify_bundled_campaign_authority(
stage / "authority" / _CAMPAIGN_AUTHORITY_NAME,
config=config,
source=source,
expected_sha256=campaign.sha256,
)
authority_inventory = _inventory(stage)
marker_name = _COMPLETE_MARKER if status == "COMPLETE" else _INSUFFICIENT_MARKER
manifest_payload: dict[str, object] = {
"schema_version": _SESSION_SCHEMA_VERSION,
"artifact_kind": "m8_prospective_live_l2_session",
"generated_at_utc": utc_now_iso(),
"status": status,
"session_id": session_id,
"study_name": config.study.name,
"protocol_version": config.study.protocol_version,
"evidence_tier": config.study.evidence_tier,
"live_trading": False,
"session": {
"date": session.date.isoformat(),
"role": session.role,
"scheduled_start_ns": session.start_ns,
"scheduled_end_ns": session.end_ns,
"scheduled_duration_seconds": (session.end_ns - session.start_ns)
/ _NANOSECONDS_PER_SECOND,
"barrier_released_ns": barrier_released_ns,
"barrier_lateness_ns": (
max(0, barrier_released_ns - session.start_ns)
if barrier_released_ns is not None
else None
),
"capture_finished_ns": capture_finished_ns,
},
"authority": {
"campaign_authority_sha256": campaign.sha256,
"config_sha256": config.hash,
"config_source_sha256": config.source_sha256,
"protocol_sha256": M8_L2_PROTOCOL_SHA256,
"protocol_freeze_commit": M8_L2_FREEZE_COMMIT,
"runtime_commit": source.commit,
"runtime_source_tree_sha256": source.source_tree_sha256,
"runtime_dirty": source.dirty,
"runtime_fingerprint": campaign.runtime.payload(),
"runtime_fingerprint_sha256": campaign.runtime.sha256,
},
"symbols": {
symbol: _symbol_payload(result, stage_root=stage)
for symbol, result in sorted(results.items())
},
"cross_symbol_observed_overlap_seconds": overlap_ns / _NANOSECONDS_PER_SECOND,
"gates": [dict(item) for item in gates],
"reason_codes": list(sorted(set(reason_codes))),
"phase_ledger": [dict(item) for item in phase_ledger],
"artifact_inventory": authority_inventory,
"terminal_marker": {
"path": marker_name,
"bytes": "complete\\n" if status == "COMPLETE" else "terminal\\n",
},
"policy": (
"both symbols share one frozen session authority; only OBSERVED continuity intervals "
"count toward overlap; failed gates publish no research result"
),
}
manifest_path = stage / "session_manifest.json"
write_json(manifest_path, manifest_payload)
_write_checksums(stage)
_fsync_tree(stage)
_revalidate_runtime_authority(
config=config,
protocol=protocol,
expected_source=source,
expected_runtime=runtime,
source_identity_was_injected=source_identity_was_injected,
)
marker_campaign = _verify_campaign_authority(
target.parent.parent,
root_identity=root_identity,
config=config,
source=source,
runtime=runtime,
expected_sha256=campaign.sha256,
)
if marker_campaign.raw != campaign.raw:
raise M8L2CaptureSystemError(
"live-L2 campaign authority changed immediately before marker materialization"
)
marker_path = stage / marker_name
_write_bytes_durable(
marker_path,
_COMPLETE_BYTES if status == "COMPLETE" else _INSUFFICIENT_BYTES,
)
_fsync_directory(stage)
_revalidate_output_layout(
target.parent.parent,
root_identity=root_identity,
sessions_identity=sessions_identity,
target=target,
)
if target.exists() or target.is_symlink():
raise M8L2CaptureSystemError(f"refusing to overwrite existing session authority: {target}")
source_parent = stage.parent
directory_flags = os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_NOFOLLOW", 0)
source_parent_descriptor = os.open(source_parent, directory_flags)
target_parent_descriptor = os.open(target.parent, directory_flags)
try:
if (
os.fstat(source_parent_descriptor).st_dev,
os.fstat(source_parent_descriptor).st_ino,
) != (root_identity):
raise M8L2CaptureSystemError("live-L2 stage parent identity changed before rename")
if (
os.fstat(target_parent_descriptor).st_dev,
os.fstat(target_parent_descriptor).st_ino,
) != sessions_identity:
raise M8L2CaptureSystemError("live-L2 target parent identity changed before rename")
_revalidate_runtime_authority(
config=config,
protocol=protocol,
expected_source=source,
expected_runtime=runtime,
source_identity_was_injected=source_identity_was_injected,
)
final_campaign = _verify_campaign_authority(
target.parent.parent,
root_identity=root_identity,
config=config,
source=source,
runtime=runtime,
expected_sha256=campaign.sha256,
)
if final_campaign.raw != campaign.raw:
raise M8L2CaptureSystemError(
"live-L2 campaign authority changed immediately before terminal publication"
)
os.rename(
stage.name,
target.name,
src_dir_fd=source_parent_descriptor,
dst_dir_fd=target_parent_descriptor,
)
# A cross-directory rename mutates both directory entries. Durability
# therefore requires both the old and the new parent directory fsyncs.
os.fsync(source_parent_descriptor)
os.fsync(target_parent_descriptor)
finally:
os.close(target_parent_descriptor)
os.close(source_parent_descriptor)
_revalidate_output_layout(
target.parent.parent,
root_identity=root_identity,
sessions_identity=sessions_identity,
target=target,
)
return verify_m8_l2_session_bundle(target, expected_config=config)
def _retain_system_failure(
*,
stage: Path,
output_root: Path,
session_id: str,
session: M8L2Session,
error: BaseException,
) -> Path:
evidence_root = stage
try:
inventory = _inventory(stage)
write_json(
stage / "SYSTEM_FAILURE.json",
{
"schema_version": _SESSION_SCHEMA_VERSION,
"artifact_kind": "m8_live_l2_nonterminal_system_failure",
"generated_at_utc": utc_now_iso(),
"terminal": False,
"research_result": False,
"session_id": session_id,
"session_date": session.date.isoformat(),
"role": session.role,
"error_type": type(error).__name__,
"error": str(error)[:2048],
"preserved_inventory_before_record": inventory,
"policy": "incomplete evidence only; no terminal marker and no research reuse",
},
)
_fsync_tree(stage)
incomplete_root = output_root / "incomplete"
_reject_existing_symlink_components(
incomplete_root, label="live-L2 incomplete-evidence directory"
)
incomplete_root.mkdir(parents=True, exist_ok=True)
_directory_identity(incomplete_root, label="live-L2 incomplete-evidence directory")
destination = incomplete_root / f"SYSTEM_FAILURE-{session_id[:20]}-{stage.name[-12:]}"
if destination.exists():
raise FileExistsError(destination)
old_parent = stage.parent
os.rename(stage, destination)
_fsync_directory(old_parent)
_fsync_directory(incomplete_root)
evidence_root = destination
except BaseException:
# The original stage is intentionally never deleted; it may contain the
# sole copy of already-received raw frames.
evidence_root = stage
return evidence_root
def _publish_or_retain_system_failure(
*,
stage: Path,
target: Path,
output_root: Path,
config: M8L2StudyConfig,
protocol: Path,
session: M8L2Session,
session_id: str,
source: CaptureSourceIdentity,
runtime: _RuntimeFingerprint,
source_identity_was_injected: bool,
campaign: _CampaignAuthority,
status: SessionStatus,
reason_codes: Sequence[str],
phase_ledger: Sequence[Mapping[str, object]],
gates: Sequence[Mapping[str, object]],
results: Mapping[str, SymbolCaptureResult],
barrier_released_ns: int | None,
overlap_ns: int,
capture_finished_ns: int | None,
root_identity: tuple[int, int],
sessions_identity: tuple[int, int],
) -> M8L2SessionBundle:
"""Publish terminally or demote every publication fault to preserved evidence."""
try:
return _publish_terminal(
stage=stage,
target=target,
config=config,
protocol=protocol,
session=session,
session_id=session_id,
source=source,
runtime=runtime,
source_identity_was_injected=source_identity_was_injected,
campaign=campaign,
status=status,
reason_codes=reason_codes,
phase_ledger=phase_ledger,
gates=gates,
results=results,
barrier_released_ns=barrier_released_ns,
overlap_ns=overlap_ns,
capture_finished_ns=capture_finished_ns,
root_identity=root_identity,
sessions_identity=sessions_identity,
)
except BaseException as error:
# If rename already succeeded but verification failed, demote our newly
# published directory. A concurrent pre-existing target is never touched
# while our private stage still exists.
evidence_stage = stage if stage.exists() else target
for marker_name in (_COMPLETE_MARKER, _INSUFFICIENT_MARKER):
marker = evidence_stage / marker_name
with suppress(OSError):
marker.unlink(missing_ok=True)
evidence_root = _retain_system_failure(
stage=evidence_stage,
output_root=output_root,
session_id=session_id,
session=session,
error=error,
)
raise M8L2CaptureSystemError(
f"live-L2 terminal publication failed without a terminal result: "
f"{type(error).__name__}: {error}",
evidence_root=evidence_root,
) from error
def _data_failure_result(
*,
symbol: str,
reason_code: str,
phase: str,
stage_root: Path,
) -> SymbolCaptureResult:
artifacts = tuple(
CapturedArtifact(path=path, kind="partial_evidence", sha256=sha256_file(path))
for path in _all_regular_files(stage_root)
)
return SymbolCaptureResult(
symbol=symbol,
capture_id=f"{symbol.lower()}-failed",
status="FAILED",
completion_reason="capture_failed",
reconstruction_status="NOT_STARTED",
messages=0,
normalized_rows=0,
reconstructed_rows=0,
excluded_rows=0,
continuity_epochs=0,
snapshot_anchors=0,
sequence_gaps=0,
quality_errors=0,
quality_warnings=0,
max_raw_frame_bytes_observed=0,
max_arrow_batch_bytes_observed=0,
first_raw_received_ns=None,
last_raw_received_ns=None,
valid_observed_intervals=(),
artifacts=artifacts,
failure_reason_code=reason_code,
failure_phase=phase,
)
async def _wait_for_start(clock: CaptureClock, start_ns: int) -> int:
while True:
now = clock.time_ns()
if now >= start_ns:
return now
# Recheck wall time at short intervals so suspend/resume and wall-clock
# adjustments cannot silently convert the absolute UTC boundary into a
# relative duration.
await clock.sleep(min(30.0, (start_ns - now) / _NANOSECONDS_PER_SECOND))
async def capture_m8_l2_session(
config: M8L2StudyConfig,
session_date: str,
output_root: str | Path,
capture_one: CaptureOne,
*,
protocol_path: str | Path | None = None,
clock: CaptureClock | None = None,
_test_source_identity: CaptureSourceIdentity | None = None,
_test_allow_injected_capture: bool = False,
) -> M8L2SessionBundle:
"""Capture both symbols under one absolute-time, immutable session authority."""
try:
canonical_config = load_m8_l2_config(config.path)
except (OSError, ValueError) as error:
raise M8L2CaptureSystemError(
"configuration is not the frozen live-L2 byte authority"
) from error
if canonical_config != config or canonical_config.hash != config.hash:
raise M8L2CaptureSystemError(
"in-memory configuration differs from the frozen live-L2 byte authority"
)
session = config.session_for_date(session_date)
if (
session.end_ns - session.start_ns
!= config.capture.duration_seconds * _NANOSECONDS_PER_SECOND
):
raise M8L2CaptureSystemError("frozen session duration and capture duration disagree")
project_root = config.path.parent.parent.resolve()
requested_protocol = (
Path(protocol_path)
if protocol_path is not None
else project_root / "docs" / "M8_L2_PROTOCOL.md"
)
if requested_protocol.is_symlink():
raise M8L2CaptureSystemError("frozen live-L2 protocol must not be a symlink")
protocol = requested_protocol.resolve()
_assert_protocol(protocol)
if _test_source_identity is not None and clock is None:
raise M8L2CaptureSystemError(
"test source identity injection requires an explicitly injected test clock"
)
if _test_allow_injected_capture and clock is None:
raise M8L2CaptureSystemError(
"test capture injection requires an explicitly injected test clock"
)
source_identity_was_injected = _test_source_identity is not None
if not source_identity_was_injected:
_assert_loaded_source_root(project_root)
if not source_identity_was_injected and not _test_allow_injected_capture:
# These private keyword-only values are unit-test seams for injected
# collectors. The production CLI supplies neither and therefore
# cannot bypass the exact adapter-origin gate.
_assert_production_capture_origin(project_root, capture_one)
source = _test_source_identity or _source_identity(project_root)
_validate_source_identity(source)
runtime = _runtime_fingerprint()
_revalidate_runtime_authority(
config=config,
protocol=protocol,
expected_source=source,
expected_runtime=runtime,
source_identity_was_injected=source_identity_was_injected,
)
root, root_identity, sessions_identity = _prepare_output_root(output_root)
campaign = _ensure_campaign_authority(
root,
root_identity=root_identity,
config=config,
source=source,
runtime=runtime,
)
session_id = _session_identity(
config=config,
session=session,
source=source,
campaign_authority_sha256=campaign.sha256,
)
target = _terminal_root(root, session, session_id)
_revalidate_output_layout(
root,
root_identity=root_identity,
sessions_identity=sessions_identity,
target=target,
)
if target.exists() or target.is_symlink():
return verify_m8_l2_session_bundle(target, expected_config=config)
stage = Path(tempfile.mkdtemp(dir=root, prefix=f".m8-l2-{session_id[:12]}-"))
authority_root = stage / "authority"
authority_root.mkdir(parents=True, exist_ok=True)
shutil.copyfile(config.path, authority_root / "m8_l2_capture_study.toml")
shutil.copyfile(protocol, authority_root / "M8_L2_PROTOCOL.md")
_write_bytes_durable(
authority_root / _CAMPAIGN_AUTHORITY_NAME,
campaign.raw,
)
capture_clock = clock or _SystemClock()
now = capture_clock.time_ns()
phase_ledger: list[dict[str, object]] = [
{"phase": "PREFLIGHT", "status": "COMPLETE", "at_ns": now}
]
if now >= session.end_ns:
phase_ledger.extend(
[
{"phase": "BOUNDARY_WAIT", "status": "MISSED", "at_ns": now},
{"phase": "DUAL_CAPTURE", "status": "NOT_RUN"},
{"phase": "SESSION_GATES", "status": "FAILED"},
{"phase": "TERMINALIZATION", "status": "READY"},
]
)
return _publish_or_retain_system_failure(
stage=stage,
target=target,
output_root=root,
config=config,
protocol=protocol,
session=session,
session_id=session_id,
source=source,
runtime=runtime,
source_identity_was_injected=source_identity_was_injected,
campaign=campaign,
status="INSUFFICIENT_DATA",
reason_codes=("MISSED_WINDOW",),
phase_ledger=phase_ledger,
gates=(),
results={},
barrier_released_ns=None,
overlap_ns=0,
capture_finished_ns=None,
root_identity=root_identity,
sessions_identity=sessions_identity,
)
try:
barrier_ns = await _wait_for_start(capture_clock, session.start_ns)
except asyncio.CancelledError:
# Before the declared interval, cancellation is restartable and does not
# consume or terminalize the future session.
raise
if barrier_ns >= session.end_ns:
try:
_revalidate_runtime_authority(
config=config,
protocol=protocol,
expected_source=source,
expected_runtime=runtime,
source_identity_was_injected=source_identity_was_injected,
)
_verify_campaign_authority(
root,
root_identity=root_identity,
config=config,
source=source,
runtime=runtime,
expected_sha256=campaign.sha256,
)
except BaseException as error:
evidence_root = _retain_system_failure(
stage=stage,
output_root=root,
session_id=session_id,
session=session,
error=error,
)
raise M8L2CaptureSystemError(
"live-L2 authority changed while waiting for the frozen window",
evidence_root=evidence_root,
) from error
phase_ledger.extend(
[
{"phase": "BOUNDARY_WAIT", "status": "MISSED", "at_ns": barrier_ns},
{"phase": "DUAL_CAPTURE", "status": "NOT_RUN"},
{"phase": "SESSION_GATES", "status": "FAILED"},
{"phase": "TERMINALIZATION", "status": "READY"},
]
)
return _publish_or_retain_system_failure(
stage=stage,
target=target,
output_root=root,
config=config,
protocol=protocol,
session=session,
session_id=session_id,
source=source,
runtime=runtime,
source_identity_was_injected=source_identity_was_injected,
campaign=campaign,
status="INSUFFICIENT_DATA",
reason_codes=("MISSED_WINDOW",),
phase_ledger=phase_ledger,
gates=(),
results={},
barrier_released_ns=barrier_ns,
overlap_ns=0,
capture_finished_ns=None,
root_identity=root_identity,
sessions_identity=sessions_identity,
)
phase_ledger.append({"phase": "BOUNDARY_WAIT", "status": "COMPLETE", "at_ns": barrier_ns})
try:
_revalidate_runtime_authority(
config=config,
protocol=protocol,
expected_source=source,
expected_runtime=runtime,
source_identity_was_injected=source_identity_was_injected,
)
_verify_campaign_authority(
root,
root_identity=root_identity,
config=config,
source=source,
runtime=runtime,
expected_sha256=campaign.sha256,
)
except BaseException as error:
evidence_root = _retain_system_failure(
stage=stage,
output_root=root,
session_id=session_id,
session=session,
error=error,
)
raise M8L2CaptureSystemError(
"live-L2 authority changed before dual-symbol capture",
evidence_root=evidence_root,
) from error
tasks: dict[str, asyncio.Task[SymbolCaptureResult]] = {}
for symbol in config.study.symbols:
symbol_root = stage / "symbols" / symbol
symbol_root.mkdir(parents=True, exist_ok=True)
tasks[symbol] = asyncio.create_task(
capture_one(
symbol=symbol,
scheduled_start_ns=session.start_ns,
scheduled_end_ns=session.end_ns,
stage_root=symbol_root,
limits=config.capture,
session_id=session_id,
),
name=f"m8-l2-{session.date.isoformat()}-{symbol}",
)
outer_canceled = False
try:
outcomes = await asyncio.gather(*tasks.values(), return_exceptions=True)
except asyncio.CancelledError:
outer_canceled = True
for task in tasks.values():
task.cancel()
outcomes = await asyncio.gather(*tasks.values(), return_exceptions=True)
capture_finished_ns = capture_clock.time_ns()
results: dict[str, SymbolCaptureResult] = {}
data_reasons: list[str] = []
system_errors: list[BaseException] = []
for symbol, outcome in zip(tasks, outcomes, strict=True):
symbol_root = stage / "symbols" / symbol
if isinstance(outcome, SymbolCaptureResult):
try:
results[symbol] = _validate_symbol_result(
outcome,
expected_symbol=symbol,
symbol_root=symbol_root,
session=session,
)
except BaseException as error:
system_errors.append(error)
elif isinstance(outcome, M8L2DataFailure):
data_reasons.append(outcome.reason_code)
partial = outcome.partial_result
if partial is None:
partial = _data_failure_result(
symbol=symbol,
reason_code=outcome.reason_code,
phase=outcome.phase,
stage_root=symbol_root,
)
try:
results[symbol] = _validate_symbol_result(
partial,
expected_symbol=symbol,
symbol_root=symbol_root,
session=session,
)
except BaseException as error:
system_errors.append(error)
elif isinstance(outcome, asyncio.CancelledError):
data_reasons.append("CAPTURE_CANCELED")
results[symbol] = _data_failure_result(
symbol=symbol,
reason_code="CAPTURE_CANCELED",
phase="DUAL_CAPTURE",
stage_root=symbol_root,
)
elif isinstance(outcome, BaseException):
system_errors.append(outcome)
if outer_canceled:
data_reasons.append("CAPTURE_CANCELED")
if system_errors:
primary = system_errors[0]
evidence_root = _retain_system_failure(
stage=stage,
output_root=root,
session_id=session_id,
session=session,
error=primary,
)
raise M8L2CaptureSystemError(
f"live-L2 capture ended in a nonterminal system failure: {type(primary).__name__}: {primary}",
evidence_root=evidence_root,
) from primary
if capture_finished_ns < session.end_ns and any(
result.status == "COMPLETE" for result in results.values()
):
early_completion_error = M8L2CaptureSystemError(
"single-symbol capture reported success before the frozen scheduled end"
)
evidence_root = _retain_system_failure(
stage=stage,
output_root=root,
session_id=session_id,
session=session,
error=early_completion_error,
)
raise M8L2CaptureSystemError(
str(early_completion_error), evidence_root=evidence_root
) from early_completion_error
phase_ledger.append(
{
"phase": "DUAL_CAPTURE",
"status": "FAILED" if data_reasons else "COMPLETE",
"symbols_completed": sorted(results),
}
)
try:
_revalidate_runtime_authority(
config=config,
protocol=protocol,
expected_source=source,
expected_runtime=runtime,
source_identity_was_injected=source_identity_was_injected,
)
_verify_campaign_authority(
root,
root_identity=root_identity,
config=config,
source=source,
runtime=runtime,
expected_sha256=campaign.sha256,
)
gates, gate_reasons, overlap_ns = _evaluate_gates(results, config=config)
except BaseException as error:
evidence_root = _retain_system_failure(
stage=stage,
output_root=root,
session_id=session_id,
session=session,
error=error,
)
raise M8L2CaptureSystemError(
"live-L2 authority/gate evaluation ended in a nonterminal system failure",
evidence_root=evidence_root,
) from error
all_reasons = sorted(set((*data_reasons, *gate_reasons)))
status: SessionStatus = "COMPLETE" if not all_reasons else "INSUFFICIENT_DATA"
phase_ledger.extend(
[
{
"phase": "SESSION_GATES",
"status": "COMPLETE" if status == "COMPLETE" else "FAILED",
},
{"phase": "TERMINALIZATION", "status": "READY"},
]
)
return _publish_or_retain_system_failure(
stage=stage,
target=target,
output_root=root,
config=config,
protocol=protocol,
session=session,
session_id=session_id,
source=source,
runtime=runtime,
source_identity_was_injected=source_identity_was_injected,
campaign=campaign,
status=status,
reason_codes=all_reasons,
phase_ledger=phase_ledger,
gates=gates,
results=results,
barrier_released_ns=barrier_ns,
overlap_ns=overlap_ns,
capture_finished_ns=capture_finished_ns,
root_identity=root_identity,
sessions_identity=sessions_identity,
)
def _safe_checksum_relative(value: str) -> str:
candidate = PurePosixPath(value)
if (
not value
or candidate.is_absolute()
or ".." in candidate.parts
or value != candidate.as_posix()
or "\n" in value
or "\r" in value
):
raise M8L2VerificationError(f"unsafe checksum path: {value!r}")
return value
def _parse_checksums(path: Path) -> dict[str, str]:
try:
lines = path.read_text(encoding="ascii").splitlines()
except (OSError, UnicodeError) as error:
raise M8L2VerificationError(f"cannot read checksum authority: {path}") from error
result: dict[str, str] = {}
for line in lines:
if len(line) < 67 or line[64:66] != " ":
raise M8L2VerificationError("checksum authority has a malformed line")
digest = line[:64]
relative = _safe_checksum_relative(line[66:])
if not _is_lower_sha256(digest):
raise M8L2VerificationError("checksum authority contains an invalid digest")
if relative in result:
raise M8L2VerificationError(f"checksum authority repeats {relative}")
result[relative] = digest
if not result:
raise M8L2VerificationError("checksum authority must protect at least one file")
return result
def _manifest_object(value: object, label: str) -> Mapping[str, Any]:
if not isinstance(value, Mapping) or not all(type(key) is str for key in value):
raise M8L2VerificationError(f"{label} must be a JSON object")
return cast(Mapping[str, Any], value)
def _verification_json(path: Path, label: str) -> object:
try:
return _strict_json(path, label=label)
except M8L2CaptureSystemError as error:
raise M8L2VerificationError(str(error)) from error
def _symbol_result_from_manifest(
value: object,
*,
symbol: str,
root: Path,
session: M8L2Session,
) -> SymbolCaptureResult:
payload = _manifest_object(value, f"symbols.{symbol}")
def integer(name: str) -> int:
raw = payload.get(name)
if type(raw) is not int or raw < 0:
raise M8L2VerificationError(f"symbols.{symbol}.{name} is invalid")
return raw
intervals_raw = payload.get("valid_observed_intervals")
if not isinstance(intervals_raw, list):
raise M8L2VerificationError(f"symbols.{symbol} intervals must be an array")
intervals: list[ObservedInterval] = []
for index, raw in enumerate(intervals_raw):
entry = _manifest_object(raw, f"symbols.{symbol}.intervals[{index}]")
continuity_id = entry.get("continuity_id")
start = entry.get("start_received_ns")
end = entry.get("end_received_ns_exclusive")
if type(continuity_id) is not str or type(start) is not int or type(end) is not int:
raise M8L2VerificationError("session manifest has a malformed OBSERVED interval")
try:
interval = ObservedInterval(continuity_id, start, end)
except ValueError as error:
raise M8L2VerificationError(
"session manifest has an invalid OBSERVED interval"
) from error
if dict(entry) != interval.to_dict():
raise M8L2VerificationError("session OBSERVED interval derived fields are inconsistent")
intervals.append(interval)
artifacts_raw = payload.get("artifacts")
if not isinstance(artifacts_raw, list):
raise M8L2VerificationError(f"symbols.{symbol}.artifacts must be an array")
artifacts: list[CapturedArtifact] = []
for index, raw in enumerate(artifacts_raw):
entry = _manifest_object(raw, f"symbols.{symbol}.artifacts[{index}]")
if set(entry) != {"path", "kind", "sha256", "bytes"}:
raise M8L2VerificationError("session symbol artifact entry is not exact")
relative_value = entry.get("path")
kind = entry.get("kind")
digest = entry.get("sha256")
size = entry.get("bytes")
if (
type(relative_value) is not str
or type(kind) is not str
or type(digest) is not str
or not _is_lower_sha256(digest)
or type(size) is not int
or size < 0
):
raise M8L2VerificationError("session symbol artifact entry is malformed")
relative = _safe_checksum_relative(relative_value)
parts = PurePosixPath(relative).parts
if len(parts) < 3 or parts[:2] != ("symbols", symbol):
raise M8L2VerificationError("session symbol artifact escapes its symbol coordinate")
path = root.joinpath(*parts)
if path.stat().st_size != size:
raise M8L2VerificationError("session symbol artifact size differs from its claim")
artifacts.append(CapturedArtifact(path=path, kind=kind, sha256=digest))
status = payload.get("status")
reconstruction = payload.get("reconstruction_status")
if status not in {"COMPLETE", "FAILED"} or reconstruction not in {
"LIVE",
"GAPPED",
"INVALID",
"NOT_STARTED",
}:
raise M8L2VerificationError("session symbol status is invalid")
capture_id = payload.get("capture_id")
completion_reason = payload.get("completion_reason")
first = payload.get("first_raw_received_ns")
last = payload.get("last_raw_received_ns")
failure_reason = payload.get("failure_reason_code")
failure_phase = payload.get("failure_phase")
if (
type(capture_id) is not str
or type(completion_reason) is not str
or (first is not None and type(first) is not int)
or (last is not None and type(last) is not int)
or (failure_reason is not None and type(failure_reason) is not str)
or (failure_phase is not None and type(failure_phase) is not str)
):
raise M8L2VerificationError("session symbol scalar claims are invalid")
result = SymbolCaptureResult(
symbol=symbol,
capture_id=capture_id,
status=cast(CaptureStatus, status),
completion_reason=completion_reason,
reconstruction_status=cast(ReconstructionStatus, reconstruction),
messages=integer("messages"),
normalized_rows=integer("normalized_rows"),
reconstructed_rows=integer("reconstructed_rows"),
excluded_rows=integer("excluded_rows"),
continuity_epochs=integer("continuity_epochs"),
snapshot_anchors=integer("snapshot_anchors"),
sequence_gaps=integer("sequence_gaps"),
quality_errors=integer("quality_errors"),
quality_warnings=integer("quality_warnings"),
max_raw_frame_bytes_observed=integer("max_raw_frame_bytes_observed"),
max_arrow_batch_bytes_observed=integer("max_arrow_batch_bytes_observed"),
first_raw_received_ns=first,
last_raw_received_ns=last,
valid_observed_intervals=tuple(intervals),
artifacts=tuple(artifacts),
failure_reason_code=failure_reason,
failure_phase=failure_phase,
)
try:
validated = _validate_symbol_result(
result,
expected_symbol=symbol,
symbol_root=root / "symbols" / symbol,
session=session,
)
except M8L2CaptureError as error:
raise M8L2VerificationError(str(error)) from error
if dict(payload) != _symbol_payload(validated, stage_root=root):
raise M8L2VerificationError("session symbol payload is not its canonical claim projection")
return validated
def verify_m8_l2_session_bundle(
bundle_dir: str | Path,
*,
expected_config: M8L2StudyConfig | None = None,
) -> M8L2SessionBundle:
"""Verify marker bytes, exact physical inventory, checksums, and frozen authority."""
requested_root = Path(bundle_dir)
try:
_reject_existing_symlink_components(requested_root, label="session bundle path")
except M8L2CaptureSystemError as error:
raise M8L2VerificationError(str(error)) from error
root = requested_root.absolute()
try:
if not stat.S_ISDIR(root.lstat().st_mode):
raise M8L2VerificationError(f"session bundle is not a regular directory: {root}")
files, _ = _walk_regular_evidence(root)
except (OSError, M8L2CaptureSystemError) as error:
raise M8L2VerificationError(f"invalid session bundle filesystem: {error}") from error
if root.parent.name != "sessions":
raise M8L2VerificationError("session bundle parent is not the canonical sessions directory")
manifest_path = root / "session_manifest.json"
checksum_path = root / _CHECKSUM_NAME
regular_paths = {_relative(path, root): path for path in files}
if "session_manifest.json" not in regular_paths or _CHECKSUM_NAME not in regular_paths:
raise M8L2VerificationError("session bundle lacks its manifest or checksum authority")
checksums = _parse_checksums(checksum_path)
marker_candidates = [
root / name for name in (_COMPLETE_MARKER, _INSUFFICIENT_MARKER) if name in regular_paths
]
if len(marker_candidates) != 1:
raise M8L2VerificationError("session bundle must have exactly one terminal marker")
marker_path = marker_candidates[0]
expected_marker_bytes = (
_COMPLETE_BYTES if marker_path.name == _COMPLETE_MARKER else _INSUFFICIENT_BYTES
)
if marker_path.read_bytes() != expected_marker_bytes:
raise M8L2VerificationError("session terminal marker bytes differ from the contract")
actual = set(regular_paths)
expected = set(checksums) | {_CHECKSUM_NAME, marker_path.name}
if actual != expected:
raise M8L2VerificationError(
"session physical inventory differs from its checksum authority "
f"(missing={sorted(expected - actual)}, extra={sorted(actual - expected)})"
)
for relative, digest in checksums.items():
path = root / relative
if sha256_file(path) != digest:
raise M8L2VerificationError(f"checksum mismatch for {relative}")
payload = _manifest_object(
_verification_json(manifest_path, "session manifest"), "session manifest"
)
if set(payload) != {
"schema_version",
"artifact_kind",
"generated_at_utc",
"status",
"session_id",
"study_name",
"protocol_version",
"evidence_tier",
"live_trading",
"session",
"authority",
"symbols",
"cross_symbol_observed_overlap_seconds",
"gates",
"reason_codes",
"phase_ledger",
"artifact_inventory",
"terminal_marker",
"policy",
}:
raise M8L2VerificationError("session manifest keys differ from the exact schema")
if payload.get("schema_version") != _SESSION_SCHEMA_VERSION:
raise M8L2VerificationError("unsupported live-L2 session manifest schema")
if payload.get("artifact_kind") != "m8_prospective_live_l2_session":
raise M8L2VerificationError("session artifact kind is invalid")
status = payload.get("status")
if status not in {"COMPLETE", "INSUFFICIENT_DATA"}:
raise M8L2VerificationError("session manifest has an unsupported status")
expected_marker = _COMPLETE_MARKER if status == "COMPLETE" else _INSUFFICIENT_MARKER
if marker_path.name != expected_marker:
raise M8L2VerificationError("terminal marker disagrees with session status")
session_id = payload.get("session_id")
if type(session_id) is not str or not _is_lower_sha256(session_id):
raise M8L2VerificationError("session manifest has an invalid session_id")
authority = _manifest_object(payload.get("authority"), "session authority")
if set(authority) != {
"campaign_authority_sha256",
"config_sha256",
"config_source_sha256",
"protocol_sha256",
"protocol_freeze_commit",
"runtime_commit",
"runtime_source_tree_sha256",
"runtime_dirty",
"runtime_fingerprint",
"runtime_fingerprint_sha256",
}:
raise M8L2VerificationError("session authority keys differ from the exact schema")
config_path = root / "authority" / "m8_l2_capture_study.toml"
protocol_path = root / "authority" / "M8_L2_PROTOCOL.md"
campaign_path = root / "authority" / _CAMPAIGN_AUTHORITY_NAME
try:
bundled_config = load_m8_l2_config(config_path)
except (OSError, ValueError) as error:
raise M8L2VerificationError("bundled live-L2 config is not the frozen authority") from error
if authority.get("config_source_sha256") != M8_L2_CONFIG_SOURCE_SHA256:
raise M8L2VerificationError("session config bytes are not the frozen authority")
if authority.get("config_sha256") != bundled_config.hash:
raise M8L2VerificationError("session config semantic hash differs from bundled bytes")
if authority.get("protocol_sha256") != M8_L2_PROTOCOL_SHA256:
raise M8L2VerificationError("session protocol bytes are not the frozen authority")
if authority.get("protocol_freeze_commit") != M8_L2_FREEZE_COMMIT:
raise M8L2VerificationError("session freeze commit is not the declared authority")
source = CaptureSourceIdentity(
commit=str(authority.get("runtime_commit")),
source_tree_sha256=str(authority.get("runtime_source_tree_sha256")),
dirty=authority.get("runtime_dirty") is not False,
)
try:
_validate_source_identity(source)
except M8L2CaptureSystemError as error:
raise M8L2VerificationError(f"invalid runtime source identity: {error}") from error
campaign_sha256 = authority.get("campaign_authority_sha256")
if type(campaign_sha256) is not str or not _is_lower_sha256(campaign_sha256):
raise M8L2VerificationError("session campaign authority digest is invalid")
try:
bundled_campaign = _verify_bundled_campaign_authority(
campaign_path,
config=bundled_config,
source=source,
expected_sha256=campaign_sha256,
)
except M8L2CaptureSystemError as error:
raise M8L2VerificationError(f"invalid bundled campaign authority: {error}") from error
try:
manifest_runtime = _runtime_from_recorded(
authority.get("runtime_fingerprint"),
authority.get("runtime_fingerprint_sha256"),
)
except M8L2CaptureSystemError as error:
raise M8L2VerificationError(f"invalid session runtime fingerprint: {error}") from error
if manifest_runtime != bundled_campaign.runtime:
raise M8L2VerificationError(
"session runtime fingerprint differs from bundled campaign authority"
)
campaign_relative = f"authority/{_CAMPAIGN_AUTHORITY_NAME}"
if checksums.get(campaign_relative) != campaign_sha256:
raise M8L2VerificationError(
"session checksum authority does not bind the campaign authority digest"
)
if sha256_file(config_path) != M8_L2_CONFIG_SOURCE_SHA256:
raise M8L2VerificationError("bundled live-L2 config bytes are corrupt")
if sha256_file(protocol_path) != M8_L2_PROTOCOL_SHA256:
raise M8L2VerificationError("bundled live-L2 protocol bytes are corrupt")
if expected_config is not None:
try:
expected_canonical = load_m8_l2_config(expected_config.path)
except (OSError, ValueError) as error:
raise M8L2VerificationError("caller config is not backed by frozen bytes") from error
if expected_canonical != expected_config:
raise M8L2VerificationError("caller config object differs from its frozen bytes")
if (
expected_config.source_sha256 != bundled_config.source_sha256
or expected_config.hash != bundled_config.hash
):
raise M8L2VerificationError("caller config semantics differ from the session authority")
if (
payload.get("study_name") != bundled_config.study.name
or payload.get("protocol_version") != bundled_config.study.protocol_version
or payload.get("evidence_tier") != bundled_config.study.evidence_tier
or payload.get("live_trading") is not False
):
raise M8L2VerificationError("session study claims differ from frozen configuration")
inventory_raw = payload.get("artifact_inventory")
if not isinstance(inventory_raw, list):
raise M8L2VerificationError("session artifact inventory must be an array")
inventory: dict[str, tuple[str, int]] = {}
for index, item in enumerate(inventory_raw):
entry = _manifest_object(item, f"artifact_inventory[{index}]")
if set(entry) != {"path", "sha256", "bytes"}:
raise M8L2VerificationError("session artifact inventory entry is not exact")
relative = _safe_checksum_relative(str(entry.get("path")))
digest = str(entry.get("sha256"))
size = entry.get("bytes")
if not _is_lower_sha256(digest) or isinstance(size, bool) or not isinstance(size, int):
raise M8L2VerificationError("session artifact inventory entry is malformed")
if relative in inventory:
raise M8L2VerificationError(f"session artifact inventory repeats {relative}")
inventory[relative] = (digest, size)
if list(inventory) != sorted(inventory):
raise M8L2VerificationError("session artifact inventory is not canonically ordered")
expected_inventory = set(checksums) - {"session_manifest.json"}
if set(inventory) != expected_inventory:
raise M8L2VerificationError("manifest artifact inventory is not exact")
for relative, (digest, size) in inventory.items():
path = root / relative
if checksums.get(relative) != digest or path.stat().st_size != size:
raise M8L2VerificationError(f"manifest inventory metadata differs for {relative}")
reasons_raw = payload.get("reason_codes")
if not isinstance(reasons_raw, list) or not all(type(item) is str for item in reasons_raw):
raise M8L2VerificationError("session reason_codes must be a string array")
reasons = tuple(cast(list[str], reasons_raw))
if list(reasons) != sorted(set(reasons)):
raise M8L2VerificationError("session reason codes are not canonical")
session_payload = _manifest_object(payload.get("session"), "session coordinates")
if set(session_payload) != {
"date",
"role",
"scheduled_start_ns",
"scheduled_end_ns",
"scheduled_duration_seconds",
"barrier_released_ns",
"barrier_lateness_ns",
"capture_finished_ns",
}:
raise M8L2VerificationError("session coordinate keys differ from the exact schema")
session_date = session_payload.get("date")
role = session_payload.get("role")
if type(session_date) is not str or type(role) is not str:
raise M8L2VerificationError("session date/role coordinates are invalid")
try:
frozen_session = bundled_config.session_for_date(session_date)
except ValueError as error:
raise M8L2VerificationError("session date is not in the frozen calendar") from error
expected_session_coordinates = {
"date": frozen_session.date.isoformat(),
"role": frozen_session.role,
"scheduled_start_ns": frozen_session.start_ns,
"scheduled_end_ns": frozen_session.end_ns,
"scheduled_duration_seconds": (frozen_session.end_ns - frozen_session.start_ns)
/ _NANOSECONDS_PER_SECOND,
}
for name, expected_value in expected_session_coordinates.items():
if session_payload.get(name) != expected_value:
raise M8L2VerificationError(f"session coordinate {name} differs from the freeze")
capture_finished = session_payload.get("capture_finished_ns")
if capture_finished is not None and type(capture_finished) is not int:
raise M8L2VerificationError("session capture_finished_ns is invalid")
recomputed_session_id = _session_identity(
config=bundled_config,
session=frozen_session,
source=source,
campaign_authority_sha256=campaign_sha256,
)
if session_id != recomputed_session_id:
raise M8L2VerificationError("session_id does not derive from the frozen authority")
expected_name = f"{frozen_session.date.isoformat()}-{frozen_session.role}-{session_id[:20]}"
if root.name != expected_name:
raise M8L2VerificationError("session bundle basename differs from its authority")
symbols_raw = _manifest_object(payload.get("symbols"), "session symbols")
if set(symbols_raw) != set(bundled_config.study.symbols) and not (
not symbols_raw and reasons == ("MISSED_WINDOW",)
):
raise M8L2VerificationError("session symbols differ from the exact frozen pair")
results: dict[str, SymbolCaptureResult] = {}
for symbol in bundled_config.study.symbols:
if symbol in symbols_raw:
results[symbol] = _symbol_result_from_manifest(
symbols_raw[symbol], symbol=symbol, root=root, session=frozen_session
)
gates_raw = payload.get("gates")
if not isinstance(gates_raw, list) or not all(isinstance(item, Mapping) for item in gates_raw):
raise M8L2VerificationError("session gates must be an array of objects")
if reasons == ("MISSED_WINDOW",):
if status != "INSUFFICIENT_DATA" or results or gates_raw:
raise M8L2VerificationError("missed-window terminal evidence is inconsistent")
overlap_ns = 0
else:
if set(results) != set(bundled_config.study.symbols):
raise M8L2VerificationError("non-missed session lacks the exact frozen symbol pair")
expected_gates, expected_reasons, overlap_ns = _evaluate_gates(
results, config=bundled_config
)
if len(expected_gates) != 29 or [dict(item) for item in gates_raw] != expected_gates:
raise M8L2VerificationError("session gates are not the exact 29 recomputed gates")
if list(reasons) != expected_reasons:
raise M8L2VerificationError("session reasons differ from recomputed gate failures")
expected_status = "COMPLETE" if not expected_reasons else "INSUFFICIENT_DATA"
if status != expected_status:
raise M8L2VerificationError("session status differs from recomputed gates")
if payload.get("cross_symbol_observed_overlap_seconds") != (
overlap_ns / _NANOSECONDS_PER_SECOND
):
raise M8L2VerificationError("cross-symbol overlap differs from recomputed intervals")
if status == "COMPLETE" and (
type(capture_finished) is not int or capture_finished < frozen_session.end_ns
):
raise M8L2VerificationError("complete session was finalized before scheduled end")
terminal_marker = _manifest_object(payload.get("terminal_marker"), "terminal marker claim")
if terminal_marker != {
"path": expected_marker,
"bytes": "complete\\n" if status == "COMPLETE" else "terminal\\n",
}:
raise M8L2VerificationError("terminal marker claim differs from exact bytes")
return M8L2SessionBundle(
root=root,
status=cast(SessionStatus, status),
session_id=session_id,
session_date=session_date,
role=role,
manifest_path=manifest_path,
manifest_sha256=checksums["session_manifest.json"],
checksum_path=checksum_path,
marker_path=marker_path,
reason_codes=reasons,
)
__all__ = [
"CaptureClock",
"CaptureOne",
"CaptureSourceIdentity",
"CapturedArtifact",
"M8L2CaptureError",
"M8L2CaptureSystemError",
"M8L2DataFailure",
"M8L2SessionBundle",
"M8L2VerificationError",
"ObservedInterval",
"SymbolCaptureResult",
"capture_m8_l2_session",
"current_m8_l2_runtime_fingerprint_sha256",
"merge_observed_intervals",
"overlapping_observed_coverage_ns",
"verify_m8_l2_session_bundle",
]
|